blob: 7f7da0f32b2cc77e7aaa2fed3b1438a2aa1fc3f0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
use num_bigint::BigUint;
pub fn powm(base: &BigUint, exp: &BigUint, modulus: &BigUint) -> BigUint {
let zero = BigUint::from(0u32);
let one = BigUint::from(1u32);
let two = BigUint::from(2u32);
let mut exp = exp.clone();
let mut result = one.clone();
let mut base = base % modulus;
while exp > zero {
if &exp % &two == one {
result = (result * &base) % modulus;
}
exp >>= 1;
base = (&base * &base) % modulus;
}
result
}
|