Mercurial > dropbear
comparison libtommath/bn_mp_prime_fermat.c @ 1733:d529a52b2f7c coverity coverity
merge coverity from main
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Fri, 26 Jun 2020 21:07:34 +0800 |
parents | 1051e4eea25a |
children |
comparison
equal
deleted
inserted
replaced
1643:b59623a64678 | 1733:d529a52b2f7c |
---|---|
1 #include <tommath_private.h> | 1 #include "tommath_private.h" |
2 #ifdef BN_MP_PRIME_FERMAT_C | 2 #ifdef BN_MP_PRIME_FERMAT_C |
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis | 3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */ |
4 * | 4 /* SPDX-License-Identifier: Unlicense */ |
5 * LibTomMath is a library that provides multiple-precision | |
6 * integer arithmetic as well as number theoretic functionality. | |
7 * | |
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. | |
11 * | |
12 * The library is free for all purposes without any express | |
13 * guarantee it works. | |
14 * | |
15 * Tom St Denis, [email protected], http://libtom.org | |
16 */ | |
17 | 5 |
18 /* performs one Fermat test. | 6 /* performs one Fermat test. |
19 * | 7 * |
20 * If "a" were prime then b**a == b (mod a) since the order of | 8 * If "a" were prime then b**a == b (mod a) since the order of |
21 * the multiplicative sub-group would be phi(a) = a-1. That means | 9 * the multiplicative sub-group would be phi(a) = a-1. That means |
22 * it would be the same as b**(a mod (a-1)) == b**1 == b (mod a). | 10 * it would be the same as b**(a mod (a-1)) == b**1 == b (mod a). |
23 * | 11 * |
24 * Sets result to 1 if the congruence holds, or zero otherwise. | 12 * Sets result to 1 if the congruence holds, or zero otherwise. |
25 */ | 13 */ |
26 int mp_prime_fermat (mp_int * a, mp_int * b, int *result) | 14 mp_err mp_prime_fermat(const mp_int *a, const mp_int *b, mp_bool *result) |
27 { | 15 { |
28 mp_int t; | 16 mp_int t; |
29 int err; | 17 mp_err err; |
30 | 18 |
31 /* default to composite */ | 19 /* default to composite */ |
32 *result = MP_NO; | 20 *result = MP_NO; |
33 | 21 |
34 /* ensure b > 1 */ | 22 /* ensure b > 1 */ |
35 if (mp_cmp_d(b, 1) != MP_GT) { | 23 if (mp_cmp_d(b, 1uL) != MP_GT) { |
36 return MP_VAL; | 24 return MP_VAL; |
37 } | 25 } |
38 | 26 |
39 /* init t */ | 27 /* init t */ |
40 if ((err = mp_init (&t)) != MP_OKAY) { | 28 if ((err = mp_init(&t)) != MP_OKAY) { |
41 return err; | 29 return err; |
42 } | 30 } |
43 | 31 |
44 /* compute t = b**a mod a */ | 32 /* compute t = b**a mod a */ |
45 if ((err = mp_exptmod (b, a, a, &t)) != MP_OKAY) { | 33 if ((err = mp_exptmod(b, a, a, &t)) != MP_OKAY) { |
46 goto LBL_T; | 34 goto LBL_T; |
47 } | 35 } |
48 | 36 |
49 /* is it equal to b? */ | 37 /* is it equal to b? */ |
50 if (mp_cmp (&t, b) == MP_EQ) { | 38 if (mp_cmp(&t, b) == MP_EQ) { |
51 *result = MP_YES; | 39 *result = MP_YES; |
52 } | 40 } |
53 | 41 |
54 err = MP_OKAY; | 42 err = MP_OKAY; |
55 LBL_T:mp_clear (&t); | 43 LBL_T: |
56 return err; | 44 mp_clear(&t); |
45 return err; | |
57 } | 46 } |
58 #endif | 47 #endif |
59 | |
60 /* ref: $Format:%D$ */ | |
61 /* git commit: $Format:%H$ */ | |
62 /* commit time: $Format:%ai$ */ |