Mercurial > dropbear
comparison libtommath/bn_mp_gcd.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_GCD_C | 2 #ifdef BN_MP_GCD_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 /* Greatest Common Divisor using the binary method */ | 15 /* Greatest Common Divisor using the binary method */ |
19 int mp_gcd (mp_int * a, mp_int * b, mp_int * c) | 16 int mp_gcd(const mp_int *a, const mp_int *b, mp_int *c) |
20 { | 17 { |
21 mp_int u, v; | 18 mp_int u, v; |
22 int k, u_lsb, v_lsb, res; | 19 int k, u_lsb, v_lsb, res; |
23 | 20 |
24 /* either zero than gcd is the largest */ | 21 /* either zero than gcd is the largest */ |
25 if (mp_iszero (a) == MP_YES) { | 22 if (mp_iszero(a) == MP_YES) { |
26 return mp_abs (b, c); | 23 return mp_abs(b, c); |
27 } | 24 } |
28 if (mp_iszero (b) == MP_YES) { | 25 if (mp_iszero(b) == MP_YES) { |
29 return mp_abs (a, c); | 26 return mp_abs(a, c); |
30 } | 27 } |
31 | 28 |
32 /* get copies of a and b we can modify */ | 29 /* get copies of a and b we can modify */ |
33 if ((res = mp_init_copy (&u, a)) != MP_OKAY) { | 30 if ((res = mp_init_copy(&u, a)) != MP_OKAY) { |
34 return res; | 31 return res; |
35 } | 32 } |
36 | 33 |
37 if ((res = mp_init_copy (&v, b)) != MP_OKAY) { | 34 if ((res = mp_init_copy(&v, b)) != MP_OKAY) { |
38 goto LBL_U; | 35 goto LBL_U; |
39 } | 36 } |
40 | 37 |
41 /* must be positive for the remainder of the algorithm */ | 38 /* must be positive for the remainder of the algorithm */ |
42 u.sign = v.sign = MP_ZPOS; | 39 u.sign = v.sign = MP_ZPOS; |
43 | 40 |
44 /* B1. Find the common power of two for u and v */ | 41 /* B1. Find the common power of two for u and v */ |
45 u_lsb = mp_cnt_lsb(&u); | 42 u_lsb = mp_cnt_lsb(&u); |
46 v_lsb = mp_cnt_lsb(&v); | 43 v_lsb = mp_cnt_lsb(&v); |
47 k = MIN(u_lsb, v_lsb); | 44 k = MIN(u_lsb, v_lsb); |
48 | 45 |
49 if (k > 0) { | 46 if (k > 0) { |
50 /* divide the power of two out */ | 47 /* divide the power of two out */ |
51 if ((res = mp_div_2d(&u, k, &u, NULL)) != MP_OKAY) { | 48 if ((res = mp_div_2d(&u, k, &u, NULL)) != MP_OKAY) { |
52 goto LBL_V; | 49 goto LBL_V; |
53 } | 50 } |
54 | 51 |
55 if ((res = mp_div_2d(&v, k, &v, NULL)) != MP_OKAY) { | 52 if ((res = mp_div_2d(&v, k, &v, NULL)) != MP_OKAY) { |
56 goto LBL_V; | 53 goto LBL_V; |
57 } | 54 } |
58 } | 55 } |
59 | 56 |
60 /* divide any remaining factors of two out */ | 57 /* divide any remaining factors of two out */ |
61 if (u_lsb != k) { | 58 if (u_lsb != k) { |
62 if ((res = mp_div_2d(&u, u_lsb - k, &u, NULL)) != MP_OKAY) { | 59 if ((res = mp_div_2d(&u, u_lsb - k, &u, NULL)) != MP_OKAY) { |
63 goto LBL_V; | 60 goto LBL_V; |
64 } | 61 } |
65 } | 62 } |
66 | 63 |
67 if (v_lsb != k) { | 64 if (v_lsb != k) { |
68 if ((res = mp_div_2d(&v, v_lsb - k, &v, NULL)) != MP_OKAY) { | 65 if ((res = mp_div_2d(&v, v_lsb - k, &v, NULL)) != MP_OKAY) { |
69 goto LBL_V; | 66 goto LBL_V; |
70 } | 67 } |
71 } | 68 } |
72 | 69 |
73 while (mp_iszero(&v) == MP_NO) { | 70 while (mp_iszero(&v) == MP_NO) { |
74 /* make sure v is the largest */ | 71 /* make sure v is the largest */ |
75 if (mp_cmp_mag(&u, &v) == MP_GT) { | 72 if (mp_cmp_mag(&u, &v) == MP_GT) { |
76 /* swap u and v to make sure v is >= u */ | 73 /* swap u and v to make sure v is >= u */ |
77 mp_exch(&u, &v); | 74 mp_exch(&u, &v); |
78 } | 75 } |
79 | |
80 /* subtract smallest from largest */ | |
81 if ((res = s_mp_sub(&v, &u, &v)) != MP_OKAY) { | |
82 goto LBL_V; | |
83 } | |
84 | |
85 /* Divide out all factors of two */ | |
86 if ((res = mp_div_2d(&v, mp_cnt_lsb(&v), &v, NULL)) != MP_OKAY) { | |
87 goto LBL_V; | |
88 } | |
89 } | |
90 | 76 |
91 /* multiply by 2**k which we divided out at the beginning */ | 77 /* subtract smallest from largest */ |
92 if ((res = mp_mul_2d (&u, k, c)) != MP_OKAY) { | 78 if ((res = s_mp_sub(&v, &u, &v)) != MP_OKAY) { |
93 goto LBL_V; | 79 goto LBL_V; |
94 } | 80 } |
95 c->sign = MP_ZPOS; | 81 |
96 res = MP_OKAY; | 82 /* Divide out all factors of two */ |
97 LBL_V:mp_clear (&u); | 83 if ((res = mp_div_2d(&v, mp_cnt_lsb(&v), &v, NULL)) != MP_OKAY) { |
98 LBL_U:mp_clear (&v); | 84 goto LBL_V; |
99 return res; | 85 } |
86 } | |
87 | |
88 /* multiply by 2**k which we divided out at the beginning */ | |
89 if ((res = mp_mul_2d(&u, k, c)) != MP_OKAY) { | |
90 goto LBL_V; | |
91 } | |
92 c->sign = MP_ZPOS; | |
93 res = MP_OKAY; | |
94 LBL_V: | |
95 mp_clear(&u); | |
96 LBL_U: | |
97 mp_clear(&v); | |
98 return res; | |
100 } | 99 } |
101 #endif | 100 #endif |
102 | 101 |
103 /* ref: $Format:%D$ */ | 102 /* ref: HEAD -> master, tag: v1.1.0 */ |
104 /* git commit: $Format:%H$ */ | 103 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */ |
105 /* commit time: $Format:%ai$ */ | 104 /* commit time: 2019-01-28 20:32:32 +0100 */ |