From 2e446c6b76698ebb99cfaf89b842dee08b42d83b Mon Sep 17 00:00:00 2001 From: Jean-Philippe Bossuat Date: Tue, 14 Jan 2025 13:49:24 +0100 Subject: [PATCH] wip --- math/src/ring/impl_u64/packing.rs | 6 ++-- math/tests/automorphism.rs | 58 ++++++++----------------------- 2 files changed, 18 insertions(+), 46 deletions(-) diff --git a/math/src/ring/impl_u64/packing.rs b/math/src/ring/impl_u64/packing.rs index 2bccd80..fb2f32f 100644 --- a/math/src/ring/impl_u64/packing.rs +++ b/math/src/ring/impl_u64/packing.rs @@ -4,12 +4,11 @@ use crate::modulus::ONCE; use crate::poly::Poly; use crate::ring::Ring; use std::cmp::min; -use std::rc::Rc; impl Ring { pub fn pack( &self, - polys: &mut Vec>>, + polys: &mut Vec>>, log_gap: usize, ) { let log_n: usize = self.log_n(); @@ -46,7 +45,7 @@ impl Ring { .prepare(self.modulus.inv(1 << (log_end - log_start))); indices.iter().for_each(|i| { - if let Some(poly) = polys[*i].as_mut() { + if let Some(poly) = polys[*i].as_deref_mut() { if !NTT { self.ntt_inplace::(poly); } @@ -126,6 +125,7 @@ fn max_gap(vec: &[usize]) -> usize { gap } + pub struct StreamRepacker { accumulators: Vec, tmp_a: Poly, diff --git a/math/tests/automorphism.rs b/math/tests/automorphism.rs index b39abbe..7ea4144 100644 --- a/math/tests/automorphism.rs +++ b/math/tests/automorphism.rs @@ -62,74 +62,46 @@ fn packing_u64() { let ring: Ring = Ring::new(n, q_base, q_power); sub_test("test_packing_u64::", || { - test_packing_full_u64::(&ring) + test_packing_sparse_u64::(&ring, 1) }); sub_test("test_packing_u64::", || { - test_packing_full_u64::(&ring) + test_packing_sparse_u64::(&ring, 1) }); sub_test("test_packing_sparse_u64::", || { - test_packing_sparse_u64::(&ring) + test_packing_sparse_u64::(&ring, 3) }); sub_test("test_packing_sparse_u64::", || { - test_packing_sparse_u64::(&ring) + test_packing_sparse_u64::(&ring, 3) }); } -fn test_packing_full_u64(ring: &Ring) { +fn test_packing_sparse_u64(ring: &Ring, gap: usize) { let n: usize = ring.n(); - let mut result: Vec>> = vec![None; n]; + let mut result: Vec>> = Vec::with_capacity(n); + result.resize_with(n, || None); - for i in 0..n { - let mut poly: Poly = ring.new_poly(); - poly.fill(&(1 + i as u64)); + let mut polys: Vec> = vec![ring.new_poly(); (n+gap-1)/gap]; + + polys.iter_mut().enumerate().for_each(|(i , poly)|{ + poly.fill(&((1 + i*gap) as u64)); if NTT { - ring.ntt_inplace::(&mut poly); + ring.ntt_inplace::(poly); } - - result[i] = Some(poly); - } + result[i*gap] = Some(poly); + }); ring.pack::(&mut result, ring.log_n()); if let Some(poly) = result[0].as_mut() { - if NTT { - ring.intt_inplace::(poly); - } - poly.0 - .iter() - .enumerate() - .for_each(|(i, x)| assert_eq!(*x, 1 + i as u64)); - } -} - -fn test_packing_sparse_u64(ring: &Ring) { - let n: usize = ring.n(); - - let mut result: Vec>> = vec![None; n]; - - let gap: usize = 3; - - for i in (0..n).step_by(gap) { - let mut poly: Poly = ring.new_poly(); - poly.fill(&(1 + i as u64)); - if NTT { - ring.ntt_inplace::(&mut poly); - } - result[i] = Some(poly); - } - - ring.pack::(&mut result, ring.log_n()); - - if let Some(poly) = result[0].as_mut() { if NTT { ring.intt_inplace::(poly); } poly.0.iter().enumerate().for_each(|(i, x)| { if i % gap == 0 { - assert_eq!(*x, 1 + i as u64) + assert_eq!(*x, (1+i) as u64) } else { assert_eq!(*x, 0u64) }