2
|
1 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
2 * |
|
3 * LibTomMath is a library that provides multiple-precision |
|
4 * integer arithmetic as well as number theoretic functionality. |
|
5 * |
|
6 * The library was designed directly after the MPI library by |
|
7 * Michael Fromberger but has been written from scratch with |
|
8 * additional optimizations in place. |
|
9 * |
|
10 * The library is free for all purposes without any express |
|
11 * guarantee it works. |
|
12 * |
|
13 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
14 */ |
|
15 #include <tommath.h> |
|
16 |
|
17 /* computes least common multiple as |a*b|/(a, b) */ |
|
18 int mp_lcm (mp_int * a, mp_int * b, mp_int * c) |
|
19 { |
|
20 int res; |
|
21 mp_int t1, t2; |
|
22 |
|
23 |
|
24 if ((res = mp_init_multi (&t1, &t2, NULL)) != MP_OKAY) { |
|
25 return res; |
|
26 } |
|
27 |
|
28 /* t1 = get the GCD of the two inputs */ |
|
29 if ((res = mp_gcd (a, b, &t1)) != MP_OKAY) { |
|
30 goto __T; |
|
31 } |
|
32 |
|
33 /* divide the smallest by the GCD */ |
|
34 if (mp_cmp_mag(a, b) == MP_LT) { |
|
35 /* store quotient in t2 such that t2 * b is the LCM */ |
|
36 if ((res = mp_div(a, &t1, &t2, NULL)) != MP_OKAY) { |
|
37 goto __T; |
|
38 } |
|
39 res = mp_mul(b, &t2, c); |
|
40 } else { |
|
41 /* store quotient in t2 such that t2 * a is the LCM */ |
|
42 if ((res = mp_div(b, &t1, &t2, NULL)) != MP_OKAY) { |
|
43 goto __T; |
|
44 } |
|
45 res = mp_mul(a, &t2, c); |
|
46 } |
|
47 |
|
48 /* fix the sign to positive */ |
|
49 c->sign = MP_ZPOS; |
|
50 |
|
51 __T: |
|
52 mp_clear_multi (&t1, &t2, NULL); |
|
53 return res; |
|
54 } |