From: Dmitry Baryshkov <dbaryshkov(a)gmail.com>
GOST curves will require different "fixups" for fast (mul X mod p)
operations. Move these operations to ecc_modulo structure and call them
via function pointer.
Signed-off-by: Dmitry Baryshkov <dbaryshkov(a)gmail.com>
---
ecc-add-jja.c | 8 ++++----
ecc-add-jjj.c | 8 ++++----
ecc-curve25519.c | 6 ++++++
ecc-curve448.c | 6 ++++++
ecc-dup-jj.c | 8 ++++----
ecc-gost-gc256b.c | 6 ++++++
ecc-gost-gc512a.c | 6 ++++++
ecc-internal.h | 25 ++++++++++++++++---------
ecc-mod-arith.c | 12 ++++++------
ecc-mul-m.c | 6 +++---
ecc-secp192r1.c | 6 ++++++
ecc-secp224r1.c | 6 ++++++
ecc-secp256r1.c | 6 ++++++
ecc-secp384r1.c | 6 ++++++
ecc-secp521r1.c | 6 ++++++
15 files changed, 91 insertions(+), 30 deletions(-)
diff --git a/ecc-add-jja.c b/ecc-add-jja.c
index 037711d38249..55ad954587da 100644
--- a/ecc-add-jja.c
+++ b/ecc-add-jja.c
@@ -102,10 +102,10 @@ ecc_add_jja (const struct ecc_curve *ecc,
/* w */
ecc_mod_mul (&ecc->p, j, y2, w);
ecc_mod_sub (&ecc->p, w, j, y1);
- ecc_mod_mul_1 (&ecc->p, w, w, 2);
+ ecc->p.mul_1 (&ecc->p, w, w, 2);
/* i replaces hh, j */
- ecc_mod_mul_1 (&ecc->p, hh, hh, 4);
+ ecc->p.mul_1 (&ecc->p, hh, hh, 4);
ecc_mod_mul (&ecc->p, j, hh, h);
/* v */
@@ -114,12 +114,12 @@ ecc_add_jja (const struct ecc_curve *ecc,
/* x_3, use (h, hh) as sqratch */
ecc_mod_sqr (&ecc->p, h, w);
ecc_mod_sub (&ecc->p, r, h, j);
- ecc_mod_submul_1 (&ecc->p, r, v, 2);
+ ecc->p.submul_1 (&ecc->p, r, v, 2);
/* y_3, use (h, hh) as sqratch */
ecc_mod_mul (&ecc->p, h, y1, j); /* frees j */
ecc_mod_sub (&ecc->p, r + ecc->p.size, v, r);
ecc_mod_mul (&ecc->p, j, r + ecc->p.size, w);
- ecc_mod_submul_1 (&ecc->p, j, h, 2);
+ ecc->p.submul_1 (&ecc->p, j, h, 2);
mpn_copyi (r + ecc->p.size, j, ecc->p.size);
}
diff --git a/ecc-add-jjj.c b/ecc-add-jjj.c
index 54b2246aeb24..cad26193234a 100644
--- a/ecc-add-jjj.c
+++ b/ecc-add-jjj.c
@@ -94,14 +94,14 @@ ecc_add_jjj (const struct ecc_curve *ecc,
ecc_mod_mul (&ecc->p, s1, p + ecc->p.size, v);
ecc_mod_mul (&ecc->p, v, j, q + ecc->p.size);
ecc_mod_sub (&ecc->p, s2, v, s1);
- ecc_mod_mul_1 (&ecc->p, s2, s2, 2);
+ ecc->p.mul_1 (&ecc->p, s2, s2, 2);
/* Store z3 */
mpn_copyi (r + 2*ecc->p.size, i, ecc->p.size);
/* i, j, v */
ecc_mod_sqr (&ecc->p, i, u2);
- ecc_mod_mul_1 (&ecc->p, i, i, 4);
+ ecc->p.mul_1 (&ecc->p, i, i, 4);
ecc_mod_mul (&ecc->p, j, u2, i);
ecc_mod_mul (&ecc->p, v, u1, i);
@@ -109,12 +109,12 @@ ecc_add_jjj (const struct ecc_curve *ecc,
/* x3, use u1, u2 as scratch */
ecc_mod_sqr (&ecc->p, u1, s2);
ecc_mod_sub (&ecc->p, r, u1, j);
- ecc_mod_submul_1 (&ecc->p, r, v, 2);
+ ecc->p.submul_1 (&ecc->p, r, v, 2);
/* y3 */
ecc_mod_mul (&ecc->p, u1, s1, j); /* Frees j */
ecc_mod_sub (&ecc->p, u2, v, r); /* Frees v */
ecc_mod_mul (&ecc->p, i, s2, u2);
- ecc_mod_submul_1 (&ecc->p, i, u1, 2);
+ ecc->p.submul_1 (&ecc->p, i, u1, 2);
mpn_copyi (r + ecc->p.size, i, ecc->p.size);
}
diff --git a/ecc-curve25519.c b/ecc-curve25519.c
index f8f2c64af868..04df696f7357 100644
--- a/ecc-curve25519.c
+++ b/ecc-curve25519.c
@@ -310,6 +310,9 @@ const struct ecc_curve _nettle_curve25519 =
ecc_curve25519_modp,
ecc_curve25519_inv,
ecc_curve25519_sqrt,
+
+ ecc_mod_mul_1_std,
+ ecc_mod_submul_1_std,
},
{
253,
@@ -329,6 +332,9 @@ const struct ecc_curve _nettle_curve25519 =
ecc_curve25519_modq,
ecc_mod_inv,
NULL,
+
+ NULL,
+ NULL,
},
0, /* No redc */
diff --git a/ecc-curve448.c b/ecc-curve448.c
index 484b7d1e0870..ce7a25d14c4e 100644
--- a/ecc-curve448.c
+++ b/ecc-curve448.c
@@ -288,6 +288,9 @@ const struct ecc_curve _nettle_curve448 =
ecc_curve448_modp,
ecc_curve448_inv,
ecc_curve448_sqrt,
+
+ ecc_mod_mul_1_std,
+ ecc_mod_submul_1_std,
},
{
446,
@@ -307,6 +310,9 @@ const struct ecc_curve _nettle_curve448 =
ecc_mod, /* FIXME: Implement optimized reduce function */
ecc_mod_inv,
NULL,
+
+ NULL,
+ NULL,
},
0, /* No redc */
diff --git a/ecc-dup-jj.c b/ecc-dup-jj.c
index 2247e8fdfd5a..4bbd5163c0e3 100644
--- a/ecc-dup-jj.c
+++ b/ecc-dup-jj.c
@@ -87,7 +87,7 @@ ecc_dup_jj (const struct ecc_curve *ecc,
ecc_mod_add (&ecc->p, sum, xp, delta);
ecc_mod_sub (&ecc->p, delta, xp, delta);
ecc_mod_mul (&ecc->p, beta, sum, delta);
- ecc_mod_mul_1 (&ecc->p, alpha, beta, 3);
+ ecc->p.mul_1 (&ecc->p, alpha, beta, 3);
/* beta */
ecc_mod_mul (&ecc->p, beta, xp, gamma);
@@ -95,16 +95,16 @@ ecc_dup_jj (const struct ecc_curve *ecc,
/* Do gamma^2 and 4*beta early, to get them out of the way. We can
then use the old area at gamma as scratch. */
ecc_mod_sqr (&ecc->p, g2, gamma);
- ecc_mod_mul_1 (&ecc->p, sum, beta, 4);
+ ecc->p.mul_1 (&ecc->p, sum, beta, 4);
/* x' */
ecc_mod_sqr (&ecc->p, gamma, alpha); /* Overwrites gamma and beta */
- ecc_mod_submul_1 (&ecc->p, gamma, sum, 2);
+ ecc->p.submul_1 (&ecc->p, gamma, sum, 2);
mpn_copyi (r, gamma, ecc->p.size);
/* y' */
ecc_mod_sub (&ecc->p, sum, sum, r);
ecc_mod_mul (&ecc->p, gamma, sum, alpha);
- ecc_mod_submul_1 (&ecc->p, gamma, g2, 8);
+ ecc->p.submul_1 (&ecc->p, gamma, g2, 8);
mpn_copyi (r + ecc->p.size, gamma, ecc->p.size);
}
diff --git a/ecc-gost-gc256b.c b/ecc-gost-gc256b.c
index a23d46fc8af6..24e1ac6c99a7 100644
--- a/ecc-gost-gc256b.c
+++ b/ecc-gost-gc256b.c
@@ -77,6 +77,9 @@ const struct ecc_curve _nettle_gost_gc256b =
ecc_gost_gc256b_modp,
ecc_mod_inv,
NULL,
+
+ ecc_mod_mul_1_std,
+ ecc_mod_submul_1_std,
},
{
256,
@@ -96,6 +99,9 @@ const struct ecc_curve _nettle_gost_gc256b =
ecc_gost_gc256b_modq,
ecc_mod_inv,
NULL,
+
+ NULL,
+ NULL,
},
USE_REDC,
diff --git a/ecc-gost-gc512a.c b/ecc-gost-gc512a.c
index 398762c337d6..5de4eda85d9c 100644
--- a/ecc-gost-gc512a.c
+++ b/ecc-gost-gc512a.c
@@ -77,6 +77,9 @@ const struct ecc_curve _nettle_gost_gc512a =
ecc_gost_gc512a_modp,
ecc_mod_inv,
NULL,
+
+ ecc_mod_mul_1_std,
+ ecc_mod_submul_1_std,
},
{
512,
@@ -96,6 +99,9 @@ const struct ecc_curve _nettle_gost_gc512a =
ecc_gost_gc512a_modq,
ecc_mod_inv,
NULL,
+
+ NULL,
+ NULL,
},
USE_REDC,
diff --git a/ecc-internal.h b/ecc-internal.h
index 9e24e0ce4521..e1380bfb2b20 100644
--- a/ecc-internal.h
+++ b/ecc-internal.h
@@ -44,9 +44,9 @@
#define ecc_pm1_redc _nettle_ecc_pm1_redc
#define ecc_mod_add _nettle_ecc_mod_add
#define ecc_mod_sub _nettle_ecc_mod_sub
-#define ecc_mod_mul_1 _nettle_ecc_mod_mul_1
-#define ecc_mod_addmul_1 _nettle_ecc_mod_addmul_1
-#define ecc_mod_submul_1 _nettle_ecc_mod_submul_1
+#define ecc_mod_mul_1_std _nettle_ecc_mod_mul_1_std
+#define ecc_mod_addmul_1_std _nettle_ecc_mod_addmul_1_std
+#define ecc_mod_submul_1_std _nettle_ecc_mod_submul_1_std
#define ecc_mod_mul _nettle_ecc_mod_mul
#define ecc_mod_sqr _nettle_ecc_mod_sqr
#define ecc_mod_random _nettle_ecc_mod_random
@@ -146,6 +146,10 @@ typedef void ecc_h_to_a_func (const struct ecc_curve *ecc,
mp_limb_t *r, const mp_limb_t *p,
mp_limb_t *scratch);
+typedef void ecc_mod_mul_1_func (const struct ecc_modulo *m,
+ mp_limb_t *rp,
+ const mp_limb_t *ap, mp_limb_t b);
+
struct ecc_modulo
{
unsigned short bit_size;
@@ -170,6 +174,9 @@ struct ecc_modulo
ecc_mod_func *reduce;
ecc_mod_inv_func *invert;
ecc_mod_sqrt_func *sqrt;
+
+ ecc_mod_mul_1_func *mul_1;
+ ecc_mod_mul_1_func *submul_1;
};
/* Represents an elliptic curve of the form
@@ -237,15 +244,15 @@ ecc_mod_sub (const struct ecc_modulo *m, mp_limb_t *rp,
const mp_limb_t *ap, const mp_limb_t *bp);
void
-ecc_mod_mul_1 (const struct ecc_modulo *m, mp_limb_t *rp,
- const mp_limb_t *ap, const mp_limb_t b);
+ecc_mod_mul_1_std (const struct ecc_modulo *m, mp_limb_t *rp,
+ const mp_limb_t *ap, const mp_limb_t b);
void
-ecc_mod_addmul_1 (const struct ecc_modulo *m, mp_limb_t *rp,
- const mp_limb_t *ap, mp_limb_t b);
+ecc_mod_addmul_1_std (const struct ecc_modulo *m, mp_limb_t *rp,
+ const mp_limb_t *ap, mp_limb_t b);
void
-ecc_mod_submul_1 (const struct ecc_modulo *m, mp_limb_t *rp,
- const mp_limb_t *ap, mp_limb_t b);
+ecc_mod_submul_1_std (const struct ecc_modulo *m, mp_limb_t *rp,
+ const mp_limb_t *ap, mp_limb_t b);
/* The mul and sqr functions need 2*m->size limbs at rp */
void
diff --git a/ecc-mod-arith.c b/ecc-mod-arith.c
index f2e47f6747c1..0399a2cdd7c5 100644
--- a/ecc-mod-arith.c
+++ b/ecc-mod-arith.c
@@ -65,8 +65,8 @@ ecc_mod_sub (const struct ecc_modulo *m, mp_limb_t *rp,
}
void
-ecc_mod_mul_1 (const struct ecc_modulo *m, mp_limb_t *rp,
- const mp_limb_t *ap, mp_limb_t b)
+ecc_mod_mul_1_std (const struct ecc_modulo *m, mp_limb_t *rp,
+ const mp_limb_t *ap, mp_limb_t b)
{
mp_limb_t hi;
@@ -80,8 +80,8 @@ ecc_mod_mul_1 (const struct ecc_modulo *m, mp_limb_t *rp,
}
void
-ecc_mod_addmul_1 (const struct ecc_modulo *m, mp_limb_t *rp,
- const mp_limb_t *ap, mp_limb_t b)
+ecc_mod_addmul_1_std (const struct ecc_modulo *m, mp_limb_t *rp,
+ const mp_limb_t *ap, mp_limb_t b)
{
mp_limb_t hi;
@@ -95,8 +95,8 @@ ecc_mod_addmul_1 (const struct ecc_modulo *m, mp_limb_t *rp,
}
void
-ecc_mod_submul_1 (const struct ecc_modulo *m, mp_limb_t *rp,
- const mp_limb_t *ap, mp_limb_t b)
+ecc_mod_submul_1_std (const struct ecc_modulo *m, mp_limb_t *rp,
+ const mp_limb_t *ap, mp_limb_t b)
{
mp_limb_t hi;
diff --git a/ecc-mul-m.c b/ecc-mul-m.c
index 68bdd16e8e94..539b9d0677e7 100644
--- a/ecc-mul-m.c
+++ b/ecc-mul-m.c
@@ -80,7 +80,7 @@ ecc_mul_m (const struct ecc_modulo *m,
ecc_mod_sqr (m, BB, B);
ecc_mod_mul (m, x3, AA, BB);
ecc_mod_sub (m, E, AA, BB);
- ecc_mod_addmul_1 (m, AA, E, a24);
+ ecc_mod_addmul_1_std (m, AA, E, a24);
ecc_mod_mul (m, z3, E, AA);
for (i = bit_high; i >= bit_low; i--)
@@ -98,7 +98,7 @@ ecc_mul_m (const struct ecc_modulo *m,
ecc_mod_sqr (m, BB, B);
ecc_mod_mul (m, x2, AA, BB); /* Last use of BB */
ecc_mod_sub (m, E, AA, BB);
- ecc_mod_addmul_1 (m, AA, E, a24);
+ ecc_mod_addmul_1_std (m, AA, E, a24);
ecc_mod_add (m, C, x3, z3);
ecc_mod_sub (m, D, x3, z3);
ecc_mod_mul (m, z2, E, AA); /* Last use of E and AA */
@@ -124,7 +124,7 @@ ecc_mul_m (const struct ecc_modulo *m,
ecc_mod_sqr (m, BB, B);
ecc_mod_mul (m, x2, AA, BB);
ecc_mod_sub (m, E, AA, BB);
- ecc_mod_addmul_1 (m, AA, E, a24);
+ ecc_mod_addmul_1_std (m, AA, E, a24);
ecc_mod_mul (m, z2, E, AA);
}
assert (m->invert_itch <= 7 * m->size);
diff --git a/ecc-secp192r1.c b/ecc-secp192r1.c
index 046026f3f697..79080495ec7e 100644
--- a/ecc-secp192r1.c
+++ b/ecc-secp192r1.c
@@ -130,6 +130,9 @@ const struct ecc_curve _nettle_secp_192r1 =
ecc_secp192r1_modp,
ecc_mod_inv,
NULL,
+
+ ecc_mod_mul_1_std,
+ ecc_mod_submul_1_std,
},
{
192,
@@ -149,6 +152,9 @@ const struct ecc_curve _nettle_secp_192r1 =
ecc_mod,
ecc_mod_inv,
NULL,
+
+ NULL,
+ NULL,
},
USE_REDC,
diff --git a/ecc-secp224r1.c b/ecc-secp224r1.c
index 05d84017a68a..064a9e2f7fd4 100644
--- a/ecc-secp224r1.c
+++ b/ecc-secp224r1.c
@@ -82,6 +82,9 @@ const struct ecc_curve _nettle_secp_224r1 =
USE_REDC ? ecc_secp224r1_redc : ecc_secp224r1_modp,
ecc_mod_inv,
NULL,
+
+ ecc_mod_mul_1_std,
+ ecc_mod_submul_1_std,
},
{
224,
@@ -101,6 +104,9 @@ const struct ecc_curve _nettle_secp_224r1 =
ecc_mod,
ecc_mod_inv,
NULL,
+
+ NULL,
+ NULL,
},
USE_REDC,
diff --git a/ecc-secp256r1.c b/ecc-secp256r1.c
index d399642453d5..0a25a086f3fe 100644
--- a/ecc-secp256r1.c
+++ b/ecc-secp256r1.c
@@ -259,6 +259,9 @@ const struct ecc_curve _nettle_secp_256r1 =
USE_REDC ? ecc_secp256r1_redc : ecc_secp256r1_modp,
ecc_mod_inv,
NULL,
+
+ ecc_mod_mul_1_std,
+ ecc_mod_submul_1_std,
},
{
256,
@@ -278,6 +281,9 @@ const struct ecc_curve _nettle_secp_256r1 =
ecc_secp256r1_modq,
ecc_mod_inv,
NULL,
+
+ NULL,
+ NULL,
},
USE_REDC,
diff --git a/ecc-secp384r1.c b/ecc-secp384r1.c
index 54bcd1128d39..5a9131f72c98 100644
--- a/ecc-secp384r1.c
+++ b/ecc-secp384r1.c
@@ -167,6 +167,9 @@ const struct ecc_curve _nettle_secp_384r1 =
ecc_secp384r1_modp,
ecc_mod_inv,
NULL,
+
+ ecc_mod_mul_1_std,
+ ecc_mod_submul_1_std,
},
{
384,
@@ -186,6 +189,9 @@ const struct ecc_curve _nettle_secp_384r1 =
ecc_mod,
ecc_mod_inv,
NULL,
+
+ NULL,
+ NULL,
},
USE_REDC,
diff --git a/ecc-secp521r1.c b/ecc-secp521r1.c
index 776f7ae03e27..f01a97537eb8 100644
--- a/ecc-secp521r1.c
+++ b/ecc-secp521r1.c
@@ -95,6 +95,9 @@ const struct ecc_curve _nettle_secp_521r1 =
ecc_secp521r1_modp,
ecc_mod_inv,
NULL,
+
+ ecc_mod_mul_1_std,
+ ecc_mod_submul_1_std,
},
{
521,
@@ -114,6 +117,9 @@ const struct ecc_curve _nettle_secp_521r1 =
ecc_mod,
ecc_mod_inv,
NULL,
+
+ NULL,
+ NULL,
},
USE_REDC,
--
2.25.0