]> git.ipfire.org Git - thirdparty/nettle.git/commitdiff
Moved mod and reduce function pointers to struct ecc_modulo.
authorNiels Möller <nisse@lysator.liu.se>
Mon, 22 Sep 2014 12:41:33 +0000 (14:41 +0200)
committerNiels Möller <nisse@lysator.liu.se>
Mon, 22 Sep 2014 12:41:33 +0000 (14:41 +0200)
15 files changed:
ChangeLog
ecc-192.c
ecc-224.c
ecc-25519.c
ecc-256.c
ecc-384.c
ecc-521.c
ecc-a-to-j.c
ecc-internal.h
ecc-j-to-a.c
ecc-modp.c
ecc-modq.c
examples/ecc-benchmark.c
testsuite/ecc-mod-test.c
testsuite/ecc-redc-test.c

index 8d1cd60e13f45ce7992593967efee46b0c6982e5..a62f0108ee39b5e34c6fc1308dad551cbce2d93b 100644 (file)
--- 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.
index 347cdec13c186523dadaf1052d31da082ed8585e..683363352087a17425d9dc8f9daaf88b1663c3ac 100644 (file)
--- 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,
index a5bed64ac513888f8ae72190f27f2626c5ea80ee..f0f211ceb081378d4f28fe09959cda572f65fadf 100644 (file)
--- 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,
index e973fc45d33d830b8b4bce7fc8cd13b5e355847e..0c02d7e7a53bd54c5617cf70437fdc870bafd916 100644 (file)
@@ -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,
index a2a03fb5fd08515e40ae07a63946bbb468bdd5aa..843ee76961a012160fe5e35ae9e44e21015c9f35 100644 (file)
--- 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,
index 2493d36ea13f1e5978ec2e2ac3f3be1f5229f78e..ab48f348191d8e8c09137827ea5427b829798a36 100644 (file)
--- 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,
index 3876f6edda587600fa170a81cd65a2db7384edc1..4e1f1ab3b186ad5ebc1f1c3fe0e383a960a2f702 100644 (file)
--- 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,
index ea0fa68ca57a84b2a475d43981982bdf29f58263..9fb0d2b80c41635e10ae480ea5cf511101610590 100644 (file)
@@ -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);
index 1088f25ae193f9b0332ca7a47231a2e88f6c74b7..f8be2647dd746b0fee26c36b41fdcc0898882fdf 100644 (file)
 /* 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;
index c862d2c41629708e9f585a5655747b750c770fd9..91b97e701256fae6d3ec7912aa465efbc8af5da9 100644 (file)
@@ -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);
     }
index 6f305aeab05d8a3ee41e5392c9d8901de04ad569..f48f7840830d31f25389c966d6dc36420e2abda5 100644 (file)
@@ -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
index 54533bdedde76a82f6000a6a96aee640ffda32c1..48bb2ba54419a796c69db53d16fb0d7373c1a203 100644 (file)
@@ -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
index e2b33c4af6e9e9060029eacd37c7e853e6a8561b..f2d5c5727e42d3b682b57cc1d842afc725445c9d 100644 (file)
@@ -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
index 3139e139b466d158d533cfa995c7e89d5b88a1e7..a33aef6222d395eccd4bb40cdd7e89dce7d4e1f1 100644 (file)
@@ -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);
 
index 96b58d46a8b99f478c07301202fc4f86352ecdca..98dc41e384829893f25100c8428eb3ab8a71b191 100644 (file)
@@ -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);