comparison libtommath/bn_mp_lcm.c @ 1739:13d834efc376 fuzz

merge from main
author Matt Johnston <matt@ucc.asn.au>
date Thu, 15 Oct 2020 19:55:15 +0800
parents 1051e4eea25a
children
comparison
equal deleted inserted replaced
1562:768ebf737aa0 1739:13d834efc376
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 * 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 /* computes least common multiple as |a*b|/(a, b) */ 6 /* computes least common multiple as |a*b|/(a, b) */
19 int mp_lcm (mp_int * a, mp_int * b, mp_int * c) 7 mp_err mp_lcm(const mp_int *a, const mp_int *b, mp_int *c)
20 { 8 {
21 int res; 9 mp_err err;
22 mp_int t1, t2; 10 mp_int t1, t2;
23 11
24 12
25 if ((res = mp_init_multi (&t1, &t2, NULL)) != MP_OKAY) { 13 if ((err = mp_init_multi(&t1, &t2, NULL)) != MP_OKAY) {
26 return res; 14 return err;
27 } 15 }
28 16
29 /* t1 = get the GCD of the two inputs */ 17 /* t1 = get the GCD of the two inputs */
30 if ((res = mp_gcd (a, b, &t1)) != MP_OKAY) { 18 if ((err = mp_gcd(a, b, &t1)) != MP_OKAY) {
31 goto LBL_T; 19 goto LBL_T;
32 } 20 }
33 21
34 /* divide the smallest by the GCD */ 22 /* divide the smallest by the GCD */
35 if (mp_cmp_mag(a, b) == MP_LT) { 23 if (mp_cmp_mag(a, b) == MP_LT) {
36 /* store quotient in t2 such that t2 * b is the LCM */ 24 /* store quotient in t2 such that t2 * b is the LCM */
37 if ((res = mp_div(a, &t1, &t2, NULL)) != MP_OKAY) { 25 if ((err = mp_div(a, &t1, &t2, NULL)) != MP_OKAY) {
38 goto LBL_T; 26 goto LBL_T;
39 } 27 }
40 res = mp_mul(b, &t2, c); 28 err = mp_mul(b, &t2, c);
41 } else { 29 } else {
42 /* store quotient in t2 such that t2 * a is the LCM */ 30 /* store quotient in t2 such that t2 * a is the LCM */
43 if ((res = mp_div(b, &t1, &t2, NULL)) != MP_OKAY) { 31 if ((err = mp_div(b, &t1, &t2, NULL)) != MP_OKAY) {
44 goto LBL_T; 32 goto LBL_T;
45 } 33 }
46 res = mp_mul(a, &t2, c); 34 err = mp_mul(a, &t2, c);
47 } 35 }
48 36
49 /* fix the sign to positive */ 37 /* fix the sign to positive */
50 c->sign = MP_ZPOS; 38 c->sign = MP_ZPOS;
51 39
52 LBL_T: 40 LBL_T:
53 mp_clear_multi (&t1, &t2, NULL); 41 mp_clear_multi(&t1, &t2, NULL);
54 return res; 42 return err;
55 } 43 }
56 #endif 44 #endif
57
58 /* ref: $Format:%D$ */
59 /* git commit: $Format:%H$ */
60 /* commit time: $Format:%ai$ */