Botan  2.7.0
Crypto and TLS for C++11
Public Types | Public Member Functions | Static Public Member Functions | List of all members
Botan::PointGFp Class Referencefinal

#include <point_gfp.h>

Public Types

enum  { WORKSPACE_SIZE = 8 }
 
enum  Compression_Type { UNCOMPRESSED = 0, COMPRESSED = 1, HYBRID = 2 }
 

Public Member Functions

void add (const PointGFp &other, std::vector< BigInt > &workspace)
 
void add (const word x_words[], size_t x_size, const word y_words[], size_t y_size, const word z_words[], size_t z_size, std::vector< BigInt > &workspace)
 
void add_affine (const PointGFp &other, std::vector< BigInt > &workspace)
 
void add_affine (const word x_words[], size_t x_size, const word y_words[], size_t y_size, std::vector< BigInt > &workspace)
 
PointGFp double_of (std::vector< BigInt > &workspace) const
 
std::vector< uint8_t > encode (PointGFp::Compression_Type format) const
 
void force_affine ()
 
BigInt get_affine_x () const
 
BigInt get_affine_y () const
 
const CurveGFpget_curve () const
 
const BigIntget_x () const
 
const BigIntget_y () const
 
const BigIntget_z () const
 
bool is_affine () const
 
bool is_zero () const
 
void mult2 (std::vector< BigInt > &workspace)
 
void mult2i (size_t i, std::vector< BigInt > &workspace)
 
PointGFpnegate ()
 
bool on_the_curve () const
 
PointGFpoperator*= (const BigInt &scalar)
 
PointGFpoperator+= (const PointGFp &rhs)
 
PointGFpoperator-= (const PointGFp &rhs)
 
PointGFpoperator= (const PointGFp &)=default
 
PointGFpoperator= (PointGFp &&other)
 
bool operator== (const PointGFp &other) const
 
PointGFp plus (const PointGFp &other, std::vector< BigInt > &workspace) const
 
 PointGFp ()=default
 
 PointGFp (const CurveGFp &curve)
 
 PointGFp (const PointGFp &)=default
 
 PointGFp (PointGFp &&other)
 
 PointGFp (const CurveGFp &curve, const BigInt &x, const BigInt &y)
 
void randomize_repr (RandomNumberGenerator &rng)
 
void randomize_repr (RandomNumberGenerator &rng, secure_vector< word > &ws)
 
void swap (PointGFp &other)
 
void swap_coords (BigInt &new_x, BigInt &new_y, BigInt &new_z)
 
PointGFp zero () const
 

Static Public Member Functions

static void force_all_affine (std::vector< PointGFp > &points, secure_vector< word > &ws)
 

Detailed Description

This class represents one point on a curve of GF(p)

Definition at line 44 of file point_gfp.h.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
Enumerator
WORKSPACE_SIZE 

Definition at line 53 of file point_gfp.h.

◆ Compression_Type

Enumerator
UNCOMPRESSED 
COMPRESSED 
HYBRID 

Definition at line 47 of file point_gfp.h.

Constructor & Destructor Documentation

◆ PointGFp() [1/5]

Botan::PointGFp::PointGFp ( )
default

Construct an uninitialized PointGFp

Referenced by mult2(), mult2i(), and operator-=().

◆ PointGFp() [2/5]

Botan::PointGFp::PointGFp ( const CurveGFp curve)
explicit

Construct the zero point

Parameters
curveThe base curve

Definition at line 17 of file point_gfp.cpp.

17  :
18  m_curve(curve),
19  m_coord_x(0),
20  m_coord_y(curve.get_1_rep()),
21  m_coord_z(0)
22  {
23  // Assumes Montgomery rep of zero is zero
24  }

◆ PointGFp() [3/5]

Botan::PointGFp::PointGFp ( const PointGFp )
default

Copy constructor

◆ PointGFp() [4/5]

Botan::PointGFp::PointGFp ( PointGFp &&  other)
inline

Move Constructor

Definition at line 74 of file point_gfp.h.

75  {
76  this->swap(other);
77  }
void swap(PointGFp &other)
Definition: point_gfp.cpp:575

◆ PointGFp() [5/5]

Botan::PointGFp::PointGFp ( const CurveGFp curve,
const BigInt x,
const BigInt y 
)

Construct a point from its affine coordinates

Parameters
curvethe base curve
xaffine x coordinate
yaffine y coordinate

Definition at line 26 of file point_gfp.cpp.

References Botan::CurveGFp::get_p(), Botan::CurveGFp::get_ws_size(), and Botan::CurveGFp::to_rep().

26  :
27  m_curve(curve),
28  m_coord_x(x),
29  m_coord_y(y),
30  m_coord_z(m_curve.get_1_rep())
31  {
32  if(x <= 0 || x >= curve.get_p())
33  throw Invalid_Argument("Invalid PointGFp affine x");
34  if(y <= 0 || y >= curve.get_p())
35  throw Invalid_Argument("Invalid PointGFp affine y");
36 
37  secure_vector<word> monty_ws(m_curve.get_ws_size());
38  m_curve.to_rep(m_coord_x, monty_ws);
39  m_curve.to_rep(m_coord_y, monty_ws);
40  }
void to_rep(BigInt &x, secure_vector< word > &ws) const
Definition: curve_gfp.h:155
size_t get_ws_size() const
Definition: curve_gfp.h:137
const BigInt & get_1_rep() const
Definition: curve_gfp.h:143

Member Function Documentation

◆ add() [1/2]

void Botan::PointGFp::add ( const PointGFp other,
std::vector< BigInt > &  workspace 
)
inline

Point addition

Parameters
otherthe point to add to *this
workspacetemp space, at least WORKSPACE_SIZE elements

Definition at line 218 of file point_gfp.h.

References BOTAN_ASSERT_NOMSG, Botan::BigInt::data(), and Botan::BigInt::size().

Referenced by Botan::operator*(), operator+=(), and plus().

