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) {
23 BigInt mask(rng, blinding_size(group_order));
36 m_base_point(base), m_mod_order(mod_order), m_p_words(base.get_curve().get_p_words()) {
41 const size_t T_bits =
round_up(order_bits + blinding_size(mod_order.
get_modulus()), WINDOW_BITS) / WINDOW_BITS;
43 std::vector<EC_Point>
T(WINDOW_SIZE * T_bits);
48 for(
size_t i = 0; i != T_bits; i++) {
55 T[7 * i + 1] = std::move(g2);
56 T[7 * i + 2] =
T[7 * i + 1].plus(
T[7 * i + 0], ws);
58 T[7 * i + 4] =
T[7 * i + 3].
plus(
T[7 * i + 0], ws);
59 T[7 * i + 5] =
T[7 * i + 3].
plus(
T[7 * i + 1], ws);
60 T[7 * i + 6] =
T[7 * i + 3].
plus(
T[7 * i + 2], ws);
68 m_W.resize(
T.size() * 2 * m_p_words);
71 for(
size_t i = 0; i !=
T.size(); ++i) {
72 T[i].get_x().encode_words(p, m_p_words);
74 T[i].get_y().encode_words(p, m_p_words);
82 std::vector<BigInt>& ws)
const {
84 throw Invalid_Argument(
"EC_Point_Base_Point_Precompute scalar must be positive");
92 scalar += group_order * blinding_mask(group_order, rng);
100 scalar += group_order;
101 if(scalar.
bits() == group_order.
bits()) {
102 scalar += group_order;
107 const size_t windows =
round_up(scalar.
bits(), WINDOW_BITS) / WINDOW_BITS;
109 const size_t elem_size = 2 * m_p_words;
111 BOTAN_ASSERT(windows <= m_W.size() / (3 * elem_size),
"Precomputed sufficient values for scalar mult");
120 std::vector<word> Wt(elem_size);
122 for(
size_t i = 0; i != windows; ++i) {
123 const size_t window = windows - i - 1;
124 const size_t base_addr = (WINDOW_SIZE * window) * elem_size;
126 const word w = scalar.
get_substring(WINDOW_BITS * window, WINDOW_BITS);
136 for(
size_t j = 0; j != elem_size; ++j) {
137 const word w1 = w_is_1.if_set_return(m_W[base_addr + 0 * elem_size + j]);
138 const word w2 = w_is_2.if_set_return(m_W[base_addr + 1 * elem_size + j]);
139 const word w3 = w_is_3.if_set_return(m_W[base_addr + 2 * elem_size + j]);
140 const word w4 = w_is_4.if_set_return(m_W[base_addr + 3 * elem_size + j]);
141 const word w5 = w_is_5.if_set_return(m_W[base_addr + 4 * elem_size + j]);
142 const word w6 = w_is_6.if_set_return(m_W[base_addr + 5 * elem_size + j]);
143 const word w7 = w_is_7.if_set_return(m_W[base_addr + 6 * elem_size + j]);
145 Wt[j] = w1 | w2 | w3 | w4 | w5 | w6 | w7;
148 R.
add_affine(&Wt[0], m_p_words, &Wt[m_p_words], m_p_words, ws);
168 std::vector<BigInt>& ws) :
169 m_curve(ipoint.get_curve()), m_p_words(m_curve.get_p_words()), m_window_bits(4) {
171 ws.resize(EC_Point::WORKSPACE_SIZE);
177 std::vector<EC_Point> U(
static_cast<size_t>(1) << m_window_bits);
181 for(
size_t i = 2; i < U.size(); i += 2) {
182 U[i] = U[i / 2].double_of(ws);
183 U[i + 1] = U[i].plus(point, ws);
189 for(size_t i = 1; i != U.size(); ++i) {
190 U[i].randomize_repr(rng);
194 m_T.resize(U.size() * 3 * m_p_words);
197 for(
size_t i = 0; i != U.size(); ++i) {
198 U[i].get_x().encode_words(p, m_p_words);
199 U[i].get_y().encode_words(p + m_p_words, m_p_words);
200 U[i].get_z().encode_words(p + 2 * m_p_words, m_p_words);
207 const BigInt& group_order,
208 std::vector<BigInt>& ws)
const {
210 throw Invalid_Argument(
"EC_Point_Var_Point_Precompute scalar must be positive");
212 if(ws.size() < EC_Point::WORKSPACE_SIZE) {
213 ws.resize(EC_Point::WORKSPACE_SIZE);
217 const BigInt scalar = k + group_order * blinding_mask(group_order, rng);
219 const size_t elem_size = 3 * m_p_words;
220 const size_t window_elems =
static_cast<size_t>(1) << m_window_bits;
222 size_t windows =
round_up(scalar.
bits(), m_window_bits) / m_window_bits;
229 const uint32_t w = scalar.
get_substring(windows * m_window_bits, m_window_bits);
232 for(
size_t i = 1; i != window_elems; ++i) {
235 for(
size_t j = 0; j != elem_size; ++j) {
236 e[j] |= wmask.if_set_return(m_T[i * elem_size + j]);
240 R.
add(&e[0], m_p_words, &e[m_p_words], m_p_words, &e[2 * m_p_words], m_p_words, ws);
251 R.
mult2i(m_window_bits, ws);
253 const uint32_t w = scalar.
get_substring((windows - 1) * m_window_bits, m_window_bits);
256 for(
size_t i = 1; i != window_elems; ++i) {
259 for(
size_t j = 0; j != elem_size; ++j) {
260 e[j] |= wmask.if_set_return(m_T[i * elem_size + j]);
264 R.
add(&e[0], m_p_words, &e[m_p_words], m_p_words, &e[2 * m_p_words], m_p_words, ws);
274EC_Point_Multi_Point_Precompute::EC_Point_Multi_Point_Precompute(
const EC_Point& x,
const EC_Point& y) {
276 m_M.push_back(x.
zero());
280 std::vector<BigInt> ws(EC_Point::WORKSPACE_SIZE);
299 m_M.push_back(y.
plus(x, ws));
300 m_M.push_back(y.
plus(x2, ws));
301 m_M.push_back(y.
plus(x3, ws));
304 m_M.push_back(y2.
plus(x, ws));
305 m_M.push_back(y2.
plus(x2, ws));
306 m_M.push_back(y2.
plus(x3, ws));
309 m_M.push_back(y3.
plus(x, ws));
310 m_M.push_back(y3.
plus(x2, ws));
311 m_M.push_back(y3.
plus(x3, ws));
313 bool no_infinity =
true;
314 for(
auto& pt : m_M) {
321 EC_Point::force_all_affine(m_M, ws[0].get_word_vector());
324 m_no_infinity = no_infinity;
328 if(m_M.size() == 1) {
332 std::vector<BigInt> ws(EC_Point::WORKSPACE_SIZE);
338 for(
size_t i = 0; i != z_bits; i += 2) {
346 const uint32_t z12 = (4 * z2_b) + z1_b;
353 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)