# BRCM_VERSION=3
[bcm963xx.git] / userapps / opensource / sshd / libtommath / bn_mp_prime_fermat.c
1 /* LibTomMath, multiple-precision integer library -- Tom St Denis
2  *
3  * LibTomMath is library that provides for multiple-precision
4  * integer arithmetic as well as number theoretic functionality.
5  *
6  * The library is 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, tomstdenis@iahu.ca, 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
26 mp_prime_fermat (mp_int * a, mp_int * b, int *result)
27 {
28   mp_int  t;
29   int     err;
30
31   /* default to fail */
32   *result = 0;
33
34   /* init t */
35   if ((err = mp_init (&t)) != MP_OKAY) {
36     return err;
37   }
38
39   /* compute t = b^a mod a */
40   if ((err = mp_exptmod (b, a, a, &t)) != MP_OKAY) {
41     goto __T;
42   }
43
44   /* is it equal to b? */
45   if (mp_cmp (&t, b) == MP_EQ) {
46     *result = 1;
47   }
48
49   err = MP_OKAY;
50 __T:mp_clear (&t);
51   return err;
52 }