|
|
1.1 ! root 1: #include "mprec.h" ! 2: ! 3: ! 4: /* ! 5: * Pow sets the mint pointed to by "c" to the mint pointed to by "a" ! 6: * raised to the mint pointed to by "b" power reduced modulo the ! 7: * mint pointed to by "m". If "b" is negative then ! 8: * mperr is called with the appropriate error message. ! 9: * Note that no assumption is made as to the distinctness of "a", "b", ! 10: * "m" and "c". ! 11: */ ! 12: ! 13: void ! 14: pow(a, b, m, c) ! 15: mint *a, *b, *c; ! 16: register mint *m; ! 17: { ! 18: mint al, bl, cl, quot; ! 19: int rem; ! 20: ! 21: if (!ispos(b)) ! 22: mperr("negative power"); ! 23: ! 24: /* make local copies of a (reduced mod m) and b */ ! 25: minit(&al); ! 26: minit("); ! 27: mdiv(a, m, ", &al); ! 28: minit(&bl); ! 29: mcopy(b, &bl); ! 30: ! 31: /* form actual power */ ! 32: minit(&cl); ! 33: sdiv(&bl, 2, &bl, &rem); ! 34: if (rem != 0) ! 35: mcopy(&al, &cl); ! 36: else ! 37: mcopy(mone, &cl); ! 38: while (!zerop(&bl)) { ! 39: mult(&al, &al, &al); ! 40: mdiv(&al, m, ", &al); ! 41: sdiv(&bl, 2, &bl, &rem); ! 42: if (rem != 0) { ! 43: mult(&cl, &al, &cl); ! 44: mdiv(&cl, m, ", &cl); ! 45: } ! 46: } ! 47: ! 48: /* clean up garbage */ ! 49: mpfree(al.val); ! 50: mpfree(bl.val); ! 51: mintfr(quot.val); ! 52: *c = cl; ! 53: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.