Coverage Report

Created: 2021-05-04 09:02

/src/botan/src/lib/pubkey/workfactor.cpp
Line
Count
Source (jump to first uncovered line)
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
12
namespace Botan {
13
14
size_t ecp_work_factor(size_t bits)
15
0
   {
16
0
   return bits / 2;
17
0
   }
18
19
namespace {
20
21
size_t nfs_workfactor(size_t bits, double log2_k)
22
340
   {
23
   // approximates natural logarithm of an integer of given bitsize
24
340
   const double log2_e = 1.44269504088896340736;
25
340
   const double log_p = bits / log2_e;
26
27
340
   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
340
   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
340
   return static_cast<size_t>(log2_k + log2_e * est);
34
340
   }
35
36
}
37
38
size_t if_work_factor(size_t bits)
39
340
   {
40
   // RFC 3766 estimates k at .02 and o(1) to be effectively zero for sizes of interest
41
42
340
   const double log2_k = -5.6438; // log2(.02)
43
340
   return nfs_workfactor(bits, log2_k);
44
340
   }
45
46
size_t dl_work_factor(size_t bits)
47
340
   {
48
   // Lacking better estimates...
49
340
   return if_work_factor(bits);
50
340
   }
51
52
size_t dl_exponent_size(size_t bits)
53
340
   {
54
340
   if(bits == 0)
55
0
      return 0;
56
340
   if(bits <= 256)
57
65
      return bits - 1;
58
275
   if(bits <= 1024)
59
77
      return 192;
60
198
   if(bits <= 1536)
61
139
      return 224;
62
59
   if(bits <= 2048)
63
59
      return 256;
64
0
   if(bits <= 4096)
65
0
      return 384;
66
0
   return 512;
67
0
   }
68
69
}