Botan  2.4.0
Crypto and TLS for C++11
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/workfactor.h>
9 #include <algorithm>
10 #include <cmath>
11 
12 namespace Botan {
13 
14 size_t ecp_work_factor(size_t bits)
15  {
16  return bits / 2;
17  }
18 
19 size_t if_work_factor(size_t bits)
20  {
21  // RFC 3766: k * e^((1.92 + o(1)) * cubrt(ln(n) * (ln(ln(n)))^2))
22  // It estimates k at .02 and o(1) to be effectively zero for sizes of interest
23  const double k = .02;
24 
25  // approximates natural logarithm of p
26  const double log2_e = std::log2(std::exp(1));
27  const double log_p = bits / log2_e;
28 
29  const double est = 1.92 * std::pow(log_p * std::log(log_p) * std::log(log_p), 1.0/3.0);
30 
31  return static_cast<size_t>(std::log2(k) + log2_e * est);
32  }
33 
34 size_t dl_work_factor(size_t bits)
35  {
36  // Lacking better estimates...
37  return if_work_factor(bits);
38  }
39 
40 size_t dl_exponent_size(size_t bits)
41  {
42  /*
43  This uses a slightly tweaked version of the standard work factor
44  function above. It assumes k is 1 (thus overestimating the strength
45  of the prime group by 5-6 bits), and always returns at least 128 bits
46  (this only matters for very small primes).
47  */
48  const size_t MIN_WORKFACTOR = 64;
49  const double log2_e = std::log2(std::exp(1));
50  const double log_p = bits / log2_e;
51 
52  const double strength = 1.92 * std::pow(log_p, 1.0/3.0) * std::pow(std::log(log_p), 2.0/3.0);
53 
54  return 2 * std::max<size_t>(MIN_WORKFACTOR, static_cast<size_t>(log2_e * strength));
55  }
56 
57 }
size_t if_work_factor(size_t bits)
Definition: workfactor.cpp:19
size_t ecp_work_factor(size_t bits)
Definition: workfactor.cpp:14
Definition: alg_id.cpp:13
size_t dl_exponent_size(size_t bits)
Definition: workfactor.cpp:40
size_t dl_work_factor(size_t bits)
Definition: workfactor.cpp:34