comparison libtommath/bn_mp_lcm.c @ 1692:1051e4eea25a

Update LibTomMath to 1.2.0 (#84) * update C files * update other files * update headers * update makefiles * remove mp_set/get_double() * use ltm 1.2.0 API * update ltm_desc * use bundled tommath if system-tommath is too old * XMALLOC etc. were changed to MP_MALLOC etc.
author Steffen Jaeckel <s@jaeckel.eu>
date Tue, 26 May 2020 17:36:47 +0200
parents f52919ffd3b1
children
comparison
equal deleted inserted replaced
1691:2d3745d58843 1692:1051e4eea25a
1 #include "tommath_private.h" 1 #include "tommath_private.h"
2 #ifdef BN_MP_LCM_C 2 #ifdef BN_MP_LCM_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 * SPDX-License-Identifier: Unlicense
13 */
14 5
15 /* computes least common multiple as |a*b|/(a, b) */ 6 /* computes least common multiple as |a*b|/(a, b) */
16 int mp_lcm(const mp_int *a, const mp_int *b, mp_int *c) 7 mp_err mp_lcm(const mp_int *a, const mp_int *b, mp_int *c)
17 { 8 {
18 int res; 9 mp_err err;
19 mp_int t1, t2; 10 mp_int t1, t2;
20 11
21 12
22 if ((res = mp_init_multi(&t1, &t2, NULL)) != MP_OKAY) { 13 if ((err = mp_init_multi(&t1, &t2, NULL)) != MP_OKAY) {
23 return res; 14 return err;
24 } 15 }
25 16
26 /* t1 = get the GCD of the two inputs */ 17 /* t1 = get the GCD of the two inputs */
27 if ((res = mp_gcd(a, b, &t1)) != MP_OKAY) { 18 if ((err = mp_gcd(a, b, &t1)) != MP_OKAY) {
28 goto LBL_T; 19 goto LBL_T;
29 } 20 }
30 21
31 /* divide the smallest by the GCD */ 22 /* divide the smallest by the GCD */
32 if (mp_cmp_mag(a, b) == MP_LT) { 23 if (mp_cmp_mag(a, b) == MP_LT) {
33 /* store quotient in t2 such that t2 * b is the LCM */ 24 /* store quotient in t2 such that t2 * b is the LCM */
34 if ((res = mp_div(a, &t1, &t2, NULL)) != MP_OKAY) { 25 if ((err = mp_div(a, &t1, &t2, NULL)) != MP_OKAY) {
35 goto LBL_T; 26 goto LBL_T;
36 } 27 }
37 res = mp_mul(b, &t2, c); 28 err = mp_mul(b, &t2, c);
38 } else { 29 } else {
39 /* store quotient in t2 such that t2 * a is the LCM */ 30 /* store quotient in t2 such that t2 * a is the LCM */
40 if ((res = mp_div(b, &t1, &t2, NULL)) != MP_OKAY) { 31 if ((err = mp_div(b, &t1, &t2, NULL)) != MP_OKAY) {
41 goto LBL_T; 32 goto LBL_T;
42 } 33 }
43 res = mp_mul(a, &t2, c); 34 err = mp_mul(a, &t2, c);
44 } 35 }
45 36
46 /* fix the sign to positive */ 37 /* fix the sign to positive */
47 c->sign = MP_ZPOS; 38 c->sign = MP_ZPOS;
48 39
49 LBL_T: 40 LBL_T:
50 mp_clear_multi(&t1, &t2, NULL); 41 mp_clear_multi(&t1, &t2, NULL);
51 return res; 42 return err;
52 } 43 }
53 #endif 44 #endif
54
55 /* ref: HEAD -> master, tag: v1.1.0 */
56 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */
57 /* commit time: 2019-01-28 20:32:32 +0100 */