1pub mod mod_exp {
2 extern crate num;
3
4 use self::num::bigint::{BigUint};
5
6 pub fn mod_exp(base: &BigUint, exponent: &BigUint, modulus: &BigUint) -> BigUint {
8 let zero = BigUint::new(vec![0]);
9 let one = BigUint::new(vec![1]);
10 let two = BigUint::new(vec![2]);
11 let mut exp = exponent.clone();
12 let mut result = one.clone();
13 let mut base = base % modulus;
14 while exp > zero {
15 if &exp % &two == one {
16 result = (result * &base) % modulus;
17 }
18 exp = exp >> 1;
19 base = (&base * &base) % modulus;
20 }
21 result
22 }
23}