3 * $Id: mpbarrett.c,v 1.3 1999/12/12 15:08:52 mdw Exp $
5 * Barrett modular reduction
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
32 * $Log: mpbarrett.c,v $
33 * Revision 1.3 1999/12/12 15:08:52 mdw
34 * Don't bother shifting %$q$% in @mpbarrett_reduce@, just skip the least
37 * Revision 1.2 1999/12/11 01:50:56 mdw
38 * Improve initialization slightly.
40 * Revision 1.1 1999/12/10 23:21:59 mdw
41 * Barrett reduction support: works with even moduli.
45 /*----- Header files ------------------------------------------------------*/
48 #include "mpbarrett.h"
50 /*----- Main code ---------------------------------------------------------*/
52 /* --- @mpbarrett_create@ --- *
54 * Arguments: @mpbarrett *mb@ = pointer to Barrett reduction context
55 * @mp *m@ = modulus to work to
60 * Use: Initializes a Barrett reduction context ready for use.
63 void mpbarrett_create(mpbarrett *mb, mp *m)
67 /* --- Validate the arguments --- */
69 assert(((void)"Barrett modulus must be positive", (m->f & MP_NEG) == 0));
71 /* --- Compute %$\mu$% --- */
76 b = mp_create(2 * mb->k + 1);
77 MPX_ZERO(b->v, b->vl - 1);
83 /* --- @mpbarrett_destroy@ --- *
85 * Arguments: @mpbarrett *mb@ = pointer to Barrett reduction context
89 * Use: Destroys a Barrett reduction context releasing any resources
93 void mpbarrett_destroy(mpbarrett *mb)
99 /* --- @mpbarrett_reduce@ --- *
101 * Arguments: @mpbarrett *mb@ = pointer to Barrett reduction context
102 * @mp *d@ = destination for result
103 * @mp *m@ = number to reduce
105 * Returns: The residue of @m@ modulo the number in the reduction
108 * Use: Performs an efficient modular reduction. The argument is
109 * assumed to be positive.
112 mp *mpbarrett_reduce(mpbarrett *mb, mp *d, mp *m)
117 /* --- Special case if @m@ is too small --- */
125 /* --- First stage --- */
129 mp_build(&qq, m->v + (k - 1), m->vl);
130 q = mp_mul(MP_NEW, &qq, mb->mu);
131 if (MP_LEN(q) <= k) {
138 /* --- Second stage --- */
145 if (MP_LEN(m) <= k + 1)
149 r = mp_create(k + 1);
150 mpx_umul(r->v, r->vl, q->v + k + 1, q->vl, mb->m->v, mb->m->vl);
151 r->f = (q->f | mb->m->f) & MP_BURN;
153 mpx_usub(d->v, d->vl, m->v, mvl, r->v, r->vl);
154 d->f = (m->f | r->f) & MP_BURN;
160 /* --- Final stage --- */
163 while (MPX_UCMP(d->v, d->vl, >=, mb->m->v, mb->m->vl))
164 mpx_usub(d->v, d->vl, d->v, d->vl, mb->m->v, mb->m->vl);
170 /* --- @mpbarrett_exp@ --- *
172 * Arguments: @mpbarrett *mb@ = pointer to Barrett reduction context
173 * @mp *d@ = fake destination
177 * Returns: Result, %$a^e \bmod m$%.
180 mp *mpbarrett_exp(mpbarrett *mb, mp *d, mp *a, mp *e)
194 dd = mp_sqr(spare, a);
195 dd = mpbarrett_reduce(mb, dd, dd);
199 dd = mp_mul(spare, x, a);
200 dd = mpbarrett_reduce(mb, dd, dd);
217 /*----- Test rig ----------------------------------------------------------*/
221 static int vmod(dstr *v)
223 mp *x = *(mp **)v[0].buf;
224 mp *n = *(mp **)v[1].buf;
225 mp *r = *(mp **)v[2].buf;
230 mpbarrett_create(&mb, n);
231 s = mpbarrett_reduce(&mb, MP_NEW, x);
233 if (MP_CMP(s, !=, r)) {
234 fputs("\n*** barrett reduction failure\n", stderr);
235 fputs("x = ", stderr); mp_writefile(x, stderr, 10); fputc('\n', stderr);
236 fputs("n = ", stderr); mp_writefile(n, stderr, 10); fputc('\n', stderr);
237 fputs("r = ", stderr); mp_writefile(r, stderr, 10); fputc('\n', stderr);
238 fputs("s = ", stderr); mp_writefile(s, stderr, 10); fputc('\n', stderr);
242 mpbarrett_destroy(&mb);
247 assert(mparena_count(MPARENA_GLOBAL) == 0);
251 static int vexp(dstr *v)
253 mp *m = *(mp **)v[0].buf;
254 mp *a = *(mp **)v[1].buf;
255 mp *b = *(mp **)v[2].buf;
256 mp *r = *(mp **)v[3].buf;
261 mpbarrett_create(&mb, m);
263 mr = mpbarrett_exp(&mb, MP_NEW, a, b);
265 if (MP_CMP(mr, !=, r)) {
266 fputs("\n*** barrett modexp failed", stderr);
267 fputs("\n m = ", stderr); mp_writefile(m, stderr, 10);
268 fputs("\n a = ", stderr); mp_writefile(a, stderr, 10);
269 fputs("\n e = ", stderr); mp_writefile(b, stderr, 10);
270 fputs("\n r = ", stderr); mp_writefile(r, stderr, 10);
271 fputs("\nmr = ", stderr); mp_writefile(mr, stderr, 10);
281 mpbarrett_destroy(&mb);
282 assert(mparena_count(MPARENA_GLOBAL) == 0);
286 static test_chunk tests[] = {
287 { "mpbarrett-reduce", vmod, { &type_mp, &type_mp, &type_mp, 0 } },
288 { "mpbarrett-exp", vexp, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } },
292 int main(int argc, char *argv[])
295 test_run(argc, argv, tests, SRCDIR "/tests/mpbarrett");
301 /*----- That's all, folks -------------------------------------------------*/