3 /* LibTomMath, multiple-precision integer library -- Tom St Denis
5 * LibTomMath is a library that provides multiple-precision
6 * integer arithmetic as well as number theoretic functionality.
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.
12 * The library is free for all purposes without any express
15 * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
18 /* Greatest Common Divisor using the binary method */
19 int mp_gcd (mp_int
* a
, mp_int
* b
, mp_int
* c
)
22 int k
, u_lsb
, v_lsb
, res
;
24 /* either zero than gcd is the largest */
25 if (mp_iszero (a
) == MP_YES
) {
28 if (mp_iszero (b
) == MP_YES
) {
32 /* get copies of a and b we can modify */
33 if ((res
= mp_init_copy (&u
, a
)) != MP_OKAY
) {
37 if ((res
= mp_init_copy (&v
, b
)) != MP_OKAY
) {
41 /* must be positive for the remainder of the algorithm */
42 u
.sign
= v
.sign
= MP_ZPOS
;
44 /* B1. Find the common power of two for u and v */
45 u_lsb
= mp_cnt_lsb(&u
);
46 v_lsb
= mp_cnt_lsb(&v
);
47 k
= MIN(u_lsb
, v_lsb
);
50 /* divide the power of two out */
51 if ((res
= mp_div_2d(&u
, k
, &u
, NULL
)) != MP_OKAY
) {
55 if ((res
= mp_div_2d(&v
, k
, &v
, NULL
)) != MP_OKAY
) {
60 /* divide any remaining factors of two out */
62 if ((res
= mp_div_2d(&u
, u_lsb
- k
, &u
, NULL
)) != MP_OKAY
) {
68 if ((res
= mp_div_2d(&v
, v_lsb
- k
, &v
, NULL
)) != MP_OKAY
) {
73 while (mp_iszero(&v
) == 0) {
74 /* make sure v is the largest */
75 if (mp_cmp_mag(&u
, &v
) == MP_GT
) {
76 /* swap u and v to make sure v is >= u */
80 /* subtract smallest from largest */
81 if ((res
= s_mp_sub(&v
, &u
, &v
)) != MP_OKAY
) {
85 /* Divide out all factors of two */
86 if ((res
= mp_div_2d(&v
, mp_cnt_lsb(&v
), &v
, NULL
)) != MP_OKAY
) {
91 /* multiply by 2**k which we divided out at the beginning */
92 if ((res
= mp_mul_2d (&u
, k
, c
)) != MP_OKAY
) {
103 /* $Source: /cvs/libtom/libtommath/bn_mp_gcd.c,v $ */
104 /* $Revision: 1.4 $ */
105 /* $Date: 2006/03/31 14:18:44 $ */