diff options
author | lloyd <[email protected]> | 2008-10-09 04:59:35 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2008-10-09 04:59:35 +0000 |
commit | dc615f5d1e4511926da5b04ee991960ffe48328a (patch) | |
tree | 4db5a571ab3ef9bc089229bc6f061936c183fa1f /src/math | |
parent | 1ee0834877e3ffa7ff1714ff54368d4794e38fac (diff) |
Clean up GF(p) implementation code
Diffstat (limited to 'src/math')
-rw-r--r-- | src/math/gfpmath/curve_gfp.cpp | 7 | ||||
-rw-r--r-- | src/math/gfpmath/gfp_element.cpp | 22 | ||||
-rw-r--r-- | src/math/gfpmath/gfp_element.h | 54 | ||||
-rw-r--r-- | src/math/gfpmath/point_gfp.cpp | 39 | ||||
-rw-r--r-- | src/math/gfpmath/point_gfp.h | 69 |
5 files changed, 99 insertions, 92 deletions
diff --git a/src/math/gfpmath/curve_gfp.cpp b/src/math/gfpmath/curve_gfp.cpp index c5abc2a5f..442fe070f 100644 --- a/src/math/gfpmath/curve_gfp.cpp +++ b/src/math/gfpmath/curve_gfp.cpp @@ -19,7 +19,7 @@ void CurveGFp::set_shrd_mod(SharedPtrConverter<GFpModulus> const mod) mA.turn_off_sp_red_mul();// m.m. is not needed, must be trf. back mB.turn_off_sp_red_mul();// m.m. is not needed, must be trf. back //ok, above we destroy any evantually computated montg. mult. values, - // but that won´t influence performance in usual applications + // but that won't influence performance in usual applications mA.set_shrd_mod(mod.get_shared()); mB.set_shrd_mod(mod.get_shared()); } @@ -95,6 +95,7 @@ GFpElement const CurveGFp::get_a() const { return mA; } + GFpElement const CurveGFp::get_b() const { return mB; @@ -116,6 +117,7 @@ void CurveGFp::swap(CurveGFp& other) std::swap(mp_mres_b, other.mp_mres_b); std::swap(mp_mres_one, other.mp_mres_one); } + GFpElement const CurveGFp::get_mres_a() const { if(mp_mres_a.get() == 0) @@ -126,6 +128,7 @@ GFpElement const CurveGFp::get_mres_a() const } return GFpElement(*mp_mres_a); } + GFpElement const CurveGFp::get_mres_b() const { if(mp_mres_b.get() == 0) @@ -136,6 +139,7 @@ GFpElement const CurveGFp::get_mres_b() const } return GFpElement(*mp_mres_b); } + std::tr1::shared_ptr<GFpElement const> const CurveGFp::get_mres_one() const { if(mp_mres_one.get() == 0) @@ -146,6 +150,7 @@ std::tr1::shared_ptr<GFpElement const> const CurveGFp::get_mres_one() const } return mp_mres_one; } + bool operator==(const CurveGFp& lhs, const CurveGFp& rhs) { return (lhs.get_p() == rhs.get_p() && lhs.get_a() == rhs.get_a() && lhs.get_b() == rhs.get_b()); diff --git a/src/math/gfpmath/gfp_element.cpp b/src/math/gfpmath/gfp_element.cpp index 939f03177..c5dd58c91 100644 --- a/src/math/gfpmath/gfp_element.cpp +++ b/src/math/gfpmath/gfp_element.cpp @@ -312,18 +312,19 @@ bool GFpElement::align_operands_res(const GFpElement& lhs, const GFpElement& rhs assert(false); } + bool GFpElement::is_trf_to_mres() const { return m_is_trf; - } -BigInt const GFpElement::get_p() const + +const BigInt& GFpElement::get_p() const { return (mp_mod->m_p); } -BigInt const GFpElement::get_value() const - { +const BigInt& GFpElement::get_value() const + { if(m_is_trf) { assert(m_use_montgm); @@ -331,7 +332,8 @@ BigInt const GFpElement::get_value() const } return m_value; } -BigInt const GFpElement::get_mres() const + +const BigInt& GFpElement::get_mres() const { if(!m_use_montgm) { @@ -342,12 +344,12 @@ BigInt const GFpElement::get_mres() const } if(!m_is_trf) { - trf_to_mres(); - } + return m_value; } + const GFpElement& GFpElement::operator=(const GFpElement& other) { m_value.grow_reg(other.m_value.size()); // grow first for exception safety @@ -423,9 +425,8 @@ const GFpElement& GFpElement::operator=(const GFpElement& other) // our precomputations aren´t set, the arguments neither, // so we let them alone return *this; - - } + void GFpElement::share_assign(const GFpElement& other) { assert((other.m_is_trf && other.m_use_montgm) || !other.m_is_trf); @@ -437,8 +438,8 @@ void GFpElement::share_assign(const GFpElement& other) m_use_montgm = other.m_use_montgm; m_is_trf = other.m_is_trf; mp_mod = other.mp_mod; // cannot throw - } + GFpElement& GFpElement::operator+=(const GFpElement& rhs) { GFpElement::align_operands_res(*this, rhs); @@ -456,6 +457,7 @@ GFpElement& GFpElement::operator+=(const GFpElement& rhs) return *this; } + GFpElement& GFpElement::operator-=(const GFpElement& rhs) { GFpElement::align_operands_res(*this, rhs); diff --git a/src/math/gfpmath/gfp_element.h b/src/math/gfpmath/gfp_element.h index d576363ac..3ee891231 100644 --- a/src/math/gfpmath/gfp_element.h +++ b/src/math/gfpmath/gfp_element.h @@ -27,8 +27,8 @@ struct Illegal_Transformation : public Exception }; /** - * This class represents one element in GF(p). Enables the convenient, transparent use - * of the montgomery multiplication. + * This class represents one element in GF(p). Enables the convenient, + * transparent use of the montgomery multiplication. */ class GFpElement { @@ -58,7 +58,7 @@ class GFpElement * @param value the element value * @param use_montgm whether this object will use Montgomery multiplication */ - explicit GFpElement (const BigInt& p, const BigInt& value, bool use_montgm = false ); + explicit GFpElement (const BigInt& p, const BigInt& value, bool use_montgm = false); /** construct an element of GF(p) with the given value (defaults to 0). @@ -79,7 +79,7 @@ class GFpElement * Copy constructor * @param other The element to clone */ - GFpElement ( const GFpElement& other ); + GFpElement(const GFpElement& other); /** * Assignment operator. @@ -88,7 +88,7 @@ class GFpElement * * @param other The element to assign to our object */ - const GFpElement& operator= ( const GFpElement& other ); + const GFpElement& operator=(const GFpElement& other); /** * Works like the assignment operator, but lets @@ -115,27 +115,27 @@ class GFpElement * @param rhs the GFpElement to add to the local value * @result *this */ - GFpElement& operator+= ( const GFpElement& rhs ); + GFpElement& operator+=(const GFpElement& rhs); /** * -= Operator * @param rhs the GFpElement to subtract from the local value * @result *this */ - GFpElement& operator-= ( const GFpElement& rhs ); + GFpElement& operator-=(const GFpElement& rhs); /** * *= Operator * @param rhs the GFpElement to multiply with the local value * @result *this */ - GFpElement& operator*= ( const GFpElement& rhs ); + GFpElement& operator*=(const GFpElement& rhs); /** * /= Operator * @param rhs the GFpElement to divide the local value by * @result *this */ - GFpElement& operator/= ( const GFpElement& rhs ); + GFpElement& operator/=(const GFpElement& rhs); /** * *= Operator @@ -145,7 +145,7 @@ class GFpElement GFpElement& operator*= (u32bit rhs); /** - * Negate internal value ( *this *= -1 ) + * Negate internal value(*this *= -1 ) * @return *this */ GFpElement& negate(); @@ -168,13 +168,13 @@ class GFpElement * return prime number of GF(p) * @result a prime number */ - BigInt const get_p() const; + const BigInt& get_p() const; /** * Return the represented value in GF(p) * @result The value in GF(p) */ - BigInt const get_value() const; + const BigInt& get_value() const; /** * Returns the shared pointer to the GFpModulus of *this. @@ -209,7 +209,7 @@ class GFpElement * Transforms this to x_bar = x * r mod m * @result return the value x_bar. */ - BigInt const get_mres() const; + const BigInt& get_mres() const; /** * Check, if montgomery multiplication is used. @@ -244,7 +244,7 @@ class GFpElement * @param elem the object to write * @result the output stream */ - friend std::ostream& operator<< ( std::ostream& output, const GFpElement& elem ); + friend std::ostream& operator<<(std::ostream& output, const GFpElement& elem); friend class Point_Coords_GFp; @@ -252,36 +252,36 @@ class GFpElement * swaps the states of *this and other, does not throw! * @param other The value to swap with */ - void swap ( GFpElement& other ); + void swap(GFpElement& other); }; // relational operators -bool operator== ( const GFpElement& lhs, const GFpElement& rhs ); -inline bool operator!= ( const GFpElement& lhs, const GFpElement& rhs ) +bool operator==(const GFpElement& lhs, const GFpElement& rhs); +inline bool operator!=(const GFpElement& lhs, const GFpElement& rhs ) { - return !operator== ( lhs, rhs ); + return !operator==(lhs, rhs); } // arithmetic operators -GFpElement operator+ ( const GFpElement& lhs, const GFpElement& rhs ); -GFpElement operator- ( const GFpElement& lhs, const GFpElement& rhs ); -GFpElement operator- ( const GFpElement& lhs ); +GFpElement operator+(const GFpElement& lhs, const GFpElement& rhs); +GFpElement operator-(const GFpElement& lhs, const GFpElement& rhs); +GFpElement operator-(const GFpElement& lhs); -GFpElement operator* ( const GFpElement& lhs, const GFpElement& rhs ); -GFpElement operator/ ( const GFpElement& lhs, const GFpElement& rhs ); +GFpElement operator*(const GFpElement& lhs, const GFpElement& rhs); +GFpElement operator/(const GFpElement& lhs, const GFpElement& rhs); GFpElement operator* (const GFpElement& lhs, u32bit rhs); GFpElement operator* (u32bit rhs, const GFpElement& lhs); // io operators -std::ostream& operator<< ( std::ostream& output, const GFpElement& elem ); +std::ostream& operator<<(std::ostream& output, const GFpElement& elem); // return (*this)^(-1) -GFpElement inverse ( const GFpElement& elem ); +GFpElement inverse(const GFpElement& elem); // encoding and decoding -SecureVector<byte> FE2OSP ( const GFpElement& elem ); -GFpElement OS2FEP ( MemoryRegion<byte> const& os, BigInt p); +SecureVector<byte> FE2OSP(const GFpElement& elem); +GFpElement OS2FEP(MemoryRegion<byte> const& os, BigInt p); inline void swap(GFpElement& x, GFpElement& y) { diff --git a/src/math/gfpmath/point_gfp.cpp b/src/math/gfpmath/point_gfp.cpp index 8743b01be..be4d56a18 100644 --- a/src/math/gfpmath/point_gfp.cpp +++ b/src/math/gfpmath/point_gfp.cpp @@ -11,8 +11,6 @@ #include <botan/point_gfp.h> #include <botan/numthry.h> -#include <iostream> - namespace Botan { // construct the point at infinity or a random point @@ -34,7 +32,6 @@ PointGFp::PointGFp(const CurveGFp& curve) } - // construct a point given its jacobian projective coordinates PointGFp::PointGFp(const CurveGFp& curve, const GFpElement& x, const GFpElement& y, const GFpElement& z) @@ -68,7 +65,7 @@ PointGFp::PointGFp ( const CurveGFp& curve, const GFpElement& x, } // copy constructor -PointGFp::PointGFp(PointGFp const& other) +PointGFp::PointGFp(const PointGFp& other) : mC(other.mC), mX(other.mX), mY(other.mY), @@ -84,7 +81,7 @@ PointGFp::PointGFp(PointGFp const& other) } // assignment operator -PointGFp const& PointGFp::operator=(PointGFp const& other) +const PointGFp& PointGFp::operator=(PointGFp const& other) { mC = other.get_curve(); mX = other.get_jac_proj_x(); @@ -100,7 +97,7 @@ PointGFp const& PointGFp::operator=(PointGFp const& other) return *this; } -PointGFp const& PointGFp::assign_within_same_curve(PointGFp const& other) +const PointGFp& PointGFp::assign_within_same_curve(PointGFp const& other) { mX = other.get_jac_proj_x(); mY = other.get_jac_proj_y(); @@ -146,7 +143,7 @@ void PointGFp::ensure_worksp() const } // arithmetic operators -PointGFp& PointGFp::operator+=(PointGFp const& rhs) +PointGFp& PointGFp::operator+=(const PointGFp& rhs) { if (is_zero()) { @@ -310,7 +307,7 @@ PointGFp& PointGFp::operator+=(PointGFp const& rhs) return *this; } -PointGFp& PointGFp::operator-=(PointGFp const& rhs) +PointGFp& PointGFp::operator-=(const PointGFp& rhs) { PointGFp minus_rhs = PointGFp(rhs).negate(); @@ -484,7 +481,7 @@ inline std::tr1::shared_ptr<PointGFp> PointGFp::mult_loop(int l, const BigInt& m, std::tr1::shared_ptr<PointGFp> H, std::tr1::shared_ptr<PointGFp> tmp, - PointGFp const& P) + const PointGFp& P) { //assert(l >= (int)m.bits()- 1); tmp = H; @@ -703,7 +700,7 @@ PointGFp const PointGFp::get_z_to_one() const * their values in affine coordinates. * returns *this. */ -PointGFp const& PointGFp::set_z_to_one() const +const PointGFp& PointGFp::set_z_to_one() const { if (!(mZ.get_value() == BigInt(1)) && !(mZ.get_value() == BigInt(0))) { @@ -862,12 +859,12 @@ void PointGFp::swap(PointGFp& other) std::swap<bool>(mAZpow4_set, other.mAZpow4_set); } -PointGFp const mult2(PointGFp const& point) +PointGFp const mult2(const PointGFp& point) { return (PointGFp(point)).mult2_in_place(); } -bool operator==(PointGFp const& lhs, PointGFp const& rhs) +bool operator==(const PointGFp& lhs, PointGFp const& rhs) { if (lhs.is_zero() && rhs.is_zero()) { @@ -888,30 +885,30 @@ bool operator==(PointGFp const& lhs, PointGFp const& rhs) } // arithmetic operators -PointGFp operator+(PointGFp const& lhs, PointGFp const& rhs) +PointGFp operator+(const PointGFp& lhs, PointGFp const& rhs) { PointGFp tmp(lhs); return tmp += rhs; } -PointGFp operator-(PointGFp const& lhs, PointGFp const& rhs) +PointGFp operator-(const PointGFp& lhs, PointGFp const& rhs) { PointGFp tmp(lhs); return tmp -= rhs; } -PointGFp operator-(PointGFp const& lhs) +PointGFp operator-(const PointGFp& lhs) { return PointGFp(lhs).negate(); } -PointGFp operator*(const BigInt& scalar, PointGFp const& point) +PointGFp operator*(const BigInt& scalar, const PointGFp& point) { PointGFp result(point); return result *= scalar; } -PointGFp operator*(PointGFp const& point, const BigInt& scalar) +PointGFp operator*(const PointGFp& point, const BigInt& scalar) { PointGFp result(point); return result *= scalar; @@ -926,7 +923,7 @@ PointGFp mult_point_secure(const PointGFp& point, const BigInt& scalar, } // encoding and decoding -SecureVector<byte> EC2OSP(PointGFp const& point, byte format) +SecureVector<byte> EC2OSP(const PointGFp& point, byte format) { SecureVector<byte> result; if (format == PointGFp::UNCOMPRESSED) @@ -948,7 +945,7 @@ SecureVector<byte> EC2OSP(PointGFp const& point, byte format) } return result; } -SecureVector<byte> encode_compressed(PointGFp const& point) +SecureVector<byte> encode_compressed(const PointGFp& point) { @@ -980,7 +977,7 @@ SecureVector<byte> encode_compressed(PointGFp const& point) } -SecureVector<byte> encode_uncompressed(PointGFp const& point) +SecureVector<byte> encode_uncompressed(const PointGFp& point) { if (point.is_zero()) { @@ -1007,7 +1004,7 @@ SecureVector<byte> encode_uncompressed(PointGFp const& point) } -SecureVector<byte> encode_hybrid(PointGFp const& point) +SecureVector<byte> encode_hybrid(const PointGFp& point) { if (point.is_zero()) { diff --git a/src/math/gfpmath/point_gfp.h b/src/math/gfpmath/point_gfp.h index b91e08a39..6118f00d5 100644 --- a/src/math/gfpmath/point_gfp.h +++ b/src/math/gfpmath/point_gfp.h @@ -48,7 +48,7 @@ class PointGFp * Construct the point O * @param curve The base curve */ - explicit PointGFp(CurveGFp const& curve); + explicit PointGFp(const CurveGFp& curve); /** * Construct a point given its affine coordinates @@ -56,8 +56,8 @@ class PointGFp * @param x affine x coordinate * @param y affine y coordinate */ - explicit PointGFp(CurveGFp const& curve, GFpElement const& x, - GFpElement const& y ); + explicit PointGFp(const CurveGFp& curve, GFpElement const& x, + GFpElement const& y); /** * Construct a point given its jacobian projective coordinates @@ -66,26 +66,26 @@ class PointGFp * @param y jacobian projective y coordinate * @param z jacobian projective y coordinate */ - explicit PointGFp(CurveGFp const& curve, GFpElement const& x, - GFpElement const& y, GFpElement const& z ); + explicit PointGFp(const CurveGFp& curve, GFpElement const& x, + GFpElement const& y, GFpElement const& z); /** * copy constructor * @param other the value to clone */ - PointGFp(PointGFp const& other ); + PointGFp(const PointGFp& other); /** * assignment operator * @param other The point to use as source for the assignment */ - PointGFp const& operator=(PointGFp const& other ); + const PointGFp& operator=(const PointGFp& other); /** * assign another point which is on the same curve as *this * @param other The point to use as source for the assignment */ - PointGFp const& assign_within_same_curve(PointGFp const& other); + const PointGFp& assign_within_same_curve(const PointGFp& other); @@ -94,14 +94,14 @@ class PointGFp * @param rhs the PointGFp to add to the local value * @result resulting PointGFp */ - PointGFp& operator+=(PointGFp const& rhs ); + PointGFp& operator+=(const PointGFp& rhs); /** * -= Operator * @param rhs the PointGFp to subtract from the local value * @result resulting PointGFp */ - PointGFp& operator-=(PointGFp const& rhs ); + PointGFp& operator-=(const PointGFp& rhs); /** * *= Operator @@ -110,7 +110,7 @@ class PointGFp * @param scalar the PointGFp to multiply with *this * @result resulting PointGFp */ - PointGFp& operator*=(const BigInt& scalar ); + PointGFp& operator*=(const BigInt& scalar); /** * the equivalent to operator*= with countermeasures against @@ -127,7 +127,7 @@ class PointGFp PointGFp& mult_this_secure(const BigInt& scalar, const BigInt& point_order, const BigInt& max_secr - ); + ); /** * Negate internal value(*this *= -1 ) @@ -145,7 +145,7 @@ class PointGFp * Set z coordinate to one. * @return *this */ - PointGFp const& set_z_to_one() const; + const PointGFp& set_z_to_one() const; /** * Turn on the special reduction multiplication (i.e. the @@ -219,7 +219,7 @@ class PointGFp * swaps the states of *this and other, does not throw! * @param other the object to swap values with */ - void swap(PointGFp& other ); + void swap(PointGFp& other); /** * Sets the shared pointer to the GFpModulus that will be @@ -234,13 +234,16 @@ class PointGFp */ void set_shrd_mod(std::tr1::shared_ptr<GFpModulus> p_mod); - static GFpElement decompress(bool yMod2, GFpElement const& x, CurveGFp const& curve ); + static GFpElement decompress(bool yMod2, GFpElement const& x, const CurveGFp& curve); private: static const u32bit GFPEL_WKSP_SIZE = 9; void ensure_worksp() const; - inline std::tr1::shared_ptr<PointGFp> mult_loop(int l, const BigInt& m, std::tr1::shared_ptr<PointGFp> H, std::tr1::shared_ptr<PointGFp> tmp, PointGFp const& P); + inline std::tr1::shared_ptr<PointGFp> mult_loop(int l, const BigInt& m, + std::tr1::shared_ptr<PointGFp> H, + std::tr1::shared_ptr<PointGFp> tmp, + const PointGFp& P); CurveGFp mC; mutable GFpElement mX; // NOTE: these values must be mutable (affine<->proj) @@ -257,40 +260,40 @@ class PointGFp }; // relational operators -bool operator==(PointGFp const& lhs, PointGFp const& rhs ); -inline bool operator!=(PointGFp const& lhs, PointGFp const& rhs ) +bool operator==(const PointGFp& lhs, const PointGFp& rhs); +inline bool operator!=(const PointGFp& lhs, const PointGFp& rhs ) { - return !operator==(lhs, rhs ); + return !operator==(lhs, rhs); } // arithmetic operators -PointGFp operator+(PointGFp const& lhs, PointGFp const& rhs ); -PointGFp operator-(PointGFp const& lhs, PointGFp const& rhs ); -PointGFp operator-(PointGFp const& lhs ); +PointGFp operator+(const PointGFp& lhs, const PointGFp& rhs); +PointGFp operator-(const PointGFp& lhs, const PointGFp& rhs); +PointGFp operator-(const PointGFp& lhs); -PointGFp operator*(const BigInt& scalar, PointGFp const& point ); -PointGFp operator*(PointGFp const& point, const BigInt& scalar ); -PointGFp mult_point_secure(PointGFp const& point, const BigInt& scalar, const BigInt& point_order, const BigInt& max_secret); +PointGFp operator*(const BigInt& scalar, const PointGFp& point); +PointGFp operator*(const PointGFp& point, const BigInt& scalar); +PointGFp mult_point_secure(const PointGFp& point, const BigInt& scalar, const BigInt& point_order, const BigInt& max_secret); -PointGFp const mult2 (PointGFp const& point); +PointGFp const mult2 (const PointGFp& point); PointGFp const create_random_point(RandomNumberGenerator& rng, - CurveGFp const& curve); + const CurveGFp& curve); // encoding and decoding -SecureVector<byte> EC2OSP(PointGFp const& point, byte format ); -PointGFp OS2ECP(MemoryRegion<byte> const& os, CurveGFp const& curve ); +SecureVector<byte> EC2OSP(const PointGFp& point, byte format); +PointGFp OS2ECP(MemoryRegion<byte> const& os, const CurveGFp& curve); -SecureVector<byte> encode_uncompressed(PointGFp const& point ); // maybe make private -SecureVector<byte> encode_hybrid(PointGFp const& point ); // maybe make private -SecureVector<byte> encode_compressed(PointGFp const& point ); // maybe make private +SecureVector<byte> encode_uncompressed(const PointGFp& point); // maybe make private +SecureVector<byte> encode_hybrid(const PointGFp& point); // maybe make private +SecureVector<byte> encode_compressed(const PointGFp& point); // maybe make private // swaps the states of point1 and point2, does not throw! // cf. Meyers, Item 25 inline void swap(PointGFp& point1, PointGFp& point2 ) { - point1.swap(point2 ); + point1.swap(point2); } } // namespace Botan |