Botan 3.5.0
Crypto and TLS for C&
workfactor.cpp
Go to the documentation of this file.
1/*
2* Public Key Work Factor Functions
3* (C) 1999-2007,2012 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/internal/workfactor.h>
9#include <cmath>
10#include <numbers>
11
12namespace Botan {
13
14size_t ecp_work_factor(size_t bits) {
15 return bits / 2;
16}
17
18namespace {
19
20size_t nfs_workfactor(size_t bits, double log2_k) {
21 // approximates natural logarithm of an integer of given bitsize
22 const double log_p = bits / std::numbers::log2e;
23
24 const double log_log_p = std::log(log_p);
25
26 // RFC 3766: k * e^((1.92 + o(1)) * cubrt(ln(n) * (ln(ln(n)))^2))
27 const double est = 1.92 * std::pow(log_p * log_log_p * log_log_p, 1.0 / 3.0);
28
29 // return log2 of the workfactor
30 return static_cast<size_t>(log2_k + std::numbers::log2e * est);
31}
32
33} // namespace
34
35size_t if_work_factor(size_t bits) {
36 if(bits < 512) {
37 return 0;
38 }
39
40 // RFC 3766 estimates k at .02 and o(1) to be effectively zero for sizes of interest
41
42 const double log2_k = -5.6438; // log2(.02)
43 return nfs_workfactor(bits, log2_k);
44}
45
46size_t dl_work_factor(size_t bits) {
47 // Lacking better estimates...
48 return if_work_factor(bits);
49}
50
51size_t dl_exponent_size(size_t bits) {
52 if(bits == 0) {
53 return 0;
54 }
55 if(bits <= 256) {
56 return bits - 1;
57 }
58 if(bits <= 1024) {
59 return 192;
60 }
61 if(bits <= 1536) {
62 return 224;
63 }
64 if(bits <= 2048) {
65 return 256;
66 }
67 if(bits <= 4096) {
68 return 384;
69 }
70 return 512;
71}
72
73} // namespace Botan
size_t ecp_work_factor(size_t bits)
size_t dl_work_factor(size_t bits)
size_t dl_exponent_size(size_t bits)
size_t if_work_factor(size_t bits)