219  {
220  BOTAN_ASSERT_NOMSG(m_curve == other.m_curve);
221 
222  const size_t p_words = m_curve.get_p_words();
223 
224  add(other.m_coord_x.data(), std::min(p_words, other.m_coord_x.size()),
225  other.m_coord_y.data(), std::min(p_words, other.m_coord_y.size()),
226  other.m_coord_z.data(), std::min(p_words, other.m_coord_z.size()),
227  workspace);
228  }
size_t get_p_words() const
Definition: curve_gfp.h:135
#define BOTAN_ASSERT_NOMSG(expr)
Definition: assert.h:56
void add(const PointGFp &other, std::vector< BigInt > &workspace)
Definition: point_gfp.h:218

◆ add() [2/2]

void Botan::PointGFp::add ( const word  x_words[],
size_t  x_size,
const word  y_words[],
size_t  y_size,
const word  z_words[],
size_t  z_size,
std::vector< BigInt > &  workspace 
)

Point addition. Array version.

Parameters
x_wordsthe words of the x coordinate of the other point
x_sizesize of x_words
y_wordsthe words of the y coordinate of the other point
y_sizesize of y_words
z_wordsthe words of the z coordinate of the other point
z_sizesize of z_words
workspacetemp space, at least WORKSPACE_SIZE elements

Definition at line 170 of file point_gfp.cpp.

References Botan::CurveGFp::get_1_rep(), Botan::CurveGFp::get_p(), Botan::CurveGFp::get_ws_size(), is_zero(), Botan::BigInt::is_zero(), Botan::BigInt::mod_sub(), Botan::CurveGFp::mul(), mult2(), Botan::BigInt::set_words(), and Botan::CurveGFp::sqr().

174  {
175  if(all_zeros(x_words, x_size) && all_zeros(z_words, z_size))
176  return;
177 
178  if(is_zero())
179  {
180  m_coord_x.set_words(x_words, x_size);
181  m_coord_y.set_words(y_words, y_size);
182  m_coord_z.set_words(z_words, z_size);
183  return;
184  }
185 
186  resize_ws(ws_bn, m_curve.get_ws_size());
187 
188  secure_vector<word>& ws = ws_bn[0].get_word_vector();
189  secure_vector<word>& sub_ws = ws_bn[1].get_word_vector();
190 
191  BigInt& T0 = ws_bn[2];
192  BigInt& T1 = ws_bn[3];
193  BigInt& T2 = ws_bn[4];
194  BigInt& T3 = ws_bn[5];
195  BigInt& T4 = ws_bn[6];
196  BigInt& T5 = ws_bn[7];
197 
198  /*
199  https://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#addition-add-1998-cmo-2
200  */
201 
202  const BigInt& p = m_curve.get_p();
203 
204  m_curve.sqr(T0, z_words, z_size, ws); // z2^2
205  m_curve.mul(T1, m_coord_x, T0, ws); // x1*z2^2
206  m_curve.mul(T3, z_words, z_size, T0, ws); // z2^3
207  m_curve.mul(T2, m_coord_y, T3, ws); // y1*z2^3
208 
209  m_curve.sqr(T3, m_coord_z, ws); // z1^2
210  m_curve.mul(T4, x_words, x_size, T3, ws); // x2*z1^2
211 
212  m_curve.mul(T5, m_coord_z, T3, ws); // z1^3
213  m_curve.mul(T0, y_words, y_size, T5, ws); // y2*z1^3
214 
215  T4.mod_sub(T1, p, sub_ws); // x2*z1^2 - x1*z2^2
216 
217  T0.mod_sub(T2, p, sub_ws);
218 
219  if(T4.is_zero())
220  {
221  if(T0.is_zero())
222  {
223  mult2(ws_bn);
224  return;
225  }
226 
227  // setting to zero:
228  m_coord_x = 0;
229  m_coord_y = m_curve.get_1_rep();
230  m_coord_z = 0;
231  return;
232  }
233 
234  m_curve.sqr(T5, T4, ws);
235 
236  m_curve.mul(T3, T1, T5, ws);
237 
238  m_curve.mul(T1, T5, T4, ws);
239 
240  m_curve.sqr(m_coord_x, T0, ws);
241  m_coord_x.mod_sub(T1, p, sub_ws);
242  m_coord_x.mod_sub(T3, p, sub_ws);
243  m_coord_x.mod_sub(T3, p, sub_ws);
244 
245  T3.mod_sub(m_coord_x, p, sub_ws);
246 
247  m_curve.mul(m_coord_y, T0, T3, ws);
248  m_curve.mul(T3, T2, T1, ws);
249 
250  m_coord_y.mod_sub(T3, p, sub_ws);
251 
252  m_curve.mul(T3, z_words, z_size, m_coord_z, ws);
253  m_curve.mul(m_coord_z, T3, T4, ws);
254  }
BigInt & mod_sub(const BigInt &y, const BigInt &mod, secure_vector< word > &ws)
Definition: big_ops2.cpp:124
void mul(BigInt &z, const BigInt &x, const BigInt &y, secure_vector< word > &ws) const
Definition: curve_gfp.h:179
size_t get_ws_size() const
Definition: curve_gfp.h:137
const BigInt & get_1_rep() const
Definition: curve_gfp.h:143
void set_words(const word w[], size_t len)
Definition: bigint.h:450
void mult2(std::vector< BigInt > &workspace)
Definition: point_gfp.cpp:276
bool is_zero() const
Definition: point_gfp.h:180
const BigInt & get_p() const
Definition: curve_gfp.h:133
void sqr(BigInt &z, const BigInt &x, secure_vector< word > &ws) const
Definition: curve_gfp.h:190

◆ add_affine() [1/2]

void Botan::PointGFp::add_affine ( const PointGFp other,
std::vector< BigInt > &  workspace 
)
inline

Point addition - mixed J+A

Parameters
otheraffine point to add - assumed to be affine!
workspacetemp space, at least WORKSPACE_SIZE elements

Definition at line 251 of file point_gfp.h.

References BOTAN_ASSERT_NOMSG, BOTAN_DEBUG_ASSERT, Botan::BigInt::data(), is_affine(), and Botan::BigInt::size().

