Botan 3.4.0
Crypto and TLS for C&
monty_exp.cpp
Go to the documentation of this file.
1/*
2* Montgomery Exponentiation
3* (C) 1999-2010,2012,2018 Jack Lloyd
4* 2016 Matthias Gierlings
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#include <botan/internal/monty_exp.h>
10
11#include <botan/numthry.h>
12#include <botan/reducer.h>
13#include <botan/internal/ct_utils.h>
14#include <botan/internal/monty.h>
15#include <botan/internal/rounding.h>
16
17namespace Botan {
18
19class Montgomery_Exponentation_State final {
20 public:
21 Montgomery_Exponentation_State(const std::shared_ptr<const Montgomery_Params>& params,
22 const BigInt& g,
23 size_t window_bits,
24 bool const_time);
25
26 BigInt exponentiation(const BigInt& k, size_t max_k_bits) const;
27
28 BigInt exponentiation_vartime(const BigInt& k) const;
29
30 private:
31 std::shared_ptr<const Montgomery_Params> m_params;
32 std::vector<Montgomery_Int> m_g;
33 size_t m_window_bits;
34};
35
36Montgomery_Exponentation_State::Montgomery_Exponentation_State(const std::shared_ptr<const Montgomery_Params>& params,
37 const BigInt& g,
38 size_t window_bits,
39 bool const_time) :
40 m_params(params), m_window_bits(window_bits == 0 ? 4 : window_bits) {
41 BOTAN_ARG_CHECK(g < m_params->p(), "Montgomery base too big");
42
43 if(m_window_bits < 1 || m_window_bits > 12) { // really even 8 is too large ...
44 throw Invalid_Argument("Invalid window bits for Montgomery exponentiation");
45 }
46
47 const size_t window_size = (static_cast<size_t>(1) << m_window_bits);
48
49 m_g.reserve(window_size);
50
51 m_g.push_back(Montgomery_Int(m_params, m_params->R1(), false));
52
53 m_g.push_back(Montgomery_Int(m_params, g));
54
55 for(size_t i = 2; i != window_size; ++i) {
56 m_g.push_back(m_g[1] * m_g[i - 1]);
57 }
58
59 // Resize each element to exactly p words
60 for(size_t i = 0; i != window_size; ++i) {
61 m_g[i].fix_size();
62 if(const_time) {
63 m_g[i].const_time_poison();
64 }
65 }
66}
67
68namespace {
69
70void const_time_lookup(secure_vector<word>& output, const std::vector<Montgomery_Int>& g, size_t nibble) {
71 BOTAN_ASSERT_NOMSG(g.size() % 2 == 0); // actually a power of 2
72
73 const size_t words = output.size();
74
75 clear_mem(output.data(), output.size());
76
77 for(size_t i = 0; i != g.size(); i += 2) {
78 const secure_vector<word>& vec_0 = g[i].repr().get_word_vector();
79 const secure_vector<word>& vec_1 = g[i + 1].repr().get_word_vector();
80
81 BOTAN_ASSERT_NOMSG(vec_0.size() >= words && vec_1.size() >= words);
82
83 const auto mask_0 = CT::Mask<word>::is_equal(nibble, i);
84 const auto mask_1 = CT::Mask<word>::is_equal(nibble, i + 1);
85
86 for(size_t w = 0; w != words; ++w) {
87 output[w] |= mask_0.if_set_return(vec_0[w]);
88 output[w] |= mask_1.if_set_return(vec_1[w]);
89 }
90 }
91}
92
93} // namespace
94
95BigInt Montgomery_Exponentation_State::exponentiation(const BigInt& scalar, size_t max_k_bits) const {
96 BOTAN_DEBUG_ASSERT(scalar.bits() <= max_k_bits);
97 // TODO add a const-time implementation of above assert and use it in release builds
98
99 const size_t exp_nibbles = (max_k_bits + m_window_bits - 1) / m_window_bits;
100
101 if(exp_nibbles == 0) {
102 return BigInt::one();
103 }
104
105 secure_vector<word> e_bits(m_params->p_words());
106 secure_vector<word> ws;
107
108 const_time_lookup(e_bits, m_g, scalar.get_substring(m_window_bits * (exp_nibbles - 1), m_window_bits));
109 Montgomery_Int x(m_params, e_bits.data(), e_bits.size(), false);
110
111 for(size_t i = exp_nibbles - 1; i > 0; --i) {
112 x.square_this_n_times(ws, m_window_bits);
113 const_time_lookup(e_bits, m_g, scalar.get_substring(m_window_bits * (i - 1), m_window_bits));
114 x.mul_by(e_bits, ws);
115 }
116
117 x.const_time_unpoison();
118 return x.value();
119}
120
121BigInt Montgomery_Exponentation_State::exponentiation_vartime(const BigInt& scalar) const {
122 const size_t exp_nibbles = (scalar.bits() + m_window_bits - 1) / m_window_bits;
123
124 secure_vector<word> ws;
125
126 if(exp_nibbles == 0) {
127 return BigInt::one();
128 }
129
130 Montgomery_Int x = m_g[scalar.get_substring(m_window_bits * (exp_nibbles - 1), m_window_bits)];
131
132 for(size_t i = exp_nibbles - 1; i > 0; --i) {
133 x.square_this_n_times(ws, m_window_bits);
134
135 const uint32_t nibble = scalar.get_substring(m_window_bits * (i - 1), m_window_bits);
136 if(nibble > 0) {
137 x.mul_by(m_g[nibble], ws);
138 }
139 }
140
141 x.const_time_unpoison();
142 return x.value();
143}
144
145std::shared_ptr<const Montgomery_Exponentation_State> monty_precompute(
146 const std::shared_ptr<const Montgomery_Params>& params, const BigInt& g, size_t window_bits, bool const_time) {
147 return std::make_shared<const Montgomery_Exponentation_State>(params, g, window_bits, const_time);
148}
149
150BigInt monty_execute(const Montgomery_Exponentation_State& precomputed_state, const BigInt& k, size_t max_k_bits) {
151 return precomputed_state.exponentiation(k, max_k_bits);
152}
153
154BigInt monty_execute_vartime(const Montgomery_Exponentation_State& precomputed_state, const BigInt& k) {
155 return precomputed_state.exponentiation_vartime(k);
156}
157
158BigInt monty_multi_exp(const std::shared_ptr<const Montgomery_Params>& params_p,
159 const BigInt& x_bn,
160 const BigInt& z1,
161 const BigInt& y_bn,
162 const BigInt& z2) {
163 if(z1.is_negative() || z2.is_negative()) {
164 throw Invalid_Argument("multi_exponentiate exponents must be positive");
165 }
166
167 const size_t z_bits = round_up(std::max(z1.bits(), z2.bits()), 2);
168
170
171 const Montgomery_Int one(params_p, params_p->R1(), false);
172 //const Montgomery_Int one(params_p, 1);
173
174 const Montgomery_Int x1(params_p, x_bn);
175 const Montgomery_Int x2 = x1.square(ws);
176 const Montgomery_Int x3 = x2.mul(x1, ws);
177
178 const Montgomery_Int y1(params_p, y_bn);
179 const Montgomery_Int y2 = y1.square(ws);
180 const Montgomery_Int y3 = y2.mul(y1, ws);
181
182 const Montgomery_Int y1x1 = y1.mul(x1, ws);
183 const Montgomery_Int y1x2 = y1.mul(x2, ws);
184 const Montgomery_Int y1x3 = y1.mul(x3, ws);
185
186 const Montgomery_Int y2x1 = y2.mul(x1, ws);
187 const Montgomery_Int y2x2 = y2.mul(x2, ws);
188 const Montgomery_Int y2x3 = y2.mul(x3, ws);
189
190 const Montgomery_Int y3x1 = y3.mul(x1, ws);
191 const Montgomery_Int y3x2 = y3.mul(x2, ws);
192 const Montgomery_Int y3x3 = y3.mul(x3, ws);
193
194 const Montgomery_Int* M[16] = {&one,
195 &x1, // 0001
196 &x2, // 0010
197 &x3, // 0011
198 &y1, // 0100
199 &y1x1,
200 &y1x2,
201 &y1x3,
202 &y2, // 1000
203 &y2x1,
204 &y2x2,
205 &y2x3,
206 &y3, // 1100
207 &y3x1,
208 &y3x2,
209 &y3x3};
210
211 Montgomery_Int H = one;
212
213 for(size_t i = 0; i != z_bits; i += 2) {
214 if(i > 0) {
215 H.square_this(ws);
216 H.square_this(ws);
217 }
218
219 const uint32_t z1_b = z1.get_substring(z_bits - i - 2, 2);
220 const uint32_t z2_b = z2.get_substring(z_bits - i - 2, 2);
221
222 const uint32_t z12 = (4 * z2_b) + z1_b;
223
224 H.mul_by(*M[z12], ws);
225 }
226
227 return H.value();
228}
229
230} // namespace Botan
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:59
#define BOTAN_DEBUG_ASSERT(expr)
Definition assert.h:98
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:29
size_t bits() const
Definition bigint.cpp:290
bool is_negative() const
Definition bigint.h:528
uint32_t get_substring(size_t offset, size_t length) const
Definition bigint.cpp:222
Montgomery_Int square(secure_vector< word > &ws) const
Definition monty.cpp:376
BigInt value() const
Definition monty.cpp:296
Montgomery_Int & square_this(secure_vector< word > &ws)
Definition monty.cpp:371
Montgomery_Int mul(const Montgomery_Int &other, secure_vector< word > &ws) const
Definition monty.cpp:340
Montgomery_Int & mul_by(const Montgomery_Int &other, secure_vector< word > &ws)
Definition monty.cpp:344
int(* final)(unsigned char *, CTX *)
BigInt monty_multi_exp(const std::shared_ptr< const Montgomery_Params > &params_p, const BigInt &x_bn, const BigInt &z1, const BigInt &y_bn, const BigInt &z2)
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61
BigInt monty_execute_vartime(const Montgomery_Exponentation_State &precomputed_state, const BigInt &k)
BigInt monty_execute(const Montgomery_Exponentation_State &precomputed_state, const BigInt &k, size_t max_k_bits)
constexpr void clear_mem(T *ptr, size_t n)
Definition mem_ops.h:120
size_t round_up(size_t n, size_t align_to)
Definition rounding.h:21
std::shared_ptr< const Montgomery_Exponentation_State > monty_precompute(const std::shared_ptr< const Montgomery_Params > &params, const BigInt &g, size_t window_bits, bool const_time)