1 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
2 /* All Rights Reserved */
6 * Copyright (c) 1980 Regents of the University of California.
7 * All rights reserved. The Berkeley software License Agreement
8 * specifies the terms and conditions for redistribution.
10 /* Portions Copyright(c) 1988, Sun Microsystems Inc. */
11 /* All Rights Reserved */
14 * Copyright (c) 1997, by Sun Microsystems, Inc.
15 * All rights reserved.
18 #ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.1 */
24 #include <sys/types.h>
27 mp_gcd(MINT
*a
, MINT
*b
, MINT
*c
)
31 x
.len
= y
.len
= z
.len
= w
.len
= 0;
35 mp_mdiv(&x
, &y
, &w
, &z
);
47 mp_invert(MINT
*x1
, MINT
*x0
, MINT
*c
)
55 static MINT
*one
= NULL
;
58 * Minimize calls to allocators. Don't use pointers for local
59 * variables, for the one "initialized" multiple precision
60 * variable, do it just once.
65 zero
.len
= q
.len
= r
.len
= t
.len
= 0;
67 x0_prime
.len
= u2
.len
= u3
.len
= 0;
69 _mp_move(x0
, &x0_prime
);
75 while (mp_mcmp(&v3
, &zero
) != 0) {
76 /* invariant: x0*u1 + x1*u2 = u3 */
77 /* invariant: x0*v1 + x2*v2 = v3 */
78 /* invariant: x(n+1) = x(n-1) % x(n) */
79 mp_mdiv(&u3
, &v3
, &q
, &r
);
88 /* now x0*u1 + x1*u2 == 1, therefore, (u2*x1) % x0 == 1 */
90 if (mp_mcmp(c
, &zero
) < 0) {
91 mp_madd(&x0_prime
, c
, c
);