diff options
Diffstat (limited to 'src/math/gfpmath/gfp_element.cpp')
-rw-r--r-- | src/math/gfpmath/gfp_element.cpp | 52 |
1 files changed, 25 insertions, 27 deletions
diff --git a/src/math/gfpmath/gfp_element.cpp b/src/math/gfpmath/gfp_element.cpp index d03439f0d..47b5429b3 100644 --- a/src/math/gfpmath/gfp_element.cpp +++ b/src/math/gfpmath/gfp_element.cpp @@ -1,13 +1,14 @@ /****************************************************** - * Arithmetic for prime fields GF(p) (source file) * - * * - * (C) 2007 Martin Doering * - * [email protected] * - * Christoph Ludwig * - * [email protected] * - * Falko Strenzke * - * [email protected] * - ******************************************************/ +* Arithmetic for prime fields GF(p) * +* * +* (C) 2007 Martin Doering * +* [email protected] * +* Christoph Ludwig * +* [email protected] * +* Falko Strenzke * +* [email protected] * +* 2008 Jack Lloyd * +******************************************************/ #include <botan/gfp_element.h> #include <botan/numthry.h> @@ -21,7 +22,7 @@ namespace Botan { namespace { -void inner_montg_mult_sos(word result[], const word* a_bar, const word* b_bar, const word* n, const word* n_dash, u32bit s) +void inner_montg_mult_sos(word result[], const word* a_bar, const word* b_bar, const word* n, const word n_dash, u32bit s) { SecureVector<word> t; t.grow_to(2*s+1); @@ -33,10 +34,6 @@ void inner_montg_mult_sos(word result[], const word* a_bar, const word* b_bar, c word S = 0; for (u32bit j=0; j<s; j++) { - // we use: - // word word_madd3(word a, word b, word c, word d, word* carry) - // returns a * b + c + d and resets the carry (not using it as input) - S = word_madd3(a_bar[j], b_bar[i], t[i+j], &C); t[i+j] = S; } @@ -51,7 +48,7 @@ void inner_montg_mult_sos(word result[], const word* a_bar, const word* b_bar, c word C = 0; word zero = 0; - word m = word_madd2(t[i], n_dash[0], &zero); + word m = word_madd2(t[i], n_dash, &zero); for (u32bit j=0; j<s; j++) { @@ -115,18 +112,19 @@ void montg_mult(BigInt& result, BigInt& a_bar, BigInt& b_bar, const BigInt& m, c if(a_bar.is_zero() || b_bar.is_zero()) result = 0; -#if 0 u32bit s = m.sig_words(); a_bar.grow_to(s); b_bar.grow_to(s); result.grow_to(s); - inner_montg_mult_sos(result.get_reg(), a_bar.data(), b_bar.data(), m.data(), m_dash.data(), s); -#else - result = a_bar * b_bar * m_dash; - if(result >= m) - result -= m; -#endif + inner_montg_mult_sos(result.get_reg(), a_bar.data(), b_bar.data(), m.data(), m_dash.data()[0], s); + /* + std::cout << "result = " << result << "\n" + << "a_bar = " << a_bar << "\n" + << "b_bar = " << b_bar << "\n" + << "m = " << m << "\n" + << "m_dash = " << m_dash.data()[0] << "\n"; + */ } /** @@ -135,6 +133,8 @@ void montg_mult(BigInt& result, BigInt& a_bar, BigInt& b_bar, const BigInt& m, c */ BigInt montgm_calc_r_oddmod(const BigInt& prime) { + assert(prime.is_odd()); + u32bit n = prime.sig_words(); BigInt result(1); result <<= n*BOTAN_MP_WORD_BITS; @@ -196,7 +196,6 @@ GFpElement::GFpElement(const GFpElement& other) : m_value(other.m_value), m_use_montgm(other.m_use_montgm), m_is_trf(other.m_is_trf) - { //creates an independent copy assert((other.m_is_trf && other.m_use_montgm) || !other.m_is_trf); @@ -246,7 +245,6 @@ void GFpElement::ensure_montgm_precomp() const assert(!mp_mod->m_r_inv.is_zero()); assert(!mp_mod->m_p_dash.is_zero()); } - } void GFpElement::set_shrd_mod(std::tr1::shared_ptr<GFpModulus> const p_mod) @@ -484,7 +482,7 @@ GFpElement& GFpElement::operator-=(const GFpElement& rhs) return *this; } -GFpElement& GFpElement::operator*= (u32bit rhs) +GFpElement& GFpElement::operator*= (u64bit rhs) { workspace = m_value; workspace *= rhs; @@ -663,14 +661,14 @@ GFpElement operator*(const GFpElement& lhs, const GFpElement& rhs) return result; } -GFpElement operator*(const GFpElement& lhs, u32bit rhs) +GFpElement operator*(const GFpElement& lhs, u64bit rhs) { GFpElement result(lhs); result *= rhs; return result; } -GFpElement operator*(u32bit lhs, const GFpElement& rhs) +GFpElement operator*(u64bit lhs, const GFpElement& rhs) { return rhs*lhs; } |