From: Niels Möller Date: Mon, 22 Sep 2014 12:41:33 +0000 (+0200) Subject: Moved mod and reduce function pointers to struct ecc_modulo. X-Git-Tag: nettle_3.1rc1~99 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=56079909294d1d5708e8959d219b23177e176ea6;p=thirdparty%2Fnettle.git Moved mod and reduce function pointers to struct ecc_modulo. --- diff --git a/ChangeLog b/ChangeLog index 8d1cd60e..a62f0108 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,6 +3,8 @@ * ecc-internal.h (struct ecc_curve): Deleted redc function pointer. Use only reduce pointer, which is redc or modp as applicable. Updated all users. + (struct ecc_modulo): Moved mod and reduce function pointers to + this struct. * ecc-generic-modp.c (ecc_generic_modp): Deleted file and function. We no longer need a wrapper around ecc_mod. diff --git a/ecc-192.c b/ecc-192.c index 347cdec1..68336335 100644 --- a/ecc-192.c +++ b/ecc-192.c @@ -121,6 +121,8 @@ const struct ecc_curve nettle_secp_192r1 = ecc_Bmodp, ecc_Bmodp_shifted, ecc_redc_ppm1, + ecc_192_modp, + ecc_192_modp, }, { 192, @@ -131,6 +133,8 @@ const struct ecc_curve nettle_secp_192r1 = ecc_Bmodq, ecc_Bmodq_shifted, NULL, + ecc_mod, + ecc_mod, }, USE_REDC, @@ -142,10 +146,6 @@ const struct ecc_curve nettle_secp_192r1 = ECC_MUL_G_ITCH (ECC_LIMB_SIZE), ECC_J_TO_A_ITCH (ECC_LIMB_SIZE), - ecc_192_modp, - ecc_192_modp, - ecc_mod, - ecc_add_jjj, ecc_mul_a, ecc_mul_g, diff --git a/ecc-224.c b/ecc-224.c index a5bed64a..f0f211ce 100644 --- a/ecc-224.c +++ b/ecc-224.c @@ -73,6 +73,8 @@ const struct ecc_curve nettle_secp_224r1 = ecc_Bmodp, ecc_Bmodp_shifted, ecc_redc_ppm1, + ecc_224_modp, + USE_REDC ? ecc_224_redc : ecc_224_modp, }, { 224, @@ -83,6 +85,8 @@ const struct ecc_curve nettle_secp_224r1 = ecc_Bmodq, ecc_Bmodq_shifted, NULL, + ecc_mod, + ecc_mod, }, USE_REDC, @@ -94,10 +98,6 @@ const struct ecc_curve nettle_secp_224r1 = ECC_MUL_G_ITCH (ECC_LIMB_SIZE), ECC_J_TO_A_ITCH (ECC_LIMB_SIZE), - ecc_224_modp, - USE_REDC ? ecc_224_redc : ecc_224_modp, - ecc_mod, - ecc_add_jjj, ecc_mul_a, ecc_mul_g, diff --git a/ecc-25519.c b/ecc-25519.c index e973fc45..0c02d7e7 100644 --- a/ecc-25519.c +++ b/ecc-25519.c @@ -241,6 +241,8 @@ const struct ecc_curve nettle_curve25519 = ecc_Bmodp, ecc_Bmodp_shifted, NULL, + ecc_25519_modp, + ecc_25519_modp, }, { 253, @@ -251,6 +253,8 @@ const struct ecc_curve nettle_curve25519 = ecc_Bmodq, ecc_mBmodq_shifted, /* Use q - 2^{252} instead. */ NULL, + ecc_25519_modq, + ecc_25519_modq, }, 0, /* No redc */ @@ -262,10 +266,6 @@ const struct ecc_curve nettle_curve25519 = ECC_MUL_G_EH_ITCH (ECC_LIMB_SIZE), ECC_EH_TO_A_ITCH (ECC_LIMB_SIZE), - ecc_25519_modp, - ecc_25519_modp, - ecc_25519_modq, - ecc_add_ehh, ecc_mul_a_eh, ecc_mul_g_eh, diff --git a/ecc-256.c b/ecc-256.c index a2a03fb5..843ee769 100644 --- a/ecc-256.c +++ b/ecc-256.c @@ -236,6 +236,8 @@ const struct ecc_curve nettle_secp_256r1 = ecc_Bmodp, ecc_Bmodp_shifted, ecc_redc_ppm1, + ecc_256_modp, + USE_REDC ? ecc_256_redc : ecc_256_modp, }, { 256, @@ -246,6 +248,8 @@ const struct ecc_curve nettle_secp_256r1 = ecc_Bmodq, ecc_Bmodq_shifted, NULL, + ecc_256_modq, + ecc_256_modq, }, USE_REDC, @@ -257,10 +261,6 @@ const struct ecc_curve nettle_secp_256r1 = ECC_MUL_G_ITCH (ECC_LIMB_SIZE), ECC_J_TO_A_ITCH (ECC_LIMB_SIZE), - ecc_256_modp, - USE_REDC ? ecc_256_redc : ecc_256_modp, - ecc_256_modq, - ecc_add_jjj, ecc_mul_a, ecc_mul_g, diff --git a/ecc-384.c b/ecc-384.c index 2493d36e..ab48f348 100644 --- a/ecc-384.c +++ b/ecc-384.c @@ -158,6 +158,8 @@ const struct ecc_curve nettle_secp_384r1 = ecc_Bmodp, ecc_Bmodp_shifted, ecc_redc_ppm1, + ecc_384_modp, + ecc_384_modp, }, { 384, @@ -168,6 +170,8 @@ const struct ecc_curve nettle_secp_384r1 = ecc_Bmodq, ecc_Bmodq_shifted, NULL, + ecc_mod, + ecc_mod, }, USE_REDC, @@ -179,10 +183,6 @@ const struct ecc_curve nettle_secp_384r1 = ECC_MUL_G_ITCH (ECC_LIMB_SIZE), ECC_J_TO_A_ITCH (ECC_LIMB_SIZE), - ecc_384_modp, - ecc_384_modp, - ecc_mod, - ecc_add_jjj, ecc_mul_a, ecc_mul_g, diff --git a/ecc-521.c b/ecc-521.c index 3876f6ed..4e1f1ab3 100644 --- a/ecc-521.c +++ b/ecc-521.c @@ -86,6 +86,8 @@ const struct ecc_curve nettle_secp_521r1 = ecc_Bmodp, ecc_Bmodp_shifted, ecc_redc_ppm1, + ecc_521_modp, + ecc_521_modp, }, { 521, @@ -96,6 +98,8 @@ const struct ecc_curve nettle_secp_521r1 = ecc_Bmodq, ecc_Bmodq_shifted, NULL, + ecc_mod, + ecc_mod, }, USE_REDC, @@ -107,10 +111,6 @@ const struct ecc_curve nettle_secp_521r1 = ECC_MUL_G_ITCH (ECC_LIMB_SIZE), ECC_J_TO_A_ITCH (ECC_LIMB_SIZE), - ecc_521_modp, - ecc_521_modp, - ecc_mod, - ecc_add_jjj, ecc_mul_a, ecc_mul_g, diff --git a/ecc-a-to-j.c b/ecc-a-to-j.c index ea0fa68c..9fb0d2b8 100644 --- a/ecc-a-to-j.c +++ b/ecc-a-to-j.c @@ -47,10 +47,10 @@ ecc_a_to_j (const struct ecc_curve *ecc, mpn_copyd (r + ecc->p.size, p, 2*ecc->p.size); mpn_zero (r, ecc->p.size); - ecc->modp (&ecc->p, r); + ecc->p.mod (&ecc->p, r); mpn_zero (r + ecc->p.size, ecc->p.size); - ecc->modp (&ecc->p, r + ecc->p.size); + ecc->p.mod (&ecc->p, r + ecc->p.size); } else if (r != p) mpn_copyi (r, p, 2*ecc->p.size); diff --git a/ecc-internal.h b/ecc-internal.h index 1088f25a..f8be2647 100644 --- a/ecc-internal.h +++ b/ecc-internal.h @@ -75,22 +75,7 @@ /* And for ecc_mul_a_eh */ #define ECC_MUL_A_EH_WBITS 4 -struct ecc_modulo -{ - unsigned short bit_size; - unsigned short size; - unsigned short B_size; - unsigned short redc_size; - - const mp_limb_t *m; - /* B^size mod m. Expected to have at least 32 leading zeros - (equality for secp_256r1). */ - const mp_limb_t *B; - /* 2^{bit_size} - p, same value as above, but shifted. */ - const mp_limb_t *B_shifted; - /* m +/- 1, for redc, excluding redc_size low limbs. */ - const mp_limb_t *redc_mpm1; -}; +struct ecc_modulo; /* Reduces from 2*ecc->size to ecc->size. */ /* Required to return a result < 2q. This property is inherited by @@ -115,6 +100,26 @@ 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); +struct ecc_modulo +{ + unsigned short bit_size; + unsigned short size; + unsigned short B_size; + unsigned short redc_size; + + const mp_limb_t *m; + /* B^size mod m. Expected to have at least 32 leading zeros + (equality for secp_256r1). */ + const mp_limb_t *B; + /* 2^{bit_size} - p, same value as above, but shifted. */ + const mp_limb_t *B_shifted; + /* m +/- 1, for redc, excluding redc_size low limbs. */ + const mp_limb_t *redc_mpm1; + + ecc_mod_func *mod; + ecc_mod_func *reduce; +}; + /* Represents an elliptic curve of the form y^2 = x^3 - 3x + b (mod p) @@ -137,10 +142,6 @@ struct ecc_curve unsigned short mul_g_itch; unsigned short h_to_a_itch; - ecc_mod_func *modp; - ecc_mod_func *reduce; - ecc_mod_func *modq; - ecc_add_func *add_hhh; ecc_mul_func *mul; ecc_mul_g_func *mul_g; diff --git a/ecc-j-to-a.c b/ecc-j-to-a.c index c862d2c4..91b97e70 100644 --- a/ecc-j-to-a.c +++ b/ecc-j-to-a.c @@ -73,16 +73,16 @@ ecc_j_to_a (const struct ecc_curve *ecc, mpn_copyi (up, p + 2*ecc->p.size, ecc->p.size); mpn_zero (up + ecc->p.size, ecc->p.size); - ecc->reduce (&ecc->p, up); + ecc->p.reduce (&ecc->p, up); mpn_zero (up + ecc->p.size, ecc->p.size); - ecc->reduce (&ecc->p, up); + ecc->p.reduce (&ecc->p, up); ecc_modp_inv (ecc, izp, up, up + ecc->p.size); /* Divide this common factor by B */ mpn_copyi (izBp, izp, ecc->p.size); mpn_zero (izBp + ecc->p.size, ecc->p.size); - ecc->reduce (&ecc->p, izBp); + ecc->p.reduce (&ecc->p, izBp); ecc_modp_mul (ecc, iz2p, izp, izBp); } diff --git a/ecc-modp.c b/ecc-modp.c index 6f305aea..f48f7840 100644 --- a/ecc-modp.c +++ b/ecc-modp.c @@ -115,7 +115,7 @@ ecc_modp_mul (const struct ecc_curve *ecc, mp_limb_t *rp, const mp_limb_t *ap, const mp_limb_t *bp) { mpn_mul_n (rp, ap, bp, ecc->p.size); - ecc->reduce (&ecc->p, rp); + ecc->p.reduce (&ecc->p, rp); } void @@ -123,7 +123,7 @@ ecc_modp_sqr (const struct ecc_curve *ecc, mp_limb_t *rp, const mp_limb_t *ap) { mpn_sqr (rp, ap, ecc->p.size); - ecc->reduce (&ecc->p, rp); + ecc->p.reduce (&ecc->p, rp); } void diff --git a/ecc-modq.c b/ecc-modq.c index 54533bde..48bb2ba5 100644 --- a/ecc-modq.c +++ b/ecc-modq.c @@ -57,7 +57,7 @@ ecc_modq_mul (const struct ecc_curve *ecc, mp_limb_t *rp, const mp_limb_t *ap, const mp_limb_t *bp) { mpn_mul_n (rp, ap, bp, ecc->q.size); - ecc->modq (&ecc->q, rp); + ecc->q.mod (&ecc->q, rp); } void diff --git a/examples/ecc-benchmark.c b/examples/ecc-benchmark.c index e2b33c4a..f2d5c572 100644 --- a/examples/ecc-benchmark.c +++ b/examples/ecc-benchmark.c @@ -150,7 +150,7 @@ bench_modp (void *p) { struct ecc_ctx *ctx = (struct ecc_ctx *) p; mpn_copyi (ctx->rp, ctx->ap, 2*ctx->ecc->p.size); - ctx->ecc->modp (&ctx->ecc->p, ctx->rp); + ctx->ecc->p.mod (&ctx->ecc->p, ctx->rp); } static void @@ -158,7 +158,7 @@ bench_reduce (void *p) { struct ecc_ctx *ctx = (struct ecc_ctx *) p; mpn_copyi (ctx->rp, ctx->ap, 2*ctx->ecc->p.size); - ctx->ecc->reduce (&ctx->ecc->p, ctx->rp); + ctx->ecc->p.reduce (&ctx->ecc->p, ctx->rp); } static void @@ -166,7 +166,7 @@ bench_modq (void *p) { struct ecc_ctx *ctx = (struct ecc_ctx *) p; mpn_copyi (ctx->rp, ctx->ap, 2*ctx->ecc->p.size); - ctx->ecc->modq (&ctx->ecc->q, ctx->rp); + ctx->ecc->q.mod(&ctx->ecc->q, ctx->rp); } static void diff --git a/testsuite/ecc-mod-test.c b/testsuite/ecc-mod-test.c index 3139e139..a33aef62 100644 --- a/testsuite/ecc-mod-test.c +++ b/testsuite/ecc-mod-test.c @@ -42,7 +42,7 @@ test_curve (gmp_randstate_t rands, const struct ecc_curve *ecc) ref_mod (ref, a, ecc->p.m, ecc->p.size); mpn_copyi (m, a, 2*ecc->p.size); - ecc->modp (&ecc->p, m); + ecc->p.mod (&ecc->p, m); if (mpn_cmp (m, ecc->p.m, ecc->p.size) >= 0) mpn_sub_n (m, m, ecc->p.m, ecc->p.size); @@ -77,7 +77,7 @@ test_curve (gmp_randstate_t rands, const struct ecc_curve *ecc) ref_mod (ref, a, ecc->q.m, ecc->p.size); mpn_copyi (m, a, 2*ecc->p.size); - ecc->modq (&ecc->q, m); + ecc->q.mod (&ecc->q, m); if (mpn_cmp (m, ecc->q.m, ecc->p.size) >= 0) mpn_sub_n (m, m, ecc->q.m, ecc->p.size); diff --git a/testsuite/ecc-redc-test.c b/testsuite/ecc-redc-test.c index 96b58d46..98dc41e3 100644 --- a/testsuite/ecc-redc-test.c +++ b/testsuite/ecc-redc-test.c @@ -57,7 +57,7 @@ test_main (void) { const struct ecc_curve *ecc = ecc_curves[i]; unsigned j; - if (ecc->reduce == ecc->modp) + if (ecc->p.reduce == ecc->p.mod) continue; ASSERT (ecc->p.redc_size != 0); @@ -73,7 +73,7 @@ test_main (void) ref_redc (ref, a, ecc->p.m, ecc->p.size); mpn_copyi (m, a, 2*ecc->p.size); - ecc->reduce (&ecc->p, m); + ecc->p.reduce (&ecc->p, m); if (mpn_cmp (m, ecc->p.m, ecc->p.size) >= 0) mpn_sub_n (m, m, ecc->p.m, ecc->p.size);