5 * Montgomery Modular Arithmetic Functions.
7 * Copyright (C) Lenka Fibikova 2000
20 #define BN_mask_word(x, m) ((x->d[0]) & (m))
22 BN_MONTGOMERY *BN_mont_new()
26 ret=(BN_MONTGOMERY *)malloc(sizeof(BN_MONTGOMERY));
28 if (ret == NULL) return NULL;
30 if ((ret->p = BN_new()) == NULL)
40 void BN_mont_clear_free(BN_MONTGOMERY *mont)
42 if (mont == NULL) return;
44 if (mont->p != NULL) BN_clear_free(mont->p);
46 mont->p_num_bytes = 0;
48 mont->p_inv_b_neg = 0;
52 int BN_to_mont(BIGNUM *x, BN_MONTGOMERY *mont, BN_CTX *ctx)
57 assert(mont->p != NULL);
61 if (!BN_lshift(x, x, mont->R_num_bits)) return 0;
62 if (!BN_mod(x, x, mont->p, ctx)) return 0;
68 static BN_ULONG BN_mont_inv(BIGNUM *a, int e, BN_CTX *ctx)
69 /* y = a^{-1} (mod 2^e) for an odd number a */
71 BN_ULONG y, exp, mask;
72 BIGNUM *x, *xy, *x_sh;
75 assert(a != NULL && ctx != NULL);
76 assert(e <= BN_BITS2);
78 assert(!BN_is_zero(a) && !a->neg);
84 if((x = BN_dup(a)) == NULL) return 0;
85 if(!BN_mask_bits(x, e)) return 0;
89 x_sh = BN_CTX_get(ctx);
90 if (x_sh == NULL) goto err;
92 if (BN_copy(xy, x) == NULL) goto err;
93 if (!BN_lshift1(x_sh, x)) goto err;
96 for (i = 2; i <= e; i++)
98 if (exp < BN_mask_word(xy, mask))
101 if (!BN_add(xy, xy, x_sh)) goto err;
105 if (!BN_lshift1(x_sh, x_sh)) goto err;
112 if (xy->d[0] != 1) goto err;
115 if (x != NULL) BN_clear_free(x);
121 if (x != NULL) BN_clear_free(x);
127 int BN_mont_set(BIGNUM *p, BN_MONTGOMERY *mont, BN_CTX *ctx)
129 assert(p != NULL && ctx != NULL);
130 assert(mont != NULL);
131 assert(mont->p != NULL);
132 assert(!BN_is_zero(p) && !p->neg);
135 mont->p_num_bytes = p->top;
136 mont->R_num_bits = (mont->p_num_bytes) * BN_BITS2;
138 if (BN_copy(mont->p, p) == NULL);
140 mont->p_inv_b_neg = BN_mont_inv(p, BN_BITS2, ctx);
141 mont->p_inv_b_neg = 0 - mont->p_inv_b_neg;
148 #define cpy_mul_add(r, b, a, w, c) { \
150 t = (BN_ULLONG)w * (a) + (b) + (c); \
155 BN_ULONG BN_mul_add_rshift(BN_ULONG *r, BN_ULONG *a, int num, BN_ULONG w)
156 /* r = (r + a * w) >> BN_BITS2 */
160 mul_add(r[0], a[0], w, c);
161 if (--num == 0) return c;
166 cpy_mul_add(r[0], r[1], a[0], w, c);
167 if (--num == 0) break;
168 cpy_mul_add(r[1], r[2], a[1], w, c);
169 if (--num == 0) break;
170 cpy_mul_add(r[2], r[3], a[2], w, c);
171 if (--num == 0) break;
172 cpy_mul_add(r[3], r[4], a[3], w, c);
173 if (--num == 0) break;
182 #define cpy_mul_add(r, b, a, bl, bh, c) { \
188 mul64(l,h,(bl),(bh)); \
190 /* non-multiply part */ \
191 l=(l+(c))&BN_MASK2; if (l < (c)) h++; \
193 l=(l+(c))&BN_MASK2; if (l < (c)) h++; \
198 static BN_ULONG BN_mul_add_rshift(BN_ULONG *r, BN_ULONG *a, int num, BN_ULONG w)
199 /* ret = (ret + a * w) << shift * BN_BITS2 */
207 mul_add(r[0], a[0], bl, bh, c);
208 if (--num == 0) return c;
213 cpy_mul_add(r[0], r[1], a[0], bl, bh, c);
214 if (--num == 0) break;
215 cpy_mul_add(r[1], r[2], a[1], bl, bh, c);
216 if (--num == 0) break;
217 cpy_mul_add(r[2], r[3], a[2], bl, bh, c);
218 if (--num == 0) break;
219 cpy_mul_add(r[3], r[4], a[3], bl, bh, c);
220 if (--num == 0) break;
226 #endif /* BN_LLONG */
230 int BN_mont_red(BIGNUM *y, BN_MONTGOMERY *mont)
231 /* yR^{-1} (mod p) */
237 assert(y != NULL && mont != NULL);
238 assert(mont->p != NULL);
239 assert(BN_cmp(y, mont->p) < 0);
243 if (BN_is_zero(y)) return 1;
246 max = mont->p_num_bytes;
248 if (bn_wexpand(y, max) == NULL) return 0;
249 for (i = y->top; i < max; i++) y->d[i] = 0;
252 /* r = [r + (y_0 * p') * p] / b */
253 for (i = 0; i < max; i++)
255 c = BN_mul_add_rshift(y->d, p->d, max, ((y->d[0]) * mont->p_inv_b_neg) & BN_MASK2);
259 while (y->d[y->top - 1] == 0) y->top--;
261 if (BN_cmp(y, p) >= 0)
263 if (!BN_sub(y, y, p)) return 0;
270 int BN_mont_mod_mul(BIGNUM *r, BIGNUM *x, BIGNUM *y, BN_MONTGOMERY *mont)
271 /* r = x * y mod p */
272 /* r != x && r! = y !!! */
278 assert(r != x && r != y);
279 assert(r != NULL && x != NULL && y != NULL && mont != NULL);
280 assert(mont->p != NULL);
281 assert(BN_cmp(x, mont->p) < 0);
282 assert(BN_cmp(y, mont->p) < 0);
286 if (BN_is_zero(x) || BN_is_zero(y))
288 if (!BN_zero(r)) return 0;
293 max = mont->p_num_bytes;
295 /* for multiplication we need at most max + 2 words
296 the last one --- max + 3 --- is only as a backstop
299 if (bn_wexpand(r, max + 3) == NULL) return 0;
300 for (i = 0; i < max + 3; i++) r->d[i] = 0;
303 for (i = 0; i < x->top; i++)
305 /* r = r + (r_0 + x_i * y_0) * p' * p */
306 c = bn_mul_add_words(r->d, p->d, max, \
307 ((r->d[0] + x->d[i] * y->d[0]) * mont->p_inv_b_neg) & BN_MASK2);
310 if (((r->d[max] += c) & BN_MASK2) < c)
311 if (((r->d[max + 1] ++) & BN_MASK2) == 0) return 0;
314 /* r = (r + x_i * y) / b */
315 c = BN_mul_add_rshift(r->d, y->d, y->top, x->d[i]);
316 for(j = y->top; j <= max + 1; j++) r->d[j - 1] = r->d[j];
319 if (((r->d[y->top - 1] += c) & BN_MASK2) < c)
322 while (((++ (r->d[j]) ) & BN_MASK2) == 0)
324 if (j > max) return 0;
330 for (i = x->top; i < max; i++)
332 /* r = (r + r_0 * p' * p) / b */
333 c = BN_mul_add_rshift(r->d, p->d, max, ((r->d[0]) * mont->p_inv_b_neg) & BN_MASK2);
335 r->d[j] = c + r->d[max];
336 if (r->d[j++] < c) r->d[j] = r->d[++j] + 1;
337 else r->d[j] = r->d[++j];
341 while (r->d[r->top - 1] == 0) r->top--;
343 if (BN_cmp(r, mont->p) >= 0)
345 if (!BN_sub(r, r, mont->p)) return 0;