chiark / gitweb /
Use the generic exponentiation functions.
[catacomb] / mpbarrett.c
1 /* -*-c-*-
2  *
3  * $Id: mpbarrett.c,v 1.8 2001/06/16 13:00:20 mdw Exp $
4  *
5  * Barrett modular reduction
6  *
7  * (c) 1999 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------* 
11  *
12  * This file is part of Catacomb.
13  *
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.
18  * 
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.
23  * 
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,
27  * MA 02111-1307, USA.
28  */
29
30 /*----- Revision history --------------------------------------------------* 
31  *
32  * $Log: mpbarrett.c,v $
33  * Revision 1.8  2001/06/16 13:00:20  mdw
34  * Use the generic exponentiation functions.
35  *
36  * Revision 1.7  2001/04/19 18:25:26  mdw
37  * Use sliding-window exponentiation.
38  *
39  * Revision 1.6  2000/10/08 12:03:44  mdw
40  * (mpbarrett_reduce): Cope with negative numbers.
41  *
42  * Revision 1.5  2000/07/29 17:04:33  mdw
43  * Change to use left-to-right bitwise exponentiation.  This will improve
44  * performance when the base is small.
45  *
46  * Revision 1.4  2000/06/17 11:45:09  mdw
47  * Major memory management overhaul.  Added arena support.  Use the secure
48  * arena for secret integers.  Replace and improve the MP management macros
49  * (e.g., replace MP_MODIFY by MP_DEST).
50  *
51  * Revision 1.3  1999/12/12 15:08:52  mdw
52  * Don't bother shifting %$q$% in @mpbarrett_reduce@, just skip the least
53  * significant digits.
54  *
55  * Revision 1.2  1999/12/11 01:50:56  mdw
56  * Improve initialization slightly.
57  *
58  * Revision 1.1  1999/12/10 23:21:59  mdw
59  * Barrett reduction support: works with even moduli.
60  *
61  */
62
63 /*----- Header files ------------------------------------------------------*/
64
65 #include "mp.h"
66 #include "mpbarrett.h"
67 #include "mpbarrett-exp.h"
68
69 /*----- Main code ---------------------------------------------------------*/
70
71 /* --- @mpbarrett_create@ --- *
72  *
73  * Arguments:   @mpbarrett *mb@ = pointer to Barrett reduction context
74  *              @mp *m@ = modulus to work to
75  *
76  *
77  * Returns:     ---
78  *
79  * Use:         Initializes a Barrett reduction context ready for use.
80  */
81
82 void mpbarrett_create(mpbarrett *mb, mp *m)
83 {
84   mp *b;
85
86   /* --- Validate the arguments --- */
87
88   assert(((void)"Barrett modulus must be positive", (m->f & MP_NEG) == 0));
89
90   /* --- Compute %$\mu$% --- */
91
92   mp_shrink(m);
93   mb->k = MP_LEN(m);
94   mb->m = MP_COPY(m);
95   b = mp_new(2 * mb->k + 1, 0);
96   MPX_ZERO(b->v, b->vl - 1);
97   b->vl[-1] = 1;
98   mp_div(&b, 0, b, m);
99   mb->mu = b;
100 }
101
102 /* --- @mpbarrett_destroy@ --- *
103  *
104  * Arguments:   @mpbarrett *mb@ = pointer to Barrett reduction context
105  *
106  * Returns:     ---
107  *
108  * Use:         Destroys a Barrett reduction context releasing any resources
109  *              claimed.
110  */
111
112 void mpbarrett_destroy(mpbarrett *mb)
113 {
114   mp_drop(mb->m);
115   mp_drop(mb->mu);
116 }
117
118 /* --- @mpbarrett_reduce@ --- *
119  *
120  * Arguments:   @mpbarrett *mb@ = pointer to Barrett reduction context
121  *              @mp *d@ = destination for result
122  *              @mp *m@ = number to reduce
123  *
124  * Returns:     The residue of @m@ modulo the number in the reduction
125  *              context.
126  *
127  * Use:         Performs an efficient modular reduction.
128  */
129
130 mp *mpbarrett_reduce(mpbarrett *mb, mp *d, mp *m)
131 {
132   mp *q;
133   size_t k = mb->k;
134
135   /* --- Special case if @m@ is too small --- */
136
137   if (MP_LEN(m) < k) {
138     m = MP_COPY(m);
139     if (d)
140       MP_DROP(d);
141     return (m);
142   }
143
144   /* --- First stage --- */
145
146   {
147     mp qq;
148     mp_build(&qq, m->v + (k - 1), m->vl);
149     q = mp_mul(MP_NEW, &qq, mb->mu);
150     if (MP_LEN(q) <= k) {
151       m = MP_COPY(m);
152       if (d)
153         MP_DROP(d);
154       return (m);
155     }
156   }
157
158   /* --- Second stage --- */
159
160   {
161     mp *r;
162     mpw *mvl;
163
164     MP_COPY(m);
165     if (MP_LEN(m) <= k + 1)
166       mvl = m->vl;
167     else
168       mvl = m->v + k + 1;
169     r = mp_new(k + 1, (q->f | mb->m->f) & MP_BURN);
170     mpx_umul(r->v, r->vl, q->v + k + 1, q->vl, mb->m->v, mb->m->vl);
171     MP_DEST(d, k + 1, r->f);
172     mpx_usub(d->v, d->vl, m->v, mvl, r->v, r->vl);
173     d->f = (m->f | r->f) & (MP_BURN | MP_NEG);
174     MP_DROP(r);
175     MP_DROP(q);
176     MP_DROP(m);
177   }
178
179   /* --- Final stage --- */
180
181   MP_SHRINK(d);
182   while (MPX_UCMP(d->v, d->vl, >=, mb->m->v, mb->m->vl))
183     mpx_usub(d->v, d->vl, d->v, d->vl, mb->m->v, mb->m->vl);
184
185   /* --- Fix up the sign --- */
186
187   if (d->f & MP_NEG) {
188     mpx_usub(d->v, d->vl, mb->m->v, mb->m->vl, d->v, d->vl);
189     d->f &= ~MP_NEG;
190   }
191
192   MP_SHRINK(d);
193   return (d);
194 }
195
196 /* --- @mpbarrett_exp@ --- *
197  *
198  * Arguments:   @mpbarrett *mb@ = pointer to Barrett reduction context
199  *              @mp *d@ = fake destination
200  *              @mp *a@ = base
201  *              @mp *e@ = exponent
202  *
203  * Returns:     Result, %$a^e \bmod m$%.
204  */
205
206 mp *mpbarrett_exp(mpbarrett *mb, mp *d, mp *a, mp *e)
207 {
208   mp *x = MP_ONE;
209   mp *spare = (e->f & MP_BURN) ? MP_NEWSEC : MP_NEW;
210
211   MP_SHRINK(e);
212   if (!MP_LEN(e))
213     ;
214   else if (MP_LEN(e) < EXP_THRESH)
215     EXP_SIMPLE(x, a, e);
216   else
217     EXP_WINDOW(x, a, e);
218   mp_drop(d);
219   mp_drop(spare);
220   return (x);
221 }
222
223 /*----- Test rig ----------------------------------------------------------*/
224
225 #ifdef TEST_RIG
226
227 static int vmod(dstr *v)
228 {
229   mp *x = *(mp **)v[0].buf;
230   mp *n = *(mp **)v[1].buf;
231   mp *r = *(mp **)v[2].buf;
232   mp *s;
233   mpbarrett mb;
234   int ok = 1;
235
236   mpbarrett_create(&mb, n);
237   s = mpbarrett_reduce(&mb, MP_NEW, x);
238
239   if (!MP_EQ(s, r)) {
240     fputs("\n*** barrett reduction failure\n", stderr);
241     fputs("x = ", stderr); mp_writefile(x, stderr, 10); fputc('\n', stderr);
242     fputs("n = ", stderr); mp_writefile(n, stderr, 10); fputc('\n', stderr);
243     fputs("r = ", stderr); mp_writefile(r, stderr, 10); fputc('\n', stderr);
244     fputs("s = ", stderr); mp_writefile(s, stderr, 10); fputc('\n', stderr);
245     ok = 0;
246   }
247
248   mpbarrett_destroy(&mb);
249   mp_drop(x);
250   mp_drop(n);
251   mp_drop(r);
252   mp_drop(s);
253   assert(mparena_count(MPARENA_GLOBAL) == 0);
254   return (ok);
255 }
256
257 static int vexp(dstr *v)
258 {
259   mp *m = *(mp **)v[0].buf;
260   mp *a = *(mp **)v[1].buf;
261   mp *b = *(mp **)v[2].buf;
262   mp *r = *(mp **)v[3].buf;
263   mp *mr;
264   int ok = 1;
265
266   mpbarrett mb;
267   mpbarrett_create(&mb, m);
268
269   mr = mpbarrett_exp(&mb, MP_NEW, a, b);
270
271   if (!MP_EQ(mr, r)) {
272     fputs("\n*** barrett modexp failed", stderr);
273     fputs("\n m = ", stderr); mp_writefile(m, stderr, 10);
274     fputs("\n a = ", stderr); mp_writefile(a, stderr, 10);
275     fputs("\n e = ", stderr); mp_writefile(b, stderr, 10);
276     fputs("\n r = ", stderr); mp_writefile(r, stderr, 10);
277     fputs("\nmr = ", stderr); mp_writefile(mr, stderr, 10);
278     fputc('\n', stderr);
279     ok = 0;
280   }
281
282   mp_drop(m);
283   mp_drop(a);
284   mp_drop(b);
285   mp_drop(r);
286   mp_drop(mr);
287   mpbarrett_destroy(&mb);
288   assert(mparena_count(MPARENA_GLOBAL) == 0);
289   return ok;
290 }
291
292 static test_chunk tests[] = {
293   { "mpbarrett-reduce", vmod, { &type_mp, &type_mp, &type_mp, 0 } },
294   { "mpbarrett-exp", vexp, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } },
295   { 0, 0, { 0 } }
296 };
297
298 int main(int argc, char *argv[])
299 {
300   sub_init();
301   test_run(argc, argv, tests, SRCDIR "/tests/mpbarrett");
302   return (0);
303 }
304
305 #endif
306
307 /*----- That's all, folks -------------------------------------------------*/