diff options
Diffstat (limited to 'src/math')
-rw-r--r-- | src/math/numbertheory/curve_gfp.h | 5 | ||||
-rw-r--r-- | src/math/numbertheory/point_gfp.cpp | 62 | ||||
-rw-r--r-- | src/math/numbertheory/point_gfp.h | 57 |
3 files changed, 57 insertions, 67 deletions
diff --git a/src/math/numbertheory/curve_gfp.h b/src/math/numbertheory/curve_gfp.h index 697065dfe..de6bed099 100644 --- a/src/math/numbertheory/curve_gfp.h +++ b/src/math/numbertheory/curve_gfp.h @@ -23,6 +23,11 @@ class BOTAN_DLL CurveGFp public: /** + * Create an uninitialized CurveGFp + */ + CurveGFp() {} + + /** * Construct the elliptic curve E: y^2 = x^3 + ax + b over GF(p) * @param p prime number of the field * @param a first coefficient diff --git a/src/math/numbertheory/point_gfp.cpp b/src/math/numbertheory/point_gfp.cpp index 06c42d18c..bed08eb39 100644 --- a/src/math/numbertheory/point_gfp.cpp +++ b/src/math/numbertheory/point_gfp.cpp @@ -67,7 +67,7 @@ PointGFp& PointGFp::operator+=(const PointGFp& rhs) { if(r.is_zero()) { - mult2_in_place(); + mult2(); return *this; } @@ -119,7 +119,7 @@ PointGFp& PointGFp::operator*=(const BigInt& scalar) } else if(value == 2) { - this->mult2_in_place(); + this->mult2(); if(scalar.is_negative()) this->negate(); } @@ -135,7 +135,7 @@ PointGFp& PointGFp::operator*=(const BigInt& scalar) for(int i = scalar.bits() - 1; i >= 0; --i) { - H.mult2_in_place(); + H.mult2(); if(scalar.get_bit(i)) H += P; } @@ -164,16 +164,8 @@ PointGFp& PointGFp::operator*=(const BigInt& scalar) return *this; } -PointGFp& PointGFp::negate() - { - if(!is_zero()) - coord_y = curve.get_p() - coord_y; - - return *this; - } - // *this *= 2 -void PointGFp::mult2_in_place() +void PointGFp::mult2() { if(is_zero()) return; @@ -196,15 +188,11 @@ void PointGFp::mult2_in_place() BigInt x = mod_p.reduce(mod_p.square(M) - mod_p.multiply(2, S)); - BigInt y = mod_p.square(y_2); - - BigInt z = mod_p.multiply(2, mod_p.reduce(y + y)); - - BigInt U = mod_p.reduce(z + z); + BigInt U = mod_p.multiply(8, mod_p.square(y_2)); - y = mod_p.reduce(mod_p.multiply(M, S - x) - U); + BigInt y = mod_p.reduce(mod_p.multiply(M, S - x) - U); - z = mod_p.multiply(2, mod_p.multiply(coord_y, coord_z)); + BigInt z = mod_p.multiply(2, mod_p.multiply(coord_y, coord_z)); coord_x = x; coord_y = y; @@ -233,12 +221,6 @@ BigInt PointGFp::get_affine_y() const return mod_p.multiply(coord_y, inverse_mod(z3, curve.get_p())); } -// Is this the point at infinity? -bool PointGFp::is_zero() const - { - return(coord_x.is_zero() && coord_z.is_zero()); - } - void PointGFp::check_invariants() const { /* @@ -292,36 +274,6 @@ bool PointGFp::operator==(const PointGFp& other) const get_curve() == other.get_curve()); } -// arithmetic operators -PointGFp operator+(const PointGFp& lhs, PointGFp const& rhs) - { - PointGFp tmp(lhs); - return tmp += rhs; - } - -PointGFp operator-(const PointGFp& lhs, PointGFp const& rhs) - { - PointGFp tmp(lhs); - return tmp -= rhs; - } - -PointGFp operator-(const PointGFp& lhs) - { - return PointGFp(lhs).negate(); - } - -PointGFp operator*(const BigInt& scalar, const PointGFp& point) - { - PointGFp result(point); - return result *= scalar; - } - -PointGFp operator*(const PointGFp& point, const BigInt& scalar) - { - PointGFp result(point); - return result *= scalar; - } - // encoding and decoding SecureVector<byte> EC2OSP(const PointGFp& point, byte format) { diff --git a/src/math/numbertheory/point_gfp.h b/src/math/numbertheory/point_gfp.h index 0741b5e56..3bb763d44 100644 --- a/src/math/numbertheory/point_gfp.h +++ b/src/math/numbertheory/point_gfp.h @@ -41,6 +41,11 @@ class BOTAN_DLL PointGFp }; /** + * Construct an uninitialized PointGFp + */ + PointGFp() {} + + /** * Construct the point O * @param curve The base curve */ @@ -98,7 +103,12 @@ class BOTAN_DLL PointGFp * Negate this point * @return *this */ - PointGFp& negate(); + PointGFp& negate() + { + if(!is_zero()) + coord_y = curve.get_p() - coord_y; + return *this; + } /** * Return base curve of this point @@ -122,25 +132,26 @@ class BOTAN_DLL PointGFp * get the jacobian projective x coordinate * @result jacobian projective x coordinate */ - const BigInt& get_jac_proj_x() const { return coord_x; } + const BigInt& get_x() const { return coord_x; } /** * get the jacobian projective y coordinate * @result jacobian projective y coordinate */ - const BigInt& get_jac_proj_y() const { return coord_y; } + const BigInt& get_y() const { return coord_y; } /** * get the jacobian projective z coordinate * @result jacobian projective z coordinate */ - const BigInt& get_jac_proj_z() const { return coord_z; } + const BigInt& get_z() const { return coord_z; } /** * Is this the point at infinity? * @result true, if this point is at infinity, false otherwise. */ - bool is_zero() const; + bool is_zero() const + { return (coord_x.is_zero() && coord_z.is_zero()); } /** * Checks whether the point is to be found on the underlying curve. @@ -162,9 +173,9 @@ class BOTAN_DLL PointGFp bool operator==(const PointGFp& other) const; private: /** - * Multiply the point by two + * Point doubling */ - void mult2_in_place(); + void mult2(); CurveGFp curve; BigInt coord_x, coord_y, coord_z; @@ -177,12 +188,34 @@ inline bool operator!=(const PointGFp& lhs, const PointGFp& rhs) } // arithmetic operators -PointGFp BOTAN_DLL operator+(const PointGFp& lhs, const PointGFp& rhs); -PointGFp BOTAN_DLL operator-(const PointGFp& lhs, const PointGFp& rhs); -PointGFp BOTAN_DLL operator-(const PointGFp& lhs); +inline PointGFp operator-(const PointGFp& lhs) + { + return PointGFp(lhs).negate(); + } + +inline PointGFp operator+(const PointGFp& lhs, const PointGFp& rhs) + { + PointGFp tmp(lhs); + return tmp += rhs; + } -PointGFp BOTAN_DLL operator*(const BigInt& scalar, const PointGFp& point); -PointGFp BOTAN_DLL operator*(const PointGFp& point, const BigInt& scalar); +inline PointGFp operator-(const PointGFp& lhs, const PointGFp& rhs) + { + PointGFp tmp(lhs); + return tmp -= rhs; + } + +inline PointGFp operator*(const BigInt& scalar, const PointGFp& point) + { + PointGFp result(point); + return result *= scalar; + } + +inline PointGFp operator*(const PointGFp& point, const BigInt& scalar) + { + PointGFp result(point); + return result *= scalar; + } // encoding and decoding SecureVector<byte> BOTAN_DLL EC2OSP(const PointGFp& point, byte format); |