Referenced by Botan::PointGFp_Base_Point_Precompute::mul(), and Botan::PointGFp_Multi_Point_Precompute::multi_exp().

252  {
253  BOTAN_ASSERT_NOMSG(m_curve == other.m_curve);
254  BOTAN_DEBUG_ASSERT(other.is_affine());
255 
256  const size_t p_words = m_curve.get_p_words();
257  add_affine(other.m_coord_x.data(), std::min(p_words, other.m_coord_x.size()),
258  other.m_coord_y.data(), std::min(p_words, other.m_coord_y.size()),
259  workspace);
260  }
size_t get_p_words() const
Definition: curve_gfp.h:135
#define BOTAN_ASSERT_NOMSG(expr)
Definition: assert.h:56
#define BOTAN_DEBUG_ASSERT(expr)
Definition: assert.h:111
void add_affine(const PointGFp &other, std::vector< BigInt > &workspace)
Definition: point_gfp.h:251

◆ add_affine() [2/2]

void Botan::PointGFp::add_affine ( const word  x_words[],
size_t  x_size,
const word  y_words[],
size_t  y_size,
std::vector< BigInt > &  workspace 
)

Point addition - mixed J+A. Array version.

Parameters
x_wordsthe words of the x coordinate of the other point
x_sizesize of x_words
y_wordsthe words of the y coordinate of the other point
y_sizesize of y_words
workspacetemp space, at least WORKSPACE_SIZE elements

Definition at line 89 of file point_gfp.cpp.

References Botan::CurveGFp::get_1_rep(), Botan::CurveGFp::get_p(), Botan::BigInt::get_word_vector(), Botan::CurveGFp::get_ws_size(), is_zero(), Botan::BigInt::is_zero(), Botan::BigInt::mod_sub(), Botan::CurveGFp::mul(), mult2(), Botan::BigInt::set_words(), and Botan::CurveGFp::sqr().

92  {
93  if(all_zeros(x_words, x_size) && all_zeros(y_words, y_size))
94  return;
95 
96  if(is_zero())
97  {
98  m_coord_x.set_words(x_words, x_size);
99  m_coord_y.set_words(y_words, y_size);
100  m_coord_z = m_curve.get_1_rep();
101  return;
102  }
103 
104  resize_ws(ws_bn, m_curve.get_ws_size());
105 
106  secure_vector<word>& ws = ws_bn[0].get_word_vector();
107  secure_vector<word>& sub_ws = ws_bn[1].get_word_vector();
108 
109  BigInt& T0 = ws_bn[2];
110  BigInt& T1 = ws_bn[3];
111  BigInt& T2 = ws_bn[4];
112  BigInt& T3 = ws_bn[5];
113  BigInt& T4 = ws_bn[6];
114 
115  /*
116  https://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#addition-add-1998-cmo-2
117  simplified with Z2 = 1
118  */
119 
120  const BigInt& p = m_curve.get_p();
121 
122  m_curve.sqr(T3, m_coord_z, ws); // z1^2
123  m_curve.mul(T4, x_words, x_size, T3, ws); // x2*z1^2
124 
125  m_curve.mul(T2, m_coord_z, T3, ws); // z1^3
126  m_curve.mul(T0, y_words, y_size, T2, ws); // y2*z1^3
127 
128  T4.mod_sub(m_coord_x, p, sub_ws); // x2*z1^2 - x1*z2^2
129 
130  T0.mod_sub(m_coord_y, p, sub_ws);
131 
132  if(T4.is_zero())
133  {
134  if(T0.is_zero())
135  {
136  mult2(ws_bn);
137  return;
138  }
139 
140  // setting to zero:
141  m_coord_x = 0;
142  m_coord_y = m_curve.get_1_rep();
143  m_coord_z = 0;
144  return;
145  }
146 
147  m_curve.sqr(T2, T4, ws);
148 
149  m_curve.mul(T3, m_coord_x, T2, ws);
150 
151  m_curve.mul(T1, T2, T4, ws);
152 
153  m_curve.sqr(m_coord_x, T0, ws);
154  m_coord_x.mod_sub(T1, p, sub_ws);
155  m_coord_x.mod_sub(T3, p, sub_ws);
156  m_coord_x.mod_sub(T3, p, sub_ws);
157 
158  T3.mod_sub(m_coord_x, p, sub_ws);
159 
160  T2 = m_coord_y;
161  m_curve.mul(T2, T0, T3, ws);
162  m_curve.mul(T3, m_coord_y, T1, ws);
163  T2.mod_sub(T3, p, sub_ws);
164  m_coord_y = T2;
165 
166  m_curve.mul(T3, m_coord_z, T4, ws);
167  m_coord_z = T3;
168  }
secure_vector< word > & get_word_vector()
Definition: bigint.h:553
BigInt & mod_sub(const BigInt &y, const BigInt &mod, secure_vector< word > &ws)
Definition: big_ops2.cpp:124
void mul(BigInt &z, const BigInt &x, const BigInt &y, secure_vector< word > &ws) const
Definition: curve_gfp.h:179
size_t get_ws_size() const
Definition: curve_gfp.h:137
const BigInt & get_1_rep() const
Definition: curve_gfp.h:143
void set_words(const word w[], size_t len)
Definition: bigint.h:450
void mult2(std::vector< BigInt > &workspace)
Definition: point_gfp.cpp:276
bool is_zero() const
Definition: point_gfp.h:180
const BigInt & get_p() const
Definition: curve_gfp.h:133
void sqr(BigInt &z, const BigInt &x, secure_vector< word > &ws) const
Definition: curve_gfp.h:190

◆ double_of()

PointGFp Botan::PointGFp::double_of ( std::vector< BigInt > &  workspace) const
inline

Point doubling

Parameters
workspacetemp space, at least WORKSPACE_SIZE elements
Returns
*this doubled

Definition at line 306 of file point_gfp.h.

References mult2().

307  {
308  PointGFp x = (*this);
309  x.mult2(workspace);
310  return x;
311  }
PointGFp()=default

◆ encode()

std::vector< uint8_t > Botan::PointGFp::encode ( PointGFp::Compression_Type  format) const

