diff --git a/wolfcrypt/src/ecc.c b/wolfcrypt/src/ecc.c index d2cd04a2f..400136024 100644 --- a/wolfcrypt/src/ecc.c +++ b/wolfcrypt/src/ecc.c @@ -1069,7 +1069,7 @@ int ecc_projective_add_point(ecc_point* P, ecc_point* Q, ecc_point* R, /* if Z is one then these are no-operations */ if (err == MP_OKAY) { - if (get_digit_count(Q->z)) { + if (!mp_iszero(Q->z)) { /* T1 = Z' * Z' */ err = mp_sqr(Q->z, &t1); if (err == MP_OKAY) @@ -1123,7 +1123,7 @@ int ecc_projective_add_point(ecc_point* P, ecc_point* Q, ecc_point* R, if (err == MP_OKAY) err = mp_sub(y, &t1, y); if (err == MP_OKAY) { - if (mp_cmp_d(y, 0) == MP_LT) + if (mp_isneg(y)) err = mp_add(y, modulus, y); } /* T1 = 2T1 */ @@ -1144,7 +1144,7 @@ int ecc_projective_add_point(ecc_point* P, ecc_point* Q, ecc_point* R, if (err == MP_OKAY) err = mp_sub(x, &t2, x); if (err == MP_OKAY) { - if (mp_cmp_d(x, 0) == MP_LT) + if (mp_isneg(x)) err = mp_add(x, modulus, x); } /* T2 = 2T2 */ @@ -1163,7 +1163,7 @@ int ecc_projective_add_point(ecc_point* P, ecc_point* Q, ecc_point* R, } if (err == MP_OKAY) { - if (get_digit_count(Q->z)) { + if (!mp_iszero(Q->z)) { /* Z = Z * Z' */ err = mp_mul(z, Q->z, z); if (err == MP_OKAY) @@ -1211,21 +1211,21 @@ int ecc_projective_add_point(ecc_point* P, ecc_point* Q, ecc_point* R, if (err == MP_OKAY) err = mp_sub(x, &t2, x); if (err == MP_OKAY) { - if (mp_cmp_d(x, 0) == MP_LT) + if (mp_isneg(x)) err = mp_add(x, modulus, x); } /* T2 = T2 - X */ if (err == MP_OKAY) err = mp_sub(&t2, x, &t2); if (err == MP_OKAY) { - if (mp_cmp_d(&t2, 0) == MP_LT) + if (mp_isneg(&t2)) err = mp_add(&t2, modulus, &t2); } /* T2 = T2 - X */ if (err == MP_OKAY) err = mp_sub(&t2, x, &t2); if (err == MP_OKAY) { - if (mp_cmp_d(&t2, 0) == MP_LT) + if (mp_isneg(&t2)) err = mp_add(&t2, modulus, &t2); } /* T2 = T2 * Y */ @@ -1238,7 +1238,7 @@ int ecc_projective_add_point(ecc_point* P, ecc_point* Q, ecc_point* R, if (err == MP_OKAY) err = mp_sub(&t2, &t1, y); if (err == MP_OKAY) { - if (mp_cmp_d(y, 0) == MP_LT) + if (mp_isneg(y)) err = mp_add(y, modulus, y); } /* Y = Y/2 */ @@ -1405,7 +1405,7 @@ int ecc_projective_dbl_point(ecc_point *P, ecc_point *R, mp_int* a, if (err == MP_OKAY) err = mp_sub(x, &t1, &t2); if (err == MP_OKAY) { - if (mp_cmp_d(&t2, 0) == MP_LT) + if (mp_isneg(&t2)) err = mp_add(&t2, modulus, &t2); } /* T1 = X + T1 */ @@ -1480,14 +1480,14 @@ int ecc_projective_dbl_point(ecc_point *P, ecc_point *R, mp_int* a, if (err == MP_OKAY) err = mp_sub(x, y, x); if (err == MP_OKAY) { - if (mp_cmp_d(x, 0) == MP_LT) + if (mp_isneg(x)) err = mp_add(x, modulus, x); } /* X = X - Y */ if (err == MP_OKAY) err = mp_sub(x, y, x); if (err == MP_OKAY) { - if (mp_cmp_d(x, 0) == MP_LT) + if (mp_isneg(x)) err = mp_add(x, modulus, x); } @@ -1495,7 +1495,7 @@ int ecc_projective_dbl_point(ecc_point *P, ecc_point *R, mp_int* a, if (err == MP_OKAY) err = mp_sub(y, x, y); if (err == MP_OKAY) { - if (mp_cmp_d(y, 0) == MP_LT) + if (mp_isneg(y)) err = mp_add(y, modulus, y); } /* Y = Y * T1 */ @@ -1508,7 +1508,7 @@ int ecc_projective_dbl_point(ecc_point *P, ecc_point *R, mp_int* a, if (err == MP_OKAY) err = mp_sub(y, &t2, y); if (err == MP_OKAY) { - if (mp_cmp_d(y, 0) == MP_LT) + if (mp_isneg(y)) err = mp_add(y, modulus, y); } @@ -2007,7 +2007,7 @@ int wc_ecc_mulmod_ex(mp_int* k, ecc_point *G, ecc_point *R, --digidx; } - /* grab the next msb from the ltiplicand */ + /* grab the next msb from the multiplicand */ i = (buf >> (DIGIT_BIT - 1)) & 1; buf <<= 1; @@ -3732,7 +3732,7 @@ static int ecc_is_point(const ecc_set_type* dp, ecc_point* ecp, mp_int* prime) #endif /* WOLFSSL_CUSTOM_CURVES */ /* adjust range (0, prime) */ - while (err == MP_OKAY && mp_cmp_d(&t1, 0) == MP_LT) { + while (err == MP_OKAY && mp_isneg(&t1)) { err = mp_add(&t1, prime, &t1); } while (err == MP_OKAY && mp_cmp(&t1, prime) != MP_LT) { diff --git a/wolfcrypt/src/tfm.c b/wolfcrypt/src/tfm.c index 7079fb9f3..59b809316 100644 --- a/wolfcrypt/src/tfm.c +++ b/wolfcrypt/src/tfm.c @@ -2365,11 +2365,6 @@ int mp_iszero(mp_int* a) return fp_iszero(a); } -int mp_isneg(mp_int* a) -{ - return fp_isneg(a); -} - int mp_count_bits (mp_int* a) { return fp_count_bits(a); diff --git a/wolfssl/wolfcrypt/integer.h b/wolfssl/wolfcrypt/integer.h index 3aa808b88..c965330ea 100644 --- a/wolfssl/wolfcrypt/integer.h +++ b/wolfssl/wolfcrypt/integer.h @@ -199,6 +199,8 @@ typedef int ltm_prime_callback(unsigned char *dst, int len, void *dat); /* ---> Basic Manipulations <--- */ #define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO) +#define mp_isone(a) \ + (((((a)->used == 1)) && ((a)->dp[0] == 1u)) ? MP_YES : MP_NO) #define mp_iseven(a) \ (((a)->used > 0 && (((a)->dp[0] & 1u) == 0u)) ? MP_YES : MP_NO) #define mp_isodd(a) \ diff --git a/wolfssl/wolfcrypt/tfm.h b/wolfssl/wolfcrypt/tfm.h index 4d1661f99..79de9bc72 100644 --- a/wolfssl/wolfcrypt/tfm.h +++ b/wolfssl/wolfcrypt/tfm.h @@ -382,6 +382,8 @@ typedef struct { /* zero/even/odd ? */ #define fp_iszero(a) (((a)->used == 0) ? FP_YES : FP_NO) +#define fp_isone(a) \ + ((((a)->used == 1) && ((a)->dp[0] == 1)) ? FP_YES : FP_NO) #define fp_iseven(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 0)) ? FP_YES : FP_NO) #define fp_isodd(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? FP_YES : FP_NO) #define fp_isneg(a) (((a)->sign != 0) ? FP_YES : FP_NO) @@ -619,7 +621,9 @@ typedef fp_int mp_int; /* Prototypes */ #define mp_zero(a) fp_zero(a) +#define mp_isone(a) fp_isone(a) #define mp_iseven(a) fp_iseven(a) +#define mp_isneg(a) fp_isneg(a) int mp_init (mp_int * a); void mp_clear (mp_int * a); #define mp_forcezero(a) fp_clear(a) @@ -650,7 +654,6 @@ int mp_sub_d(fp_int *a, fp_digit b, fp_int *c); int mp_copy(fp_int* a, fp_int* b); int mp_isodd(mp_int* a); int mp_iszero(mp_int* a); -int mp_isneg(mp_int* a); int mp_count_bits(mp_int *a); int mp_leading_bit(mp_int *a); int mp_set_int(mp_int *a, mp_digit b);