Commit 0b511c91 authored by Niels Möller's avatar Niels Möller

Use struct ecc_modulo for all mod and redc functions.

parent a78c9459
2014-09-22 Niels Möller <nisse@lysator.liu.se> 2014-09-22 Niels Möller <nisse@lysator.liu.se>
* ecc-generic-modp.c (ecc_generic_modp): Deleted file and
function. We no longer need a wrapper around ecc_mod.
* ecc-generic-modq.c (ecc_generic_modq): Likewise deleted.
* Makefile.in (hogweed_SOURCES): Removed ecc-generic-modp.c and
ecc-generic-modq.c.
* ecc-internal.h (typedef ecc_mod_func): Take a const struct
ecc_modulo * argument, not const struct ecc_curve *. Updated all
implementations and all callers.
* ecc-mod.c (ecc_mod): Use struct ecc_modulo to specify the
modulo. Drop input size argument, always reduce from 2*size to
size.
* ecc-internal.h (struct ecc_modulo): New struct, collecting * ecc-internal.h (struct ecc_modulo): New struct, collecting
constants needed for modulo arithmetic. constants needed for modulo arithmetic.
(struct ecc_curve): Use struct ecc_modulo for p and q arithmetic. (struct ecc_curve): Use struct ecc_modulo for p and q arithmetic.
......
...@@ -162,7 +162,7 @@ hogweed_SOURCES = sexp.c sexp-format.c \ ...@@ -162,7 +162,7 @@ hogweed_SOURCES = sexp.c sexp-format.c \
der-iterator.c der2rsa.c der2dsa.c \ der-iterator.c der2rsa.c der2dsa.c \
sec-add-1.c sec-sub-1.c sec-modinv.c sec-tabselect.c \ sec-add-1.c sec-sub-1.c sec-modinv.c sec-tabselect.c \
gmp-glue.c cnd-copy.c \ gmp-glue.c cnd-copy.c \
ecc-mod.c ecc-generic-modp.c ecc-generic-modq.c \ ecc-mod.c \
ecc-modp.c ecc-modq.c ecc-pp1-redc.c ecc-pm1-redc.c \ ecc-modp.c ecc-modq.c ecc-pp1-redc.c ecc-pm1-redc.c \
ecc-192.c ecc-224.c ecc-256.c ecc-384.c ecc-521.c \ ecc-192.c ecc-224.c ecc-256.c ecc-384.c ecc-521.c \
ecc-25519.c \ ecc-25519.c \
......
...@@ -52,14 +52,14 @@ ...@@ -52,14 +52,14 @@
#define ecc_192_modp nettle_ecc_192_modp #define ecc_192_modp nettle_ecc_192_modp
void void
ecc_192_modp (const struct ecc_curve *ecc, mp_limb_t *rp); ecc_192_modp (const struct ecc_modulo *m, mp_limb_t *rp);
/* Use that p = 2^{192} - 2^64 - 1, to eliminate 128 bits at a time. */ /* Use that p = 2^{192} - 2^64 - 1, to eliminate 128 bits at a time. */
#elif GMP_NUMB_BITS == 32 #elif GMP_NUMB_BITS == 32
/* p is 6 limbs, p = B^6 - B^2 - 1 */ /* p is 6 limbs, p = B^6 - B^2 - 1 */
static void static void
ecc_192_modp (const struct ecc_curve *ecc UNUSED, mp_limb_t *rp) ecc_192_modp (const struct ecc_modulo *m UNUSED, mp_limb_t *rp)
{ {
mp_limb_t cy; mp_limb_t cy;
...@@ -84,7 +84,7 @@ ecc_192_modp (const struct ecc_curve *ecc UNUSED, mp_limb_t *rp) ...@@ -84,7 +84,7 @@ ecc_192_modp (const struct ecc_curve *ecc UNUSED, mp_limb_t *rp)
#elif GMP_NUMB_BITS == 64 #elif GMP_NUMB_BITS == 64
/* p is 3 limbs, p = B^3 - B - 1 */ /* p is 3 limbs, p = B^3 - B - 1 */
static void static void
ecc_192_modp (const struct ecc_curve *ecc UNUSED, mp_limb_t *rp) ecc_192_modp (const struct ecc_modulo *m UNUSED, mp_limb_t *rp)
{ {
mp_limb_t cy; mp_limb_t cy;
...@@ -107,7 +107,7 @@ ecc_192_modp (const struct ecc_curve *ecc UNUSED, mp_limb_t *rp) ...@@ -107,7 +107,7 @@ ecc_192_modp (const struct ecc_curve *ecc UNUSED, mp_limb_t *rp)
} }
#else #else
#define ecc_192_modp ecc_generic_modp #define ecc_192_modp ecc_mod
#endif #endif
const struct ecc_curve nettle_secp_192r1 = const struct ecc_curve nettle_secp_192r1 =
...@@ -145,7 +145,7 @@ const struct ecc_curve nettle_secp_192r1 = ...@@ -145,7 +145,7 @@ const struct ecc_curve nettle_secp_192r1 =
ecc_192_modp, ecc_192_modp,
ECC_REDC_SIZE >= 1 ? ecc_pp1_redc : NULL, ECC_REDC_SIZE >= 1 ? ecc_pp1_redc : NULL,
ecc_192_modp, ecc_192_modp,
ecc_generic_modq, ecc_mod,
ecc_add_jjj, ecc_add_jjj,
ecc_mul_a, ecc_mul_a,
......
...@@ -45,11 +45,11 @@ ...@@ -45,11 +45,11 @@
#define USE_REDC 0 #define USE_REDC 0
#define ecc_224_modp nettle_ecc_224_modp #define ecc_224_modp nettle_ecc_224_modp
void void
ecc_224_modp (const struct ecc_curve *ecc, mp_limb_t *rp); ecc_224_modp (const struct ecc_modulo *m, mp_limb_t *rp);
#else #else
#define USE_REDC (ECC_REDC_SIZE != 0) #define USE_REDC (ECC_REDC_SIZE != 0)
#define ecc_224_modp ecc_generic_modp #define ecc_224_modp ecc_mod
#endif #endif
#include "ecc-224.h" #include "ecc-224.h"
...@@ -97,7 +97,7 @@ const struct ecc_curve nettle_secp_224r1 = ...@@ -97,7 +97,7 @@ const struct ecc_curve nettle_secp_224r1 =
ecc_224_modp, ecc_224_modp,
ecc_224_redc, ecc_224_redc,
USE_REDC ? ecc_224_redc : ecc_224_modp, USE_REDC ? ecc_224_redc : ecc_224_modp,
ecc_generic_modq, ecc_mod,
ecc_add_jjj, ecc_add_jjj,
ecc_mul_a, ecc_mul_a,
......
...@@ -58,7 +58,7 @@ ecc_25519_modp (const struct ecc_curve *ecc, mp_limb_t *rp); ...@@ -58,7 +58,7 @@ ecc_25519_modp (const struct ecc_curve *ecc, mp_limb_t *rp);
#endif #endif
static void static void
ecc_25519_modp(const struct ecc_curve *ecc UNUSED, mp_limb_t *rp) ecc_25519_modp(const struct ecc_modulo *m UNUSED, mp_limb_t *rp)
{ {
mp_limb_t hi, cy; mp_limb_t hi, cy;
...@@ -78,7 +78,7 @@ ecc_25519_modp(const struct ecc_curve *ecc UNUSED, mp_limb_t *rp) ...@@ -78,7 +78,7 @@ ecc_25519_modp(const struct ecc_curve *ecc UNUSED, mp_limb_t *rp)
#endif #endif
static void static void
ecc_25519_modq (const struct ecc_curve *ecc, mp_limb_t *rp) ecc_25519_modq (const struct ecc_modulo *q, mp_limb_t *rp)
{ {
mp_size_t n; mp_size_t n;
mp_limb_t cy; mp_limb_t cy;
...@@ -87,17 +87,17 @@ ecc_25519_modq (const struct ecc_curve *ecc, mp_limb_t *rp) ...@@ -87,17 +87,17 @@ ecc_25519_modq (const struct ecc_curve *ecc, mp_limb_t *rp)
for (n = ECC_LIMB_SIZE; n-- > 0;) for (n = ECC_LIMB_SIZE; n-- > 0;)
{ {
cy = mpn_submul_1 (rp + n, cy = mpn_submul_1 (rp + n,
ecc->q.B_shifted, ECC_LIMB_SIZE, q->B_shifted, ECC_LIMB_SIZE,
rp[n + ECC_LIMB_SIZE]); rp[n + ECC_LIMB_SIZE]);
/* Top limb of mBmodq_shifted is zero, so we get cy == 0 or 1 */ /* Top limb of mBmodq_shifted is zero, so we get cy == 0 or 1 */
assert (cy < 2); assert (cy < 2);
cnd_add_n (cy, rp+n, ecc->q.m, ECC_LIMB_SIZE); cnd_add_n (cy, rp+n, q->m, ECC_LIMB_SIZE);
} }
cy = mpn_submul_1 (rp, ecc->q.m, ECC_LIMB_SIZE, cy = mpn_submul_1 (rp, q->m, ECC_LIMB_SIZE,
rp[ECC_LIMB_SIZE-1] >> (GMP_NUMB_BITS - QHIGH_BITS)); rp[ECC_LIMB_SIZE-1] >> (GMP_NUMB_BITS - QHIGH_BITS));
assert (cy < 2); assert (cy < 2);
cnd_add_n (cy, rp, ecc->q.m, ECC_LIMB_SIZE); cnd_add_n (cy, rp, q->m, ECC_LIMB_SIZE);
} }
/* Needs 2*ecc->size limbs at rp, and 2*ecc->size additional limbs of /* Needs 2*ecc->size limbs at rp, and 2*ecc->size additional limbs of
......
...@@ -65,22 +65,22 @@ ecc_256_redc (const struct ecc_curve *ecc, mp_limb_t *rp); ...@@ -65,22 +65,22 @@ ecc_256_redc (const struct ecc_curve *ecc, mp_limb_t *rp);
#endif /* !HAVE_NATIVE_ecc_256_redc */ #endif /* !HAVE_NATIVE_ecc_256_redc */
#if ECC_BMODP_SIZE < ECC_LIMB_SIZE #if ECC_BMODP_SIZE < ECC_LIMB_SIZE
#define ecc_256_modp ecc_generic_modp #define ecc_256_modp ecc_mod
#define ecc_256_modq ecc_generic_modq #define ecc_256_modq ecc_mod
#elif GMP_NUMB_BITS == 64 #elif GMP_NUMB_BITS == 64
static void static void
ecc_256_modp (const struct ecc_curve *ecc, mp_limb_t *rp) ecc_256_modp (const struct ecc_modulo *p, mp_limb_t *rp)
{ {
mp_limb_t u1, u0; mp_limb_t u1, u0;
mp_size_t n; mp_size_t n;
n = 2*ecc->p.size; n = 2*p->size;
u1 = rp[--n]; u1 = rp[--n];
u0 = rp[n-1]; u0 = rp[n-1];
/* This is not particularly fast, but should work well with assembly implementation. */ /* This is not particularly fast, but should work well with assembly implementation. */
for (; n >= ecc->p.size; n--) for (; n >= p->size; n--)
{ {
mp_limb_t q2, q1, q0, t, cy; mp_limb_t q2, q1, q0, t, cy;
...@@ -115,8 +115,8 @@ ecc_256_modp (const struct ecc_curve *ecc, mp_limb_t *rp) ...@@ -115,8 +115,8 @@ ecc_256_modp (const struct ecc_curve *ecc, mp_limb_t *rp)
/* We multiply by two low limbs of p, 2^96 - 1, so we could use /* We multiply by two low limbs of p, 2^96 - 1, so we could use
shifts rather than mul. */ shifts rather than mul. */
t = mpn_submul_1 (rp + n - 4, ecc->p.m, 2, q1); t = mpn_submul_1 (rp + n - 4, p->m, 2, q1);
t += cnd_sub_n (q2, rp + n - 3, ecc->p.m, 1); t += cnd_sub_n (q2, rp + n - 3, p->m, 1);
t += (-q2) & 0xffffffff; t += (-q2) & 0xffffffff;
u0 = rp[n-2]; u0 = rp[n-2];
...@@ -124,7 +124,7 @@ ecc_256_modp (const struct ecc_curve *ecc, mp_limb_t *rp) ...@@ -124,7 +124,7 @@ ecc_256_modp (const struct ecc_curve *ecc, mp_limb_t *rp)
u0 -= t; u0 -= t;
t = (u1 < cy); t = (u1 < cy);
u1 -= cy; u1 -= cy;
u1 += cnd_add_n (t, rp + n - 4, ecc->p.m, 3); u1 += cnd_add_n (t, rp + n - 4, p->m, 3);
u1 -= (-t) & 0xffffffff; u1 -= (-t) & 0xffffffff;
} }
rp[2] = u0; rp[2] = u0;
...@@ -132,17 +132,17 @@ ecc_256_modp (const struct ecc_curve *ecc, mp_limb_t *rp) ...@@ -132,17 +132,17 @@ ecc_256_modp (const struct ecc_curve *ecc, mp_limb_t *rp)
} }
static void static void
ecc_256_modq (const struct ecc_curve *ecc, mp_limb_t *rp) ecc_256_modq (const struct ecc_modulo *q, mp_limb_t *rp)
{ {
mp_limb_t u2, u1, u0; mp_limb_t u2, u1, u0;
mp_size_t n; mp_size_t n;
n = 2*ecc->q.size; n = 2*q->size;
u2 = rp[--n]; u2 = rp[--n];
u1 = rp[n-1]; u1 = rp[n-1];
/* This is not particularly fast, but should work well with assembly implementation. */ /* This is not particularly fast, but should work well with assembly implementation. */
for (; n >= ecc->q.size; n--) for (; n >= q->size; n--)
{ {
mp_limb_t q2, q1, q0, t, c1, c0; mp_limb_t q2, q1, q0, t, c1, c0;
...@@ -196,9 +196,9 @@ ecc_256_modq (const struct ecc_curve *ecc, mp_limb_t *rp) ...@@ -196,9 +196,9 @@ ecc_256_modq (const struct ecc_curve *ecc, mp_limb_t *rp)
assert (q2 < 2); assert (q2 < 2);
c0 = cnd_sub_n (q2, rp + n - 3, ecc->q.m, 1); c0 = cnd_sub_n (q2, rp + n - 3, q->m, 1);
c0 += (-q2) & ecc->q.m[1]; c0 += (-q2) & q->m[1];
t = mpn_submul_1 (rp + n - 4, ecc->q.m, 2, q1); t = mpn_submul_1 (rp + n - 4, q->m, 2, q1);
c0 += t; c0 += t;
c1 = c0 < t; c1 = c0 < t;
...@@ -213,7 +213,7 @@ ecc_256_modq (const struct ecc_curve *ecc, mp_limb_t *rp) ...@@ -213,7 +213,7 @@ ecc_256_modq (const struct ecc_curve *ecc, mp_limb_t *rp)
u1 += t; u1 += t;
u2 += (t<<32) + (u0 < t); u2 += (t<<32) + (u0 < t);
t = cnd_add_n (t, rp + n - 4, ecc->q.m, 2); t = cnd_add_n (t, rp + n - 4, q->m, 2);
u1 += t; u1 += t;
u2 += (u1 < t); u2 += (u1 < t);
} }
......
...@@ -62,7 +62,7 @@ ecc_384_modp (const struct ecc_curve *ecc, mp_limb_t *rp); ...@@ -62,7 +62,7 @@ ecc_384_modp (const struct ecc_curve *ecc, mp_limb_t *rp);
almost 8 at a time. Do only 7, to avoid additional carry almost 8 at a time. Do only 7, to avoid additional carry
propagation, followed by 5. */ propagation, followed by 5. */
static void static void
ecc_384_modp (const struct ecc_curve *ecc, mp_limb_t *rp) ecc_384_modp (const struct ecc_modulo *p, mp_limb_t *rp)
{ {
mp_limb_t cy, bw; mp_limb_t cy, bw;
...@@ -99,14 +99,14 @@ ecc_384_modp (const struct ecc_curve *ecc, mp_limb_t *rp) ...@@ -99,14 +99,14 @@ ecc_384_modp (const struct ecc_curve *ecc, mp_limb_t *rp)
assert (cy >= bw); assert (cy >= bw);
cy -= bw; cy -= bw;
assert (cy <= 1); assert (cy <= 1);
cy = cnd_add_n (cy, rp, ecc->Bmodp, ECC_LIMB_SIZE); cy = cnd_add_n (cy, rp, p->B, ECC_LIMB_SIZE);
assert (cy == 0); assert (cy == 0);
} }
#elif GMP_NUMB_BITS == 64 #elif GMP_NUMB_BITS == 64
/* p is 6 limbs, and B^6 - p = B^2 + 2^32 (B - 1) + 1. Eliminate 3 /* p is 6 limbs, and B^6 - p = B^2 + 2^32 (B - 1) + 1. Eliminate 3
(almost 4) limbs at a time. */ (almost 4) limbs at a time. */
static void static void
ecc_384_modp (const struct ecc_curve *ecc, mp_limb_t *rp) ecc_384_modp (const struct ecc_modulo *p, mp_limb_t *rp)
{ {
mp_limb_t tp[6]; mp_limb_t tp[6];
mp_limb_t cy; mp_limb_t cy;
...@@ -140,11 +140,11 @@ ecc_384_modp (const struct ecc_curve *ecc, mp_limb_t *rp) ...@@ -140,11 +140,11 @@ ecc_384_modp (const struct ecc_curve *ecc, mp_limb_t *rp)
cy = sec_add_1 (rp + 5, rp + 5, 1, cy); cy = sec_add_1 (rp + 5, rp + 5, 1, cy);
assert (cy <= 1); assert (cy <= 1);
cy = cnd_add_n (cy, rp, ecc->p.B, ECC_LIMB_SIZE); cy = cnd_add_n (cy, rp, p->B, ECC_LIMB_SIZE);
assert (cy == 0); assert (cy == 0);
} }
#else #else
#define ecc_384_modp ecc_generic_modp #define ecc_384_modp ecc_mod
#endif #endif
const struct ecc_curve nettle_secp_384r1 = const struct ecc_curve nettle_secp_384r1 =
...@@ -182,7 +182,7 @@ const struct ecc_curve nettle_secp_384r1 = ...@@ -182,7 +182,7 @@ const struct ecc_curve nettle_secp_384r1 =
ecc_384_modp, ecc_384_modp,
ECC_REDC_SIZE > 0 ? ecc_pp1_redc : NULL, ECC_REDC_SIZE > 0 ? ecc_pp1_redc : NULL,
ecc_384_modp, ecc_384_modp,
ecc_generic_modq, ecc_mod,
ecc_add_jjj, ecc_add_jjj,
ecc_mul_a, ecc_mul_a,
......
...@@ -57,7 +57,7 @@ ecc_521_modp (const struct ecc_curve *ecc, mp_limb_t *rp); ...@@ -57,7 +57,7 @@ ecc_521_modp (const struct ecc_curve *ecc, mp_limb_t *rp);
/* Result may be *slightly* larger than 2^521 */ /* Result may be *slightly* larger than 2^521 */
static void static void
ecc_521_modp (const struct ecc_curve *ecc UNUSED, mp_limb_t *rp) ecc_521_modp (const struct ecc_modulo *m UNUSED, mp_limb_t *rp)
{ {
/* FIXME: Should use mpn_addlsh_n_ip1 */ /* FIXME: Should use mpn_addlsh_n_ip1 */
mp_limb_t hi; mp_limb_t hi;
...@@ -110,7 +110,7 @@ const struct ecc_curve nettle_secp_521r1 = ...@@ -110,7 +110,7 @@ const struct ecc_curve nettle_secp_521r1 =
ecc_521_modp, ecc_521_modp,
ECC_REDC_SIZE > 0 ? ecc_pp1_redc : NULL, ECC_REDC_SIZE > 0 ? ecc_pp1_redc : NULL,
ecc_521_modp, ecc_521_modp,
ecc_generic_modq, ecc_mod,
ecc_add_jjj, ecc_add_jjj,
ecc_mul_a, ecc_mul_a,
......
...@@ -47,10 +47,10 @@ ecc_a_to_j (const struct ecc_curve *ecc, ...@@ -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_copyd (r + ecc->p.size, p, 2*ecc->p.size);
mpn_zero (r, ecc->p.size); mpn_zero (r, ecc->p.size);
ecc->modp (ecc, r); ecc->modp (&ecc->p, r);
mpn_zero (r + ecc->p.size, ecc->p.size); mpn_zero (r + ecc->p.size, ecc->p.size);
ecc->modp (ecc, r + ecc->p.size); ecc->modp (&ecc->p, r + ecc->p.size);
} }
else if (r != p) else if (r != p)
mpn_copyi (r, p, 2*ecc->p.size); mpn_copyi (r, p, 2*ecc->p.size);
......
/* ecc-generic-modp.c
Copyright (C) 2013 Niels Möller
This file is part of GNU Nettle.
GNU Nettle is free software: you can redistribute it and/or
modify it under the terms of either:
* the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your
option) any later version.
or
* the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your
option) any later version.
or both in parallel, as here.
GNU Nettle is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received copies of the GNU General Public License and
the GNU Lesser General Public License along with this program. If
not, see http://www.gnu.org/licenses/.
*/
/* Development of Nettle's ECC support was funded by the .SE Internet Fund. */
#if HAVE_CONFIG_H
# include "config.h"
#endif
#include <assert.h>
#include "ecc-internal.h"
void
ecc_generic_modp (const struct ecc_curve *ecc, mp_limb_t *rp)
{
assert (ecc->p.B_size < ecc->p.size);
ecc_mod (&ecc->p, rp, 2*ecc->p.size);
}
/* ecc-generic-modq.c
Copyright (C) 2013 Niels Möller
This file is part of GNU Nettle.
GNU Nettle is free software: you can redistribute it and/or
modify it under the terms of either:
* the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your
option) any later version.
or
* the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your
option) any later version.
or both in parallel, as here.
GNU Nettle is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received copies of the GNU General Public License and
the GNU Lesser General Public License along with this program. If
not, see http://www.gnu.org/licenses/.
*/
/* Development of Nettle's ECC support was funded by the .SE Internet Fund. */
#if HAVE_CONFIG_H
# include "config.h"
#endif
#include <assert.h>
#include "ecc-internal.h"
void
ecc_generic_modq (const struct ecc_curve *ecc, mp_limb_t *rp)
{
assert (ecc->q.B_size < ecc->q.size);
ecc_mod (&ecc->q, rp, 2*ecc->q.size);
}
...@@ -40,10 +40,8 @@ ...@@ -40,10 +40,8 @@
#include "gmp-glue.h" #include "gmp-glue.h"
/* Name mangling */ /* Name mangling */
#define ecc_generic_modp _nettle_ecc_generic_modp
#define ecc_pp1_redc _nettle_ecc_pp1_redc #define ecc_pp1_redc _nettle_ecc_pp1_redc
#define ecc_pm1_redc _nettle_ecc_pm1_redc #define ecc_pm1_redc _nettle_ecc_pm1_redc
#define ecc_generic_modq _nettle_ecc_generic_modq
#define ecc_modp_add _nettle_ecc_modp_add #define ecc_modp_add _nettle_ecc_modp_add
#define ecc_modp_sub _nettle_ecc_modp_sub #define ecc_modp_sub _nettle_ecc_modp_sub
#define ecc_modp_mul_1 _nettle_ecc_modp_mul_1 #define ecc_modp_mul_1 _nettle_ecc_modp_mul_1
...@@ -97,7 +95,7 @@ struct ecc_modulo ...@@ -97,7 +95,7 @@ struct ecc_modulo
/* Reduces from 2*ecc->size to ecc->size. */ /* Reduces from 2*ecc->size to ecc->size. */
/* Required to return a result < 2q. This property is inherited by /* Required to return a result < 2q. This property is inherited by
modp_mul and modp_sqr. */ modp_mul and modp_sqr. */
typedef void ecc_mod_func (const struct ecc_curve *ecc, mp_limb_t *rp); typedef void ecc_mod_func (const struct ecc_modulo *m, mp_limb_t *rp);
typedef void ecc_add_func (const struct ecc_curve *ecc, typedef void ecc_add_func (const struct ecc_curve *ecc,
mp_limb_t *r, mp_limb_t *r,
...@@ -180,11 +178,9 @@ struct ecc_curve ...@@ -180,11 +178,9 @@ struct ecc_curve
}; };
/* In-place reduction. */ /* In-place reduction. */
ecc_mod_func ecc_generic_modp; ecc_mod_func ecc_mod;
ecc_mod_func ecc_pp1_redc; ecc_mod_func ecc_pp1_redc;
ecc_mod_func ecc_pm1_redc; ecc_mod_func ecc_pm1_redc;
ecc_mod_func ecc_generic_modq;
void void
ecc_modp_add (const struct ecc_curve *ecc, mp_limb_t *rp, ecc_modp_add (const struct ecc_curve *ecc, mp_limb_t *rp,
...@@ -233,9 +229,6 @@ void ...@@ -233,9 +229,6 @@ void
ecc_modq_random (const struct ecc_curve *ecc, mp_limb_t *xp, ecc_modq_random (const struct ecc_curve *ecc, mp_limb_t *xp,
void *ctx, nettle_random_func *random, mp_limb_t *scratch); void *ctx, nettle_random_func *random, mp_limb_t *scratch);
void
ecc_mod (const struct ecc_modulo *m, mp_limb_t *rp, mp_size_t rn);
void void
ecc_hash (const struct ecc_curve *ecc, ecc_hash (const struct ecc_curve *ecc,
mp_limb_t *hp, mp_limb_t *hp,
......
...@@ -73,16 +73,16 @@ ecc_j_to_a (const struct ecc_curve *ecc, ...@@ -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_copyi (up, p + 2*ecc->p.size, ecc->p.size);
mpn_zero (up + ecc->p.size, ecc->p.size); mpn_zero (up + ecc->p.size, ecc->p.size);
ecc->redc (ecc, up); ecc->redc (&ecc->p, up);
mpn_zero (up + ecc->p.size, ecc->p.size); mpn_zero (up + ecc->p.size, ecc->p.size);
ecc->redc (ecc, up); ecc->redc (&ecc->p, up);
ecc_modp_inv (ecc, izp, up, up + ecc->p.size); ecc_modp_inv (ecc, izp, up, up + ecc->p.size);
/* Divide this common factor by B */ /* Divide this common factor by B */
mpn_copyi (izBp, izp, ecc->p.size); mpn_copyi (izBp, izp, ecc->p.size);
mpn_zero (izBp + ecc->p.size, ecc->p.size); mpn_zero (izBp + ecc->p.size, ecc->p.size);
ecc->redc (ecc, izBp); ecc->redc (&ecc->p, izBp);
ecc_modp_mul (ecc, iz2p, izp, izBp); ecc_modp_mul (ecc, iz2p, izp, izBp);
} }
......
...@@ -39,14 +39,15 @@ ...@@ -39,14 +39,15 @@
#include "ecc-internal.h" #include "ecc-internal.h"
/* Computes r mod m. */ /* Computes r mod m, input 2*m->size, output m->size. */
void void
ecc_mod (const struct ecc_modulo *m, mp_limb_t *rp, mp_size_t rn) ecc_mod (const struct ecc_modulo *m, mp_limb_t *rp)
{ {
mp_limb_t hi; mp_limb_t hi;
mp_size_t mn = m->size; mp_size_t mn = m->size;
mp_size_t bn = m->B_size; mp_size_t bn = m->B_size;
mp_size_t sn = mn - bn; mp_size_t sn = mn - bn;
mp_size_t rn = 2*mn;
mp_size_t i; mp_size_t i;
unsigned shift; unsigned shift;
......
...@@ -115,7 +115,7 @@ ecc_modp_mul (const struct ecc_curve *ecc, mp_limb_t *rp, ...@@ -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) const mp_limb_t *ap, const mp_limb_t *bp)
{ {
mpn_mul_n (rp, ap, bp, ecc->p.size); mpn_mul_n (rp, ap, bp, ecc->p.size);
ecc->reduce (ecc, rp); ecc->reduce (&ecc->p, rp);
} }
void void
...@@ -123,7 +123,7 @@ ecc_modp_sqr (const struct ecc_curve *ecc, mp_limb_t *rp, ...@@ -123,7 +123,7 @@ ecc_modp_sqr (const struct ecc_curve *ecc, mp_limb_t *rp,
const mp_limb_t *ap) const mp_limb_t *ap)
{ {
mpn_sqr (rp, ap, ecc->p.size); mpn_sqr (rp, ap, ecc->p.size);
ecc->reduce (ecc, rp); ecc->reduce (&ecc->p, rp);
} }
void void
......
...@@ -57,7 +57,7 @@ ecc_modq_mul (const struct ecc_curve *ecc, mp_limb_t *rp, ...@@ -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) const mp_limb_t *ap, const mp_limb_t *bp)
{ {