Mercurial > dropbear
comparison libtommath/bn_mp_prime_fermat.c @ 1655:f52919ffd3b1
update ltm to 1.1.0 and enable FIPS 186.4 compliant key-generation (#79)
* make key-generation compliant to FIPS 186.4
* fix includes in tommath_class.h
* update fuzzcorpus instead of error-out
* fixup fuzzing make-targets
* update Makefile.in
* apply necessary patches to ltm sources
* clean-up not required ltm files
* update to vanilla ltm 1.1.0
this already only contains the required files
* remove set/get double
author | Steffen Jaeckel <s_jaeckel@gmx.de> |
---|---|
date | Mon, 16 Sep 2019 15:50:38 +0200 |
parents | 8bba51a55704 |
children | 1051e4eea25a |
comparison
equal
deleted
inserted
replaced
1654:cc0fc5131c5c | 1655:f52919ffd3b1 |
---|---|
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 * |
5 * LibTomMath is a library that provides multiple-precision | 5 * LibTomMath is a library that provides multiple-precision |
6 * integer arithmetic as well as number theoretic functionality. | 6 * integer arithmetic as well as number theoretic functionality. |
7 * | 7 * |
8 * The library was designed directly after the MPI library by | 8 * The library was designed directly after the MPI library by |
9 * Michael Fromberger but has been written from scratch with | 9 * Michael Fromberger but has been written from scratch with |
10 * additional optimizations in place. | 10 * additional optimizations in place. |
11 * | 11 * |
12 * The library is free for all purposes without any express | 12 * SPDX-License-Identifier: Unlicense |
13 * guarantee it works. | |
14 * | |
15 * Tom St Denis, [email protected], http://libtom.org | |
16 */ | 13 */ |
17 | 14 |
18 /* performs one Fermat test. | 15 /* performs one Fermat test. |
19 * | 16 * |
20 * If "a" were prime then b**a == b (mod a) since the order of | 17 * 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 | 18 * 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). | 19 * it would be the same as b**(a mod (a-1)) == b**1 == b (mod a). |
23 * | 20 * |
24 * Sets result to 1 if the congruence holds, or zero otherwise. | 21 * Sets result to 1 if the congruence holds, or zero otherwise. |
25 */ | 22 */ |
26 int mp_prime_fermat (mp_int * a, mp_int * b, int *result) | 23 int mp_prime_fermat(const mp_int *a, const mp_int *b, int *result) |
27 { | 24 { |
28 mp_int t; | 25 mp_int t; |
29 int err; | 26 int err; |
30 | 27 |
31 /* default to composite */ | 28 /* default to composite */ |
32 *result = MP_NO; | 29 *result = MP_NO; |
33 | 30 |
34 /* ensure b > 1 */ | 31 /* ensure b > 1 */ |
35 if (mp_cmp_d(b, 1) != MP_GT) { | 32 if (mp_cmp_d(b, 1uL) != MP_GT) { |
36 return MP_VAL; | 33 return MP_VAL; |
37 } | 34 } |
38 | 35 |
39 /* init t */ | 36 /* init t */ |
40 if ((err = mp_init (&t)) != MP_OKAY) { | 37 if ((err = mp_init(&t)) != MP_OKAY) { |
41 return err; | 38 return err; |
42 } | 39 } |
43 | 40 |
44 /* compute t = b**a mod a */ | 41 /* compute t = b**a mod a */ |
45 if ((err = mp_exptmod (b, a, a, &t)) != MP_OKAY) { | 42 if ((err = mp_exptmod(b, a, a, &t)) != MP_OKAY) { |
46 goto LBL_T; | 43 goto LBL_T; |
47 } | 44 } |
48 | 45 |
49 /* is it equal to b? */ | 46 /* is it equal to b? */ |
50 if (mp_cmp (&t, b) == MP_EQ) { | 47 if (mp_cmp(&t, b) == MP_EQ) { |
51 *result = MP_YES; | 48 *result = MP_YES; |
52 } | 49 } |
53 | 50 |
54 err = MP_OKAY; | 51 err = MP_OKAY; |
55 LBL_T:mp_clear (&t); | 52 LBL_T: |
56 return err; | 53 mp_clear(&t); |
54 return err; | |
57 } | 55 } |
58 #endif | 56 #endif |
59 | 57 |
60 /* ref: $Format:%D$ */ | 58 /* ref: HEAD -> master, tag: v1.1.0 */ |
61 /* git commit: $Format:%H$ */ | 59 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */ |
62 /* commit time: $Format:%ai$ */ | 60 /* commit time: 2019-01-28 20:32:32 +0100 */ |