/* -*-c-*-
*
- * $Id: mp-arith.c,v 1.1 1999/11/17 18:02:16 mdw Exp $
+ * $Id: mp-arith.c,v 1.10 2001/04/03 19:36:05 mdw Exp $
*
* Basic arithmetic on multiprecision integers
*
/*----- Revision history --------------------------------------------------*
*
* $Log: mp-arith.c,v $
+ * 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.
*
#include "mp.h"
+/*----- Macros ------------------------------------------------------------*/
+
+#define MAX(x, y) ((x) >= (y) ? (x) : (y))
+
/*----- Main code ---------------------------------------------------------*/
/* --- @mp_2c@ --- *
if (!(a->f & MP_NEG))
return (MP_COPY(a));
- MP_MODIFY(d, MP_LEN(a));
+ MP_DEST(d, MP_LEN(a), a->f);
mpx_2c(d->v, d->vl, a->v, a->vl);
d->f = a->f & MP_BURN;
MP_SHRINK(d);
if (!MP_LEN(a) || a->vl[-1] < MPW_MAX / 2)
return (MP_COPY(a));
- MP_MODIFY(d, MP_LEN(a));
+ MP_DEST(d, MP_LEN(a), a->f);
mpx_2c(d->v, d->vl, a->v, a->vl);
d->f = (a->f & (MP_BURN | MP_NEG)) ^ MP_NEG;
MP_SHRINK(d);
/* --- @mp_lsl@ --- *
*
* Arguments: @mp *d@ = destination
- * @const mp *a@ = source
+ * @mp *a@ = source
* @size_t n@ = number of bits to move
*
* Returns: Result, @a@ shifted left by @n@.
*/
-mp *mp_lsl(mp *d, const mp *a, size_t n)
+mp *mp_lsl(mp *d, mp *a, size_t n)
{
- MP_MODIFY(d, MP_LEN(a) + (n + MPW_BITS - 1) / MPW_BITS);
+ MP_DEST(d, MP_LEN(a) + (n + MPW_BITS - 1) / MPW_BITS, a->f);
mpx_lsl(d->v, d->vl, a->v, a->vl, n);
d->f = a->f & (MP_NEG | MP_BURN);
MP_SHRINK(d);
/* --- @mp_lsr@ --- *
*
* Arguments: @mp *d@ = destination
- * @const mp *a@ = source
+ * @mp *a@ = source
* @size_t n@ = number of bits to move
*
* Returns: Result, @a@ shifted left by @n@.
*/
-mp *mp_lsr(mp *d, const mp *a, size_t n)
+mp *mp_lsr(mp *d, mp *a, size_t n)
{
- MP_MODIFY(d, MP_LEN(a));
+ MP_DEST(d, MP_LEN(a), a->f);
mpx_lsr(d->v, d->vl, a->v, a->vl, n);
d->f = a->f & (MP_NEG | MP_BURN);
MP_SHRINK(d);
return (d);
}
+/* --- @mp_eq@ --- *
+ *
+ * Arguments: @const mp *a, *b@ = two numbers
+ *
+ * Returns: Nonzero if the numbers are equal.
+ */
+
+int mp_eq(const mp *a, const mp *b) { return (MP_EQ(a, b)); }
+
/* --- @mp_cmp@ --- *
*
* Arguments: @const mp *a, *b@ = two numbers
return (+1);
}
+/* --- @mpx_and@, @mpx_or@, @mpx_xor@, @mpx_not@ --- *
+ *
+ * Arguments: @mp *d@ = destination
+ * @mp *a, *b@ = sources
+ *
+ * Returns: The result of the obvious bitwise operation.
+ */
+
+#define MP_BITBINOP(name) \
+ \
+mp *mp_##name(mp *d, mp *a, mp *b) \
+{ \
+ MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)), a->f | b->f); \
+ mpx_##name(d->v, d->vl, a->v, a->vl, b->v, b->vl); \
+ d->f = (a->f | b->f) & MP_BURN; \
+ MP_SHRINK(d); \
+ return (d); \
+}
+
+MP_BITBINOP(and)
+MP_BITBINOP(or)
+MP_BITBINOP(xor)
+
+mp *mp_not(mp *d, mp *a)
+{
+ MP_DEST(d, MP_LEN(a), a->f);
+ mpx_not(d->v, d->vl, a->v, a->vl);
+ d->f = a->f & MP_BURN;
+ MP_SHRINK(d);
+ return (d);
+}
+
/* --- @mp_add@ --- *
*
* Arguments: @mp *d@ = destination
- * @const mp *a, *b@ = sources
+ * @mp *a, *b@ = sources
*
* Returns: Result, @a@ added to @b@.
*/
-mp *mp_add(mp *d, const mp *a, const mp *b)
+mp *mp_add(mp *d, mp *a, mp *b)
{
- MP_MODIFY(d, (MP_LEN(a) > MP_LEN(b) ? MP_LEN(a) : MP_LEN(b)) + 1);
+ MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)) + 1, a->f | b->f);
if (!((a->f ^ b->f) & MP_NEG))
mpx_uadd(d->v, d->vl, a->v, a->vl, b->v, b->vl);
else {
if (MPX_UCMP(a->v, a->vl, <, b->v, b->vl)) {
- const mp *t = a; a = b; b = t;
+ mp *t = a; a = b; b = t;
}
mpx_usub(d->v, d->vl, a->v, a->vl, b->v, b->vl);
}
/* --- @mp_sub@ --- *
*
* Arguments: @mp *d@ = destination
- * @const mp *a, *b@ = sources
+ * @mp *a, *b@ = sources
*
* Returns: Result, @b@ subtracted from @a@.
*/
-mp *mp_sub(mp *d, const mp *a, const mp *b)
+mp *mp_sub(mp *d, mp *a, mp *b)
{
unsigned sgn = 0;
- MP_MODIFY(d, (MP_LEN(a) > MP_LEN(b) ? MP_LEN(a) : MP_LEN(b)) + 1);
+ MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)) + 1, a->f | b->f);
if ((a->f ^ b->f) & MP_NEG)
mpx_uadd(d->v, d->vl, a->v, a->vl, b->v, b->vl);
else {
if (MPX_UCMP(a->v, a->vl, <, b->v, b->vl)) {
- const mp *t = a; a = b; b = t;
+ mp *t = a; a = b; b = t;
sgn = MP_NEG;
}
mpx_usub(d->v, d->vl, a->v, a->vl, b->v, b->vl);
/* --- @mp_mul@ --- *
*
* Arguments: @mp *d@ = destination
- * @const mp *a, *b@ = sources
+ * @mp *a, *b@ = sources
*
* Returns: Result, @a@ multiplied by @b@.
*/
-mp *mp_mul(mp *d, const mp *a, const mp *b)
+mp *mp_mul(mp *d, mp *a, mp *b)
{
- if (d == a || d == b)
- d = MP_NEW;
- MP_MODIFY(d, MP_LEN(a) + MP_LEN(b));
- mpx_umul(d->v, d->vl, a->v, a->vl, b->v, b->vl);
+ a = MP_COPY(a);
+ b = MP_COPY(b);
+
+ if (MP_LEN(a) <= MPK_THRESH || MP_LEN(b) <= MPK_THRESH) {
+ MP_DEST(d, MP_LEN(a) + MP_LEN(b), a->f | b->f | MP_UNDEF);
+ mpx_umul(d->v, d->vl, a->v, a->vl, b->v, b->vl);
+ } else {
+ size_t m = 2 * MAX(MP_LEN(a), MP_LEN(b)) + 2;
+ mpw *s;
+ MP_DEST(d, m, a->f | b->f | MP_UNDEF);
+ m += MPK_SLOP;
+ s = mpalloc(d->a, m);
+ mpx_kmul(d->v, d->vl, a->v, a->vl, b->v, b->vl, s, s + m);
+ mpfree(d->a, s);
+ }
+
d->f = ((a->f | b->f) & MP_BURN) | ((a->f ^ b->f) & MP_NEG);
MP_SHRINK(d);
+ MP_DROP(a);
+ MP_DROP(b);
return (d);
}
/* --- @mp_sqr@ --- *
*
* Arguments: @mp *d@ = destination
- * @const mp *a@ = source
+ * @mp *a@ = source
*
* Returns: Result, @a@ squared.
*/
-mp *mp_sqr(mp *d, const mp *a)
+mp *mp_sqr(mp *d, mp *a)
{
- if (d == a)
- d = MP_NEW;
- MP_MODIFY(d, 2 * MP_LEN(a));
- mpx_usqr(d->v, d->vl, a->v, a->vl);
+ size_t m = MP_LEN(a);
+
+ a = MP_COPY(a);
+ MP_DEST(d, 2 * m + 2, a->f | MP_UNDEF);
+ if (m > MPK_THRESH) {
+ mpw *s;
+ m = 2 * (m + 1) + MPK_SLOP;
+ s = mpalloc(d->a, m);
+ mpx_ksqr(d->v, d->vl, a->v, a->vl, s, s + m);
+ mpfree(d->a, s);
+ } else
+ mpx_usqr(d->v, d->vl, a->v, a->vl);
d->f = a->f & MP_BURN;
MP_SHRINK(d);
+ MP_DROP(a);
return (d);
}
/* --- @mp_div@ --- *
*
* Arguments: @mp **qq, **rr@ = destination, quotient and remainder
- * @const mp *a, *b@ = sources
+ * @mp *a, *b@ = sources
*
* Use: Calculates the quotient and remainder when @a@ is divided by
* @b@. The destinations @*qq@ and @*rr@ must be distinct.
* straightforward.
*/
-void mp_div(mp **qq, mp **rr, const mp *a, const mp *b)
+void mp_div(mp **qq, mp **rr, mp *a, mp *b)
{
mp *r = rr ? *rr : MP_NEW;
mp *q = qq ? *qq : MP_NEW;
mpw *sv, *svl;
- /* --- Set up some temporary workspace --- */
-
- {
- size_t rq = MP_LEN(b) + 1;
- sv = MP_ALLOC(rq);
- svl = sv + rq;
- }
-
/* --- Set the remainder up right --- *
*
* Just in case the divisor is larger, be able to cope with this. It's not
* important in @mpx_udiv@, but it is here because of the sign correction.
*/
- {
- size_t rq = MP_LEN(a) + 2;
- if (MP_LEN(b) > rq)
- rq = MP_LEN(b);
-
- if (r == a) {
- MP_SPLIT(r);
- MP_ENSURE(r, MP_LEN(r) + 2);
- } else {
- if (r == b)
- r = MP_NEW;
- MP_MODIFY(r, MP_LEN(a) + 2);
- memcpy(r->v, a->v, MPWS(MP_LEN(a)));
- memset(r->v + MP_LEN(a), 0, MPWS(2));
- }
- }
+ b = MP_COPY(b);
+ a = MP_COPY(a);
+ if (r)
+ MP_DROP(r);
+ r = a;
+ MP_DEST(r, MP_LEN(a) + 2, a->f | b->f);
/* --- Fix up the quotient too --- */
- if (q == a || q == b)
- q = MP_NEW;
- MP_MODIFY(q, MP_LEN(a));
+ r = MP_COPY(r);
+ MP_DEST(q, MP_LEN(r), r->f | MP_UNDEF);
+ MP_DROP(r);
+
+ /* --- Set up some temporary workspace --- */
+
+ {
+ size_t rq = MP_LEN(b) + 1;
+ sv = mpalloc(r->a, rq);
+ svl = sv + rq;
+ }
/* --- Perform the calculation --- */
* remainder from @b@.
*/
- q->f = ((a->f | b->f) & MP_BURN) | ((a->f ^ b->f) & MP_NEG);
+ q->f = ((r->f | b->f) & MP_BURN) | ((r->f ^ b->f) & MP_NEG);
if (q->f & MP_NEG) {
- mpw *v = r->v;
- while (v < r->vl) {
+ mpw *v;
+ for (v = r->v; v < r->vl; v++) {
if (*v) {
MPX_UADDN(q->v, q->vl, 1);
mpx_usub(r->v, r->vl, b->v, b->vl, r->v, r->vl);
}
}
- r->f = ((a->f | b->f) & MP_BURN) | (b->f & MP_NEG);
+ r->f = ((r->f | b->f) & MP_BURN) | (b->f & MP_NEG);
/* --- Store the return values --- */
+ mpfree(r->a, sv);
+ MP_DROP(b);
+
if (!qq)
MP_DROP(q);
else {
MP_SHRINK(r);
*rr = r;
}
+}
- MP_FREE(sv);
+/* --- @mp_odd@ --- *
+ *
+ * Arguments: @mp *d@ = pointer to destination integer
+ * @mp *m@ = pointer to source integer
+ * @size_t *s@ = where to store the power of 2
+ *
+ * Returns: An odd integer integer %$t$% such that %$m = 2^s t$%.
+ *
+ * Use: Computes a power of two and an odd integer which, when
+ * multiplied, give a specified result. This sort of thing is
+ * useful in number theory quite often.
+ */
+
+mp *mp_odd(mp *d, mp *m, size_t *s)
+{
+ size_t ss = 0;
+ const mpw *v, *vl;
+
+ v = m->v;
+ vl = m->vl;
+ for (; !*v && v < vl; v++)
+ ss += MPW_BITS;
+ if (v >= vl)
+ ss = 0;
+ else {
+ mpw x = *v;
+ mpw mask = MPW_MAX;
+ unsigned z = MPW_BITS / 2;
+
+ while (z) {
+ mask >>= z;
+ if (!(x & mask)) {
+ x >>= z;
+ ss += z;
+ }
+ z >>= 1;
+ }
+ }
+
+ *s = ss;
+ return (mp_lsr(d, m, ss));
}
/*----- Test rig ----------------------------------------------------------*/
static int verify(const char *op, mp *expect, mp *result, mp *a, mp *b)
{
- if (MP_CMP(expect, !=, result)) {
+ 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);
}
#define RIG(name, op) \
- static int t ## name(dstr *v) \
+ static int t##name(dstr *v) \
{ \
mp *a = *(mp **)v[0].buf; \
mpw n = *(int *)v[1].buf; \
mp_build(&b, &n, &n + 1); \
ok = verify(#name, r, c, a, &b); \
mp_drop(a); mp_drop(c); mp_drop(r); \
+ assert(mparena_count(MPARENA_GLOBAL) == 0); \
return (ok); \
}
#undef RIG
#define RIG(name, op) \
- static int t ## name(dstr *v) \
+ static int t##name(dstr *v) \
{ \
mp *a = *(mp **)v[0].buf; \
mp *b = *(mp **)v[1].buf; \
mp *c = op(MP_NEW, a, b); \
int ok = verify(#name, r, c, a, b); \
mp_drop(a); mp_drop(b); mp_drop(c); mp_drop(r); \
+ assert(mparena_count(MPARENA_GLOBAL) == 0); \
return (ok); \
}
ok &= verify("div(quotient)", q, c, a, b);
ok &= verify("div(remainder)", r, d, a, b);
mp_drop(a); mp_drop(b); mp_drop(c); mp_drop(d); mp_drop(r); mp_drop(q);
+ assert(mparena_count(MPARENA_GLOBAL) == 0);
+ return (ok);
+}
+
+static int todd(dstr *v)
+{
+ mp *a = *(mp **)v[0].buf;
+ size_t rs = *(uint32 *)v[1].buf;
+ mp *rt = *(mp **)v[2].buf;
+ int ok = 1;
+ mp *t;
+ size_t s;
+ t = mp_odd(MP_NEW, a, &s);
+ if (s != rs || !MP_EQ(t, rt)) {
+ ok = 0;
+ fprintf(stderr, "\n*** odd failed");
+ fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 10);
+ fprintf(stderr, "\n*** s = %lu", (unsigned long)s);
+ fputs("\n*** t = ", stderr); mp_writefile(t, stderr, 10);
+ fprintf(stderr, "\n*** rs = %lu", (unsigned long)rs);
+ fputs("\n*** rt = ", stderr); mp_writefile(rt, stderr, 10);
+ fputc('\n', stderr);
+ }
+ mp_drop(a);
+ mp_drop(rt);
+ mp_drop(t);
return (ok);
}
{ "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 } },
+ { "odd", todd, { &type_mp, &type_uint32, &type_mp, 0 } },
{ 0, 0, { 0 } },
};