Skip to content

Commit

Permalink
refactor(boolean): call as_view on LWE secret keys where appropriate
Browse files Browse the repository at this point in the history
- previous code had to manually create a view from a container, this is
less convoluted and more user friendly
  • Loading branch information
IceTDrinker committed Jan 8, 2024
1 parent ef9ec13 commit 00ddfde
Showing 1 changed file with 18 additions and 22 deletions.
40 changes: 18 additions & 22 deletions tfhe/src/boolean/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ impl BooleanEngine {
client_key.glwe_secret_key.as_lwe_secret_key(),
client_key.parameters.glwe_modular_std_dev,
),
EncryptionKeyChoice::Small => {
let view = LweSecretKey::from_container(client_key.lwe_secret_key.as_ref());
(view, client_key.parameters.lwe_modular_std_dev)
}
EncryptionKeyChoice::Small => (
client_key.lwe_secret_key.as_view(),
client_key.parameters.lwe_modular_std_dev,
),
};

// Formula is (n + 1) * log2(q) + 128
Expand Down Expand Up @@ -161,10 +161,10 @@ impl BooleanEngine {
client_key.glwe_secret_key.as_lwe_secret_key(),
client_key.parameters.glwe_modular_std_dev,
),
EncryptionKeyChoice::Small => {
let view = LweSecretKey::from_container(client_key.lwe_secret_key.as_ref());
(view, client_key.parameters.lwe_modular_std_dev)
}
EncryptionKeyChoice::Small => (
client_key.lwe_secret_key.as_view(),
client_key.parameters.lwe_modular_std_dev,
),
};

// Formula is (n + 1) * log2(q) + 128
Expand Down Expand Up @@ -211,9 +211,7 @@ impl BooleanEngine {
cks2.glwe_secret_key.as_lwe_secret_key(),
),
(EncryptionKeyChoice::Small, EncryptionKeyChoice::Small) => {
let view1 = LweSecretKey::from_container(cks1.lwe_secret_key.as_ref());
let view2 = LweSecretKey::from_container(cks2.lwe_secret_key.as_ref());
(view1, view2)
(cks1.lwe_secret_key.as_view(), cks2.lwe_secret_key.as_view())
}
(choice1, choice2) => panic!(
"EncryptionKeyChoice of cks1 and cks2 must be the same.\
Expand Down Expand Up @@ -251,10 +249,10 @@ cks1 has {choice1:?}, cks2 has: {choice2:?}
cks.glwe_secret_key.as_lwe_secret_key(),
cks.parameters.glwe_modular_std_dev,
),
EncryptionKeyChoice::Small => {
let view = LweSecretKey::from_container(cks.lwe_secret_key.as_ref());
(view, cks.parameters.lwe_modular_std_dev)
}
EncryptionKeyChoice::Small => (
cks.lwe_secret_key.as_view(),
cks.parameters.lwe_modular_std_dev,
),
};

// encryption
Expand Down Expand Up @@ -282,10 +280,10 @@ cks1 has {choice1:?}, cks2 has: {choice2:?}
cks.glwe_secret_key.as_lwe_secret_key(),
cks.parameters.glwe_modular_std_dev,
),
EncryptionKeyChoice::Small => {
let view = LweSecretKey::from_container(cks.lwe_secret_key.as_ref());
(view, cks.parameters.lwe_modular_std_dev)
}
EncryptionKeyChoice::Small => (
cks.lwe_secret_key.as_view(),
cks.parameters.lwe_modular_std_dev,
),
};

// encryption
Expand Down Expand Up @@ -357,9 +355,7 @@ cks1 has {choice1:?}, cks2 has: {choice2:?}
Ciphertext::Encrypted(ciphertext) => {
let lwe_sk = match cks.parameters.encryption_key_choice {
EncryptionKeyChoice::Big => cks.glwe_secret_key.as_lwe_secret_key(),
EncryptionKeyChoice::Small => {
LweSecretKey::from_container(cks.lwe_secret_key.as_ref())
}
EncryptionKeyChoice::Small => cks.lwe_secret_key.as_view(),
};

// decryption
Expand Down

0 comments on commit 00ddfde

Please sign in to comment.