From 4a6c96dfc8afccdd7bd24b3b65a08c5c0d325900 Mon Sep 17 00:00:00 2001 From: Rasoul Akhavan Mahdavi Date: Mon, 1 Dec 2025 22:07:03 -0500 Subject: [PATCH] clippy + cleanup --- poulpy-schemes/benches/bdd_arithmetic.rs | 27 ++--------- poulpy-schemes/benches/bdd_prepare.rs | 32 +++---------- poulpy-schemes/examples/bdd_arithmetic.rs | 9 ++-- poulpy-schemes/examples/max_array.rs | 57 +++++++++++------------ 4 files changed, 43 insertions(+), 82 deletions(-) diff --git a/poulpy-schemes/benches/bdd_arithmetic.rs b/poulpy-schemes/benches/bdd_arithmetic.rs index ed48d13..e4b9e9c 100644 --- a/poulpy-schemes/benches/bdd_arithmetic.rs +++ b/poulpy-schemes/benches/bdd_arithmetic.rs @@ -2,7 +2,7 @@ use std::hint::black_box; use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main}; use poulpy_core::{ - GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWEExternalProduct, LWEEncryptSk, ScratchTakeCore, + GLWEDecrypt, GLWEEncryptSk, LWEEncryptSk, ScratchTakeCore, layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEToGGSWKeyLayout, GGSWLayout, GGSWPreparedFactory, GLWEAutomorphismKeyLayout, GLWELayout, GLWESecret, GLWESecretPrepared, GLWESecretPreparedFactory, GLWESwitchingKeyLayout, GLWEToLWEKeyLayout, @@ -17,7 +17,7 @@ pub use poulpy_cpu_avx::FFT64Avx as BackendImpl; pub use poulpy_cpu_ref::FFT64Ref as BackendImpl; use poulpy_hal::{ - api::{ModuleN, ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxRotateInplace}, + api::{ModuleN, ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow}, layouts::{Backend, Module, Scratch, ScratchOwned}, source::Source, }; @@ -30,8 +30,7 @@ use poulpy_schemes::bin_fhe::{ BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory, BlindRotationKeyInfos, BlindRotationKeyLayout, CGGI, }, circuit_bootstrapping::{ - CircuitBootstrappingKey, CircuitBootstrappingKeyEncryptSk, CircuitBootstrappingKeyLayout, - CircuitBootstrappingKeyPrepared, CircuitBootstrappingKeyPreparedFactory, CirtuitBootstrappingExecute, + CircuitBootstrappingKey, CircuitBootstrappingKeyEncryptSk, CircuitBootstrappingKeyLayout, CircuitBootstrappingKeyPrepared, }, }; @@ -58,16 +57,10 @@ where Module: ModuleNew + ModuleN + GLWESecretPreparedFactory - + GLWEExternalProduct + GLWEDecrypt - + LWEEncryptSk + CircuitBootstrappingKeyEncryptSk - + CircuitBootstrappingKeyPreparedFactory - + CirtuitBootstrappingExecute + GGSWPreparedFactory - + GGSWNoise + GLWEEncryptSk - + VecZnxRotateInplace + BDDKeyEncryptSk + BDDKeyPreparedFactory + FheUintPrepare @@ -169,7 +162,7 @@ where a_enc_prepared, b_enc_prepared, bdd_key_prepared, - glwe_layout: params.glwe_layout.clone(), + glwe_layout: params.glwe_layout, } } @@ -220,16 +213,11 @@ fn bench_operation( Module: ModuleNew + ModuleN + GLWESecretPreparedFactory - + GLWEExternalProduct + GLWEDecrypt + LWEEncryptSk + CircuitBootstrappingKeyEncryptSk - + CircuitBootstrappingKeyPreparedFactory - + CirtuitBootstrappingExecute + GGSWPreparedFactory - + GGSWNoise + GLWEEncryptSk - + VecZnxRotateInplace + BDDKeyEncryptSk + BDDKeyPreparedFactory + FheUintPrepare @@ -257,16 +245,11 @@ where Module: ModuleNew + ModuleN + GLWESecretPreparedFactory - + GLWEExternalProduct + GLWEDecrypt + LWEEncryptSk + CircuitBootstrappingKeyEncryptSk - + CircuitBootstrappingKeyPreparedFactory - + CirtuitBootstrappingExecute + GGSWPreparedFactory - + GGSWNoise + GLWEEncryptSk - + VecZnxRotateInplace + BDDKeyEncryptSk + BDDKeyPreparedFactory + FheUintPrepare @@ -286,7 +269,7 @@ where const RANK: u32 = 2; let params: Params = Params { - name: String::from(format!("n_glwe={N_GLWE}")), + name: format!("n_glwe={N_GLWE}"), block_size: BINARY_BLOCK_SIZE as usize, glwe_layout: GLWELayout { n: Degree(N_GLWE), diff --git a/poulpy-schemes/benches/bdd_prepare.rs b/poulpy-schemes/benches/bdd_prepare.rs index f1a3edf..e3eccc9 100644 --- a/poulpy-schemes/benches/bdd_prepare.rs +++ b/poulpy-schemes/benches/bdd_prepare.rs @@ -2,7 +2,7 @@ use std::hint::black_box; use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main}; use poulpy_core::{ - GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWEExternalProduct, LWEEncryptSk, ScratchTakeCore, + GLWEDecrypt, GLWEEncryptSk, ScratchTakeCore, layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEToGGSWKeyLayout, GGSWLayout, GGSWPreparedFactory, GLWEAutomorphismKeyLayout, GLWELayout, GLWESecret, GLWESecretPrepared, GLWESecretPreparedFactory, GLWESwitchingKeyLayout, GLWEToLWEKeyLayout, @@ -17,22 +17,18 @@ pub use poulpy_cpu_avx::FFT64Avx as BackendImpl; pub use poulpy_cpu_ref::FFT64Ref as BackendImpl; use poulpy_hal::{ - api::{ModuleN, ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxRotateInplace}, + api::{ModuleN, ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow}, layouts::{Backend, Module, Scratch, ScratchOwned}, source::Source, }; use poulpy_schemes::bin_fhe::{ bdd_arithmetic::{ - BDDKey, BDDKeyEncryptSk, BDDKeyLayout, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, - FheUintPrepare, FheUintPrepared, + BDDKey, BDDKeyEncryptSk, BDDKeyLayout, BDDKeyPrepared, BDDKeyPreparedFactory, FheUint, FheUintPrepare, FheUintPrepared, }, blind_rotation::{ BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory, BlindRotationKeyInfos, BlindRotationKeyLayout, CGGI, }, - circuit_bootstrapping::{ - CircuitBootstrappingKeyEncryptSk, CircuitBootstrappingKeyLayout, CircuitBootstrappingKeyPreparedFactory, - CirtuitBootstrappingExecute, - }, + circuit_bootstrapping::{CircuitBootstrappingKeyEncryptSk, CircuitBootstrappingKeyLayout}, }; pub fn benc_bdd_prepare(c: &mut Criterion, label: &str) @@ -40,20 +36,13 @@ where Module: ModuleNew + ModuleN + GLWESecretPreparedFactory - + GLWEExternalProduct + GLWEDecrypt - + LWEEncryptSk + CircuitBootstrappingKeyEncryptSk - + CircuitBootstrappingKeyPreparedFactory - + CirtuitBootstrappingExecute + GGSWPreparedFactory - + GGSWNoise + GLWEEncryptSk - + VecZnxRotateInplace + BDDKeyEncryptSk + BDDKeyPreparedFactory - + FheUintPrepare - + ExecuteBDDCircuit2WTo1W, + + FheUintPrepare, BlindRotationKey, BRA>: BlindRotationKeyFactory, // TODO find a way to remove this bound or move it to CBT KEY ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchTakeCore, @@ -75,20 +64,13 @@ where Module: ModuleNew + ModuleN + GLWESecretPreparedFactory - + GLWEExternalProduct + GLWEDecrypt - + LWEEncryptSk + CircuitBootstrappingKeyEncryptSk - + CircuitBootstrappingKeyPreparedFactory - + CirtuitBootstrappingExecute + GGSWPreparedFactory - + GGSWNoise + GLWEEncryptSk - + VecZnxRotateInplace + BDDKeyEncryptSk + BDDKeyPreparedFactory - + FheUintPrepare - + ExecuteBDDCircuit2WTo1W, + + FheUintPrepare, BlindRotationKey, BRA>: BlindRotationKeyFactory, /* TODO find a way to remove this bound or move it to CBT KEY */ ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchTakeCore, @@ -157,7 +139,7 @@ where const RANK: u32 = 2; let params: Params = Params { - name: String::from(format!("n_glwe={N_GLWE}")), + name: format!("n_glwe={N_GLWE}"), block_size: BINARY_BLOCK_SIZE as usize, glwe_layout: GLWELayout { n: Degree(N_GLWE), diff --git a/poulpy-schemes/examples/bdd_arithmetic.rs b/poulpy-schemes/examples/bdd_arithmetic.rs index a39103b..f1756bd 100644 --- a/poulpy-schemes/examples/bdd_arithmetic.rs +++ b/poulpy-schemes/examples/bdd_arithmetic.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use poulpy_core::{ - GLWEDecrypt, GLWEEncryptSk, GLWEExternalProduct, LWEEncryptSk, ScratchTakeCore, + GLWEDecrypt, GLWEEncryptSk, ScratchTakeCore, layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEToGGSWKeyLayout, GGSWLayout, GGSWPreparedFactory, GLWEAutomorphismKeyLayout, GLWELayout, GLWESecret, GLWESecretPrepared, GLWESecretPreparedFactory, GLWESwitchingKeyLayout, GLWEToLWEKeyLayout, @@ -9,7 +9,7 @@ use poulpy_core::{ }, }; use poulpy_hal::{ - api::{ModuleN, ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxRotateInplace}, + api::{ModuleN, ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow}, layouts::{Backend, Module, Scratch, ScratchOwned}, source::Source, }; @@ -41,18 +41,17 @@ use poulpy_cpu_ref::FFT64Ref; // // - Result Decryption // +// There also is an example use of the GLWE Blind Selection operation, +// which can choose between any number of encrypted fheuint inputs fn example_bdd_arithmetic() where Module: ModuleNew + ModuleN + GLWESecretPreparedFactory - + GLWEExternalProduct + GLWEDecrypt - + LWEEncryptSk + GGSWPreparedFactory + GLWEEncryptSk - + VecZnxRotateInplace + BDDKeyEncryptSk + BDDKeyPreparedFactory + FheUintPrepare diff --git a/poulpy-schemes/examples/max_array.rs b/poulpy-schemes/examples/max_array.rs index dc3c8af..43ef444 100644 --- a/poulpy-schemes/examples/max_array.rs +++ b/poulpy-schemes/examples/max_array.rs @@ -76,37 +76,34 @@ where dsize: Dsize(1), }; - // Used to generate CBT Keys - let cbt_layout = CircuitBootstrappingKeyLayout { - brk_layout: BlindRotationKeyLayout { - n_glwe: Degree(N_GLWE), - n_lwe: Degree(N_LWE), - base2k: Base2K(BASE2K), - k: TorusPrecision(4 * BASE2K), - dnum: Dnum(4), - rank: Rank(RANK), - }, - atk_layout: GLWEAutomorphismKeyLayout { - n: Degree(N_GLWE), - base2k: Base2K(BASE2K), - k: TorusPrecision(4 * BASE2K), - dnum: Dnum(4), - dsize: Dsize(1), - rank: Rank(RANK), - }, - tsk_layout: GGLWEToGGSWKeyLayout { - n: Degree(N_GLWE), - base2k: Base2K(BASE2K), - k: TorusPrecision(4 * BASE2K), - dnum: Dnum(4), - dsize: Dsize(1), - rank: Rank(RANK), - }, - }; - // Used to generate BDD Keys, for the arithmetic operations let bdd_layout = BDDKeyLayout { - cbt_layout: cbt_layout, + cbt_layout: CircuitBootstrappingKeyLayout { + brk_layout: BlindRotationKeyLayout { + n_glwe: Degree(N_GLWE), + n_lwe: Degree(N_LWE), + base2k: Base2K(BASE2K), + k: TorusPrecision(4 * BASE2K), + dnum: Dnum(4), + rank: Rank(RANK), + }, + atk_layout: GLWEAutomorphismKeyLayout { + n: Degree(N_GLWE), + base2k: Base2K(BASE2K), + k: TorusPrecision(4 * BASE2K), + dnum: Dnum(4), + dsize: Dsize(1), + rank: Rank(RANK), + }, + tsk_layout: GGLWEToGGSWKeyLayout { + n: Degree(N_GLWE), + base2k: Base2K(BASE2K), + k: TorusPrecision(4 * BASE2K), + dnum: Dnum(4), + dsize: Dsize(1), + rank: Rank(RANK), + }, + }, ks_glwe_layout: Some(GLWESwitchingKeyLayout { n: Degree(N_GLWE), base2k: Base2K(BASE2K), @@ -212,7 +209,7 @@ where let mut input_i_enc_prepared: FheUintPrepared, u32, BE> = FheUintPrepared::alloc_from_infos(&module, &ggsw_layout); - input_i_enc_prepared.prepare(&module, &input_i, &bdd_key_prepared, scratch.borrow()); + input_i_enc_prepared.prepare(&module, input_i, &bdd_key_prepared, scratch.borrow()); // b = (input_i < max) compare_enc.sltu(