Coverage Report

Created: 2023-12-08 07:00

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