Merge pull request #2618 from ejohnstown/maintenance-prime

Maintenance: Prime
This commit is contained in:
toddouska
2019-11-27 14:06:23 -08:00
committed by GitHub
4 changed files with 60 additions and 5 deletions

View File

@ -4717,10 +4717,15 @@ int mp_prime_is_prime (mp_int * a, int t, int *result)
return MP_VAL;
}
if (mp_isone(a)) {
*result = MP_NO;
return MP_OKAY;
}
/* is the input equal to one of the primes in the table? */
for (ix = 0; ix < PRIME_SIZE; ix++) {
if (mp_cmp_d(a, ltm_prime_tab[ix]) == MP_EQ) {
*result = 1;
*result = MP_YES;
return MP_OKAY;
}
}
@ -4780,6 +4785,11 @@ int mp_prime_is_prime_ex (mp_int * a, int t, int *result, WC_RNG *rng)
return MP_VAL;
}
if (mp_isone(a)) {
*result = MP_NO;
return MP_OKAY;
}
/* is the input equal to one of the primes in the table? */
for (ix = 0; ix < PRIME_SIZE; ix++) {
if (mp_cmp_d(a, ltm_prime_tab[ix]) == MP_EQ) {

View File

@ -1703,6 +1703,11 @@ int sp_prime_is_prime(sp_int *a, int t, int* result)
err = MP_VAL;
}
if (sp_isone(a)) {
*result = MP_NO;
return MP_OKAY;
}
if (err == MP_OKAY && a->used == 1) {
/* check against primes table */
for (i = 0; i < SP_PRIME_SIZE; i++) {
@ -1783,6 +1788,11 @@ int sp_prime_is_prime_ex(sp_int* a, int t, int* result, WC_RNG* rng)
if (a == NULL || result == NULL || rng == NULL)
err = MP_VAL;
if (sp_isone(a)) {
*result = MP_NO;
return MP_OKAY;
}
if (err == MP_OKAY && a->used == 1) {
/* check against primes table */
for (i = 0; i < SP_PRIME_SIZE; i++) {

View File

@ -4096,6 +4096,11 @@ int fp_isprime_ex(fp_int *a, int t, int* result)
return FP_VAL;
}
if (fp_isone(a)) {
*result = FP_NO;
return FP_OKAY;
}
/* check against primes table */
for (r = 0; r < FP_PRIME_SIZE; r++) {
if (fp_cmp_d(a, primes[r]) == FP_EQ) {
@ -4146,6 +4151,11 @@ int mp_prime_is_prime_ex(mp_int* a, int t, int* result, WC_RNG* rng)
if (a == NULL || result == NULL || rng == NULL)
return FP_VAL;
if (fp_isone(a)) {
*result = FP_NO;
return FP_OKAY;
}
if (ret == FP_YES) {
fp_digit d;
int r;

View File

@ -1080,7 +1080,7 @@ initDefaultName();
test_pass("mp test passed!\n");
#endif
#if defined(WOLFSSL_PUBLIC_MP) && !defined(WOLFSSL_SP_MATH)
#if defined(WOLFSSL_PUBLIC_MP)
if ( (ret = prime_test()) != 0)
return err_sys("prime test failed!\n", ret);
else
@ -24568,7 +24568,7 @@ done:
#endif
#if defined(WOLFSSL_PUBLIC_MP) && !defined(WOLFSSL_SP_MATH)
#if defined(WOLFSSL_PUBLIC_MP)
typedef struct pairs_t {
const unsigned char* coeff;
@ -24623,16 +24623,24 @@ static const unsigned char controlPrime[] = {
0xe6, 0xd1, 0x1b, 0x5d, 0x5e, 0x96, 0xfa, 0x53
};
static const unsigned char testOne[] = { 1 };
static int GenerateNextP(mp_int* p1, mp_int* p2, int k)
{
int ret;
mp_int ki;
ret = mp_sub_d(p1, 1, p2);
ret = mp_init(&ki);
if (ret == 0)
ret = mp_mul_d(p2, k, p2);
ret = mp_set(&ki, k);
if (ret == 0)
ret = mp_sub_d(p1, 1, p2);
if (ret == 0)
ret = mp_mul(p2, &ki, p2);
if (ret == 0)
ret = mp_add_d(p2, 1, p2);
mp_clear(&ki);
return ret;
}
@ -24738,6 +24746,23 @@ int prime_test(void)
if (!isPrime)
return -9661;
ret = mp_read_unsigned_bin(&n, testOne, sizeof(testOne));
if (ret != 0)
return -9662;
/* This test result should indicate the value as not prime. */
ret = mp_prime_is_prime_ex(&n, 8, &isPrime, &rng);
if (ret != 0)
return -9663;
if (isPrime)
return -9664;
ret = mp_prime_is_prime(&n, 8, &isPrime);
if (ret != 0)
return -9665;
if (isPrime)
return -9666;
mp_clear(&p3);
mp_clear(&p2);
mp_clear(&p1);