added Map based on FnvHashMap, and AutoPermMap, generalized gal_el

This commit is contained in:
Jean-Philippe Bossuat
2025-01-16 16:08:22 +01:00
parent 7c8f2f3a63
commit 7c654d2464
10 changed files with 111 additions and 45 deletions

View File

@@ -1,4 +1,29 @@
use crate::modulus::WordOps;
use crate::ring::Ring;
use num::Unsigned;
use utils::map::Map;
pub struct AutoPermMap(Map<usize, AutoPerm>);
impl AutoPermMap {
pub fn new() -> Self {
Self {
0: Map::<usize, AutoPerm>::new(),
}
}
pub fn insert(&mut self, perm: AutoPerm) {
self.0.insert(perm.gal_el, perm);
}
pub fn gen<O: Unsigned, const NTT: bool>(&mut self, ring: &Ring<O>, gen_1: usize, gen_2: bool) {
self.insert(AutoPerm::new::<O, NTT>(ring, gen_1, gen_2))
}
pub fn get(&self, gal_el: &usize) -> Option<&AutoPerm> {
self.0.get(gal_el)
}
}
pub struct AutoPerm {
pub gal_el: usize,
@@ -9,29 +34,21 @@ impl AutoPerm {
/// Returns a lookup table for the automorphism X^{i} -> X^{i * k mod nth_root}.
/// Method will panic if n or nth_root are not power-of-two.
/// Method will panic if gal_el is not coprime with nth_root.
pub fn new<const NTT: bool>(n: usize, gal_el: usize, nth_root: usize) -> Self {
assert!(n & (n - 1) == 0, "invalid n={}: not a power-of-two", n);
assert!(
nth_root & (nth_root - 1) == 0,
"invalid nth_root={}: not a power-of-two",
n
);
assert!(
gal_el & 1 == 1,
"invalid gal_el={}: not coprime with nth_root={}",
gal_el,
nth_root
);
pub fn new<O: Unsigned, const NTT: bool>(ring: &Ring<O>, gen_1: usize, gen_2: bool) -> Self {
let n = ring.n();
let cyclotomic_order = ring.cyclotomic_order();
let gal_el = ring.galois_element(gen_1, gen_2);
let mut permutation: Vec<usize> = Vec::with_capacity(n);
if NTT {
let mask = nth_root - 1;
let log_nth_root_half: u32 = nth_root.log2() as u32 - 1;
let mask = cyclotomic_order - 1;
let log_cyclotomic_order_half: u32 = cyclotomic_order.log2() as u32 - 1;
for i in 0..n {
let i_rev: usize = 2 * i.reverse_bits_msb(log_nth_root_half) + 1;
let i_rev: usize = 2 * i.reverse_bits_msb(log_cyclotomic_order_half) + 1;
let gal_el_i: usize = ((gal_el * i_rev) & mask) >> 1;
permutation.push(gal_el_i.reverse_bits_msb(log_nth_root_half));
permutation.push(gal_el_i.reverse_bits_msb(log_cyclotomic_order_half));
}
} else {
let log_n: usize = n.log2();