Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 84 lines (70 sloc) 1.856 kb
e549ccf added libtommath-0.32
Tom St Denis authored
1 #include <tommath.h>
2 #ifdef BN_MP_PRIME_IS_PRIME_C
b1756f2 added libtommath-0.15
Tom St Denis authored
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis
4 *
c1da6aa added libtommath-0.25
Tom St Denis authored
5 * LibTomMath is a library that provides multiple-precision
b1756f2 added libtommath-0.15
Tom St Denis authored
6 * integer arithmetic as well as number theoretic functionality.
7 *
c1da6aa added libtommath-0.25
Tom St Denis authored
8 * The library was designed directly after the MPI library by
b1756f2 added libtommath-0.15
Tom St Denis authored
9 * Michael Fromberger but has been written from scratch with
10 * additional optimizations in place.
11 *
12 * The library is free for all purposes without any express
13 * guarantee it works.
14 *
333aebc added libtommath-0.41
Tom St Denis authored
15 * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
b1756f2 added libtommath-0.15
Tom St Denis authored
16 */
17
18 /* performs a variable number of rounds of Miller-Rabin
19 *
20 * Probability of error after t rounds is no more than
e549ccf added libtommath-0.32
Tom St Denis authored
21
b1756f2 added libtommath-0.15
Tom St Denis authored
22 *
23 * Sets result to 1 if probably prime, 0 otherwise
24 */
455bb4d added libtommath-0.28
Tom St Denis authored
25 int mp_prime_is_prime (mp_int * a, int t, int *result)
b1756f2 added libtommath-0.15
Tom St Denis authored
26 {
27 mp_int b;
28 int ix, err, res;
29
30 /* default to no */
455bb4d added libtommath-0.28
Tom St Denis authored
31 *result = MP_NO;
b1756f2 added libtommath-0.15
Tom St Denis authored
32
33 /* valid value of t? */
eed6765 added libtommath-0.23
Tom St Denis authored
34 if (t <= 0 || t > PRIME_SIZE) {
b1756f2 added libtommath-0.15
Tom St Denis authored
35 return MP_VAL;
36 }
37
fd181cc added libtommath-0.17
Tom St Denis authored
38 /* is the input equal to one of the primes in the table? */
39 for (ix = 0; ix < PRIME_SIZE; ix++) {
4b7111d added libtommath-0.33
Tom St Denis authored
40 if (mp_cmp_d(a, ltm_prime_tab[ix]) == MP_EQ) {
fd181cc added libtommath-0.17
Tom St Denis authored
41 *result = 1;
42 return MP_OKAY;
43 }
44 }
45
b1756f2 added libtommath-0.15
Tom St Denis authored
46 /* first perform trial division */
47 if ((err = mp_prime_is_divisible (a, &res)) != MP_OKAY) {
48 return err;
49 }
eed6765 added libtommath-0.23
Tom St Denis authored
50
51 /* return if it was trivially divisible */
455bb4d added libtommath-0.28
Tom St Denis authored
52 if (res == MP_YES) {
b1756f2 added libtommath-0.15
Tom St Denis authored
53 return MP_OKAY;
54 }
55
56 /* now perform the miller-rabin rounds */
57 if ((err = mp_init (&b)) != MP_OKAY) {
58 return err;
59 }
60
61 for (ix = 0; ix < t; ix++) {
62 /* set the prime */
4b7111d added libtommath-0.33
Tom St Denis authored
63 mp_set (&b, ltm_prime_tab[ix]);
b1756f2 added libtommath-0.15
Tom St Denis authored
64
65 if ((err = mp_prime_miller_rabin (a, &b, &res)) != MP_OKAY) {
4b7111d added libtommath-0.33
Tom St Denis authored
66 goto LBL_B;
b1756f2 added libtommath-0.15
Tom St Denis authored
67 }
68
455bb4d added libtommath-0.28
Tom St Denis authored
69 if (res == MP_NO) {
4b7111d added libtommath-0.33
Tom St Denis authored
70 goto LBL_B;
b1756f2 added libtommath-0.15
Tom St Denis authored
71 }
72 }
73
74 /* passed the test */
455bb4d added libtommath-0.28
Tom St Denis authored
75 *result = MP_YES;
4b7111d added libtommath-0.33
Tom St Denis authored
76 LBL_B:mp_clear (&b);
b1756f2 added libtommath-0.15
Tom St Denis authored
77 return err;
78 }
e549ccf added libtommath-0.32
Tom St Denis authored
79 #endif
9294e79 added libtommath-0.36
Tom St Denis authored
80
81 /* $Source$ */
82 /* $Revision$ */
83 /* $Date$ */
Something went wrong with that request. Please try again.