Botan 3.0.0-alpha0
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 <algorithm>
10#include <cmath>
11
12namespace Botan {
13
14size_t ecp_work_factor(size_t bits)
15 {
16 return bits / 2;
17 }
18
19namespace {
20
21size_t nfs_workfactor(size_t bits, double log2_k)
22 {
23 // approximates natural logarithm of an integer of given bitsize
24 const double log2_e = 1.44269504088896340736;
25 const double log_p = bits / log2_e;
26
27 const double log_log_p = std::log(log_p);
28
29 // RFC 3766: k * e^((1.92 + o(1)) * cubrt(ln(n) * (ln(ln(n)))^2))
30 const double est = 1.92 * std::pow(log_p * log_log_p * log_log_p, 1.0/3.0);
31
32 // return log2 of the workfactor
33 return static_cast<size_t>(log2_k + log2_e * est);
34 }
35
36}
37
38size_t if_work_factor(size_t bits)
39 {
40 if(bits < 512)
41 return 0;
42
43 // RFC 3766 estimates k at .02 and o(1) to be effectively zero for sizes of interest
44
45 const double log2_k = -5.6438; // log2(.02)
46 return nfs_workfactor(bits, log2_k);
47 }
48
49size_t dl_work_factor(size_t bits)
50 {
51 // Lacking better estimates...
52 return if_work_factor(bits);
53 }
54
55size_t dl_exponent_size(size_t bits)
56 {
57 if(bits == 0)
58 return 0;
59 if(bits <= 256)
60 return bits - 1;
61 if(bits <= 1024)
62 return 192;
63 if(bits <= 1536)
64 return 224;
65 if(bits <= 2048)
66 return 256;
67 if(bits <= 4096)
68 return 384;
69 return 512;
70 }
71
72}
Definition: alg_id.cpp:13
size_t ecp_work_factor(size_t bits)
Definition: workfactor.cpp:14
size_t dl_work_factor(size_t bits)
Definition: workfactor.cpp:49
size_t dl_exponent_size(size_t bits)
Definition: workfactor.cpp:55
size_t if_work_factor(size_t bits)
Definition: workfactor.cpp:38