diff --git a/backends/concrete-cpu/implementation/include/concrete-cpu.h b/backends/concrete-cpu/implementation/include/concrete-cpu.h index 8d99db8813..5c91bbbacb 100644 --- a/backends/concrete-cpu/implementation/include/concrete-cpu.h +++ b/backends/concrete-cpu/implementation/include/concrete-cpu.h @@ -398,7 +398,8 @@ void simulation_circuit_bootstrap_boolean_vertical_packing_lwe_ciphertext_u64(co uint64_t pp_level, uint64_t pp_log_base, uint32_t ciphertext_modulus_log, - uint64_t security_level); + uint64_t security_level, + struct Csprng *csprng); void simulation_extract_bit_lwe_ciphertext_u64(uint64_t *lwe_list_out, uint64_t lwe_in, @@ -412,7 +413,8 @@ void simulation_extract_bit_lwe_ciphertext_u64(uint64_t *lwe_list_out, uint64_t br_log_base, uint64_t br_level, uint32_t ciphertext_modulus_log, - uint64_t security_level); + uint64_t security_level, + struct Csprng *csprng); #ifdef __cplusplus } // extern "C" diff --git a/backends/concrete-cpu/implementation/src/c_api/wop_pbs_simulation.rs b/backends/concrete-cpu/implementation/src/c_api/wop_pbs_simulation.rs index 60b9ab387f..0eb23a25fc 100644 --- a/backends/concrete-cpu/implementation/src/c_api/wop_pbs_simulation.rs +++ b/backends/concrete-cpu/implementation/src/c_api/wop_pbs_simulation.rs @@ -2,7 +2,11 @@ use crate::c_api::utils::nounwind; use crate::implementation::wop_simulation::{ circuit_bootstrap_boolean_vertical_packing, extract_bits, }; +use concrete_csprng::generators::SoftwareRandomGenerator; use core::slice; +use tfhe::core_crypto::commons::math::random::RandomGenerator; + +use super::types::Csprng; #[no_mangle] pub unsafe extern "C" fn simulation_extract_bit_lwe_ciphertext_u64( @@ -19,10 +23,13 @@ pub unsafe extern "C" fn simulation_extract_bit_lwe_ciphertext_u64( br_level: u64, ciphertext_modulus_log: u32, security_level: u64, + csprng: *mut Csprng, ) { nounwind(|| { assert!(64 <= number_of_bits_to_extract + delta_log); + let csprng = &mut *(csprng as *mut RandomGenerator); + extract_bits( slice::from_raw_parts_mut(lwe_list_out, number_of_bits_to_extract), lwe_in, @@ -37,6 +44,7 @@ pub unsafe extern "C" fn simulation_extract_bit_lwe_ciphertext_u64( br_level, ciphertext_modulus_log, security_level, + csprng, ); }) } @@ -61,6 +69,7 @@ pub unsafe extern "C" fn simulation_circuit_bootstrap_boolean_vertical_packing_l pp_log_base: u64, ciphertext_modulus_log: u32, security_level: u64, + csprng: *mut Csprng, ) { nounwind(|| { assert_ne!(cb_log_base, 0); @@ -73,6 +82,8 @@ pub unsafe extern "C" fn simulation_circuit_bootstrap_boolean_vertical_packing_l let lwe_list_in = slice::from_raw_parts(lwe_list_in, ct_in_count); + let csprng = &mut *(csprng as *mut RandomGenerator); + circuit_bootstrap_boolean_vertical_packing( lwe_list_in, lwe_list_out, @@ -88,6 +99,7 @@ pub unsafe extern "C" fn simulation_circuit_bootstrap_boolean_vertical_packing_l pp_log_base, ciphertext_modulus_log, security_level, + csprng, ); }) } diff --git a/backends/concrete-cpu/implementation/src/implementation/wop_simulation.rs b/backends/concrete-cpu/implementation/src/implementation/wop_simulation.rs index 203a37ad46..9e104bd01f 100644 --- a/backends/concrete-cpu/implementation/src/implementation/wop_simulation.rs +++ b/backends/concrete-cpu/implementation/src/implementation/wop_simulation.rs @@ -8,12 +8,10 @@ use concrete_cpu_noise_model::gaussian_noise::noise::keyswitch::variance_keyswit use concrete_cpu_noise_model::gaussian_noise::noise::modulus_switching::estimate_modulus_switching_noise_with_binary_key; use concrete_cpu_noise_model::gaussian_noise::noise::private_packing_keyswitch::estimate_packing_private_keyswitch; use concrete_csprng::generators::SoftwareRandomGenerator; +use concrete_security_curves::gaussian::security::{minimal_variance_glwe, minimal_variance_lwe}; use tfhe::core_crypto::commons::math::random::RandomGenerator; use tfhe::core_crypto::commons::parameters::*; -use concrete_csprng::seeders::Seed; -use concrete_security_curves::gaussian::security::{minimal_variance_glwe, minimal_variance_lwe}; - use tfhe::core_crypto::entities::{Polynomial, PolynomialList}; pub fn random_gaussian_pair( @@ -71,9 +69,8 @@ pub fn extract_bits( br_level: u64, ciphertext_modulus_log: u32, security_level: u64, + csprng: &mut RandomGenerator, ) { - let mut csprng = RandomGenerator::::new(Seed(0)); - let polynomial_size = 1 << log_poly_size; let mut lookup_table = vec![0_u64; polynomial_size as usize]; let ciphertext_n_bits = u64::BITS as usize; @@ -101,7 +98,7 @@ pub fn extract_bits( ciphertext_modulus_log, variance_ksk, ); - let (keyswitch_noise, _) = random_gaussian_pair(keyswitch_variance, &mut csprng); + let (keyswitch_noise, _) = random_gaussian_pair(keyswitch_variance, csprng); // Key switch to input PBS key let keyswitched_shifted_lwe = shifted_lwe.wrapping_add(from_torus(keyswitch_noise)); @@ -131,7 +128,7 @@ pub fn extract_bits( log_poly_size, ciphertext_modulus_log, ); - let (modulus_switch_noise, _) = random_gaussian_pair(modulus_switch_variance, &mut csprng); + let (modulus_switch_noise, _) = random_gaussian_pair(modulus_switch_variance, csprng); let modulus_switched_lwe = modular_add( integer_round( @@ -164,7 +161,7 @@ pub fn extract_bits( 53, variance_bsk, ); - let (blind_rotate_noise, _) = random_gaussian_pair(blind_rotate_variance, &mut csprng); + let (blind_rotate_noise, _) = random_gaussian_pair(blind_rotate_variance, csprng); let blind_rotated_lwe = if modulus_switched_lwe < polynomial_size { lookup_table[modulus_switched_lwe as usize].wrapping_add(from_torus(blind_rotate_noise)) @@ -577,9 +574,8 @@ pub fn circuit_bootstrap_boolean_vertical_packing( pp_log_base: u64, ciphertext_modulus_log: u32, security_level: u64, + csprng: &mut RandomGenerator, ) { - let sw_csprng = &mut RandomGenerator::::new(Seed(0)); - let mut ggsw_list = vec![0_u64; lwe_list_in.len()]; let delta_log = u64::BITS as usize - 1; for (lwe_in, ggsw) in zip_eq(lwe_list_in.iter(), ggsw_list.iter_mut()) { @@ -589,7 +585,7 @@ pub fn circuit_bootstrap_boolean_vertical_packing( log_poly_size, lwe_dimension, ciphertext_modulus_log, - sw_csprng, + csprng, ); } @@ -608,7 +604,7 @@ pub fn circuit_bootstrap_boolean_vertical_packing( pbs_level, ciphertext_modulus_log, security_level, - sw_csprng, + csprng, ); } } diff --git a/compilers/concrete-compiler/compiler/lib/Runtime/simulation.cpp b/compilers/concrete-compiler/compiler/lib/Runtime/simulation.cpp index a68c76b589..491fbe7890 100644 --- a/compilers/concrete-compiler/compiler/lib/Runtime/simulation.cpp +++ b/compilers/concrete-compiler/compiler/lib/Runtime/simulation.cpp @@ -162,7 +162,7 @@ void sim_wop_pbs_crt( simulation_extract_bit_lwe_ciphertext_u64( &extract_bits_output_buffer[extract_bits_output_offset], in_block, delta_log, nb_bits_to_extract, log_poly_size, glwe_dim, lwe_small_dim, - ksk_base_log, ksk_level_count, bsk_base_log, bsk_level_count, 64, 128); + ksk_base_log, ksk_level_count, bsk_base_log, bsk_level_count, 64, 128, csprng.ptr); } size_t ct_in_count = total_number_of_bits_per_block; @@ -178,7 +178,7 @@ void sim_wop_pbs_crt( extract_bits_output_buffer, out_aligned + out_offset, ct_in_count, ct_out_count, lut_size, lut_count, lut_ct_aligned + lut_ct_offset, glwe_dim, log_poly_size, lwe_small_dim, bsk_level_count, bsk_base_log, - cbs_level_count, cbs_base_log, pksk_level_count, pksk_base_log, 64, 128); + cbs_level_count, cbs_base_log, pksk_level_count, pksk_base_log, 64, 128, csprng.ptr); } uint64_t sim_neg_lwe_u64(uint64_t plaintext) { return ~plaintext + 1; } diff --git a/frontends/concrete-python/concrete/fhe/compilation/server.py b/frontends/concrete-python/concrete/fhe/compilation/server.py index 5a13fdef97..812c532da8 100644 --- a/frontends/concrete-python/concrete/fhe/compilation/server.py +++ b/frontends/concrete-python/concrete/fhe/compilation/server.py @@ -30,6 +30,7 @@ KeyType, OptimizerMultiParameterStrategy, OptimizerStrategy, + Encoding, PrimitiveOperation, ) from mlir.ir import Module as MlirModule @@ -117,6 +118,7 @@ def create( options = CompilationOptions.new(backend) options.simulation(is_simulated) + options.force_encoding(Encoding.CRT) options.set_loop_parallelize(configuration.loop_parallelize) options.set_dataflow_parallelize(configuration.dataflow_parallelize)