INNER CODE UNIT · Rust
sum_n
IronCoreLabs/recrypt-rs · src/internal/mod.rs:386
fn sum_n<T: Add<Output = T> + Copy + Zero + PartialEq>(t: T, n: u32) -> T {
if n == 0 {
Zero::zero()
} else {
sum_n_loop(t, n - 1, t)
}
}
fn sum_n_loop<T: Add<Output = T> + Copy>(t: T, k: u32, extra: T) -> T {
if k == 1 {
t + extra
} else {
let x = if (k & 1) == 1 { t + extra } else { extra };
sum_n_loop(t + t, k >> 1, x)
}
}
/// This is not constant time, it reveals the n, but if the t has a constant time square and multiply, doesn't reveal the t.