/* -*-c-*-
*
- * $Id: mp-arith.c,v 1.6 2000/06/17 11:45:09 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
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
a = MP_COPY(a);
b = MP_COPY(b);
- if (MP_LEN(a) <= KARATSUBA_CUTOFF || MP_LEN(b) <= KARATSUBA_CUTOFF) {
+ 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 += KARATSUBA_SLOP;
+ 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);
a = MP_COPY(a);
MP_DEST(d, 2 * m + 2, a->f | MP_UNDEF);
- if (m > KARATSUBA_CUTOFF) {
+ if (m > MPK_THRESH) {
mpw *s;
- m = 2 * (m + 1) + KARATSUBA_SLOP;
+ 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);
}
}
+/* --- @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 ----------------------------------------------------------*/
#ifdef 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);
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);
+}
+
static test_chunk tests[] = {
{ "lsl", tlsl, { &type_mp, &type_mp, &type_mp, 0 } },
{ "lsr", tlsr, { &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 } },
+ { "odd", todd, { &type_mp, &type_uint32, &type_mp, 0 } },
{ 0, 0, { 0 } },
};