Skip to content

Commit

Permalink
qbo/remove-csprng-instantiation-for-crt-simulation
Browse files Browse the repository at this point in the history
  • Loading branch information
BourgerieQuentin committed Apr 25, 2024
1 parent e238067 commit d13e086
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 16 deletions.
6 changes: 4 additions & 2 deletions backends/concrete-cpu/implementation/include/concrete-cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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<SoftwareRandomGenerator>);

extract_bits(
slice::from_raw_parts_mut(lwe_list_out, number_of_bits_to_extract),
lwe_in,
Expand All @@ -37,6 +44,7 @@ pub unsafe extern "C" fn simulation_extract_bit_lwe_ciphertext_u64(
br_level,
ciphertext_modulus_log,
security_level,
csprng,
);
})
}
Expand All @@ -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);
Expand All @@ -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<SoftwareRandomGenerator>);

circuit_bootstrap_boolean_vertical_packing(
lwe_list_in,
lwe_list_out,
Expand All @@ -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,
);
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -71,9 +69,8 @@ pub fn extract_bits(
br_level: u64,
ciphertext_modulus_log: u32,
security_level: u64,
csprng: &mut RandomGenerator<SoftwareRandomGenerator>,
) {
let mut csprng = RandomGenerator::<SoftwareRandomGenerator>::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;
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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<SoftwareRandomGenerator>,
) {
let sw_csprng = &mut RandomGenerator::<SoftwareRandomGenerator>::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()) {
Expand All @@ -589,7 +585,7 @@ pub fn circuit_bootstrap_boolean_vertical_packing(
log_poly_size,
lwe_dimension,
ciphertext_modulus_log,
sw_csprng,
csprng,
);
}

Expand All @@ -608,7 +604,7 @@ pub fn circuit_bootstrap_boolean_vertical_packing(
pbs_level,
ciphertext_modulus_log,
security_level,
sw_csprng,
csprng,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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; }
Expand Down
2 changes: 2 additions & 0 deletions frontends/concrete-python/concrete/fhe/compilation/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
KeyType,
OptimizerMultiParameterStrategy,
OptimizerStrategy,
Encoding,
PrimitiveOperation,
)
from mlir.ir import Module as MlirModule
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit d13e086

Please sign in to comment.