Coverage Report

Created: 2023-06-07 06:59

/src/botan/src/lib/math/numbertheory/reducer.cpp
Line
Count
Source (jump to first uncovered line)
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
1
Modular_Reducer::Modular_Reducer(const BigInt& mod) {
20
1
   if(mod < 0) {
21
0
      throw Invalid_Argument("Modular_Reducer: modulus must be positive");
22
0
   }
23
24
   // Left uninitialized if mod == 0
25
1
   m_mod_words = 0;
26
27
1
   if(mod > 0) {
28
1
      m_modulus = mod;
29
1
      m_mod_words = m_modulus.sig_words();
30
31
      // Compute mu = floor(2^{2k} / m)
32
1
      m_mu.set_bit(2 * BOTAN_MP_WORD_BITS * m_mod_words);
33
1
      m_mu = ct_divide(m_mu, m_modulus);
34
1
   }
35
1
}
36
37
277
BigInt Modular_Reducer::reduce(const BigInt& x) const {
38
277
   BigInt r;
39
277
   secure_vector<word> ws;
40
277
   reduce(r, x, ws);
41
277
   return r;
42
277
}
43
44
namespace {
45
46
/*
47
* Like if(cnd) x.rev_sub(...) but in const time
48
*/
49
277
void cnd_rev_sub(bool cnd, BigInt& x, const word y[], size_t y_sw, secure_vector<word>& ws) {
50
277
   if(x.sign() != BigInt::Positive) {
51
0
      throw Invalid_State("BigInt::sub_rev requires this is positive");
52
0
   }
53
54
277
   const size_t x_sw = x.sig_words();
55
56
277
   const size_t max_words = std::max(x_sw, y_sw);
57
277
   ws.resize(std::max(x_sw, y_sw));
58
277
   clear_mem(ws.data(), ws.size());
59
277
   x.grow_to(max_words);
60
61
277
   const int32_t relative_size = bigint_sub_abs(ws.data(), x.data(), x_sw, y, y_sw);
62
63
277
   x.cond_flip_sign((relative_size > 0) && cnd);
64
277
   bigint_cnd_swap(cnd, x.mutable_data(), ws.data(), max_words);
65
277
}
66
67
}  // namespace
68
69
277
void Modular_Reducer::reduce(BigInt& t1, const BigInt& x, secure_vector<word>& ws) const {
70
277
   if(&t1 == &x) {
71
0
      throw Invalid_State("Modular_Reducer arguments cannot alias");
72
0
   }
73
277
   if(m_mod_words == 0) {
74
0
      throw Invalid_State("Modular_Reducer: Never initalized");
75
0
   }
76
77
277
   const size_t x_sw = x.sig_words();
78
79
277
   if(x_sw > 2 * m_mod_words) {
80
      // too big, fall back to slow boat division
81
0
      t1 = ct_modulo(x, m_modulus);
82
0
      return;
83
0
   }
84
85
277
   t1 = x;
86
277
   t1.set_sign(BigInt::Positive);
87
277
   t1 >>= (BOTAN_MP_WORD_BITS * (m_mod_words - 1));
88
89
277
   t1.mul(m_mu, ws);
90
277
   t1 >>= (BOTAN_MP_WORD_BITS * (m_mod_words + 1));
91
92
   // TODO add masked mul to avoid computing high bits
93
277
   t1.mul(m_modulus, ws);
94
277
   t1.mask_bits(BOTAN_MP_WORD_BITS * (m_mod_words + 1));
95
96
277
   t1.rev_sub(x.data(), std::min(x_sw, m_mod_words + 1), ws);
97
98
   /*
99
   * If t1 < 0 then we must add b^(k+1) where b = 2^w. To avoid a
100
   * side channel perform the addition unconditionally, with ws set
101
   * to either b^(k+1) or else 0.
102
   */
103
277
   const word t1_neg = t1.is_negative();
104
105
277
   if(ws.size() < m_mod_words + 2) {
106
0
      ws.resize(m_mod_words + 2);
107
0
   }
108
277
   clear_mem(ws.data(), ws.size());
109
277
   ws[m_mod_words + 1] = t1_neg;
110
111
277
   t1.add(ws.data(), m_mod_words + 2, BigInt::Positive);
112
113
   // Per HAC this step requires at most 2 subtractions
114
277
   t1.ct_reduce_below(m_modulus, ws, 2);
115
116
277
   cnd_rev_sub(t1.is_nonzero() && x.is_negative(), t1, m_modulus.data(), m_modulus.size(), ws);
117
277
}
118
119
}  // namespace Botan