sp_int: clamp more results

This commit is contained in:
Sean Parkinson
2019-11-26 10:05:09 +10:00
parent dadbeff433
commit 245a2b7012

View File

@@ -179,6 +179,7 @@ int sp_read_unsigned_bin(sp_int* a, const byte* in, word32 inSz)
for (j++; j < a->size; j++)
a->dp[j] = 0;
sp_clamp(a);
return MP_OKAY;
}
@@ -234,6 +235,7 @@ int sp_read_radix(sp_int* a, const char* in, int radix)
for (k++; k < a->size; k++)
a->dp[k] = 0;
}
sp_clamp(a);
return err;
}
@@ -485,6 +487,7 @@ int sp_sub_d(sp_int* a, sp_int_digit d, sp_int* r)
}
for (++i; i < a->used; i++)
r->dp[i] = a->dp[i];
sp_clamp(r);
return MP_OKAY;
}
@@ -665,14 +668,18 @@ static int sp_div(sp_int* a, sp_int* d, sp_int* r, sp_int* rem)
ret = sp_cmp(a, d);
if (ret == MP_LT) {
sp_copy(a, rem);
if (rem != NULL) {
sp_copy(a, rem);
}
if (r != NULL) {
sp_set(r, 0);
}
done = 1;
}
else if (ret == MP_EQ) {
sp_set(rem, 0);
if (rem != NULL) {
sp_set(rem, 0);
}
if (r != NULL) {
sp_set(r, 1);
}
@@ -680,7 +687,9 @@ static int sp_div(sp_int* a, sp_int* d, sp_int* r, sp_int* rem)
}
else if (sp_count_bits(a) == sp_count_bits(d)) {
/* a is greater than d but same bit length */
sp_sub(a, d, rem);
if (rem != NULL) {
sp_sub(a, d, rem);
}
if (r != NULL) {
sp_set(r, 1);
}
@@ -718,6 +727,7 @@ static int sp_div(sp_int* a, sp_int* d, sp_int* r, sp_int* rem)
tr->used = sa->used - d->used;
sp_clear(tr);
tr->used = sa->used - d->used;
dt = d->dp[d->used-1];
for (i = sa->used - 1; i >= d->used; i--) {
w = ((sp_int_word)sa->dp[i] << SP_WORD_SIZE) | sa->dp[i-1];
@@ -835,6 +845,7 @@ int sp_lshd(sp_int* a, int s)
XMEMMOVE(a->dp + s, a->dp, a->used * sizeof(sp_int_digit));
a->used += s;
XMEMSET(a->dp, 0, s * sizeof(sp_int_digit));
sp_clamp(a);
return MP_OKAY;
}
@@ -1303,8 +1314,9 @@ int sp_invmod(sp_int* a, sp_int* m, sp_int* r)
sp_int u[1], v[1], t[1], b[1], c[1];
#endif
if (sp_iszero(a) || sp_iszero(m))
if (sp_iszero(a) || sp_iszero(m)) {
err = MP_VAL;
}
else if (sp_iseven(m)) {
/* a^-1 mod m = m + (1 - m*(m^-1 % a)) / a
* = m - (m*(m^-1 % a) - 1) / a