Skip to content

Commit e11f70f

Browse files
authored
Merge pull request #269 from libtom/fix-miller-rabin-trials
do 2 MR rounds for numbers >=2048bits
2 parents b31a108 + 51cda5b commit e11f70f

File tree

3 files changed

+5
-11
lines changed

3 files changed

+5
-11
lines changed

bn_mp_prime_is_prime.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ mp_err mp_prime_is_prime(const mp_int *a, int t, mp_bool *result)
2525
/* default to no */
2626
*result = MP_NO;
2727

28-
/* valid value of t? */
29-
if (t > MP_PRIME_SIZE) {
30-
return MP_VAL;
31-
}
32-
3328
/* Some shortcuts */
3429
/* N > 3 */
3530
if (a->used == 1) {

bn_mp_prime_rabin_miller_trials.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ static const struct {
1919
{ 768, 5 },
2020
{ 896, 4 },
2121
{ 1024, 4 },
22-
{ 2048, 2 },
23-
{ 4096, 1 },
22+
{ 2048, 2 } /* For bigger keysizes use always at least 2 Rounds */
2423
};
2524

2625
/* returns # of RM trials required for a given bit size and max. error of 2^(-96)*/
@@ -35,7 +34,7 @@ int mp_prime_rabin_miller_trials(int size)
3534
return (x == 0) ? sizes[0].t : sizes[x - 1].t;
3635
}
3736
}
38-
return sizes[x-1].t + 1;
37+
return sizes[x-1].t;
3938
}
4039

4140

demo/test.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -890,7 +890,7 @@ static int test_mp_prime_is_prime(void)
890890
mp_read_radix(&a,
891891
"91xLNF3roobhzgTzoFIG6P13ZqhOVYSN60Fa7Cj2jVR1g0k89zdahO9/kAiRprpfO1VAp1aBHucLFV/qLKLFb+zonV7R2Vxp1K13ClwUXStpV0oxTNQVjwybmFb5NBEHImZ6V7P6+udRJuH8VbMEnS0H8/pSqQrg82OoQQ2fPpAk6G1hkjqoCv5s/Yr",
892892
64);
893-
mp_prime_is_prime(&a, 8, &cnt);
893+
mp_prime_is_prime(&a, mp_prime_rabin_miller_trials(mp_count_bits(&a)), &cnt);
894894
if (cnt == MP_YES) {
895895
printf("Arnault's pseudoprime is not prime but mp_prime_is_prime says it is.\n");
896896
goto LBL_ERR;
@@ -900,7 +900,7 @@ static int test_mp_prime_is_prime(void)
900900
mp_set(&a, 1uL);
901901
mp_mul_2d(&a,1119,&a);
902902
mp_add_d(&a, 53uL, &a);
903-
err = mp_prime_is_prime(&a, 8, &cnt);
903+
err = mp_prime_is_prime(&a, mp_prime_rabin_miller_trials(mp_count_bits(&a)), &cnt);
904904
/* small problem */
905905
if (err != MP_OKAY) {
906906
printf("\nfailed with error: %s\n", mp_error_to_string(err));
@@ -930,7 +930,7 @@ static int test_mp_prime_is_prime(void)
930930
/* let's see if it's really a safe prime */
931931
mp_sub_d(&a, 1uL, &b);
932932
mp_div_2(&b, &b);
933-
err = mp_prime_is_prime(&b, 8, &cnt);
933+
err = mp_prime_is_prime(&b, mp_prime_rabin_miller_trials(mp_count_bits(&b)), &cnt);
934934
/* small problem */
935935
if (err != MP_OKAY) {
936936
printf("\nfailed with error: %s\n", mp_error_to_string(err));

0 commit comments

Comments
 (0)