From 4c3a56810818903f65adee5eafb1613052e21f1c Mon Sep 17 00:00:00 2001 From: Jean-Philippe Bossuat Date: Mon, 26 May 2025 13:55:21 +0200 Subject: [PATCH] bug fixes --- Cargo.lock | 35 ++++++++++++----------------------- Cargo.toml | 2 +- backend/src/encoding.rs | 8 ++++---- core/src/automorphism.rs | 17 +++++++++++++---- core/src/gglwe_ciphertext.rs | 13 +++++++++++-- core/src/glwe_ciphertext.rs | 20 ++++++++++++-------- core/src/glwe_ops.rs | 8 ++++---- 7 files changed, 57 insertions(+), 46 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1b4ded4..ab139e4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -145,25 +145,22 @@ dependencies = [ [[package]] name = "criterion" -version = "0.5.1" +version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f2b12d017a929603d80db1831cd3a24082f8137ce19c69e6447f54f5fc8d692f" +checksum = "3bf7af66b0989381bd0be551bd7cc91912a655a58c6918420c9527b1fd8b4679" dependencies = [ "anes", "cast", "ciborium", "clap", "criterion-plot", - "is-terminal", - "itertools 0.10.5", + "itertools 0.13.0", "num-traits", - "once_cell", "oorandom", "plotters", "rayon", "regex", "serde", - "serde_derive", "serde_json", "tinytemplate", "walkdir", @@ -254,23 +251,6 @@ dependencies = [ "crunchy", ] -[[package]] -name = "hermit-abi" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fbf6a919d6cf397374f7dfeeea91d974c7c0a7221d0d0f4f20d859d329e53fcc" - -[[package]] -name = "is-terminal" -version = "0.4.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "261f68e344040fbd0edea105bef17c66edf46f984ddb1115b775ce31be948f4b" -dependencies = [ - "hermit-abi", - "libc", - "windows-sys 0.52.0", -] - [[package]] name = "itertools" version = "0.10.5" @@ -280,6 +260,15 @@ dependencies = [ "either", ] +[[package]] +name = "itertools" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" +dependencies = [ + "either", +] + [[package]] name = "itertools" version = "0.14.0" diff --git a/Cargo.toml b/Cargo.toml index dd3a518..4852656 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,4 +9,4 @@ rand_chacha = "0.9.0" rand_core = "0.9.3" rand_distr = "0.5.1" itertools = "0.14.0" -criterion = "0.5.1" +criterion = "0.6.0" diff --git a/backend/src/encoding.rs b/backend/src/encoding.rs index dc423f9..48501b6 100644 --- a/backend/src/encoding.rs +++ b/backend/src/encoding.rs @@ -263,14 +263,14 @@ fn decode_coeff_i64>(a: &VecZnx, col_i: usize, basek: usize, k assert!(col_i < a.cols()) } - let cols: usize = (k + basek - 1) / basek; + let size: usize = (k + basek - 1) / basek; let data: &[i64] = a.raw(); let mut res: i64 = data[i]; let rem: usize = basek - (k % basek); let slice_size: usize = a.n() * a.size(); - (1..cols).for_each(|i| { - let x = data[i * slice_size]; - if i == cols - 1 && rem != basek { + (1..size).for_each(|i| { + let x: i64 = data[i * slice_size]; + if i == size - 1 && rem != basek { let k_rem: usize = basek - rem; res = (res << k_rem) + (x >> rem); } else { diff --git a/core/src/automorphism.rs b/core/src/automorphism.rs index 91f13e5..8c00744 100644 --- a/core/src/automorphism.rs +++ b/core/src/automorphism.rs @@ -1,7 +1,7 @@ use backend::{ - Backend, FFT64, MatZnxDft, MatZnxDftOps, MatZnxDftToMut, MatZnxDftToRef, Module, ScalarZnx, ScalarZnxDftOps, ScalarZnxOps, - ScalarZnxToRef, Scratch, VecZnx, VecZnxBigAlloc, VecZnxDftAlloc, VecZnxDftOps, VecZnxDftToMut, VecZnxDftToRef, VecZnxOps, - ZnxZero, + Backend, FFT64, MatZnxDft, MatZnxDftOps, MatZnxDftToMut, MatZnxDftToRef, Module, ScalarZnx, ScalarZnxDftAlloc, + ScalarZnxDftOps, ScalarZnxOps, ScalarZnxToRef, Scratch, VecZnx, VecZnxBigAlloc, VecZnxDftAlloc, VecZnxDftOps, VecZnxDftToMut, + VecZnxDftToRef, VecZnxOps, ZnxZero, }; use sampling::source::Source; @@ -107,7 +107,7 @@ where impl AutomorphismKey, FFT64> { pub fn generate_from_sk_scratch_space(module: &Module, rank: usize, size: usize) -> usize { - GGLWECiphertext::generate_from_sk_scratch_space(module, rank, size) + GGLWECiphertext::generate_from_sk_scratch_space(module, rank, size) + module.bytes_of_scalar_znx_dft(rank) } pub fn generate_from_pk_scratch_space(module: &Module, rank: usize, pk_size: usize) -> usize { @@ -188,6 +188,15 @@ where assert_eq!(sk.n(), module.n()); assert_eq!(self.rank_out(), self.rank_in()); assert_eq!(sk.rank(), self.rank()); + assert!( + scratch.available() >= AutomorphismKey::generate_from_sk_scratch_space(module, self.rank(), self.size()), + "scratch.available(): {} < AutomorphismKey::generate_from_sk_scratch_space(module, self.rank()={}, \ + self.size()={}): {}", + scratch.available(), + self.rank(), + self.size(), + AutomorphismKey::generate_from_sk_scratch_space(module, self.rank(), self.size()) + ) } let (sk_out_dft_data, scratch_1) = scratch.tmp_scalar_znx_dft(module, sk.rank()); diff --git a/core/src/gglwe_ciphertext.rs b/core/src/gglwe_ciphertext.rs index d5621c9..b7a798e 100644 --- a/core/src/gglwe_ciphertext.rs +++ b/core/src/gglwe_ciphertext.rs @@ -1,7 +1,7 @@ use backend::{ Backend, FFT64, MatZnxDft, MatZnxDftAlloc, MatZnxDftOps, MatZnxDftToMut, MatZnxDftToRef, Module, ScalarZnx, ScalarZnxDft, - ScalarZnxDftToRef, ScalarZnxToRef, Scratch, VecZnxAlloc, VecZnxDftAlloc, VecZnxDftToMut, VecZnxDftToRef, VecZnxOps, ZnxInfos, - ZnxZero, + ScalarZnxDftToRef, ScalarZnxToRef, Scratch, VecZnxAlloc, VecZnxBigScratch, VecZnxDftAlloc, VecZnxDftToMut, VecZnxDftToRef, + VecZnxOps, ZnxInfos, ZnxZero, }; use sampling::source::Source; @@ -115,6 +115,15 @@ where assert_eq!(self.n(), module.n()); assert_eq!(sk_dft.n(), module.n()); assert_eq!(pt.n(), module.n()); + assert!( + scratch.available() >= GGLWECiphertext::generate_from_sk_scratch_space(module, self.rank(), self.size()), + "scratch.available: {} < GGLWECiphertext::generate_from_sk_scratch_space(module, self.rank()={}, \ + self.size()={}): {}", + scratch.available(), + self.rank(), + self.size(), + GGLWECiphertext::generate_from_sk_scratch_space(module, self.rank(), self.size()) + ) } let rows: usize = self.rows(); diff --git a/core/src/glwe_ciphertext.rs b/core/src/glwe_ciphertext.rs index 9c3f32e..8adb7e7 100644 --- a/core/src/glwe_ciphertext.rs +++ b/core/src/glwe_ciphertext.rs @@ -97,9 +97,7 @@ where impl GLWECiphertext> { pub fn encrypt_sk_scratch_space(module: &Module, ct_size: usize) -> usize { - module.vec_znx_big_normalize_tmp_bytes() - + module.bytes_of_vec_znx_dft(1, ct_size) - + module.bytes_of_vec_znx_big(1, ct_size) + module.vec_znx_big_normalize_tmp_bytes() + module.bytes_of_vec_znx_dft(1, ct_size) + module.bytes_of_vec_znx(1, ct_size) } pub fn encrypt_pk_scratch_space(module: &Module, pk_size: usize) -> usize { ((module.bytes_of_vec_znx_dft(1, pk_size) + module.bytes_of_vec_znx_big(1, pk_size)) | module.bytes_of_scalar_znx(1)) @@ -534,11 +532,11 @@ where module.vec_znx_big_automorphism_inplace(apply_auto, &mut res_big, i); } - match OP{ - 1=> module.vec_znx_big_add_small_inplace(&mut res_big, i, lhs, i), - 2=> module.vec_znx_big_sub_small_a_inplace(&mut res_big, i, lhs, i), - 3=> module.vec_znx_big_sub_small_b_inplace(&mut res_big, i, lhs, i), - _=>{}, + match OP { + 1 => module.vec_znx_big_add_small_inplace(&mut res_big, i, lhs, i), + 2 => module.vec_znx_big_sub_small_a_inplace(&mut res_big, i, lhs, i), + 3 => module.vec_znx_big_sub_small_b_inplace(&mut res_big, i, lhs, i), + _ => {} } module.vec_znx_big_normalize(basek, self, i, &res_big, i, scratch1); }); @@ -636,6 +634,12 @@ where assert_eq!(pt.n(), module.n()); assert!(col < self.rank() + 1); } + assert!( + scratch.available() >= GLWECiphertext::encrypt_sk_scratch_space(module, self.size()), + "scratch.available(): {} < GLWECiphertext::encrypt_sk_scratch_space: {}", + scratch.available(), + GLWECiphertext::encrypt_sk_scratch_space(module, self.size()) + ) } let basek: usize = self.basek(); diff --git a/core/src/glwe_ops.rs b/core/src/glwe_ops.rs index 61f9ad2..834e357 100644 --- a/core/src/glwe_ops.rs +++ b/core/src/glwe_ops.rs @@ -175,7 +175,7 @@ where self.set_k(a.k()); } - pub fn rotate_inplace(&mut self, module: &Module, k: i64){ + pub fn rotate_inplace(&mut self, module: &Module, k: i64) { #[cfg(debug_assertions)] { assert_eq!(self.n(), module.n()); @@ -240,8 +240,8 @@ where } } -impl GLWECiphertext>{ - pub fn rsh_scratch_space(module: &Module) -> usize{ +impl GLWECiphertext> { + pub fn rsh_scratch_space(module: &Module) -> usize { VecZnx::rsh_scratch_space(module.n()) } -} \ No newline at end of file +}