Coverage Report

Created: 2019-12-03 15:21

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