Skip to content

Commit 8532dfa

Browse files
committed
chore: add tool to update AP params' msg and carry moduli
1 parent 71f7231 commit 8532dfa

File tree

2 files changed

+35
-34
lines changed

2 files changed

+35
-34
lines changed

tfhe/src/shortint/server_key/tests/noise_distribution/br_dp_packingks_ms.rs

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use super::utils::noise_simulation::*;
22
use super::utils::traits::*;
33
use super::utils::{
44
expected_pfail_for_precision, mean_and_variance_check, normality_check, pfail_check,
5-
precision_with_padding, DecryptionAndNoiseResult, NoiseSample, PfailAndPrecision,
6-
PfailTestMeta, PfailTestResult,
5+
precision_with_padding, update_ap_params_msg_and_carry_moduli, DecryptionAndNoiseResult,
6+
NoiseSample, PfailAndPrecision, PfailTestMeta, PfailTestResult,
77
};
88
use super::{should_run_short_pfail_tests_debug, should_use_single_key_debug};
99
use crate::shortint::atomic_pattern::AtomicPattern;
@@ -19,7 +19,7 @@ use crate::shortint::parameters::test_params::{
1919
};
2020
use crate::shortint::parameters::{
2121
AtomicPatternParameters, CarryModulus, CiphertextModulusLog, CompressionParameters,
22-
MessageModulus, PBSParameters, Variance,
22+
MessageModulus, Variance,
2323
};
2424
use crate::shortint::server_key::ServerKey;
2525
use crate::shortint::{PaddingBit, ShortintEncoding};
@@ -616,9 +616,23 @@ where
616616
compression_carry_mod,
617617
);
618618

619+
// Here we update the message modulus only:
620+
// - because the message modulus matches for the compression encoding and compute encoding
621+
// - so that the carry modulus stays the same and we apply the same dot product as normal
622+
// for 2_2
623+
// - so that the effective encoding after the storage is the one we used to evaluate the
624+
// pfail
619625
let updated_message_mod = MessageModulus(1 << 6);
620626
let updated_carry_mod = compression_carry_mod;
621627

628+
update_ap_params_msg_and_carry_moduli(&mut params, updated_message_mod, updated_carry_mod);
629+
630+
assert!(
631+
(params.message_modulus().0 * params.carry_modulus().0).ilog2()
632+
<= comp_params.storage_log_modulus().0 as u32,
633+
"Compression storage modulus cannot store enough bits for pfail estimation"
634+
);
635+
622636
let updated_precision_with_padding =
623637
precision_with_padding(updated_message_mod, updated_carry_mod);
624638

@@ -633,27 +647,6 @@ where
633647
updated_carry_mod,
634648
);
635649

636-
// Here we update the message modulus only:
637-
// - because the message modulus matches for the compression encoding and compute encoding
638-
// - so that the carry modulus stays the same and we apply the same dot product as normal
639-
// for 2_2
640-
// - so that the effective encoding after the storage is the one we used to evaluate the
641-
// pfail
642-
// TODO: do something about this
643-
match &mut params {
644-
AtomicPatternParameters::Standard(pbsparameters) => match pbsparameters {
645-
PBSParameters::PBS(classic_pbsparameters) => {
646-
classic_pbsparameters.message_modulus = updated_message_mod
647-
}
648-
PBSParameters::MultiBitPBS(multi_bit_pbsparameters) => {
649-
multi_bit_pbsparameters.message_modulus = updated_message_mod
650-
}
651-
},
652-
AtomicPatternParameters::KeySwitch32(key_switch32_pbsparameters) => {
653-
key_switch32_pbsparameters.message_modulus = updated_message_mod
654-
}
655-
}
656-
657650
let pfail_test_meta = if should_run_short_pfail_tests_debug() {
658651
// To have the same amount of keys generated as the case where a single run is a single
659652
// sample

tfhe/src/shortint/server_key/tests/noise_distribution/utils/mod.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -503,19 +503,11 @@ impl DecryptionAndNoiseResult {
503503
}
504504
}
505505

506-
pub fn update_ap_params_for_pfail(
506+
pub fn update_ap_params_msg_and_carry_moduli(
507507
ap_params: &mut AtomicPatternParameters,
508508
new_message_modulus: MessageModulus,
509509
new_carry_modulus: CarryModulus,
510-
) -> (PfailAndPrecision, PfailAndPrecision) {
511-
let orig_pfail_and_precision = PfailAndPrecision::new_from_ap_params(&*ap_params);
512-
513-
println!("original_pfail: {}", orig_pfail_and_precision.pfail());
514-
println!(
515-
"original_pfail_log2: {}",
516-
orig_pfail_and_precision.pfail().log2()
517-
);
518-
510+
) {
519511
match ap_params {
520512
AtomicPatternParameters::Standard(pbsparameters) => match pbsparameters {
521513
PBSParameters::PBS(classic_pbsparameters) => {
@@ -532,6 +524,22 @@ pub fn update_ap_params_for_pfail(
532524
key_switch32_pbsparameters.carry_modulus = new_carry_modulus;
533525
}
534526
}
527+
}
528+
529+
pub fn update_ap_params_for_pfail(
530+
ap_params: &mut AtomicPatternParameters,
531+
new_message_modulus: MessageModulus,
532+
new_carry_modulus: CarryModulus,
533+
) -> (PfailAndPrecision, PfailAndPrecision) {
534+
let orig_pfail_and_precision = PfailAndPrecision::new_from_ap_params(&*ap_params);
535+
536+
println!("original_pfail: {}", orig_pfail_and_precision.pfail());
537+
println!(
538+
"original_pfail_log2: {}",
539+
orig_pfail_and_precision.pfail().log2()
540+
);
541+
542+
update_ap_params_msg_and_carry_moduli(ap_params, new_message_modulus, new_carry_modulus);
535543

536544
let new_expected_pfail = equivalent_pfail_gaussian_noise(
537545
orig_pfail_and_precision.precision_with_padding().value,

0 commit comments

Comments
 (0)