-
-int BN_mont_mod_add(BIGNUM *r, BIGNUM *x, BIGNUM *y, BN_MONTGOMERY *mont)
-{
- assert(r != NULL && x != NULL && y != NULL && mont != NULL);
- assert(mont->p != NULL);
- assert(BN_cmp(x, mont->p) < 0);
- assert(BN_cmp(y, mont->p) < 0);
- assert(!x->neg);
- assert(!y->neg);
-
- if (!BN_add(r, x, y)) return 0;
- if (BN_cmp(r, mont->p) >= 0)
- {
- if (!BN_sub(r, r, mont->p)) return 0;
- }
-
- return 1;
-}
-
-
-int BN_mont_mod_sub(BIGNUM *r, BIGNUM *x, BIGNUM *y, BN_MONTGOMERY *mont)
-{
- assert(r != NULL && x != NULL && y != NULL && mont != NULL);
- assert(mont->p != NULL);
- assert(BN_cmp(x, mont->p) < 0);
- assert(BN_cmp(y, mont->p) < 0);
- assert(!x->neg);
- assert(!y->neg);
-
- if (!BN_sub(r, x, y)) return 0;
- if (r->neg)
- {
- if (!BN_add(r, r, mont->p)) return 0;
- }
-
- return 1;
-}
-
-int BN_mont_mod_lshift1(BIGNUM *r, BIGNUM *x, BN_MONTGOMERY *mont)
-{
- assert(r != NULL && x != NULL && mont != NULL);
- assert(mont->p != NULL);
- assert(BN_cmp(x, mont->p) < 0);
- assert(!x->neg);
-
- if (!BN_lshift1(r, x)) return 0;
-
- if (BN_cmp(r, mont->p) >= 0)
- {
- if (!BN_sub(r, r, mont->p)) return 0;
- }
-
- return 1;
-}
-
-int BN_mont_mod_lshift(BIGNUM *r, BIGNUM *x, int n, BN_MONTGOMERY *mont)
-{
- int sh_nb;
-
- assert(r != NULL && x != NULL && mont != NULL);
- assert(mont->p != NULL);
- assert(BN_cmp(x, mont->p) < 0);
- assert(!x->neg);
- assert(n > 0);
-
- if (r != x)
- {
- if (BN_copy(r, x) == NULL) return 0;
- }
-
- while (n)
- {
- sh_nb = BN_num_bits(mont->p) - BN_num_bits(r);
- if (sh_nb > n) sh_nb = n;
-
- if (sh_nb)
- {
- if(!BN_lshift(r, r, sh_nb)) return 0;
- }
- else
- {
- sh_nb = 1;
- if (!BN_lshift1(r, r)) return 0;
- }
-
- if (BN_cmp(r, mont->p) >= 0)
- {
- if (!BN_sub(r, r, mont->p)) return 0;
- }
-
- n -= sh_nb;
- }
-
- return 1;
-}