9#include <botan/internal/monty_exp.h>
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>
19class Montgomery_Exponentation_State
final {
21 Montgomery_Exponentation_State(
const std::shared_ptr<const Montgomery_Params>& params,
26 BigInt exponentiation(
const BigInt& k,
size_t max_k_bits)
const;
28 BigInt exponentiation_vartime(
const BigInt& k)
const;
31 std::shared_ptr<const Montgomery_Params> m_params;
32 std::vector<Montgomery_Int> m_g;
36Montgomery_Exponentation_State::Montgomery_Exponentation_State(
const std::shared_ptr<const Montgomery_Params>& params,
40 m_params(params), m_window_bits(window_bits == 0 ? 4 : window_bits) {
43 if(m_window_bits < 1 || m_window_bits > 12) {
44 throw Invalid_Argument(
"Invalid window bits for Montgomery exponentiation");
47 const size_t window_size = (
static_cast<size_t>(1) << m_window_bits);
49 m_g.reserve(window_size);
51 m_g.push_back(Montgomery_Int(m_params, m_params->R1(),
false));
53 m_g.push_back(Montgomery_Int(m_params, g));
55 for(
size_t i = 2; i != window_size; ++i) {
56 m_g.push_back(m_g[1] * m_g[i - 1]);
60 for(
size_t i = 0; i != window_size; ++i) {
65 CT::poison_range(m_g);
71void const_time_lookup(secure_vector<word>& output,
const std::vector<Montgomery_Int>& g,
size_t nibble) {
74 const size_t words = output.size();
78 for(
size_t i = 0; i != g.size(); i += 2) {
79 const secure_vector<word>& vec_0 = g[i].repr().get_word_vector();
80 const secure_vector<word>& vec_1 = g[i + 1].repr().get_word_vector();
84 const auto mask_0 = CT::Mask<word>::is_equal(nibble, i);
85 const auto mask_1 = CT::Mask<word>::is_equal(nibble, i + 1);
87 for(
size_t w = 0; w != words; ++w) {
88 output[w] |= mask_0.if_set_return(vec_0[w]);
89 output[w] |= mask_1.if_set_return(vec_1[w]);
96BigInt Montgomery_Exponentation_State::exponentiation(
const BigInt& scalar,
size_t max_k_bits)
const {
100 const size_t exp_nibbles = (max_k_bits + m_window_bits - 1) / m_window_bits;
102 if(exp_nibbles == 0) {
103 return BigInt::one();
106 secure_vector<word> e_bits(m_params->p_words());
107 secure_vector<word> ws;
109 const_time_lookup(e_bits, m_g, scalar.get_substring(m_window_bits * (exp_nibbles - 1), m_window_bits));
110 Montgomery_Int x(m_params, e_bits.data(), e_bits.size(),
false);
112 for(
size_t i = exp_nibbles - 1; i > 0; --i) {
113 x.square_this_n_times(ws, m_window_bits);
114 const_time_lookup(e_bits, m_g, scalar.get_substring(m_window_bits * (i - 1), m_window_bits));
115 x.mul_by(e_bits, ws);
122BigInt Montgomery_Exponentation_State::exponentiation_vartime(
const BigInt& scalar)
const {
123 const size_t exp_nibbles = (scalar.bits() + m_window_bits - 1) / m_window_bits;
125 secure_vector<word> ws;
127 if(exp_nibbles == 0) {
128 return BigInt::one();
131 Montgomery_Int x = m_g[scalar.get_substring(m_window_bits * (exp_nibbles - 1), m_window_bits)];
133 for(
size_t i = exp_nibbles - 1; i > 0; --i) {
134 x.square_this_n_times(ws, m_window_bits);
136 const uint32_t nibble = scalar.get_substring(m_window_bits * (i - 1), m_window_bits);
138 x.mul_by(m_g[nibble], ws);
147 const std::shared_ptr<const Montgomery_Params>& params,
const BigInt& g,
size_t window_bits,
bool const_time) {
148 return std::make_shared<const Montgomery_Exponentation_State>(params, g, window_bits, const_time);
152 return precomputed_state.exponentiation(k, max_k_bits);
156 return precomputed_state.exponentiation_vartime(k);
214 for(
size_t i = 0; i != z_bits; i += 2) {
223 const uint32_t z12 = (4 * z2_b) + z1_b;
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_DEBUG_ASSERT(expr)
#define BOTAN_ARG_CHECK(expr, msg)
uint32_t get_substring(size_t offset, size_t length) const
Montgomery_Int square(secure_vector< word > &ws) const
Montgomery_Int & square_this(secure_vector< word > &ws)
Montgomery_Int mul(const Montgomery_Int &other, secure_vector< word > &ws) const
Montgomery_Int & mul_by(const Montgomery_Int &other, secure_vector< word > &ws)
int(* final)(unsigned char *, CTX *)
constexpr size_t round_up(size_t n, size_t align_to)
BigInt monty_multi_exp(const std::shared_ptr< const Montgomery_Params > ¶ms_p, const BigInt &x_bn, const BigInt &z1, const BigInt &y_bn, const BigInt &z2)
std::vector< T, secure_allocator< T > > secure_vector
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)
std::shared_ptr< const Montgomery_Exponentation_State > monty_precompute(const std::shared_ptr< const Montgomery_Params > ¶ms, const BigInt &g, size_t window_bits, bool const_time)