[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

@@ -17,6 +17,6 @@ fn main() {
let n: u64 = 1024; let n: u64 = 1024;
let nth_root: u64 = n<<1; let nth_root: u64 = n<<1;
let ntt_table: Table<'_, u64> = Table::<u64>::new(&mut prime_instance, n, nth_root); let ntt_table: Table<'_, u64> = Table::<u64>::new(&mut prime_instance, nth_root);
} }

View File

@@ -5,15 +5,12 @@ pub struct Table<'a, O>{
prime:&'a Prime<O>, prime:&'a Prime<O>,
pub psi_forward_rev:Vec<Montgomery<O>>, pub psi_forward_rev:Vec<Montgomery<O>>,
psi_backward_rev: Vec<Montgomery<O>>, psi_backward_rev: Vec<Montgomery<O>>,
n_inv: Montgomery<O>,
} }
impl<'a> Table<'a, u64> { 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!(nth_root&(nth_root-1) == 0, "invalid argument: nth_root = {} is not a power of two", nth_root);
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);
let psi: u64 = prime.primitive_nth_root(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_forward_rev[0] = prime.montgomery.one();
psi_backward_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{ 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_prev: usize = (i-1).reverse_bits() >> (usize::BITS - log_nth_root_half);
let i_rev_next: usize = i.reverse_bits() >> (usize::MAX - log_nth_root_half) as usize; 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_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); 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{ Self{
prime: prime, prime: prime,
psi_forward_rev: psi_forward_rev, psi_forward_rev: psi_forward_rev,
psi_backward_rev: psi_backward_rev, psi_backward_rev: psi_backward_rev,
n_inv: n_inv,
} }
} }
} }

View File

@@ -25,10 +25,9 @@ impl<O> BarrettPrecomp<O>{
impl BarrettPrecomp<u64>{ impl BarrettPrecomp<u64>{
pub fn new(q: u64) -> BarrettPrecomp<u64> { pub fn new(q: u64) -> BarrettPrecomp<u64> {
let mut big_r = BigUint::parse_bytes(b"100000000000000000000000000000000", 16).unwrap(); let big_r: BigUint = (BigUint::from(1 as usize)<<((u64::BITS<<1) as usize)) / BigUint::from(q);
big_r = big_r / BigUint::from(q); let lo: u64 = (&big_r & BigUint::from(u64::MAX)).to_u64().unwrap();
let lo = (&big_r & BigUint::from(u64::MAX)).to_u64().unwrap(); let hi: u64 = (big_r >> u64::BITS).to_u64().unwrap();
let hi = (big_r >> 64u64).to_u64().unwrap();
Self{q, lo, hi} Self{q, lo, hi}
} }