From f1713c638e2bcef604f70193818a9fd40f3f1a2f Mon Sep 17 00:00:00 2001 From: mdw Date: Thu, 22 Jun 2000 19:02:53 +0000 Subject: [PATCH] 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. --- mp-arith.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git a/mp-arith.c b/mp-arith.c index fe6a45d..c67fdd8 100644 --- a/mp-arith.c +++ b/mp-arith.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: mp-arith.c,v 1.6 2000/06/17 11:45:09 mdw Exp $ + * $Id: mp-arith.c,v 1.7 2000/06/22 19:02:53 mdw Exp $ * * Basic arithmetic on multiprecision integers * @@ -30,6 +30,11 @@ /*----- Revision history --------------------------------------------------* * * $Log: mp-arith.c,v $ + * 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 @@ -364,6 +369,49 @@ void mp_div(mp **qq, mp **rr, mp *a, mp *b) } } +/* --- @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 @@ -438,6 +486,31 @@ static int tdiv(dstr *v) 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_CMP(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 } }, @@ -445,6 +518,7 @@ static test_chunk tests[] = { { "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 } }, }; -- 2.11.0