1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use std::u64;
use std::i64;
pub fn mod_add(a:u64, b:u64, q:u64) -> u64 {
let a0 = a % q;
let b0 = b % q;
let sum = a0.checked_add(b0);
match sum {
Some(x) => x % q,
None => slow_add(a0,b0,q)
}
}
pub fn mod_abs(a: i64, q: u64) -> u64 {
let mut abs_a = a;
while abs_a < 0 {
abs_a += q as i64;
}
abs_a as u64
}
pub fn mod_sub(a: i64, b: i64, modulus: u64) -> u64 {
mod_add(mod_abs(a,modulus), mod_abs(b, modulus), modulus)
}
fn slow_add(a:u64, b:u64, q:u64) -> u64 {
if a > b {
let neg_b = q -b;
a - neg_b
} else {
let neg_a = q - a;
b - neg_a
}
}
pub fn mod_mul(a:u64, b:u64, q:u64) -> u64 {
let a0 = a % q;
let b0 = b % q;
let prod = a0.checked_mul(b0);
match prod {
Some(x) => x % q,
None => slow_mul(a0, b0, q)
}
}
fn slow_mul(a:u64, b:u64, q:u64) -> u64 {
let mut total = 0;
for _ii in 0..b {
total = mod_add(total, a, q);
}
total
}
pub fn mod_exp(base :u64, exponent :u64, q:u64) -> u64 {
if exponent == 0{
return 1
}
let reduced_base = base % q;
let mut current_state = reduced_base;
for _i in 0..exponent-1 {
current_state = mod_mul(current_state, reduced_base, q);
}
current_state
}