Mercurial > dropbear
comparison bn_mp_prime_fermat.c @ 1:22d5cf7d4b1a libtommath
Renaming branch
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Mon, 31 May 2004 18:23:46 +0000 |
parents | |
children | d29b64170cf0 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 1:22d5cf7d4b1a |
---|---|
1 /* LibTomMath, multiple-precision integer library -- Tom St Denis | |
2 * | |
3 * LibTomMath is a library that provides multiple-precision | |
4 * integer arithmetic as well as number theoretic functionality. | |
5 * | |
6 * The library was designed directly after the MPI library by | |
7 * Michael Fromberger but has been written from scratch with | |
8 * additional optimizations in place. | |
9 * | |
10 * The library is free for all purposes without any express | |
11 * guarantee it works. | |
12 * | |
13 * Tom St Denis, [email protected], http://math.libtomcrypt.org | |
14 */ | |
15 #include <tommath.h> | |
16 | |
17 /* performs one Fermat test. | |
18 * | |
19 * If "a" were prime then b**a == b (mod a) since the order of | |
20 * the multiplicative sub-group would be phi(a) = a-1. That means | |
21 * it would be the same as b**(a mod (a-1)) == b**1 == b (mod a). | |
22 * | |
23 * Sets result to 1 if the congruence holds, or zero otherwise. | |
24 */ | |
25 int mp_prime_fermat (mp_int * a, mp_int * b, int *result) | |
26 { | |
27 mp_int t; | |
28 int err; | |
29 | |
30 /* default to composite */ | |
31 *result = MP_NO; | |
32 | |
33 /* ensure b > 1 */ | |
34 if (mp_cmp_d(b, 1) != MP_GT) { | |
35 return MP_VAL; | |
36 } | |
37 | |
38 /* init t */ | |
39 if ((err = mp_init (&t)) != MP_OKAY) { | |
40 return err; | |
41 } | |
42 | |
43 /* compute t = b**a mod a */ | |
44 if ((err = mp_exptmod (b, a, a, &t)) != MP_OKAY) { | |
45 goto __T; | |
46 } | |
47 | |
48 /* is it equal to b? */ | |
49 if (mp_cmp (&t, b) == MP_EQ) { | |
50 *result = MP_YES; | |
51 } | |
52 | |
53 err = MP_OKAY; | |
54 __T:mp_clear (&t); | |
55 return err; | |
56 } |