10 #include <botan/point_gfp.h> 11 #include <botan/numthry.h> 12 #include <botan/reducer.h> 13 #include <botan/internal/mp_core.h> 14 #include <botan/internal/assert.h> 19 curve(curve), ws(2 * (curve.get_p_words() + 2))
22 coord_y = monty_mult(1, curve.
get_r2());
27 curve(curve), ws(2 * (curve.get_p_words() + 2))
29 if(x <= 0 || x >= curve.
get_p())
31 if(y <= 0 || y >= curve.
get_p())
33 coord_x = monty_mult(x, curve.
get_r2());
34 coord_y = monty_mult(y, curve.
get_r2());
35 coord_z = monty_mult(1, curve.
get_r2());
60 p.
data(), p_size, p_dash,
65 void PointGFp::monty_sqr(
BigInt& z,
const BigInt& x)
const 88 p.
data(), p_size, p_dash,
93 void PointGFp::add(
const PointGFp& rhs, std::vector<BigInt>& ws_bn)
97 coord_x = rhs.coord_x;
98 coord_y = rhs.coord_y;
99 coord_z = rhs.coord_z;
107 BigInt& rhs_z2 = ws_bn[0];
111 BigInt& lhs_z2 = ws_bn[3];
118 monty_sqr(rhs_z2, rhs.coord_z);
119 monty_mult(U1, coord_x, rhs_z2);
120 monty_mult(S1, coord_y, monty_mult(rhs.coord_z, rhs_z2));
122 monty_sqr(lhs_z2, coord_z);
123 monty_mult(U2, rhs.coord_x, lhs_z2);
124 monty_mult(S2, rhs.coord_y, monty_mult(coord_z, lhs_z2));
150 monty_mult(S2, U2, H);
152 U2 = monty_mult(U1, U2);
154 monty_sqr(coord_x, r);
156 coord_x -= (U2 << 1);
164 monty_mult(coord_y, r, U2);
165 coord_y -= monty_mult(S1, S2);
169 monty_mult(coord_z, monty_mult(coord_z, rhs.coord_z), H);
173 void PointGFp::mult2(std::vector<BigInt>& ws_bn)
195 monty_sqr(y_2, coord_y);
197 monty_mult(S, coord_x, y_2);
202 monty_sqr(z4, monty_sqr(coord_z));
203 monty_mult(a_z4, curve.
get_a_r(), z4);
205 M = 3 * monty_sqr(coord_x);
229 monty_mult(z, coord_y, coord_z);
242 std::vector<BigInt> ws(9);
261 *
this = scalar * *
this;
273 std::vector<BigInt> ws(9);
279 const bool z1_b = z1.
get_bit(bits_left - 1);
280 const bool z2_b = z2.
get_bit(bits_left - 1);
282 if(z1_b ==
true && z2_b ==
true)
305 std::vector<BigInt> ws(9);
307 if(scalar.
abs() <= 2)
322 const size_t scalar_bits = scalar.
bits();
329 size_t bits_left = scalar_bits;
334 const bool bit_set = scalar.
get_bit(bits_left - 1);
356 const size_t window_size = 4;
358 std::vector<PointGFp> Ps(1 << window_size);
362 for(
size_t i = 2; i != Ps.size(); ++i)
365 Ps[i].add(point, ws);
369 size_t bits_left = scalar_bits;
371 while(bits_left >= window_size)
373 for(
size_t i = 0; i != window_size; ++i)
379 H.add(Ps[nibble], ws);
381 bits_left -= window_size;
387 if(scalar.
get_bit(bits_left-1))
407 BigInt z2 = monty_sqr(coord_z);
410 z2 = monty_mult(z2, r2);
411 return monty_mult(coord_x, z2);
421 BigInt z3 = monty_mult(coord_z, monty_sqr(coord_z));
423 z3 = monty_mult(z3, r2);
424 return monty_mult(coord_y, z3);
439 BigInt y2 = monty_mult(monty_sqr(coord_y), 1);
440 BigInt x3 = monty_mult(coord_x, monty_sqr(coord_x));
446 BigInt z2 = monty_sqr(coord_z);
450 if(y2 != monty_mult(x3 + ax + b_r, 1))
454 BigInt z3 = monty_mult(coord_z, z2);
456 BigInt ax_z4 = monty_mult(ax, monty_sqr(z2));
458 BigInt b_z6 = monty_mult(b_r, monty_sqr(z3));
460 if(y2 != monty_mult(x3 + ax_z4 + b_z6, 1))
469 curve.
swap(other.curve);
470 coord_x.
swap(other.coord_x);
471 coord_y.
swap(other.coord_y);
472 coord_z.
swap(other.coord_z);
538 BigInt decompress_point(
bool yMod2,
547 g = g % curve.
get_p();
555 z = curve.
get_p() - z;
568 const byte pc = data[0];
572 if(pc == 2 || pc == 3)
577 const bool y_mod_2 = ((pc & 0x01) == 1);
578 y = decompress_point(y_mod_2, x, curve);
582 const size_t l = (data_len - 1) / 2;
588 else if(pc == 6 || pc == 7)
590 const size_t l = (data_len - 1) / 2;
596 const bool y_mod_2 = ((pc & 0x01) == 1);
598 if(decompress_point(y_mod_2, x, curve) != y)
599 throw Illegal_Point(
"OS2ECP: Decoding error in hybrid format");
607 throw Illegal_Point(
"OS2ECP: Decoded point was not on the curve");
bool get_bit(size_t n) const
PointGFp & operator*=(const BigInt &scalar)
size_t get_p_words() const
u32bit get_substring(size_t offset, size_t length) const
PointGFp OS2ECP(const byte data[], size_t data_len, const CurveGFp &curve)
SecureVector< word > & get_reg()
BigInt BOTAN_DLL ressol(const BigInt &x, const BigInt &p)
std::invalid_argument Invalid_Argument
void bigint_monty_sqr(word z[], size_t z_size, const word x[], size_t x_size, size_t x_sw, const word p[], size_t p_size, word p_dash, word workspace[])
BigInt get_affine_x() const
BigInt get_affine_y() const
const word * data() const
#define BOTAN_ASSERT(expr, msg)
SecureVector< byte > EC2OSP(const PointGFp &point, byte format)
void swap(PointGFp &other)
friend BOTAN_DLL PointGFp multi_exponentiate(const PointGFp &p1, const BigInt &z1, const PointGFp &p2, const BigInt &z2)
const BigInt & get_b_r() const
void swap(CurveGFp &other)
const BigInt & get_b() const
byte byte_at(size_t n) const
void bigint_monty_mul(word z[], size_t z_size, const word x[], size_t x_size, size_t x_sw, const word y[], size_t y_size, size_t y_sw, const word p[], size_t p_size, word p_dash, word workspace[])
bool on_the_curve() const
void swap(MemoryRegion< T > &other)
BigInt inverse_mod(const BigInt &n, const BigInt &mod)
const BigInt & get_a() const
bool operator==(const PointGFp &other) const
static BigInt decode(const byte buf[], size_t length, Base base=Binary)
friend BOTAN_DLL PointGFp operator*(const BigInt &scalar, const PointGFp &point)
const BigInt & get_r2() const
const BigInt & get_a_r() const
const CurveGFp & get_curve() const
PointGFp & operator+=(const PointGFp &rhs)
const BigInt & get_p() const
PointGFp & operator-=(const PointGFp &rhs)
void zeroise(MemoryRegion< T > &vec)
static SecureVector< byte > encode_1363(const BigInt &n, size_t bytes)