10#include <botan/ec_point.h>
12#include <botan/numthry.h>
14#include <botan/internal/ct_utils.h>
23 m_curve(curve), m_coord_x(x), m_coord_y(y), m_coord_z(m_curve.get_1_rep()) {
24 if(x < 0 || x >= curve.
get_p()) {
27 if(y < 0 || y >= curve.
get_p()) {
32 m_curve.
to_rep(m_coord_x, monty_ws);
33 m_curve.
to_rep(m_coord_y, monty_ws);
54 m_coord_x = m_curve.
mul_to_tmp(m_coord_x, mask2, ws);
55 m_coord_y = m_curve.
mul_to_tmp(m_coord_y, mask3, ws);
56 m_coord_z = m_curve.
mul_to_tmp(m_coord_z, mask, ws);
61inline void resize_ws(std::vector<BigInt>& ws_bn,
size_t cap_size) {
64 for(
auto& ws : ws_bn) {
65 if(ws.size() < cap_size) {
66 ws.get_word_vector().resize(cap_size);
74 const word x_words[],
size_t x_size,
const word y_words[],
size_t y_size, std::vector<BigInt>& ws_bn) {
104 m_curve.
sqr(T3, m_coord_z, ws);
105 m_curve.
mul(T4, x_words, x_size, T3, ws);
107 m_curve.
mul(T2, m_coord_z, T3, ws);
108 m_curve.
mul(T0, y_words, y_size, T2, ws);
110 T4.
mod_sub(m_coord_x, p, sub_ws);
112 T0.
mod_sub(m_coord_y, p, sub_ws);
127 m_curve.
sqr(T2, T4, ws);
129 m_curve.
mul(T3, m_coord_x, T2, ws);
131 m_curve.
mul(T1, T2, T4, ws);
133 m_curve.
sqr(m_coord_x, T0, ws);
134 m_coord_x.
mod_sub(T1, p, sub_ws);
136 m_coord_x.
mod_sub(T3, p, sub_ws);
137 m_coord_x.
mod_sub(T3, p, sub_ws);
139 T3.
mod_sub(m_coord_x, p, sub_ws);
141 m_curve.
mul(T2, T0, T3, ws);
142 m_curve.
mul(T0, m_coord_y, T1, ws);
146 m_curve.
mul(T0, m_coord_z, T4, ws);
152 const word y_words[],
154 const word z_words[],
156 std::vector<BigInt>& ws_bn) {
186 m_curve.
sqr(T0, z_words, z_size, ws);
187 m_curve.
mul(T1, m_coord_x, T0, ws);
188 m_curve.
mul(T3, z_words, z_size, T0, ws);
189 m_curve.
mul(T2, m_coord_y, T3, ws);
191 m_curve.
sqr(T3, m_coord_z, ws);
192 m_curve.
mul(T4, x_words, x_size, T3, ws);
194 m_curve.
mul(T5, m_coord_z, T3, ws);
195 m_curve.
mul(T0, y_words, y_size, T5, ws);
214 m_curve.
sqr(T5, T4, ws);
216 m_curve.
mul(T3, T1, T5, ws);
218 m_curve.
mul(T1, T5, T4, ws);
220 m_curve.
sqr(m_coord_x, T0, ws);
221 m_coord_x.
mod_sub(T1, p, sub_ws);
222 m_coord_x.
mod_sub(T3, p, sub_ws);
223 m_coord_x.
mod_sub(T3, p, sub_ws);
225 T3.
mod_sub(m_coord_x, p, sub_ws);
227 m_curve.
mul(m_coord_y, T0, T3, ws);
228 m_curve.
mul(T3, T2, T1, ws);
230 m_coord_y.
mod_sub(T3, p, sub_ws);
232 m_curve.
mul(T3, z_words, z_size, m_coord_z, ws);
233 m_curve.
mul(m_coord_z, T3, T4, ws);
237 if(iterations == 0) {
250 for(
size_t i = 0; i != iterations; ++i) {
282 m_curve.
sqr(T0, m_coord_y, ws);
284 m_curve.
mul(T1, m_coord_x, T0, ws);
289 m_curve.
sqr(T4, m_coord_x, ws);
296 m_curve.
sqr(T3, m_coord_z, ws);
303 T3.
mod_add(m_coord_x, p, sub_ws);
305 m_curve.
mul(T4, T2, T3, ws);
309 m_curve.
sqr(T3, m_coord_z, ws);
310 m_curve.
sqr(T4, T3, ws);
313 m_curve.
sqr(T4, m_coord_x, ws);
318 m_curve.
sqr(T2, T4, ws);
322 m_curve.
sqr(T3, T0, ws);
327 m_curve.
mul(T0, T4, T1, ws);
332 m_curve.
mul(T2, m_coord_y, m_coord_z, ws);
359 *
this = scalar * *
this;
366 const size_t scalar_bits = scalar.
bits();
372 for(
size_t i = scalar_bits; i > 0; i--) {
373 const size_t b = scalar.
get_bit(i - 1);
374 R[b ^ 1].
add(R[b], ws);
389 if(points.size() <= 1) {
390 for(
auto& point : points) {
396 for(
auto& point : points) {
397 if(point.is_zero()) {
398 throw Invalid_State(
"Cannot convert zero ECC point to affine");
411 const CurveGFp& curve = points[0].m_curve;
418 std::vector<BigInt> c(points.size());
419 c[0] = points[0].m_coord_z;
421 for(
size_t i = 1; i != points.size(); ++i) {
422 curve.
mul(c[i], c[i - 1], points[i].m_coord_z, ws);
427 BigInt z_inv, z2_inv, z3_inv;
429 for(
size_t i = points.size() - 1; i != 0; i--) {
432 curve.
mul(z_inv, s_inv, c[i - 1], ws);
434 s_inv = curve.
mul_to_tmp(s_inv, point.m_coord_z, ws);
436 curve.
sqr(z2_inv, z_inv, ws);
437 curve.
mul(z3_inv, z2_inv, z_inv, ws);
438 point.m_coord_x = curve.
mul_to_tmp(point.m_coord_x, z2_inv, ws);
439 point.m_coord_y = curve.
mul_to_tmp(point.m_coord_y, z3_inv, ws);
440 point.m_coord_z = rep_1;
443 curve.
sqr(z2_inv, s_inv, ws);
444 curve.
mul(z3_inv, z2_inv, s_inv, ws);
445 points[0].m_coord_x = curve.
mul_to_tmp(points[0].m_coord_x, z2_inv, ws);
446 points[0].m_coord_y = curve.
mul_to_tmp(points[0].m_coord_y, z3_inv, ws);
447 points[0].m_coord_z = rep_1;
452 throw Invalid_State(
"Cannot convert zero ECC point to affine");
460 m_coord_x = m_curve.
mul_to_tmp(m_coord_x, z2_inv, ws);
461 m_coord_y = m_curve.
mul_to_tmp(m_coord_y, z3_inv, ws);
466 return m_curve.
is_one(m_coord_z);
484 m_curve.
mul(r, m_coord_x, z2, monty_ws);
505 m_curve.
mul(r, m_coord_y, z3_inv, monty_ws);
548 m_curve.
swap(other.m_curve);
549 m_coord_x.
swap(other.m_coord_x);
550 m_coord_y.
swap(other.m_coord_y);
551 m_coord_z.
swap(other.m_coord_z);
555 if(m_curve != other.m_curve) {
570 return std::vector<uint8_t>(1);
573 const size_t p_bytes = m_curve.
get_p().
bytes();
578 std::vector<uint8_t> result;
581 result.resize(1 + 2 * p_bytes);
586 result.resize(1 + p_bytes);
587 result[0] = 0x02 |
static_cast<uint8_t
>(y.
get_bit(0));
590 result.resize(1 + 2 * p_bytes);
591 result[0] = 0x06 |
static_cast<uint8_t
>(y.
get_bit(0));
635 EC_Point point(curve, xy.first, xy.second);
638 throw Decoding_Error(
"OS2ECP: Decoded point was not on the curve");
645 const uint8_t data[],
size_t data_len,
const BigInt& curve_p,
const BigInt& curve_a,
const BigInt& curve_b) {
650 const uint8_t pc = data[0];
654 if(pc == 2 || pc == 3) {
658 const bool y_mod_2 = ((pc & 0x01) == 1);
659 y = decompress_point(y_mod_2, x, curve_p, curve_a, curve_b);
661 const size_t l = (data_len - 1) / 2;
666 }
else if(pc == 6 || pc == 7) {
667 const size_t l = (data_len - 1) / 2;
673 const bool y_mod_2 = ((pc & 0x01) == 1);
675 if(decompress_point(y_mod_2, x, curve_p, curve_a, curve_b) != y) {
679 throw Invalid_Argument(
"OS2ECP: Unknown format type " + std::to_string(pc));
682 return std::make_pair(x, y);
#define BOTAN_DEBUG_ASSERT(expr)
#define BOTAN_ASSERT(expr, assertion_made)
BigInt & mod_mul(uint8_t y, const BigInt &mod, secure_vector< word > &ws)
static BigInt decode(const uint8_t buf[], size_t length)
static BigInt random_integer(RandomNumberGenerator &rng, const BigInt &min, const BigInt &max)
void set_words(const word w[], size_t len)
BigInt & mod_add(const BigInt &y, const BigInt &mod, secure_vector< word > &ws)
BigInt & mod_sub(const BigInt &y, const BigInt &mod, secure_vector< word > &ws)
static secure_vector< uint8_t > encode_1363(const BigInt &n, size_t bytes)
bool get_bit(size_t n) const
bool a_is_minus_3() const
void mul(BigInt &z, const BigInt &x, const BigInt &y, secure_vector< word > &ws) const
size_t get_ws_size() const
BigInt invert_element(const BigInt &x, secure_vector< word > &ws) const
const BigInt & get_1_rep() const
const BigInt & get_b_rep() const
bool is_one(const BigInt &x) const
void sqr(BigInt &z, const BigInt &x, secure_vector< word > &ws) const
void swap(CurveGFp &other)
const BigInt & get_a_rep() const
BigInt from_rep_to_tmp(const BigInt &x, secure_vector< word > &ws) const
const BigInt & get_a() const
void to_rep(BigInt &x, secure_vector< word > &ws) const
const BigInt & get_p() const
BigInt sqr_to_tmp(const BigInt &x, secure_vector< word > &ws) const
const BigInt & get_b() const
void from_rep(BigInt &x, secure_vector< word > &ws) const
BigInt mul_to_tmp(const BigInt &x, const BigInt &y, secure_vector< word > &ws) const
void randomize_repr(RandomNumberGenerator &rng)
BigInt get_affine_x() const
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)
EC_Point & operator-=(const EC_Point &rhs)
bool on_the_curve() const
EC_Point & operator+=(const EC_Point &rhs)
bool operator==(const EC_Point &other) const
BigInt get_affine_y() const
EC_Point & operator*=(const BigInt &scalar)
friend void swap(EC_Point &x, EC_Point &y)
static void force_all_affine(std::vector< EC_Point > &points, secure_vector< word > &ws)
std::vector< uint8_t > encode(EC_Point_Format format) const
CT::Mask< T > all_zeros(const T elem[], size_t len)
BigInt operator*(const BigInt &x, const BigInt &y)
EC_Point OS2ECP(const uint8_t data[], size_t data_len, const CurveGFp &curve)
BigInt sqrt_modulo_prime(const BigInt &a, const BigInt &p)
std::vector< T, secure_allocator< T > > secure_vector