aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorlloyd <[email protected]>2012-08-01 19:32:10 +0000
committerlloyd <[email protected]>2012-08-01 19:32:10 +0000
commit7dbcedf896b78db3920368d7dabf2dbc2fa50e09 (patch)
treec6d0228cea1f0a7e7c3cf1e461a5d4ca8dde1040
parent3df2a2980adaa8ba598698dc988cbde0433b32f5 (diff)
Remove z_size parameter to bigint_monty_redc because it should always
be 2*(p_size+1). Document that it clears the high part of z. Don't clear the workspace before calling Karatsuba in bigint_mul or bigint_sqr - they clear it every time anyway. Don't bother masking words in the Montgomery_Exponentiator as redc zeros those words. Also don't bother calling z.clear() as the multiply operation does that already.
-rw-r--r--src/math/mp/mp_core.h10
-rw-r--r--src/math/mp/mp_karat.cpp6
-rw-r--r--src/math/mp/mp_monty.cpp8
-rw-r--r--src/math/numbertheory/powm_mnt.cpp14
4 files changed, 12 insertions, 26 deletions
diff --git a/src/math/mp/mp_core.h b/src/math/mp/mp_core.h
index 579f3fef4..a84b38cdd 100644
--- a/src/math/mp/mp_core.h
+++ b/src/math/mp/mp_core.h
@@ -98,15 +98,17 @@ void bigint_linmul3(word z[], const word x[], size_t x_size, word y);
/**
* Montgomery Reduction
-* @param z integer to reduce (also output in first p_size+1 words)
-* @param z_size size of z (should be >= 2*p_size+1)
+* @param z integer to reduce, of size exactly 2*(p_size+1).
+ Output is in the first p_size+1 words, higher
+ words are set to zero.
* @param p modulus
* @param p_size size of p
* @param p_dash Montgomery value
* @param workspace array of at least 2*(p_size+1) words
*/
-void bigint_monty_redc(word z[], size_t z_size,
- const word p[], size_t p_size, word p_dash,
+void bigint_monty_redc(word z[],
+ const word p[], size_t p_size,
+ word p_dash,
word workspace[]);
/*
diff --git a/src/math/mp/mp_karat.cpp b/src/math/mp/mp_karat.cpp
index 6d9adb4bf..b549a05c8 100644
--- a/src/math/mp/mp_karat.cpp
+++ b/src/math/mp/mp_karat.cpp
@@ -253,10 +253,7 @@ void bigint_mul(word z[], size_t z_size, word workspace[],
const size_t N = karatsuba_size(z_size, x_size, x_sw, y_size, y_sw);
if(N)
- {
- clear_mem(workspace, 2*N);
karatsuba_mul(z, x, y, N, workspace);
- }
else
bigint_simple_mul(z, x, x_sw, y, y_sw);
}
@@ -297,10 +294,7 @@ void bigint_sqr(word z[], size_t z_size, word workspace[],
const size_t N = karatsuba_size(z_size, x_size, x_sw);
if(N)
- {
- clear_mem(workspace, 2*N);
karatsuba_sqr(z, x, N, workspace);
- }
else
bigint_simple_sqr(z, x, x_sw);
}
diff --git a/src/math/mp/mp_monty.cpp b/src/math/mp/mp_monty.cpp
index d37fb5844..57a2b51a6 100644
--- a/src/math/mp/mp_monty.cpp
+++ b/src/math/mp/mp_monty.cpp
@@ -18,10 +18,12 @@ extern "C" {
/*
* Montgomery Reduction Algorithm
*/
-void bigint_monty_redc(word z[], size_t z_size,
+void bigint_monty_redc(word z[],
const word p[], size_t p_size,
word p_dash, word ws[])
{
+ const size_t z_size = 2*(p_size+1);
+
const size_t blocks_of_8 = p_size - (p_size % 8);
for(size_t i = 0; i != p_size; ++i)
@@ -76,7 +78,7 @@ void bigint_monty_mul(word z[], size_t z_size,
&x[0], x_size, x_sw,
&y[0], y_size, y_sw);
- bigint_monty_redc(&z[0], z_size,
+ bigint_monty_redc(&z[0],
&p[0], p_size, p_dash,
&ws[0]);
}
@@ -89,7 +91,7 @@ void bigint_monty_sqr(word z[], size_t z_size,
bigint_sqr(&z[0], z_size, &ws[0],
&x[0], x_size, x_sw);
- bigint_monty_redc(&z[0], z_size,
+ bigint_monty_redc(&z[0],
&p[0], p_size, p_dash,
&ws[0]);
}
diff --git a/src/math/numbertheory/powm_mnt.cpp b/src/math/numbertheory/powm_mnt.cpp
index 416f430b7..1928cef9d 100644
--- a/src/math/numbertheory/powm_mnt.cpp
+++ b/src/math/numbertheory/powm_mnt.cpp
@@ -90,7 +90,6 @@ void Montgomery_Exponentiator::set_base(const BigInt& base)
m_modulus.data(), m_mod_words, m_mod_prime,
&workspace[0]);
- z.mask_bits(BOTAN_MP_WORD_BITS * (m_mod_words + 1));
m_g[0] = z;
const BigInt& x = m_g[0];
@@ -101,15 +100,12 @@ void Montgomery_Exponentiator::set_base(const BigInt& base)
const BigInt& y = m_g[i-1];
const size_t y_sig = y.sig_words();
- z.clear();
-
bigint_monty_mul(z.mutable_data(), z.size(),
x.data(), x.size(), x_sig,
y.data(), y.size(), y_sig,
m_modulus.data(), m_mod_words, m_mod_prime,
&workspace[0]);
- z.mask_bits(BOTAN_MP_WORD_BITS * (m_mod_words + 1));
m_g[i] = z;
}
}
@@ -132,14 +128,11 @@ BigInt Montgomery_Exponentiator::execute() const
{
for(size_t k = 0; k != m_window_bits; ++k)
{
- z.clear();
-
bigint_monty_sqr(z.mutable_data(), z_size,
x.data(), x.size(), x.sig_words(),
m_modulus.data(), m_mod_words, m_mod_prime,
&workspace[0]);
- z.mask_bits(BOTAN_MP_WORD_BITS * (m_mod_words + 1));
x = z;
}
@@ -147,27 +140,22 @@ BigInt Montgomery_Exponentiator::execute() const
{
const BigInt& y = m_g[nibble-1];
- z.clear();
-
bigint_monty_mul(z.mutable_data(), z_size,
x.data(), x.size(), x.sig_words(),
y.data(), y.size(), y.sig_words(),
m_modulus.data(), m_mod_words, m_mod_prime,
&workspace[0]);
- z.mask_bits(BOTAN_MP_WORD_BITS * (m_mod_words + 1));
x = z;
}
}
x.grow_to(2*m_mod_words + 1);
- bigint_monty_redc(x.mutable_data(), x.size(),
+ bigint_monty_redc(x.mutable_data(),
m_modulus.data(), m_mod_words, m_mod_prime,
&workspace[0]);
- x.mask_bits(MP_WORD_BITS * (m_mod_words + 1));
-
return x;
}