mirror of
https://github.com/wolfSSL/wolfssl.git
synced 2025-07-30 10:47:28 +02:00
SP int: don't call sp_clear unless no longer using sp_int
Put in casts to be sure no compiler complains. Tidy up other parts of the code.
This commit is contained in:
@ -5614,9 +5614,6 @@ static int _sp_add_d(sp_int* a, sp_int_digit d, sp_int* r)
|
|||||||
*/
|
*/
|
||||||
static void _sp_sub_d(sp_int* a, sp_int_digit d, sp_int* r)
|
static void _sp_sub_d(sp_int* a, sp_int_digit d, sp_int* r)
|
||||||
{
|
{
|
||||||
int i = 0;
|
|
||||||
sp_int_digit a0 = a->dp[0];
|
|
||||||
|
|
||||||
/* Set result used to be same as input. Updated with clamp. */
|
/* Set result used to be same as input. Updated with clamp. */
|
||||||
r->used = a->used;
|
r->used = a->used;
|
||||||
/* Only possible when not handling negatives. */
|
/* Only possible when not handling negatives. */
|
||||||
@ -5625,6 +5622,9 @@ static void _sp_sub_d(sp_int* a, sp_int_digit d, sp_int* r)
|
|||||||
r->dp[0] = 0;
|
r->dp[0] = 0;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
int i = 0;
|
||||||
|
sp_int_digit a0 = a->dp[0];
|
||||||
|
|
||||||
r->dp[0] = a0 - d;
|
r->dp[0] = a0 - d;
|
||||||
/* Check for borrow. */
|
/* Check for borrow. */
|
||||||
if (r->dp[0] > a0) {
|
if (r->dp[0] > a0) {
|
||||||
@ -7796,9 +7796,12 @@ static int _sp_div(sp_int* a, sp_int* d, sp_int* r, sp_int* trial)
|
|||||||
/* Set result size to clear. */
|
/* Set result size to clear. */
|
||||||
r->used = a->used - d->used + 1;
|
r->used = a->used - d->used + 1;
|
||||||
/* Set all potentially used digits to zero. */
|
/* Set all potentially used digits to zero. */
|
||||||
sp_clear(r);
|
for (i = 0; i < r->used; i++) {
|
||||||
/* Set result size. */
|
r->dp[i] = 0;
|
||||||
r->used = a->used - d->used + 1;
|
}
|
||||||
|
#ifdef WOLFSSL_SP_INT_NEGATIVE
|
||||||
|
r->sign = MP_ZPOS;
|
||||||
|
#endif
|
||||||
/* Get the most significant digit (will have top bit set). */
|
/* Get the most significant digit (will have top bit set). */
|
||||||
dt = d->dp[d->used-1];
|
dt = d->dp[d->used-1];
|
||||||
|
|
||||||
@ -17716,7 +17719,7 @@ static WC_INLINE int sp_div_primes(sp_int* a, int* haveRes, int* result)
|
|||||||
/* Do trial division of a with all known small primes. */
|
/* Do trial division of a with all known small primes. */
|
||||||
for (i = 0; i < SP_PRIME_SIZE; i++) {
|
for (i = 0; i < SP_PRIME_SIZE; i++) {
|
||||||
/* Small prime divides a when remainder is 0. */
|
/* Small prime divides a when remainder is 0. */
|
||||||
err = sp_mod_d(a, sp_primes[i], &d);
|
err = sp_mod_d(a, (sp_int_digit)sp_primes[i], &d);
|
||||||
if ((err != MP_OKAY) || (d == 0)) {
|
if ((err != MP_OKAY) || (d == 0)) {
|
||||||
*result = MP_NO;
|
*result = MP_NO;
|
||||||
*haveRes = 1;
|
*haveRes = 1;
|
||||||
@ -17726,7 +17729,7 @@ static WC_INLINE int sp_div_primes(sp_int* a, int* haveRes, int* result)
|
|||||||
#else
|
#else
|
||||||
/* Start with first prime in composite. */
|
/* Start with first prime in composite. */
|
||||||
i = 0;
|
i = 0;
|
||||||
for (j = 0; (!*haveRes) && (j < SP_COMP_CNT); j++) {
|
for (j = 0; (!(*haveRes)) && (j < SP_COMP_CNT); j++) {
|
||||||
/* Reduce a down to a single word. */
|
/* Reduce a down to a single word. */
|
||||||
err = sp_mod_d(a, sp_comp[j], &d);
|
err = sp_mod_d(a, sp_comp[j], &d);
|
||||||
if ((err != MP_OKAY) || (d == 0)) {
|
if ((err != MP_OKAY) || (d == 0)) {
|
||||||
@ -17891,7 +17894,7 @@ int sp_prime_is_prime_ex(sp_int* a, int trials, int* result, WC_RNG* rng)
|
|||||||
|
|
||||||
/* Check against known small primes when a has 1 digit. */
|
/* Check against known small primes when a has 1 digit. */
|
||||||
if ((err == MP_OKAY) && (!haveRes) && (a->used == 1) &&
|
if ((err == MP_OKAY) && (!haveRes) && (a->used == 1) &&
|
||||||
(a->dp[0] <= sp_primes[SP_PRIME_SIZE - 1])) {
|
(a->dp[0] <= (sp_int_digit)sp_primes[SP_PRIME_SIZE - 1])) {
|
||||||
haveRes = sp_cmp_primes(a, &ret);
|
haveRes = sp_cmp_primes(a, &ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -17940,7 +17943,7 @@ int sp_prime_is_prime_ex(sp_int* a, int trials, int* result, WC_RNG* rng)
|
|||||||
b->dp[b->used-1] >>=
|
b->dp[b->used-1] >>=
|
||||||
SP_WORD_SIZE - ((baseSz * 8) & SP_WORD_MASK);
|
SP_WORD_SIZE - ((baseSz * 8) & SP_WORD_MASK);
|
||||||
}
|
}
|
||||||
#endif /* LITTLE_ENDIAN_ORDER */
|
#endif /* BIG_ENDIAN_ORDER */
|
||||||
|
|
||||||
/* Ensure the top word has no more bits than necessary. */
|
/* Ensure the top word has no more bits than necessary. */
|
||||||
if (bits > 0) {
|
if (bits > 0) {
|
||||||
|
Reference in New Issue
Block a user