From c2e1480f4c8cbc6c8a70decd43a29b8944ea5fb1 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Bossuat Date: Wed, 5 Feb 2025 09:15:45 +0100 Subject: [PATCH] small API changes to encoding --- base2k/examples/rlwe_encrypt.rs | 4 ++-- base2k/examples/vector_matrix_product.rs | 4 ++-- base2k/src/encoding.rs | 27 +++++++++++------------- base2k/src/vec_znx.rs | 4 ++-- 4 files changed, 18 insertions(+), 21 deletions(-) diff --git a/base2k/examples/rlwe_encrypt.rs b/base2k/examples/rlwe_encrypt.rs index 729d895..e9f902e 100644 --- a/base2k/examples/rlwe_encrypt.rs +++ b/base2k/examples/rlwe_encrypt.rs @@ -53,7 +53,7 @@ fn main() { .for_each(|x| *x = source.next_u64n(16, 15) as i64); // m - m.encode_i64_vec(log_base2k, log_scale, &want, 4); + m.encode_vec_i64(log_base2k, log_scale, &want, 4); m.normalize(log_base2k, &mut carry); // buf_big <- m - buf_big @@ -78,7 +78,7 @@ fn main() { // have = m * 2^{log_scale} + e let mut have: Vec = vec![i64::default(); n]; - res.decode_i64_vec(log_base2k, res.limbs() * log_base2k, &mut have); + res.decode_vec_i64(log_base2k, res.limbs() * log_base2k, &mut have); let scale: f64 = (1 << (res.limbs() * log_base2k - log_scale)) as f64; izip!(want.iter(), have.iter()) diff --git a/base2k/examples/vector_matrix_product.rs b/base2k/examples/vector_matrix_product.rs index 1bfb1b6..d1a780b 100644 --- a/base2k/examples/vector_matrix_product.rs +++ b/base2k/examples/vector_matrix_product.rs @@ -26,7 +26,7 @@ fn main() { a_values[1] = (1 << log_base2k) + 1; let mut a: VecZnx = module.new_vec_znx(limbs); - a.encode_i64_vec(log_base2k, log_k, &a_values, 32); + a.encode_vec_i64(log_base2k, log_k, &a_values, 32); a.normalize(log_base2k, &mut buf); (0..a.limbs()).for_each(|i| println!("{}: {:?}", i, a.at(i))); @@ -50,7 +50,7 @@ fn main() { module.vec_znx_big_normalize(log_base2k, &mut res, &c_big, &mut buf); let mut values_res: Vec = vec![i64::default(); n]; - res.decode_i64_vec(log_base2k, log_k, &mut values_res); + res.decode_vec_i64(log_base2k, log_k, &mut values_res); (0..res.limbs()).for_each(|i| println!("{}: {:?}", i, res.at(i))); diff --git a/base2k/src/encoding.rs b/base2k/src/encoding.rs index 6ca3ac3..4631cf5 100644 --- a/base2k/src/encoding.rs +++ b/base2k/src/encoding.rs @@ -12,7 +12,7 @@ pub trait Encoding { /// * `log_k`: base two logarithm of the scaling of the data. /// * `data`: data to encode on the receiver. /// * `log_max`: base two logarithm of the infinity norm of the input data. - fn encode_i64_vec(&mut self, log_base2k: usize, log_k: usize, data: &[i64], log_max: usize); + fn encode_vec_i64(&mut self, log_base2k: usize, log_k: usize, data: &[i64], log_max: usize); /// decode a vector of i64 from the receiver. /// @@ -21,7 +21,7 @@ pub trait Encoding { /// * `log_base2k`: base two logarithm decomposition of the receiver. /// * `log_k`: base two logarithm of the scaling of the data. /// * `data`: data to decode from the receiver. - fn decode_i64_vec(&self, log_base2k: usize, log_k: usize, data: &mut [i64]); + fn decode_vec_i64(&self, log_base2k: usize, log_k: usize, data: &mut [i64]); /// encodes a single i64 on the receiver at the given index. /// @@ -32,7 +32,7 @@ pub trait Encoding { /// * `i`: index of the coefficient on which to encode the data. /// * `data`: data to encode on the receiver. /// * `log_max`: base two logarithm of the infinity norm of the input data. - fn encode_i64_coeff( + fn encode_coeff_i64( &mut self, log_base2k: usize, log_k: usize, @@ -49,11 +49,11 @@ pub trait Encoding { /// * `log_k`: base two logarithm of the scaling of the data. /// * `i`: index of the coefficient to decode. /// * `data`: data to decode from the receiver. - fn decode_i64_coeff(&self, log_base2k: usize, log_k: usize, i: usize) -> i64; + fn decode_coeff_i64(&self, log_base2k: usize, log_k: usize, i: usize) -> i64; } impl Encoding for VecZnx { - fn encode_i64_vec(&mut self, log_base2k: usize, log_k: usize, data: &[i64], log_max: usize) { + fn encode_vec_i64(&mut self, log_base2k: usize, log_k: usize, data: &[i64], log_max: usize) { let limbs: usize = (log_k + log_base2k - 1) / log_base2k; assert!(limbs <= self.limbs(), "invalid argument log_k: (log_k + self.log_base2k - 1)/self.log_base2k={} > self.limbs()={}", limbs, self.limbs()); @@ -99,7 +99,7 @@ impl Encoding for VecZnx { } } - fn decode_i64_vec(&self, log_base2k: usize, log_k: usize, data: &mut [i64]) { + fn decode_vec_i64(&self, log_base2k: usize, log_k: usize, data: &mut [i64]) { let limbs: usize = (log_k + log_base2k - 1) / log_base2k; assert!( data.len() >= self.n, @@ -123,7 +123,7 @@ impl Encoding for VecZnx { }) } - fn encode_i64_coeff( + fn encode_coeff_i64( &mut self, log_base2k: usize, log_k: usize, @@ -168,7 +168,7 @@ impl Encoding for VecZnx { } } - fn decode_i64_coeff(&self, log_base2k: usize, log_k: usize, i: usize) -> i64 { + fn decode_coeff_i64(&self, log_base2k: usize, log_k: usize, i: usize) -> i64 { let limbs: usize = (log_k + log_base2k - 1) / log_base2k; assert!(i < self.n()); let mut res: i64 = self.data[i]; @@ -203,9 +203,9 @@ mod tests { have.iter_mut() .enumerate() .for_each(|(i, x)| *x = (i as i64) - (n as i64) / 2); - a.encode_i64_vec(log_base2k, log_k, &have, 10); + a.encode_vec_i64(log_base2k, log_k, &have, 10); let mut want = vec![i64::default(); n]; - a.decode_i64_vec(log_base2k, log_k, &mut want); + a.decode_vec_i64(log_base2k, log_k, &mut want); izip!(want, have).for_each(|(a, b)| assert_eq!(a, b)); } @@ -223,14 +223,11 @@ mod tests { .next_u64n(u64::MAX, u64::MAX) .wrapping_sub(u64::MAX / 2 + 1) as i64; }); - a.encode_i64_vec(log_base2k, log_k, &have, 63); + a.encode_vec_i64(log_base2k, log_k, &have, 63); //(0..a.limbs()).for_each(|i| println!("i:{} -> {:?}", i, a.at(i))); let mut want = vec![i64::default(); n]; //(0..a.limbs()).for_each(|i| println!("i:{} -> {:?}", i, a.at(i))); - a.decode_i64_vec(log_base2k, log_k, &mut want); + a.decode_vec_i64(log_base2k, log_k, &mut want); izip!(want, have).for_each(|(a, b)| assert_eq!(a, b, "{} != {}", a, b)); } - - #[test] - fn test_normalize() {} } diff --git a/base2k/src/vec_znx.rs b/base2k/src/vec_znx.rs index 410b3f3..ec51800 100644 --- a/base2k/src/vec_znx.rs +++ b/base2k/src/vec_znx.rs @@ -120,7 +120,7 @@ impl VecZnx { /// .next_u64n(u64::MAX, u64::MAX) /// .wrapping_sub(u64::MAX / 2 + 1) as i64; /// }); - /// a.encode_i64_vec(log_base2k, log_k, &have, 63); + /// a.encode_vec_i64(log_base2k, log_k, &have, 63); /// a.normalize(log_base2k, &mut carry); /// /// // Ensures normalized values are in the range +/- 2^{log_base2k-1} @@ -131,7 +131,7 @@ impl VecZnx { /// /// // Ensures reconstructed normalized values are equal to non-normalized values. /// let mut want = vec![i64::default(); n]; - /// a.decode_i64_vec(log_base2k, log_k, &mut want); + /// a.decode_vec_i64(log_base2k, log_k, &mut want); /// izip!(want, have).for_each(|(a, b)| assert_eq!(a, b, "{} != {}", a, b)); /// ``` pub fn normalize(&mut self, log_base2k: usize, carry: &mut [u8]) {