Merge pull request #43 from fperrad/20151127_lint
lintings and more This fixes #71
This commit is contained in:
commit
faea5da30a
@ -27,10 +27,10 @@ static const struct {
|
|||||||
/* return a char * string for a given code */
|
/* return a char * string for a given code */
|
||||||
const char *mp_error_to_string(int code)
|
const char *mp_error_to_string(int code)
|
||||||
{
|
{
|
||||||
int x;
|
size_t x;
|
||||||
|
|
||||||
/* scan the lookup table for the given message */
|
/* scan the lookup table for the given message */
|
||||||
for (x = 0; x < (int)(sizeof(msgs) / sizeof(msgs[0])); x++) {
|
for (x = 0; x < (sizeof(msgs) / sizeof(msgs[0])); x++) {
|
||||||
if (msgs[x].code == code) {
|
if (msgs[x].code == code) {
|
||||||
return msgs[x].msg;
|
return msgs[x].msg;
|
||||||
}
|
}
|
||||||
|
@ -59,7 +59,7 @@ int fast_mp_invmod(const mp_int *a, const mp_int *b, mp_int *c)
|
|||||||
if ((res = mp_copy(&y, &v)) != MP_OKAY) {
|
if ((res = mp_copy(&y, &v)) != MP_OKAY) {
|
||||||
goto LBL_ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
mp_set(&D, 1);
|
mp_set(&D, 1uL);
|
||||||
|
|
||||||
top:
|
top:
|
||||||
/* 4. while u is even do */
|
/* 4. while u is even do */
|
||||||
@ -128,7 +128,7 @@ top:
|
|||||||
/* now a = C, b = D, gcd == g*v */
|
/* now a = C, b = D, gcd == g*v */
|
||||||
|
|
||||||
/* if v != 1 then there is no inverse */
|
/* if v != 1 then there is no inverse */
|
||||||
if (mp_cmp_d(&v, 1) != MP_EQ) {
|
if (mp_cmp_d(&v, 1uL) != MP_EQ) {
|
||||||
res = MP_VAL;
|
res = MP_VAL;
|
||||||
goto LBL_ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,7 @@ int fast_mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho)
|
|||||||
int ix, res, olduse;
|
int ix, res, olduse;
|
||||||
mp_word W[MP_WARRAY];
|
mp_word W[MP_WARRAY];
|
||||||
|
|
||||||
if (x->used > MP_WARRAY) {
|
if (x->used > (int)MP_WARRAY) {
|
||||||
return MP_VAL;
|
return MP_VAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,7 +77,7 @@ int fast_mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho)
|
|||||||
* that W[ix-1] have the carry cleared (see after the inner loop)
|
* that W[ix-1] have the carry cleared (see after the inner loop)
|
||||||
*/
|
*/
|
||||||
mp_digit mu;
|
mp_digit mu;
|
||||||
mu = (mp_digit)(((W[ix] & MP_MASK) * rho) & MP_MASK);
|
mu = ((W[ix] & MP_MASK) * rho) & MP_MASK;
|
||||||
|
|
||||||
/* a = a + mu * m * b**i
|
/* a = a + mu * m * b**i
|
||||||
*
|
*
|
||||||
@ -106,12 +106,12 @@ int fast_mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho)
|
|||||||
|
|
||||||
/* inner loop */
|
/* inner loop */
|
||||||
for (iy = 0; iy < n->used; iy++) {
|
for (iy = 0; iy < n->used; iy++) {
|
||||||
*_W++ += ((mp_word)mu) * ((mp_word)*tmpn++);
|
*_W++ += (mp_word)mu * (mp_word)*tmpn++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* now fix carry for next digit, W[ix+1] */
|
/* now fix carry for next digit, W[ix+1] */
|
||||||
W[ix + 1] += W[ix] >> ((mp_word) DIGIT_BIT);
|
W[ix + 1] += W[ix] >> (mp_word)DIGIT_BIT;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* now we have to propagate the carries and
|
/* now we have to propagate the carries and
|
||||||
@ -131,7 +131,7 @@ int fast_mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho)
|
|||||||
_W = W + ++ix;
|
_W = W + ++ix;
|
||||||
|
|
||||||
for (; ix <= ((n->used * 2) + 1); ix++) {
|
for (; ix <= ((n->used * 2) + 1); ix++) {
|
||||||
*_W++ += *_W1++ >> ((mp_word) DIGIT_BIT);
|
*_W++ += *_W1++ >> (mp_word)DIGIT_BIT;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* copy out, A = A/b**n
|
/* copy out, A = A/b**n
|
||||||
@ -148,7 +148,7 @@ int fast_mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho)
|
|||||||
_W = W + n->used;
|
_W = W + n->used;
|
||||||
|
|
||||||
for (ix = 0; ix < (n->used + 1); ix++) {
|
for (ix = 0; ix < (n->used + 1); ix++) {
|
||||||
*tmpx++ = (mp_digit)(*_W++ & ((mp_word) MP_MASK));
|
*tmpx++ = *_W++ & (mp_word)MP_MASK;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* zero oldused digits, if the input a was larger than
|
/* zero oldused digits, if the input a was larger than
|
||||||
|
@ -69,15 +69,15 @@ int fast_s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs)
|
|||||||
|
|
||||||
/* execute loop */
|
/* execute loop */
|
||||||
for (iz = 0; iz < iy; ++iz) {
|
for (iz = 0; iz < iy; ++iz) {
|
||||||
_W += ((mp_word)*tmpx++)*((mp_word)*tmpy--);
|
_W += (mp_word)*tmpx++ * (mp_word)*tmpy--;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* store term */
|
/* store term */
|
||||||
W[ix] = ((mp_digit)_W) & MP_MASK;
|
W[ix] = (mp_digit)_W & MP_MASK;
|
||||||
|
|
||||||
/* make next carry */
|
/* make next carry */
|
||||||
_W = _W >> ((mp_word)DIGIT_BIT);
|
_W = _W >> (mp_word)DIGIT_BIT;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* setup dest */
|
/* setup dest */
|
||||||
|
@ -60,14 +60,14 @@ int fast_s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c, int dig
|
|||||||
|
|
||||||
/* execute loop */
|
/* execute loop */
|
||||||
for (iz = 0; iz < iy; iz++) {
|
for (iz = 0; iz < iy; iz++) {
|
||||||
_W += ((mp_word)*tmpx++)*((mp_word)*tmpy--);
|
_W += (mp_word)*tmpx++ * (mp_word)*tmpy--;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* store term */
|
/* store term */
|
||||||
W[ix] = ((mp_digit)_W) & MP_MASK;
|
W[ix] = (mp_digit)_W & MP_MASK;
|
||||||
|
|
||||||
/* make next carry */
|
/* make next carry */
|
||||||
_W = _W >> ((mp_word)DIGIT_BIT);
|
_W = _W >> (mp_word)DIGIT_BIT;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* setup dest */
|
/* setup dest */
|
||||||
|
@ -70,22 +70,22 @@ int fast_s_mp_sqr(const mp_int *a, mp_int *b)
|
|||||||
|
|
||||||
/* execute loop */
|
/* execute loop */
|
||||||
for (iz = 0; iz < iy; iz++) {
|
for (iz = 0; iz < iy; iz++) {
|
||||||
_W += ((mp_word)*tmpx++)*((mp_word)*tmpy--);
|
_W += (mp_word)*tmpx++ * (mp_word)*tmpy--;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* double the inner product and add carry */
|
/* double the inner product and add carry */
|
||||||
_W = _W + _W + W1;
|
_W = _W + _W + W1;
|
||||||
|
|
||||||
/* even columns have the square term in them */
|
/* even columns have the square term in them */
|
||||||
if ((ix&1) == 0) {
|
if (((unsigned)ix & 1u) == 0u) {
|
||||||
_W += ((mp_word)a->dp[ix>>1])*((mp_word)a->dp[ix>>1]);
|
_W += (mp_word)a->dp[ix>>1] * (mp_word)a->dp[ix>>1];
|
||||||
}
|
}
|
||||||
|
|
||||||
/* store it */
|
/* store it */
|
||||||
W[ix] = (mp_digit)(_W & MP_MASK);
|
W[ix] = _W & MP_MASK;
|
||||||
|
|
||||||
/* make next carry */
|
/* make next carry */
|
||||||
W1 = _W >> ((mp_word)DIGIT_BIT);
|
W1 = _W >> (mp_word)DIGIT_BIT;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* setup dest */
|
/* setup dest */
|
||||||
|
@ -36,7 +36,7 @@ int mp_2expt(mp_int *a, int b)
|
|||||||
a->used = (b / DIGIT_BIT) + 1;
|
a->used = (b / DIGIT_BIT) + 1;
|
||||||
|
|
||||||
/* put the single bit in its place */
|
/* put the single bit in its place */
|
||||||
a->dp[b / DIGIT_BIT] = ((mp_digit)1) << (b % DIGIT_BIT);
|
a->dp[b / DIGIT_BIT] = (mp_digit)1 << (mp_digit)(b % DIGIT_BIT);
|
||||||
|
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,7 @@ void mp_clamp(mp_int *a)
|
|||||||
/* decrease used while the most significant digit is
|
/* decrease used while the most significant digit is
|
||||||
* zero.
|
* zero.
|
||||||
*/
|
*/
|
||||||
while ((a->used > 0) && (a->dp[a->used - 1] == 0)) {
|
while ((a->used > 0) && (a->dp[a->used - 1] == 0u)) {
|
||||||
--(a->used);
|
--(a->used);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,17 +31,17 @@ int mp_cnt_lsb(const mp_int *a)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* scan lower digits until non-zero */
|
/* scan lower digits until non-zero */
|
||||||
for (x = 0; (x < a->used) && (a->dp[x] == 0); x++) {}
|
for (x = 0; (x < a->used) && (a->dp[x] == 0u); x++) {}
|
||||||
q = a->dp[x];
|
q = a->dp[x];
|
||||||
x *= DIGIT_BIT;
|
x *= DIGIT_BIT;
|
||||||
|
|
||||||
/* now scan this digit until a 1 is found */
|
/* now scan this digit until a 1 is found */
|
||||||
if ((q & 1) == 0) {
|
if ((q & 1u) == 0u) {
|
||||||
do {
|
do {
|
||||||
qq = q & 15;
|
qq = q & 15u;
|
||||||
x += lnz[qq];
|
x += lnz[qq];
|
||||||
q >>= 4;
|
q >>= 4;
|
||||||
} while (qq == 0);
|
} while (qq == 0u);
|
||||||
}
|
}
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
@ -31,9 +31,9 @@ int mp_count_bits(const mp_int *a)
|
|||||||
|
|
||||||
/* take the last digit and count the bits in it */
|
/* take the last digit and count the bits in it */
|
||||||
q = a->dp[a->used - 1];
|
q = a->dp[a->used - 1];
|
||||||
while (q > ((mp_digit) 0)) {
|
while (q > (mp_digit)0) {
|
||||||
++r;
|
++r;
|
||||||
q >>= ((mp_digit) 1);
|
q >>= (mp_digit)1;
|
||||||
}
|
}
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
26
bn_mp_div.c
26
bn_mp_div.c
@ -47,7 +47,7 @@ int mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
mp_set(&tq, 1);
|
mp_set(&tq, 1uL);
|
||||||
n = mp_count_bits(a) - mp_count_bits(b);
|
n = mp_count_bits(a) - mp_count_bits(b);
|
||||||
if (((res = mp_abs(a, &ta)) != MP_OKAY) ||
|
if (((res = mp_abs(a, &ta)) != MP_OKAY) ||
|
||||||
((res = mp_abs(b, &tb)) != MP_OKAY) ||
|
((res = mp_abs(b, &tb)) != MP_OKAY) ||
|
||||||
@ -150,7 +150,7 @@ int mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d)
|
|||||||
|
|
||||||
/* normalize both x and y, ensure that y >= b/2, [b == 2**DIGIT_BIT] */
|
/* normalize both x and y, ensure that y >= b/2, [b == 2**DIGIT_BIT] */
|
||||||
norm = mp_count_bits(&y) % DIGIT_BIT;
|
norm = mp_count_bits(&y) % DIGIT_BIT;
|
||||||
if (norm < (int)(DIGIT_BIT-1)) {
|
if (norm < (DIGIT_BIT - 1)) {
|
||||||
norm = (DIGIT_BIT - 1) - norm;
|
norm = (DIGIT_BIT - 1) - norm;
|
||||||
if ((res = mp_mul_2d(&x, norm, &x)) != MP_OKAY) {
|
if ((res = mp_mul_2d(&x, norm, &x)) != MP_OKAY) {
|
||||||
goto LBL_Y;
|
goto LBL_Y;
|
||||||
@ -190,16 +190,16 @@ int mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d)
|
|||||||
/* step 3.1 if xi == yt then set q{i-t-1} to b-1,
|
/* step 3.1 if xi == yt then set q{i-t-1} to b-1,
|
||||||
* otherwise set q{i-t-1} to (xi*b + x{i-1})/yt */
|
* otherwise set q{i-t-1} to (xi*b + x{i-1})/yt */
|
||||||
if (x.dp[i] == y.dp[t]) {
|
if (x.dp[i] == y.dp[t]) {
|
||||||
q.dp[(i - t) - 1] = ((((mp_digit)1) << DIGIT_BIT) - 1);
|
q.dp[(i - t) - 1] = ((mp_digit)1 << (mp_digit)DIGIT_BIT) - (mp_digit)1;
|
||||||
} else {
|
} else {
|
||||||
mp_word tmp;
|
mp_word tmp;
|
||||||
tmp = ((mp_word) x.dp[i]) << ((mp_word) DIGIT_BIT);
|
tmp = (mp_word)x.dp[i] << (mp_word)DIGIT_BIT;
|
||||||
tmp |= ((mp_word) x.dp[i - 1]);
|
tmp |= (mp_word)x.dp[i - 1];
|
||||||
tmp /= ((mp_word) y.dp[t]);
|
tmp /= (mp_word)y.dp[t];
|
||||||
if (tmp > (mp_word)MP_MASK) {
|
if (tmp > (mp_word)MP_MASK) {
|
||||||
tmp = MP_MASK;
|
tmp = MP_MASK;
|
||||||
}
|
}
|
||||||
q.dp[(i - t) - 1] = (mp_digit)(tmp & (mp_word)(MP_MASK));
|
q.dp[(i - t) - 1] = (mp_digit)(tmp & (mp_word)MP_MASK);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* while (q{i-t-1} * (yt * b + y{t-1})) >
|
/* while (q{i-t-1} * (yt * b + y{t-1})) >
|
||||||
@ -207,13 +207,13 @@ int mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d)
|
|||||||
|
|
||||||
do q{i-t-1} -= 1;
|
do q{i-t-1} -= 1;
|
||||||
*/
|
*/
|
||||||
q.dp[(i - t) - 1] = (q.dp[(i - t) - 1] + 1) & MP_MASK;
|
q.dp[(i - t) - 1] = (q.dp[(i - t) - 1] + 1uL) & (mp_digit)MP_MASK;
|
||||||
do {
|
do {
|
||||||
q.dp[(i - t) - 1] = (q.dp[(i - t) - 1] - 1) & MP_MASK;
|
q.dp[(i - t) - 1] = (q.dp[(i - t) - 1] - 1uL) & (mp_digit)MP_MASK;
|
||||||
|
|
||||||
/* find left hand */
|
/* find left hand */
|
||||||
mp_zero(&t1);
|
mp_zero(&t1);
|
||||||
t1.dp[0] = ((t - 1) < 0) ? 0 : y.dp[t - 1];
|
t1.dp[0] = ((t - 1) < 0) ? 0u : y.dp[t - 1];
|
||||||
t1.dp[1] = y.dp[t];
|
t1.dp[1] = y.dp[t];
|
||||||
t1.used = 2;
|
t1.used = 2;
|
||||||
if ((res = mp_mul_d(&t1, q.dp[(i - t) - 1], &t1)) != MP_OKAY) {
|
if ((res = mp_mul_d(&t1, q.dp[(i - t) - 1], &t1)) != MP_OKAY) {
|
||||||
@ -221,8 +221,8 @@ int mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* find right hand */
|
/* find right hand */
|
||||||
t2.dp[0] = ((i - 2) < 0) ? 0 : x.dp[i - 2];
|
t2.dp[0] = ((i - 2) < 0) ? 0u : x.dp[i - 2];
|
||||||
t2.dp[1] = ((i - 1) < 0) ? 0 : x.dp[i - 1];
|
t2.dp[1] = ((i - 1) < 0) ? 0u : x.dp[i - 1];
|
||||||
t2.dp[2] = x.dp[i];
|
t2.dp[2] = x.dp[i];
|
||||||
t2.used = 3;
|
t2.used = 3;
|
||||||
} while (mp_cmp_mag(&t1, &t2) == MP_GT);
|
} while (mp_cmp_mag(&t1, &t2) == MP_GT);
|
||||||
@ -252,7 +252,7 @@ int mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d)
|
|||||||
goto LBL_Y;
|
goto LBL_Y;
|
||||||
}
|
}
|
||||||
|
|
||||||
q.dp[(i - t) - 1] = (q.dp[(i - t) - 1] - 1UL) & MP_MASK;
|
q.dp[(i - t) - 1] = (q.dp[(i - t) - 1] - 1uL) & MP_MASK;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,7 +42,7 @@ int mp_div_2(const mp_int *a, mp_int *b)
|
|||||||
r = 0;
|
r = 0;
|
||||||
for (x = b->used - 1; x >= 0; x--) {
|
for (x = b->used - 1; x >= 0; x--) {
|
||||||
/* get the carry for the next iteration */
|
/* get the carry for the next iteration */
|
||||||
rr = *tmpa & 1;
|
rr = *tmpa & 1u;
|
||||||
|
|
||||||
/* shift the current digit, add in carry and store */
|
/* shift the current digit, add in carry and store */
|
||||||
*tmpb-- = (*tmpa-- >> 1) | (r << (DIGIT_BIT - 1));
|
*tmpb-- = (*tmpa-- >> 1) | (r << (DIGIT_BIT - 1));
|
||||||
|
@ -44,20 +44,20 @@ int mp_div_2d(const mp_int *a, int b, mp_int *c, mp_int *d)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* shift by as many digits in the bit count */
|
/* shift by as many digits in the bit count */
|
||||||
if (b >= (int)DIGIT_BIT) {
|
if (b >= DIGIT_BIT) {
|
||||||
mp_rshd(c, b / DIGIT_BIT);
|
mp_rshd(c, b / DIGIT_BIT);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* shift any bit count < DIGIT_BIT */
|
/* shift any bit count < DIGIT_BIT */
|
||||||
D = (mp_digit)(b % DIGIT_BIT);
|
D = (mp_digit)(b % DIGIT_BIT);
|
||||||
if (D != 0) {
|
if (D != 0u) {
|
||||||
mp_digit *tmpc, mask, shift;
|
mp_digit *tmpc, mask, shift;
|
||||||
|
|
||||||
/* mask */
|
/* mask */
|
||||||
mask = (((mp_digit)1) << D) - 1;
|
mask = (1uL << D) - 1uL;
|
||||||
|
|
||||||
/* shift for lsb */
|
/* shift for lsb */
|
||||||
shift = DIGIT_BIT - D;
|
shift = (mp_digit)DIGIT_BIT - D;
|
||||||
|
|
||||||
/* alias */
|
/* alias */
|
||||||
tmpc = c->dp + (c->used - 1);
|
tmpc = c->dp + (c->used - 1);
|
||||||
|
@ -24,7 +24,7 @@ int mp_div_3(const mp_int *a, mp_int *c, mp_digit *d)
|
|||||||
int res, ix;
|
int res, ix;
|
||||||
|
|
||||||
/* b = 2**DIGIT_BIT / 3 */
|
/* b = 2**DIGIT_BIT / 3 */
|
||||||
b = (((mp_word)1) << ((mp_word)DIGIT_BIT)) / ((mp_word)3);
|
b = ((mp_word)1 << (mp_word)DIGIT_BIT) / (mp_word)3;
|
||||||
|
|
||||||
if ((res = mp_init_size(&q, a->used)) != MP_OKAY) {
|
if ((res = mp_init_size(&q, a->used)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
@ -34,11 +34,11 @@ int mp_div_3(const mp_int *a, mp_int *c, mp_digit *d)
|
|||||||
q.sign = a->sign;
|
q.sign = a->sign;
|
||||||
w = 0;
|
w = 0;
|
||||||
for (ix = a->used - 1; ix >= 0; ix--) {
|
for (ix = a->used - 1; ix >= 0; ix--) {
|
||||||
w = (w << ((mp_word)DIGIT_BIT)) | ((mp_word)a->dp[ix]);
|
w = (w << (mp_word)DIGIT_BIT) | (mp_word)a->dp[ix];
|
||||||
|
|
||||||
if (w >= 3) {
|
if (w >= 3u) {
|
||||||
/* multiply w by [1/3] */
|
/* multiply w by [1/3] */
|
||||||
t = (w * ((mp_word)b)) >> ((mp_word)DIGIT_BIT);
|
t = (w * (mp_word)b) >> (mp_word)DIGIT_BIT;
|
||||||
|
|
||||||
/* now subtract 3 * [w/3] from w, to get the remainder */
|
/* now subtract 3 * [w/3] from w, to get the remainder */
|
||||||
w -= t+t+t;
|
w -= t+t+t;
|
||||||
@ -46,9 +46,9 @@ int mp_div_3(const mp_int *a, mp_int *c, mp_digit *d)
|
|||||||
/* fixup the remainder as required since
|
/* fixup the remainder as required since
|
||||||
* the optimization is not exact.
|
* the optimization is not exact.
|
||||||
*/
|
*/
|
||||||
while (w >= 3) {
|
while (w >= 3u) {
|
||||||
t += 1;
|
t += 1u;
|
||||||
w -= 3;
|
w -= 3u;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
t = 0;
|
t = 0;
|
||||||
|
@ -20,12 +20,12 @@ static int s_is_power_of_two(mp_digit b, int *p)
|
|||||||
int x;
|
int x;
|
||||||
|
|
||||||
/* fast return if no power of two */
|
/* fast return if no power of two */
|
||||||
if ((b == 0) || ((b & (b-1)) != 0)) {
|
if ((b == 0u) || ((b & (b-1u)) != 0u)) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (x = 0; x < DIGIT_BIT; x++) {
|
for (x = 0; x < DIGIT_BIT; x++) {
|
||||||
if (b == (((mp_digit)1)<<x)) {
|
if (b == (1uL<<(mp_digit)x)) {
|
||||||
*p = x;
|
*p = x;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -42,12 +42,12 @@ int mp_div_d(const mp_int *a, mp_digit b, mp_int *c, mp_digit *d)
|
|||||||
int res, ix;
|
int res, ix;
|
||||||
|
|
||||||
/* cannot divide by zero */
|
/* cannot divide by zero */
|
||||||
if (b == 0) {
|
if (b == 0u) {
|
||||||
return MP_VAL;
|
return MP_VAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* quick outs */
|
/* quick outs */
|
||||||
if ((b == 1) || (mp_iszero(a) == MP_YES)) {
|
if ((b == 1u) || (mp_iszero(a) == MP_YES)) {
|
||||||
if (d != NULL) {
|
if (d != NULL) {
|
||||||
*d = 0;
|
*d = 0;
|
||||||
}
|
}
|
||||||
@ -60,7 +60,7 @@ int mp_div_d(const mp_int *a, mp_digit b, mp_int *c, mp_digit *d)
|
|||||||
/* power of two ? */
|
/* power of two ? */
|
||||||
if (s_is_power_of_two(b, &ix) == 1) {
|
if (s_is_power_of_two(b, &ix) == 1) {
|
||||||
if (d != NULL) {
|
if (d != NULL) {
|
||||||
*d = a->dp[0] & ((((mp_digit)1)<<ix) - 1);
|
*d = a->dp[0] & ((1uL<<(mp_digit)ix) - 1uL);
|
||||||
}
|
}
|
||||||
if (c != NULL) {
|
if (c != NULL) {
|
||||||
return mp_div_2d(a, ix, c, NULL);
|
return mp_div_2d(a, ix, c, NULL);
|
||||||
@ -70,7 +70,7 @@ int mp_div_d(const mp_int *a, mp_digit b, mp_int *c, mp_digit *d)
|
|||||||
|
|
||||||
#ifdef BN_MP_DIV_3_C
|
#ifdef BN_MP_DIV_3_C
|
||||||
/* three? */
|
/* three? */
|
||||||
if (b == 3) {
|
if (b == 3u) {
|
||||||
return mp_div_3(a, c, d);
|
return mp_div_3(a, c, d);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@ -84,15 +84,15 @@ int mp_div_d(const mp_int *a, mp_digit b, mp_int *c, mp_digit *d)
|
|||||||
q.sign = a->sign;
|
q.sign = a->sign;
|
||||||
w = 0;
|
w = 0;
|
||||||
for (ix = a->used - 1; ix >= 0; ix--) {
|
for (ix = a->used - 1; ix >= 0; ix--) {
|
||||||
w = (w << ((mp_word)DIGIT_BIT)) | ((mp_word)a->dp[ix]);
|
w = (w << (mp_word)DIGIT_BIT) | (mp_word)a->dp[ix];
|
||||||
|
|
||||||
if (w >= b) {
|
if (w >= b) {
|
||||||
t = (mp_digit)(w / b);
|
t = (mp_digit)(w / b);
|
||||||
w -= ((mp_word)t) * ((mp_word)b);
|
w -= (mp_word)t * (mp_word)b;
|
||||||
} else {
|
} else {
|
||||||
t = 0;
|
t = 0;
|
||||||
}
|
}
|
||||||
q.dp[ix] = (mp_digit)t;
|
q.dp[ix] = t;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (d != NULL) {
|
if (d != NULL) {
|
||||||
|
@ -61,7 +61,7 @@ top:
|
|||||||
|
|
||||||
/* compute (x mod B**m) + k * [x/B**m] inline and inplace */
|
/* compute (x mod B**m) + k * [x/B**m] inline and inplace */
|
||||||
for (i = 0; i < m; i++) {
|
for (i = 0; i < m; i++) {
|
||||||
r = (((mp_word)*tmpx2++) * (mp_word)k) + *tmpx1 + mu;
|
r = ((mp_word)*tmpx2++ * (mp_word)k) + *tmpx1 + mu;
|
||||||
*tmpx1++ = (mp_digit)(r & MP_MASK);
|
*tmpx1++ = (mp_digit)(r & MP_MASK);
|
||||||
mu = (mp_digit)(r >> ((mp_word)DIGIT_BIT));
|
mu = (mp_digit)(r >> ((mp_word)DIGIT_BIT));
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,7 @@ void mp_dr_setup(const mp_int *a, mp_digit *d)
|
|||||||
/* the casts are required if DIGIT_BIT is one less than
|
/* the casts are required if DIGIT_BIT is one less than
|
||||||
* the number of bits in a mp_digit [e.g. DIGIT_BIT==31]
|
* the number of bits in a mp_digit [e.g. DIGIT_BIT==31]
|
||||||
*/
|
*/
|
||||||
*d = (mp_digit)((((mp_word)1) << ((mp_word)DIGIT_BIT)) - ((mp_word)a->dp[0]));
|
*d = (mp_digit)(((mp_word)1 << (mp_word)DIGIT_BIT) - (mp_word)a->dp[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -38,33 +38,33 @@ int mp_export(void *rop, size_t *countp, int order, size_t size,
|
|||||||
} lint;
|
} lint;
|
||||||
lint.i = 0x01020304;
|
lint.i = 0x01020304;
|
||||||
|
|
||||||
endian = (lint.c[0] == 4) ? -1 : 1;
|
endian = (lint.c[0] == '\x04') ? -1 : 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
odd_nails = (nails % 8);
|
odd_nails = (nails % 8u);
|
||||||
odd_nail_mask = 0xff;
|
odd_nail_mask = 0xff;
|
||||||
for (i = 0; i < odd_nails; ++i) {
|
for (i = 0; i < odd_nails; ++i) {
|
||||||
odd_nail_mask ^= (1 << (7 - i));
|
odd_nail_mask ^= (unsigned char)(1u << (7u - i));
|
||||||
}
|
}
|
||||||
nail_bytes = nails / 8;
|
nail_bytes = nails / 8u;
|
||||||
|
|
||||||
bits = mp_count_bits(&t);
|
bits = (size_t)mp_count_bits(&t);
|
||||||
count = (bits / ((size * 8) - nails)) + (((bits % ((size * 8) - nails)) != 0) ? 1 : 0);
|
count = (bits / ((size * 8u) - nails)) + (((bits % ((size * 8u) - nails)) != 0u) ? 1u : 0u);
|
||||||
|
|
||||||
for (i = 0; i < count; ++i) {
|
for (i = 0; i < count; ++i) {
|
||||||
for (j = 0; j < size; ++j) {
|
for (j = 0; j < size; ++j) {
|
||||||
unsigned char *byte = (unsigned char *)rop +
|
unsigned char *byte = (unsigned char *)rop +
|
||||||
(((order == -1) ? i : ((count - 1) - i)) * size) +
|
(((order == -1) ? i : ((count - 1u) - i)) * size) +
|
||||||
((endian == -1) ? j : ((size - 1) - j));
|
((endian == -1) ? j : ((size - 1u) - j));
|
||||||
|
|
||||||
if (j >= (size - nail_bytes)) {
|
if (j >= (size - nail_bytes)) {
|
||||||
*byte = 0;
|
*byte = 0;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
*byte = (unsigned char)((j == ((size - nail_bytes) - 1)) ? (t.dp[0] & odd_nail_mask) : (t.dp[0] & 0xFF));
|
*byte = (unsigned char)((j == ((size - nail_bytes) - 1u)) ? (t.dp[0] & odd_nail_mask) : (t.dp[0] & 0xFFuL));
|
||||||
|
|
||||||
if ((result = mp_div_2d(&t, ((j == ((size - nail_bytes) - 1)) ? (8 - odd_nails) : 8), &t, NULL)) != MP_OKAY) {
|
if ((result = mp_div_2d(&t, (j == ((size - nail_bytes) - 1u)) ? (int)(8u - odd_nails) : 8, &t, NULL)) != MP_OKAY) {
|
||||||
mp_clear(&t);
|
mp_clear(&t);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -28,12 +28,12 @@ int mp_expt_d_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* set initial result */
|
/* set initial result */
|
||||||
mp_set(c, 1);
|
mp_set(c, 1uL);
|
||||||
|
|
||||||
if (fast != 0) {
|
if (fast != 0) {
|
||||||
while (b > 0) {
|
while (b > 0u) {
|
||||||
/* if the bit is set multiply */
|
/* if the bit is set multiply */
|
||||||
if ((b & 1) != 0) {
|
if ((b & 1u) != 0u) {
|
||||||
if ((res = mp_mul(c, &g, c)) != MP_OKAY) {
|
if ((res = mp_mul(c, &g, c)) != MP_OKAY) {
|
||||||
mp_clear(&g);
|
mp_clear(&g);
|
||||||
return res;
|
return res;
|
||||||
@ -41,7 +41,7 @@ int mp_expt_d_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* square */
|
/* square */
|
||||||
if (b > 1) {
|
if (b > 1u) {
|
||||||
if ((res = mp_sqr(&g, &g)) != MP_OKAY) {
|
if ((res = mp_sqr(&g, &g)) != MP_OKAY) {
|
||||||
mp_clear(&g);
|
mp_clear(&g);
|
||||||
return res;
|
return res;
|
||||||
@ -52,7 +52,7 @@ int mp_expt_d_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
|
|||||||
b >>= 1;
|
b >>= 1;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (x = 0; x < DIGIT_BIT; x++) {
|
for (x = 0; x < (unsigned)DIGIT_BIT; x++) {
|
||||||
/* square */
|
/* square */
|
||||||
if ((res = mp_sqr(c, c)) != MP_OKAY) {
|
if ((res = mp_sqr(c, c)) != MP_OKAY) {
|
||||||
mp_clear(&g);
|
mp_clear(&g);
|
||||||
@ -60,7 +60,7 @@ int mp_expt_d_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* if the bit is set multiply */
|
/* if the bit is set multiply */
|
||||||
if ((b & (mp_digit)(((mp_digit)1) << (DIGIT_BIT - 1))) != 0) {
|
if ((b & (1uL << (DIGIT_BIT - 1))) != 0u) {
|
||||||
if ((res = mp_mul(c, &g, c)) != MP_OKAY) {
|
if ((res = mp_mul(c, &g, c)) != MP_OKAY) {
|
||||||
mp_clear(&g);
|
mp_clear(&g);
|
||||||
return res;
|
return res;
|
||||||
|
@ -39,7 +39,7 @@ int mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y
|
|||||||
* one of many reduction algorithms without modding the guts of
|
* one of many reduction algorithms without modding the guts of
|
||||||
* the code with if statements everywhere.
|
* the code with if statements everywhere.
|
||||||
*/
|
*/
|
||||||
int (*redux)(mp_int *,const mp_int *,mp_digit);
|
int (*redux)(mp_int *x, const mp_int *n, mp_digit rho);
|
||||||
|
|
||||||
/* find window size */
|
/* find window size */
|
||||||
x = mp_count_bits(X);
|
x = mp_count_bits(X);
|
||||||
@ -96,7 +96,7 @@ int mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y
|
|||||||
|
|
||||||
/* automatically pick the comba one if available (saves quite a few calls/ifs) */
|
/* automatically pick the comba one if available (saves quite a few calls/ifs) */
|
||||||
#ifdef BN_FAST_MP_MONTGOMERY_REDUCE_C
|
#ifdef BN_FAST_MP_MONTGOMERY_REDUCE_C
|
||||||
if ((((P->used * 2) + 1) < MP_WARRAY) &&
|
if ((((P->used * 2) + 1) < (int)MP_WARRAY) &&
|
||||||
(P->used < (1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) {
|
(P->used < (1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) {
|
||||||
redux = fast_mp_montgomery_reduce;
|
redux = fast_mp_montgomery_reduce;
|
||||||
} else
|
} else
|
||||||
@ -160,7 +160,7 @@ int mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y
|
|||||||
goto LBL_RES;
|
goto LBL_RES;
|
||||||
#endif
|
#endif
|
||||||
} else {
|
} else {
|
||||||
mp_set(&res, 1);
|
mp_set(&res, 1uL);
|
||||||
if ((err = mp_mod(G, P, &M[1])) != MP_OKAY) {
|
if ((err = mp_mod(G, P, &M[1])) != MP_OKAY) {
|
||||||
goto LBL_RES;
|
goto LBL_RES;
|
||||||
}
|
}
|
||||||
|
@ -28,13 +28,13 @@ int mp_exteuclid(const mp_int *a, const mp_int *b, mp_int *U1, mp_int *U2, mp_in
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* initialize, (u1,u2,u3) = (1,0,a) */
|
/* initialize, (u1,u2,u3) = (1,0,a) */
|
||||||
mp_set(&u1, 1);
|
mp_set(&u1, 1uL);
|
||||||
if ((err = mp_copy(a, &u3)) != MP_OKAY) {
|
if ((err = mp_copy(a, &u3)) != MP_OKAY) {
|
||||||
goto LBL_ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* initialize, (v1,v2,v3) = (0,1,b) */
|
/* initialize, (v1,v2,v3) = (0,1,b) */
|
||||||
mp_set(&v2, 1);
|
mp_set(&v2, 1uL);
|
||||||
if ((err = mp_copy(b, &v3)) != MP_OKAY) {
|
if ((err = mp_copy(b, &v3)) != MP_OKAY) {
|
||||||
goto LBL_ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,7 @@ int mp_fread(mp_int *a, int radix, FILE *stream)
|
|||||||
|
|
||||||
/* if first digit is - then set negative */
|
/* if first digit is - then set negative */
|
||||||
ch = fgetc(stream);
|
ch = fgetc(stream);
|
||||||
if (ch == '-') {
|
if (ch == (int)'-') {
|
||||||
neg = MP_NEG;
|
neg = MP_NEG;
|
||||||
ch = fgetc(stream);
|
ch = fgetc(stream);
|
||||||
} else {
|
} else {
|
||||||
@ -35,28 +35,28 @@ int mp_fread(mp_int *a, int radix, FILE *stream)
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
pos = ch - '(';
|
pos = (unsigned)(ch - (int)'(');
|
||||||
if (mp_s_rmap_reverse_sz < pos) {
|
if (mp_s_rmap_reverse_sz < pos) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
y = mp_s_rmap_reverse[pos];
|
y = (int)mp_s_rmap_reverse[pos];
|
||||||
|
|
||||||
if (y == 0xff || y >= radix) {
|
if ((y == 0xff) || (y >= radix)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* shift up and add */
|
/* shift up and add */
|
||||||
if ((err = mp_mul_d(a, radix, a)) != MP_OKAY) {
|
if ((err = mp_mul_d(a, (mp_digit)radix, a)) != MP_OKAY) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
if ((err = mp_add_d(a, y, a)) != MP_OKAY) {
|
if ((err = mp_add_d(a, (mp_digit)y, a)) != MP_OKAY) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
ch = fgetc(stream);
|
ch = fgetc(stream);
|
||||||
}
|
}
|
||||||
if (mp_cmp_d(a, 0) != MP_EQ) {
|
if (mp_cmp_d(a, 0uL) != MP_EQ) {
|
||||||
a->sign = neg;
|
a->sign = neg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@ int mp_fwrite(const mp_int *a, int radix, FILE *stream)
|
|||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
buf = OPT_CAST(char) XMALLOC(len);
|
buf = OPT_CAST(char) XMALLOC((size_t)len);
|
||||||
if (buf == NULL) {
|
if (buf == NULL) {
|
||||||
return MP_MEM;
|
return MP_MEM;
|
||||||
}
|
}
|
||||||
@ -36,7 +36,7 @@ int mp_fwrite(const mp_int *a, int radix, FILE *stream)
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (x = 0; x < len; x++) {
|
for (x = 0; x < len; x++) {
|
||||||
if (fputc(buf[x], stream) == EOF) {
|
if (fputc((int)buf[x], stream) == EOF) {
|
||||||
XFREE(buf);
|
XFREE(buf);
|
||||||
return MP_VAL;
|
return MP_VAL;
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ unsigned long mp_get_int(const mp_int *a)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* get number of digits of the lsb we have to read */
|
/* get number of digits of the lsb we have to read */
|
||||||
i = MIN(a->used, (int)(((sizeof(unsigned long) * CHAR_BIT) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1;
|
i = MIN(a->used, ((((int)sizeof(unsigned long) * CHAR_BIT) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1;
|
||||||
|
|
||||||
/* get most significant digit of result */
|
/* get most significant digit of result */
|
||||||
res = DIGIT(a, i);
|
res = DIGIT(a, i);
|
||||||
|
@ -26,7 +26,7 @@ unsigned long mp_get_long(const mp_int *a)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* get number of digits of the lsb we have to read */
|
/* get number of digits of the lsb we have to read */
|
||||||
i = MIN(a->used, (int)(((sizeof(unsigned long) * CHAR_BIT) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1;
|
i = MIN(a->used, ((((int)sizeof(unsigned long) * CHAR_BIT) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1;
|
||||||
|
|
||||||
/* get most significant digit of result */
|
/* get most significant digit of result */
|
||||||
res = DIGIT(a, i);
|
res = DIGIT(a, i);
|
||||||
|
@ -26,7 +26,7 @@ unsigned long long mp_get_long_long(const mp_int *a)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* get number of digits of the lsb we have to read */
|
/* get number of digits of the lsb we have to read */
|
||||||
i = MIN(a->used, (int)(((sizeof(unsigned long long) * CHAR_BIT) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1;
|
i = MIN(a->used, ((((int)sizeof(unsigned long long) * CHAR_BIT) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1;
|
||||||
|
|
||||||
/* get most significant digit of result */
|
/* get most significant digit of result */
|
||||||
res = DIGIT(a, i);
|
res = DIGIT(a, i);
|
||||||
|
@ -32,7 +32,7 @@ int mp_grow(mp_int *a, int size)
|
|||||||
* in case the operation failed we don't want
|
* in case the operation failed we don't want
|
||||||
* to overwrite the dp member of a.
|
* to overwrite the dp member of a.
|
||||||
*/
|
*/
|
||||||
tmp = OPT_CAST(mp_digit) XREALLOC(a->dp, sizeof(mp_digit) * size);
|
tmp = OPT_CAST(mp_digit) XREALLOC(a->dp, sizeof(mp_digit) * (size_t)size);
|
||||||
if (tmp == NULL) {
|
if (tmp == NULL) {
|
||||||
/* reallocation failed but "a" is still valid [can be freed] */
|
/* reallocation failed but "a" is still valid [can be freed] */
|
||||||
return MP_MEM;
|
return MP_MEM;
|
||||||
|
@ -34,27 +34,27 @@ int mp_import(mp_int *rop, size_t count, int order, size_t size,
|
|||||||
} lint;
|
} lint;
|
||||||
lint.i = 0x01020304;
|
lint.i = 0x01020304;
|
||||||
|
|
||||||
endian = (lint.c[0] == 4) ? -1 : 1;
|
endian = (lint.c[0] == '\x04') ? -1 : 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
odd_nails = (nails % 8);
|
odd_nails = (nails % 8u);
|
||||||
odd_nail_mask = 0xff;
|
odd_nail_mask = 0xff;
|
||||||
for (i = 0; i < odd_nails; ++i) {
|
for (i = 0; i < odd_nails; ++i) {
|
||||||
odd_nail_mask ^= (1 << (7 - i));
|
odd_nail_mask ^= (unsigned char)(1u << (7u - i));
|
||||||
}
|
}
|
||||||
nail_bytes = nails / 8;
|
nail_bytes = nails / 8u;
|
||||||
|
|
||||||
for (i = 0; i < count; ++i) {
|
for (i = 0; i < count; ++i) {
|
||||||
for (j = 0; j < (size - nail_bytes); ++j) {
|
for (j = 0; j < (size - nail_bytes); ++j) {
|
||||||
unsigned char byte = *((unsigned char *)op +
|
unsigned char byte = *((unsigned char *)op +
|
||||||
(((order == 1) ? i : ((count - 1) - i)) * size) +
|
(((order == 1) ? i : ((count - 1u) - i)) * size) +
|
||||||
((endian == 1) ? (j + nail_bytes) : (((size - 1) - j) - nail_bytes)));
|
((endian == 1) ? (j + nail_bytes) : (((size - 1u) - j) - nail_bytes)));
|
||||||
|
|
||||||
if ((result = mp_mul_2d(rop, ((j == 0) ? (8 - odd_nails) : 8), rop)) != MP_OKAY) {
|
if ((result = mp_mul_2d(rop, (j == 0u) ? (int)(8u - odd_nails) : 8, rop)) != MP_OKAY) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
rop->dp[0] |= (j == 0) ? (byte & odd_nail_mask) : byte;
|
rop->dp[0] |= (j == 0u) ? (mp_digit)(byte & odd_nail_mask) : (mp_digit)byte;
|
||||||
rop->used += 1;
|
rop->used += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,7 @@ int mp_init(mp_int *a)
|
|||||||
int i;
|
int i;
|
||||||
|
|
||||||
/* allocate memory required and clear it */
|
/* allocate memory required and clear it */
|
||||||
a->dp = OPT_CAST(mp_digit) XMALLOC(sizeof(mp_digit) * MP_PREC);
|
a->dp = OPT_CAST(mp_digit) XMALLOC(sizeof(mp_digit) * (size_t)MP_PREC);
|
||||||
if (a->dp == NULL) {
|
if (a->dp == NULL) {
|
||||||
return MP_MEM;
|
return MP_MEM;
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ int mp_init_size(mp_int *a, int size)
|
|||||||
size += (MP_PREC * 2) - (size % MP_PREC);
|
size += (MP_PREC * 2) - (size % MP_PREC);
|
||||||
|
|
||||||
/* alloc mem */
|
/* alloc mem */
|
||||||
a->dp = OPT_CAST(mp_digit) XMALLOC(sizeof(mp_digit) * size);
|
a->dp = OPT_CAST(mp_digit) XMALLOC(sizeof(mp_digit) * (size_t)size);
|
||||||
if (a->dp == NULL) {
|
if (a->dp == NULL) {
|
||||||
return MP_MEM;
|
return MP_MEM;
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@
|
|||||||
int mp_invmod(const mp_int *a, const mp_int *b, mp_int *c)
|
int mp_invmod(const mp_int *a, const mp_int *b, mp_int *c)
|
||||||
{
|
{
|
||||||
/* b cannot be negative and has to be >1 */
|
/* b cannot be negative and has to be >1 */
|
||||||
if ((b->sign == MP_NEG) || (mp_cmp_d(b, 1) != MP_GT)) {
|
if ((b->sign == MP_NEG) || (mp_cmp_d(b, 1uL) != MP_GT)) {
|
||||||
return MP_VAL;
|
return MP_VAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,8 +53,8 @@ int mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c)
|
|||||||
if ((res = mp_copy(&y, &v)) != MP_OKAY) {
|
if ((res = mp_copy(&y, &v)) != MP_OKAY) {
|
||||||
goto LBL_ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
mp_set(&A, 1);
|
mp_set(&A, 1uL);
|
||||||
mp_set(&D, 1);
|
mp_set(&D, 1uL);
|
||||||
|
|
||||||
top:
|
top:
|
||||||
/* 4. while u is even do */
|
/* 4. while u is even do */
|
||||||
@ -143,13 +143,13 @@ top:
|
|||||||
/* now a = C, b = D, gcd == g*v */
|
/* now a = C, b = D, gcd == g*v */
|
||||||
|
|
||||||
/* if v != 1 then there is no inverse */
|
/* if v != 1 then there is no inverse */
|
||||||
if (mp_cmp_d(&v, 1) != MP_EQ) {
|
if (mp_cmp_d(&v, 1uL) != MP_EQ) {
|
||||||
res = MP_VAL;
|
res = MP_VAL;
|
||||||
goto LBL_ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* if its too low */
|
/* if its too low */
|
||||||
while (mp_cmp_d(&C, 0) == MP_LT) {
|
while (mp_cmp_d(&C, 0uL) == MP_LT) {
|
||||||
if ((res = mp_add(&C, b, &C)) != MP_OKAY) {
|
if ((res = mp_add(&C, b, &C)) != MP_OKAY) {
|
||||||
goto LBL_ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
|
@ -58,15 +58,15 @@ int mp_is_square(const mp_int *arg, int *ret)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* First check mod 128 (suppose that DIGIT_BIT is at least 7) */
|
/* First check mod 128 (suppose that DIGIT_BIT is at least 7) */
|
||||||
if (rem_128[127 & DIGIT(arg, 0)] == 1) {
|
if (rem_128[127u & DIGIT(arg, 0)] == (char)1) {
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Next check mod 105 (3*5*7) */
|
/* Next check mod 105 (3*5*7) */
|
||||||
if ((res = mp_mod_d(arg, 105, &c)) != MP_OKAY) {
|
if ((res = mp_mod_d(arg, 105uL, &c)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
if (rem_105[c] == 1) {
|
if (rem_105[c] == (char)1) {
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,13 +82,13 @@ int mp_is_square(const mp_int *arg, int *ret)
|
|||||||
* free "t" so the easiest way is to goto ERR. We know that res
|
* free "t" so the easiest way is to goto ERR. We know that res
|
||||||
* is already equal to MP_OKAY from the mp_mod call
|
* is already equal to MP_OKAY from the mp_mod call
|
||||||
*/
|
*/
|
||||||
if (((1L<<(r%11)) & 0x5C4L) != 0L) goto ERR;
|
if (((1uL<<(r%11uL)) & 0x5C4uL) != 0uL) goto ERR;
|
||||||
if (((1L<<(r%13)) & 0x9E4L) != 0L) goto ERR;
|
if (((1uL<<(r%13uL)) & 0x9E4uL) != 0uL) goto ERR;
|
||||||
if (((1L<<(r%17)) & 0x5CE8L) != 0L) goto ERR;
|
if (((1uL<<(r%17uL)) & 0x5CE8uL) != 0uL) goto ERR;
|
||||||
if (((1L<<(r%19)) & 0x4F50CL) != 0L) goto ERR;
|
if (((1uL<<(r%19uL)) & 0x4F50CuL) != 0uL) goto ERR;
|
||||||
if (((1L<<(r%23)) & 0x7ACCA0L) != 0L) goto ERR;
|
if (((1uL<<(r%23uL)) & 0x7ACCA0uL) != 0uL) goto ERR;
|
||||||
if (((1L<<(r%29)) & 0xC2EDD0CL) != 0L) goto ERR;
|
if (((1uL<<(r%29uL)) & 0xC2EDD0CuL) != 0uL) goto ERR;
|
||||||
if (((1L<<(r%31)) & 0x6DE2B848L) != 0L) goto ERR;
|
if (((1uL<<(r%31uL)) & 0x6DE2B848uL) != 0uL) goto ERR;
|
||||||
|
|
||||||
/* Final check - is sqr(sqrt(arg)) == arg ? */
|
/* Final check - is sqr(sqrt(arg)) == arg ? */
|
||||||
if ((res = mp_sqrt(arg, &t)) != MP_OKAY) {
|
if ((res = mp_sqrt(arg, &t)) != MP_OKAY) {
|
||||||
|
@ -32,14 +32,14 @@ int mp_jacobi(const mp_int *a, const mp_int *n, int *c)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* if n <= 0 return MP_VAL */
|
/* if n <= 0 return MP_VAL */
|
||||||
if (mp_cmp_d(n, 0) != MP_GT) {
|
if (mp_cmp_d(n, 0uL) != MP_GT) {
|
||||||
return MP_VAL;
|
return MP_VAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* step 1. handle case of a == 0 */
|
/* step 1. handle case of a == 0 */
|
||||||
if (mp_iszero(a) == MP_YES) {
|
if (mp_iszero(a) == MP_YES) {
|
||||||
/* special case of a == 0 and n == 1 */
|
/* special case of a == 0 and n == 1 */
|
||||||
if (mp_cmp_d(n, 1) == MP_EQ) {
|
if (mp_cmp_d(n, 1uL) == MP_EQ) {
|
||||||
*c = 1;
|
*c = 1;
|
||||||
} else {
|
} else {
|
||||||
*c = 0;
|
*c = 0;
|
||||||
@ -48,7 +48,7 @@ int mp_jacobi(const mp_int *a, const mp_int *n, int *c)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* step 2. if a == 1, return 1 */
|
/* step 2. if a == 1, return 1 */
|
||||||
if (mp_cmp_d(a, 1) == MP_EQ) {
|
if (mp_cmp_d(a, 1uL) == MP_EQ) {
|
||||||
*c = 1;
|
*c = 1;
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
@ -72,26 +72,26 @@ int mp_jacobi(const mp_int *a, const mp_int *n, int *c)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* step 4. if e is even set s=1 */
|
/* step 4. if e is even set s=1 */
|
||||||
if ((k & 1) == 0) {
|
if (((unsigned)k & 1u) == 0u) {
|
||||||
s = 1;
|
s = 1;
|
||||||
} else {
|
} else {
|
||||||
/* else set s=1 if p = 1/7 (mod 8) or s=-1 if p = 3/5 (mod 8) */
|
/* else set s=1 if p = 1/7 (mod 8) or s=-1 if p = 3/5 (mod 8) */
|
||||||
residue = n->dp[0] & 7;
|
residue = n->dp[0] & 7u;
|
||||||
|
|
||||||
if ((residue == 1) || (residue == 7)) {
|
if ((residue == 1u) || (residue == 7u)) {
|
||||||
s = 1;
|
s = 1;
|
||||||
} else if ((residue == 3) || (residue == 5)) {
|
} else if ((residue == 3u) || (residue == 5u)) {
|
||||||
s = -1;
|
s = -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* step 5. if p == 3 (mod 4) *and* a1 == 3 (mod 4) then s = -s */
|
/* step 5. if p == 3 (mod 4) *and* a1 == 3 (mod 4) then s = -s */
|
||||||
if (((n->dp[0] & 3) == 3) && ((a1.dp[0] & 3) == 3)) {
|
if (((n->dp[0] & 3u) == 3u) && ((a1.dp[0] & 3u) == 3u)) {
|
||||||
s = -s;
|
s = -s;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* if a1 == 1 we're done */
|
/* if a1 == 1 we're done */
|
||||||
if (mp_cmp_d(&a1, 1) == MP_EQ) {
|
if (mp_cmp_d(&a1, 1uL) == MP_EQ) {
|
||||||
*c = s;
|
*c = s;
|
||||||
} else {
|
} else {
|
||||||
/* n1 = n mod a1 */
|
/* n1 = n mod a1 */
|
||||||
|
@ -27,7 +27,7 @@ int mp_mod_2d(const mp_int *a, int b, mp_int *c)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* if the modulus is larger than the value than return */
|
/* if the modulus is larger than the value than return */
|
||||||
if (b >= (int)(a->used * DIGIT_BIT)) {
|
if (b >= (a->used * DIGIT_BIT)) {
|
||||||
res = mp_copy(a, c);
|
res = mp_copy(a, c);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@ -43,7 +43,7 @@ int mp_mod_2d(const mp_int *a, int b, mp_int *c)
|
|||||||
}
|
}
|
||||||
/* clear the digit that is not completely outside/inside the modulus */
|
/* clear the digit that is not completely outside/inside the modulus */
|
||||||
c->dp[b / DIGIT_BIT] &=
|
c->dp[b / DIGIT_BIT] &=
|
||||||
(mp_digit)((((mp_digit) 1) << (((mp_digit) b) % DIGIT_BIT)) - ((mp_digit) 1));
|
(1uL << (mp_digit)(b % DIGIT_BIT)) - 1uL;
|
||||||
mp_clamp(c);
|
mp_clamp(c);
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@ int mp_montgomery_calc_normalization(mp_int *a, const mp_int *b)
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
mp_set(a, 1);
|
mp_set(a, 1uL);
|
||||||
bits = 1;
|
bits = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,10 +28,10 @@ int mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho)
|
|||||||
* are fixed up in the inner loop.
|
* are fixed up in the inner loop.
|
||||||
*/
|
*/
|
||||||
digs = (n->used * 2) + 1;
|
digs = (n->used * 2) + 1;
|
||||||
if ((digs < MP_WARRAY) &&
|
if ((digs < (int)MP_WARRAY) &&
|
||||||
(x->used <= MP_WARRAY) &&
|
(x->used <= (int)MP_WARRAY) &&
|
||||||
(n->used <
|
(n->used <
|
||||||
(1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) {
|
(int)(1u << (((size_t)CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)DIGIT_BIT))))) {
|
||||||
return fast_mp_montgomery_reduce(x, n, rho);
|
return fast_mp_montgomery_reduce(x, n, rho);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,16 +76,16 @@ int mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho)
|
|||||||
(mp_word)u + (mp_word)*tmpx;
|
(mp_word)u + (mp_word)*tmpx;
|
||||||
|
|
||||||
/* get carry */
|
/* get carry */
|
||||||
u = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
|
u = (mp_digit)(r >> (mp_word)DIGIT_BIT);
|
||||||
|
|
||||||
/* fix digit */
|
/* fix digit */
|
||||||
*tmpx++ = (mp_digit)(r & ((mp_word) MP_MASK));
|
*tmpx++ = (mp_digit)(r & (mp_word)MP_MASK);
|
||||||
}
|
}
|
||||||
/* At this point the ix'th digit of x should be zero */
|
/* At this point the ix'th digit of x should be zero */
|
||||||
|
|
||||||
|
|
||||||
/* propagate carries upwards as required*/
|
/* propagate carries upwards as required*/
|
||||||
while (u != 0) {
|
while (u != 0u) {
|
||||||
*tmpx += u;
|
*tmpx += u;
|
||||||
u = *tmpx >> DIGIT_BIT;
|
u = *tmpx >> DIGIT_BIT;
|
||||||
*tmpx++ &= MP_MASK;
|
*tmpx++ &= MP_MASK;
|
||||||
|
@ -30,24 +30,24 @@ int mp_montgomery_setup(const mp_int *n, mp_digit *rho)
|
|||||||
*/
|
*/
|
||||||
b = n->dp[0];
|
b = n->dp[0];
|
||||||
|
|
||||||
if ((b & 1) == 0) {
|
if ((b & 1u) == 0u) {
|
||||||
return MP_VAL;
|
return MP_VAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
x = (((b + 2) & 4) << 1) + b; /* here x*a==1 mod 2**4 */
|
x = (((b + 2u) & 4u) << 1) + b; /* here x*a==1 mod 2**4 */
|
||||||
x *= 2 - (b * x); /* here x*a==1 mod 2**8 */
|
x *= 2u - (b * x); /* here x*a==1 mod 2**8 */
|
||||||
#if !defined(MP_8BIT)
|
#if !defined(MP_8BIT)
|
||||||
x *= 2 - (b * x); /* here x*a==1 mod 2**16 */
|
x *= 2u - (b * x); /* here x*a==1 mod 2**16 */
|
||||||
#endif
|
#endif
|
||||||
#if defined(MP_64BIT) || !(defined(MP_8BIT) || defined(MP_16BIT))
|
#if defined(MP_64BIT) || !(defined(MP_8BIT) || defined(MP_16BIT))
|
||||||
x *= 2 - (b * x); /* here x*a==1 mod 2**32 */
|
x *= 2u - (b * x); /* here x*a==1 mod 2**32 */
|
||||||
#endif
|
#endif
|
||||||
#ifdef MP_64BIT
|
#ifdef MP_64BIT
|
||||||
x *= 2 - (b * x); /* here x*a==1 mod 2**64 */
|
x *= 2u - (b * x); /* here x*a==1 mod 2**64 */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* rho = -1/m mod b */
|
/* rho = -1/m mod b */
|
||||||
*rho = (mp_digit)(((mp_word)1 << ((mp_word) DIGIT_BIT)) - x) & MP_MASK;
|
*rho = (mp_digit)(((mp_word)1 << (mp_word)DIGIT_BIT) - x) & MP_MASK;
|
||||||
|
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
|
@ -43,9 +43,9 @@ int mp_mul(const mp_int *a, const mp_int *b, mp_int *c)
|
|||||||
int digs = a->used + b->used + 1;
|
int digs = a->used + b->used + 1;
|
||||||
|
|
||||||
#ifdef BN_FAST_S_MP_MUL_DIGS_C
|
#ifdef BN_FAST_S_MP_MUL_DIGS_C
|
||||||
if ((digs < MP_WARRAY) &&
|
if ((digs < (int)MP_WARRAY) &&
|
||||||
(MIN(a->used, b->used) <=
|
(MIN(a->used, b->used) <=
|
||||||
(1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) {
|
(int)(1u << (((size_t)CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)DIGIT_BIT))))) {
|
||||||
res = fast_s_mp_mul_digs(a, b, c, digs);
|
res = fast_s_mp_mul_digs(a, b, c, digs);
|
||||||
} else
|
} else
|
||||||
#endif
|
#endif
|
||||||
|
@ -46,10 +46,10 @@ int mp_mul_2(const mp_int *a, mp_int *b)
|
|||||||
/* get what will be the *next* carry bit from the
|
/* get what will be the *next* carry bit from the
|
||||||
* MSB of the current digit
|
* MSB of the current digit
|
||||||
*/
|
*/
|
||||||
rr = *tmpa >> ((mp_digit)(DIGIT_BIT - 1));
|
rr = *tmpa >> (mp_digit)(DIGIT_BIT - 1);
|
||||||
|
|
||||||
/* now shift up this digit, add in the carry [from the previous] */
|
/* now shift up this digit, add in the carry [from the previous] */
|
||||||
*tmpb++ = ((*tmpa++ << ((mp_digit)1)) | r) & MP_MASK;
|
*tmpb++ = ((*tmpa++ << 1uL) | r) & MP_MASK;
|
||||||
|
|
||||||
/* copy the carry that would be from the source
|
/* copy the carry that would be from the source
|
||||||
* digit into the next iteration
|
* digit into the next iteration
|
||||||
@ -58,7 +58,7 @@ int mp_mul_2(const mp_int *a, mp_int *b)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* new leading digit? */
|
/* new leading digit? */
|
||||||
if (r != 0) {
|
if (r != 0u) {
|
||||||
/* add a MSB which is always 1 at this point */
|
/* add a MSB which is always 1 at this point */
|
||||||
*tmpb = 1;
|
*tmpb = 1;
|
||||||
++(b->used);
|
++(b->used);
|
||||||
|
@ -28,14 +28,14 @@ int mp_mul_2d(const mp_int *a, int b, mp_int *c)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (c->alloc < (int)(c->used + (b / DIGIT_BIT) + 1)) {
|
if (c->alloc < (c->used + (b / DIGIT_BIT) + 1)) {
|
||||||
if ((res = mp_grow(c, c->used + (b / DIGIT_BIT) + 1)) != MP_OKAY) {
|
if ((res = mp_grow(c, c->used + (b / DIGIT_BIT) + 1)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* shift by as many digits in the bit count */
|
/* shift by as many digits in the bit count */
|
||||||
if (b >= (int)DIGIT_BIT) {
|
if (b >= DIGIT_BIT) {
|
||||||
if ((res = mp_lshd(c, b / DIGIT_BIT)) != MP_OKAY) {
|
if ((res = mp_lshd(c, b / DIGIT_BIT)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@ -43,15 +43,15 @@ int mp_mul_2d(const mp_int *a, int b, mp_int *c)
|
|||||||
|
|
||||||
/* shift any bit count < DIGIT_BIT */
|
/* shift any bit count < DIGIT_BIT */
|
||||||
d = (mp_digit)(b % DIGIT_BIT);
|
d = (mp_digit)(b % DIGIT_BIT);
|
||||||
if (d != 0) {
|
if (d != 0u) {
|
||||||
mp_digit *tmpc, shift, mask, r, rr;
|
mp_digit *tmpc, shift, mask, r, rr;
|
||||||
int x;
|
int x;
|
||||||
|
|
||||||
/* bitmask for carries */
|
/* bitmask for carries */
|
||||||
mask = (((mp_digit)1) << d) - 1;
|
mask = (1uL << d) - 1uL;
|
||||||
|
|
||||||
/* shift for msbs */
|
/* shift for msbs */
|
||||||
shift = DIGIT_BIT - d;
|
shift = (mp_digit)DIGIT_BIT - d;
|
||||||
|
|
||||||
/* alias */
|
/* alias */
|
||||||
tmpc = c->dp;
|
tmpc = c->dp;
|
||||||
@ -71,7 +71,7 @@ int mp_mul_2d(const mp_int *a, int b, mp_int *c)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* set final carry */
|
/* set final carry */
|
||||||
if (r != 0) {
|
if (r != 0u) {
|
||||||
c->dp[(c->used)++] = r;
|
c->dp[(c->used)++] = r;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -50,10 +50,10 @@ int mp_mul_d(const mp_int *a, mp_digit b, mp_int *c)
|
|||||||
r = (mp_word)u + ((mp_word)*tmpa++ * (mp_word)b);
|
r = (mp_word)u + ((mp_word)*tmpa++ * (mp_word)b);
|
||||||
|
|
||||||
/* mask off higher bits to get a single digit */
|
/* mask off higher bits to get a single digit */
|
||||||
*tmpc++ = (mp_digit)(r & ((mp_word)MP_MASK));
|
*tmpc++ = (mp_digit)(r & (mp_word)MP_MASK);
|
||||||
|
|
||||||
/* send carry into next iteration */
|
/* send carry into next iteration */
|
||||||
u = (mp_digit)(r >> ((mp_word)DIGIT_BIT));
|
u = (mp_digit)(r >> (mp_word)DIGIT_BIT);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* store final carry [if any] and increment ix offset */
|
/* store final carry [if any] and increment ix offset */
|
||||||
|
@ -31,7 +31,7 @@ int mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
|
|||||||
int res;
|
int res;
|
||||||
|
|
||||||
/* input must be positive if b is even */
|
/* input must be positive if b is even */
|
||||||
if (((b & 1) == 0) && (a->sign == MP_NEG)) {
|
if (((b & 1u) == 0u) && (a->sign == MP_NEG)) {
|
||||||
return MP_VAL;
|
return MP_VAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,7 +52,7 @@ int mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
|
|||||||
a_.sign = MP_ZPOS;
|
a_.sign = MP_ZPOS;
|
||||||
|
|
||||||
/* t2 = 2 */
|
/* t2 = 2 */
|
||||||
mp_set(&t2, 2);
|
mp_set(&t2, 2uL);
|
||||||
|
|
||||||
do {
|
do {
|
||||||
/* t1 = t2 */
|
/* t1 = t2 */
|
||||||
@ -63,7 +63,7 @@ int mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
|
|||||||
/* t2 = t1 - ((t1**b - a) / (b * t1**(b-1))) */
|
/* t2 = t1 - ((t1**b - a) / (b * t1**(b-1))) */
|
||||||
|
|
||||||
/* t3 = t1**(b-1) */
|
/* t3 = t1**(b-1) */
|
||||||
if ((res = mp_expt_d_ex(&t1, b - 1, &t3, fast)) != MP_OKAY) {
|
if ((res = mp_expt_d_ex(&t1, b - 1u, &t3, fast)) != MP_OKAY) {
|
||||||
goto LBL_T3;
|
goto LBL_T3;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -101,7 +101,7 @@ int mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (mp_cmp(&t2, &a_) == MP_GT) {
|
if (mp_cmp(&t2, &a_) == MP_GT) {
|
||||||
if ((res = mp_sub_d(&t1, 1, &t1)) != MP_OKAY) {
|
if ((res = mp_sub_d(&t1, 1uL, &t1)) != MP_OKAY) {
|
||||||
goto LBL_T3;
|
goto LBL_T3;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -32,7 +32,7 @@ int mp_prime_fermat(const mp_int *a, const mp_int *b, int *result)
|
|||||||
*result = MP_NO;
|
*result = MP_NO;
|
||||||
|
|
||||||
/* ensure b > 1 */
|
/* ensure b > 1 */
|
||||||
if (mp_cmp_d(b, 1) != MP_GT) {
|
if (mp_cmp_d(b, 1uL) != MP_GT) {
|
||||||
return MP_VAL;
|
return MP_VAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,7 +35,7 @@ int mp_prime_is_divisible(const mp_int *a, int *result)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* is the residue zero? */
|
/* is the residue zero? */
|
||||||
if (res == 0) {
|
if (res == 0u) {
|
||||||
*result = MP_YES;
|
*result = MP_YES;
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,7 @@ int mp_prime_miller_rabin(const mp_int *a, const mp_int *b, int *result)
|
|||||||
*result = MP_NO;
|
*result = MP_NO;
|
||||||
|
|
||||||
/* ensure b > 1 */
|
/* ensure b > 1 */
|
||||||
if (mp_cmp_d(b, 1) != MP_GT) {
|
if (mp_cmp_d(b, 1uL) != MP_GT) {
|
||||||
return MP_VAL;
|
return MP_VAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,7 +39,7 @@ int mp_prime_miller_rabin(const mp_int *a, const mp_int *b, int *result)
|
|||||||
if ((err = mp_init_copy(&n1, a)) != MP_OKAY) {
|
if ((err = mp_init_copy(&n1, a)) != MP_OKAY) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
if ((err = mp_sub_d(&n1, 1, &n1)) != MP_OKAY) {
|
if ((err = mp_sub_d(&n1, 1uL, &n1)) != MP_OKAY) {
|
||||||
goto LBL_N1;
|
goto LBL_N1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,7 +67,7 @@ int mp_prime_miller_rabin(const mp_int *a, const mp_int *b, int *result)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* if y != 1 and y != n1 do */
|
/* if y != 1 and y != n1 do */
|
||||||
if ((mp_cmp_d(&y, 1) != MP_EQ) && (mp_cmp(&y, &n1) != MP_EQ)) {
|
if ((mp_cmp_d(&y, 1uL) != MP_EQ) && (mp_cmp(&y, &n1) != MP_EQ)) {
|
||||||
j = 1;
|
j = 1;
|
||||||
/* while j <= s-1 and y != n1 */
|
/* while j <= s-1 and y != n1 */
|
||||||
while ((j <= (s - 1)) && (mp_cmp(&y, &n1) != MP_EQ)) {
|
while ((j <= (s - 1)) && (mp_cmp(&y, &n1) != MP_EQ)) {
|
||||||
@ -76,7 +76,7 @@ int mp_prime_miller_rabin(const mp_int *a, const mp_int *b, int *result)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* if y == 1 then composite */
|
/* if y == 1 then composite */
|
||||||
if (mp_cmp_d(&y, 1) == MP_EQ) {
|
if (mp_cmp_d(&y, 1uL) == MP_EQ) {
|
||||||
goto LBL_Y;
|
goto LBL_Y;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,10 +46,10 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style)
|
|||||||
* however, the prime must be
|
* however, the prime must be
|
||||||
* congruent to 3 mod 4
|
* congruent to 3 mod 4
|
||||||
*/
|
*/
|
||||||
if ((ltm_prime_tab[x + 1] & 3) != 3) {
|
if ((ltm_prime_tab[x + 1] & 3u) != 3u) {
|
||||||
/* scan upwards for a prime congruent to 3 mod 4 */
|
/* scan upwards for a prime congruent to 3 mod 4 */
|
||||||
for (y = x + 1; y < PRIME_SIZE; y++) {
|
for (y = x + 1; y < PRIME_SIZE; y++) {
|
||||||
if ((ltm_prime_tab[y] & 3) == 3) {
|
if ((ltm_prime_tab[y] & 3u) == 3u) {
|
||||||
mp_set(a, ltm_prime_tab[y]);
|
mp_set(a, ltm_prime_tab[y]);
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
@ -62,8 +62,8 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* at this point a maybe 1 */
|
/* at this point a maybe 1 */
|
||||||
if (mp_cmp_d(a, 1) == MP_EQ) {
|
if (mp_cmp_d(a, 1uL) == MP_EQ) {
|
||||||
mp_set(a, 2);
|
mp_set(a, 2uL);
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
/* fall through to the sieve */
|
/* fall through to the sieve */
|
||||||
@ -80,15 +80,15 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style)
|
|||||||
|
|
||||||
if (bbs_style == 1) {
|
if (bbs_style == 1) {
|
||||||
/* if a mod 4 != 3 subtract the correct value to make it so */
|
/* if a mod 4 != 3 subtract the correct value to make it so */
|
||||||
if ((a->dp[0] & 3) != 3) {
|
if ((a->dp[0] & 3u) != 3u) {
|
||||||
if ((err = mp_sub_d(a, (a->dp[0] & 3) + 1, a)) != MP_OKAY) {
|
if ((err = mp_sub_d(a, (a->dp[0] & 3u) + 1u, a)) != MP_OKAY) {
|
||||||
return err;
|
return err;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (mp_iseven(a) == MP_YES) {
|
if (mp_iseven(a) == MP_YES) {
|
||||||
/* force odd */
|
/* force odd */
|
||||||
if ((err = mp_sub_d(a, 1, a)) != MP_OKAY) {
|
if ((err = mp_sub_d(a, 1uL, a)) != MP_OKAY) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -127,11 +127,11 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* set flag if zero */
|
/* set flag if zero */
|
||||||
if (res_tab[x] == 0) {
|
if (res_tab[x] == 0u) {
|
||||||
y = 1;
|
y = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} while ((y == 1) && (step < ((((mp_digit)1) << DIGIT_BIT) - kstep)));
|
} while ((y == 1) && (step < ((1uL << DIGIT_BIT) - kstep)));
|
||||||
|
|
||||||
/* add the step */
|
/* add the step */
|
||||||
if ((err = mp_add_d(a, step, a)) != MP_OKAY) {
|
if ((err = mp_add_d(a, step, a)) != MP_OKAY) {
|
||||||
@ -139,7 +139,7 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* if didn't pass sieve and step == MAX then skip test */
|
/* if didn't pass sieve and step == MAX then skip test */
|
||||||
if ((y == 1) && (step >= ((((mp_digit)1) << DIGIT_BIT) - kstep))) {
|
if ((y == 1) && (step >= ((1uL << DIGIT_BIT) - kstep))) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,7 +49,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback
|
|||||||
bsize = (size>>3) + ((size&7)?1:0);
|
bsize = (size>>3) + ((size&7)?1:0);
|
||||||
|
|
||||||
/* we need a buffer of bsize bytes */
|
/* we need a buffer of bsize bytes */
|
||||||
tmp = OPT_CAST(unsigned char) XMALLOC(bsize);
|
tmp = OPT_CAST(unsigned char) XMALLOC((size_t)bsize);
|
||||||
if (tmp == NULL) {
|
if (tmp == NULL) {
|
||||||
return MP_MEM;
|
return MP_MEM;
|
||||||
}
|
}
|
||||||
@ -100,7 +100,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback
|
|||||||
|
|
||||||
if ((flags & LTM_PRIME_SAFE) != 0) {
|
if ((flags & LTM_PRIME_SAFE) != 0) {
|
||||||
/* see if (a-1)/2 is prime */
|
/* see if (a-1)/2 is prime */
|
||||||
if ((err = mp_sub_d(a, 1, a)) != MP_OKAY) {
|
if ((err = mp_sub_d(a, 1uL, a)) != MP_OKAY) {
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
if ((err = mp_div_2(a, a)) != MP_OKAY) {
|
if ((err = mp_div_2(a, a)) != MP_OKAY) {
|
||||||
@ -119,7 +119,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback
|
|||||||
if ((err = mp_mul_2(a, a)) != MP_OKAY) {
|
if ((err = mp_mul_2(a, a)) != MP_OKAY) {
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
if ((err = mp_add_d(a, 1, a)) != MP_OKAY) {
|
if ((err = mp_add_d(a, 1uL, a)) != MP_OKAY) {
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#if MP_GEN_RANDOM_MAX == 0xffffffff
|
#if MP_GEN_RANDOM_MAX == 0xffffffffu
|
||||||
#define MP_GEN_RANDOM_SHIFT 32
|
#define MP_GEN_RANDOM_SHIFT 32
|
||||||
#elif MP_GEN_RANDOM_MAX == 32767
|
#elif MP_GEN_RANDOM_MAX == 32767
|
||||||
/* SHRT_MAX */
|
/* SHRT_MAX */
|
||||||
@ -54,7 +54,7 @@ int mp_rand(mp_int *a, int digits)
|
|||||||
/* first place a random non-zero digit */
|
/* first place a random non-zero digit */
|
||||||
do {
|
do {
|
||||||
d = s_gen_random();
|
d = s_gen_random();
|
||||||
} while (d == 0);
|
} while (d == 0u);
|
||||||
|
|
||||||
if ((res = mp_add_d(a, d, a)) != MP_OKAY) {
|
if ((res = mp_add_d(a, d, a)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
|
@ -50,17 +50,17 @@ int mp_read_radix(mp_int *a, const char *str, int radix)
|
|||||||
* [e.g. in hex]
|
* [e.g. in hex]
|
||||||
*/
|
*/
|
||||||
ch = (radix <= 36) ? (char)toupper((int)*str) : *str;
|
ch = (radix <= 36) ? (char)toupper((int)*str) : *str;
|
||||||
pos = ch - '(';
|
pos = (unsigned)(ch - '(');
|
||||||
if (mp_s_rmap_reverse_sz < pos) {
|
if (mp_s_rmap_reverse_sz < pos) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
y = mp_s_rmap_reverse[pos];
|
y = (int)mp_s_rmap_reverse[pos];
|
||||||
|
|
||||||
/* if the char was found in the map
|
/* if the char was found in the map
|
||||||
* and is less than the given radix add it
|
* and is less than the given radix add it
|
||||||
* to the number, otherwise exit the loop.
|
* to the number, otherwise exit the loop.
|
||||||
*/
|
*/
|
||||||
if (y == 0xff || y >= radix) {
|
if ((y == 0xff) || (y >= radix)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if ((res = mp_mul_d(a, (mp_digit)radix, a)) != MP_OKAY) {
|
if ((res = mp_mul_d(a, (mp_digit)radix, a)) != MP_OKAY) {
|
||||||
@ -73,7 +73,7 @@ int mp_read_radix(mp_int *a, const char *str, int radix)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* if an illegal character was found, fail. */
|
/* if an illegal character was found, fail. */
|
||||||
if (!(*str == '\0' || *str == '\r' || *str == '\n')) {
|
if (!((*str == '\0') || (*str == '\r') || (*str == '\n'))) {
|
||||||
mp_zero(a);
|
mp_zero(a);
|
||||||
return MP_VAL;
|
return MP_VAL;
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ int mp_read_signed_bin(mp_int *a, const unsigned char *b, int c)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* first byte is 0 for positive, non-zero for negative */
|
/* first byte is 0 for positive, non-zero for negative */
|
||||||
if (b[0] == 0) {
|
if (b[0] == (unsigned char)0) {
|
||||||
a->sign = MP_ZPOS;
|
a->sign = MP_ZPOS;
|
||||||
} else {
|
} else {
|
||||||
a->sign = MP_NEG;
|
a->sign = MP_NEG;
|
||||||
|
@ -41,7 +41,7 @@ int mp_read_unsigned_bin(mp_int *a, const unsigned char *b, int c)
|
|||||||
a->used += 1;
|
a->used += 1;
|
||||||
#else
|
#else
|
||||||
a->dp[0] = (*b & MP_MASK);
|
a->dp[0] = (*b & MP_MASK);
|
||||||
a->dp[1] |= ((*b++ >> 7U) & 1);
|
a->dp[1] |= ((*b++ >> 7) & 1u);
|
||||||
a->used += 2;
|
a->used += 2;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@ int mp_reduce(mp_int *x, const mp_int *m, const mp_int *mu)
|
|||||||
mp_rshd(&q, um - 1);
|
mp_rshd(&q, um - 1);
|
||||||
|
|
||||||
/* according to HAC this optimization is ok */
|
/* according to HAC this optimization is ok */
|
||||||
if (((mp_digit) um) > (((mp_digit)1) << (DIGIT_BIT - 1))) {
|
if ((mp_digit)um > (1uL << (DIGIT_BIT - 1))) {
|
||||||
if ((res = mp_mul(&q, mu, &q)) != MP_OKAY) {
|
if ((res = mp_mul(&q, mu, &q)) != MP_OKAY) {
|
||||||
goto CLEANUP;
|
goto CLEANUP;
|
||||||
}
|
}
|
||||||
@ -73,8 +73,8 @@ int mp_reduce(mp_int *x, const mp_int *m, const mp_int *mu)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* If x < 0, add b**(k+1) to it */
|
/* If x < 0, add b**(k+1) to it */
|
||||||
if (mp_cmp_d(x, 0) == MP_LT) {
|
if (mp_cmp_d(x, 0uL) == MP_LT) {
|
||||||
mp_set(&q, 1);
|
mp_set(&q, 1uL);
|
||||||
if ((res = mp_lshd(&q, um + 1)) != MP_OKAY)
|
if ((res = mp_lshd(&q, um + 1)) != MP_OKAY)
|
||||||
goto CLEANUP;
|
goto CLEANUP;
|
||||||
if ((res = mp_add(x, &q, x)) != MP_OKAY)
|
if ((res = mp_add(x, &q, x)) != MP_OKAY)
|
||||||
|
@ -32,7 +32,7 @@ top:
|
|||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (d != 1) {
|
if (d != 1u) {
|
||||||
/* q = q * d */
|
/* q = q * d */
|
||||||
if ((res = mp_mul_d(&q, d, &q)) != MP_OKAY) {
|
if ((res = mp_mul_d(&q, d, &q)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
|
@ -32,7 +32,7 @@ int mp_reduce_is_2k(const mp_int *a)
|
|||||||
|
|
||||||
/* Test every bit from the second digit up, must be 1 */
|
/* Test every bit from the second digit up, must be 1 */
|
||||||
for (ix = DIGIT_BIT; ix < iy; ix++) {
|
for (ix = DIGIT_BIT; ix < iy; ix++) {
|
||||||
if ((a->dp[iw] & iz) == 0) {
|
if ((a->dp[iw] & iz) == 0u) {
|
||||||
return MP_NO;
|
return MP_NO;
|
||||||
}
|
}
|
||||||
iz <<= 1;
|
iz <<= 1;
|
||||||
|
@ -20,7 +20,7 @@ void mp_set(mp_int *a, mp_digit b)
|
|||||||
{
|
{
|
||||||
mp_zero(a);
|
mp_zero(a);
|
||||||
a->dp[0] = b & MP_MASK;
|
a->dp[0] = b & MP_MASK;
|
||||||
a->used = (a->dp[0] != 0) ? 1 : 0;
|
a->used = (a->dp[0] != 0u) ? 1 : 0;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -30,7 +30,7 @@ int mp_set_int(mp_int *a, unsigned long b)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* OR in the top four bits of the source */
|
/* OR in the top four bits of the source */
|
||||||
a->dp[0] |= (b >> 28) & 15;
|
a->dp[0] |= (mp_digit)(b >> 28) & 15uL;
|
||||||
|
|
||||||
/* shift the source up to the next four bits */
|
/* shift the source up to the next four bits */
|
||||||
b <<= 4;
|
b <<= 4;
|
||||||
|
@ -26,7 +26,7 @@ int mp_shrink(mp_int *a)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (a->alloc != used) {
|
if (a->alloc != used) {
|
||||||
if ((tmp = OPT_CAST(mp_digit) XREALLOC(a->dp, sizeof(mp_digit) * used)) == NULL) {
|
if ((tmp = OPT_CAST(mp_digit) XREALLOC(a->dp, sizeof(mp_digit) * (size_t)used)) == NULL) {
|
||||||
return MP_MEM;
|
return MP_MEM;
|
||||||
}
|
}
|
||||||
a->dp = tmp;
|
a->dp = tmp;
|
||||||
|
@ -35,9 +35,9 @@ int mp_sqr(const mp_int *a, mp_int *b)
|
|||||||
{
|
{
|
||||||
#ifdef BN_FAST_S_MP_SQR_C
|
#ifdef BN_FAST_S_MP_SQR_C
|
||||||
/* can we use the fast comba multiplier? */
|
/* can we use the fast comba multiplier? */
|
||||||
if ((((a->used * 2) + 1) < MP_WARRAY) &&
|
if ((((a->used * 2) + 1) < (int)MP_WARRAY) &&
|
||||||
(a->used <
|
(a->used <
|
||||||
(1 << (((sizeof(mp_word) * CHAR_BIT) - (2 * DIGIT_BIT)) - 1)))) {
|
(int)(1u << (((sizeof(mp_word) * (size_t)CHAR_BIT) - (2u * (size_t)DIGIT_BIT)) - 1u)))) {
|
||||||
res = fast_s_mp_sqr(a, b);
|
res = fast_s_mp_sqr(a, b);
|
||||||
} else
|
} else
|
||||||
#endif
|
#endif
|
||||||
|
@ -22,11 +22,11 @@ int mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret)
|
|||||||
mp_digit i;
|
mp_digit i;
|
||||||
|
|
||||||
/* first handle the simple cases */
|
/* first handle the simple cases */
|
||||||
if (mp_cmp_d(n, 0) == MP_EQ) {
|
if (mp_cmp_d(n, 0uL) == MP_EQ) {
|
||||||
mp_zero(ret);
|
mp_zero(ret);
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
if (mp_cmp_d(prime, 2) == MP_EQ) return MP_VAL; /* prime must be odd */
|
if (mp_cmp_d(prime, 2uL) == MP_EQ) return MP_VAL; /* prime must be odd */
|
||||||
if ((res = mp_jacobi(n, prime, &legendre)) != MP_OKAY) return res;
|
if ((res = mp_jacobi(n, prime, &legendre)) != MP_OKAY) return res;
|
||||||
if (legendre == -1) return MP_VAL; /* quadratic non-residue mod prime */
|
if (legendre == -1) return MP_VAL; /* quadratic non-residue mod prime */
|
||||||
|
|
||||||
@ -38,9 +38,9 @@ int mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret)
|
|||||||
* compute directly: res = n^(prime+1)/4 mod prime
|
* compute directly: res = n^(prime+1)/4 mod prime
|
||||||
* Handbook of Applied Cryptography algorithm 3.36
|
* Handbook of Applied Cryptography algorithm 3.36
|
||||||
*/
|
*/
|
||||||
if ((res = mp_mod_d(prime, 4, &i)) != MP_OKAY) goto cleanup;
|
if ((res = mp_mod_d(prime, 4uL, &i)) != MP_OKAY) goto cleanup;
|
||||||
if (i == 3) {
|
if (i == 3u) {
|
||||||
if ((res = mp_add_d(prime, 1, &t1)) != MP_OKAY) goto cleanup;
|
if ((res = mp_add_d(prime, 1uL, &t1)) != MP_OKAY) goto cleanup;
|
||||||
if ((res = mp_div_2(&t1, &t1)) != MP_OKAY) goto cleanup;
|
if ((res = mp_div_2(&t1, &t1)) != MP_OKAY) goto cleanup;
|
||||||
if ((res = mp_div_2(&t1, &t1)) != MP_OKAY) goto cleanup;
|
if ((res = mp_div_2(&t1, &t1)) != MP_OKAY) goto cleanup;
|
||||||
if ((res = mp_exptmod(n, &t1, prime, ret)) != MP_OKAY) goto cleanup;
|
if ((res = mp_exptmod(n, &t1, prime, ret)) != MP_OKAY) goto cleanup;
|
||||||
@ -52,30 +52,30 @@ int mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret)
|
|||||||
|
|
||||||
/* factor out powers of 2 from prime-1, defining Q and S as: prime-1 = Q*2^S */
|
/* factor out powers of 2 from prime-1, defining Q and S as: prime-1 = Q*2^S */
|
||||||
if ((res = mp_copy(prime, &Q)) != MP_OKAY) goto cleanup;
|
if ((res = mp_copy(prime, &Q)) != MP_OKAY) goto cleanup;
|
||||||
if ((res = mp_sub_d(&Q, 1, &Q)) != MP_OKAY) goto cleanup;
|
if ((res = mp_sub_d(&Q, 1uL, &Q)) != MP_OKAY) goto cleanup;
|
||||||
/* Q = prime - 1 */
|
/* Q = prime - 1 */
|
||||||
mp_zero(&S);
|
mp_zero(&S);
|
||||||
/* S = 0 */
|
/* S = 0 */
|
||||||
while (mp_iseven(&Q) != MP_NO) {
|
while (mp_iseven(&Q) != MP_NO) {
|
||||||
if ((res = mp_div_2(&Q, &Q)) != MP_OKAY) goto cleanup;
|
if ((res = mp_div_2(&Q, &Q)) != MP_OKAY) goto cleanup;
|
||||||
/* Q = Q / 2 */
|
/* Q = Q / 2 */
|
||||||
if ((res = mp_add_d(&S, 1, &S)) != MP_OKAY) goto cleanup;
|
if ((res = mp_add_d(&S, 1uL, &S)) != MP_OKAY) goto cleanup;
|
||||||
/* S = S + 1 */
|
/* S = S + 1 */
|
||||||
}
|
}
|
||||||
|
|
||||||
/* find a Z such that the Legendre symbol (Z|prime) == -1 */
|
/* find a Z such that the Legendre symbol (Z|prime) == -1 */
|
||||||
if ((res = mp_set_int(&Z, 2)) != MP_OKAY) goto cleanup;
|
if ((res = mp_set_int(&Z, 2uL)) != MP_OKAY) goto cleanup;
|
||||||
/* Z = 2 */
|
/* Z = 2 */
|
||||||
while (1) {
|
while (1) {
|
||||||
if ((res = mp_jacobi(&Z, prime, &legendre)) != MP_OKAY) goto cleanup;
|
if ((res = mp_jacobi(&Z, prime, &legendre)) != MP_OKAY) goto cleanup;
|
||||||
if (legendre == -1) break;
|
if (legendre == -1) break;
|
||||||
if ((res = mp_add_d(&Z, 1, &Z)) != MP_OKAY) goto cleanup;
|
if ((res = mp_add_d(&Z, 1uL, &Z)) != MP_OKAY) goto cleanup;
|
||||||
/* Z = Z + 1 */
|
/* Z = Z + 1 */
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_exptmod(&Z, &Q, prime, &C)) != MP_OKAY) goto cleanup;
|
if ((res = mp_exptmod(&Z, &Q, prime, &C)) != MP_OKAY) goto cleanup;
|
||||||
/* C = Z ^ Q mod prime */
|
/* C = Z ^ Q mod prime */
|
||||||
if ((res = mp_add_d(&Q, 1, &t1)) != MP_OKAY) goto cleanup;
|
if ((res = mp_add_d(&Q, 1uL, &t1)) != MP_OKAY) goto cleanup;
|
||||||
if ((res = mp_div_2(&t1, &t1)) != MP_OKAY) goto cleanup;
|
if ((res = mp_div_2(&t1, &t1)) != MP_OKAY) goto cleanup;
|
||||||
/* t1 = (Q + 1) / 2 */
|
/* t1 = (Q + 1) / 2 */
|
||||||
if ((res = mp_exptmod(n, &t1, prime, &R)) != MP_OKAY) goto cleanup;
|
if ((res = mp_exptmod(n, &t1, prime, &R)) != MP_OKAY) goto cleanup;
|
||||||
@ -84,24 +84,24 @@ int mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret)
|
|||||||
/* T = n ^ Q mod prime */
|
/* T = n ^ Q mod prime */
|
||||||
if ((res = mp_copy(&S, &M)) != MP_OKAY) goto cleanup;
|
if ((res = mp_copy(&S, &M)) != MP_OKAY) goto cleanup;
|
||||||
/* M = S */
|
/* M = S */
|
||||||
if ((res = mp_set_int(&two, 2)) != MP_OKAY) goto cleanup;
|
if ((res = mp_set_int(&two, 2uL)) != MP_OKAY) goto cleanup;
|
||||||
|
|
||||||
res = MP_VAL;
|
res = MP_VAL;
|
||||||
while (1) {
|
while (1) {
|
||||||
if ((res = mp_copy(&T, &t1)) != MP_OKAY) goto cleanup;
|
if ((res = mp_copy(&T, &t1)) != MP_OKAY) goto cleanup;
|
||||||
i = 0;
|
i = 0;
|
||||||
while (1) {
|
while (1) {
|
||||||
if (mp_cmp_d(&t1, 1) == MP_EQ) break;
|
if (mp_cmp_d(&t1, 1uL) == MP_EQ) break;
|
||||||
if ((res = mp_exptmod(&t1, &two, prime, &t1)) != MP_OKAY) goto cleanup;
|
if ((res = mp_exptmod(&t1, &two, prime, &t1)) != MP_OKAY) goto cleanup;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
if (i == 0) {
|
if (i == 0u) {
|
||||||
if ((res = mp_copy(&R, ret)) != MP_OKAY) goto cleanup;
|
if ((res = mp_copy(&R, ret)) != MP_OKAY) goto cleanup;
|
||||||
res = MP_OKAY;
|
res = MP_OKAY;
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
if ((res = mp_sub_d(&M, i, &t1)) != MP_OKAY) goto cleanup;
|
if ((res = mp_sub_d(&M, i, &t1)) != MP_OKAY) goto cleanup;
|
||||||
if ((res = mp_sub_d(&t1, 1, &t1)) != MP_OKAY) goto cleanup;
|
if ((res = mp_sub_d(&t1, 1uL, &t1)) != MP_OKAY) goto cleanup;
|
||||||
if ((res = mp_exptmod(&two, &t1, prime, &t1)) != MP_OKAY) goto cleanup;
|
if ((res = mp_exptmod(&two, &t1, prime, &t1)) != MP_OKAY) goto cleanup;
|
||||||
/* t1 = 2 ^ (M - i - 1) */
|
/* t1 = 2 ^ (M - i - 1) */
|
||||||
if ((res = mp_exptmod(&C, &t1, prime, &t1)) != MP_OKAY) goto cleanup;
|
if ((res = mp_exptmod(&C, &t1, prime, &t1)) != MP_OKAY) goto cleanup;
|
||||||
|
@ -67,13 +67,13 @@ int mp_sub_d(const mp_int *a, mp_digit b, mp_int *c)
|
|||||||
|
|
||||||
/* subtract first digit */
|
/* subtract first digit */
|
||||||
*tmpc = *tmpa++ - b;
|
*tmpc = *tmpa++ - b;
|
||||||
mu = *tmpc >> ((sizeof(mp_digit) * CHAR_BIT) - 1);
|
mu = *tmpc >> ((sizeof(mp_digit) * (size_t)CHAR_BIT) - 1u);
|
||||||
*tmpc++ &= MP_MASK;
|
*tmpc++ &= MP_MASK;
|
||||||
|
|
||||||
/* handle rest of the digits */
|
/* handle rest of the digits */
|
||||||
for (ix = 1; ix < a->used; ix++) {
|
for (ix = 1; ix < a->used; ix++) {
|
||||||
*tmpc = *tmpa++ - mu;
|
*tmpc = *tmpa++ - mu;
|
||||||
mu = *tmpc >> ((sizeof(mp_digit) * CHAR_BIT) - 1);
|
mu = *tmpc >> ((sizeof(mp_digit) * (size_t)CHAR_BIT) - 1u);
|
||||||
*tmpc++ &= MP_MASK;
|
*tmpc++ &= MP_MASK;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,7 @@ int mp_to_signed_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen)
|
|||||||
if (*outlen < (unsigned long)mp_signed_bin_size(a)) {
|
if (*outlen < (unsigned long)mp_signed_bin_size(a)) {
|
||||||
return MP_VAL;
|
return MP_VAL;
|
||||||
}
|
}
|
||||||
*outlen = mp_signed_bin_size(a);
|
*outlen = (unsigned long)mp_signed_bin_size(a);
|
||||||
return mp_to_signed_bin(a, b);
|
return mp_to_signed_bin(a, b);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -28,9 +28,9 @@ int mp_to_unsigned_bin(const mp_int *a, unsigned char *b)
|
|||||||
x = 0;
|
x = 0;
|
||||||
while (mp_iszero(&t) == MP_NO) {
|
while (mp_iszero(&t) == MP_NO) {
|
||||||
#ifndef MP_8BIT
|
#ifndef MP_8BIT
|
||||||
b[x++] = (unsigned char)(t.dp[0] & 255);
|
b[x++] = (unsigned char)(t.dp[0] & 255u);
|
||||||
#else
|
#else
|
||||||
b[x++] = (unsigned char)(t.dp[0] | ((t.dp[1] & 0x01) << 7));
|
b[x++] = (unsigned char)(t.dp[0] | ((t.dp[1] & 1u) << 7));
|
||||||
#endif
|
#endif
|
||||||
if ((res = mp_div_2d(&t, 8, &t, NULL)) != MP_OKAY) {
|
if ((res = mp_div_2d(&t, 8, &t, NULL)) != MP_OKAY) {
|
||||||
mp_clear(&t);
|
mp_clear(&t);
|
||||||
|
@ -21,7 +21,7 @@ int mp_to_unsigned_bin_n(const mp_int *a, unsigned char *b, unsigned long *outle
|
|||||||
if (*outlen < (unsigned long)mp_unsigned_bin_size(a)) {
|
if (*outlen < (unsigned long)mp_unsigned_bin_size(a)) {
|
||||||
return MP_VAL;
|
return MP_VAL;
|
||||||
}
|
}
|
||||||
*outlen = mp_unsigned_bin_size(a);
|
*outlen = (unsigned long)mp_unsigned_bin_size(a);
|
||||||
return mp_to_unsigned_bin(a, b);
|
return mp_to_unsigned_bin(a, b);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -219,7 +219,7 @@ int mp_toom_mul(const mp_int *a, const mp_int *b, mp_int *c)
|
|||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
/* 3r2 - r1 - r3 */
|
/* 3r2 - r1 - r3 */
|
||||||
if ((res = mp_mul_d(&w2, 3, &w2)) != MP_OKAY) {
|
if ((res = mp_mul_d(&w2, 3uL, &w2)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_sub(&w2, &w1, &w2)) != MP_OKAY) {
|
if ((res = mp_sub(&w2, &w1, &w2)) != MP_OKAY) {
|
||||||
|
@ -162,7 +162,7 @@ int mp_toom_sqr(const mp_int *a, mp_int *b)
|
|||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
/* 3r2 - r1 - r3 */
|
/* 3r2 - r1 - r3 */
|
||||||
if ((res = mp_mul_d(&w2, 3, &w2)) != MP_OKAY) {
|
if ((res = mp_mul_d(&w2, 3uL, &w2)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_sub(&w2, &w1, &w2)) != MP_OKAY) {
|
if ((res = mp_sub(&w2, &w1, &w2)) != MP_OKAY) {
|
||||||
|
@ -19,7 +19,7 @@
|
|||||||
int mp_unsigned_bin_size(const mp_int *a)
|
int mp_unsigned_bin_size(const mp_int *a)
|
||||||
{
|
{
|
||||||
int size = mp_count_bits(a);
|
int size = mp_count_bits(a);
|
||||||
return (size / 8) + (((size & 7) != 0) ? 1 : 0);
|
return (size / 8) + ((((unsigned)size & 7u) != 0u) ? 1 : 0);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -67,7 +67,7 @@ int s_mp_add(const mp_int *a, const mp_int *b, mp_int *c)
|
|||||||
*tmpc = *tmpa++ + *tmpb++ + u;
|
*tmpc = *tmpa++ + *tmpb++ + u;
|
||||||
|
|
||||||
/* U = carry bit of T[i] */
|
/* U = carry bit of T[i] */
|
||||||
u = *tmpc >> ((mp_digit)DIGIT_BIT);
|
u = *tmpc >> (mp_digit)DIGIT_BIT;
|
||||||
|
|
||||||
/* take away carry bit from T[i] */
|
/* take away carry bit from T[i] */
|
||||||
*tmpc++ &= MP_MASK;
|
*tmpc++ &= MP_MASK;
|
||||||
@ -82,7 +82,7 @@ int s_mp_add(const mp_int *a, const mp_int *b, mp_int *c)
|
|||||||
*tmpc = x->dp[i] + u;
|
*tmpc = x->dp[i] + u;
|
||||||
|
|
||||||
/* U = carry bit of T[i] */
|
/* U = carry bit of T[i] */
|
||||||
u = *tmpc >> ((mp_digit)DIGIT_BIT);
|
u = *tmpc >> (mp_digit)DIGIT_BIT;
|
||||||
|
|
||||||
/* take away carry bit from T[i] */
|
/* take away carry bit from T[i] */
|
||||||
*tmpc++ &= MP_MASK;
|
*tmpc++ &= MP_MASK;
|
||||||
|
@ -25,7 +25,7 @@ int s_mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, i
|
|||||||
mp_int M[TAB_SIZE], res, mu;
|
mp_int M[TAB_SIZE], res, mu;
|
||||||
mp_digit buf;
|
mp_digit buf;
|
||||||
int err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize;
|
int err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize;
|
||||||
int (*redux)(mp_int *, const mp_int *, const mp_int *);
|
int (*redux)(mp_int *x, const mp_int *m, const mp_int *mu);
|
||||||
|
|
||||||
/* find window size */
|
/* find window size */
|
||||||
x = mp_count_bits(X);
|
x = mp_count_bits(X);
|
||||||
@ -133,7 +133,7 @@ int s_mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, i
|
|||||||
if ((err = mp_init(&res)) != MP_OKAY) {
|
if ((err = mp_init(&res)) != MP_OKAY) {
|
||||||
goto LBL_MU;
|
goto LBL_MU;
|
||||||
}
|
}
|
||||||
mp_set(&res, 1);
|
mp_set(&res, 1uL);
|
||||||
|
|
||||||
/* set initial mode and bit cnt */
|
/* set initial mode and bit cnt */
|
||||||
mode = 0;
|
mode = 0;
|
||||||
|
@ -28,9 +28,9 @@ int s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs)
|
|||||||
mp_digit tmpx, *tmpt, *tmpy;
|
mp_digit tmpx, *tmpt, *tmpy;
|
||||||
|
|
||||||
/* can we use the fast multiplier? */
|
/* can we use the fast multiplier? */
|
||||||
if (((digs) < MP_WARRAY) &&
|
if ((digs < (int)MP_WARRAY) &&
|
||||||
(MIN(a->used, b->used) <
|
(MIN(a->used, b->used) <
|
||||||
(1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) {
|
(int)(1u << (((size_t)CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)DIGIT_BIT))))) {
|
||||||
return fast_s_mp_mul_digs(a, b, c, digs);
|
return fast_s_mp_mul_digs(a, b, c, digs);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,10 +66,10 @@ int s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs)
|
|||||||
(mp_word)u;
|
(mp_word)u;
|
||||||
|
|
||||||
/* the new column is the lower part of the result */
|
/* the new column is the lower part of the result */
|
||||||
*tmpt++ = (mp_digit)(r & ((mp_word) MP_MASK));
|
*tmpt++ = (mp_digit)(r & (mp_word)MP_MASK);
|
||||||
|
|
||||||
/* get the carry word from the result */
|
/* get the carry word from the result */
|
||||||
u = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
|
u = (mp_digit)(r >> (mp_word)DIGIT_BIT);
|
||||||
}
|
}
|
||||||
/* set carry if it is placed below digs */
|
/* set carry if it is placed below digs */
|
||||||
if ((ix + iy) < digs) {
|
if ((ix + iy) < digs) {
|
||||||
|
@ -28,8 +28,8 @@ int s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs)
|
|||||||
|
|
||||||
/* can we use the fast multiplier? */
|
/* can we use the fast multiplier? */
|
||||||
#ifdef BN_FAST_S_MP_MUL_HIGH_DIGS_C
|
#ifdef BN_FAST_S_MP_MUL_HIGH_DIGS_C
|
||||||
if (((a->used + b->used + 1) < MP_WARRAY)
|
if (((a->used + b->used + 1) < (int)MP_WARRAY)
|
||||||
&& (MIN(a->used, b->used) < (1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) {
|
&& (MIN(a->used, b->used) < (int)(1u << (((size_t)CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)DIGIT_BIT))))) {
|
||||||
return fast_s_mp_mul_high_digs(a, b, c, digs);
|
return fast_s_mp_mul_high_digs(a, b, c, digs);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@ -61,10 +61,10 @@ int s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs)
|
|||||||
(mp_word)u;
|
(mp_word)u;
|
||||||
|
|
||||||
/* get the lower part */
|
/* get the lower part */
|
||||||
*tmpt++ = (mp_digit)(r & ((mp_word) MP_MASK));
|
*tmpt++ = (mp_digit)(r & (mp_word)MP_MASK);
|
||||||
|
|
||||||
/* carry the carry */
|
/* carry the carry */
|
||||||
u = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
|
u = (mp_digit)(r >> (mp_word)DIGIT_BIT);
|
||||||
}
|
}
|
||||||
*tmpt = u;
|
*tmpt = u;
|
||||||
}
|
}
|
||||||
|
@ -38,10 +38,10 @@ int s_mp_sqr(const mp_int *a, mp_int *b)
|
|||||||
((mp_word)a->dp[ix] * (mp_word)a->dp[ix]);
|
((mp_word)a->dp[ix] * (mp_word)a->dp[ix]);
|
||||||
|
|
||||||
/* store lower part in result */
|
/* store lower part in result */
|
||||||
t.dp[ix+ix] = (mp_digit)(r & ((mp_word)MP_MASK));
|
t.dp[ix+ix] = (mp_digit)(r & (mp_word)MP_MASK);
|
||||||
|
|
||||||
/* get the carry */
|
/* get the carry */
|
||||||
u = (mp_digit)(r >> ((mp_word)DIGIT_BIT));
|
u = (mp_digit)(r >> (mp_word)DIGIT_BIT);
|
||||||
|
|
||||||
/* left hand side of A[ix] * A[iy] */
|
/* left hand side of A[ix] * A[iy] */
|
||||||
tmpx = a->dp[ix];
|
tmpx = a->dp[ix];
|
||||||
@ -51,24 +51,24 @@ int s_mp_sqr(const mp_int *a, mp_int *b)
|
|||||||
|
|
||||||
for (iy = ix + 1; iy < pa; iy++) {
|
for (iy = ix + 1; iy < pa; iy++) {
|
||||||
/* first calculate the product */
|
/* first calculate the product */
|
||||||
r = ((mp_word)tmpx) * ((mp_word)a->dp[iy]);
|
r = (mp_word)tmpx * (mp_word)a->dp[iy];
|
||||||
|
|
||||||
/* now calculate the double precision result, note we use
|
/* now calculate the double precision result, note we use
|
||||||
* addition instead of *2 since it's easier to optimize
|
* addition instead of *2 since it's easier to optimize
|
||||||
*/
|
*/
|
||||||
r = ((mp_word) *tmpt) + r + r + ((mp_word) u);
|
r = (mp_word)*tmpt + r + r + (mp_word)u;
|
||||||
|
|
||||||
/* store lower part */
|
/* store lower part */
|
||||||
*tmpt++ = (mp_digit)(r & ((mp_word) MP_MASK));
|
*tmpt++ = (mp_digit)(r & (mp_word)MP_MASK);
|
||||||
|
|
||||||
/* get carry */
|
/* get carry */
|
||||||
u = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
|
u = (mp_digit)(r >> (mp_word)DIGIT_BIT);
|
||||||
}
|
}
|
||||||
/* propagate upwards */
|
/* propagate upwards */
|
||||||
while (u != ((mp_digit) 0)) {
|
while (u != 0uL) {
|
||||||
r = ((mp_word) *tmpt) + ((mp_word) u);
|
r = (mp_word)*tmpt + (mp_word)u;
|
||||||
*tmpt++ = (mp_digit)(r & ((mp_word) MP_MASK));
|
*tmpt++ = (mp_digit)(r & (mp_word)MP_MASK);
|
||||||
u = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
|
u = (mp_digit)(r >> (mp_word)DIGIT_BIT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,7 +53,7 @@ int s_mp_sub(const mp_int *a, const mp_int *b, mp_int *c)
|
|||||||
* if a carry does occur it will propagate all the way to the
|
* if a carry does occur it will propagate all the way to the
|
||||||
* MSB. As a result a single shift is enough to get the carry
|
* MSB. As a result a single shift is enough to get the carry
|
||||||
*/
|
*/
|
||||||
u = *tmpc >> ((mp_digit)((CHAR_BIT * sizeof(mp_digit)) - 1));
|
u = *tmpc >> (((size_t)CHAR_BIT * sizeof(mp_digit)) - 1u);
|
||||||
|
|
||||||
/* Clear carry from T[i] */
|
/* Clear carry from T[i] */
|
||||||
*tmpc++ &= MP_MASK;
|
*tmpc++ &= MP_MASK;
|
||||||
@ -65,7 +65,7 @@ int s_mp_sub(const mp_int *a, const mp_int *b, mp_int *c)
|
|||||||
*tmpc = *tmpa++ - u;
|
*tmpc = *tmpa++ - u;
|
||||||
|
|
||||||
/* U = carry bit of T[i] */
|
/* U = carry bit of T[i] */
|
||||||
u = *tmpc >> ((mp_digit)((CHAR_BIT * sizeof(mp_digit)) - 1));
|
u = *tmpc >> (((size_t)CHAR_BIT * sizeof(mp_digit)) - 1u);
|
||||||
|
|
||||||
/* Clear carry from T[i] */
|
/* Clear carry from T[i] */
|
||||||
*tmpc++ &= MP_MASK;
|
*tmpc++ &= MP_MASK;
|
||||||
|
24
tommath.h
24
tommath.h
@ -105,7 +105,7 @@ typedef mp_digit mp_min_u32;
|
|||||||
/* use arc4random on platforms that support it */
|
/* use arc4random on platforms that support it */
|
||||||
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
|
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
|
||||||
# define MP_GEN_RANDOM() arc4random()
|
# define MP_GEN_RANDOM() arc4random()
|
||||||
# define MP_GEN_RANDOM_MAX 0xffffffff
|
# define MP_GEN_RANDOM_MAX 0xffffffffu
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* use rand() as fall-back if there's no better rand function */
|
/* use rand() as fall-back if there's no better rand function */
|
||||||
@ -160,7 +160,7 @@ extern int KARATSUBA_MUL_CUTOFF,
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2) */
|
/* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2) */
|
||||||
#define MP_WARRAY (1 << (((sizeof(mp_word) * CHAR_BIT) - (2 * DIGIT_BIT)) + 1))
|
#define MP_WARRAY (1u << (((sizeof(mp_word) * CHAR_BIT) - (2 * DIGIT_BIT)) + 1))
|
||||||
|
|
||||||
/* the infamous mp_int structure */
|
/* the infamous mp_int structure */
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@ -395,7 +395,7 @@ int mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast);
|
|||||||
int mp_sqrt(const mp_int *arg, mp_int *ret);
|
int mp_sqrt(const mp_int *arg, mp_int *ret);
|
||||||
|
|
||||||
/* special sqrt (mod prime) */
|
/* special sqrt (mod prime) */
|
||||||
int mp_sqrtmod_prime(const mp_int *arg, const mp_int *prime, mp_int *ret);
|
int mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret);
|
||||||
|
|
||||||
/* is number a square? */
|
/* is number a square? */
|
||||||
int mp_is_square(const mp_int *arg, int *ret);
|
int mp_is_square(const mp_int *arg, int *ret);
|
||||||
@ -408,13 +408,13 @@ int mp_reduce_setup(mp_int *a, const mp_int *b);
|
|||||||
|
|
||||||
/* Barrett Reduction, computes a (mod b) with a precomputed value c
|
/* Barrett Reduction, computes a (mod b) with a precomputed value c
|
||||||
*
|
*
|
||||||
* Assumes that 0 < a <= b*b, note if 0 > a > -(b*b) then you can merely
|
* Assumes that 0 < x <= m*m, note if 0 > x > -(m*m) then you can merely
|
||||||
* compute the reduction as -1 * mp_reduce(mp_abs(a)) [pseudo code].
|
* compute the reduction as -1 * mp_reduce(mp_abs(x)) [pseudo code].
|
||||||
*/
|
*/
|
||||||
int mp_reduce(mp_int *a, const mp_int *b, const mp_int *c);
|
int mp_reduce(mp_int *x, const mp_int *m, const mp_int *mu);
|
||||||
|
|
||||||
/* setups the montgomery reduction */
|
/* setups the montgomery reduction */
|
||||||
int mp_montgomery_setup(const mp_int *a, mp_digit *mp);
|
int mp_montgomery_setup(const mp_int *n, mp_digit *rho);
|
||||||
|
|
||||||
/* computes a = B**n mod b without division or multiplication useful for
|
/* computes a = B**n mod b without division or multiplication useful for
|
||||||
* normalizing numbers in a Montgomery system.
|
* normalizing numbers in a Montgomery system.
|
||||||
@ -422,7 +422,7 @@ int mp_montgomery_setup(const mp_int *a, mp_digit *mp);
|
|||||||
int mp_montgomery_calc_normalization(mp_int *a, const mp_int *b);
|
int mp_montgomery_calc_normalization(mp_int *a, const mp_int *b);
|
||||||
|
|
||||||
/* computes x/R == x (mod N) via Montgomery Reduction */
|
/* computes x/R == x (mod N) via Montgomery Reduction */
|
||||||
int mp_montgomery_reduce(mp_int *a, const mp_int *m, mp_digit mp);
|
int mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho);
|
||||||
|
|
||||||
/* returns 1 if a is a valid DR modulus */
|
/* returns 1 if a is a valid DR modulus */
|
||||||
int mp_dr_is_modulus(const mp_int *a);
|
int mp_dr_is_modulus(const mp_int *a);
|
||||||
@ -430,8 +430,8 @@ int mp_dr_is_modulus(const mp_int *a);
|
|||||||
/* sets the value of "d" required for mp_dr_reduce */
|
/* sets the value of "d" required for mp_dr_reduce */
|
||||||
void mp_dr_setup(const mp_int *a, mp_digit *d);
|
void mp_dr_setup(const mp_int *a, mp_digit *d);
|
||||||
|
|
||||||
/* reduces a modulo b using the Diminished Radix method */
|
/* reduces a modulo n using the Diminished Radix method */
|
||||||
int mp_dr_reduce(mp_int *a, const mp_int *b, mp_digit mp);
|
int mp_dr_reduce(mp_int *x, const mp_int *n, mp_digit k);
|
||||||
|
|
||||||
/* returns true if a can be reduced with mp_reduce_2k */
|
/* returns true if a can be reduced with mp_reduce_2k */
|
||||||
int mp_reduce_is_2k(const mp_int *a);
|
int mp_reduce_is_2k(const mp_int *a);
|
||||||
@ -451,8 +451,8 @@ int mp_reduce_2k_setup_l(const mp_int *a, mp_int *d);
|
|||||||
/* reduces a modulo b where b is of the form 2**p - k [0 <= a] */
|
/* reduces a modulo b where b is of the form 2**p - k [0 <= a] */
|
||||||
int mp_reduce_2k_l(mp_int *a, const mp_int *n, const mp_int *d);
|
int mp_reduce_2k_l(mp_int *a, const mp_int *n, const mp_int *d);
|
||||||
|
|
||||||
/* d = a**b (mod c) */
|
/* Y = G**X (mod P) */
|
||||||
int mp_exptmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d);
|
int mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y);
|
||||||
|
|
||||||
/* ---> Primes <--- */
|
/* ---> Primes <--- */
|
||||||
|
|
||||||
|
@ -101,7 +101,7 @@ int func_name (mp_int * a, type b) \
|
|||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
/* OR in the top four bits of the source */ \
|
/* OR in the top four bits of the source */ \
|
||||||
a->dp[0] |= (b >> ((sizeof(type) * 8u) - 4u)) & 15u; \
|
a->dp[0] |= (mp_digit)(b >> ((sizeof(type) * 8u) - 4u)) & 15uL;\
|
||||||
\
|
\
|
||||||
/* shift the source up to the next four bits */ \
|
/* shift the source up to the next four bits */ \
|
||||||
b <<= 4; \
|
b <<= 4; \
|
||||||
|
Loading…
Reference in New Issue
Block a user