5 * Montgomery Modular Arithmetic Functions.
7 * Copyright (C) Lenka Fibikova 2000
21 #define BN_mask_word(x, m) ((x->d[0]) & (m))
23 BN_MONTGOMERY *BN_mont_new()
27 ret=(BN_MONTGOMERY *)malloc(sizeof(BN_MONTGOMERY));
29 if (ret == NULL) return NULL;
31 if ((ret->p = BN_new()) == NULL)
41 void BN_mont_clear_free(BN_MONTGOMERY *mont)
43 if (mont == NULL) return;
45 if (mont->p != NULL) BN_clear_free(mont->p);
47 mont->p_num_bytes = 0;
49 mont->p_inv_b_neg = 0;
53 int BN_to_mont(BIGNUM *x, BN_MONTGOMERY *mont, BN_CTX *ctx)
58 assert(mont->p != NULL);
62 if (!BN_lshift(x, x, mont->R_num_bits)) return 0;
63 if (!BN_mod(x, x, mont->p, ctx)) return 0;
69 static BN_ULONG BN_mont_inv(BIGNUM *a, int e, BN_CTX *ctx)
70 /* y = a^{-1} (mod 2^e) for an odd number a */
72 BN_ULONG y, exp, mask;
73 BIGNUM *x, *xy, *x_sh;
76 assert(a != NULL && ctx != NULL);
77 assert(e <= BN_BITS2);
79 assert(!BN_is_zero(a) && !a->neg);
85 if((x = BN_dup(a)) == NULL) return 0;
86 if(!BN_mask_bits(x, e)) return 0;
90 x_sh = BN_CTX_get(ctx);
91 if (x_sh == NULL) goto err;
93 if (BN_copy(xy, x) == NULL) goto err;
94 if (!BN_lshift1(x_sh, x)) goto err;
97 for (i = 2; i <= e; i++)
99 if (exp < BN_mask_word(xy, mask))
102 if (!BN_add(xy, xy, x_sh)) goto err;
106 if (!BN_lshift1(x_sh, x_sh)) goto err;
113 if (xy->d[0] != 1) goto err;
116 if (x != NULL) BN_clear_free(x);
122 if (x != NULL) BN_clear_free(x);
128 int BN_mont_set(BIGNUM *p, BN_MONTGOMERY *mont, BN_CTX *ctx)
130 assert(p != NULL && ctx != NULL);
131 assert(mont != NULL);
132 assert(mont->p != NULL);
133 assert(!BN_is_zero(p) && !p->neg);
136 mont->p_num_bytes = p->top;
137 mont->R_num_bits = (mont->p_num_bytes) * BN_BITS2;
139 if (BN_copy(mont->p, p) == NULL);
141 mont->p_inv_b_neg = BN_mont_inv(p, BN_BITS2, ctx);
142 mont->p_inv_b_neg = 0 - mont->p_inv_b_neg;
148 static int BN_cpy_mul_word(BIGNUM *ret, BIGNUM *a, BN_ULONG w)
151 if (BN_copy(ret, a) == NULL) return 0;
153 if (!BN_mul_word(ret, w)) return 0;
159 int BN_mont_red(BIGNUM *y, BN_MONTGOMERY *mont, BN_CTX *ctx)
160 /* yR^{-1} (mod p) */
166 assert(y != NULL && mont != NULL && ctx != NULL);
167 assert(mont->p != NULL);
168 assert(BN_cmp(y, mont->p) < 0);
172 if (BN_is_zero(y)) return 1;
177 up = BN_CTX_get(ctx);
178 if (up == NULL) goto err;
180 for (i = 0; i < mont->p_num_bytes; i++)
182 u = (y->d[0]) * mont->p_inv_b_neg; /* u = y_0 * p' */
184 if (!BN_cpy_mul_word(up, p, u)) goto err; /* up = u * p */
186 if (!BN_add(y, y, up)) goto err;
188 if (y->d[0]) goto err;
190 if (!BN_rshift(y, y, BN_BITS2)) goto err; /* y = (y + up)/b */
194 if (BN_cmp(y, mont->p) >= 0)
196 if (!BN_sub(y, y, mont->p)) goto err;
208 int BN_mont_mod_mul(BIGNUM *r, BIGNUM *x, BIGNUM *y, BN_MONTGOMERY *mont, BN_CTX *ctx)
209 /* r = x * y mod p */
210 /* r != x && r! = y !!! */
217 assert(r != x && r != y);
218 assert(r != NULL && x != NULL && y != NULL && mont != NULL && ctx != NULL);
219 assert(mont->p != NULL);
220 assert(BN_cmp(x, mont->p) < 0);
221 assert(BN_cmp(y, mont->p) < 0);
225 if (BN_is_zero(x) || BN_is_zero(y))
227 if (!BN_zero(r)) return 0;
234 xiy = BN_CTX_get(ctx);
235 up = BN_CTX_get(ctx);
236 if (up == NULL) goto err;
238 if (!BN_zero(r)) goto err;
240 for (i = 0; i < x->top; i++)
242 u = (r->d[0] + x->d[i] * y->d[0]) * mont->p_inv_b_neg;
244 if (!BN_cpy_mul_word(xiy, y, x->d[i])) goto err;
245 if (!BN_cpy_mul_word(up, mont->p, u)) goto err;
247 if (!BN_add(r, r, xiy)) goto err;
248 if (!BN_add(r, r, up)) goto err;
251 if (r->d[0]) goto err;
253 if (!BN_rshift(r, r, BN_BITS2)) goto err;
256 for (i = x->top; i < mont->p_num_bytes; i++)
258 u = (r->d[0]) * mont->p_inv_b_neg;
260 if (!BN_cpy_mul_word(up, mont->p, u)) goto err;
262 if (!BN_add(r, r, up)) goto err;
265 if (r->d[0]) goto err;
267 if (!BN_rshift(r, r, BN_BITS2)) goto err;
271 if (BN_cmp(r, mont->p) >= 0)
273 if (!BN_sub(r, r, mont->p)) goto err;