diff options
author | lloyd <[email protected]> | 2010-02-25 05:11:53 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2010-02-25 05:11:53 +0000 |
commit | 42d9abd0775e67c58e94f0651f19f67bc6bf3e8e (patch) | |
tree | 5e4b562e9ad63c631e1c5e844e6bf0d95c522b8a /src | |
parent | 890c95ae45b60f58690fd0b18386cc29549f145f (diff) |
Inline some simple GFpElement functions. Remove ostream << operator
Diffstat (limited to 'src')
-rw-r--r-- | src/math/gfpmath/gfp_element.cpp | 65 | ||||
-rw-r--r-- | src/math/gfpmath/gfp_element.h | 15 |
2 files changed, 15 insertions, 65 deletions
diff --git a/src/math/gfpmath/gfp_element.cpp b/src/math/gfpmath/gfp_element.cpp index bd86c6f3e..a5074c476 100644 --- a/src/math/gfpmath/gfp_element.cpp +++ b/src/math/gfpmath/gfp_element.cpp @@ -12,61 +12,39 @@ #include <botan/internal/def_powm.h> #include <botan/internal/mp_asm.h> #include <botan/internal/mp_asmi.h> -#include <ostream> -#include <assert.h> namespace Botan { -GFpElement::GFpElement(const BigInt& p, const BigInt& value) : - mod_p(p), - m_value(value %p) - { - } - GFpElement& GFpElement::operator+=(const GFpElement& rhs) { - BigInt workspace = m_value; - workspace += rhs.m_value; - if(workspace >= mod_p) - workspace -= mod_p; - - m_value = workspace; - assert(m_value < mod_p); - assert(m_value >= 0); + m_value += rhs.m_value; + if(m_value >= mod_p) + m_value -= mod_p; return *this; } GFpElement& GFpElement::operator-=(const GFpElement& rhs) { - BigInt workspace = m_value; + m_value -= rhs.m_value; + if(m_value.is_negative()) + m_value += mod_p; - workspace -= rhs.m_value; - - if(workspace.is_negative()) - workspace += mod_p; - - m_value = workspace; - assert(m_value < mod_p); - assert(m_value >= 0); return *this; } -GFpElement& GFpElement::operator*= (u32bit rhs) +GFpElement& GFpElement::operator*=(u32bit rhs) { - BigInt workspace = m_value; - workspace *= rhs; - workspace %= mod_p; - m_value = workspace; + m_value *= rhs; + m_value %= mod_p; return *this; } GFpElement& GFpElement::operator*=(const GFpElement& rhs) { - BigInt workspace = m_value; - workspace *= rhs.m_value; - workspace %= mod_p; - m_value = workspace; + m_value *= rhs.m_value; + m_value %= mod_p; + return *this; } @@ -78,12 +56,6 @@ GFpElement& GFpElement::operator/=(const GFpElement& rhs) return *this; } -bool GFpElement::is_zero() const - { - return (m_value.is_zero()); - // this is correct because x_bar = x * r = x = 0 for x = 0 - } - GFpElement& GFpElement::inverse_in_place() { m_value = inverse_mod(m_value, mod_p); @@ -93,7 +65,6 @@ GFpElement& GFpElement::inverse_in_place() GFpElement& GFpElement::negate() { m_value = mod_p - m_value; - assert(m_value <= mod_p); return *this; } @@ -103,11 +74,6 @@ void GFpElement::swap(GFpElement& other) std::swap(mod_p, other.mod_p); } -std::ostream& operator<<(std::ostream& output, const GFpElement& elem) - { - return output << '(' << elem.get_value() << "," << elem.get_p() << ')'; - } - bool operator==(const GFpElement& lhs, const GFpElement& rhs) { return (lhs.get_p() == rhs.get_p() && @@ -120,7 +86,6 @@ GFpElement operator+(const GFpElement& lhs, const GFpElement& rhs) // then += returns an element which uses montgm. // thus the return value of op+ here will be an element // using montgm in this case - // NOTE: the rhs might be transformed when using op+, the lhs never GFpElement result(lhs); result += rhs; return result; @@ -131,7 +96,6 @@ GFpElement operator-(const GFpElement& lhs, const GFpElement& rhs) GFpElement result(lhs); result -= rhs; return result; - // NOTE: the rhs might be transformed when using op-, the lhs never } GFpElement operator-(const GFpElement& lhs) @@ -141,10 +105,6 @@ GFpElement operator-(const GFpElement& lhs) 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; @@ -185,4 +145,3 @@ GFpElement inverse(const GFpElement& elem) } } - diff --git a/src/math/gfpmath/gfp_element.h b/src/math/gfpmath/gfp_element.h index 5d27a6442..58f3e79e2 100644 --- a/src/math/gfpmath/gfp_element.h +++ b/src/math/gfpmath/gfp_element.h @@ -11,7 +11,6 @@ #define BOTAN_GFP_ELEMENT_H__ #include <botan/bigint.h> -#include <iosfwd> namespace Botan { @@ -29,7 +28,8 @@ class BOTAN_DLL GFpElement * @param p the prime number of the field * @param value the element value */ - GFpElement(const BigInt& p, const BigInt& value); + GFpElement(const BigInt& p, const BigInt& value) : + mod_p(p), m_value(value % p) {} // GFpElement(const GFpElement& other) = default; // const GFpElement& operator=(const GFpElement& other) = default; @@ -86,7 +86,7 @@ class BOTAN_DLL GFpElement * a backtransformation to the ordinary-residue) * @result true, if the value is zero, false otherwise. */ - bool is_zero() const; + bool is_zero() const { return m_value.is_zero(); } /** * return prime number of GF(p) @@ -127,15 +127,6 @@ 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); - -/** -* write a GFpElement to an output stream. -* @param output the output stream to write to -* @param elem the object to write -* @result the output stream -*/ -BOTAN_DLL std::ostream& operator<<(std::ostream& output, const GFpElement& elem); - // return (*this)^(-1) GFpElement BOTAN_DLL inverse(const GFpElement& elem); |