aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorlloyd <[email protected]>2008-10-09 20:08:42 +0000
committerlloyd <[email protected]>2008-10-09 20:08:42 +0000
commitea0ab5f4d4dab9952565efd7d42d8fe9091f6f13 (patch)
tree16edd58cf155c4875749b0a103a42a4a12d5201e /src
parent30466ac31e9d83370e76e871e8480210b9876251 (diff)
Some small changes to GFpElement, including accepting u64bit instead
of u32bit to the operator* and operator*= Instead of passing the n_dash array to inner_montg_mult_sos, which only uses the first word, just pass the word directly.
Diffstat (limited to 'src')
-rw-r--r--src/math/gfpmath/gfp_element.cpp52
-rw-r--r--src/math/gfpmath/gfp_element.h6
2 files changed, 28 insertions, 30 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 *
- * Christoph Ludwig *
- * Falko Strenzke *
- ******************************************************/
+* Arithmetic for prime fields GF(p) *
+* *
+* (C) 2007 Martin Doering *
+* Christoph Ludwig *
+* Falko Strenzke *
+* 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;
}
diff --git a/src/math/gfpmath/gfp_element.h b/src/math/gfpmath/gfp_element.h
index 39e728cfa..067825bc3 100644
--- a/src/math/gfpmath/gfp_element.h
+++ b/src/math/gfpmath/gfp_element.h
@@ -142,7 +142,7 @@ class BOTAN_DLL GFpElement
* @param rhs the value to multiply with the local value
* @result *this
*/
- GFpElement& operator*= (u32bit rhs);
+ GFpElement& operator*= (u64bit rhs);
/**
* Negate internal value(*this *= -1 )
@@ -270,8 +270,8 @@ 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, u32bit rhs);
-GFpElement operator* (u32bit rhs, const GFpElement& lhs);
+GFpElement operator* (const GFpElement& lhs, u64bit rhs);
+GFpElement operator* (u64bit rhs, const GFpElement& lhs);
// io operators
std::ostream& operator<<(std::ostream& output, const GFpElement& elem);