EC2OSP - elliptic curve to octet string primitive

Parameters
formatwhich format to encode using

Definition at line 597 of file point_gfp.cpp.

References Botan::BigInt::bytes(), COMPRESSED, Botan::BigInt::encode_1363(), get_affine_x(), get_affine_y(), Botan::BigInt::get_bit(), Botan::CurveGFp::get_p(), HYBRID, is_zero(), and UNCOMPRESSED.

Referenced by Botan::ECIES_Encryptor::ECIES_Encryptor(), and Botan::EC_PublicKey::public_key_bits().

598  {
599  if(is_zero())
600  return std::vector<uint8_t>(1); // single 0 byte
601 
602  const size_t p_bytes = m_curve.get_p().bytes();
603 
604  const BigInt x = get_affine_x();
605  const BigInt y = get_affine_y();
606 
607  std::vector<uint8_t> result;
608 
609  if(format == PointGFp::UNCOMPRESSED)
610  {
611  result.resize(1 + 2*p_bytes);
612  result[0] = 0x04;
613  BigInt::encode_1363(&result[1], p_bytes, x);
614  BigInt::encode_1363(&result[1+p_bytes], p_bytes, y);
615  }
616  else if(format == PointGFp::COMPRESSED)
617  {
618  result.resize(1 + p_bytes);
619  result[0] = 0x02 | static_cast<uint8_t>(y.get_bit(0));
620  BigInt::encode_1363(&result[1], p_bytes, x);
621  }
622  else if(format == PointGFp::HYBRID)
623  {
624  result.resize(1 + 2*p_bytes);
625  result[0] = 0x06 | static_cast<uint8_t>(y.get_bit(0));
626  BigInt::encode_1363(&result[1], p_bytes, x);
627  BigInt::encode_1363(&result[1+p_bytes], p_bytes, y);
628  }
629  else
630  throw Invalid_Argument("EC2OSP illegal point encoding");
631 
632  return result;
633  }
BigInt get_affine_x() const
Definition: point_gfp.cpp:501
BigInt get_affine_y() const
Definition: point_gfp.cpp:520
size_t bytes() const
Definition: bigint.cpp:220
bool is_zero() const
Definition: point_gfp.h:180
static secure_vector< uint8_t > encode_1363(const BigInt &n, size_t bytes)
Definition: big_code.cpp:82
const BigInt & get_p() const
Definition: curve_gfp.h:133

◆ force_affine()

void Botan::PointGFp::force_affine ( )

Force this point to affine coordinates

Definition at line 481 of file point_gfp.cpp.

References Botan::CurveGFp::get_1_rep(), Botan::CurveGFp::invert_element(), is_zero(), Botan::CurveGFp::mul_to_tmp(), and Botan::CurveGFp::sqr_to_tmp().

Referenced by force_all_affine().

482  {
483  if(is_zero())
484  throw Invalid_State("Cannot convert zero ECC point to affine");
485 
486  secure_vector<word> ws;
487 
488  const BigInt z_inv = m_curve.invert_element(m_coord_z, ws);
489  const BigInt z2_inv = m_curve.sqr_to_tmp(z_inv, ws);
490  const BigInt z3_inv = m_curve.mul_to_tmp(z_inv, z2_inv, ws);
491  m_coord_x = m_curve.mul_to_tmp(m_coord_x, z2_inv, ws);
492  m_coord_y = m_curve.mul_to_tmp(m_coord_y, z3_inv, ws);
493  m_coord_z = m_curve.get_1_rep();
494  }
const BigInt & get_1_rep() const
Definition: curve_gfp.h:143
BigInt invert_element(const BigInt &x, secure_vector< word > &ws) const
Definition: curve_gfp.h:150
BigInt sqr_to_tmp(const BigInt &x, secure_vector< word > &ws) const
Definition: curve_gfp.h:217
BigInt mul_to_tmp(const BigInt &x, const BigInt &y, secure_vector< word > &ws) const
Definition: curve_gfp.h:210
bool is_zero() const
Definition: point_gfp.h:180

◆ force_all_affine()

void Botan::PointGFp::force_all_affine ( std::vector< PointGFp > &  points,
secure_vector< word > &  ws 
)
static

Force all points on the list to affine coordinates

Definition at line 422 of file point_gfp.cpp.

References force_affine(), Botan::CurveGFp::get_1_rep(), Botan::CurveGFp::get_ws_size(), Botan::CurveGFp::invert_element(), Botan::CurveGFp::mul(), Botan::CurveGFp::mul_to_tmp(), and Botan::CurveGFp::sqr().

Referenced by Botan::PointGFp_Multi_Point_Precompute::PointGFp_Multi_Point_Precompute().

424  {
425  if(points.size() <= 1)
426  {
427  for(size_t i = 0; i != points.size(); ++i)
428  points[i].force_affine();
429  return;
430  }
431 
432  /*
433  For >= 2 points use Montgomery's trick
434 
435  See Algorithm 2.26 in "Guide to Elliptic Curve Cryptography"
436  (Hankerson, Menezes, Vanstone)
437 
438  TODO is it really necessary to save all k points in c?
439  */
440 
441  const CurveGFp& curve = points[0].m_curve;
442  const BigInt& rep_1 = curve.get_1_rep();
443 
444  if(ws.size() < curve.get_ws_size())
445  ws.resize(curve.get_ws_size());
446 
447  std::vector<BigInt> c(points.size());
448  c[0] = points[0].m_coord_z;
449 
450  for(size_t i = 1; i != points.size(); ++i)
451  {
452  curve.mul(c[i], c[i-1], points[i].m_coord_z, ws);
453  }
454 
455  BigInt s_inv = curve.invert_element(c[c.size()-1], ws);
456 
457  BigInt z_inv, z2_inv, z3_inv;
458 
459  for(size_t i = points.size() - 1; i != 0; i--)
460  {
461  PointGFp& point = points[i];
462 
463  curve.mul(z_inv, s_inv, c[i-1], ws);
464 
465  s_inv = curve.mul_to_tmp(s_inv, point.m_coord_z, ws);
466 
467  curve.sqr(z2_inv, z_inv, ws);
468  curve.mul(z3_inv, z2_inv, z_inv, ws);
469  point.m_coord_x = curve.mul_to_tmp(point.m_coord_x, z2_inv, ws);
470  point.m_coord_y = curve.mul_to_tmp(point.m_coord_y, z3_inv, ws);
471  point.m_coord_z = rep_1;
472  }
473 
474  curve.sqr(z2_inv, s_inv, ws);
475  curve.mul(z3_inv, z2_inv, s_inv, ws);
476  points[0].m_coord_x = curve.mul_to_tmp(points[0].m_coord_x, z2_inv, ws);
477  points[0].m_coord_y = curve.mul_to_tmp(points[0].m_coord_y, z3_inv, ws);
478  points[0].m_coord_z = rep_1;
479  }
void force_affine()
Definition: point_gfp.cpp:481
PointGFp()=default

