3 * $Id: mpbarrett.c,v 1.4 2000/06/17 11:45:09 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.4 2000/06/17 11:45:09 mdw
34 * Major memory management overhaul. Added arena support. Use the secure
35 * arena for secret integers. Replace and improve the MP management macros
36 * (e.g., replace MP_MODIFY by MP_DEST).
38 * Revision 1.3 1999/12/12 15:08:52 mdw
39 * Don't bother shifting %$q$% in @mpbarrett_reduce@, just skip the least
42 * Revision 1.2 1999/12/11 01:50:56 mdw
43 * Improve initialization slightly.
45 * Revision 1.1 1999/12/10 23:21:59 mdw
46 * Barrett reduction support: works with even moduli.
50 /*----- Header files ------------------------------------------------------*/
53 #include "mpbarrett.h"
55 /*----- Main code ---------------------------------------------------------*/
57 /* --- @mpbarrett_create@ --- *
59 * Arguments: @mpbarrett *mb@ = pointer to Barrett reduction context
60 * @mp *m@ = modulus to work to
65 * Use: Initializes a Barrett reduction context ready for use.
68 void mpbarrett_create(mpbarrett *mb, mp *m)
72 /* --- Validate the arguments --- */
74 assert(((void)"Barrett modulus must be positive", (m->f & MP_NEG) == 0));
76 /* --- Compute %$\mu$% --- */
81 b = mp_new(2 * mb->k + 1, 0);
82 MPX_ZERO(b->v, b->vl - 1);
88 /* --- @mpbarrett_destroy@ --- *
90 * Arguments: @mpbarrett *mb@ = pointer to Barrett reduction context
94 * Use: Destroys a Barrett reduction context releasing any resources
98 void mpbarrett_destroy(mpbarrett *mb)
104 /* --- @mpbarrett_reduce@ --- *
106 * Arguments: @mpbarrett *mb@ = pointer to Barrett reduction context
107 * @mp *d@ = destination for result
108 * @mp *m@ = number to reduce
110 * Returns: The residue of @m@ modulo the number in the reduction
113 * Use: Performs an efficient modular reduction. The argument is
114 * assumed to be positive.
117 mp *mpbarrett_reduce(mpbarrett *mb, mp *d, mp *m)
122 /* --- Special case if @m@ is too small --- */
131 /* --- First stage --- */
135 mp_build(&qq, m->v + (k - 1), m->vl);
136 q = mp_mul(MP_NEW, &qq, mb->mu);
137 if (MP_LEN(q) <= k) {
145 /* --- Second stage --- */
152 if (MP_LEN(m) <= k + 1)
156 r = mp_new(k + 1, (q->f | mb->m->f) & MP_BURN);
157 mpx_umul(r->v, r->vl, q->v + k + 1, q->vl, mb->m->v, mb->m->vl);
158 MP_DEST(d, k + 1, r->f);
159 mpx_usub(d->v, d->vl, m->v, mvl, r->v, r->vl);
160 d->f = (m->f | r->f) & MP_BURN;
166 /* --- Final stage --- */
169 while (MPX_UCMP(d->v, d->vl, >=, mb->m->v, mb->m->vl))
170 mpx_usub(d->v, d->vl, d->v, d->vl, mb->m->v, mb->m->vl);
176 /* --- @mpbarrett_exp@ --- *
178 * Arguments: @mpbarrett *mb@ = pointer to Barrett reduction context
179 * @mp *d@ = fake destination
183 * Returns: Result, %$a^e \bmod m$%.
186 mp *mpbarrett_exp(mpbarrett *mb, mp *d, mp *a, mp *e)
202 dd = mp_sqr(spare, a);
203 dd = mpbarrett_reduce(mb, dd, dd);
207 dd = mp_mul(spare, x, a);
208 dd = mpbarrett_reduce(mb, dd, dd);
225 /*----- Test rig ----------------------------------------------------------*/
229 static int vmod(dstr *v)
231 mp *x = *(mp **)v[0].buf;
232 mp *n = *(mp **)v[1].buf;
233 mp *r = *(mp **)v[2].buf;
238 mpbarrett_create(&mb, n);
239 s = mpbarrett_reduce(&mb, MP_NEW, x);
241 if (MP_CMP(s, !=, r)) {
242 fputs("\n*** barrett reduction failure\n", stderr);
243 fputs("x = ", stderr); mp_writefile(x, stderr, 10); fputc('\n', stderr);
244 fputs("n = ", stderr); mp_writefile(n, stderr, 10); fputc('\n', stderr);
245 fputs("r = ", stderr); mp_writefile(r, stderr, 10); fputc('\n', stderr);
246 fputs("s = ", stderr); mp_writefile(s, stderr, 10); fputc('\n', stderr);
250 mpbarrett_destroy(&mb);
255 assert(mparena_count(MPARENA_GLOBAL) == 0);
259 static int vexp(dstr *v)
261 mp *m = *(mp **)v[0].buf;
262 mp *a = *(mp **)v[1].buf;
263 mp *b = *(mp **)v[2].buf;
264 mp *r = *(mp **)v[3].buf;
269 mpbarrett_create(&mb, m);
271 mr = mpbarrett_exp(&mb, MP_NEW, a, b);
273 if (MP_CMP(mr, !=, r)) {
274 fputs("\n*** barrett modexp failed", stderr);
275 fputs("\n m = ", stderr); mp_writefile(m, stderr, 10);
276 fputs("\n a = ", stderr); mp_writefile(a, stderr, 10);
277 fputs("\n e = ", stderr); mp_writefile(b, stderr, 10);
278 fputs("\n r = ", stderr); mp_writefile(r, stderr, 10);
279 fputs("\nmr = ", stderr); mp_writefile(mr, stderr, 10);
289 mpbarrett_destroy(&mb);
290 assert(mparena_count(MPARENA_GLOBAL) == 0);
294 static test_chunk tests[] = {
295 { "mpbarrett-reduce", vmod, { &type_mp, &type_mp, &type_mp, 0 } },
296 { "mpbarrett-exp", vexp, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } },
300 int main(int argc, char *argv[])
303 test_run(argc, argv, tests, SRCDIR "/tests/mpbarrett");
309 /*----- That's all, folks -------------------------------------------------*/