From b3abfac5f28a582c1cff54fbc6200b9aa3306b33 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Niels=20M=C3=B6ller?= Date: Wed, 10 Nov 2021 18:46:02 +0100 Subject: [PATCH] eccdata: Generate both redc and non-redc versions of ecc_sqrt_z. --- ChangeLog | 6 ++++++ eccdata.c | 20 ++++++++++++++++---- 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 + + * 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 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 @@ -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 \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 -- 2.47.2