small API changes to encoding

This commit is contained in:
Jean-Philippe Bossuat
2025-02-05 09:15:45 +01:00
parent a790ff37cc
commit c2e1480f4c
4 changed files with 18 additions and 21 deletions

View File

@@ -53,7 +53,7 @@ fn main() {
.for_each(|x| *x = source.next_u64n(16, 15) as i64); .for_each(|x| *x = source.next_u64n(16, 15) as i64);
// m // 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); m.normalize(log_base2k, &mut carry);
// buf_big <- m - buf_big // buf_big <- m - buf_big
@@ -78,7 +78,7 @@ fn main() {
// have = m * 2^{log_scale} + e // have = m * 2^{log_scale} + e
let mut have: Vec<i64> = vec![i64::default(); n]; let mut have: Vec<i64> = 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; let scale: f64 = (1 << (res.limbs() * log_base2k - log_scale)) as f64;
izip!(want.iter(), have.iter()) izip!(want.iter(), have.iter())

View File

@@ -26,7 +26,7 @@ fn main() {
a_values[1] = (1 << log_base2k) + 1; a_values[1] = (1 << log_base2k) + 1;
let mut a: VecZnx = module.new_vec_znx(limbs); 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); a.normalize(log_base2k, &mut buf);
(0..a.limbs()).for_each(|i| println!("{}: {:?}", i, a.at(i))); (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); module.vec_znx_big_normalize(log_base2k, &mut res, &c_big, &mut buf);
let mut values_res: Vec<i64> = vec![i64::default(); n]; let mut values_res: Vec<i64> = 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))); (0..res.limbs()).for_each(|i| println!("{}: {:?}", i, res.at(i)));

View File

@@ -12,7 +12,7 @@ pub trait Encoding {
/// * `log_k`: base two logarithm of the scaling of the data. /// * `log_k`: base two logarithm of the scaling of the data.
/// * `data`: data to encode on the receiver. /// * `data`: data to encode on the receiver.
/// * `log_max`: base two logarithm of the infinity norm of the input data. /// * `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. /// 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_base2k`: base two logarithm decomposition of the receiver.
/// * `log_k`: base two logarithm of the scaling of the data. /// * `log_k`: base two logarithm of the scaling of the data.
/// * `data`: data to decode from the receiver. /// * `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. /// 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. /// * `i`: index of the coefficient on which to encode the data.
/// * `data`: data to encode on the receiver. /// * `data`: data to encode on the receiver.
/// * `log_max`: base two logarithm of the infinity norm of the input data. /// * `log_max`: base two logarithm of the infinity norm of the input data.
fn encode_i64_coeff( fn encode_coeff_i64(
&mut self, &mut self,
log_base2k: usize, log_base2k: usize,
log_k: usize, log_k: usize,
@@ -49,11 +49,11 @@ pub trait Encoding {
/// * `log_k`: base two logarithm of the scaling of the data. /// * `log_k`: base two logarithm of the scaling of the data.
/// * `i`: index of the coefficient to decode. /// * `i`: index of the coefficient to decode.
/// * `data`: data to decode from the receiver. /// * `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 { 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; 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()); 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; let limbs: usize = (log_k + log_base2k - 1) / log_base2k;
assert!( assert!(
data.len() >= self.n, data.len() >= self.n,
@@ -123,7 +123,7 @@ impl Encoding for VecZnx {
}) })
} }
fn encode_i64_coeff( fn encode_coeff_i64(
&mut self, &mut self,
log_base2k: usize, log_base2k: usize,
log_k: 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; let limbs: usize = (log_k + log_base2k - 1) / log_base2k;
assert!(i < self.n()); assert!(i < self.n());
let mut res: i64 = self.data[i]; let mut res: i64 = self.data[i];
@@ -203,9 +203,9 @@ mod tests {
have.iter_mut() have.iter_mut()
.enumerate() .enumerate()
.for_each(|(i, x)| *x = (i as i64) - (n as i64) / 2); .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]; 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)); izip!(want, have).for_each(|(a, b)| assert_eq!(a, b));
} }
@@ -223,14 +223,11 @@ mod tests {
.next_u64n(u64::MAX, u64::MAX) .next_u64n(u64::MAX, u64::MAX)
.wrapping_sub(u64::MAX / 2 + 1) as i64; .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))); //(0..a.limbs()).for_each(|i| println!("i:{} -> {:?}", i, a.at(i)));
let mut want = vec![i64::default(); n]; let mut want = vec![i64::default(); n];
//(0..a.limbs()).for_each(|i| println!("i:{} -> {:?}", i, a.at(i))); //(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)); izip!(want, have).for_each(|(a, b)| assert_eq!(a, b, "{} != {}", a, b));
} }
#[test]
fn test_normalize() {}
} }

View File

@@ -120,7 +120,7 @@ impl VecZnx {
/// .next_u64n(u64::MAX, u64::MAX) /// .next_u64n(u64::MAX, u64::MAX)
/// .wrapping_sub(u64::MAX / 2 + 1) as i64; /// .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); /// a.normalize(log_base2k, &mut carry);
/// ///
/// // Ensures normalized values are in the range +/- 2^{log_base2k-1} /// // 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. /// // Ensures reconstructed normalized values are equal to non-normalized values.
/// let mut want = vec![i64::default(); n]; /// 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)); /// izip!(want, have).for_each(|(a, b)| assert_eq!(a, b, "{} != {}", a, b));
/// ``` /// ```
pub fn normalize(&mut self, log_base2k: usize, carry: &mut [u8]) { pub fn normalize(&mut self, log_base2k: usize, carry: &mut [u8]) {