◆ get_affine_x()

BigInt Botan::PointGFp::get_affine_x ( ) const

get affine x coordinate

Returns
affine x coordinate

Definition at line 501 of file point_gfp.cpp.

References Botan::CurveGFp::from_rep(), Botan::CurveGFp::invert_element(), is_affine(), is_zero(), Botan::CurveGFp::mul(), and Botan::CurveGFp::sqr_to_tmp().

Referenced by Botan::EC_Group::blinded_base_point_multiply_x(), encode(), operator==(), Botan::GOST_3410_PublicKey::public_key_bits(), and Botan::sm2_compute_za().

502  {
503  if(is_zero())
504  throw Illegal_Transformation("Cannot convert zero point to affine");
505 
506  secure_vector<word> monty_ws;
507 
508  if(is_affine())
509  return m_curve.from_rep(m_coord_x, monty_ws);
510 
511  BigInt z2 = m_curve.sqr_to_tmp(m_coord_z, monty_ws);
512  z2 = m_curve.invert_element(z2, monty_ws);
513 
514  BigInt r;
515  m_curve.mul(r, m_coord_x, z2, monty_ws);
516  m_curve.from_rep(r, monty_ws);
517  return r;
518  }
bool is_affine() const
Definition: point_gfp.cpp:496
void mul(BigInt &z, const BigInt &x, const BigInt &y, secure_vector< word > &ws) const
Definition: curve_gfp.h:179
void from_rep(BigInt &x, secure_vector< word > &ws) const
Definition: curve_gfp.h:160
BigInt invert_element(const BigInt &x, secure_vector< word > &ws) const
Definition: curve_gfp.h:150
BigInt sqr_to_tmp(const BigInt &x, secure_vector< word > &ws) const
Definition: curve_gfp.h:217
bool is_zero() const
Definition: point_gfp.h:180

◆ get_affine_y()

BigInt Botan::PointGFp::get_affine_y ( ) const

get affine y coordinate

Returns
affine y coordinate

Definition at line 520 of file point_gfp.cpp.

References Botan::CurveGFp::from_rep(), Botan::CurveGFp::invert_element(), is_affine(), is_zero(), Botan::CurveGFp::mul(), Botan::CurveGFp::mul_to_tmp(), and Botan::CurveGFp::sqr_to_tmp().

Referenced by encode(), operator==(), Botan::GOST_3410_PublicKey::public_key_bits(), and Botan::sm2_compute_za().

521  {
522  if(is_zero())
523  throw Illegal_Transformation("Cannot convert zero point to affine");
524 
525  secure_vector<word> monty_ws;
526 
527  if(is_affine())
528  return m_curve.from_rep(m_coord_y, monty_ws);
529 
530  const BigInt z2 = m_curve.sqr_to_tmp(m_coord_z, monty_ws);
531  const BigInt z3 = m_curve.mul_to_tmp(m_coord_z, z2, monty_ws);
532  const BigInt z3_inv = m_curve.invert_element(z3, monty_ws);
533 
534  BigInt r;
535  m_curve.mul(r, m_coord_y, z3_inv, monty_ws);
536  m_curve.from_rep(r, monty_ws);
537  return r;
538  }
bool is_affine() const
Definition: point_gfp.cpp:496
void mul(BigInt &z, const BigInt &x, const BigInt &y, secure_vector< word > &ws) const
Definition: curve_gfp.h:179
void from_rep(BigInt &x, secure_vector< word > &ws) const
Definition: curve_gfp.h:160
BigInt invert_element(const BigInt &x, secure_vector< word > &ws) const
Definition: curve_gfp.h:150
BigInt sqr_to_tmp(const BigInt &x, secure_vector< word > &ws) const
Definition: curve_gfp.h:217
BigInt mul_to_tmp(const BigInt &x, const BigInt &y, secure_vector< word > &ws) const
Definition: curve_gfp.h:210
bool is_zero() const
Definition: point_gfp.h:180

◆ get_curve()

const CurveGFp& Botan::PointGFp::get_curve ( ) const
inline

Return base curve of this point

Returns
the curve over GF(p) of this point

You should not need to use this

Definition at line 324 of file point_gfp.h.

Referenced by Botan::EC_PublicKey::EC_PublicKey().

324 { return m_curve; }

◆ get_x()

const BigInt& Botan::PointGFp::get_x ( ) const
inline

Definition at line 152 of file point_gfp.h.

152 { return m_coord_x; }

◆ get_y()

const BigInt& Botan::PointGFp::get_y ( ) const
inline

Definition at line 153 of file point_gfp.h.

153 { return m_coord_y; }

◆ get_z()

const BigInt& Botan::PointGFp::get_z ( ) const
inline

Definition at line 154 of file point_gfp.h.

154 { return m_coord_z; }

◆ is_affine()

bool Botan::PointGFp::is_affine ( ) const

Definition at line 496 of file point_gfp.cpp.

References Botan::CurveGFp::is_one().

Referenced by add_affine(), get_affine_x(), and get_affine_y().

497  {
498  return m_curve.is_one(m_coord_z);
499  }
bool is_one(const BigInt &x) const
Definition: curve_gfp.h:148

◆ is_zero()

