[barrett]: some cleaning toward more generic code

-
This commit is contained in:
Jean-Philippe Bossuat
2024-12-06 10:52:18 +01:00
parent ed2f028df5
commit a24ad55adc
3 changed files with 9 additions and 16 deletions

View File

@@ -5,15 +5,12 @@ pub struct Table<'a, O>{
prime:&'a Prime<O>,
pub psi_forward_rev:Vec<Montgomery<O>>,
psi_backward_rev: Vec<Montgomery<O>>,
n_inv: Montgomery<O>,
}
impl<'a> Table<'a, u64> {
pub fn new(prime: &'a mut Prime<u64>, n: u64, nth_root: u64)->Self{
pub fn new(prime: &'a mut Prime<u64>, nth_root: u64)->Self{
assert!(n&(n-1) == 0, "invalid argument: n = {} is not a power of two", n);
assert!(n&(n-1) == 0, "invalid argument: nth_root = {} is not a power of two", nth_root);
assert!(n < nth_root, "invalid argument: n = {} cannot be greater or equal to nth_root = {}", n, nth_root);
assert!(nth_root&(nth_root-1) == 0, "invalid argument: nth_root = {} is not a power of two", nth_root);
let psi: u64 = prime.primitive_nth_root(nth_root);
@@ -26,24 +23,21 @@ impl<'a> Table<'a, u64> {
psi_forward_rev[0] = prime.montgomery.one();
psi_backward_rev[0] = prime.montgomery.one();
let log_nth_root_half: usize = (usize::MAX - ((nth_root>>1 as usize)-1).leading_zeros() as usize) as usize;
let log_nth_root_half: u32 = usize::BITS - ((nth_root>>1 as usize)-1).leading_zeros();
for i in 1..(nth_root>>1) as usize{
let i_rev_prev: usize = (i-1).reverse_bits() >> (usize::MAX - log_nth_root_half) as usize;
let i_rev_next: usize = i.reverse_bits() >> (usize::MAX - log_nth_root_half) as usize;
let i_rev_prev: usize = (i-1).reverse_bits() >> (usize::BITS - log_nth_root_half);
let i_rev_next: usize = i.reverse_bits() >> (usize::BITS - log_nth_root_half);
psi_forward_rev[i_rev_next] = prime.montgomery.mul_internal(psi_forward_rev[i_rev_prev], psi_mont);
psi_backward_rev[i_rev_next] = prime.montgomery.mul_internal(psi_backward_rev[i_rev_prev], psi_inv_mont);
}
let n_inv: Montgomery<u64> = prime.montgomery.pow(prime.montgomery.prepare(nth_root>>1), prime.phi-1);
Self{
prime: prime,
psi_forward_rev: psi_forward_rev,
psi_backward_rev: psi_backward_rev,
n_inv: n_inv,
}
}
}