1 #include "tommath_private.h"
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 /* SPDX-License-Identifier: Unlicense */
6 /* computes least common multiple as |a*b|/(a, b) */
7 mp_err
mp_lcm(const mp_int
*a
, const mp_int
*b
, mp_int
*c
)
13 if ((err
= mp_init_multi(&t1
, &t2
, NULL
)) != MP_OKAY
) {
17 /* t1 = get the GCD of the two inputs */
18 if ((err
= mp_gcd(a
, b
, &t1
)) != MP_OKAY
) {
22 /* divide the smallest by the GCD */
23 if (mp_cmp_mag(a
, b
) == MP_LT
) {
24 /* store quotient in t2 such that t2 * b is the LCM */
25 if ((err
= mp_div(a
, &t1
, &t2
, NULL
)) != MP_OKAY
) {
28 err
= mp_mul(b
, &t2
, c
);
30 /* store quotient in t2 such that t2 * a is the LCM */
31 if ((err
= mp_div(b
, &t1
, &t2
, NULL
)) != MP_OKAY
) {
34 err
= mp_mul(a
, &t2
, c
);
37 /* fix the sign to positive */
41 mp_clear_multi(&t1
, &t2
, NULL
);