Skip to content

Commit

Permalink
chore!: use u64 for shortint metadata
Browse files Browse the repository at this point in the history
This switches from usize to u64 for shortint's metdata:
* Degree
* MaxDegree
* CarryModulus
* MessageModulus

The reasoning is that usize should be preferred when the value is used
as some kind of index, memory access, etc, and not numbers like these
metadata are.

This is a breaking API change
This is also a somewhat breaking serialization change
depending on the serialization format (bincode should be ok as it
encodes usize as u64)
  • Loading branch information
tmontaigu committed Nov 22, 2024
1 parent 0ced3d6 commit 38dbd31
Show file tree
Hide file tree
Showing 107 changed files with 797 additions and 865 deletions.
12 changes: 6 additions & 6 deletions tfhe/benches/shortint/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fn bench_server_key_unary_function<F>(

let mut rng = rand::thread_rng();

let modulus = cks.parameters.message_modulus().0 as u64;
let modulus = cks.parameters.message_modulus().0;

let clear_text = rng.gen::<u64>() % modulus;

Expand Down Expand Up @@ -138,7 +138,7 @@ fn bench_server_key_binary_function<F>(

let mut rng = rand::thread_rng();

let modulus = cks.parameters.message_modulus().0 as u64;
let modulus = cks.parameters.message_modulus().0;

let clear_0 = rng.gen::<u64>() % modulus;
let clear_1 = rng.gen::<u64>() % modulus;
Expand Down Expand Up @@ -184,7 +184,7 @@ fn bench_server_key_binary_scalar_function<F>(

let mut rng = rand::thread_rng();

let modulus = cks.parameters.message_modulus().0 as u64;
let modulus = cks.parameters.message_modulus().0;

let clear_0 = rng.gen::<u64>() % modulus;
let clear_1 = rng.gen::<u64>() % modulus;
Expand Down Expand Up @@ -229,7 +229,7 @@ fn bench_server_key_binary_scalar_division_function<F>(

let mut rng = rand::thread_rng();

let modulus = cks.parameters.message_modulus().0 as u64;
let modulus = cks.parameters.message_modulus().0;
assert_ne!(modulus, 1);

let clear_0 = rng.gen::<u64>() % modulus;
Expand Down Expand Up @@ -270,7 +270,7 @@ fn carry_extract_bench(c: &mut Criterion, params_set: BenchParamsSet) {

let mut rng = rand::thread_rng();

let modulus = cks.parameters.message_modulus().0 as u64;
let modulus = cks.parameters.message_modulus().0;

let clear_0 = rng.gen::<u64>() % modulus;

Expand Down Expand Up @@ -306,7 +306,7 @@ fn programmable_bootstrapping_bench(c: &mut Criterion, params_set: BenchParamsSe

let mut rng = rand::thread_rng();

let modulus = cks.parameters.message_modulus().0 as u64;
let modulus = cks.parameters.message_modulus().0;

let acc = sks.generate_lookup_table(|x| x);

Expand Down
8 changes: 4 additions & 4 deletions tfhe/benches/utilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ pub struct CryptoParametersRecord<Scalar: UnsignedInteger> {
pub br_base_log: Option<DecompositionBaseLog>,
pub packing_ks_level: Option<DecompositionLevelCount>,
pub packing_ks_base_log: Option<DecompositionBaseLog>,
pub message_modulus: Option<usize>,
pub carry_modulus: Option<usize>,
pub message_modulus: Option<u64>,
pub carry_modulus: Option<u64>,
pub ciphertext_modulus: Option<CiphertextModulus<Scalar>>,
pub lwe_per_glwe: Option<LweCiphertextCount>,
pub storage_log_modulus: Option<CiphertextModulusLog>,
Expand Down Expand Up @@ -267,8 +267,8 @@ struct BenchmarkParametersRecord<Scalar: UnsignedInteger> {
display_name: String,
crypto_parameters_alias: String,
crypto_parameters: CryptoParametersRecord<Scalar>,
message_modulus: Option<usize>,
carry_modulus: Option<usize>,
message_modulus: Option<u64>,
carry_modulus: Option<u64>,
ciphertext_modulus: usize,
bit_size: u32,
polynomial_multiplication: PolynomialMultiplication,
Expand Down
2 changes: 1 addition & 1 deletion tfhe/docs/references/fine-grained-apis/integer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ fn main() {
let msg2 = 13;

// message_modulus^vec_length
let modulus = client_key.parameters().message_modulus().0.pow(num_block as u32) as u64;
let modulus = client_key.parameters().message_modulus().0.pow(num_block as u32);

// We use the client key to encrypt two messages:
let ct_1 = client_key.encrypt(msg1);
Expand Down
8 changes: 4 additions & 4 deletions tfhe/docs/references/fine-grained-apis/integer/operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ fn main() {
let scalar = 3u64;

// message_modulus^vec_length
let modulus = client_key.parameters().message_modulus().0.pow(num_block as u32) as u64;
let modulus = client_key.parameters().message_modulus().0.pow(num_block as u32);

// We use the client key to encrypt two messages:
let mut ct_1 = client_key.encrypt(msg1);
Expand Down Expand Up @@ -154,7 +154,7 @@ fn main() {
let scalar = 3u64;

// message_modulus^vec_length
let modulus = client_key.parameters().message_modulus().0.pow(num_block as u32) as u64;
let modulus = client_key.parameters().message_modulus().0.pow(num_block as u32);

// We use the client key to encrypt two messages:
let mut ct_1 = client_key.encrypt(msg1);
Expand Down Expand Up @@ -193,7 +193,7 @@ fn main() {
let scalar = 3u64;

// message_modulus^vec_length
let modulus = client_key.parameters().message_modulus().0.pow(num_block as u32) as u64;
let modulus = client_key.parameters().message_modulus().0.pow(num_block as u32);

// We use the client key to encrypt two messages:
let mut ct_1 = client_key.encrypt(msg1);
Expand Down Expand Up @@ -240,7 +240,7 @@ fn main() {
let scalar = 3u64;

// message_modulus^vec_length
let modulus = client_key.parameters().message_modulus().0.pow(num_block as u32) as u64;
let modulus = client_key.parameters().message_modulus().0.pow(num_block as u32);

// We use the client key to encrypt two messages:
let mut ct_1 = client_key.encrypt(msg1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let msg2 = 12;

// message_modulus^vec_length
let modulus = client_key.parameters().message_modulus().0.pow(num_block as u32) as u64;
let modulus = client_key.parameters().message_modulus().0.pow(num_block as u32);

let ct_1 = client_key.encrypt(msg1);
let ct_2 = client_key.encrypt(msg2);
Expand Down
2 changes: 1 addition & 1 deletion tfhe/docs/references/fine-grained-apis/quick_start.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ fn main() {

// We use the client key to decrypt the output of the circuit:
let output = client_key.decrypt(&ct_3);
assert_eq!(output, (msg1 + msg2) % modulus as u64);
assert_eq!(output, (msg1 + msg2) % modulus);
}
```

Expand Down
2 changes: 1 addition & 1 deletion tfhe/docs/references/fine-grained-apis/shortint/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,6 @@ fn main() {

// We use the client key to decrypt the output of the circuit:
let output = client_key.decrypt(&ct_3);
assert_eq!(output, (msg1 + msg2) % modulus as u64);
assert_eq!(output, (msg1 + msg2) % modulus);
}
```
16 changes: 8 additions & 8 deletions tfhe/docs/references/fine-grained-apis/shortint/operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fn main() {

// We use the client key to decrypt the output of the circuit:
let output = client_key.decrypt(&ct_1);
println!("expected {}, found {}", ((msg1 * scalar as u64 - msg2) * msg2) % modulus as u64, output);
println!("expected {}, found {}", ((msg1 * scalar as u64 - msg2) * msg2) % modulus, output);
}
```

Expand Down Expand Up @@ -114,7 +114,7 @@ fn main() {

// We use the client key to decrypt the output of the circuit:
let output = client_key.decrypt(&ct_1);
assert_eq!(output, ((msg1 * scalar as u64 - msg2) * msg2) % modulus as u64);
assert_eq!(output, ((msg1 * scalar as u64 - msg2) * msg2) % modulus);
}
```

Expand Down Expand Up @@ -146,7 +146,7 @@ fn main() {

// We use the client key to decrypt the output of the circuit:
let output = client_key.decrypt(&ct_1);
assert_eq!(output, ((msg1 * scalar as u64 - msg2) * msg2) % modulus as u64);
assert_eq!(output, ((msg1 * scalar as u64 - msg2) * msg2) % modulus);
}
```

Expand Down Expand Up @@ -180,7 +180,7 @@ fn main() {

// We use the client key to decrypt the output of the circuit:
let output = client_key.decrypt(&ct_1);
assert_eq!(output, ((msg1 * scalar as u64 - msg2) * msg2) % modulus as u64);
assert_eq!(output, ((msg1 * scalar as u64 - msg2) * msg2) % modulus);
}
```

Expand Down Expand Up @@ -255,7 +255,7 @@ fn main() {

// We use the client key to decrypt the output of the circuit:
let output = client_key.decrypt(&ct_3);
assert_eq!(output, (msg1 + msg2) % modulus as u64);
assert_eq!(output, (msg1 + msg2) % modulus);
}
```

Expand Down Expand Up @@ -286,7 +286,7 @@ fn main() {

// We use the client key to decrypt the output of the circuit:
let output = client_key.decrypt(&ct_3);
assert_eq!(output, (msg1 & msg2) % modulus as u64);
assert_eq!(output, (msg1 & msg2) % modulus);
}
```

Expand Down Expand Up @@ -317,7 +317,7 @@ fn main() {

// We use the client key to decrypt the output of the circuit:
let output = client_key.decrypt(&ct_3);
assert_eq!(output, (msg1 >= msg2) as u64 % modulus as u64);
assert_eq!(output, (msg1 >= msg2) as u64 % modulus);
}
```

Expand Down Expand Up @@ -365,7 +365,7 @@ fn main() {
let msg1 = 3;
let msg2 = 2;

let modulus = client_key.parameters.message_modulus().0 as u64;
let modulus = client_key.parameters.message_modulus().0;

// We use the private client key to encrypt two messages:
let ct_1 = client_key.encrypt(msg1);
Expand Down
4 changes: 2 additions & 2 deletions tfhe/js_on_wasm_tests/test-hlapi-signed.js
Original file line number Diff line number Diff line change
Expand Up @@ -567,8 +567,8 @@ test('hlapi_compact_pk_conformance', (t) => {
let serializedPublicKey = publicKey.safe_serialize(limit);
let _publicKey = TfheCompactPublicKey.safe_deserialize_conformant(serializedPublicKey, limit, publicKeyParams);

const message_modulus = 4;
const carry_modulus = 4;
const message_modulus = BigInt(4);
const carry_modulus = BigInt(4);
const modulus_pow_2_exponent = 64;
const ks_level = 5;
const ks_base_log = 3;
Expand Down
4 changes: 2 additions & 2 deletions tfhe/src/c_api/shortint/ciphertext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct ShortintCompressedCiphertext(pub(in crate::c_api) shortint::Compresse
#[no_mangle]
pub unsafe extern "C" fn shortint_ciphertext_set_degree(
ciphertext: *mut ShortintCiphertext,
degree: usize,
degree: u64,
) -> c_int {
catch_panic(|| {
let ciphertext = get_mut_checked(ciphertext).unwrap();
Expand All @@ -24,7 +24,7 @@ pub unsafe extern "C" fn shortint_ciphertext_set_degree(
#[no_mangle]
pub unsafe extern "C" fn shortint_ciphertext_get_degree(
ciphertext: *const ShortintCiphertext,
result: *mut usize,
result: *mut u64,
) -> c_int {
catch_panic(|| {
check_ptr_is_non_null_and_aligned(result).unwrap();
Expand Down
24 changes: 11 additions & 13 deletions tfhe/src/c_api/shortint/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub enum ShortintEncryptionKeyChoice {
ShortintEncryptionKeyChoiceSmall,
}

impl From<ShortintEncryptionKeyChoice> for crate::shortint::parameters::EncryptionKeyChoice {
impl From<ShortintEncryptionKeyChoice> for EncryptionKeyChoice {
fn from(value: ShortintEncryptionKeyChoice) -> Self {
match value {
ShortintEncryptionKeyChoice::ShortintEncryptionKeyChoiceBig => Self::Big,
Expand All @@ -34,9 +34,9 @@ pub struct ShortintPBSParameters {
pub pbs_level: usize,
pub ks_base_log: usize,
pub ks_level: usize,
pub message_modulus: usize,
pub carry_modulus: usize,
pub max_noise_level: usize,
pub message_modulus: u64,
pub carry_modulus: u64,
pub max_noise_level: u64,
pub log2_p_fail: f64,
pub modulus_power_of_2_exponent: usize,
pub encryption_key_choice: ShortintEncryptionKeyChoice,
Expand All @@ -56,14 +56,12 @@ impl TryFrom<ShortintPBSParameters> for crate::shortint::ClassicPBSParameters {
pbs_level: DecompositionLevelCount(c_params.pbs_level),
ks_base_log: DecompositionBaseLog(c_params.ks_base_log),
ks_level: DecompositionLevelCount(c_params.ks_level),
message_modulus: crate::shortint::parameters::MessageModulus(c_params.message_modulus),
carry_modulus: crate::shortint::parameters::CarryModulus(c_params.carry_modulus),
ciphertext_modulus: crate::shortint::parameters::CiphertextModulus::try_new_power_of_2(
message_modulus: MessageModulus(c_params.message_modulus),
carry_modulus: CarryModulus(c_params.carry_modulus),
ciphertext_modulus: CiphertextModulus::try_new_power_of_2(
c_params.modulus_power_of_2_exponent,
)?,
max_noise_level: crate::shortint::parameters::MaxNoiseLevel::new(
c_params.max_noise_level as u64,
),
max_noise_level: MaxNoiseLevel::new(c_params.max_noise_level),
log2_p_fail: c_params.log2_p_fail,
encryption_key_choice: c_params.encryption_key_choice.into(),
})
Expand Down Expand Up @@ -113,7 +111,7 @@ impl ShortintPBSParameters {
ks_level: rust_params.ks_level.0,
message_modulus: rust_params.message_modulus.0,
carry_modulus: rust_params.carry_modulus.0,
max_noise_level: rust_params.max_noise_level.get() as usize,
max_noise_level: rust_params.max_noise_level.get(),
log2_p_fail: rust_params.log2_p_fail,
modulus_power_of_2_exponent: convert_modulus(rust_params.ciphertext_modulus),
encryption_key_choice: ShortintEncryptionKeyChoice::convert(
Expand Down Expand Up @@ -170,8 +168,8 @@ impl ShortintCompactCiphertextListCastingParameters {
pub struct ShortintCompactPublicKeyEncryptionParameters {
pub encryption_lwe_dimension: usize,
pub encryption_noise_distribution: crate::c_api::core_crypto::DynamicDistribution,
pub message_modulus: usize,
pub carry_modulus: usize,
pub message_modulus: u64,
pub carry_modulus: u64,
pub modulus_power_of_2_exponent: usize,
// Normally the CompactPublicKeyEncryptionParameters has an additional field expansion_kind,
// but it's only used to manage different kind of parameters internally, for the C API
Expand Down
4 changes: 2 additions & 2 deletions tfhe/src/high_level_api/integers/unsigned/tests/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ fn test_cpk_encrypt_cast_compute_hl() {
assert_eq!(param_pke_only.message_modulus, param_fhe.message_modulus);
assert_eq!(param_pke_only.carry_modulus, param_fhe.carry_modulus);

let modulus = param_fhe.message_modulus.0.pow(num_block as u32) as u64;
let modulus = param_fhe.message_modulus.0.pow(num_block as u32);

let (client_key, server_key) = generate_keys(
ConfigBuilder::with_custom_parameters(param_fhe)
Expand Down Expand Up @@ -563,7 +563,7 @@ fn test_compressed_cpk_encrypt_cast_compute_hl() {
assert_eq!(param_pke_only.message_modulus, param_fhe.message_modulus);
assert_eq!(param_pke_only.carry_modulus, param_fhe.carry_modulus);

let modulus = param_fhe.message_modulus.0.pow(num_block as u32) as u64;
let modulus = param_fhe.message_modulus.0.pow(num_block as u32);

let config = ConfigBuilder::with_custom_parameters(param_fhe)
.use_dedicated_compact_public_key_parameters((param_pke_only, param_ksk))
Expand Down
16 changes: 9 additions & 7 deletions tfhe/src/integer/ciphertext/compact_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ fn sanitize_boolean_blocks(
sks: &ServerKey,
infos: &[DataKind],
) -> Vec<Ciphertext> {
let message_modulus = sks.message_modulus().0 as u64;
let message_modulus = sks.message_modulus().0;
let msg_extract_bool = sks.key.generate_lookup_table(|x: u64| {
let tmp = x % message_modulus;
if tmp == 0 {
Expand Down Expand Up @@ -293,7 +293,7 @@ impl CompactCiphertextListBuilder {
}

// Here self.messages are decomposed blocks in range [0..message_modulus[
let msg_mod = self.pk.key.message_modulus().0 as u64;
let msg_mod = self.pk.key.message_modulus().0;
let packed_messaged_iter = self
.messages
.chunks(2)
Expand Down Expand Up @@ -321,7 +321,7 @@ impl CompactCiphertextListBuilder {
crs,
metadata,
load,
self.pk.key.parameters.message_modulus.0 as u64,
self.pk.key.parameters.message_modulus.0,
)?;
Ok(ProvenCompactCiphertextList {
ct_list,
Expand All @@ -344,7 +344,7 @@ impl CompactCiphertextListBuilder {
));
}

let msg_mod = self.pk.key.parameters.message_modulus.0 as u64;
let msg_mod = self.pk.key.parameters.message_modulus.0;
let packed_messages = self
.messages
.chunks(2)
Expand Down Expand Up @@ -458,8 +458,8 @@ struct IntegerUnpackingToShortintCastingModeHelper {

impl IntegerUnpackingToShortintCastingModeHelper {
pub fn new(message_modulus: MessageModulus, carry_modulus: CarryModulus) -> Self {
let message_modulus = message_modulus.0 as u64;
let carry_modulus = carry_modulus.0 as u64;
let message_modulus = message_modulus.0;
let carry_modulus = carry_modulus.0;
let msg_extract = Box::new(move |x: u64| x % message_modulus);
let carry_extract = Box::new(move |x: u64| (x / carry_modulus) % message_modulus);
let msg_extract_bool = Box::new(move |x: u64| {
Expand Down Expand Up @@ -1083,7 +1083,9 @@ mod tests {
let metadata = [b'i', b'n', b't', b'e', b'g', b'e', b'r'];

let num_blocks = 4usize;
let modulus = (pke_params.message_modulus.0 as u64)
let modulus = pke_params
.message_modulus
.0
.checked_pow(num_blocks as u32)
.unwrap();

Expand Down
Loading

0 comments on commit 38dbd31

Please sign in to comment.