File:  [MW Coherent from dump] / coherent / d / usr / lib / libmp / pow.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs
Wed May 29 04:56:38 2019 UTC (7 years, 2 months ago) by root
Branches: MarkWilliams, MAIN
CVS tags: relic, HEAD
coherent

#include "mprec.h"


/*
 *	Pow sets the mint pointed to by "c" to the mint pointed to by "a"
 *	raised to the mint pointed to by "b" power reduced modulo the
 *	mint pointed to by "m".  If "b" is negative then
 *	mperr is called with the appropriate error message.
 *	Note that no assumption is made as to the distinctness of "a", "b",
 *	"m" and "c".
 */

void
pow(a, b, m, c)
mint *a, *b, *c;
register mint *m;
{
	mint	al, bl, cl, quot;
	int rem;

	if (!ispos(b))
		mperr("negative power");

	/* make local copies of a (reduced mod m) and b */
	minit(&al);
	minit(&quot);
	mdiv(a, m, &quot, &al);
	minit(&bl);
	mcopy(b, &bl);

	/* form actual power */
	minit(&cl);
	sdiv(&bl, 2, &bl, &rem);
	if (rem != 0)
		mcopy(&al, &cl);
	else
		mcopy(mone, &cl);
	while (!zerop(&bl)) {
		mult(&al, &al, &al);
		mdiv(&al, m, &quot, &al);
		sdiv(&bl, 2, &bl, &rem);
		if (rem != 0) {
			mult(&cl, &al, &cl);
			mdiv(&cl, m, &quot, &cl);
		}
	}

	/* clean up garbage */
	mpfree(al.val);
	mpfree(bl.val);
	mintfr(quot.val);
	*c = cl;
}

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.