# BRCM_VERSION=3
[bcm963xx.git] / userapps / opensource / sshd / libtommath / bn_mp_prime_is_divisible.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 /* determines if an integers is divisible by one of the first 256 primes or not
18  *
19  * sets result to 0 if not, 1 if yes
20  */
21 int
22 mp_prime_is_divisible (mp_int * a, int *result)
23 {
24   int     err, ix;
25   mp_digit res;
26
27   /* default to not */
28   *result = 0;
29
30   for (ix = 0; ix < PRIME_SIZE; ix++) {
31     /* is it equal to the prime? */
32     if (mp_cmp_d (a, __prime_tab[ix]) == MP_EQ) {
33       *result = 1;
34       return MP_OKAY;
35     }
36
37     /* what is a mod __prime_tab[ix] */
38     if ((err = mp_mod_d (a, __prime_tab[ix], &res)) != MP_OKAY) {
39       return err;
40     }
41
42     /* is the residue zero? */
43     if (res == 0) {
44       *result = 1;
45       return MP_OKAY;
46     }
47   }
48
49   return MP_OKAY;
50 }