xref: /illumos-gate/usr/src/lib/libmp/common/pow.c (revision 7c478bd9)
1*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
2*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
3*7c478bd9Sstevel@tonic-gate 
4*7c478bd9Sstevel@tonic-gate 
5*7c478bd9Sstevel@tonic-gate /*
6*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
7*7c478bd9Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
8*7c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
9*7c478bd9Sstevel@tonic-gate  */
10*7c478bd9Sstevel@tonic-gate /* 	Portions Copyright(c) 1988, Sun Microsystems Inc.	*/
11*7c478bd9Sstevel@tonic-gate /*	All Rights Reserved					*/
12*7c478bd9Sstevel@tonic-gate 
13*7c478bd9Sstevel@tonic-gate /*
14*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1997, by Sun Microsystems, Inc.
15*7c478bd9Sstevel@tonic-gate  * All rights reserved.
16*7c478bd9Sstevel@tonic-gate  */
17*7c478bd9Sstevel@tonic-gate 
18*7c478bd9Sstevel@tonic-gate #ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.1	*/
19*7c478bd9Sstevel@tonic-gate 
20*7c478bd9Sstevel@tonic-gate #include <stdio.h>
21*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
22*7c478bd9Sstevel@tonic-gate 
23*7c478bd9Sstevel@tonic-gate /* LINTLIBRARY */
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate #include <mp.h>
26*7c478bd9Sstevel@tonic-gate #include "libmp.h"
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate void
mp_pow(MINT * a,MINT * b,MINT * c,MINT * d)29*7c478bd9Sstevel@tonic-gate mp_pow(MINT *a, MINT *b, MINT *c, MINT *d)
30*7c478bd9Sstevel@tonic-gate {
31*7c478bd9Sstevel@tonic-gate 	int i, j, n;
32*7c478bd9Sstevel@tonic-gate 	MINT x, y;
33*7c478bd9Sstevel@tonic-gate 	MINT a0, b0, c0;
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate 	a0.len = b0.len = c0.len = x.len = y.len = 0;
36*7c478bd9Sstevel@tonic-gate 	_mp_move(a, &a0);
37*7c478bd9Sstevel@tonic-gate 	_mp_move(b, &b0);
38*7c478bd9Sstevel@tonic-gate 	_mp_move(c, &c0);
39*7c478bd9Sstevel@tonic-gate 	_mp_xfree(d);
40*7c478bd9Sstevel@tonic-gate 	d->len = 1;
41*7c478bd9Sstevel@tonic-gate 	d->val = _mp_xalloc(1, "mp_pow");
42*7c478bd9Sstevel@tonic-gate 	*d->val = 1;
43*7c478bd9Sstevel@tonic-gate 	for (j = 0; j < b0.len; j++) {
44*7c478bd9Sstevel@tonic-gate 		n = b0.val[b0.len - j - 1];
45*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < 15; i++) {
46*7c478bd9Sstevel@tonic-gate 			mp_mult(d, d, &x);
47*7c478bd9Sstevel@tonic-gate 			mp_mdiv(&x, &c0, &y, d);
48*7c478bd9Sstevel@tonic-gate 			if ((n = n << 1) & 0100000) {
49*7c478bd9Sstevel@tonic-gate 				mp_mult(&a0, d, &x);
50*7c478bd9Sstevel@tonic-gate 				mp_mdiv(&x, &c0, &y, d);
51*7c478bd9Sstevel@tonic-gate 			}
52*7c478bd9Sstevel@tonic-gate 		}
53*7c478bd9Sstevel@tonic-gate 	}
54*7c478bd9Sstevel@tonic-gate 	_mp_xfree(&x);
55*7c478bd9Sstevel@tonic-gate 	_mp_xfree(&y);
56*7c478bd9Sstevel@tonic-gate 	_mp_xfree(&a0);
57*7c478bd9Sstevel@tonic-gate 	_mp_xfree(&b0);
58*7c478bd9Sstevel@tonic-gate 	_mp_xfree(&c0);
59*7c478bd9Sstevel@tonic-gate }
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate void
mp_rpow(MINT * a,short n,MINT * b)62*7c478bd9Sstevel@tonic-gate mp_rpow(MINT *a, short n, MINT *b)
63*7c478bd9Sstevel@tonic-gate {
64*7c478bd9Sstevel@tonic-gate 	MINT x, y;
65*7c478bd9Sstevel@tonic-gate 	int	i;
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate 	x.len = 1;
68*7c478bd9Sstevel@tonic-gate 	x.val = _mp_xalloc(1, "mp_rpow");
69*7c478bd9Sstevel@tonic-gate 	*x.val = n;
70*7c478bd9Sstevel@tonic-gate 	y.len = n * a->len + 4;
71*7c478bd9Sstevel@tonic-gate 	y.val = _mp_xalloc(y.len, "mp_rpow2");
72*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < y.len; i++)
73*7c478bd9Sstevel@tonic-gate 		y.val[i] = 0;
74*7c478bd9Sstevel@tonic-gate 	y.val[y.len - 1] = 010000;
75*7c478bd9Sstevel@tonic-gate 	mp_pow(a, &x, &y, b);
76*7c478bd9Sstevel@tonic-gate 	_mp_xfree(&x);
77*7c478bd9Sstevel@tonic-gate 	_mp_xfree(&y);
78*7c478bd9Sstevel@tonic-gate }
79