Botan 3.13.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,2025 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/exceptn.h>
12#include <botan/mem_ops.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_Exponentiation_State final {
20 public:
21 Montgomery_Exponentiation_State(const Montgomery_Int& g, size_t window_bits, bool const_time);
22
23 Montgomery_Int exponentiation(const BigInt& k, size_t max_k_bits) const;
24
25 Montgomery_Int exponentiation_vartime(const BigInt& k) const;
26
27 private:
28 Montgomery_Params m_params;
29 std::vector<Montgomery_Int> m_g;
30 size_t m_window_bits;
31};
32
33Montgomery_Exponentiation_State::Montgomery_Exponentiation_State(const Montgomery_Int& g,
34 size_t window_bits,
35 bool const_time) :
36 m_params(g._params()), m_window_bits(window_bits == 0 ? 4 : window_bits) {
37 if(m_window_bits < 1 || m_window_bits > 12) { // really even 8 is too large ...
38 throw Invalid_Argument("Invalid window bits for Montgomery exponentiation");
39 }
40
41 const size_t window_size = (static_cast<size_t>(1) << m_window_bits);
42
43 m_g.reserve(window_size);
44
45 m_g.push_back(Montgomery_Int::one(m_params));
46
47 m_g.push_back(g);
48
49 secure_vector<word> ws(2 * m_params.p_words());
50
51 for(size_t i = 2; i != window_size; ++i) {
52 if(i % 2 == 0) {
53 m_g.push_back(m_g[i / 2].square(ws));
54 } else {
55 m_g.push_back(m_g[1].mul(m_g[i - 1], ws));
56 }
57 }
58
59 if(const_time) {
60 CT::poison_range(m_g);
61 }
62}
63
64namespace {
65
66void const_time_lookup(secure_vector<word>& output, const std::vector<Montgomery_Int>& g, size_t nibble) {
67 BOTAN_ASSERT_NOMSG(g.size() % 2 == 0); // actually a power of 2
68
69 const size_t words = output.size();
70
71 clear_mem(output.data(), output.size());
72
73 for(size_t i = 0; i != g.size(); i += 2) {
74 const secure_vector<word>& vec_0 = g[i].repr();
75 const secure_vector<word>& vec_1 = g[i + 1].repr();
76
77 BOTAN_ASSERT_NOMSG(vec_0.size() >= words && vec_1.size() >= words);
78
79 const auto mask_0 = CT::Mask<word>::is_equal(nibble, i);
80 const auto mask_1 = CT::Mask<word>::is_equal(nibble, i + 1);
81
82 for(size_t w = 0; w != words; ++w) {
83 output[w] |= mask_0.if_set_return(vec_0[w]);
84 output[w] |= mask_1.if_set_return(vec_1[w]);
85 }
86 }
87}
88
89} // namespace
90
91Montgomery_Int Montgomery_Exponentiation_State::exponentiation(const BigInt& scalar, size_t max_k_bits) const {
92 BOTAN_ARG_CHECK(scalar.signum() >= 0, "Invalid scalar for Montgomery exponentiation");
93 BOTAN_DEBUG_ASSERT(scalar.bits() <= max_k_bits);
94 // TODO add a const-time implementation of above assert and use it in release builds
95
96 const size_t exp_nibbles = (max_k_bits + m_window_bits - 1) / m_window_bits;
97
98 if(exp_nibbles == 0) {
99 return Montgomery_Int::one(m_params);
100 }
101
102 secure_vector<word> e_bits(m_params.p_words());
103 secure_vector<word> ws(2 * m_params.p_words());
104
105 const_time_lookup(e_bits, m_g, scalar.get_substring(m_window_bits * (exp_nibbles - 1), m_window_bits));
106 Montgomery_Int x(m_params, std::span{e_bits});
107
108 for(size_t i = exp_nibbles - 1; i > 0; --i) {
109 x.square_this_n_times(ws, m_window_bits);
110 const_time_lookup(e_bits, m_g, scalar.get_substring(m_window_bits * (i - 1), m_window_bits));
111 x.mul_by(e_bits, ws);
112 }
113
114 CT::unpoison(x);
115 return x;
116}
117
118Montgomery_Int Montgomery_Exponentiation_State::exponentiation_vartime(const BigInt& scalar) const {
119 const size_t exp_nibbles = (scalar.bits() + m_window_bits - 1) / m_window_bits;
120
121 secure_vector<word> ws(2 * m_params.p_words());
122
123 if(exp_nibbles == 0) {
124 return Montgomery_Int::one(m_params);
125 }
126
127 Montgomery_Int x = m_g[scalar.get_substring(m_window_bits * (exp_nibbles - 1), m_window_bits)];
128
129 for(size_t i = exp_nibbles - 1; i > 0; --i) {
130 x.square_this_n_times(ws, m_window_bits);
131
132 const uint32_t nibble = scalar.get_substring(m_window_bits * (i - 1), m_window_bits);
133 if(nibble > 0) {
134 x.mul_by(m_g[nibble], ws);
135 }
136 }
137
138 CT::unpoison(x);
139 return x;
140}
141
142std::shared_ptr<const Montgomery_Exponentiation_State> monty_precompute(const Montgomery_Int& g,
143 size_t window_bits,
144 bool const_time) {
145 return std::make_shared<const Montgomery_Exponentiation_State>(g, window_bits, const_time);
146}
147
148std::shared_ptr<const Montgomery_Exponentiation_State> monty_precompute(const Montgomery_Params& params,
149 const BigInt& g,
150 size_t window_bits,
151 bool const_time) {
152 BOTAN_ARG_CHECK(g.signum() >= 0 && g < params.p(), "Montgomery exponentiation base integer out of range");
153 const Montgomery_Int monty_g(params, g);
154 return monty_precompute(monty_g, window_bits, const_time);
155}
156
157Montgomery_Int monty_execute(const Montgomery_Exponentiation_State& precomputed_state,
158 const BigInt& k,
159 size_t max_k_bits) {
160 return precomputed_state.exponentiation(k, max_k_bits);
161}
162
163Montgomery_Int monty_execute_vartime(const Montgomery_Exponentiation_State& precomputed_state, const BigInt& k) {
164 return precomputed_state.exponentiation_vartime(k);
165}
166
168 const Montgomery_Params& params_p, const BigInt& x_bn, const BigInt& z1, const BigInt& y_bn, const BigInt& z2) {
169 if(z1.signum() < 0 || z2.signum() < 0) {
170 throw Invalid_Argument("multi_exponentiate exponents must be positive");
171 }
172
173 const size_t z_bits = round_up(std::max(z1.bits(), z2.bits()), 2);
174
175 secure_vector<word> ws(2 * params_p.p_words());
176
177 const Montgomery_Int one = Montgomery_Int::one(params_p);
178
179 const Montgomery_Int x1(params_p, x_bn);
180 const Montgomery_Int x2 = x1.square(ws);
181 const Montgomery_Int x3 = x2.mul(x1, ws);
182
183 const Montgomery_Int y1(params_p, y_bn);
184 const Montgomery_Int y2 = y1.square(ws);
185 const Montgomery_Int y3 = y2.mul(y1, ws);
186
187 const Montgomery_Int y1x1 = y1.mul(x1, ws);
188 const Montgomery_Int y1x2 = y1.mul(x2, ws);
189 const Montgomery_Int y1x3 = y1.mul(x3, ws);
190
191 const Montgomery_Int y2x1 = y2.mul(x1, ws);
192 const Montgomery_Int y2x2 = y2.mul(x2, ws);
193 const Montgomery_Int y2x3 = y2.mul(x3, ws);
194
195 const Montgomery_Int y3x1 = y3.mul(x1, ws);
196 const Montgomery_Int y3x2 = y3.mul(x2, ws);
197 const Montgomery_Int y3x3 = y3.mul(x3, ws);
198
199 // NOLINTNEXTLINE(*-const-correctness) bug in clang-tidy
200 const Montgomery_Int* M[16] = {&one,
201 &x1, // 0001
202 &x2, // 0010
203 &x3, // 0011
204 &y1, // 0100
205 &y1x1,
206 &y1x2,
207 &y1x3,
208 &y2, // 1000
209 &y2x1,
210 &y2x2,
211 &y2x3,
212 &y3, // 1100
213 &y3x1,
214 &y3x2,
215 &y3x3};
216
217 Montgomery_Int H = one;
218
219 for(size_t i = 0; i != z_bits; i += 2) {
220 if(i > 0) {
221 H.square_this_n_times(ws, 2);
222 }
223
224 const uint32_t z1_b = z1.get_substring(z_bits - i - 2, 2);
225 const uint32_t z2_b = z2.get_substring(z_bits - i - 2, 2);
226
227 const uint32_t z12 = (4 * z2_b) + z1_b;
228
229 if(z12 > 0) {
230 H.mul_by(*M[z12], ws);
231 }
232 }
233
234 return H;
235}
236
237} // namespace Botan
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:75
#define BOTAN_DEBUG_ASSERT(expr)
Definition assert.h:129
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
int signum() const
Definition bigint.h:493
size_t bits() const
Definition bigint.cpp:307
uint32_t get_substring(size_t offset, size_t length) const
Definition bigint.cpp:239
Montgomery_Int square(secure_vector< word > &ws) const
Definition monty.cpp:401
static Montgomery_Int one(const Montgomery_Params &params)
Definition monty.cpp:231
Montgomery_Int & square_this_n_times(secure_vector< word > &ws, size_t n)
Definition monty.cpp:372
Montgomery_Int mul(const Montgomery_Int &other, secure_vector< word > &ws) const
Definition monty.cpp:320
Montgomery_Int & mul_by(const Montgomery_Int &other, secure_vector< word > &ws)
Definition monty.cpp:340
size_t p_words() const
Definition monty.h:51
const BigInt & p() const
Definition monty.h:41
std::shared_ptr< const Montgomery_Exponentiation_State > monty_precompute(const Montgomery_Int &g, size_t window_bits, bool const_time)
BigInt square(const BigInt &x)
Definition numthry.cpp:184
constexpr size_t round_up(size_t n, size_t align_to)
Definition rounding.h:26
Montgomery_Int monty_multi_exp(const Montgomery_Params &params_p, const BigInt &x_bn, const BigInt &z1, const BigInt &y_bn, const BigInt &z2)
Montgomery_Int monty_execute_vartime(const Montgomery_Exponentiation_State &precomputed_state, const BigInt &k)
Montgomery_Int monty_execute(const Montgomery_Exponentiation_State &precomputed_state, const BigInt &k, size_t max_k_bits)
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128
constexpr void clear_mem(T *ptr, size_t n)
Definition mem_ops.h:118