aboutsummaryrefslogtreecommitdiffstats
path: root/src/math/mp
diff options
context:
space:
mode:
Diffstat (limited to 'src/math/mp')
-rw-r--r--src/math/mp/mp_core.h19
-rw-r--r--src/math/mp/mp_monty.cpp33
2 files changed, 49 insertions, 3 deletions
diff --git a/src/math/mp/mp_core.h b/src/math/mp/mp_core.h
index e1692006e..96f43d713 100644
--- a/src/math/mp/mp_core.h
+++ b/src/math/mp/mp_core.h
@@ -77,7 +77,7 @@ void bigint_simple_sqr(word z[], const word x[], size_t x_size);
void bigint_linmul2(word x[], size_t x_size, word y);
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 x_size+1 words)
* @param z_size size of z (should be >= 2*x_size+1)
@@ -92,6 +92,23 @@ void bigint_monty_redc(word z[], size_t z_size,
word u);
/*
+* Montgomery Multiplication
+*/
+void bigint_monty_mul(word z[], size_t z_size,
+ const word x[], size_t x_size, size_t x_sw,
+ const word y[], size_t y_size, size_t y_sw,
+ const word p[], size_t p_size, word p_dash,
+ word workspace[]);
+
+/*
+* Montgomery Squaring
+*/
+void bigint_monty_sqr(word z[], size_t z_size,
+ const word x[], size_t x_size, size_t x_sw,
+ const word p[], size_t p_size, word p_dash,
+ word workspace[]);
+
+/*
* Division operation
*/
size_t bigint_divcore(word q, word y2, word y1,
diff --git a/src/math/mp/mp_monty.cpp b/src/math/mp/mp_monty.cpp
index d7f7e0306..cdb31f951 100644
--- a/src/math/mp/mp_monty.cpp
+++ b/src/math/mp/mp_monty.cpp
@@ -31,10 +31,10 @@ void bigint_monty_redc(word z[], size_t z_size,
const word y = z_i[0] * u;
- /*
+#if 1
bigint_linmul3(ws, x, x_size, y);
bigint_add2(z_i, z_size - i, ws, x_size+1);
- */
+#else
word carry = 0;
for(size_t j = 0; j != blocks_of_8; j += 8)
@@ -53,6 +53,7 @@ void bigint_monty_redc(word z[], size_t z_size,
++z_i[j];
carry = !z_i[j];
}
+#endif
}
word borrow = 0;
@@ -67,6 +68,34 @@ void bigint_monty_redc(word z[], size_t z_size,
clear_mem(z + x_size + 1, z_size - x_size - 1);
}
+void bigint_monty_mul(word z[], size_t z_size,
+ const word x[], size_t x_size, size_t x_sw,
+ const word y[], size_t y_size, size_t y_sw,
+ const word p[], size_t p_size, word p_dash,
+ word workspace[])
+ {
+ bigint_mul(&z[0], z_size, &workspace[0],
+ &x[0], x_size, x_sw,
+ &y[0], y_size, y_sw);
+
+ bigint_monty_redc(&z[0], z_size,
+ &workspace[0],
+ &p[0], p_size, p_dash);
+
+ }
+
+void bigint_monty_sqr(word z[], size_t z_size,
+ const word x[], size_t x_size, size_t x_sw,
+ const word p[], size_t p_size, word p_dash,
+ word workspace[])
+ {
+ bigint_sqr(&z[0], z_size, &workspace[0],
+ &x[0], x_size, x_sw);
+
+ bigint_monty_redc(&z[0], z_size, &workspace[0],
+ &p[0], p_size, p_dash);
+ }
+
}
}