• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 12873787387

20 Jan 2025 06:20PM UTC coverage: 91.212% (+0.003%) from 91.209%
12873787387

push

github

web-flow
Merge pull request #4575 from randombit/jack/cleanup-up-includes

Clean up includes

93551 of 102564 relevant lines covered (91.21%)

11622221.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.08
/src/lib/math/numbertheory/reducer.cpp
1
/*
2
* Modular Reducer
3
* (C) 1999-2011,2018 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include <botan/reducer.h>
9

10
#include <botan/internal/ct_utils.h>
11
#include <botan/internal/divide.h>
12
#include <botan/internal/mp_core.h>
13

14
namespace Botan {
15

16
/*
17
* Modular_Reducer Constructor
18
*/
19
Modular_Reducer::Modular_Reducer(const BigInt& mod) {
127,298✔
20
   if(mod < 0) {
127,298✔
21
      throw Invalid_Argument("Modular_Reducer: modulus must be positive");
9✔
22
   }
23

24
   // Left uninitialized if mod == 0
25
   m_mod_words = 0;
127,289✔
26

27
   if(mod > 0) {
127,289✔
28
      m_modulus = mod;
126,930✔
29
      m_mod_words = m_modulus.sig_words();
126,930✔
30

31
      // Compute mu = floor(2^{2k} / m)
32
      m_mu.set_bit(2 * BOTAN_MP_WORD_BITS * m_mod_words);
126,930✔
33
      m_mu = ct_divide(m_mu, m_modulus);
380,790✔
34
   }
35
}
127,298✔
36

37
BigInt Modular_Reducer::reduce(const BigInt& x) const {
6,379,809✔
38
   BigInt r;
6,379,809✔
39
   secure_vector<word> ws;
6,379,809✔
40
   reduce(r, x, ws);
6,379,809✔
41
   return r;
6,379,809✔
42
}
6,379,809✔
43

44
BigInt Modular_Reducer::square(const BigInt& x) const {
2,290,052✔
45
   secure_vector<word> ws;
2,290,052✔
46
   BigInt x2 = x;
2,290,052✔
47
   x2.square(ws);
2,290,052✔
48
   BigInt r;
2,290,052✔
49
   reduce(r, x2, ws);
2,290,052✔
50
   return r;
2,290,052✔
51
}
4,580,104✔
52

53
namespace {
54

55
/*
56
* Like if(cnd) x.rev_sub(...) but in const time
57
*/
58
void cnd_rev_sub(bool cnd, BigInt& x, const word y[], size_t y_sw, secure_vector<word>& ws) {
8,668,320✔
59
   if(x.sign() != BigInt::Positive) {
8,668,320✔
60
      throw Invalid_State("BigInt::sub_rev requires this is positive");
×
61
   }
62

63
   const size_t x_sw = x.sig_words();
8,668,320✔
64

65
   const size_t max_words = std::max(x_sw, y_sw);
8,668,320✔
66
   ws.resize(std::max(x_sw, y_sw));
8,668,320✔
67
   clear_mem(ws.data(), ws.size());
8,668,320✔
68
   x.grow_to(max_words);
8,668,320✔
69

70
   const int32_t relative_size = bigint_sub_abs(ws.data(), x._data(), x_sw, y, y_sw);
8,668,320✔
71

72
   x.cond_flip_sign((relative_size > 0) && cnd);
8,668,320✔
73
   bigint_cnd_swap(static_cast<word>(cnd), x.mutable_data(), ws.data(), max_words);
8,668,320✔
74
}
8,668,320✔
75

76
}  // namespace
77

78
void Modular_Reducer::reduce(BigInt& t1, const BigInt& x, secure_vector<word>& ws) const {
8,669,861✔
79
   if(&t1 == &x) {
8,669,861✔
80
      throw Invalid_State("Modular_Reducer arguments cannot alias");
×
81
   }
82
   if(m_mod_words == 0) {
8,669,861✔
83
      throw Invalid_State("Modular_Reducer: Never initalized");
×
84
   }
85

86
   const size_t x_sw = x.sig_words();
8,669,861✔
87

88
   if(x_sw > 2 * m_mod_words) {
8,669,861✔
89
      // too big, fall back to slow boat division
90
      t1 = ct_modulo(x, m_modulus);
1,541✔
91
      return;
1,541✔
92
   }
93

94
   t1 = x;
8,668,320✔
95
   t1.set_sign(BigInt::Positive);
8,668,320✔
96
   t1 >>= (BOTAN_MP_WORD_BITS * (m_mod_words - 1));
8,668,320✔
97

98
   t1.mul(m_mu, ws);
8,668,320✔
99
   t1 >>= (BOTAN_MP_WORD_BITS * (m_mod_words + 1));
8,668,320✔
100

101
   // TODO add masked mul to avoid computing high bits
102
   t1.mul(m_modulus, ws);
8,668,320✔
103
   t1.mask_bits(BOTAN_MP_WORD_BITS * (m_mod_words + 1));
8,668,320✔
104

105
   t1.rev_sub(x._data(), std::min(x_sw, m_mod_words + 1), ws);
10,365,149✔
106

107
   /*
108
   * If t1 < 0 then we must add b^(k+1) where b = 2^w. To avoid a
109
   * side channel perform the addition unconditionally, with ws set
110
   * to either b^(k+1) or else 0.
111
   */
112
   const word t1_neg = t1.is_negative();
8,668,320✔
113

114
   if(ws.size() < m_mod_words + 2) {
8,668,320✔
115
      ws.resize(m_mod_words + 2);
36,033✔
116
   }
117
   clear_mem(ws.data(), ws.size());
8,668,320✔
118
   ws[m_mod_words + 1] = t1_neg;
8,668,320✔
119

120
   t1.add(ws.data(), m_mod_words + 2, BigInt::Positive);
8,668,320✔
121

122
   // Per HAC this step requires at most 2 subtractions
123
   t1.ct_reduce_below(m_modulus, ws, 2);
8,668,320✔
124

125
   cnd_rev_sub(t1.is_nonzero() && x.is_negative(), t1, m_modulus._data(), m_modulus.size(), ws);
25,131,379✔
126
}
127

128
}  // namespace Botan
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc