arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/backend/src/encoding.rs
backend/src/encoding.rsRaw
  1use crate::ffi::znx::znx_zero_i64_ref;
  2use crate::znx_base::{ZnxView, ZnxViewMut};
  3use crate::{VecZnx, znx_base::ZnxInfos};
  4use itertools::izip;
  5use rug::{Assign, Float};
  6use std::cmp::min;
  7
  8pub trait Encoding {
  9    /// encode a vector of i64 on the receiver.
 10    ///
 11    /// # Arguments
 12    ///
 13    /// * `col_i`: the index of the poly where to encode the data.
 14    /// * `basek`: base two negative logarithm decomposition of the receiver.
 15    /// * `k`: base two negative logarithm of the scaling of the data.
 16    /// * `data`: data to encode on the receiver.
 17    /// * `log_max`: base two logarithm of the infinity norm of the input data.
 18    fn encode_vec_i64(&mut self, col_i: usize, basek: usize, k: usize, data: &[i64], log_max: usize);
 19
 20    /// encodes a single i64 on the receiver at the given index.
 21    ///
 22    /// # Arguments
 23    ///
 24    /// * `col_i`: the index of the poly where to encode the data.
 25    /// * `basek`: base two negative logarithm decomposition of the receiver.
 26    /// * `k`: base two negative logarithm of the scaling of the data.
 27    /// * `i`: index of the coefficient on which to encode the data.
 28    /// * `data`: data to encode on the receiver.
 29    /// * `log_max`: base two logarithm of the infinity norm of the input data.
 30    fn encode_coeff_i64(&mut self, col_i: usize, basek: usize, k: usize, i: usize, data: i64, log_max: usize);
 31}
 32
 33pub trait Decoding {
 34    /// decode a vector of i64 from the receiver.
 35    ///
 36    /// # Arguments
 37    ///
 38    /// * `col_i`: the index of the poly where to encode the data.
 39    /// * `basek`: base two negative logarithm decomposition of the receiver.
 40    /// * `k`: base two logarithm of the scaling of the data.
 41    /// * `data`: data to decode from the receiver.
 42    fn decode_vec_i64(&self, col_i: usize, basek: usize, k: usize, data: &mut [i64]);
 43
 44    /// decode a vector of Float from the receiver.
 45    ///
 46    /// # Arguments
 47    /// * `col_i`: the index of the poly where to encode the data.
 48    /// * `basek`: base two negative logarithm decomposition of the receiver.
 49    /// * `data`: data to decode from the receiver.
 50    fn decode_vec_float(&self, col_i: usize, basek: usize, data: &mut [Float]);
 51
 52    /// decode a single of i64 from the receiver at the given index.
 53    ///
 54    /// # Arguments
 55    ///
 56    /// * `col_i`: the index of the poly where to encode the data.
 57    /// * `basek`: base two negative logarithm decomposition of the receiver.
 58    /// * `k`: base two negative logarithm of the scaling of the data.
 59    /// * `i`: index of the coefficient to decode.
 60    /// * `data`: data to decode from the receiver.
 61    fn decode_coeff_i64(&self, col_i: usize, basek: usize, k: usize, i: usize) -> i64;
 62}
 63
 64impl<D: AsMut<[u8]> + AsRef<[u8]>> Encoding for VecZnx<D> {
 65    fn encode_vec_i64(&mut self, col_i: usize, basek: usize, k: usize, data: &[i64], log_max: usize) {
 66        encode_vec_i64(self, col_i, basek, k, data, log_max)
 67    }
 68
 69    fn encode_coeff_i64(&mut self, col_i: usize, basek: usize, k: usize, i: usize, value: i64, log_max: usize) {
 70        encode_coeff_i64(self, col_i, basek, k, i, value, log_max)
 71    }
 72}
 73
 74impl<D: AsRef<[u8]>> Decoding for VecZnx<D> {
 75    fn decode_vec_i64(&self, col_i: usize, basek: usize, k: usize, data: &mut [i64]) {
 76        decode_vec_i64(self, col_i, basek, k, data)
 77    }
 78
 79    fn decode_vec_float(&self, col_i: usize, basek: usize, data: &mut [Float]) {
 80        decode_vec_float(self, col_i, basek, data)
 81    }
 82
 83    fn decode_coeff_i64(&self, col_i: usize, basek: usize, k: usize, i: usize) -> i64 {
 84        decode_coeff_i64(self, col_i, basek, k, i)
 85    }
 86}
 87
 88fn encode_vec_i64<D: AsMut<[u8]> + AsRef<[u8]>>(
 89    a: &mut VecZnx<D>,
 90    col_i: usize,
 91    basek: usize,
 92    k: usize,
 93    data: &[i64],
 94    log_max: usize,
 95) {
 96    let size: usize = (k + basek - 1) / basek;
 97
 98    #[cfg(debug_assertions)]
 99    {
100        assert!(
101            size <= a.size(),
102            "invalid argument k: (k + a.basek - 1)/a.basek={} > a.size()={}",
103            size,
104            a.size()
105        );
106        assert!(col_i < a.cols());
107        assert!(data.len() <= a.n())
108    }
109
110    let data_len: usize = data.len();
111    let k_rem: usize = basek - (k % basek);
112
113    // Zeroes coefficients of the i-th column
114    (0..a.size()).for_each(|i| unsafe {
115        znx_zero_i64_ref(a.n() as u64, a.at_mut_ptr(col_i, i));
116    });
117
118    // If 2^{basek} * 2^{k_rem} < 2^{63}-1, then we can simply copy
119    // values on the last limb.
120    // Else we decompose values base2k.
121    if log_max + k_rem < 63 || k_rem == basek {
122        a.at_mut(col_i, size - 1)[..data_len].copy_from_slice(&data[..data_len]);
123    } else {
124        let mask: i64 = (1 << basek) - 1;
125        let steps: usize = min(size, (log_max + basek - 1) / basek);
126        (size - steps..size)
127            .rev()
128            .enumerate()
129            .for_each(|(i, i_rev)| {
130                let shift: usize = i * basek;
131                izip!(a.at_mut(col_i, i_rev).iter_mut(), data.iter()).for_each(|(y, x)| *y = (x >> shift) & mask);
132            })
133    }
134
135    // Case where self.prec % self.k != 0.
136    if k_rem != basek {
137        let steps: usize = min(size, (log_max + basek - 1) / basek);
138        (size - steps..size).rev().for_each(|i| {
139            a.at_mut(col_i, i)[..data_len]
140                .iter_mut()
141                .for_each(|x| *x <<= k_rem);
142        })
143    }
144}
145
146fn decode_vec_i64<D: AsRef<[u8]>>(a: &VecZnx<D>, col_i: usize, basek: usize, k: usize, data: &mut [i64]) {
147    let size: usize = (k + basek - 1) / basek;
148    #[cfg(debug_assertions)]
149    {
150        assert!(
151            data.len() >= a.n(),
152            "invalid data: data.len()={} < a.n()={}",
153            data.len(),
154            a.n()
155        );
156        assert!(col_i < a.cols());
157    }
158    data.copy_from_slice(a.at(col_i, 0));
159    let rem: usize = basek - (k % basek);
160    if k < basek {
161        data.iter_mut().for_each(|x| *x >>= rem);
162    } else {
163        (1..size).for_each(|i| {
164            if i == size - 1 && rem != basek {
165                let k_rem: usize = basek - rem;
166                izip!(a.at(col_i, i).iter(), data.iter_mut()).for_each(|(x, y)| {
167                    *y = (*y << k_rem) + (x >> rem);
168                });
169            } else {
170                izip!(a.at(col_i, i).iter(), data.iter_mut()).for_each(|(x, y)| {
171                    *y = (*y << basek) + x;
172                });
173            }
174        })
175    }
176}
177
178fn decode_vec_float<D: AsRef<[u8]>>(a: &VecZnx<D>, col_i: usize, basek: usize, data: &mut [Float]) {
179    let size: usize = a.size();
180    #[cfg(debug_assertions)]
181    {
182        assert!(
183            data.len() >= a.n(),
184            "invalid data: data.len()={} < a.n()={}",
185            data.len(),
186            a.n()
187        );
188        assert!(col_i < a.cols());
189    }
190
191    let prec: u32 = (basek * size) as u32;
192
193    // 2^{basek}
194    let base = Float::with_val(prec, (1 << basek) as f64);
195
196    // y[i] = sum x[j][i] * 2^{-basek*j}
197    (0..size).for_each(|i| {
198        if i == 0 {
199            izip!(a.at(col_i, size - i - 1).iter(), data.iter_mut()).for_each(|(x, y)| {
200                y.assign(*x);
201                *y /= &base;
202            });
203        } else {
204            izip!(a.at(col_i, size - i - 1).iter(), data.iter_mut()).for_each(|(x, y)| {
205                *y += Float::with_val(prec, *x);
206                *y /= &base;
207            });
208        }
209    });
210}
211
212fn encode_coeff_i64<D: AsMut<[u8]> + AsRef<[u8]>>(
213    a: &mut VecZnx<D>,
214    col_i: usize,
215    basek: usize,
216    k: usize,
217    i: usize,
218    value: i64,
219    log_max: usize,
220) {
221    let size: usize = (k + basek - 1) / basek;
222
223    #[cfg(debug_assertions)]
224    {
225        assert!(i < a.n());
226        assert!(
227            size <= a.size(),
228            "invalid argument k: (k + a.basek - 1)/a.basek={} > a.size()={}",
229            size,
230            a.size()
231        );
232        assert!(col_i < a.cols());
233    }
234
235    let k_rem: usize = basek - (k % basek);
236    (0..a.size()).for_each(|j| a.at_mut(col_i, j)[i] = 0);
237
238    // If 2^{basek} * 2^{k_rem} < 2^{63}-1, then we can simply copy
239    // values on the last limb.
240    // Else we decompose values base2k.
241    if log_max + k_rem < 63 || k_rem == basek {
242        a.at_mut(col_i, size - 1)[i] = value;
243    } else {
244        let mask: i64 = (1 << basek) - 1;
245        let steps: usize = min(size, (log_max + basek - 1) / basek);
246        (size - steps..size)
247            .rev()
248            .enumerate()
249            .for_each(|(j, j_rev)| {
250                a.at_mut(col_i, j_rev)[i] = (value >> (j * basek)) & mask;
251            })
252    }
253
254    // Case where prec % k != 0.
255    if k_rem != basek {
256        let steps: usize = min(size, (log_max + basek - 1) / basek);
257        (size - steps..size).rev().for_each(|j| {
258            a.at_mut(col_i, j)[i] <<= k_rem;
259        })
260    }
261}
262
263fn decode_coeff_i64<D: AsRef<[u8]>>(a: &VecZnx<D>, col_i: usize, basek: usize, k: usize, i: usize) -> i64 {
264    #[cfg(debug_assertions)]
265    {
266        assert!(i < a.n());
267        assert!(col_i < a.cols())
268    }
269
270    let size: usize = (k + basek - 1) / basek;
271    let data: &[i64] = a.raw();
272    let mut res: i64 = 0;
273    let rem: usize = basek - (k % basek);
274    let slice_size: usize = a.n() * a.cols();
275    (0..size).for_each(|j| {
276        let x: i64 = data[j * slice_size + i];
277        if j == size - 1 && rem != basek {
278            let k_rem: usize = basek - rem;
279            res = (res << k_rem) + (x >> rem);
280        } else {
281            res = (res << basek) + x;
282        }
283    });
284    res
285}
286
287#[cfg(test)]
288mod tests {
289    use crate::vec_znx_ops::*;
290    use crate::znx_base::*;
291    use crate::{Decoding, Encoding, FFT64, Module, VecZnx, znx_base::ZnxInfos};
292    use itertools::izip;
293    use sampling::source::Source;
294
295    #[test]
296    fn test_set_get_i64_lo_norm() {
297        let n: usize = 8;
298        let module: Module<FFT64> = Module::<FFT64>::new(n);
299        let basek: usize = 17;
300        let size: usize = 5;
301        let k: usize = size * basek - 5;
302        let mut a: VecZnx<_> = module.new_vec_znx(2, size);
303        let mut source: Source = Source::new([0u8; 32]);
304        let raw: &mut [i64] = a.raw_mut();
305        raw.iter_mut().enumerate().for_each(|(i, x)| *x = i as i64);
306        (0..a.cols()).for_each(|col_i| {
307            let mut have: Vec<i64> = vec![i64::default(); n];
308            have.iter_mut()
309                .for_each(|x| *x = (source.next_i64() << 56) >> 56);
310            a.encode_vec_i64(col_i, basek, k, &have, 10);
311            let mut want: Vec<i64> = vec![i64::default(); n];
312            a.decode_vec_i64(col_i, basek, k, &mut want);
313            izip!(want, have).for_each(|(a, b)| assert_eq!(a, b, "{} != {}", a, b));
314        });
315    }
316
317    #[test]
318    fn test_set_get_i64_hi_norm() {
319        let n: usize = 8;
320        let module: Module<FFT64> = Module::<FFT64>::new(n);
321        let basek: usize = 17;
322        let size: usize = 5;
323        for k in [1, basek / 2, size * basek - 5] {
324            let mut a: VecZnx<_> = module.new_vec_znx(2, size);
325            let mut source = Source::new([0u8; 32]);
326            let raw: &mut [i64] = a.raw_mut();
327            raw.iter_mut().enumerate().for_each(|(i, x)| *x = i as i64);
328            (0..a.cols()).for_each(|col_i| {
329                let mut have: Vec<i64> = vec![i64::default(); n];
330                have.iter_mut().for_each(|x| {
331                    if k < 64 {
332                        *x = source.next_u64n(1 << k, (1 << k) - 1) as i64;
333                    } else {
334                        *x = source.next_i64();
335                    }
336                });
337                a.encode_vec_i64(col_i, basek, k, &have, std::cmp::min(k, 64));
338                let mut want = vec![i64::default(); n];
339                a.decode_vec_i64(col_i, basek, k, &mut want);
340                izip!(want, have).for_each(|(a, b)| assert_eq!(a, b, "{} != {}", a, b));
341            })
342        }
343    }
344}