summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNiels Möller <nisse@lysator.liu.se>2021-11-10 18:46:02 +0100
committerNiels Möller <nisse@lysator.liu.se>2021-11-10 18:46:02 +0100
commitb3abfac5f28a582c1cff54fbc6200b9aa3306b33 (patch)
tree3f5bdaa9064d2c88af7a8ef471ce58807dc560f4
parent2dbe065dfd5d79f077cc8019ecdb2f1482362210 (diff)
downloadnettle-ecc-sqrt.tar.gz
eccdata: Generate both redc and non-redc versions of ecc_sqrt_z.ecc-sqrt
-rw-r--r--ChangeLog6
-rw-r--r--eccdata.c20
2 files changed, 22 insertions, 4 deletions
diff --git a/ChangeLog b/ChangeLog
index 64ca9cbd..07a8fa36 100644
--- 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.
diff --git a/eccdata.c b/eccdata.c
index 1b4cb0b5..d99d92da 100644
--- a/eccdata.c
+++ b/eccdata.c
@@ -1111,6 +1111,17 @@ output_bignum (const char *name, const mpz_t x,
}
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,
unsigned size, unsigned bits_per_limb)
@@ -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