diff --git a/backend/src/vec_znx.rs b/backend/src/vec_znx.rs index feba7ce..b397adb 100644 --- a/backend/src/vec_znx.rs +++ b/backend/src/vec_znx.rs @@ -371,3 +371,19 @@ impl VecZnxToRef for VecZnx<&[u8]> { } } } + +impl VecZnx +where + VecZnx: VecZnxToRef, +{ + pub fn clone(&self) -> VecZnx> { + let self_ref: VecZnx<&[u8]> = self.to_ref(); + + VecZnx { + data: self_ref.data.to_vec(), + n: self_ref.n, + cols: self_ref.cols, + size: self_ref.size, + } + } +} diff --git a/core/src/glwe_ciphertext.rs b/core/src/glwe_ciphertext.rs index 18c2ce0..4205637 100644 --- a/core/src/glwe_ciphertext.rs +++ b/core/src/glwe_ciphertext.rs @@ -12,7 +12,6 @@ use crate::{ elem::{Infos, SetMetaData}, ggsw_ciphertext::GGSWCiphertext, glwe_ciphertext_fourier::GLWECiphertextFourier, - glwe_ops::GLWEOps, glwe_plaintext::GLWEPlaintext, keys::{GLWEPublicKey, SecretDistribution, SecretKeyFourier}, keyswitch_key::GLWESwitchingKey, @@ -215,8 +214,6 @@ where } } -impl GLWEOps for GLWECiphertext where VecZnx: VecZnxToMut {} - impl GLWECiphertext where VecZnx: VecZnxToMut, @@ -713,6 +710,14 @@ impl GLWECiphertext where VecZnx: VecZnxToRef, { + pub fn clone(&self) -> GLWECiphertext> { + GLWECiphertext { + data: self.data.clone(), + basek: self.basek(), + k: self.k(), + } + } + pub fn decrypt( &self, module: &Module, diff --git a/core/src/glwe_ops.rs b/core/src/glwe_ops.rs index 691e34d..95357ed 100644 --- a/core/src/glwe_ops.rs +++ b/core/src/glwe_ops.rs @@ -1,12 +1,16 @@ -use backend::{Backend, Module, Scratch, VecZnx, VecZnxOps, VecZnxToMut, VecZnxToRef, ZnxZero}; +use backend::{FFT64, Module, Scratch, VecZnx, VecZnxOps, VecZnxToMut, VecZnxToRef, ZnxZero}; -use crate::elem::{Infos, SetMetaData}; +use crate::{ + elem::{Infos, SetMetaData}, + glwe_ciphertext::GLWECiphertext, +}; -pub trait GLWEOps +impl GLWECiphertext where - Self: Sized + VecZnxToMut + SetMetaData + Infos, + Self: Infos, + VecZnx: VecZnxToMut, { - fn add(&mut self, module: &Module, a: &A, b: &B) + pub fn add(&mut self, module: &Module, a: &A, b: &B) where A: VecZnxToRef + Infos, B: VecZnxToRef + Infos, @@ -50,7 +54,7 @@ where self.set_k(a.k().max(b.k())); } - fn add_inplace(&mut self, module: &Module, a: &A) + pub fn add_inplace(&mut self, module: &Module, a: &A) where A: VecZnxToRef + Infos, { @@ -69,7 +73,7 @@ where self.set_k(a.k().max(self.k())); } - fn sub(&mut self, module: &Module, a: &A, b: &B) + pub fn sub(&mut self, module: &Module, a: &A, b: &B) where A: VecZnxToRef + Infos, B: VecZnxToRef + Infos, @@ -114,7 +118,7 @@ where self.set_k(a.k().max(b.k())); } - fn sub_inplace_ab(&mut self, module: &Module, a: &A) + pub fn sub_inplace_ab(&mut self, module: &Module, a: &A) where A: VecZnxToRef + Infos, { @@ -133,7 +137,7 @@ where self.set_k(a.k().max(self.k())); } - fn sub_inplace_ba(&mut self, module: &Module, a: &A) + pub fn sub_inplace_ba(&mut self, module: &Module, a: &A) where A: VecZnxToRef + Infos, { @@ -152,7 +156,7 @@ where self.set_k(a.k().max(self.k())); } - fn rotate(&mut self, module: &Module, k: i64, a: &A) + pub fn rotate(&mut self, module: &Module, k: i64, a: &A) where A: VecZnxToRef + Infos, { @@ -160,7 +164,6 @@ where { assert_eq!(a.n(), module.n()); assert_eq!(self.n(), module.n()); - assert_eq!(self.basek(), a.basek()); assert_eq!(self.rank(), a.rank()) } @@ -168,10 +171,11 @@ where module.vec_znx_rotate(k, self, i, a, i); }); + self.set_basek(a.basek()); self.set_k(a.k()); } - fn rotate_inplace(&mut self, module: &Module, k: i64) + pub fn rotate_inplace(&mut self, module: &Module, k: i64) where A: VecZnxToRef + Infos, { @@ -185,7 +189,7 @@ where }); } - fn copy(&mut self, module: &Module, a: &A) + pub fn copy(&mut self, module: &Module, a: &A) where A: VecZnxToRef + Infos, { @@ -193,11 +197,10 @@ where { assert_eq!(self.n(), module.n()); assert_eq!(a.n(), module.n()); + assert_eq!(self.rank(), a.rank()); } - let cols: usize = self.rank().min(a.rank()) + 1; - - (0..cols).for_each(|i| { + (0..self.rank() + 1).for_each(|i| { module.vec_znx_copy(self, i, a, i); }); @@ -205,9 +208,37 @@ where self.set_basek(a.basek()); } - fn rsh(&mut self, k: usize, scratch: &mut Scratch) { + pub fn rsh(&mut self, k: usize, scratch: &mut Scratch) { let basek: usize = self.basek(); let mut self_mut: VecZnx<&mut [u8]> = self.to_mut(); self_mut.rsh(basek, k, scratch); } + + pub fn normalize(&mut self, module: &Module, a: &A, scratch: &mut Scratch) + where + A: VecZnxToMut + Infos, + { + #[cfg(debug_assertions)] + { + assert_eq!(self.n(), module.n()); + assert_eq!(a.n(), module.n()); + assert_eq!(self.rank(), a.rank()); + } + + (0..self.rank() + 1).for_each(|i| { + module.vec_znx_normalize(a.basek(), self, i, a, i, scratch); + }); + self.set_basek(a.basek()); + self.set_k(a.k()); + } + + pub fn normalize_inplace(&mut self, module: &Module, scratch: &mut Scratch) { + #[cfg(debug_assertions)] + { + assert_eq!(self.n(), module.n()); + } + (0..self.rank() + 1).for_each(|i| { + module.vec_znx_normalize_inplace(self.basek(), self, i, scratch); + }); + } } diff --git a/core/src/trace.rs b/core/src/trace.rs index 07ce1b3..40bd8b7 100644 --- a/core/src/trace.rs +++ b/core/src/trace.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use backend::{FFT64, MatZnxDft, MatZnxDftToRef, Module, Scratch, VecZnx, VecZnxToMut, VecZnxToRef}; -use crate::{automorphism::AutomorphismKey, glwe_ciphertext::GLWECiphertext, glwe_ops::GLWEOps}; +use crate::{automorphism::AutomorphismKey, glwe_ciphertext::GLWECiphertext}; impl GLWECiphertext> { pub fn trace_galois_elements(module: &Module) -> Vec {