* 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.
ecc_Bmodp,
ecc_Bmodp_shifted,
ecc_redc_ppm1,
+ ecc_192_modp,
+ ecc_192_modp,
},
{
192,
ecc_Bmodq,
ecc_Bmodq_shifted,
NULL,
+ ecc_mod,
+ ecc_mod,
},
USE_REDC,
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,
ecc_Bmodp,
ecc_Bmodp_shifted,
ecc_redc_ppm1,
+ ecc_224_modp,
+ USE_REDC ? ecc_224_redc : ecc_224_modp,
},
{
224,
ecc_Bmodq,
ecc_Bmodq_shifted,
NULL,
+ ecc_mod,
+ ecc_mod,
},
USE_REDC,
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,
ecc_Bmodp,
ecc_Bmodp_shifted,
NULL,
+ ecc_25519_modp,
+ ecc_25519_modp,
},
{
253,
ecc_Bmodq,
ecc_mBmodq_shifted, /* Use q - 2^{252} instead. */
NULL,
+ ecc_25519_modq,
+ ecc_25519_modq,
},
0, /* No redc */
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,
ecc_Bmodp,
ecc_Bmodp_shifted,
ecc_redc_ppm1,
+ ecc_256_modp,
+ USE_REDC ? ecc_256_redc : ecc_256_modp,
},
{
256,
ecc_Bmodq,
ecc_Bmodq_shifted,
NULL,
+ ecc_256_modq,
+ ecc_256_modq,
},
USE_REDC,
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,
ecc_Bmodp,
ecc_Bmodp_shifted,
ecc_redc_ppm1,
+ ecc_384_modp,
+ ecc_384_modp,
},
{
384,
ecc_Bmodq,
ecc_Bmodq_shifted,
NULL,
+ ecc_mod,
+ ecc_mod,
},
USE_REDC,
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,
ecc_Bmodp,
ecc_Bmodp_shifted,
ecc_redc_ppm1,
+ ecc_521_modp,
+ ecc_521_modp,
},
{
521,
ecc_Bmodq,
ecc_Bmodq_shifted,
NULL,
+ ecc_mod,
+ ecc_mod,
},
USE_REDC,
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,
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);
/* 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
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)
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;
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);
}
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
const mp_limb_t *ap)
{
mpn_sqr (rp, ap, ecc->p.size);
- ecc->reduce (&ecc->p, rp);
+ ecc->p.reduce (&ecc->p, rp);
}
void
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
{
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
{
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
{
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
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);
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);
{
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);
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);