From a69a3efddbe1261f55e90d4ff477053ab80663a5 Mon Sep 17 00:00:00 2001 From: mdw Date: Mon, 4 Oct 2004 08:14:35 +0000 Subject: [PATCH] Rename MP_IS* to MP_*P, for consistency's sake. Use these macros more often. Make {mp,gf}reduce_exp invert correctly. --- ec-exp.c | 8 ++++---- ec-info.c | 4 ++-- f-binpoly.c | 4 ++-- f-niceprime.c | 10 +++++----- f-prime.c | 12 ++++++------ g-prime.c | 4 ++-- gdsa.c | 6 +++--- gf-gcd.c | 4 ++-- gfreduce.c | 22 ++++++++++++++-------- gkcdsa.c | 4 ++-- group-exp.c | 8 ++++---- mp-arith.c | 18 +++++++++--------- mp-gcd.c | 18 +++++++++--------- mp-io.c | 6 +++--- mp-jacobi.c | 6 +++--- mp-sqrt.c | 6 +++--- mp.h | 14 +++++++------- mpbarrett-exp.c | 18 ++++++++++-------- mpbarrett-mexp.c | 8 ++++---- mpint.h | 4 ++-- mpmont-exp.c | 24 +++++++++++++----------- mpmont-mexp.c | 4 ++-- mpmont.c | 4 ++-- mpreduce.c | 22 ++++++++++++++-------- mptext-len.c | 4 ++-- mptext.c | 6 +++--- mptext.h | 4 ++-- pgen.c | 4 ++-- rsa-gen.c | 4 ++-- tests/gfreduce | 19 ++++++++++++++++++- tests/mpbarrett | 3 ++- tests/mpmont | 5 +++-- tests/mpreduce | 5 ++++- 33 files changed, 165 insertions(+), 127 deletions(-) diff --git a/ec-exp.c b/ec-exp.c index edcf620..c367bda 100644 --- a/ec-exp.c +++ b/ec-exp.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: ec-exp.c,v 1.2 2004/04/08 01:36:15 mdw Exp $ + * $Id$ * * Point multiplication for elliptic curves * @@ -57,10 +57,10 @@ ec *ec_imul(ec_curve *c, ec *d, const ec *p, mp *n) t.x->f |= MP_BURN; MP_SHRINK(n); EC_SETINF(d); - if (MP_LEN(n) == 0) + if (MP_ZEROP(n)) ; else { - if (n->f & MP_NEG) + if (MP_NEGP(n)) EC_NEG(c, &t, &t); if (MP_LEN(n) < EXP_THRESH) EXP_SIMPLE(*d, t, n); @@ -100,7 +100,7 @@ static ec *immul(ec_curve *c, ec *d, ec_mulfactor *f, size_t n) for (i = 0; i < n; i++) { MP_SHRINK(f[i].exp); - if (f[i].exp->f & MP_NEG) + if (MP_NEGP(f[i].exp)) EC_NEG(c, &f[i].base, &f[i].base); if (f[i].base.x && f[i].exp->f & MP_BURN) f[i].base.x->f |= MP_BURN; diff --git a/ec-info.c b/ec-info.c index 4f852c2..ea584de 100644 --- a/ec-info.c +++ b/ec-info.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: ec-info.c,v 1.7 2004/04/17 09:58:37 mdw Exp $ + * $Id$ * * Elliptic curve information management * @@ -325,7 +325,7 @@ void ec_freeinfo(ec_info *ei) static int primeeltp(mp *x, field *f) { - return (!MP_ISNEG(x) && MP_CMP(x, <, f->m)); + return (!MP_NEGP(x) && MP_CMP(x, <, f->m)); } static const char *primecheck(const ec_info *ei, grand *gr) diff --git a/f-binpoly.c b/f-binpoly.c index 4282ad4..8fae42a 100644 --- a/f-binpoly.c +++ b/f-binpoly.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: f-binpoly.c,v 1.9 2004/04/08 01:36:15 mdw Exp $ + * $Id$ * * Binary fields with polynomial basis representation * @@ -52,7 +52,7 @@ static void fdestroy(field *ff) static mp *frand(field *f, mp *d, grand *r) { return (mprand(d, f->nbits, r, 0)); } -static int fzerop(field *ff, mp *x) { return (!MP_LEN(x)); } +static int fzerop(field *ff, mp *x) { return (MP_ZEROP(x)); } static mp *fadd(field *ff, mp *d, mp *x, mp *y) { return (gf_add(d, x, y)); } diff --git a/f-niceprime.c b/f-niceprime.c index 8645cb6..d9ec1ac 100644 --- a/f-niceprime.c +++ b/f-niceprime.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: f-niceprime.c,v 1.6 2004/04/08 01:36:15 mdw Exp $ + * $Id$ * * Prime fields with efficient reduction for special-form primes * @@ -50,21 +50,21 @@ static void fdestroy(field *ff) static mp *frand(field *ff, mp *d, grand *r) { fctx *f = (fctx *)ff; return (mprand_range(d, f->r.p, r, 0)); } -static int fzerop(field *ff, mp *x) { return (!MP_LEN(x)); } +static int fzerop(field *ff, mp *x) { return (MP_ZEROP(x)); } static mp *fneg(field *ff, mp *d, mp *x) { fctx *f = (fctx *)ff; return (mp_sub(d, f->r.p, x)); } static mp *fadd(field *ff, mp *d, mp *x, mp *y) { fctx *f = (fctx *)ff; d = mp_add(d, x, y); - if (d->f & MP_NEG) d = mp_add(d, d, f->r.p); + if (MP_NEGP(d)) d = mp_add(d, d, f->r.p); else if (MP_CMP(d, >, f->r.p)) d = mp_sub(d, d, f->r.p); return (d); } static mp *fsub(field *ff, mp *d, mp *x, mp *y) { fctx *f = (fctx *)ff; d = mp_sub(d, x, y); - if (d->f & MP_NEG) d = mp_add(d, d, f->r.p); + if (MP_NEGP(d)) d = mp_add(d, d, f->r.p); else if (MP_CMP(d, >, f->r.p)) d = mp_sub(d, d, f->r.p); return (d); } @@ -109,7 +109,7 @@ static mp *fqdl(field *ff, mp *d, mp *x) { static mp *fhlv(field *ff, mp *d, mp *x) { fctx *f = (fctx *)ff; - if (!MP_LEN(x)) { MP_COPY(x); MP_DROP(d); return (x); } + if (MP_ZEROP(x)) { MP_COPY(x); MP_DROP(d); return (x); } if (x->v[0] & 1) { d = mp_add(d, x, f->r.p); x = d; } return (mp_lsr(d, x, 1)); } diff --git a/f-prime.c b/f-prime.c index f58bf63..1dcfded 100644 --- a/f-prime.c +++ b/f-prime.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: f-prime.c,v 1.12 2004/04/08 01:36:15 mdw Exp $ + * $Id$ * * Prime fields with Montgomery arithmetic * @@ -59,21 +59,21 @@ static mp *fin(field *ff, mp *d, mp *x) { static mp *fout(field *ff, mp *d, mp *x) { fctx *f = (fctx *)ff; return (mpmont_reduce(&f->mm, d, x)); } -static int fzerop(field *ff, mp *x) { return (!MP_LEN(x)); } +static int fzerop(field *ff, mp *x) { return (MP_ZEROP(x)); } static mp *fneg(field *ff, mp *d, mp *x) { fctx *f = (fctx *)ff; return (mp_sub(d, f->mm.m, x)); } static mp *fadd(field *ff, mp *d, mp *x, mp *y) { fctx *f = (fctx *)ff; d = mp_add(d, x, y); - if (d->f & MP_NEG) d = mp_add(d, d, f->mm.m); + if (MP_NEGP(d)) d = mp_add(d, d, f->mm.m); else if (MP_CMP(d, >, f->mm.m)) d = mp_sub(d, d, f->mm.m); return (d); } static mp *fsub(field *ff, mp *d, mp *x, mp *y) { fctx *f = (fctx *)ff; d = mp_sub(d, x, y); - if (d->f & MP_NEG) d = mp_add(d, d, f->mm.m); + if (MP_NEGP(d)) d = mp_add(d, d, f->mm.m); else if (MP_CMP(d, >, f->mm.m)) d = mp_sub(d, d, f->mm.m); return (d); } @@ -121,7 +121,7 @@ static mp *fqdl(field *ff, mp *d, mp *x) { static mp *fhlv(field *ff, mp *d, mp *x) { fctx *f = (fctx *)ff; - if (!MP_LEN(x)) { MP_COPY(x); MP_DROP(d); return (x); } + if (MP_ZEROP(x)) { MP_COPY(x); MP_DROP(d); return (x); } if (x->v[0] & 1) { d = mp_add(d, x, f->mm.m); x = d; } return (mp_lsr(d, x, 1)); } @@ -151,7 +151,7 @@ field *field_prime(mp *p) { fctx *f; - if (!MP_ISPOS(p) || !MP_ISODD(p)) + if (!MP_POSP(p) || !MP_ODDP(p)) return (0); f = CREATE(fctx); f->f.ops = &fops; diff --git a/g-prime.c b/g-prime.c index aa17685..6b9d28b 100644 --- a/g-prime.c +++ b/g-prime.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: g-prime.c,v 1.4 2004/04/08 01:36:15 mdw Exp $ + * $Id$ * * Abstraction for prime groups * @@ -173,7 +173,7 @@ group *group_prime(const gprime_param *gp) { gctx *g; - if (!MP_ISPOS(gp->p) || !MP_ISODD(gp->p)) + if (!MP_POSP(gp->p) || !MP_ODDP(gp->p)) return (0); g = CREATE(gctx); g->g.ops = &gops; diff --git a/gdsa.c b/gdsa.c index 9b23f4f..751bcce 100644 --- a/gdsa.c +++ b/gdsa.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: gdsa.c,v 1.2 2004/04/08 01:36:15 mdw Exp $ + * $Id$ * * Generalized version of DSA * @@ -88,10 +88,10 @@ void gdsa_sign(const gdsa *c, gdsa_sig *s, const void *m, mp *k) new_k: k = mprand_range(k, g->r, c->r, 0); have_k: - if (MP_ISZERO(k)) goto new_k; + if (MP_ZEROP(k)) goto new_k; G_EXP(g, z, g->g, k); sr = G_TOINT(g, sr, z); assert(sr); - if (MP_ISZERO(sr)) goto new_k; + if (MP_ZEROP(sr)) goto new_k; mp_div(0, &sr, sr, g->r); mpbarrett_create(&b, g->r); diff --git a/gf-gcd.c b/gf-gcd.c index 8eb9bbf..622679a 100644 --- a/gf-gcd.c +++ b/gf-gcd.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: gf-gcd.c,v 1.3 2004/04/08 01:36:15 mdw Exp $ + * $Id$ * * Euclidian algorithm on binary polynomials * @@ -119,7 +119,7 @@ void gf_gcd(mp **gcd, mp **xx, mp **yy, mp *a, mp *b) u = MP_COPY(a); v = MP_COPY(b); - while (MP_LEN(v)) { + while (!MP_ZEROP(v)) { mp *t; gf_div(&q, &u, u, v); if (f & f_ext) { diff --git a/gfreduce.c b/gfreduce.c index ae31d5c..bdf3579 100644 --- a/gfreduce.c +++ b/gfreduce.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: gfreduce.c,v 1.5 2004/04/08 01:36:15 mdw Exp $ + * $Id$ * * Efficient reduction modulo sparse binary polynomials * @@ -338,7 +338,7 @@ int gfreduce_trace(gfreduce *r, mp *x) y = gfreduce_do(r, t, t); y = gf_add(y, y, x); } - rc = !MP_ISZERO(y); + rc = !MP_ZEROP(y); mp_drop(spare); mp_drop(y); return (rc); @@ -410,7 +410,7 @@ mp *gfreduce_quadsolve(gfreduce *r, mp *d, mp *x) z = gf_add(z, z, t); w = gf_add(w, w, rho); } - if (!MP_ISZERO(w)) + if (!MP_ZEROP(w)) break; MP_DROP(z); MP_DROP(w); @@ -450,13 +450,19 @@ mp *gfreduce_exp(gfreduce *gr, mp *d, mp *a, mp *e) mp *spare = (e->f & MP_BURN) ? MP_NEWSEC : MP_NEW; MP_SHRINK(e); - if (!MP_LEN(e)) + MP_COPY(a); + if (MP_ZEROP(e)) ; - else if (MP_LEN(e) < EXP_THRESH) - EXP_SIMPLE(x, a, e); - else - EXP_WINDOW(x, a, e); + else { + if (MP_NEGP(e)) + a = gf_modinv(a, a, gr->p); + if (MP_LEN(e) < EXP_THRESH) + EXP_SIMPLE(x, a, e); + else + EXP_WINDOW(x, a, e); + } mp_drop(d); + mp_drop(a); mp_drop(spare); return (x); } diff --git a/gkcdsa.c b/gkcdsa.c index 6d812ff..433a158 100644 --- a/gkcdsa.c +++ b/gkcdsa.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: gkcdsa.c,v 1.2 2004/04/08 01:36:15 mdw Exp $ + * $Id$ * * Generalized version of KCDSA * @@ -129,7 +129,7 @@ void gkcdsa_sign(const gkcdsa *c, gkcdsa_sig *s, const void *m, mp *k) new_k: k = mprand_range(k, g->r, c->r, 0); have_k: - if (MP_ISZERO(k)) goto new_k; + if (MP_ZEROP(k)) goto new_k; G_EXP(g, z, g->g, k); if (!s->r) s->r = xmalloc(hsz); h = hashge(g, c->h, z); GH_DONE(h, s->r); diff --git a/group-exp.c b/group-exp.c index 2ca684c..ca9cfb0 100644 --- a/group-exp.c +++ b/group-exp.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: group-exp.c,v 1.2 2004/04/08 01:36:15 mdw Exp $ + * $Id$ * * Exponentiation for abstract groups * @@ -55,10 +55,10 @@ void group_stdexp(group *gg, ge *d, ge *x, mp *n) G_COPY(gg, d, gg->i); if (n->f & MP_BURN) G_BURN(gg, t); - if (MP_LEN(n) == 0) + if (MP_ZEROP(n)) ; else { - if (n->f & MP_NEG) + if (MP_NEGP(n)) G_INV(gg, t, t); if (MP_LEN(n) < EXP_THRESH) EXP_SIMPLE(d, t, n); @@ -91,7 +91,7 @@ void group_stdmexp(group *gg, ge *d, const group_expfactor *f, size_t n) for (i = 0; i < n; i++) { ff[i].base = G_CREATE(gg); MP_SHRINK(f[i].exp); - if (f[i].exp->f & MP_NEG) + if (MP_NEGP(f[i].exp)) G_INV(gg, ff[i].base, f[i].base); else G_COPY(gg, ff[i].base, f[i].base); diff --git a/mp-arith.c b/mp-arith.c index 736be2f..4bd0976 100644 --- a/mp-arith.c +++ b/mp-arith.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: mp-arith.c,v 1.18 2004/04/08 01:36:15 mdw Exp $ + * $Id$ * * Basic arithmetic on multiprecision integers * @@ -90,7 +90,7 @@ mp *mp_lsr(mp *d, mp *a, size_t n) mp *mp_lsl2c(mp *d, mp *a, size_t n) { - if (!(a->f & MP_NEG)) + if (!MP_NEGP(a)) return (mp_lsl(d, a, n)); d = mp_not2c(d, a); d = mp_lslc(d, d, n); @@ -100,7 +100,7 @@ mp *mp_lsl2c(mp *d, mp *a, size_t n) mp *mp_lsr2c(mp *d, mp *a, size_t n) { - if (!(a->f & MP_NEG)) + if (!MP_NEGP(a)) return (mp_lsr(d, a, n)); d = mp_not2c(d, a); d = mp_lsr(d, d, n); @@ -135,7 +135,7 @@ int mp_testbit(mp *x, unsigned long n) int mp_testbit2c(mp *x, unsigned long n) { int r; - if (!(x->f & MP_NEG)) + if (!MP_NEGP(x)) return (mp_testbit(x, n)); x = mp_not2c(MP_NEW, x); r = !mp_testbit(x, n); @@ -192,7 +192,7 @@ mp *mp_clearbit(mp *d, mp *x, unsigned long n) mp *mp_setbit2c(mp *d, mp *x, unsigned long n) { - if (!(x->f & MP_NEG)) + if (!MP_NEGP(x)) return mp_setbit(d, x, n); d = mp_not2c(d, x); d = mp_clearbit(d, d, n); @@ -202,7 +202,7 @@ mp *mp_setbit2c(mp *d, mp *x, unsigned long n) mp *mp_clearbit2c(mp *d, mp *x, unsigned long n) { - if (!(x->f & MP_NEG)) + if (!MP_NEGP(x)) return mp_clearbit(d, x, n); d = mp_not2c(d, x); d = mp_setbit(d, d, n); @@ -400,12 +400,12 @@ mp *mp_not2c(mp *d, mp *a) MP_DEST(d, MP_LEN(a) + 1, a->f); if (d == a) { - if (a->f & MP_NEG) + if (MP_NEGP(a)) MPX_USUBN(d->v, d->vl, 1); else MPX_UADDN(d->v, d->vl, 1); } else { - if (a->f & MP_NEG) + if (MP_NEGP(a)) mpx_usub(d->v, d->vl, a->v, a->vl, &one, &one + 1); else mpx_uadd(d->v, d->vl, a->v, a->vl, &one, &one + 1); @@ -590,7 +590,7 @@ void mp_div(mp **qq, mp **rr, mp *a, mp *b) */ q->f = ((r->f | b->f) & MP_BURN) | ((r->f ^ b->f) & MP_NEG); - if (q->f & MP_NEG) { + if (MP_NEGP(q)) { mpw *v; for (v = r->v; v < r->vl; v++) { if (*v) { diff --git a/mp-gcd.c b/mp-gcd.c index 5e663a1..d997072 100644 --- a/mp-gcd.c +++ b/mp-gcd.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: mp-gcd.c,v 1.7 2004/04/08 01:36:15 mdw Exp $ + * $Id$ * * Extended GCD calculation * @@ -63,9 +63,9 @@ void mp_gcd(mp **gcd, mp **xx, mp **yy, mp *a, mp *b) if (xx || yy) f |= f_ext; - if (a->f & MP_NEG) + if (MP_NEGP(a)) f |= f_aneg; - if (b->f & MP_NEG) + if (MP_NEGP(b)) f |= f_bneg; /* --- Ensure that @a@ is larger than @b@ --- * @@ -87,7 +87,7 @@ void mp_gcd(mp **gcd, mp **xx, mp **yy, mp *a, mp *b) if (gcd) { if (*gcd) MP_DROP(*gcd); a = MP_COPY(a); - if (a->f & MP_NEG) { + if (MP_NEGP(a)) { MP_SPLIT(a); a->f &= ~MP_NEG; f |= f_aneg; @@ -131,7 +131,7 @@ void mp_gcd(mp **gcd, mp **xx, mp **yy, mp *a, mp *b) u = MP_COPY(a); v = MP_COPY(b); - while (MP_LEN(v)) { + while (!MP_ZEROP(v)) { mp *t; mp_div(&q, &u, u, v); if (f & f_ext) { @@ -178,11 +178,11 @@ void mp_gcd(mp **gcd, mp **xx, mp **yy, mp *a, mp *b) */ if (yy) { - if (y->f & MP_NEG) { + if (MP_NEGP(y)) { do { y = mp_add(y, y, a); x = mp_sub(x, x, b); - } while (y->f & MP_NEG); + } while (MP_NEGP(y)); } else { while (MP_CMP(y, >=, a)) { y = mp_sub(y, y, a); @@ -190,10 +190,10 @@ void mp_gcd(mp **gcd, mp **xx, mp **yy, mp *a, mp *b) } } } else { - if (x->f & MP_NEG) { + if (MP_NEGP(x)) { do x = mp_add(x, x, b); - while (x->f & MP_NEG); + while (MP_NEGP(x)); } else { while (MP_CMP(x, >=, b)) x = mp_sub(x, x, b); diff --git a/mp-io.c b/mp-io.c index 4ef8ee0..d2f2768 100644 --- a/mp-io.c +++ b/mp-io.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: mp-io.c,v 1.7 2004/04/08 01:36:15 mdw Exp $ + * $Id$ * * Loading and storing of multiprecision integers * @@ -220,7 +220,7 @@ mp *mp_loadl2c(mp *d, const void *pv, size_t sz) void mp_storel2c(const mp *m, void *pv, size_t sz) { - if (m->f & MP_NEG) + if (MP_NEGP(m)) mpx_storel2cn(m->v, m->vl, pv, sz); else mpx_storel(m->v, m->vl, pv, sz); @@ -272,7 +272,7 @@ mp *mp_loadb2c(mp *d, const void *pv, size_t sz) void mp_storeb2c(const mp *m, void *pv, size_t sz) { - if (m->f & MP_NEG) + if (MP_NEGP(m)) mpx_storeb2cn(m->v, m->vl, pv, sz); else mpx_storeb(m->v, m->vl, pv, sz); diff --git a/mp-jacobi.c b/mp-jacobi.c index 2562342..47cd2f4 100644 --- a/mp-jacobi.c +++ b/mp-jacobi.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: mp-jacobi.c,v 1.5 2004/04/08 01:36:15 mdw Exp $ + * $Id$ * * Compute Jacobi symbol * @@ -50,7 +50,7 @@ int mp_jacobi(mp *a, mp *n) { int s = 1; - assert(((void)"n must be odd in mp_jacobi", MP_LEN(n) && (n->v[0] & 1))); + assert(MP_ODDP(n)); /* --- Take copies of the arguments --- */ @@ -66,7 +66,7 @@ int mp_jacobi(mp *a, mp *n) /* --- Some simple special cases --- */ MP_SHRINK(a); - if (MP_LEN(a) == 0) { + if (MP_ZEROP(a)) { s = 0; goto done; } diff --git a/mp-sqrt.c b/mp-sqrt.c index 01fe000..d18d183 100644 --- a/mp-sqrt.c +++ b/mp-sqrt.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: mp-sqrt.c,v 1.5 2004/04/08 01:36:15 mdw Exp $ + * $Id$ * * Compute integer square roots * @@ -54,7 +54,7 @@ mp *mp_sqrt(mp *d, mp *a) /* --- Sanity preservation --- */ - assert(((void)"imaginary root in mp_sqrt", !(a->f & MP_NEG))); + assert(!MP_NEGP(a)); /* --- Deal with trivial cases --- */ @@ -86,7 +86,7 @@ mp *mp_sqrt(mp *d, mp *a) q = mp_sub(q, q, a); if (q->v == q->vl) break; - if (q->f & MP_NEG) { + if (MP_NEGP(q)) { r = mp_lsl(r, d, 1); r->f |= MP_NEG; if (MP_CMP(q, <=, r)) diff --git a/mp.h b/mp.h index 8319173..f6fa14f 100644 --- a/mp.h +++ b/mp.h @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: mp.h,v 1.19 2004/04/08 01:36:15 mdw Exp $ + * $Id$ * * Simple multiprecision arithmetic * @@ -363,7 +363,7 @@ extern void mp_shrink(mp */*m*/); #define MP_SHRINK(m) do { \ mp *_mm = (m); \ MPX_SHRINK(_mm->v, _mm->vl); \ - if (!MP_LEN(_mm)) \ + if (MP_ZEROP(_mm)) \ _mm->f &= ~MP_NEG; \ } while (0) @@ -795,11 +795,11 @@ extern int mp_cmp(const mp */*a*/, const mp */*b*/); /* --- Other handy macros --- */ -#define MP_ISNEG(x) ((x)->f & MP_NEG) -#define MP_ISZERO(x) (!MP_LEN(x)) -#define MP_ISPOS(x) (!MP_ISNEG(x) && !MP_ISZERO(x)) -#define MP_ISODD(x) (!MP_ISZERO(x) && ((x)->v[0] & 1u)) -#define MP_ISEVEN(x) (!MP_ISODD(x)) +#define MP_NEGP(x) ((x)->f & MP_NEG) +#define MP_ZEROP(x) (!MP_LEN(x)) +#define MP_POSP(x) (!MP_NEGP(x) && !MP_ZEROP(x)) +#define MP_ODDP(x) (!MP_ZEROP(x) && ((x)->v[0] & 1u)) +#define MP_EVENP(x) (!MP_ODDP(x)) /*----- Arithmetic operations ---------------------------------------------*/ diff --git a/mpbarrett-exp.c b/mpbarrett-exp.c index 56e7c76..9885e2f 100644 --- a/mpbarrett-exp.c +++ b/mpbarrett-exp.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: mpbarrett-exp.c,v 1.2 2004/04/08 01:36:15 mdw Exp $ + * $Id$ * * Modular exponentiation using Barrett reduction * @@ -52,14 +52,16 @@ mp *mpbarrett_exp(mpbarrett *mb, mp *d, mp *a, mp *e) MP_COPY(a); MP_SHRINK(e); - if (e->f & MP_NEG) - a = mp_modinv(a, a, mb->m); - if (!MP_LEN(e)) + if (MP_ZEROP(e)) ; - else if (MP_LEN(e) < EXP_THRESH) - EXP_SIMPLE(x, a, e); - else - EXP_WINDOW(x, a, e); + else { + if (MP_NEGP(e)) + a = mp_modinv(a, a, mb->m); + if (MP_LEN(e) < EXP_THRESH) + EXP_SIMPLE(x, a, e); + else + EXP_WINDOW(x, a, e); + } mp_drop(d); mp_drop(spare); mp_drop(a); diff --git a/mpbarrett-mexp.c b/mpbarrett-mexp.c index 4be854d..0338527 100644 --- a/mpbarrett-mexp.c +++ b/mpbarrett-mexp.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: mpbarrett-mexp.c,v 1.3 2004/04/08 01:36:15 mdw Exp $ + * $Id$ * * Multiple simultaneous exponentiations * @@ -63,10 +63,10 @@ mp *mpbarrett_mexp(mpbarrett *mb, mp *d, const mp_expfactor *f, size_t n) for (i = 0; i < n; i++) { if (f[i].exp->f & MP_BURN) spare = MP_NEWSEC; - if (!(f[i].exp->f & MP_NEG)) - ff[i].base = MP_COPY(f[i].base); - else + if (MP_NEGP(f[i].exp)) ff[i].base = mp_modinv(MP_NEW, f[i].base, mb->m); + else + ff[i].base = MP_COPY(f[i].base); ff[i].exp = f[i].exp; } mp_drop(g); diff --git a/mpint.h b/mpint.h index 13df890..23378cd 100644 --- a/mpint.h +++ b/mpint.h @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: mpint.h,v 1.6 2004/04/08 01:36:15 mdw Exp $ + * $Id$ * * Conversion between MPs and standard C integers * @@ -136,7 +136,7 @@ _v++; \ _max /= (mpd)MPW_MAX + 1; \ } \ - if (!(_m->f & MP_NEG)) \ + if (!MP_NEGP(_m)) \ _i = -_i; \ (i) = _i; \ } while (0) diff --git a/mpmont-exp.c b/mpmont-exp.c index c43d02f..0caf118 100644 --- a/mpmont-exp.c +++ b/mpmont-exp.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: mpmont-exp.c,v 1.2 2004/04/08 01:36:15 mdw Exp $ + * $Id$ * * Modular exponentiation with Montgomery reduction * @@ -52,17 +52,19 @@ mp *mpmont_expr(mpmont *mm, mp *d, mp *a, mp *e) MP_COPY(a); MP_SHRINK(e); - if (e->f & MP_NEG) { - a = mpmont_reduce(mm, a, a); - a = mp_modinv(a, a, mm->m); - a = mpmont_mul(mm, a, a, mm->r2); - } - if (MP_LEN(e) == 0) + if (MP_ZEROP(e)) ; - else if (MP_LEN(e) < EXP_THRESH) - EXP_SIMPLE(x, a, e); - else - EXP_WINDOW(x, a, e); + else { + if (MP_NEGP(e)) { + a = mpmont_reduce(mm, a, a); + a = mp_modinv(a, a, mm->m); + a = mpmont_mul(mm, a, a, mm->r2); + } + if (MP_LEN(e) < EXP_THRESH) + EXP_SIMPLE(x, a, e); + else + EXP_WINDOW(x, a, e); + } mp_drop(d); mp_drop(spare); mp_drop(a); diff --git a/mpmont-mexp.c b/mpmont-mexp.c index 884d660..ce46fef 100644 --- a/mpmont-mexp.c +++ b/mpmont-mexp.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: mpmont-mexp.c,v 1.9 2004/04/08 01:36:15 mdw Exp $ + * $Id$ * * Multiple simultaneous exponentiations * @@ -63,7 +63,7 @@ static mp *mexpr(mpmont *mm, mp *d, mp_expfactor *f, size_t n) mp *t; if (f[i].exp->f & MP_BURN) spare = MP_NEWSEC; - if (f[i].exp->f & MP_NEG) { + if (MP_NEGP(f[i].exp)) { t = mpmont_reduce(mm, f[i].base, f[i].base); t = mp_modinv(t, t, mm->m); f[i].base = mpmont_mul(mm, t, t, mm->r2); diff --git a/mpmont.c b/mpmont.c index 39f51ed..24fc9a3 100644 --- a/mpmont.c +++ b/mpmont.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: mpmont.c,v 1.19 2004/04/08 01:36:15 mdw Exp $ + * $Id$ * * Montgomery reduction * @@ -76,7 +76,7 @@ void mpmont_create(mpmont *mm, mp *m) /* --- Take a copy of the modulus --- */ - assert(MP_ISPOS(m) && MP_ISODD(m)); + assert(MP_POSP(m) && MP_ODDP(m)); mm->m = MP_COPY(m); /* --- Determine %$R^2$% --- */ diff --git a/mpreduce.c b/mpreduce.c index 7d31334..13e705e 100644 --- a/mpreduce.c +++ b/mpreduce.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: mpreduce.c,v 1.2 2004/04/08 01:36:15 mdw Exp $ + * $Id$ * * Efficient reduction modulo nice primes * @@ -64,7 +64,7 @@ void mpreduce_create(mpreduce *r, mp *p) /* --- Fill in the easy stuff --- */ - assert(MP_ISPOS(p)); + assert(MP_POSP(p)); d = mp_bits(p); r->lim = d/MPW_BITS; r->s = d%MPW_BITS; @@ -253,7 +253,7 @@ mp *mpreduce_do(mpreduce *r, mp *d, mp *x) /* --- If source is negative, divide --- */ - if (MP_ISNEG(x)) { + if (MP_NEGP(x)) { mp_div(0, &d, x, r->p); return (d); } @@ -335,12 +335,18 @@ mp *mpreduce_exp(mpreduce *mr, mp *d, mp *a, mp *e) mp *spare = (e->f & MP_BURN) ? MP_NEWSEC : MP_NEW; MP_SHRINK(e); - if (!MP_LEN(e)) + MP_COPY(a); + if (MP_ZEROP(e)) ; - else if (MP_LEN(e) < EXP_THRESH) - EXP_SIMPLE(x, a, e); - else - EXP_WINDOW(x, a, e); + else { + if (MP_NEGP(e)) + a = mp_modinv(a, a, mr->p); + if (MP_LEN(e) < EXP_THRESH) + EXP_SIMPLE(x, a, e); + else + EXP_WINDOW(x, a, e); + } + mp_drop(a); mp_drop(d); mp_drop(spare); return (x); diff --git a/mptext-len.c b/mptext-len.c index 5baa389..cf208af 100644 --- a/mptext-len.c +++ b/mptext-len.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: mptext-len.c,v 1.2 2004/04/08 01:36:15 mdw Exp $ + * $Id$ * * Work out length of a number's string representation * @@ -41,7 +41,7 @@ * * Returns: The number of digits needed to represent the number in the * given base. This will not include space for a leading sign - * (use @MP_ISNEG@ to check that, or just add one on for luck); + * (use @MP_NEGP@ to check that, or just add one on for luck); * neither will it add space for a terminating null. In general * the answer will be an overestimate. */ diff --git a/mptext.c b/mptext.c index 8130679..d528386 100644 --- a/mptext.c +++ b/mptext.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: mptext.c,v 1.18 2004/04/08 01:36:15 mdw Exp $ + * $Id$ * * Textual representation of multiprecision numbers * @@ -542,7 +542,7 @@ static int complicated(mp *m, int radix, mp **pr, unsigned i, unsigned z, assert(i); mp_div(&q, &m, m, pr[i]); - if (!MP_LEN(q)) + if (MP_ZEROP(q)) d = z; else { if (z > d) @@ -675,7 +675,7 @@ int mp_write(mp *m, int radix, const mptext_ops *ops, void *p) /* --- If the number is negative, sort that out --- */ - if (m->f & MP_NEG) { + if (MP_NEGP(m)) { if (ops->put("-", 1, p)) return (EOF); m->f &= ~MP_NEG; diff --git a/mptext.h b/mptext.h index ddd6328..fe89946 100644 --- a/mptext.h +++ b/mptext.h @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: mptext.h,v 1.7 2004/04/08 01:36:15 mdw Exp $ + * $Id$ * * Textual representation of multiprecision numbers * @@ -93,7 +93,7 @@ extern int mp_write(mp */*m*/, int /*radix*/, * * Returns: The number of digits needed to represent the number in the * given base. This will not include space for a leading sign - * (use @MP_ISNEG@ to check that, or just add one on for luck); + * (use @MP_NEGP@ to check that, or just add one on for luck); * neither will it add space for a terminating null. In general * the answer will be an overestimate. */ diff --git a/pgen.c b/pgen.c index ac8db38..439cbde 100644 --- a/pgen.c +++ b/pgen.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: pgen.c,v 1.10 2004/04/08 01:36:15 mdw Exp $ + * $Id$ * * Prime generation glue * @@ -325,7 +325,7 @@ int pgen_primep(mp *p, grand *gr) rabin r; mp *x = MP_NEW; - if (MP_ISNEG(p)) return (0); + if (MP_NEGP(p)) return (0); switch (pfilt_smallfactor(p)) { case PGEN_DONE: return (1); case PGEN_FAIL: return (0); diff --git a/rsa-gen.c b/rsa-gen.c index 951ab9f..d284715 100644 --- a/rsa-gen.c +++ b/rsa-gen.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: rsa-gen.c,v 1.5 2004/04/08 01:36:15 mdw Exp $ + * $Id$ * * RSA parameter generation * @@ -122,7 +122,7 @@ again: goto again; } - if (phi->f & MP_NEG) { + if (MP_NEGP(phi)) { mp *z = rp->p; rp->p = rp->q; rp->q = z; diff --git a/tests/gfreduce b/tests/gfreduce index f548b95..fa411fa 100644 --- a/tests/gfreduce +++ b/tests/gfreduce @@ -1,4 +1,4 @@ -# $Id: gfreduce,v 1.4 2004/03/27 17:54:12 mdw Exp $ +# $Id$ # # Test efficient polynomial reduction @@ -19,6 +19,10 @@ reduce { modexp { 0x20000000000000000000000000000000000000000000000000000000000001001 + 0x1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + 0 + 1; + 0x20000000000000000000000000000000000000000000000000000000000001001 0x02 0x1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff 1; @@ -34,6 +38,19 @@ modexp { 0x34235950984598345900983409845690805680985 0x0fffffffffffffffffffffffffffffffffffffffffff 1; + + 0x800000000000000000000000000000000000000c9 + 0x3f0eba16286a2d57ea0991168d4994637e8343e36 + -1 + 0x3c8c172e24598e90b9542e6b8f6571f54be572b50; + 0x800000000000000000000000000000000000000c9 + 0x3c8c172e24598e90b9542e6b8f6571f54be572b50 + 342345 + 0x3521fe2666efe92ca4eb160a286e0fd9427b84a62; + 0x800000000000000000000000000000000000000c9 + 0x3f0eba16286a2d57ea0991168d4994637e8343e36 + -342345 + 0x3521fe2666efe92ca4eb160a286e0fd9427b84a62; } sqrt { diff --git a/tests/mpbarrett b/tests/mpbarrett index c3074d0..c09b75b 100644 --- a/tests/mpbarrett +++ b/tests/mpbarrett @@ -1,6 +1,6 @@ # Test vectors for Barrett modular reduction # -# $Id: mpbarrett,v 1.4 2004/04/01 12:50:41 mdw Exp $ +# $Id$ mpbarrett-reduce { 17 11 6; @@ -54,6 +54,7 @@ mpbarrett-exp { # --- Negative exponents --- + 0xfffffffdffffffffffffffffffffffff 0xfffffffdfffffffffffffffffffffffe 0 1; 8939489893434234331 1804289383 -8939035539979879765 6139425926295484741; # --- DSA public key derivation --- diff --git a/tests/mpmont b/tests/mpmont index 389f2ed..fb05f5c 100644 --- a/tests/mpmont +++ b/tests/mpmont @@ -1,6 +1,6 @@ # Test vectors for Montgomery reduction # -# $Id: mpmont,v 1.7 2004/04/01 12:50:41 mdw Exp $ +# $Id$ create { 340809809850981098423498794792349 # m @@ -35,8 +35,9 @@ exp { 8939489893434234331 1804289383 454353454354565 6139425926295484741; 8939489893434234331 1804289383 8939489893434234330 1; - # --- Negative exponents --- + # --- Negative and zero exponents --- + 0xfffffffdffffffffffffffffffffffff 0xfffffffdfffffffffffffffffffffffe 0 1; 8939489893434234331 1804289383 -8939035539979879765 6139425926295484741; # --- DSA public key derivation --- diff --git a/tests/mpreduce b/tests/mpreduce index 2816b29..1734b18 100644 --- a/tests/mpreduce +++ b/tests/mpreduce @@ -1,4 +1,4 @@ -# $Id: mpreduce,v 1.1 2004/03/27 00:04:46 mdw Exp $ +# $Id$ # # Tests for efficient reduction @@ -13,9 +13,12 @@ reduce { } modexp { + 0xfffffffdffffffffffffffffffffffff 0xfffffffdfffffffffffffffffffffffe 0 1; 0xfffffffdffffffffffffffffffffffff 2 0xfffffffdfffffffffffffffffffffffe 1; 0xfffffffdffffffffffffffffffffffff 2 0xfffffffdfffffffffffffffffffffffd 0x7fffffff000000000000000000000000; + 8939489893434234331 1804289383 + -8939035539979879765 6139425926295484741; } -- 2.11.0