Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /************************************************************************
- Computes base^exp mod (mod) (modular exponentiation) using the
- algorithm square-and-multiply. Returns a 16 bit number.
- base 16 bits positive integer
- exp 16 bits positive integer
- mod 16 bits positive integer
- *************************************************************************/
- unsigned short power(unsigned short base, unsigned short exp,
- unsigned short mod) {
- unsigned long result=1;
- while (exp>0) {
- /* Exp is odd, bitwise test */
- if (exp & 1) {
- result=(base*result) % mod;
- }
- base=(base*base) % mod;
- exp/=2; /* Integer division, rounds down */
- }
- /* Will not truncate since mod is unsigned short */
- return (unsigned short)result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement