21a7c4b1 |
1 | /* -*-c-*- |
21a7c4b1 |
2 | * |
3 | * Barrett modular reduction |
4 | * |
5 | * (c) 1999 Straylight/Edgeware |
6 | */ |
7 | |
45c0fd36 |
8 | /*----- Licensing notice --------------------------------------------------* |
21a7c4b1 |
9 | * |
10 | * This file is part of Catacomb. |
11 | * |
12 | * Catacomb is free software; you can redistribute it and/or modify |
13 | * it under the terms of the GNU Library General Public License as |
14 | * published by the Free Software Foundation; either version 2 of the |
15 | * License, or (at your option) any later version. |
45c0fd36 |
16 | * |
21a7c4b1 |
17 | * Catacomb is distributed in the hope that it will be useful, |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20 | * GNU Library General Public License for more details. |
45c0fd36 |
21 | * |
21a7c4b1 |
22 | * You should have received a copy of the GNU Library General Public |
23 | * License along with Catacomb; if not, write to the Free |
24 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
25 | * MA 02111-1307, USA. |
26 | */ |
27 | |
21a7c4b1 |
28 | /*----- Header files ------------------------------------------------------*/ |
29 | |
30 | #include "mp.h" |
31 | #include "mpbarrett.h" |
32 | |
33 | /*----- Main code ---------------------------------------------------------*/ |
34 | |
35 | /* --- @mpbarrett_create@ --- * |
36 | * |
37 | * Arguments: @mpbarrett *mb@ = pointer to Barrett reduction context |
38 | * @mp *m@ = modulus to work to |
39 | * |
40 | * |
f4535c64 |
41 | * Returns: Zero on success, nonzero on error. |
21a7c4b1 |
42 | * |
43 | * Use: Initializes a Barrett reduction context ready for use. |
44 | */ |
45 | |
f4535c64 |
46 | int mpbarrett_create(mpbarrett *mb, mp *m) |
21a7c4b1 |
47 | { |
48 | mp *b; |
49 | |
50 | /* --- Validate the arguments --- */ |
51 | |
f4535c64 |
52 | if (!MP_POSP(m)) |
53 | return (-1); |
21a7c4b1 |
54 | |
55 | /* --- Compute %$\mu$% --- */ |
56 | |
57 | mp_shrink(m); |
58 | mb->k = MP_LEN(m); |
59 | mb->m = MP_COPY(m); |
d34decd2 |
60 | b = mp_new(2 * mb->k + 1, 0); |
ed6d1bd2 |
61 | MPX_ZERO(b->v, b->vl - 1); |
62 | b->vl[-1] = 1; |
21a7c4b1 |
63 | mp_div(&b, 0, b, m); |
64 | mb->mu = b; |
f4535c64 |
65 | return (0); |
21a7c4b1 |
66 | } |
67 | |
68 | /* --- @mpbarrett_destroy@ --- * |
69 | * |
70 | * Arguments: @mpbarrett *mb@ = pointer to Barrett reduction context |
71 | * |
72 | * Returns: --- |
73 | * |
74 | * Use: Destroys a Barrett reduction context releasing any resources |
75 | * claimed. |
76 | */ |
77 | |
78 | void mpbarrett_destroy(mpbarrett *mb) |
79 | { |
80 | mp_drop(mb->m); |
81 | mp_drop(mb->mu); |
82 | } |
83 | |
84 | /* --- @mpbarrett_reduce@ --- * |
85 | * |
86 | * Arguments: @mpbarrett *mb@ = pointer to Barrett reduction context |
87 | * @mp *d@ = destination for result |
88 | * @mp *m@ = number to reduce |
89 | * |
90 | * Returns: The residue of @m@ modulo the number in the reduction |
91 | * context. |
92 | * |
30cbe7a7 |
93 | * Use: Performs an efficient modular reduction. |
21a7c4b1 |
94 | */ |
95 | |
96 | mp *mpbarrett_reduce(mpbarrett *mb, mp *d, mp *m) |
97 | { |
98 | mp *q; |
99 | size_t k = mb->k; |
100 | |
101 | /* --- Special case if @m@ is too small --- */ |
102 | |
103 | if (MP_LEN(m) < k) { |
104 | m = MP_COPY(m); |
d34decd2 |
105 | if (d) |
106 | MP_DROP(d); |
21a7c4b1 |
107 | return (m); |
108 | } |
109 | |
110 | /* --- First stage --- */ |
111 | |
112 | { |
113 | mp qq; |
114 | mp_build(&qq, m->v + (k - 1), m->vl); |
115 | q = mp_mul(MP_NEW, &qq, mb->mu); |
0a5d0bc7 |
116 | if (MP_LEN(q) <= k) { |
117 | m = MP_COPY(m); |
d34decd2 |
118 | if (d) |
119 | MP_DROP(d); |
ea85a558 |
120 | MP_DROP(q); |
0a5d0bc7 |
121 | return (m); |
122 | } |
21a7c4b1 |
123 | } |
124 | |
125 | /* --- Second stage --- */ |
126 | |
127 | { |
128 | mp *r; |
129 | mpw *mvl; |
130 | |
131 | MP_COPY(m); |
132 | if (MP_LEN(m) <= k + 1) |
133 | mvl = m->vl; |
134 | else |
135 | mvl = m->v + k + 1; |
d34decd2 |
136 | r = mp_new(k + 1, (q->f | mb->m->f) & MP_BURN); |
0a5d0bc7 |
137 | mpx_umul(r->v, r->vl, q->v + k + 1, q->vl, mb->m->v, mb->m->vl); |
4910e99c |
138 | MP_DEST(d, k + 1, r->f | MP_UNDEF); |
21a7c4b1 |
139 | mpx_usub(d->v, d->vl, m->v, mvl, r->v, r->vl); |
30cbe7a7 |
140 | d->f = (m->f | r->f) & (MP_BURN | MP_NEG); |
21a7c4b1 |
141 | MP_DROP(r); |
142 | MP_DROP(q); |
143 | MP_DROP(m); |
144 | } |
145 | |
146 | /* --- Final stage --- */ |
147 | |
148 | MP_SHRINK(d); |
149 | while (MPX_UCMP(d->v, d->vl, >=, mb->m->v, mb->m->vl)) |
150 | mpx_usub(d->v, d->vl, d->v, d->vl, mb->m->v, mb->m->vl); |
151 | |
30cbe7a7 |
152 | /* --- Fix up the sign --- */ |
153 | |
154 | if (d->f & MP_NEG) { |
155 | mpx_usub(d->v, d->vl, mb->m->v, mb->m->vl, d->v, d->vl); |
156 | d->f &= ~MP_NEG; |
157 | } |
158 | |
21a7c4b1 |
159 | MP_SHRINK(d); |
160 | return (d); |
161 | } |
162 | |
21a7c4b1 |
163 | /*----- Test rig ----------------------------------------------------------*/ |
164 | |
165 | #ifdef TEST_RIG |
166 | |
167 | static int vmod(dstr *v) |
168 | { |
169 | mp *x = *(mp **)v[0].buf; |
170 | mp *n = *(mp **)v[1].buf; |
171 | mp *r = *(mp **)v[2].buf; |
172 | mp *s; |
173 | mpbarrett mb; |
174 | int ok = 1; |
175 | |
176 | mpbarrett_create(&mb, n); |
177 | s = mpbarrett_reduce(&mb, MP_NEW, x); |
178 | |
30cbe7a7 |
179 | if (!MP_EQ(s, r)) { |
21a7c4b1 |
180 | fputs("\n*** barrett reduction failure\n", stderr); |
181 | fputs("x = ", stderr); mp_writefile(x, stderr, 10); fputc('\n', stderr); |
182 | fputs("n = ", stderr); mp_writefile(n, stderr, 10); fputc('\n', stderr); |
183 | fputs("r = ", stderr); mp_writefile(r, stderr, 10); fputc('\n', stderr); |
184 | fputs("s = ", stderr); mp_writefile(s, stderr, 10); fputc('\n', stderr); |
185 | ok = 0; |
186 | } |
187 | |
188 | mpbarrett_destroy(&mb); |
189 | mp_drop(x); |
190 | mp_drop(n); |
191 | mp_drop(r); |
192 | mp_drop(s); |
193 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
194 | return (ok); |
195 | } |
196 | |
21a7c4b1 |
197 | static test_chunk tests[] = { |
198 | { "mpbarrett-reduce", vmod, { &type_mp, &type_mp, &type_mp, 0 } }, |
21a7c4b1 |
199 | { 0, 0, { 0 } } |
200 | }; |
201 | |
202 | int main(int argc, char *argv[]) |
203 | { |
204 | sub_init(); |
0f00dc4c |
205 | test_run(argc, argv, tests, SRCDIR "/t/mpbarrett"); |
21a7c4b1 |
206 | return (0); |
207 | } |
208 | |
209 | #endif |
210 | |
211 | /*----- That's all, folks -------------------------------------------------*/ |