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