7#include <botan/internal/point_mul.h>
9#include <botan/reducer.h>
11#include <botan/internal/ct_utils.h>
12#include <botan/internal/rounding.h>
18size_t blinding_size(
const BigInt& group_order) {
19 return (group_order.bits() + 1) / 2;
22BigInt blinding_mask(
const BigInt& group_order, RandomNumberGenerator& rng) {
24 BigInt mask(rng, blinding_size(group_order));
40 m_base_point(base), m_mod_order(mod_order), m_p_words(base.get_curve().get_p_words()) {
45 const size_t T_bits =
round_up(order_bits + blinding_size(mod_order.
get_modulus()), WINDOW_BITS) / WINDOW_BITS;
47 std::vector<EC_Point>
T(WINDOW_SIZE * T_bits);
52 for(
size_t i = 0; i != T_bits; i++) {
59 T[7 * i + 1] = std::move(g2);
60 T[7 * i + 2] =
T[7 * i + 1].plus(
T[7 * i + 0], ws);
62 T[7 * i + 4] =
T[7 * i + 3].
plus(
T[7 * i + 0], ws);
63 T[7 * i + 5] =
T[7 * i + 3].
plus(
T[7 * i + 1], ws);
64 T[7 * i + 6] =
T[7 * i + 3].
plus(
T[7 * i + 2], ws);
72 m_W.resize(
T.size() * 2 * m_p_words);
75 for(
size_t i = 0; i !=
T.size(); ++i) {
76 T[i].get_x().encode_words(p, m_p_words);
78 T[i].get_y().encode_words(p, m_p_words);
86 std::vector<BigInt>& ws)
const {
88 throw Invalid_Argument(
"EC_Point_Base_Point_Precompute scalar must be positive");
96 scalar += group_order * blinding_mask(group_order, rng);
104 scalar += group_order;
105 if(scalar.
bits() == group_order.
bits()) {
106 scalar += group_order;
111 const size_t windows =
round_up(scalar.
bits(), WINDOW_BITS) / WINDOW_BITS;
113 const size_t elem_size = 2 * m_p_words;
115 BOTAN_ASSERT(windows <= m_W.size() / (3 * elem_size),
"Precomputed sufficient values for scalar mult");
124 std::vector<word> Wt(elem_size);
126 for(
size_t i = 0; i != windows; ++i) {
127 const size_t window = windows - i - 1;
128 const size_t base_addr = (WINDOW_SIZE * window) * elem_size;
130 const word w = scalar.
get_substring(WINDOW_BITS * window, WINDOW_BITS);
140 for(
size_t j = 0; j != elem_size; ++j) {
141 const word w1 = w_is_1.if_set_return(m_W[base_addr + 0 * elem_size + j]);
142 const word w2 = w_is_2.if_set_return(m_W[base_addr + 1 * elem_size + j]);
143 const word w3 = w_is_3.if_set_return(m_W[base_addr + 2 * elem_size + j]);
144 const word w4 = w_is_4.if_set_return(m_W[base_addr + 3 * elem_size + j]);
145 const word w5 = w_is_5.if_set_return(m_W[base_addr + 4 * elem_size + j]);
146 const word w6 = w_is_6.if_set_return(m_W[base_addr + 5 * elem_size + j]);
147 const word w7 = w_is_7.if_set_return(m_W[base_addr + 6 * elem_size + j]);
149 Wt[j] = w1 | w2 | w3 | w4 | w5 | w6 | w7;
152 R.
add_affine(&Wt[0], m_p_words, &Wt[m_p_words], m_p_words, ws);
172 std::vector<BigInt>& ws) :
173 m_curve(ipoint.get_curve()), m_p_words(m_curve.get_p_words()), m_window_bits(4) {
175 ws.resize(EC_Point::WORKSPACE_SIZE);
181 std::vector<EC_Point> U(
static_cast<size_t>(1) << m_window_bits);
185 for(
size_t i = 2; i < U.size(); i += 2) {
186 U[i] = U[i / 2].double_of(ws);
187 U[i + 1] = U[i].plus(point, ws);
193 for(size_t i = 1; i != U.size(); ++i) {
194 U[i].randomize_repr(rng);
198 m_T.resize(U.size() * 3 * m_p_words);
201 for(
size_t i = 0; i != U.size(); ++i) {
202 U[i].get_x().encode_words(p, m_p_words);
203 U[i].get_y().encode_words(p + m_p_words, m_p_words);
204 U[i].get_z().encode_words(p + 2 * m_p_words, m_p_words);
211 const BigInt& group_order,
212 std::vector<BigInt>& ws)
const {
214 throw Invalid_Argument(
"EC_Point_Var_Point_Precompute scalar must be positive");
216 if(ws.size() < EC_Point::WORKSPACE_SIZE) {
217 ws.resize(EC_Point::WORKSPACE_SIZE);
221 const BigInt scalar = k + group_order * blinding_mask(group_order, rng);
223 const size_t elem_size = 3 * m_p_words;
224 const size_t window_elems =
static_cast<size_t>(1) << m_window_bits;
226 size_t windows =
round_up(scalar.
bits(), m_window_bits) / m_window_bits;
233 const uint32_t w = scalar.
get_substring(windows * m_window_bits, m_window_bits);
236 for(
size_t i = 1; i != window_elems; ++i) {
239 for(
size_t j = 0; j != elem_size; ++j) {
240 e[j] |= wmask.if_set_return(m_T[i * elem_size + j]);
244 R.
add(&e[0], m_p_words, &e[m_p_words], m_p_words, &e[2 * m_p_words], m_p_words, ws);
255 R.
mult2i(m_window_bits, ws);
257 const uint32_t w = scalar.
get_substring((windows - 1) * m_window_bits, m_window_bits);
260 for(
size_t i = 1; i != window_elems; ++i) {
263 for(
size_t j = 0; j != elem_size; ++j) {
264 e[j] |= wmask.if_set_return(m_T[i * elem_size + j]);
268 R.
add(&e[0], m_p_words, &e[m_p_words], m_p_words, &e[2 * m_p_words], m_p_words, ws);
278EC_Point_Multi_Point_Precompute::EC_Point_Multi_Point_Precompute(
const EC_Point& x,
const EC_Point& y) {
280 m_M.push_back(x.
zero());
284 std::vector<BigInt> ws(EC_Point::WORKSPACE_SIZE);
303 m_M.push_back(y.
plus(x, ws));
304 m_M.push_back(y.
plus(x2, ws));
305 m_M.push_back(y.
plus(x3, ws));
308 m_M.push_back(y2.
plus(x, ws));
309 m_M.push_back(y2.
plus(x2, ws));
310 m_M.push_back(y2.
plus(x3, ws));
313 m_M.push_back(y3.
plus(x, ws));
314 m_M.push_back(y3.
plus(x2, ws));
315 m_M.push_back(y3.
plus(x3, ws));
317 bool no_infinity =
true;
318 for(
auto& pt : m_M) {
325 EC_Point::force_all_affine(m_M, ws[0].get_word_vector());
328 m_no_infinity = no_infinity;
278EC_Point_Multi_Point_Precompute::EC_Point_Multi_Point_Precompute(
const EC_Point& x,
const EC_Point& y) {
…}
332 if(m_M.size() == 1) {
336 std::vector<BigInt> ws(EC_Point::WORKSPACE_SIZE);
342 for(
size_t i = 0; i != z_bits; i += 2) {
350 const uint32_t z12 = (4 * z2_b) + z1_b;
357 H.
add(m_M[z12 - 1], ws);
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_DEBUG_ASSERT(expr)
#define BOTAN_ASSERT(expr, assertion_made)
uint32_t get_substring(size_t offset, size_t length) const
static constexpr Mask< T > is_equal(T x, T y)
EC_Point_Base_Point_Precompute(const EC_Point &base_point, const Modular_Reducer &mod_order)
EC_Point mul(const BigInt &k, RandomNumberGenerator &rng, const BigInt &group_order, std::vector< BigInt > &ws) const
EC_Point multi_exp(const BigInt &k1, const BigInt &k2) const
EC_Point_Var_Point_Precompute(const EC_Point &point, RandomNumberGenerator &rng, std::vector< BigInt > &ws)
void swap(EC_Point &other) noexcept
EC_Point plus(const EC_Point &other, std::vector< BigInt > &workspace) const
void randomize_repr(RandomNumberGenerator &rng)
void add(const EC_Point &other, std::vector< BigInt > &workspace)
void add_affine(const EC_Point &other, std::vector< BigInt > &workspace)
void mult2(std::vector< BigInt > &workspace)
void mult2i(size_t i, std::vector< BigInt > &workspace)
bool on_the_curve() const
static void force_all_affine(std::span< EC_Point > points, secure_vector< word > &ws)
const BigInt & get_modulus() const
BigInt reduce(const BigInt &x) const
virtual bool is_seeded() const =0
constexpr size_t round_up(size_t n, size_t align_to)
EC_Point multi_exponentiate(const EC_Point &p1, const BigInt &z1, const EC_Point &p2, const BigInt &z2)
std::vector< T, secure_allocator< T > > secure_vector
constexpr void clear_mem(T *ptr, size_t n)