Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 106 lines (90 sloc) 2.549 kB
e549ccf added libtommath-0.32
Tom St Denis authored
1 #include <tommath.h>
2 #ifdef BN_MP_GCD_C
57354e1 added libtommath-0.12
Tom St Denis authored
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis
4 *
c1da6aa added libtommath-0.25
Tom St Denis authored
5 * LibTomMath is a library that provides multiple-precision
57354e1 added libtommath-0.12
Tom St Denis authored
6 * integer arithmetic as well as number theoretic functionality.
7 *
c1da6aa added libtommath-0.25
Tom St Denis authored
8 * The library was designed directly after the MPI library by
57354e1 added libtommath-0.12
Tom St Denis authored
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 *
333aebc added libtommath-0.41
Tom St Denis authored
15 * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
57354e1 added libtommath-0.12
Tom St Denis authored
16 */
17
4c1d3f0 added libtommath-0.22
Tom St Denis authored
18 /* Greatest Common Divisor using the binary method */
455bb4d added libtommath-0.28
Tom St Denis authored
19 int mp_gcd (mp_int * a, mp_int * b, mp_int * c)
57354e1 added libtommath-0.12
Tom St Denis authored
20 {
4c1d3f0 added libtommath-0.22
Tom St Denis authored
21 mp_int u, v;
22 int k, u_lsb, v_lsb, res;
57354e1 added libtommath-0.12
Tom St Denis authored
23
24 /* either zero than gcd is the largest */
f0b91a5 added libtommath-0.39
Tom St Denis authored
25 if (mp_iszero (a) == MP_YES) {
c1da6aa added libtommath-0.25
Tom St Denis authored
26 return mp_abs (b, c);
57354e1 added libtommath-0.12
Tom St Denis authored
27 }
f0b91a5 added libtommath-0.39
Tom St Denis authored
28 if (mp_iszero (b) == MP_YES) {
c1da6aa added libtommath-0.25
Tom St Denis authored
29 return mp_abs (a, c);
57354e1 added libtommath-0.12
Tom St Denis authored
30 }
c1da6aa added libtommath-0.25
Tom St Denis authored
31
32 /* get copies of a and b we can modify */
57354e1 added libtommath-0.12
Tom St Denis authored
33 if ((res = mp_init_copy (&u, a)) != MP_OKAY) {
34 return res;
35 }
36
37 if ((res = mp_init_copy (&v, b)) != MP_OKAY) {
4b7111d added libtommath-0.33
Tom St Denis authored
38 goto LBL_U;
57354e1 added libtommath-0.12
Tom St Denis authored
39 }
40
41 /* must be positive for the remainder of the algorithm */
42 u.sign = v.sign = MP_ZPOS;
43
4c1d3f0 added libtommath-0.22
Tom St Denis authored
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);
57354e1 added libtommath-0.12
Tom St Denis authored
48
c1da6aa added libtommath-0.25
Tom St Denis authored
49 if (k > 0) {
50 /* divide the power of two out */
51 if ((res = mp_div_2d(&u, k, &u, NULL)) != MP_OKAY) {
4b7111d added libtommath-0.33
Tom St Denis authored
52 goto LBL_V;
c1da6aa added libtommath-0.25
Tom St Denis authored
53 }
57354e1 added libtommath-0.12
Tom St Denis authored
54
c1da6aa added libtommath-0.25
Tom St Denis authored
55 if ((res = mp_div_2d(&v, k, &v, NULL)) != MP_OKAY) {
4b7111d added libtommath-0.33
Tom St Denis authored
56 goto LBL_V;
c1da6aa added libtommath-0.25
Tom St Denis authored
57 }
57354e1 added libtommath-0.12
Tom St Denis authored
58 }
59
4c1d3f0 added libtommath-0.22
Tom St Denis authored
60 /* divide any remaining factors of two out */
61 if (u_lsb != k) {
62 if ((res = mp_div_2d(&u, u_lsb - k, &u, NULL)) != MP_OKAY) {
4b7111d added libtommath-0.33
Tom St Denis authored
63 goto LBL_V;
4c1d3f0 added libtommath-0.22
Tom St Denis authored
64 }
57354e1 added libtommath-0.12
Tom St Denis authored
65 }
66
4c1d3f0 added libtommath-0.22
Tom St Denis authored
67 if (v_lsb != k) {
68 if ((res = mp_div_2d(&v, v_lsb - k, &v, NULL)) != MP_OKAY) {
4b7111d added libtommath-0.33
Tom St Denis authored
69 goto LBL_V;
4c1d3f0 added libtommath-0.22
Tom St Denis authored
70 }
71 }
c1da6aa added libtommath-0.25
Tom St Denis authored
72
4c1d3f0 added libtommath-0.22
Tom St Denis authored
73 while (mp_iszero(&v) == 0) {
74 /* make sure v is the largest */
75 if (mp_cmp_mag(&u, &v) == MP_GT) {
c1da6aa added libtommath-0.25
Tom St Denis authored
76 /* swap u and v to make sure v is >= u */
4c1d3f0 added libtommath-0.22
Tom St Denis authored
77 mp_exch(&u, &v);
78 }
79
80 /* subtract smallest from largest */
81 if ((res = s_mp_sub(&v, &u, &v)) != MP_OKAY) {
4b7111d added libtommath-0.33
Tom St Denis authored
82 goto LBL_V;
4c1d3f0 added libtommath-0.22
Tom St Denis authored
83 }
84
85 /* Divide out all factors of two */
86 if ((res = mp_div_2d(&v, mp_cnt_lsb(&v), &v, NULL)) != MP_OKAY) {
4b7111d added libtommath-0.33
Tom St Denis authored
87 goto LBL_V;
4c1d3f0 added libtommath-0.22
Tom St Denis authored
88 }
89 }
c1da6aa added libtommath-0.25
Tom St Denis authored
90
91 /* multiply by 2**k which we divided out at the beginning */
4c1d3f0 added libtommath-0.22
Tom St Denis authored
92 if ((res = mp_mul_2d (&u, k, c)) != MP_OKAY) {
4b7111d added libtommath-0.33
Tom St Denis authored
93 goto LBL_V;
4c1d3f0 added libtommath-0.22
Tom St Denis authored
94 }
95 c->sign = MP_ZPOS;
57354e1 added libtommath-0.12
Tom St Denis authored
96 res = MP_OKAY;
4b7111d added libtommath-0.33
Tom St Denis authored
97 LBL_V:mp_clear (&u);
98 LBL_U:mp_clear (&v);
57354e1 added libtommath-0.12
Tom St Denis authored
99 return res;
100 }
e549ccf added libtommath-0.32
Tom St Denis authored
101 #endif
9294e79 added libtommath-0.36
Tom St Denis authored
102
103 /* $Source$ */
104 /* $Revision$ */
105 /* $Date$ */
Something went wrong with that request. Please try again.