bool Botan::PointGFp::is_zero ( ) const
inline

Is this the point at infinity?

Returns
true, if this point is at infinity, false otherwise.

Definition at line 180 of file point_gfp.h.

Referenced by add(), add_affine(), Botan::EC_Group::blinded_base_point_multiply_x(), Botan::ECIES_KA_Operation::derive_secret(), encode(), force_affine(), get_affine_x(), get_affine_y(), mult2(), on_the_curve(), operator-=(), operator==(), and Botan::EC_Group::verify_public_element().

181  { return (m_coord_x.is_zero() && m_coord_z.is_zero()); }
bool is_zero() const
Definition: bigint.h:355

◆ mult2()

void Botan::PointGFp::mult2 ( std::vector< BigInt > &  workspace)

Point doubling

Parameters
workspacetemp space, at least WORKSPACE_SIZE elements

Definition at line 276 of file point_gfp.cpp.

References Botan::CurveGFp::a_is_minus_3(), Botan::CurveGFp::a_is_zero(), Botan::CurveGFp::get_a_rep(), Botan::CurveGFp::get_p(), Botan::CurveGFp::get_ws_size(), is_zero(), Botan::BigInt::is_zero(), Botan::BigInt::mod_add(), Botan::BigInt::mod_sub(), Botan::CurveGFp::mul(), PointGFp(), Botan::CurveGFp::redc_mod_p(), and Botan::CurveGFp::sqr().

Referenced by add(), add_affine(), double_of(), mult2i(), Botan::operator*(), and Botan::PointGFp_Multi_Point_Precompute::PointGFp_Multi_Point_Precompute().

277  {
278  if(is_zero())
279  return;
280 
281  if(m_coord_y.is_zero())
282  {
283  *this = PointGFp(m_curve); // setting myself to zero
284  return;
285  }
286 
287  resize_ws(ws_bn, m_curve.get_ws_size());
288 
289  secure_vector<word>& ws = ws_bn[0].get_word_vector();
290  secure_vector<word>& sub_ws = ws_bn[1].get_word_vector();
291 
292  BigInt& T0 = ws_bn[2];
293  BigInt& T1 = ws_bn[3];
294  BigInt& T2 = ws_bn[4];
295  BigInt& T3 = ws_bn[5];
296  BigInt& T4 = ws_bn[6];
297 
298  /*
299  https://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#doubling-dbl-1986-cc
300  */
301  const BigInt& p = m_curve.get_p();
302 
303  m_curve.sqr(T0, m_coord_y, ws);
304 
305  m_curve.mul(T1, m_coord_x, T0, ws);
306  T1 <<= 2; // * 4
307  m_curve.redc_mod_p(T1, sub_ws);
308 
309  if(m_curve.a_is_zero())
310  {
311  // if a == 0 then 3*x^2 + a*z^4 is just 3*x^2
312  m_curve.sqr(T4, m_coord_x, ws); // x^2
313  T4 *= 3; // 3*x^2
314  m_curve.redc_mod_p(T4, sub_ws);
315  }
316  else if(m_curve.a_is_minus_3())
317  {
318  /*
319  if a == -3 then
320  3*x^2 + a*z^4 == 3*x^2 - 3*z^4 == 3*(x^2-z^4) == 3*(x-z^2)*(x+z^2)
321  */
322  m_curve.sqr(T3, m_coord_z, ws); // z^2
323 
324  // (x-z^2)
325  T2 = m_coord_x;
326  T2.mod_sub(T3, p, sub_ws);
327 
328  // (x+z^2)
329  T3.mod_add(m_coord_x, p, sub_ws);
330 
331  m_curve.mul(T4, T2, T3, ws); // (x-z^2)*(x+z^2)
332 
333  T4 *= 3; // 3*(x-z^2)*(x+z^2)
334  m_curve.redc_mod_p(T4, sub_ws);
335  }
336  else
337  {
338  m_curve.sqr(T3, m_coord_z, ws); // z^2
339  m_curve.sqr(T4, T3, ws); // z^4
340  m_curve.mul(T3, m_curve.get_a_rep(), T4, ws); // a*z^4
341 
342  m_curve.sqr(T4, m_coord_x, ws); // x^2
343  T4 *= 3; // 3*x^2
344  T4.mod_add(T3, p, sub_ws); // 3*x^2 + a*z^4
345  }
346 
347  m_curve.sqr(T2, T4, ws);
348  T2.mod_sub(T1, p, sub_ws);
349  T2.mod_sub(T1, p, sub_ws);
350 
351  m_curve.sqr(T3, T0, ws);
352  T3 <<= 3;
353  m_curve.redc_mod_p(T3, sub_ws);
354 
355  T1.mod_sub(T2, p, sub_ws);
356 
357  m_curve.mul(T0, T4, T1, ws);
358  T0.mod_sub(T3, p, sub_ws);
359 
360  m_coord_x = T2;
361 
362  m_curve.mul(T2, m_coord_y, m_coord_z, ws);
363  T2 <<= 1;
364  m_curve.redc_mod_p(T2, sub_ws);
365 
366  m_coord_y = T0;
367  m_coord_z = T2;
368  }
bool a_is_minus_3() const
Definition: curve_gfp.h:145
const BigInt & get_a_rep() const
Definition: curve_gfp.h:139
void redc_mod_p(BigInt &z, secure_vector< word > &ws) const
Definition: curve_gfp.h:174
BigInt & mod_sub(const BigInt &y, const BigInt &mod, secure_vector< word > &ws)
Definition: big_ops2.cpp:124
bool is_zero() const
Definition: bigint.h:355
void mul(BigInt &z, const BigInt &x, const BigInt &y, secure_vector< word > &ws) const
Definition: curve_gfp.h:179
size_t get_ws_size() const
Definition: curve_gfp.h:137
PointGFp()=default
BigInt & mod_add(const BigInt &y, const BigInt &mod, secure_vector< word > &ws)
Definition: big_ops2.cpp:112
bool a_is_zero() const
Definition: curve_gfp.h:146
bool is_zero() const
Definition: point_gfp.h:180
const BigInt & get_p() const
Definition: curve_gfp.h:133
void sqr(BigInt &z, const BigInt &x, secure_vector< word > &ws) const
Definition: curve_gfp.h:190

