Prime Number Testing
1. Update the function wolfSSL_BN_is_prime_ex to use mp_prime_is_prime_ex. 2. Modified fast and normal mp_prime_is_prime_ex() to use random numbers that are in the range 2 < a < n-2.
This commit is contained in:
@@ -4609,9 +4609,10 @@ LBL_B:mp_clear (&b);
|
||||
*/
|
||||
int mp_prime_is_prime_ex (mp_int * a, int t, int *result, WC_RNG *rng)
|
||||
{
|
||||
mp_int b;
|
||||
mp_int b, c;
|
||||
int ix, err, res;
|
||||
byte scratch[16];
|
||||
byte* base = NULL;
|
||||
word32 baseSz = 0;
|
||||
|
||||
/* default to no */
|
||||
*result = MP_NO;
|
||||
@@ -4643,19 +4644,39 @@ int mp_prime_is_prime_ex (mp_int * a, int t, int *result, WC_RNG *rng)
|
||||
if ((err = mp_init (&b)) != MP_OKAY) {
|
||||
return err;
|
||||
}
|
||||
if ((err = mp_init (&c)) != MP_OKAY) {
|
||||
mp_clear(&b);
|
||||
return err;
|
||||
}
|
||||
|
||||
baseSz = mp_count_bits(a);
|
||||
baseSz = (baseSz / 8) + (baseSz % 8) ? 1 : 0;
|
||||
|
||||
base = (byte*)XMALLOC(baseSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||
if (base == NULL) {
|
||||
err = MP_MEM;
|
||||
goto LBL_B;
|
||||
}
|
||||
|
||||
if ((err = mp_copy(a, 2, &c)) != MP_OKAY) {
|
||||
goto LBL_B;
|
||||
}
|
||||
|
||||
/* now do a miller rabin with up to t random numbers, this should
|
||||
* give a (1/4)^t chance of a false prime. */
|
||||
for (ix = 0; ix < t; ix++) {
|
||||
/* Set a test candidate. */
|
||||
if ((err = wc_RNG_GenerateBlock(rng, scratch, sizeof(scratch))) != 0) {
|
||||
if ((err = wc_RNG_GenerateBlock(rng, base, baseSz)) != 0) {
|
||||
goto LBL_B;
|
||||
}
|
||||
|
||||
if ((err = mp_read_unsigned_bin(&b, scratch, sizeof(scratch))) != MP_OKAY) {
|
||||
if ((err = mp_read_unsigned_bin(&b, base, baseSz)) != MP_OKAY) {
|
||||
goto LBL_B;
|
||||
}
|
||||
|
||||
if (mp_cmp_d(&b, 2) != MP_GT || mp_cmp(&b, &c) != MP_LT)
|
||||
continue;
|
||||
|
||||
if ((err = mp_prime_miller_rabin (a, &b, &res)) != MP_OKAY) {
|
||||
goto LBL_B;
|
||||
}
|
||||
@@ -4668,6 +4689,8 @@ int mp_prime_is_prime_ex (mp_int * a, int t, int *result, WC_RNG *rng)
|
||||
/* passed the test */
|
||||
*result = MP_YES;
|
||||
LBL_B:mp_clear (&b);
|
||||
mp_clear (&c);
|
||||
XFREE(base, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user