2 #ifdef BN_MP_PRIME_IS_PRIME_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis
5 * LibTomMath is a library that provides multiple-precision
6 * integer arithmetic as well as number theoretic functionality.
8 * The library was designed directly after the MPI library by
9 * Michael Fromberger but has been written from scratch with
10 * additional optimizations in place.
12 * The library is free for all purposes without any express
15 * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
18 /* performs a variable number of rounds of Miller-Rabin
20 * Probability of error after t rounds is no more than
23 * Sets result to 1 if probably prime, 0 otherwise
25 int mp_prime_is_prime (mp_int
* a
, int t
, int *result
)
33 /* valid value of t? */
34 if (t
<= 0 || t
> PRIME_SIZE
) {
38 /* is the input equal to one of the primes in the table? */
39 for (ix
= 0; ix
< PRIME_SIZE
; ix
++) {
40 if (mp_cmp_d(a
, ltm_prime_tab
[ix
]) == MP_EQ
) {
46 /* first perform trial division */
47 if ((err
= mp_prime_is_divisible (a
, &res
)) != MP_OKAY
) {
51 /* return if it was trivially divisible */
56 /* now perform the miller-rabin rounds */
57 if ((err
= mp_init (&b
)) != MP_OKAY
) {
61 for (ix
= 0; ix
< t
; ix
++) {
63 mp_set (&b
, ltm_prime_tab
[ix
]);
65 if ((err
= mp_prime_miller_rabin (a
, &b
, &res
)) != MP_OKAY
) {
81 /* $Source: /cvs/libtom/libtommath/bn_mp_prime_is_prime.c,v $ */
82 /* $Revision: 1.3 $ */
83 /* $Date: 2006/03/31 14:18:44 $ */