X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/dd22938ef0d9b0131dad9171a8a95866ceec9607..ba6e6b64033b1f9de49feccb5c9cd438354481f7:/mp-arith.c diff --git a/mp-arith.c b/mp-arith.c index 23137e2..f00af54 100644 --- a/mp-arith.c +++ b/mp-arith.c @@ -1,13 +1,13 @@ /* -*-c-*- * - * $Id: mp-arith.c,v 1.12 2002/10/09 00:36:03 mdw Exp $ + * $Id$ * * Basic arithmetic on multiprecision integers * * (c) 1999 Straylight/Edgeware */ -/*----- Licensing notice --------------------------------------------------* +/*----- Licensing notice --------------------------------------------------* * * This file is part of Catacomb. * @@ -15,63 +15,18 @@ * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. - * + * * Catacomb 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 Library General Public License for more details. - * + * * You should have received a copy of the GNU Library General Public * License along with Catacomb; if not, write to the Free * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, * MA 02111-1307, USA. */ -/*----- Revision history --------------------------------------------------* - * - * $Log: mp-arith.c,v $ - * Revision 1.12 2002/10/09 00:36:03 mdw - * Fix bounds on workspace for Karatsuba operations. - * - * Revision 1.11 2002/10/06 22:52:50 mdw - * Pile of changes for supporting two's complement properly. - * - * Revision 1.10 2001/04/03 19:36:05 mdw - * Add some simple bitwise operations so that Perl can use them. - * - * Revision 1.9 2000/10/08 15:48:35 mdw - * Rename Karatsuba constants now that we have @gfx_kmul@ too. - * - * Revision 1.8 2000/10/08 12:02:21 mdw - * Use @MP_EQ@ instead of @MP_CMP@. - * - * Revision 1.7 2000/06/22 19:02:53 mdw - * New function @mp_odd@ to extract powers of two from an integer. This is - * common code from the Rabin-Miller test, RSA key recovery and modular - * square-root extraction. - * - * Revision 1.6 2000/06/17 11:45:09 mdw - * Major memory management overhaul. Added arena support. Use the secure - * arena for secret integers. Replace and improve the MP management macros - * (e.g., replace MP_MODIFY by MP_DEST). - * - * Revision 1.5 1999/12/22 15:54:41 mdw - * Adjust Karatsuba parameters. Calculate destination size better. - * - * Revision 1.4 1999/12/13 15:35:16 mdw - * Slightly different rules on memory allocation. - * - * Revision 1.3 1999/12/11 10:57:43 mdw - * Karatsuba squaring algorithm. - * - * Revision 1.2 1999/12/10 23:18:39 mdw - * Change interface for suggested destinations. - * - * Revision 1.1 1999/11/17 18:02:16 mdw - * New multiprecision integer arithmetic suite. - * - */ - /*----- Header files ------------------------------------------------------*/ #include "mp.h" @@ -82,13 +37,18 @@ /*----- Main code ---------------------------------------------------------*/ -/* --- @mp_lsl@, @mp_lsr@ --- * +/* --- @mp_lsl@, @mp_lslc@, @mp_lsr@ --- * * * Arguments: @mp *d@ = destination * @mp *a@ = source * @size_t n@ = number of bits to move * * Returns: Result, @a@ shifted left or right by @n@. + * + * Use: Bitwise shift operators. @mp_lslc@ fills the bits introduced + * on the right with ones instead of zeroes: it's used + * internally by @mp_lsl2c@, though it may be useful on its + * own. */ mp *mp_lsl(mp *d, mp *a, size_t n) @@ -100,6 +60,15 @@ mp *mp_lsl(mp *d, mp *a, size_t n) return (d); } +mp *mp_lslc(mp *d, mp *a, size_t n) +{ + MP_DEST(d, MP_LEN(a) + (n + MPW_BITS - 1) / MPW_BITS, a->f); + mpx_lslc(d->v, d->vl, a->v, a->vl, n); + d->f = a->f & (MP_NEG | MP_BURN); + MP_SHRINK(d); + return (d); +} + mp *mp_lsr(mp *d, mp *a, size_t n) { MP_DEST(d, MP_LEN(a), a->f); @@ -121,17 +90,17 @@ 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_lsl(d, d, n); + d = mp_lslc(d, d, n); d = mp_not2c(d, d); return (d); } 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); @@ -142,34 +111,31 @@ mp *mp_lsr2c(mp *d, mp *a, size_t n) /* --- @mp_testbit@ --- * * * Arguments: @mp *x@ = a large integer - * @size_t n@ = which bit to test + * @unsigned long n@ = which bit to test * * Returns: Nonzero if the bit is set, zero if not. */ -int mp_testbit(mp *x, size_t n) +int mp_testbit(mp *x, unsigned long n) { - size_t o; if (n > MPW_BITS * MP_LEN(x)) return (0); - o = n / MPW_BITS; - n %= MPW_BITS; - return ((x->v[o] >> n) & 1); + return ((x->v[n/MPW_BITS] >> n%MPW_BITS) & 1u); } /* --- @mp_testbit2c@ --- * * * Arguments: @mp *x@ = a large integer - * @size_t n@ = which bit to test + * @unsigned long n@ = which bit to test * * Returns: Nonzero if the bit is set, zero if not. Fakes up two's * complement representation. */ -int mp_testbit2c(mp *x, size_t 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); @@ -177,6 +143,73 @@ int mp_testbit2c(mp *x, size_t n) return (r); } +/* --- @mp_setbit@, @mp_clearbit@ --- * + * + * Arguments: @mp *d@ = a destination + * @mp *x@ = a large integer + * @unsigned long n@ = which bit to modify + * + * Returns: The argument @x@, with the appropriate bit set or cleared. + */ + +mp *mp_setbit(mp *d, mp *x, unsigned long n) +{ + size_t rq; + + rq = n + MPW_BITS; rq -= rq % MPW_BITS; + if (d != x) { + if (d) MP_DROP(d); + d = MP_COPY(x); + } + MP_DEST(d, rq, x->f & (MP_NEG | MP_BURN)); + d->v[n/MPW_BITS] |= 1 << n%MPW_BITS; + return (d); +} + +mp *mp_clearbit(mp *d, mp *x, unsigned long n) +{ + size_t rq; + + rq = n + MPW_BITS; rq -= rq % MPW_BITS; + if (d != x) { + if (d) MP_DROP(d); + d = MP_COPY(x); + } + MP_DEST(d, rq, x->f & (MP_NEG | MP_BURN)); + d->v[n/MPW_BITS] &= ~(1 << n%MPW_BITS); + return (d); +} + +/* --- @mp_setbit2c@, @mp_clearbit2c@ --- * + * + * Arguments: @mp *d@ = a destination + * @mp *x@ = a large integer + * @unsigned long n@ = which bit to modify + * + * Returns: The argument @x@, with the appropriate bit set or cleared. + * Fakes up two's complement representation. + */ + +mp *mp_setbit2c(mp *d, mp *x, unsigned long n) +{ + if (!MP_NEGP(x)) + return mp_setbit(d, x, n); + d = mp_not2c(d, x); + d = mp_clearbit(d, d, n); + d = mp_not2c(d, d); + return (d); +} + +mp *mp_clearbit2c(mp *d, mp *x, unsigned long n) +{ + if (!MP_NEGP(x)) + return mp_clearbit(d, x, n); + d = mp_not2c(d, x); + d = mp_setbit(d, d, n); + d = mp_not2c(d, d); + return (d); +} + /* --- @mp_eq@ --- * * * Arguments: @const mp *a, *b@ = two numbers @@ -196,14 +229,42 @@ int mp_eq(const mp *a, const mp *b) { return (MP_EQ(a, b)); } int mp_cmp(const mp *a, const mp *b) { - if (!((a->f ^ b->f) & MP_NEG)) - return (mpx_ucmp(a->v, a->vl, b->v, b->vl)); - else if (a->f & MP_NEG) + if (!((a->f ^ b->f) & MP_NEG)) { + if (a->f & MP_NEG) + return (-mpx_ucmp(a->v, a->vl, b->v, b->vl)); + else + return (mpx_ucmp(a->v, a->vl, b->v, b->vl)); + } else if (a->f & MP_NEG) return (-1); else return (+1); } +/* --- @mp_neg@ --- * + * + * Arguments: @mp *d@ = destination + * @mp *a@ = argument + * + * Returns: The negation of the argument. + * + * Use: Negates its argument. + */ + +mp *mp_neg(mp *d, mp *a) +{ + /* --- Surprising amounts of messing about required --- */ + + MP_SHRINK(a); + MP_COPY(a); + if (d) + MP_DROP(d); + if (a->v == a->vl) + return (a); + MP_DEST(a, MP_LEN(a), a->f); + a->f ^= MP_NEG; + return (a); +} + /* --- @mp_bitop@ --- * * * Arguments: @mp *d@ = destination @@ -223,7 +284,7 @@ int mp_cmp(const mp *a, const mp *b) \ mp *mp_bit##string(mp *d, mp *a, mp *b) \ { \ - MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)), a->f | b->f); \ + MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)), (a->f | b->f) & ~MP_NEG); \ mpx_bit##string(d->v, d->vl, a->v, a->vl, b->v, b->vl); \ d->f = (a->f | b->f) & MP_BURN; \ MP_SHRINK(d); \ @@ -238,7 +299,7 @@ MPX_DOBIN(MP_BITBINOP) * @mp *a@ = source * * Returns: The bitwise complement of the source. - */ + */ mp *mp_not(mp *d, mp *a) { @@ -272,10 +333,10 @@ mp *mp_not(mp *d, mp *a) * negative at the end, we preinvert the output and then invert again with a * sign-swap. * - * Start with: wxyz WXYZ + * Start with: wxyz WXYZ * If @a@ negative: yzwx or YZWX - * If @b@ negative: xwzy XWZY - * If both negative: zyxw ZYXW + * If @b@ negative: xwzy XWZY + * If both negative: zyxw ZYXW */ #define MP_BIT2CBINOP(n, base, an, bn, abn, p_base, p_an, p_bn, p_abn) \ @@ -299,7 +360,7 @@ mp *mp_bit##n##2c(mp *d, mp *a, mp *b) \ p_bn \ } else { /* Both negative */ \ mp *t = mp_not2c(MP_NEW, a); \ - mp *d = mp_not2c(d, b); \ + d = mp_not2c(d, b); \ d = mp_bit##abn(d, t, d); \ MP_DROP(t); \ p_abn \ @@ -342,12 +403,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); @@ -504,7 +565,7 @@ void mp_div(mp **qq, mp **rr, mp *a, mp *b) if (r) MP_DROP(r); r = a; - MP_DEST(r, MP_LEN(a) + 2, a->f | b->f); + MP_DEST(r, MAX(MP_LEN(a), MP_LEN(b)) + 2, a->f | b->f); /* --- Fix up the quotient too --- */ @@ -532,7 +593,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) { @@ -591,16 +652,16 @@ mp *mp_odd(mp *d, mp *m, size_t *s) ss = 0; else { mpw x = *v; - mpw mask = MPW_MAX; - unsigned z = MPW_BITS / 2; + unsigned z = MPW_P2; + mpw mask = ((mpw)1 << z) - 1; while (z) { - mask >>= z; if (!(x & mask)) { x >>= z; ss += z; } z >>= 1; + mask >>= z; } } @@ -616,8 +677,8 @@ static int verify(const char *op, mp *expect, mp *result, mp *a, mp *b) { if (!MP_EQ(expect, result)) { fprintf(stderr, "\n*** %s failed", op); - fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 10); - fputs("\n*** b = ", stderr); mp_writefile(b, stderr, 10); + fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 10); + fputs("\n*** b = ", stderr); mp_writefile(b, stderr, 10); fputs("\n*** result = ", stderr); mp_writefile(result, stderr, 10); fputs("\n*** expect = ", stderr); mp_writefile(expect, stderr, 10); fputc('\n', stderr); @@ -665,6 +726,7 @@ RIG(lsr2c, mp_lsr2c) RIG(add, mp_add) RIG(sub, mp_sub) RIG(mul, mp_mul) +RIG(exp, mp_exp) #undef RIG @@ -687,7 +749,7 @@ static int tdiv(dstr *v) static int tbin(dstr *v) { static mp *(*fn[])(mp *, mp *, mp *) = { -#define DO(string) mp_bit##string##2c, +#define DO(string) mp_bit##string##2c, MPX_DOBIN(DO) #undef DO }; @@ -697,7 +759,7 @@ MPX_DOBIN(DO) mp *b = *(mp **)v[2].buf; mp *r = *(mp **)v[3].buf; mp *c; - + if (strcmp(v[0].buf, "and") == 0) op = 1; else if (strcmp(v[0].buf, "or") == 0) op = 7; else if (strcmp(v[0].buf, "nand") == 0) op = 14; @@ -719,6 +781,100 @@ MPX_DOBIN(DO) return (ok); } +static int tset(dstr *v) +{ + mp *a = *(mp **)v[0].buf; + unsigned long n = *(unsigned long *)v[1].buf; + mp *r = *(mp **)v[2].buf; + mp *c; + int ok = 1; + + c = mp_setbit2c(MP_NEW, a, n); + if (!MP_EQ(c, r)) { + ok = 0; + fprintf(stderr, "\n***setbit (set) failed"); + fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 16); + fprintf(stderr, "\n*** n = %lu", n); + fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 16); + fputs("\n*** c = ", stderr); mp_writefile(c, stderr, 16); + fputc('\n', stderr); + } + if (!mp_testbit2c(r, n)) { + ok = 0; + fprintf(stderr, "\n***setbit (test) failed"); + fprintf(stderr, "\n*** n = %lu", n); + fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 16); + fputc('\n', stderr); + } + mp_drop(a); + mp_drop(r); + mp_drop(c); + assert(mparena_count(MPARENA_GLOBAL) == 0); + return (ok); +} + +static int tclr(dstr *v) +{ + mp *a = *(mp **)v[0].buf; + unsigned long n = *(unsigned long *)v[1].buf; + mp *r = *(mp **)v[2].buf; + mp *c; + int ok = 1; + + c = mp_clearbit2c(MP_NEW, a, n); + if (!MP_EQ(c, r)) { + ok = 0; + fprintf(stderr, "\n***clrbit (set) failed"); + fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 16); + fprintf(stderr, "\n*** n = %lu", n); + fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 16); + fputs("\n*** c = ", stderr); mp_writefile(c, stderr, 16); + fputc('\n', stderr); + } + if (mp_testbit2c(r, n)) { + ok = 0; + fprintf(stderr, "\n***clrbit (test) failed"); + fprintf(stderr, "\n*** n = %lu", n); + fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 16); + fputc('\n', stderr); + } + mp_drop(a); + mp_drop(c); + mp_drop(r); + assert(mparena_count(MPARENA_GLOBAL) == 0); + return (ok); +} + +static int tneg(dstr *v) +{ + mp *a = *(mp **)v[0].buf; + mp *r = *(mp **)v[1].buf; + int ok = 1; + mp *n = mp_neg(MP_NEW, a); + if (!MP_EQ(r, n)) { + ok = 0; + fprintf(stderr, "\n*** neg failed\n"); + fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 10); + fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 10); + fputs("\n*** n = ", stderr); mp_writefile(n, stderr, 10); + fputc('\n', stderr); + } + mp_drop(n); + n = mp_neg(a, a); + if (!MP_EQ(r, n)) { + ok = 0; + fprintf(stderr, "\n*** neg failed\n"); + fputs("\n*** a* = ", stderr); mp_writefile(a, stderr, 10); + fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 10); + fputs("\n*** n = ", stderr); mp_writefile(n, stderr, 10); + fputc('\n', stderr); + } + mp_drop(a); + mp_drop(r); + assert(mparena_count(MPARENA_GLOBAL) == 0); + return (ok); +} + static int todd(dstr *v) { mp *a = *(mp **)v[0].buf; @@ -741,6 +897,7 @@ static int todd(dstr *v) mp_drop(a); mp_drop(rt); mp_drop(t); + assert(mparena_count(MPARENA_GLOBAL) == 0); return (ok); } @@ -749,12 +906,16 @@ static test_chunk tests[] = { { "lsr", tlsr, { &type_mp, &type_int, &type_mp, 0 } }, { "lsl2c", tlsl2c, { &type_mp, &type_int, &type_mp, 0 } }, { "lsr2c", tlsr2c, { &type_mp, &type_int, &type_mp, 0 } }, + { "setbit", tset, { &type_mp, &type_ulong, &type_mp, 0 } }, + { "clrbit", tclr, { &type_mp, &type_ulong, &type_mp, 0 } }, { "add", tadd, { &type_mp, &type_mp, &type_mp, 0 } }, { "sub", tsub, { &type_mp, &type_mp, &type_mp, 0 } }, { "mul", tmul, { &type_mp, &type_mp, &type_mp, 0 } }, { "div", tdiv, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } }, + { "exp", texp, { &type_mp, &type_mp, &type_mp, 0 } }, { "bin2c", tbin, { &type_string, &type_mp, &type_mp, &type_mp, 0 } }, { "odd", todd, { &type_mp, &type_uint32, &type_mp, 0 } }, + { "neg", tneg, { &type_mp, &type_mp, 0 } }, { 0, 0, { 0 } }, };