◆ mult2i()

void Botan::PointGFp::mult2i ( size_t  i,
std::vector< BigInt > &  workspace 
)

Repeated point doubling

Parameters
inumber of doublings to perform
workspacetemp space, at least WORKSPACE_SIZE elements

Definition at line 256 of file point_gfp.cpp.

References Botan::BigInt::is_zero(), mult2(), and PointGFp().

Referenced by Botan::PointGFp_Multi_Point_Precompute::multi_exp().

257  {
258  if(iterations == 0)
259  return;
260 
261  if(m_coord_y.is_zero())
262  {
263  *this = PointGFp(m_curve); // setting myself to zero
264  return;
265  }
266 
267  /*
268  TODO we can save 2 squarings per iteration by computing
269  a*Z^4 using values cached from previous iteration
270  */
271  for(size_t i = 0; i != iterations; ++i)
272  mult2(ws_bn);
273  }
bool is_zero() const
Definition: bigint.h:355
PointGFp()=default
void mult2(std::vector< BigInt > &workspace)
Definition: point_gfp.cpp:276

◆ negate()

PointGFp& Botan::PointGFp::negate ( )
inline

Negate this point

Returns
*this

Definition at line 133 of file point_gfp.h.

References Botan::CT::is_zero().

Referenced by Botan::PointGFp_Multi_Point_Precompute::multi_exp(), and Botan::operator-().

134  {
135  if(!is_zero())
136  m_coord_y = m_curve.get_p() - m_coord_y;
137  return *this;
138  }
bool is_zero() const
Definition: point_gfp.h:180
const BigInt & get_p() const
Definition: curve_gfp.h:133

◆ on_the_curve()

bool Botan::PointGFp::on_the_curve ( ) const

Checks whether the point is to be found on the underlying curve; used to prevent fault attacks.

Returns
if the point is on the curve

Definition at line 540 of file point_gfp.cpp.

References Botan::CurveGFp::from_rep(), Botan::CurveGFp::get_a_rep(), Botan::CurveGFp::get_b_rep(), is_zero(), Botan::CurveGFp::mul_to_tmp(), and Botan::CurveGFp::sqr_to_tmp().

Referenced by Botan::EC_PrivateKey::EC_PrivateKey(), Botan::GOST_3410_PublicKey::GOST_3410_PublicKey(), Botan::PointGFp_Base_Point_Precompute::mul(), Botan::operator*(), Botan::EC_Group::verify_group(), and Botan::EC_Group::verify_public_element().

541  {
542  /*
543  Is the point still on the curve?? (If everything is correct, the
544  point is always on its curve; then the function will return true.
545  If somehow the state is corrupted, which suggests a fault attack
546  (or internal computational error), then return false.
547  */
548  if(is_zero())
549  return true;
550 
551  secure_vector<word> monty_ws;
552 
553  const BigInt y2 = m_curve.from_rep(m_curve.sqr_to_tmp(m_coord_y, monty_ws), monty_ws);
554  const BigInt x3 = m_curve.mul_to_tmp(m_coord_x, m_curve.sqr_to_tmp(m_coord_x, monty_ws), monty_ws);
555  const BigInt ax = m_curve.mul_to_tmp(m_coord_x, m_curve.get_a_rep(), monty_ws);
556  const BigInt z2 = m_curve.sqr_to_tmp(m_coord_z, monty_ws);
557 
558  if(m_coord_z == z2) // Is z equal to 1 (in Montgomery form)?
559  {
560  if(y2 != m_curve.from_rep(x3 + ax + m_curve.get_b_rep(), monty_ws))
561  return false;
562  }
563 
564  const BigInt z3 = m_curve.mul_to_tmp(m_coord_z, z2, monty_ws);
565  const BigInt ax_z4 = m_curve.mul_to_tmp(ax, m_curve.sqr_to_tmp(z2, monty_ws), monty_ws);
566  const BigInt b_z6 = m_curve.mul_to_tmp(m_curve.get_b_rep(), m_curve.sqr_to_tmp(z3, monty_ws), monty_ws);
567 
568  if(y2 != m_curve.from_rep(x3 + ax_z4 + b_z6, monty_ws))
569  return false;
570 
571  return true;
572  }
const BigInt & get_a_rep() const
Definition: curve_gfp.h:139
const BigInt & get_b_rep() const
Definition: curve_gfp.h:141
void from_rep(BigInt &x, secure_vector< word > &ws) const
Definition: curve_gfp.h:160
BigInt sqr_to_tmp(const BigInt &x, secure_vector< word > &ws) const
Definition: curve_gfp.h:217
BigInt mul_to_tmp(const BigInt &x, const BigInt &y, secure_vector< word > &ws) const
Definition: curve_gfp.h:210
bool is_zero() const
Definition: point_gfp.h:180

◆ operator*=()

PointGFp & Botan::PointGFp::operator*= ( const BigInt scalar)

*= Operator

Parameters
scalarthe PointGFp to multiply with *this
Returns
resulting PointGFp

Definition at line 390 of file point_gfp.cpp.

391  {
392  *this = scalar * *this;
393  return *this;
394  }

◆ operator+=()

PointGFp & Botan::PointGFp::operator+= ( const PointGFp rhs)

+= Operator

Parameters
rhsthe PointGFp to add to the local value
Returns
resulting PointGFp

Definition at line 371 of file point_gfp.cpp.

References add(), and WORKSPACE_SIZE.

372  {
373  std::vector<BigInt> ws(PointGFp::WORKSPACE_SIZE);
374  add(rhs, ws);
375  return *this;
376  }
void add(const PointGFp &other, std::vector< BigInt > &workspace)
Definition: point_gfp.h:218

◆ operator-=()

PointGFp & Botan::PointGFp::operator-= ( const PointGFp rhs)

-= Operator

Parameters
rhsthe PointGFp to subtract from the local value
Returns
resulting PointGFp

