summaryrefslogtreecommitdiff
path: root/ecc-mod-arith.c
diff options
context:
space:
mode:
authorNiels Möller <nisse@lysator.liu.se>2020-10-14 20:17:08 +0200
committerNiels Möller <nisse@lysator.liu.se>2020-10-14 20:17:08 +0200
commit1cbc9e094eae458ff83b0a59c33a929520c51a63 (patch)
treef36f1075a0baec46379a6aa31bc99b0d31e1a1f2 /ecc-mod-arith.c
parent04b25045c8aeed37b875861378302aa086f287a8 (diff)
downloadnettle-1cbc9e094eae458ff83b0a59c33a929520c51a63.tar.gz
Shared implementation of ecc_mod_pow_2k and related functions
Diffstat (limited to 'ecc-mod-arith.c')
-rw-r--r--ecc-mod-arith.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/ecc-mod-arith.c b/ecc-mod-arith.c
index f2e47f67..0b315552 100644
--- a/ecc-mod-arith.c
+++ b/ecc-mod-arith.c
@@ -125,3 +125,41 @@ ecc_mod_sqr (const struct ecc_modulo *m, mp_limb_t *rp,
mpn_sqr (rp, ap, m->size);
m->reduce (m, rp);
}
+
+/* Compute R <-- X^{2^k} mod M. Needs 2*ecc->size limbs at rp, and
+ 2*ecc->size additional limbs of scratch space. No overlap
+ allowed. */
+void
+ecc_mod_pow_2k (const struct ecc_modulo *m,
+ mp_limb_t *rp, const mp_limb_t *xp,
+ unsigned k, mp_limb_t *tp)
+{
+ if (k & 1)
+ {
+ ecc_mod_sqr (m, rp, xp);
+ k--;
+ }
+ else
+ {
+ ecc_mod_sqr (m, tp, xp);
+ ecc_mod_sqr (m, rp, tp);
+ k -= 2;
+ }
+ while (k > 0)
+ {
+ ecc_mod_sqr (m, tp, rp);
+ ecc_mod_sqr (m, rp, tp);
+ k -= 2;
+ }
+}
+
+/* Computes R <-- X^{2^k} * Y. Scratch requirements as ecc_mod_pow_2k. */
+void
+ecc_mod_pow_2k_mul (const struct ecc_modulo *m,
+ mp_limb_t *rp, const mp_limb_t *xp,
+ unsigned k, const mp_limb_t *yp,
+ mp_limb_t *tp)
+{
+ ecc_mod_pow_2k (m, tp, xp, k, rp);
+ ecc_mod_mul (m, rp, tp, yp);
+}