]> git.ipfire.org Git - thirdparty/nettle.git/commitdiff
eccdata: Generate both redc and non-redc versions of ecc_sqrt_z. ecc-sqrt
authorNiels Möller <nisse@lysator.liu.se>
Wed, 10 Nov 2021 17:46:02 +0000 (18:46 +0100)
committerNiels Möller <nisse@lysator.liu.se>
Wed, 10 Nov 2021 17:46:02 +0000 (18:46 +0100)
ChangeLog
eccdata.c

index 64ca9cbd4ca3435019013e4548bfe34ce9902f82..07a8fa36c7ce779d9b70800659e37aefd2927fe4 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2021-11-10  Niels Möller  <nisse@lysator.liu.se>
+
+       * eccdata.c (output_bignum_redc): New function.
+       (output_curve): Generate both redc and non-redc versions of
+       ecc_sqrt_z. Fixes secp224r1 sqrt, in configs using redc.
+
 2021-11-08  Niels Möller  <nisse@lysator.liu.se>
 
        Square root functions, based on patch by Wim Lewis.
index 1b4cb0b5083e127aa0ebb09d2d15e66b191bce53..d99d92da592f8534dbf232727ff57442f2170413 100644 (file)
--- a/eccdata.c
+++ b/eccdata.c
@@ -1110,6 +1110,17 @@ output_bignum (const char *name, const mpz_t x,
   printf("\n};\n");
 }
 
+static void
+output_bignum_redc (const char *name, const mpz_t x, const mpz_t p,
+                   unsigned size, unsigned bits_per_limb)
+{
+  mpz_t t;
+  mpz_init (t);
+  mpz_mul_2exp (t, x, size * bits_per_limb);
+  mpz_mod (t, t, p);
+  output_bignum (name, t, size, bits_per_limb);
+}
+
 static void
 output_point (const struct ecc_curve *ecc,
              const struct ecc_point *p, int use_redc,
@@ -1167,8 +1178,10 @@ output_curve (const struct ecc_curve *ecc, unsigned bits_per_limb)
   unsigned bits, e;
   int redc_limbs;
   mpz_t t;
+  mpz_t z;
 
   mpz_init (t);
+  mpz_init (z);
 
   printf ("/* For NULL. */\n#include <stddef.h>\n");
 
@@ -1304,10 +1317,8 @@ output_curve (const struct ecc_curve *ecc, unsigned bits_per_limb)
       /* p-1 = 2^e s, s odd, t = (s-1)/2*/
       unsigned g, i;
       mpz_t s;
-      mpz_t z;
 
       mpz_init (s);
-      mpz_init (z);
 
       mpz_sub_ui (s, ecc->p, 1);
       e = mpz_scan1 (s, 0);
@@ -1334,12 +1345,10 @@ output_curve (const struct ecc_curve *ecc, unsigned bits_per_limb)
        }
       mpz_add_ui (t, t, 1);
       assert (mpz_cmp (t, ecc->p) == 0);
-      output_bignum ("ecc_sqrt_z", z, limb_size, bits_per_limb);
 
       mpz_fdiv_q_2exp (t, s, 1);
 
       mpz_clear (s);
-      mpz_clear (z);
     }
   printf ("#define ECC_SQRT_E %u\n", e);
   printf ("#define ECC_SQRT_T_BITS %u\n",
@@ -1348,6 +1357,7 @@ output_curve (const struct ecc_curve *ecc, unsigned bits_per_limb)
 
   printf ("#if USE_REDC\n");
   printf ("#define ecc_unit ecc_Bmodp\n");
+  output_bignum_redc ("ecc_sqrt_z", z, ecc->p, limb_size, bits_per_limb);
 
   printf ("static const mp_limb_t ecc_table[%lu] = {",
         (unsigned long) (2*ecc->table_size * limb_size));
@@ -1360,6 +1370,7 @@ output_curve (const struct ecc_curve *ecc, unsigned bits_per_limb)
 
   mpz_set_ui (t, 1);
   output_bignum ("ecc_unit", t, limb_size, bits_per_limb);
+  output_bignum ("ecc_sqrt_z", z, limb_size, bits_per_limb);
   
   printf ("static const mp_limb_t ecc_table[%lu] = {",
         (unsigned long) (2*ecc->table_size * limb_size));
@@ -1370,6 +1381,7 @@ output_curve (const struct ecc_curve *ecc, unsigned bits_per_limb)
   printf ("#endif\n");
   
   mpz_clear (t);
+  mpz_clear (z);
 }
 
 int