Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(shortint): remove oprf test flakiness #1910

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 39 additions & 37 deletions tfhe/src/shortint/oprf.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use super::Ciphertext;
use crate::core_crypto::fft_impl::common::modulus_switch;
use crate::core_crypto::prelude::{
keyswitch_lwe_ciphertext, lwe_ciphertext_plaintext_add_assign, LweCiphertext, LweSize,
Plaintext,
keyswitch_lwe_ciphertext, lwe_ciphertext_plaintext_add_assign, CiphertextModulusLog,
LweCiphertext, LweSize, Plaintext,
};
use crate::shortint::ciphertext::Degree;
use crate::shortint::engine::ShortintEngine;
Expand Down Expand Up @@ -41,6 +42,21 @@ impl ServerKey {
ct
}

pub(crate) fn create_random_from_seed_modulus_switched(
&self,
seed: Seed,
lwe_size: LweSize,
log_modulus: CiphertextModulusLog,
) -> LweCiphertext<Vec<u64>> {
let mut ct = self.create_random_from_seed(seed, lwe_size);

for i in ct.as_mut() {
*i = modulus_switch(*i, log_modulus) << (64 - log_modulus.0);
}

ct
}
IceTDrinker marked this conversation as resolved.
Show resolved Hide resolved

/// Uniformly generates a random encrypted value in `[0, 2^random_bits_count[`
/// `2^random_bits_count` must be smaller than the message modulus
/// The encryted value is oblivious to the server
Expand Down Expand Up @@ -107,7 +123,13 @@ impl ServerKey {

let in_lwe_size = self.bootstrapping_key.input_lwe_dimension().to_lwe_size();

let seeded = self.create_random_from_seed(seed, in_lwe_size);
let seeded = self.create_random_from_seed_modulus_switched(
seed,
in_lwe_size,
self.bootstrapping_key
.polynomial_size()
.to_blind_rotation_input_modulus_log(),
);

let p = 1 << random_bits_count;

Expand Down Expand Up @@ -160,13 +182,8 @@ impl ServerKey {

#[cfg(test)]
pub(crate) mod test {
use crate::core_crypto::commons::generators::DeterministicSeeder;
use crate::core_crypto::prelude::{
decrypt_lwe_ciphertext, DefaultRandomGenerator, GlweSecretKey, LweSecretKey,
};
use crate::shortint::engine::ShortintEngine;
use crate::core_crypto::prelude::decrypt_lwe_ciphertext;
use crate::shortint::{ClientKey, ServerKey};
use itertools::Itertools;
use rayon::prelude::*;
use statrs::distribution::ContinuousCDF;
use std::collections::HashMap;
Expand All @@ -177,35 +194,14 @@ pub(crate) mod test {
}

#[test]
// This test is seeded which prevents flakiness
// The noise added by the KS and the MS before the PRF LUT evaluation can make this test fail
// if the seeded input is close to a boundary between 2 encoded values
// Using another KS key can, with a non-neglibgible probability,
// change the output of the PRF after decoding
fn oprf_compare_plain_ci_run_filter() {
let parameters = crate::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS;

let glwe_sk = (0..parameters.glwe_dimension.0 * parameters.polynomial_size.0)
.map(|i| if i % 2 == 0 { 0 } else { 1 })
.collect_vec();

let lwe_sk = (0..parameters.lwe_dimension.0)
.map(|i| if i % 2 == 0 { 0 } else { 1 })
.collect_vec();

let ck = ClientKey {
glwe_secret_key: GlweSecretKey::from_container(glwe_sk, parameters.polynomial_size),
lwe_secret_key: LweSecretKey::from_container(lwe_sk),
parameters: parameters.into(),
};

let mut deterministic_seeder = DeterministicSeeder::<DefaultRandomGenerator>::new(Seed(0));

let mut engine = ShortintEngine::new_from_seeder(&mut deterministic_seeder);

let sk = engine.new_server_key(&ck);
use crate::shortint::gen_keys;
use crate::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS;
let (ck, sk) = gen_keys(PARAM_MESSAGE_2_CARRY_2_KS_PBS);

oprf_compare_plain_from_seed(Seed(0), &ck, &sk);
for seed in 0..1000 {
oprf_compare_plain_from_seed(Seed(seed), &ck, &sk);
}
}

fn oprf_compare_plain_from_seed(seed: Seed, ck: &ClientKey, sk: &ServerKey) {
Expand All @@ -227,7 +223,13 @@ pub(crate) mod test {

let lwe_size = sk.bootstrapping_key.input_lwe_dimension().to_lwe_size();

let ct = sk.create_random_from_seed(seed, lwe_size);
let ct = sk.create_random_from_seed_modulus_switched(
seed,
lwe_size,
sk.bootstrapping_key
.polynomial_size()
.to_blind_rotation_input_modulus_log(),
);

let sk = ck.small_lwe_secret_key();

Expand Down
Loading