Definition at line 378 of file point_gfp.cpp.

References is_zero(), and PointGFp().

379  {
380  PointGFp minus_rhs = PointGFp(rhs).negate();
381 
382  if(is_zero())
383  *this = minus_rhs;
384  else
385  *this += minus_rhs;
386 
387  return *this;
388  }
PointGFp()=default
bool is_zero() const
Definition: point_gfp.h:180

◆ operator=() [1/2]

PointGFp& Botan::PointGFp::operator= ( const PointGFp )
default

Standard Assignment

◆ operator=() [2/2]

PointGFp& Botan::PointGFp::operator= ( PointGFp &&  other)
inline

Move Assignment

Definition at line 87 of file point_gfp.h.

88  {
89  if(this != &other)
90  this->swap(other);
91  return (*this);
92  }
void swap(PointGFp &other)
Definition: point_gfp.cpp:575

◆ operator==()

bool Botan::PointGFp::operator== ( const PointGFp other) const

Equality operator

Definition at line 583 of file point_gfp.cpp.

References get_affine_x(), get_affine_y(), and is_zero().

584  {
585  if(m_curve != other.m_curve)
586  return false;
587 
588  // If this is zero, only equal if other is also zero
589  if(is_zero())
590  return other.is_zero();
591 
592  return (get_affine_x() == other.get_affine_x() &&
593  get_affine_y() == other.get_affine_y());
594  }
BigInt get_affine_x() const
Definition: point_gfp.cpp:501
BigInt get_affine_y() const
Definition: point_gfp.cpp:520
bool is_zero() const
Definition: point_gfp.h:180

◆ plus()

PointGFp Botan::PointGFp::plus ( const PointGFp other,
std::vector< BigInt > &  workspace 
) const
inline

Point addition

Parameters
otherthe point to add to *this
workspacetemp space, at least WORKSPACE_SIZE elements
Returns
other plus *this

Definition at line 294 of file point_gfp.h.

References add().

Referenced by Botan::PointGFp_Multi_Point_Precompute::PointGFp_Multi_Point_Precompute().

295  {
296  PointGFp x = (*this);
297  x.add(other, workspace);
298  return x;
299  }
PointGFp()=default

◆ randomize_repr() [1/2]

void Botan::PointGFp::randomize_repr ( RandomNumberGenerator rng)

Randomize the point representation The actual value (get_affine_x, get_affine_y) does not change

Definition at line 42 of file point_gfp.cpp.

References Botan::CurveGFp::get_ws_size().

Referenced by Botan::PointGFp_Base_Point_Precompute::mul().

43  {
44  secure_vector<word> ws(m_curve.get_ws_size());
45  randomize_repr(rng, ws);
46  }
size_t get_ws_size() const
Definition: curve_gfp.h:137
void randomize_repr(RandomNumberGenerator &rng)
Definition: point_gfp.cpp:42

◆ randomize_repr() [2/2]

void Botan::PointGFp::randomize_repr ( RandomNumberGenerator rng,
secure_vector< word > &  ws 
)

Randomize the point representation The actual value (get_affine_x, get_affine_y) does not change

Definition at line 48 of file point_gfp.cpp.

References Botan::CurveGFp::get_p(), Botan::CurveGFp::mul_to_tmp(), Botan::BigInt::random_integer(), and Botan::CurveGFp::sqr_to_tmp().

49  {
50  const BigInt mask = BigInt::random_integer(rng, 2, m_curve.get_p());
51 
52  /*
53  * No reason to convert this to Montgomery representation first,
54  * just pretend the random mask was chosen as Redc(mask) and the
55  * random mask we generated above is in the Montgomery
56  * representation.
57  * //m_curve.to_rep(mask, ws);
58  */
59  const BigInt mask2 = m_curve.sqr_to_tmp(mask, ws);
60  const BigInt mask3 = m_curve.mul_to_tmp(mask2, mask, ws);
61 
62  m_coord_x = m_curve.mul_to_tmp(m_coord_x, mask2, ws);
63  m_coord_y = m_curve.mul_to_tmp(m_coord_y, mask3, ws);
64  m_coord_z = m_curve.mul_to_tmp(m_coord_z, mask, ws);
65  }
static BigInt random_integer(RandomNumberGenerator &rng, const BigInt &min, const BigInt &max)
Definition: big_rand.cpp:45
BigInt sqr_to_tmp(const BigInt &x, secure_vector< word > &ws) const
Definition: curve_gfp.h:217
BigInt mul_to_tmp(const BigInt &x, const BigInt &y, secure_vector< word > &ws) const
Definition: curve_gfp.h:210
const BigInt & get_p() const
Definition: curve_gfp.h:133

◆ swap()

void Botan::PointGFp::swap ( PointGFp other)

swaps the states of *this and other, does not throw!

Parameters
otherthe object to swap values with

Definition at line 575 of file point_gfp.cpp.

References Botan::BigInt::swap(), and Botan::CurveGFp::swap().

576  {
577  m_curve.swap(other.m_curve);
578  m_coord_x.swap(other.m_coord_x);
579  m_coord_y.swap(other.m_coord_y);
580  m_coord_z.swap(other.m_coord_z);
581  }
void swap(BigInt &other)
Definition: bigint.h:150
void swap(CurveGFp &other)
Definition: curve_gfp.h:224

◆ swap_coords()

void Botan::PointGFp::swap_coords ( BigInt new_x,
BigInt new_y,
BigInt new_z 
)
inline

Definition at line 156 of file point_gfp.h.

157  {
158  m_coord_x.swap(new_x);
159  m_coord_y.swap(new_y);
160  m_coord_z.swap(new_z);
161  }
void swap(BigInt &other)
Definition: bigint.h:150

◆ zero()

PointGFp Botan::PointGFp::zero ( ) const
inline

Return the zero (aka infinite) point associated with this curve

Definition at line 316 of file point_gfp.h.

Referenced by Botan::PointGFp_Base_Point_Precompute::mul(), and Botan::operator*().

316 { return PointGFp(m_curve); }
PointGFp()=default

The documentation for this class was generated from the following files: