diff options
author | lloyd <[email protected]> | 2010-02-25 18:41:19 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2010-02-25 18:41:19 +0000 |
commit | 6ef4dfa0e7ba4b6e618bdb42ed78bd17a206ba4c (patch) | |
tree | 64201815bd2b7fb64d82ca93e0448c67546e360b /src/math | |
parent | 687f24ead9508819659e485c33ac4a5c0ba5a585 (diff) |
Inline GFpElement
Diffstat (limited to 'src/math')
-rw-r--r-- | src/math/gfpmath/gfp_element.cpp | 144 | ||||
-rw-r--r-- | src/math/gfpmath/gfp_element.h | 148 | ||||
-rw-r--r-- | src/math/gfpmath/info.txt | 1 |
3 files changed, 121 insertions, 172 deletions
diff --git a/src/math/gfpmath/gfp_element.cpp b/src/math/gfpmath/gfp_element.cpp deleted file mode 100644 index 456b09478..000000000 --- a/src/math/gfpmath/gfp_element.cpp +++ /dev/null @@ -1,144 +0,0 @@ -/* -* Arithmetic for prime fields GF(p) -* -* (C) 2007 Martin Doering, Christoph Ludwig, Falko Strenzke -* 2008-2010 Jack Lloyd -* -* Distributed under the terms of the Botan license -*/ - -#include <botan/gfp_element.h> -#include <botan/numthry.h> - -namespace Botan { - -GFpElement& GFpElement::operator+=(const GFpElement& rhs) - { - m_value += rhs.m_value; - if(m_value >= mod_p) - m_value -= mod_p; - - return *this; - } - -GFpElement& GFpElement::operator-=(const GFpElement& rhs) - { - m_value -= rhs.m_value; - if(m_value.is_negative()) - m_value += mod_p; - - return *this; - } - -GFpElement& GFpElement::operator*=(u32bit rhs) - { - m_value *= rhs; - m_value %= mod_p; - return *this; - } - -GFpElement& GFpElement::operator*=(const GFpElement& rhs) - { - m_value *= rhs.m_value; - m_value %= mod_p; - - return *this; - } - -GFpElement& GFpElement::operator/=(const GFpElement& rhs) - { - GFpElement inv_rhs(rhs); - inv_rhs.inverse_in_place(); - *this *= inv_rhs; - return *this; - } - -GFpElement& GFpElement::inverse_in_place() - { - m_value = inverse_mod(m_value, mod_p); - return *this; - } - -GFpElement& GFpElement::negate() - { - m_value = mod_p - m_value; - return *this; - } - -void GFpElement::swap(GFpElement& other) - { - std::swap(m_value, other.m_value); - std::swap(mod_p, other.mod_p); - } - -bool operator==(const GFpElement& lhs, const GFpElement& rhs) - { - return (lhs.get_p() == rhs.get_p() && - lhs.get_value() == rhs.get_value()); - } - -GFpElement operator+(const GFpElement& lhs, const GFpElement& rhs) - { - // consider the case that lhs and rhs both use montgm: - // then += returns an element which uses montgm. - // thus the return value of op+ here will be an element - // using montgm in this case - GFpElement result(lhs); - result += rhs; - return result; - } - -GFpElement operator-(const GFpElement& lhs, const GFpElement& rhs) - { - GFpElement result(lhs); - result -= rhs; - return result; - } - -GFpElement operator-(const GFpElement& lhs) - { - return(GFpElement(lhs)).negate(); - } - -GFpElement operator*(const GFpElement& lhs, const GFpElement& rhs) - { - GFpElement result(lhs); - result *= rhs; - return result; - } - -GFpElement operator*(const GFpElement& lhs, u32bit rhs) - { - GFpElement result(lhs); - result *= rhs; - return result; - } - -GFpElement operator*(u32bit lhs, const GFpElement& rhs) - { - return rhs*lhs; - } - -GFpElement operator/(const GFpElement& lhs, const GFpElement& rhs) - { - GFpElement result (lhs); - result /= rhs; - return result; - } - -SecureVector<byte> FE2OSP(const GFpElement& elem) - { - return BigInt::encode_1363(elem.get_value(), elem.get_p().bytes()); - } - -GFpElement OS2FEP(MemoryRegion<byte> const& os, BigInt p) - { - return GFpElement(p, BigInt::decode(os.begin(), os.size())); - } - -GFpElement inverse(const GFpElement& elem) - { - return GFpElement(elem).inverse_in_place(); - } - -} diff --git a/src/math/gfpmath/gfp_element.h b/src/math/gfpmath/gfp_element.h index 58f3e79e2..17d4d9458 100644 --- a/src/math/gfpmath/gfp_element.h +++ b/src/math/gfpmath/gfp_element.h @@ -2,7 +2,7 @@ * Arithmetic for prime fields GF(p) * * (C) 2007 Martin Doering, Christoph Ludwig, Falko Strenzke -* 2009-2010 Jack Lloyd +* 2008-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ @@ -11,6 +11,7 @@ #define BOTAN_GFP_ELEMENT_H__ #include <botan/bigint.h> +#include <botan/numthry.h> namespace Botan { @@ -39,47 +40,86 @@ class BOTAN_DLL GFpElement * @param rhs the GFpElement to add to the local value * @result *this */ - GFpElement& operator+=(const GFpElement& rhs); + GFpElement& operator+=(const GFpElement& rhs) + { + m_value += rhs.m_value; + if(m_value >= mod_p) + m_value -= mod_p; + + return *this; + } /** * -= Operator * @param rhs the GFpElement to subtract from the local value * @result *this */ - GFpElement& operator-=(const GFpElement& rhs); + GFpElement& operator-=(const GFpElement& rhs) + { + m_value -= rhs.m_value; + if(m_value.is_negative()) + m_value += mod_p; + + return *this; + } /** * *= Operator - * @param rhs the GFpElement to multiply with the local value + * @param rhs the value to multiply with the local value * @result *this */ - GFpElement& operator*=(const GFpElement& rhs); + GFpElement& operator*=(u32bit rhs) + { + m_value *= rhs; + m_value %= mod_p; + return *this; + } + /** - * /= Operator - * @param rhs the GFpElement to divide the local value by + * *= Operator + * @param rhs the GFpElement to multiply with the local value * @result *this */ - GFpElement& operator/=(const GFpElement& rhs); + GFpElement& operator*=(const GFpElement& rhs) + { + m_value *= rhs.m_value; + m_value %= mod_p; + return *this; + } /** - * *= Operator - * @param rhs the value to multiply with the local value + * /= Operator + * @param rhs the GFpElement to divide the local value by * @result *this */ - GFpElement& operator*=(u32bit rhs); + GFpElement& operator/=(const GFpElement& rhs) + { + GFpElement inv_rhs(rhs); + inv_rhs.inverse_in_place(); + *this *= inv_rhs; + return *this; + } /** * Negate internal value(*this *= -1 ) * @return *this */ - GFpElement& negate(); + GFpElement& negate() + { + m_value = mod_p - m_value; + return *this; + } /** * Assigns the inverse of *this to *this, i.e. * *this = (*this)^(-1) * @result *this */ - GFpElement& inverse_in_place(); + GFpElement& inverse_in_place() + { + m_value = inverse_mod(m_value, mod_p); + return *this; + } /** * checks whether the value is zero (without provoking @@ -104,35 +144,89 @@ class BOTAN_DLL 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) + { + std::swap(m_value, other.m_value); + std::swap(mod_p, other.mod_p); + } + + bool operator==(const GFpElement& other) const + { + return (m_value == other.m_value && mod_p == other.mod_p); + } + private: BigInt mod_p; // modulus BigInt m_value; }; -// relational operators -bool BOTAN_DLL operator==(const GFpElement& lhs, const GFpElement& rhs); inline bool operator!=(const GFpElement& lhs, const GFpElement& rhs ) { - return !operator==(lhs, rhs); + return !(lhs == rhs); } // arithmetic operators -GFpElement BOTAN_DLL operator+(const GFpElement& lhs, const GFpElement& rhs); -GFpElement BOTAN_DLL operator-(const GFpElement& lhs, const GFpElement& rhs); -GFpElement BOTAN_DLL operator-(const GFpElement& lhs); +inline GFpElement operator+(const GFpElement& lhs, const GFpElement& rhs) + { + GFpElement result(lhs); + result += rhs; + return result; + } -GFpElement BOTAN_DLL operator*(const GFpElement& lhs, const GFpElement& rhs); -GFpElement BOTAN_DLL operator/(const GFpElement& lhs, const GFpElement& rhs); -GFpElement BOTAN_DLL operator*(const GFpElement& lhs, u32bit rhs); -GFpElement BOTAN_DLL operator*(u32bit rhs, const GFpElement& lhs); +inline GFpElement operator-(const GFpElement& lhs, const GFpElement& rhs) + { + GFpElement result(lhs); + result -= rhs; + return result; + } + +inline GFpElement operator-(const GFpElement& lhs) + { + return(GFpElement(lhs)).negate(); + } + +inline GFpElement operator*(const GFpElement& lhs, u32bit rhs) + { + GFpElement result(lhs); + result *= rhs; + return result; + } + +inline GFpElement operator*(const GFpElement& lhs, const GFpElement& rhs) + { + GFpElement result(lhs); + result *= rhs; + return result; + } + +inline GFpElement operator*(u32bit rhs, const GFpElement& lhs) + { + return rhs*lhs; + } + +inline GFpElement operator/(const GFpElement& lhs, const GFpElement& rhs) + { + GFpElement result (lhs); + result /= rhs; + return result; + } // return (*this)^(-1) -GFpElement BOTAN_DLL inverse(const GFpElement& elem); +inline GFpElement inverse(const GFpElement& elem) + { + return GFpElement(elem).inverse_in_place(); + } // encoding and decoding -SecureVector<byte> BOTAN_DLL FE2OSP(const GFpElement& elem); -GFpElement BOTAN_DLL OS2FEP(MemoryRegion<byte> const& os, BigInt p); +inline SecureVector<byte> FE2OSP(const GFpElement& elem) + { + return BigInt::encode_1363(elem.get_value(), elem.get_p().bytes()); + } + +inline GFpElement OS2FEP(const MemoryRegion<byte>& os, const BigInt& p) + { + return GFpElement(p, BigInt::decode(os.begin(), os.size())); + } inline void swap(GFpElement& x, GFpElement& y) { diff --git a/src/math/gfpmath/info.txt b/src/math/gfpmath/info.txt index 68cff026d..77d776a5e 100644 --- a/src/math/gfpmath/info.txt +++ b/src/math/gfpmath/info.txt @@ -7,7 +7,6 @@ point_gfp.h </header:public> <source> -gfp_element.cpp point_gfp.cpp </source> |