Coverage Report

Created: 2026-02-07 07:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/botan/src/lib/pubkey/workfactor.cpp
Line
Count
Source
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
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 log_p = static_cast<double>(bits) / std::numbers::log2e;
23
24
0
   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
0
   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
0
   return static_cast<size_t>(log2_k + std::numbers::log2e * est);
31
0
}
32
33
}  // namespace
34
35
0
size_t if_work_factor(size_t bits) {
36
0
   if(bits < 512) {
37
0
      return 0;
38
0
   }
39
40
   // RFC 3766 estimates k at .02 and o(1) to be effectively zero for sizes of interest
41
42
0
   const double log2_k = -5.6438;  // log2(.02)
43
0
   return nfs_workfactor(bits, log2_k);
44
0
}
45
46
0
size_t dl_work_factor(size_t bits) {
47
   // Lacking better estimates...
48
0
   return if_work_factor(bits);
49
0
}
50
51
0
size_t dl_exponent_size(size_t bits) {
52
0
   if(bits == 0) {
53
0
      return 0;
54
0
   }
55
0
   if(bits <= 256) {
56
0
      return bits - 1;
57
0
   }
58
0
   if(bits <= 1024) {
59
0
      return 192;
60
0
   }
61
0
   if(bits <= 1536) {
62
0
      return 224;
63
0
   }
64
0
   if(bits <= 2048) {
65
0
      return 256;
66
0
   }
67
0
   if(bits <= 4096) {
68
0
      return 384;
69
0
   }
70
0
   return 512;
71
0
}
72
73
}  // namespace Botan