BN_ULONG tab[16], top3b = a >> 61;
register BN_ULONG a1, a2, a4, a8;
- a1 = a & (0x1FFFFFFFFFFFFFFF); a2 = a1 << 1; a4 = a2 << 1; a8 = a4 << 1;
+ a1 = a & (0x1FFFFFFFFFFFFFFFULL); a2 = a1 << 1; a4 = a2 << 1; a8 = a4 << 1;
tab[ 0] = 0; tab[ 1] = a1; tab[ 2] = a2; tab[ 3] = a1^a2;
tab[ 4] = a4; tab[ 5] = a1^a4; tab[ 6] = a2^a4; tab[ 7] = a1^a2^a4;
int i;
const BIGNUM *at, *bt;
+ bn_check_top(a);
+ bn_check_top(b);
+
if (a->top < b->top) { at = b; bt = a; }
else { at = a; bt = b; }
int j, k;
int n, dN, d0, d1;
BN_ULONG zz, *z;
-
+
+ bn_check_top(a);
+
if (!p[0])
+ {
/* reduction mod 1 => return 0 */
- return BN_zero(r);
+ BN_zero(r);
+ return 1;
+ }
/* Since the algorithm does reduction in the r value, if a != r, copy
* the contents of a into r so we can do reduction in r.
}
bn_correct_top(r);
-
return 1;
}
*/
int BN_GF2m_mod(BIGNUM *r, const BIGNUM *a, const BIGNUM *p)
{
+ int ret = 0;
const int max = BN_num_bits(p);
- unsigned int *arr=NULL, ret = 0;
+ unsigned int *arr=NULL;
+ bn_check_top(a);
+ bn_check_top(p);
if ((arr = (unsigned int *)OPENSSL_malloc(sizeof(unsigned int) * max)) == NULL) goto err;
ret = BN_GF2m_poly2arr(p, arr, max);
if (!ret || ret > max)
}
ret = BN_GF2m_mod_arr(r, a, arr);
bn_check_top(r);
- err:
+err:
if (arr) OPENSSL_free(arr);
return ret;
}
int zlen, i, j, k, ret = 0;
BIGNUM *s;
BN_ULONG x1, x0, y1, y0, zz[4];
-
+
+ bn_check_top(a);
+ bn_check_top(b);
+
if (a == b)
{
return BN_GF2m_mod_sqr_arr(r, a, p, ctx);
}
-
BN_CTX_start(ctx);
if ((s = BN_CTX_get(ctx)) == NULL) goto err;
ret = 1;
bn_check_top(r);
- err:
+err:
BN_CTX_end(ctx);
return ret;
-
}
/* Compute the product of two polynomials a and b, reduce modulo p, and store
*/
int BN_GF2m_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *p, BN_CTX *ctx)
{
+ int ret = 0;
const int max = BN_num_bits(p);
- unsigned int *arr=NULL, ret = 0;
+ unsigned int *arr=NULL;
+ bn_check_top(a);
+ bn_check_top(b);
+ bn_check_top(p);
if ((arr = (unsigned int *)OPENSSL_malloc(sizeof(unsigned int) * max)) == NULL) goto err;
ret = BN_GF2m_poly2arr(p, arr, max);
if (!ret || ret > max)
}
ret = BN_GF2m_mod_mul_arr(r, a, b, arr, ctx);
bn_check_top(r);
- err:
+err:
if (arr) OPENSSL_free(arr);
return ret;
}
{
int i, ret = 0;
BIGNUM *s;
-
+
+ bn_check_top(a);
BN_CTX_start(ctx);
if ((s = BN_CTX_get(ctx)) == NULL) return 0;
if (!bn_wexpand(s, 2 * a->top)) goto err;
if (!BN_GF2m_mod_arr(r, s, p)) goto err;
bn_check_top(r);
ret = 1;
- err:
+err:
BN_CTX_end(ctx);
return ret;
}
*/
int BN_GF2m_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
{
+ int ret = 0;
const int max = BN_num_bits(p);
- unsigned int *arr=NULL, ret = 0;
+ unsigned int *arr=NULL;
+
+ bn_check_top(a);
+ bn_check_top(p);
if ((arr = (unsigned int *)OPENSSL_malloc(sizeof(unsigned int) * max)) == NULL) goto err;
ret = BN_GF2m_poly2arr(p, arr, max);
if (!ret || ret > max)
}
ret = BN_GF2m_mod_sqr_arr(r, a, arr, ctx);
bn_check_top(r);
- err:
+err:
if (arr) OPENSSL_free(arr);
return ret;
}
BIGNUM *b, *c, *u, *v, *tmp;
int ret = 0;
+ bn_check_top(a);
+ bn_check_top(p);
+
BN_CTX_start(ctx);
b = BN_CTX_get(ctx);
if (v == NULL) goto err;
if (!BN_one(b)) goto err;
- if (!BN_zero(c)) goto err;
if (!BN_GF2m_mod(u, a, p)) goto err;
if (!BN_copy(v, p)) goto err;
bn_check_top(r);
ret = 1;
- err:
+err:
BN_CTX_end(ctx);
return ret;
}
BIGNUM *field;
int ret = 0;
+ bn_check_top(xx);
BN_CTX_start(ctx);
if ((field = BN_CTX_get(ctx)) == NULL) goto err;
if (!BN_GF2m_arr2poly(p, field)) goto err;
ret = BN_GF2m_mod_inv(r, xx, field, ctx);
bn_check_top(r);
- err:
+err:
BN_CTX_end(ctx);
return ret;
}
{
BIGNUM *xinv = NULL;
int ret = 0;
-
+
+ bn_check_top(y);
+ bn_check_top(x);
+ bn_check_top(p);
+
BN_CTX_start(ctx);
xinv = BN_CTX_get(ctx);
if (xinv == NULL) goto err;
bn_check_top(r);
ret = 1;
- err:
+err:
BN_CTX_end(ctx);
return ret;
}
BIGNUM *a, *b, *u, *v;
int ret = 0;
+ bn_check_top(y);
+ bn_check_top(x);
+ bn_check_top(p);
+
BN_CTX_start(ctx);
a = BN_CTX_get(ctx);
if (!BN_GF2m_mod(u, y, p)) goto err;
if (!BN_GF2m_mod(a, x, p)) goto err;
if (!BN_copy(b, p)) goto err;
- if (!BN_zero(v)) goto err;
while (!BN_is_odd(a))
{
bn_check_top(r);
ret = 1;
- err:
+err:
BN_CTX_end(ctx);
return ret;
}
BIGNUM *field;
int ret = 0;
+ bn_check_top(yy);
+ bn_check_top(xx);
+
BN_CTX_start(ctx);
if ((field = BN_CTX_get(ctx)) == NULL) goto err;
if (!BN_GF2m_arr2poly(p, field)) goto err;
ret = BN_GF2m_mod_div(r, yy, xx, field, ctx);
bn_check_top(r);
- err:
+err:
BN_CTX_end(ctx);
return ret;
}
{
int ret = 0, i, n;
BIGNUM *u;
-
+
+ bn_check_top(a);
+ bn_check_top(b);
+
if (BN_is_zero(b))
return(BN_one(r));
if (BN_abs_is_word(b, 1))
return (BN_copy(r, a) != NULL);
-
BN_CTX_start(ctx);
if ((u = BN_CTX_get(ctx)) == NULL) goto err;
}
if (!BN_copy(r, u)) goto err;
bn_check_top(r);
-
ret = 1;
-
- err:
+err:
BN_CTX_end(ctx);
return ret;
}
*/
int BN_GF2m_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *p, BN_CTX *ctx)
{
+ int ret = 0;
const int max = BN_num_bits(p);
- unsigned int *arr=NULL, ret = 0;
+ unsigned int *arr=NULL;
+ bn_check_top(a);
+ bn_check_top(b);
+ bn_check_top(p);
if ((arr = (unsigned int *)OPENSSL_malloc(sizeof(unsigned int) * max)) == NULL) goto err;
ret = BN_GF2m_poly2arr(p, arr, max);
if (!ret || ret > max)
}
ret = BN_GF2m_mod_exp_arr(r, a, b, arr, ctx);
bn_check_top(r);
- err:
+err:
if (arr) OPENSSL_free(arr);
return ret;
}
int ret = 0;
BIGNUM *u;
+ bn_check_top(a);
+
if (!p[0])
+ {
/* reduction mod 1 => return 0 */
- return BN_zero(r);
-
+ BN_zero(r);
+ return 1;
+ }
+
BN_CTX_start(ctx);
if ((u = BN_CTX_get(ctx)) == NULL) goto err;
- if (!BN_zero(u)) goto err;
if (!BN_set_bit(u, p[0] - 1)) goto err;
ret = BN_GF2m_mod_exp_arr(r, a, u, p, ctx);
bn_check_top(r);
- err:
+err:
BN_CTX_end(ctx);
return ret;
}
*/
int BN_GF2m_mod_sqrt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
{
+ int ret = 0;
const int max = BN_num_bits(p);
- unsigned int *arr=NULL, ret = 0;
+ unsigned int *arr=NULL;
+ bn_check_top(a);
+ bn_check_top(p);
if ((arr = (unsigned int *)OPENSSL_malloc(sizeof(unsigned int) * max)) == NULL) goto err;
ret = BN_GF2m_poly2arr(p, arr, max);
if (!ret || ret > max)
}
ret = BN_GF2m_mod_sqrt_arr(r, a, arr, ctx);
bn_check_top(r);
- err:
+err:
if (arr) OPENSSL_free(arr);
return ret;
}
unsigned int j;
BIGNUM *a, *z, *rho, *w, *w2, *tmp;
+ bn_check_top(a_);
+
if (!p[0])
+ {
/* reduction mod 1 => return 0 */
- return BN_zero(r);
+ BN_zero(r);
+ return 1;
+ }
BN_CTX_start(ctx);
a = BN_CTX_get(ctx);
if (BN_is_zero(a))
{
- ret = BN_zero(r);
+ BN_zero(r);
+ ret = 1;
goto err;
}
{
if (!BN_rand(rho, p[0], 0, 0)) goto err;
if (!BN_GF2m_mod_arr(rho, rho, p)) goto err;
- if (!BN_zero(z)) goto err;
+ BN_zero(z);
if (!BN_copy(w, rho)) goto err;
for (j = 1; j <= p[0] - 1; j++)
{
if (!BN_GF2m_mod_sqr_arr(w, z, p, ctx)) goto err;
if (!BN_GF2m_add(w, z, w)) goto err;
- if (BN_GF2m_cmp(w, a)) goto err;
+ if (BN_GF2m_cmp(w, a))
+ {
+ BNerr(BN_F_BN_GF2M_MOD_SOLVE_QUAD_ARR, BN_R_NO_SOLUTION);
+ goto err;
+ }
if (!BN_copy(r, z)) goto err;
bn_check_top(r);
ret = 1;
- err:
+err:
BN_CTX_end(ctx);
return ret;
}
*/
int BN_GF2m_mod_solve_quad(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
{
+ int ret = 0;
const int max = BN_num_bits(p);
- unsigned int *arr=NULL, ret = 0;
- if ((arr = (unsigned int *)OPENSSL_malloc(sizeof(unsigned int) * max)) == NULL) goto err;
+ unsigned int *arr=NULL;
+ bn_check_top(a);
+ bn_check_top(p);
+ if ((arr = (unsigned int *)OPENSSL_malloc(sizeof(unsigned int) *
+ max)) == NULL) goto err;
ret = BN_GF2m_poly2arr(p, arr, max);
if (!ret || ret > max)
{
}
ret = BN_GF2m_mod_solve_quad_arr(r, a, arr, ctx);
bn_check_top(r);
- err:
+err:
if (arr) OPENSSL_free(arr);
return ret;
}
{
int i;
+ bn_check_top(a);
BN_zero(a);
for (i = 0; p[i] != 0; i++)
{
}
BN_set_bit(a, 0);
bn_check_top(a);
-
+
return 1;
}