Commit 296b09cc authored by Niels Möller's avatar Niels Möller

New ecc_mod_* functions, replacing ecc_modp_* and ecc_modq_*.

parent 61a54890
2014-09-22 Niels Möller <nisse@lysator.liu.se>
* ecc-mod-arith.c: New file, replacing ecc-modp.c and ecc-modq.c.
All functions take a struct ecc_modulo as argument.
(ecc_mod_add, ecc_mod_sub, ecc_mod_mul_1, ecc_mod_addmul_1)
(ecc_mod_submul_1, ecc_mod_mul, ecc_mod_sqr): New functions,
replacing the corresponding ecc_modp_* functions. For convenience,
old names are defined as macros wrapping the new functions.
* ecc-modp.c: Deleted file.
* ecc-modq.c: Deleted file.
* Makefile.in (hogweed_SOURCES): Updated accordingly.
* testsuite/ecc-redc-test.c (test_main): Relaxed tests for which
tests to run.
......
......@@ -163,7 +163,7 @@ hogweed_SOURCES = sexp.c sexp-format.c \
sec-add-1.c sec-sub-1.c sec-tabselect.c \
gmp-glue.c cnd-copy.c \
ecc-mod.c ecc-mod-inv.c \
ecc-modp.c ecc-modq.c ecc-pp1-redc.c ecc-pm1-redc.c \
ecc-mod-arith.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-25519.c \
ecc-size.c ecc-j-to-a.c ecc-a-to-j.c \
......
......@@ -42,15 +42,13 @@
/* Name mangling */
#define ecc_pp1_redc _nettle_ecc_pp1_redc
#define ecc_pm1_redc _nettle_ecc_pm1_redc
#define ecc_modp_add _nettle_ecc_modp_add
#define ecc_modp_sub _nettle_ecc_modp_sub
#define ecc_modp_mul_1 _nettle_ecc_modp_mul_1
#define ecc_modp_addmul_1 _nettle_ecc_modp_addmul_1
#define ecc_modp_submul_1 _nettle_ecc_modp_submul_1
#define ecc_modp_mul _nettle_ecc_modp_mul
#define ecc_modp_sqr _nettle_ecc_modp_sqr
#define ecc_modq_mul _nettle_ecc_modq_mul
#define ecc_modq_add _nettle_ecc_modq_add
#define ecc_mod_add _nettle_ecc_mod_add
#define ecc_mod_sub _nettle_ecc_mod_sub
#define ecc_mod_mul_1 _nettle_ecc_mod_mul_1
#define ecc_mod_addmul_1 _nettle_ecc_mod_addmul_1
#define ecc_mod_submul_1 _nettle_ecc_mod_submul_1
#define ecc_mod_mul _nettle_ecc_mod_mul
#define ecc_mod_sqr _nettle_ecc_mod_sqr
#define ecc_modq_random _nettle_ecc_modq_random
#define ecc_mod _nettle_ecc_mod
#define ecc_mod_inv _nettle_ecc_mod_inv
......@@ -78,7 +76,7 @@ struct ecc_modulo;
/* Reduces from 2*ecc->size to ecc->size. */
/* Required to return a result < 2q. This property is inherited by
modp_mul and modp_sqr. */
mod_mul and mod_sqr. */
typedef void ecc_mod_func (const struct ecc_modulo *m, mp_limb_t *rp);
typedef void ecc_mod_inv_func (const struct ecc_modulo *m,
......@@ -162,7 +160,7 @@ struct ecc_curve
equivalent Edwards curve. */
const mp_limb_t *edwards_root;
/* For redc, same as Bmodp, otherwise 1. */
/* For redc, same as B mod p, otherwise 1. */
const mp_limb_t *unit;
/* Tables for multiplying by the generator, size determined by k and
......@@ -186,40 +184,53 @@ ecc_mod_func ecc_pm1_redc;
ecc_mod_inv_func ecc_mod_inv;
void
ecc_modp_add (const struct ecc_curve *ecc, mp_limb_t *rp,
const mp_limb_t *ap, const mp_limb_t *bp);
ecc_mod_add (const struct ecc_modulo *m, mp_limb_t *rp,
const mp_limb_t *ap, const mp_limb_t *bp);
void
ecc_modp_sub (const struct ecc_curve *ecc, mp_limb_t *rp,
const mp_limb_t *ap, const mp_limb_t *bp);
ecc_mod_sub (const struct ecc_modulo *m, mp_limb_t *rp,
const mp_limb_t *ap, const mp_limb_t *bp);
void
ecc_modp_mul_1 (const struct ecc_curve *ecc, mp_limb_t *rp,
const mp_limb_t *ap, const mp_limb_t b);
ecc_mod_mul_1 (const struct ecc_modulo *m, mp_limb_t *rp,
const mp_limb_t *ap, const mp_limb_t b);
void
ecc_modp_addmul_1 (const struct ecc_curve *ecc, mp_limb_t *rp,
const mp_limb_t *ap, mp_limb_t b);
ecc_mod_addmul_1 (const struct ecc_modulo *m, mp_limb_t *rp,
const mp_limb_t *ap, mp_limb_t b);
void
ecc_modp_submul_1 (const struct ecc_curve *ecc, mp_limb_t *rp,
const mp_limb_t *ap, mp_limb_t b);
ecc_mod_submul_1 (const struct ecc_modulo *m, mp_limb_t *rp,
const mp_limb_t *ap, mp_limb_t b);
/* NOTE: mul and sqr needs 2*ecc->size limbs at rp */
void
ecc_modp_mul (const struct ecc_curve *ecc, mp_limb_t *rp,
const mp_limb_t *ap, const mp_limb_t *bp);
ecc_mod_mul (const struct ecc_modulo *m, mp_limb_t *rp,
const mp_limb_t *ap, const mp_limb_t *bp);
void
ecc_modp_sqr (const struct ecc_curve *ecc, mp_limb_t *rp,
const mp_limb_t *ap);
ecc_mod_sqr (const struct ecc_modulo *m, mp_limb_t *rp,
const mp_limb_t *ap);
#define ecc_modp_add(ecc, r, a, b) \
ecc_mod_add (&(ecc)->p, (r), (a), (b))
#define ecc_modp_sub(ecc, r, a, b) \
ecc_mod_sub (&(ecc)->p, (r), (a), (b))
#define ecc_modp_mul_1(ecc, r, a, b) \
ecc_mod_mul_1 (&(ecc)->p, (r), (a), (b))
#define ecc_modp_addmul_1(ecc, r, a, b) \
ecc_mod_addmul_1 (&(ecc)->p, (r), (a), (b))
#define ecc_modp_submul_1(ecc, r, a, b) \
ecc_mod_submul_1 (&(ecc)->p, (r), (a), (b))
#define ecc_modp_mul(ecc, r, a, b) \
ecc_mod_mul (&(ecc)->p, (r), (a), (b))
#define ecc_modp_sqr(ecc, r, a) \
ecc_mod_sqr (&(ecc)->p, (r), (a))
#define ecc_modq_add(ecc, r, a, b) \
ecc_mod_add (&(ecc)->q, (r), (a), (b))
#define ecc_modq_mul(ecc, r, a, b) \
ecc_mod_mul (&(ecc)->q, (r), (a), (b))
/* mod q operations. */
void
ecc_modq_mul (const struct ecc_curve *ecc, mp_limb_t *rp,
const mp_limb_t *ap, const mp_limb_t *bp);
void
ecc_modq_add (const struct ecc_curve *ecc, mp_limb_t *rp,
const mp_limb_t *ap, const mp_limb_t *bp);
void
ecc_modq_random (const struct ecc_curve *ecc, mp_limb_t *xp,
void *ctx, nettle_random_func *random, mp_limb_t *scratch);
......
/* ecc-modp.c
/* ecc-mod-arith.c
Copyright (C) 2013 Niels Möller
Copyright (C) 2013, 2014 Niels Möller
This file is part of GNU Nettle.
......@@ -43,85 +43,85 @@
not necessarily < p. */
void
ecc_modp_add (const struct ecc_curve *ecc, mp_limb_t *rp,
const mp_limb_t *ap, const mp_limb_t *bp)
ecc_mod_add (const struct ecc_modulo *m, mp_limb_t *rp,
const mp_limb_t *ap, const mp_limb_t *bp)
{
mp_limb_t cy;
cy = mpn_add_n (rp, ap, bp, ecc->p.size);
cy = cnd_add_n (cy, rp, ecc->p.B, ecc->p.size);
cy = cnd_add_n (cy, rp, ecc->p.B, ecc->p.size);
cy = mpn_add_n (rp, ap, bp, m->size);
cy = cnd_add_n (cy, rp, m->B, m->size);
cy = cnd_add_n (cy, rp, m->B, m->size);
assert (cy == 0);
}
void
ecc_modp_sub (const struct ecc_curve *ecc, mp_limb_t *rp,
const mp_limb_t *ap, const mp_limb_t *bp)
ecc_mod_sub (const struct ecc_modulo *m, mp_limb_t *rp,
const mp_limb_t *ap, const mp_limb_t *bp)
{
mp_limb_t cy;
cy = mpn_sub_n (rp, ap, bp, ecc->p.size);
cy = cnd_sub_n (cy, rp, ecc->p.B, ecc->p.size);
cy = cnd_sub_n (cy, rp, ecc->p.B, ecc->p.size);
cy = mpn_sub_n (rp, ap, bp, m->size);
cy = cnd_sub_n (cy, rp, m->B, m->size);
cy = cnd_sub_n (cy, rp, m->B, m->size);
assert (cy == 0);
}
void
ecc_modp_mul_1 (const struct ecc_curve *ecc, mp_limb_t *rp,
const mp_limb_t *ap, mp_limb_t b)
ecc_mod_mul_1 (const struct ecc_modulo *m, mp_limb_t *rp,
const mp_limb_t *ap, mp_limb_t b)
{
mp_limb_t hi;
assert (b <= 0xffffffff);
hi = mpn_mul_1 (rp, ap, ecc->p.size, b);
hi = mpn_addmul_1 (rp, ecc->p.B, ecc->p.size, hi);
hi = mpn_mul_1 (rp, ap, m->size, b);
hi = mpn_addmul_1 (rp, m->B, m->size, hi);
assert (hi <= 1);
hi = cnd_add_n (hi, rp, ecc->p.B, ecc->p.size);
hi = cnd_add_n (hi, rp, m->B, m->size);
/* Sufficient if b < B^size / p */
assert (hi == 0);
}
void
ecc_modp_addmul_1 (const struct ecc_curve *ecc, mp_limb_t *rp,
const mp_limb_t *ap, mp_limb_t b)
ecc_mod_addmul_1 (const struct ecc_modulo *m, mp_limb_t *rp,
const mp_limb_t *ap, mp_limb_t b)
{
mp_limb_t hi;
assert (b <= 0xffffffff);
hi = mpn_addmul_1 (rp, ap, ecc->p.size, b);
hi = mpn_addmul_1 (rp, ecc->p.B, ecc->p.size, hi);
hi = mpn_addmul_1 (rp, ap, m->size, b);
hi = mpn_addmul_1 (rp, m->B, m->size, hi);
assert (hi <= 1);
hi = cnd_add_n (hi, rp, ecc->p.B, ecc->p.size);
hi = cnd_add_n (hi, rp, m->B, m->size);
/* Sufficient roughly if b < B^size / p */
assert (hi == 0);
}
void
ecc_modp_submul_1 (const struct ecc_curve *ecc, mp_limb_t *rp,
const mp_limb_t *ap, mp_limb_t b)
ecc_mod_submul_1 (const struct ecc_modulo *m, mp_limb_t *rp,
const mp_limb_t *ap, mp_limb_t b)
{
mp_limb_t hi;
assert (b <= 0xffffffff);
hi = mpn_submul_1 (rp, ap, ecc->p.size, b);
hi = mpn_submul_1 (rp, ecc->p.B, ecc->p.size, hi);
hi = mpn_submul_1 (rp, ap, m->size, b);
hi = mpn_submul_1 (rp, m->B, m->size, hi);
assert (hi <= 1);
hi = cnd_sub_n (hi, rp, ecc->p.B, ecc->p.size);
hi = cnd_sub_n (hi, rp, m->B, m->size);
/* Sufficient roughly if b < B^size / p */
assert (hi == 0);
}
/* NOTE: mul and sqr needs 2*ecc->p.size limbs at rp */
/* NOTE: mul and sqr needs 2*m->size limbs at rp */
void
ecc_modp_mul (const struct ecc_curve *ecc, mp_limb_t *rp,
const mp_limb_t *ap, const mp_limb_t *bp)
ecc_mod_mul (const struct ecc_modulo *m, mp_limb_t *rp,
const mp_limb_t *ap, const mp_limb_t *bp)
{
mpn_mul_n (rp, ap, bp, ecc->p.size);
ecc->p.reduce (&ecc->p, rp);
mpn_mul_n (rp, ap, bp, m->size);
m->reduce (m, rp);
}
void
ecc_modp_sqr (const struct ecc_curve *ecc, mp_limb_t *rp,
const mp_limb_t *ap)
ecc_mod_sqr (const struct ecc_modulo *m, mp_limb_t *rp,
const mp_limb_t *ap)
{
mpn_sqr (rp, ap, ecc->p.size);
ecc->p.reduce (&ecc->p, rp);
mpn_sqr (rp, ap, m->size);
m->reduce (m, rp);
}
/* ecc-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"
/* Arithmetic mod q, the group order. */
void
ecc_modq_add (const struct ecc_curve *ecc, mp_limb_t *rp,
const mp_limb_t *ap, const mp_limb_t *bp)
{
mp_limb_t cy;
cy = mpn_add_n (rp, ap, bp, ecc->q.size);
cy = cnd_add_n (cy, rp, ecc->q.B, ecc->q.size);
cy = cnd_add_n (cy, rp, ecc->q.B, ecc->q.size);
assert (cy == 0);
}
void
ecc_modq_mul (const struct ecc_curve *ecc, mp_limb_t *rp,
const mp_limb_t *ap, const mp_limb_t *bp)
{
mpn_mul_n (rp, ap, bp, ecc->q.size);
ecc->q.mod (&ecc->q, rp);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment