mirror of
https://github.com/zama-ai/tfhe-rs.git
synced 2026-01-08 22:28:01 -05:00
update affected code
This commit is contained in:
@@ -241,10 +241,9 @@ impl ClientKey {
|
||||
where
|
||||
T: RecomposableFrom<u64> + UnsignedNumeric,
|
||||
{
|
||||
self.decrypt_radix_impl(
|
||||
&ctxt.blocks,
|
||||
crate::shortint::ClientKey::decrypt_message_and_carry,
|
||||
)
|
||||
self.decrypt_radix_impl(&ctxt.blocks, |ck, ct| {
|
||||
crate::shortint::ClientKey::decrypt_decode_padding(ck, ct).carry_and_msg
|
||||
})
|
||||
}
|
||||
|
||||
/// Decrypts a ciphertext encrypting an radix integer encrypted without padding
|
||||
@@ -271,10 +270,9 @@ impl ClientKey {
|
||||
where
|
||||
T: RecomposableFrom<u64> + UnsignedNumeric,
|
||||
{
|
||||
self.decrypt_radix_impl(
|
||||
&ctxt.blocks,
|
||||
crate::shortint::ClientKey::decrypt_message_and_carry_without_padding,
|
||||
)
|
||||
self.decrypt_radix_impl(&ctxt.blocks, |ck, ct| {
|
||||
crate::shortint::ClientKey::decrypt_decode_without_padding(ck, ct).carry_and_msg
|
||||
})
|
||||
}
|
||||
|
||||
/// Decrypts a ciphertext in radix decomposition into 64bits
|
||||
@@ -368,7 +366,9 @@ impl ClientKey {
|
||||
where
|
||||
T: RecomposableSignedInteger,
|
||||
{
|
||||
self.decrypt_signed_radix_impl(ctxt, crate::shortint::ClientKey::decrypt_message_and_carry)
|
||||
self.decrypt_signed_radix_impl(ctxt, |ck, ct| {
|
||||
crate::shortint::ClientKey::decrypt_decode_padding(ck, ct).carry_and_msg
|
||||
})
|
||||
}
|
||||
|
||||
pub fn decrypt_signed_radix_impl<T, F>(
|
||||
@@ -551,7 +551,7 @@ impl ClientKey {
|
||||
// Decrypting each block individually
|
||||
for (c_i, b_i) in ctxt.blocks.iter().zip(ctxt.moduli.iter()) {
|
||||
// decrypt the component i of the integer and multiply it by the radix product
|
||||
val.push(self.key.decrypt_message_and_carry(c_i) % b_i);
|
||||
val.push(self.key.decrypt_decode_padding(c_i).carry_and_msg % b_i);
|
||||
}
|
||||
|
||||
// Computing the inverse CRT to recompose the message
|
||||
|
||||
@@ -3408,15 +3408,15 @@ where
|
||||
// Manually check that each shortint block of the input
|
||||
// corresponds to what we want.
|
||||
let shortint_cks = &cks.as_ref().key;
|
||||
let first_block = shortint_cks.decrypt_message_and_carry(&ct.blocks[0]);
|
||||
let first_block_msg = first_block % block_msg_mod;
|
||||
let first_block_carry = first_block / block_msg_mod;
|
||||
let first_block = shortint_cks.decrypt_decode_padding(&ct.blocks[0]);
|
||||
let first_block_msg = first_block.msg;
|
||||
let first_block_carry = first_block.carry;
|
||||
assert_eq!(first_block_msg, (block_msg_mod - 1 + msg) % block_msg_mod);
|
||||
assert_eq!(first_block_carry, (block_msg_mod - 1 + msg) / block_msg_mod);
|
||||
for b in &ct.blocks[1..] {
|
||||
let block = shortint_cks.decrypt_message_and_carry(b);
|
||||
let msg = block % block_msg_mod;
|
||||
let carry = block / block_msg_mod;
|
||||
let block = shortint_cks.decrypt_decode_padding(b);
|
||||
let msg = block.msg;
|
||||
let carry = block.carry;
|
||||
assert_eq!(msg, block_msg_mod - 1);
|
||||
assert_eq!(carry, 0);
|
||||
}
|
||||
@@ -3439,11 +3439,11 @@ where
|
||||
// Manually check each shortint block of the output
|
||||
let shortint_cks = &cks.as_ref().key;
|
||||
assert_eq!(
|
||||
shortint_cks.decrypt_message_and_carry(&ct.blocks[0]),
|
||||
shortint_cks.decrypt(&ct.blocks[0]),
|
||||
(block_msg_mod - 1 + msg) % block_msg_mod
|
||||
);
|
||||
for b in &ct.blocks[1..] {
|
||||
assert_eq!(shortint_cks.decrypt_message_and_carry(b), 0);
|
||||
assert_eq!(shortint_cks.decrypt(b), 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3501,9 +3501,9 @@ where
|
||||
.take(cks.num_blocks());
|
||||
let shortint_cks = &cks.as_ref().key;
|
||||
for (block, expected_msg) in ct.blocks.iter().zip(expected_block_iter) {
|
||||
let block = shortint_cks.decrypt_message_and_carry(block);
|
||||
let msg = block % block_msg_mod;
|
||||
let carry = block / block_msg_mod;
|
||||
let block = shortint_cks.decrypt_decode_padding(block);
|
||||
let msg = block.msg;
|
||||
let carry = block.carry;
|
||||
|
||||
assert_eq!(msg, expected_msg);
|
||||
assert_eq!(carry, 0);
|
||||
@@ -3544,9 +3544,9 @@ where
|
||||
expected_blocks[absorber_block_index] = 0;
|
||||
|
||||
for (block, expected_block) in ct.blocks.iter().zip(expected_blocks) {
|
||||
let block = shortint_cks.decrypt_message_and_carry(block);
|
||||
let msg = block % block_msg_mod;
|
||||
let carry = block / block_msg_mod;
|
||||
let block = shortint_cks.decrypt_decode_padding(block);
|
||||
let msg = block.msg;
|
||||
let carry = block.carry;
|
||||
|
||||
let expected_msg = expected_block % block_msg_mod;
|
||||
let expected_carry = expected_block / block_msg_mod;
|
||||
@@ -3597,9 +3597,9 @@ where
|
||||
.take(cks.num_blocks());
|
||||
let shortint_cks = &cks.as_ref().key;
|
||||
for (block, expected_msg) in ct.blocks.iter().zip(expected_block_iter) {
|
||||
let block = shortint_cks.decrypt_message_and_carry(block);
|
||||
let msg = block % block_msg_mod;
|
||||
let carry = block / block_msg_mod;
|
||||
let block = shortint_cks.decrypt_decode_padding(block);
|
||||
let msg = block.msg;
|
||||
let carry = block.carry;
|
||||
|
||||
assert_eq!(msg, expected_msg);
|
||||
assert_eq!(carry, 0);
|
||||
|
||||
@@ -238,8 +238,8 @@ impl ClientKey {
|
||||
/// // |-------|---------|
|
||||
/// // | 0 1 | 1 1 |
|
||||
///
|
||||
/// let dec = cks.decrypt_message_and_carry(&ct);
|
||||
/// assert_eq!(msg, dec);
|
||||
/// let dec = cks.decrypt_decode_padding(&ct);
|
||||
/// assert_eq!(msg, dec.carry_and_msg);
|
||||
/// ```
|
||||
pub fn unchecked_encrypt(&self, message: u64) -> Ciphertext {
|
||||
ShortintEngine::with_thread_local_mut(|engine| engine.unchecked_encrypt(self, message))
|
||||
|
||||
@@ -10,7 +10,7 @@ fn gen_multi_keys_test_fresh_ci_run_filter() {
|
||||
PARAM_KEYSWITCH_1_1_KS_PBS_TO_2_2_KS_PBS,
|
||||
));
|
||||
let ck1 = keys.client_key_1();
|
||||
let (ck2, sk2) = (keys.client_key_2(), keys.server_key_2());
|
||||
let ck2 = keys.client_key_2();
|
||||
let ksk = keys.key_switching_key();
|
||||
|
||||
assert_eq!(ksk.cast_rshift, 2);
|
||||
@@ -18,44 +18,36 @@ fn gen_multi_keys_test_fresh_ci_run_filter() {
|
||||
// Message 0 Carry 0
|
||||
let cipher = ck1.encrypt(0);
|
||||
let output_of_cast = ksk.cast(&cipher);
|
||||
let clear = ck2.decrypt(&output_of_cast);
|
||||
assert_eq!(clear, 0);
|
||||
let ct_carry = sk2.carry_extract(&output_of_cast);
|
||||
let carry = ck2.decrypt(&ct_carry);
|
||||
assert_eq!(carry, 0);
|
||||
let clear = ck2.decrypt_decode_padding(&output_of_cast);
|
||||
assert_eq!(clear.msg, 0);
|
||||
assert_eq!(clear.carry, 0);
|
||||
|
||||
// Message 1 Carry 0
|
||||
let cipher = ck1.encrypt(1);
|
||||
let output_of_cast = ksk.cast(&cipher);
|
||||
let clear = ck2.decrypt(&output_of_cast);
|
||||
assert_eq!(clear, 1);
|
||||
let ct_carry = sk2.carry_extract(&output_of_cast);
|
||||
let carry = ck2.decrypt(&ct_carry);
|
||||
assert_eq!(carry, 0);
|
||||
let clear = ck2.decrypt_decode_padding(&output_of_cast);
|
||||
assert_eq!(clear.msg, 1);
|
||||
assert_eq!(clear.carry, 0);
|
||||
|
||||
// Message 0 Carry 1
|
||||
let cipher = ck1.unchecked_encrypt(2);
|
||||
let output_of_cast = ksk.cast(&cipher);
|
||||
let clear = ck2.decrypt(&output_of_cast);
|
||||
assert_eq!(clear, 2);
|
||||
let ct_carry = sk2.carry_extract(&output_of_cast);
|
||||
let carry = ck2.decrypt(&ct_carry);
|
||||
assert_eq!(carry, 0);
|
||||
let clear = ck2.decrypt_decode_padding(&output_of_cast);
|
||||
assert_eq!(clear.msg, 1);
|
||||
assert_eq!(clear.carry, 0);
|
||||
|
||||
// Message 1 Carry 1
|
||||
let cipher = ck1.unchecked_encrypt(3);
|
||||
let output_of_cast = ksk.cast(&cipher);
|
||||
let clear = ck2.decrypt(&output_of_cast);
|
||||
assert_eq!(clear, 3);
|
||||
let ct_carry = sk2.carry_extract(&output_of_cast);
|
||||
let carry = ck2.decrypt(&ct_carry);
|
||||
assert_eq!(carry, 0);
|
||||
let clear = ck2.decrypt_decode_padding(&output_of_cast);
|
||||
assert_eq!(clear.msg, 1);
|
||||
assert_eq!(clear.carry, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gen_multi_keys_test_fresh_2_ci_run_filter() {
|
||||
let keys2 = KEY_CACHE.get_from_param(PARAM_MESSAGE_3_CARRY_3_KS_PBS);
|
||||
let (ck2, sk2) = (keys2.client_key(), keys2.server_key());
|
||||
let ck2 = keys2.client_key();
|
||||
|
||||
let ksk_params = ShortintKeySwitchingParameters::new(
|
||||
ck2.parameters.ks_base_log(),
|
||||
@@ -75,38 +67,30 @@ fn gen_multi_keys_test_fresh_2_ci_run_filter() {
|
||||
// Message 0 Carry 0
|
||||
let cipher = ck1.encrypt(0);
|
||||
let output_of_cast = ksk.cast(&cipher);
|
||||
let clear = ck2.decrypt(&output_of_cast);
|
||||
let ct_carry = sk2.carry_extract(&output_of_cast);
|
||||
let carry = ck2.decrypt(&ct_carry);
|
||||
assert_eq!(clear, 0);
|
||||
assert_eq!(carry, 0);
|
||||
let clear = ck2.decrypt_decode_padding(&output_of_cast);
|
||||
assert_eq!(clear.msg, 0);
|
||||
assert_eq!(clear.carry, 0);
|
||||
|
||||
// Message 1 Carry 0
|
||||
let cipher = ck1.encrypt(1);
|
||||
let output_of_cast = ksk.cast(&cipher);
|
||||
let clear = ck2.decrypt(&output_of_cast);
|
||||
let ct_carry = sk2.carry_extract(&output_of_cast);
|
||||
let carry = ck2.decrypt(&ct_carry);
|
||||
assert_eq!(clear, 1);
|
||||
assert_eq!(carry, 0);
|
||||
let clear = ck2.decrypt_decode_padding(&output_of_cast);
|
||||
assert_eq!(clear.msg, 1);
|
||||
assert_eq!(clear.carry, 0);
|
||||
|
||||
// Message 0 Carry 1
|
||||
let cipher = ck1.unchecked_encrypt(2);
|
||||
let output_of_cast = ksk.cast(&cipher);
|
||||
let clear = ck2.decrypt(&output_of_cast);
|
||||
let ct_carry = sk2.carry_extract(&output_of_cast);
|
||||
let carry = ck2.decrypt(&ct_carry);
|
||||
assert_eq!(clear, 2);
|
||||
assert_eq!(carry, 0);
|
||||
let clear = ck2.decrypt_decode_padding(&output_of_cast);
|
||||
assert_eq!(clear.msg, 0);
|
||||
assert_eq!(clear.carry, 1);
|
||||
|
||||
// Message 1 Carry 1
|
||||
let cipher = ck1.unchecked_encrypt(3);
|
||||
let output_of_cast = ksk.cast(&cipher);
|
||||
let clear = ck2.decrypt(&output_of_cast);
|
||||
let ct_carry = sk2.carry_extract(&output_of_cast);
|
||||
let carry = ck2.decrypt(&ct_carry);
|
||||
assert_eq!(clear, 3);
|
||||
assert_eq!(carry, 0);
|
||||
let clear = ck2.decrypt_decode_padding(&output_of_cast);
|
||||
assert_eq!(clear.msg, 1);
|
||||
assert_eq!(clear.carry, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -117,7 +101,7 @@ fn gen_multi_keys_test_add_with_overflow_ci_run_filter() {
|
||||
PARAM_KEYSWITCH_1_1_KS_PBS_TO_2_2_KS_PBS,
|
||||
));
|
||||
let (ck1, sk1) = (keys.client_key_1(), keys.server_key_1());
|
||||
let (ck2, sk2) = (keys.client_key_2(), keys.server_key_2());
|
||||
let ck2 = keys.client_key_2();
|
||||
let ksk = keys.key_switching_key();
|
||||
|
||||
// voluntary overflow
|
||||
@@ -128,11 +112,9 @@ fn gen_multi_keys_test_add_with_overflow_ci_run_filter() {
|
||||
let c4 = sk1.unchecked_add(&c3, &c2);
|
||||
|
||||
let output_of_cast = ksk.cast(&c4);
|
||||
let clear = ck2.decrypt(&output_of_cast);
|
||||
assert_eq!(clear, 3);
|
||||
let ct_carry = sk2.carry_extract(&output_of_cast);
|
||||
let carry = ck2.decrypt(&ct_carry);
|
||||
assert_eq!(carry, 0);
|
||||
let clear = ck2.decrypt_decode_padding(&output_of_cast);
|
||||
assert_eq!(clear.msg, 3);
|
||||
assert_eq!(clear.carry, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -158,7 +140,7 @@ fn gen_multi_keys_test_no_shift_ci_run_filter() {
|
||||
#[test]
|
||||
fn gen_multi_keys_test_truncate_ci_run_filter() {
|
||||
let keys2 = KEY_CACHE.get_from_param(PARAM_MESSAGE_1_CARRY_1_KS_PBS);
|
||||
let (ck2, sk2) = (keys2.client_key(), keys2.server_key());
|
||||
let ck2 = keys2.client_key();
|
||||
|
||||
let ksk_params = ShortintKeySwitchingParameters::new(
|
||||
ck2.parameters.ks_base_log(),
|
||||
@@ -170,7 +152,7 @@ fn gen_multi_keys_test_truncate_ci_run_filter() {
|
||||
PARAM_MESSAGE_1_CARRY_1_KS_PBS,
|
||||
ksk_params,
|
||||
));
|
||||
let (ck1, sk1) = (keys.client_key_1(), keys.server_key_1());
|
||||
let ck1 = keys.client_key_1();
|
||||
let ksk = keys.key_switching_key();
|
||||
|
||||
assert_eq!(ksk.cast_rshift, -2);
|
||||
@@ -178,50 +160,38 @@ fn gen_multi_keys_test_truncate_ci_run_filter() {
|
||||
// Message 0 Carry 0
|
||||
let cipher = ck1.unchecked_encrypt(0);
|
||||
let output_of_cast = ksk.cast(&cipher);
|
||||
let clear = ck2.decrypt(&output_of_cast);
|
||||
assert_eq!(clear, 0);
|
||||
let ct_carry = sk2.carry_extract(&output_of_cast);
|
||||
let carry = ck2.decrypt(&ct_carry);
|
||||
assert_eq!(carry, 0);
|
||||
let clear = ck2.decrypt_decode_padding(&output_of_cast);
|
||||
assert_eq!(clear.msg, 0);
|
||||
assert_eq!(clear.carry, 0);
|
||||
|
||||
// Message 1 Carry 0
|
||||
let cipher = ck1.unchecked_encrypt(1);
|
||||
let output_of_cast = ksk.cast(&cipher);
|
||||
let clear = ck2.decrypt(&output_of_cast);
|
||||
assert_eq!(clear, 1);
|
||||
let ct_carry = sk2.carry_extract(&output_of_cast);
|
||||
let carry = ck2.decrypt(&ct_carry);
|
||||
assert_eq!(carry, 0);
|
||||
let clear = ck2.decrypt_decode_padding(&output_of_cast);
|
||||
assert_eq!(clear.msg, 1);
|
||||
assert_eq!(clear.carry, 0);
|
||||
|
||||
// Message 0 Carry 1
|
||||
let cipher = ck1.unchecked_encrypt(2);
|
||||
let output_of_cast = ksk.cast(&cipher);
|
||||
let clear = ck2.decrypt(&output_of_cast);
|
||||
assert_eq!(clear, 0);
|
||||
let ct_carry = sk2.carry_extract(&output_of_cast);
|
||||
let carry = ck2.decrypt(&ct_carry);
|
||||
assert_eq!(carry, 1);
|
||||
let clear = ck2.decrypt_decode_padding(&output_of_cast);
|
||||
assert_eq!(clear.msg, 0);
|
||||
assert_eq!(clear.carry, 1);
|
||||
|
||||
// Message 1 Carry 1
|
||||
let cipher = ck1.unchecked_encrypt(3);
|
||||
let output_of_cast = ksk.cast(&cipher);
|
||||
let clear = ck2.decrypt(&output_of_cast);
|
||||
assert_eq!(clear, 1);
|
||||
let ct_carry = sk2.carry_extract(&output_of_cast);
|
||||
let carry = ck2.decrypt(&ct_carry);
|
||||
assert_eq!(carry, 1);
|
||||
let clear = ck2.decrypt_decode_padding(&output_of_cast);
|
||||
assert_eq!(clear.msg, 1);
|
||||
assert_eq!(clear.carry, 1);
|
||||
|
||||
// Actual truncation
|
||||
let cipher = ck1.unchecked_encrypt(12);
|
||||
let clear = ck1.decrypt(&cipher);
|
||||
let ct_carry = sk1.carry_extract(&cipher);
|
||||
let carry = ck1.decrypt(&ct_carry);
|
||||
assert_eq!((clear, carry), (0, 3));
|
||||
let clear = ck1.decrypt_decode_padding(&cipher);
|
||||
assert_eq!((clear.msg, clear.carry), (0, 3));
|
||||
|
||||
let output_of_cast = ksk.cast(&cipher);
|
||||
let clear = ck2.decrypt(&output_of_cast);
|
||||
assert_eq!(clear, 0);
|
||||
let ct_carry = sk2.carry_extract(&output_of_cast);
|
||||
let carry = ck2.decrypt(&ct_carry);
|
||||
assert_eq!(carry, 0);
|
||||
let clear = ck2.decrypt_decode_padding(&output_of_cast);
|
||||
assert_eq!(clear.msg, 0);
|
||||
assert_eq!(clear.carry, 0);
|
||||
}
|
||||
|
||||
@@ -126,8 +126,8 @@ impl CompressedPublicKey {
|
||||
/// // |-------|---------|
|
||||
/// // | 0 1 | 1 1 |
|
||||
///
|
||||
/// let dec = cks.decrypt_message_and_carry(&ct);
|
||||
/// assert_eq!(msg, dec);
|
||||
/// let dec = cks.decrypt_decode_padding(&ct);
|
||||
/// assert_eq!(msg, dec.carry_and_msg);
|
||||
/// ```
|
||||
pub fn unchecked_encrypt(&self, message: u64) -> Ciphertext {
|
||||
ShortintEngine::with_thread_local_mut(|engine| {
|
||||
@@ -154,8 +154,8 @@ impl CompressedPublicKey {
|
||||
/// let msg = 6;
|
||||
/// let ct = pk.encrypt_without_padding(msg);
|
||||
///
|
||||
/// let dec = cks.decrypt_message_and_carry_without_padding(&ct);
|
||||
/// assert_eq!(msg, dec);
|
||||
/// let dec = cks.decrypt_decode_without_padding(&ct);
|
||||
/// assert_eq!(msg, dec.carry_and_msg);
|
||||
/// ```
|
||||
pub fn encrypt_without_padding(&self, message: u64) -> Ciphertext {
|
||||
ShortintEngine::with_thread_local_mut(|engine| {
|
||||
|
||||
@@ -122,8 +122,8 @@ impl PublicKey {
|
||||
/// // |-------|---------|
|
||||
/// // | 0 1 | 1 1 |
|
||||
///
|
||||
/// let dec = cks.decrypt_message_and_carry(&ct);
|
||||
/// assert_eq!(msg, dec);
|
||||
/// let dec = cks.decrypt_decode_padding(&ct);
|
||||
/// assert_eq!(msg, dec.carry_and_msg);
|
||||
/// ```
|
||||
pub fn unchecked_encrypt(&self, message: u64) -> Ciphertext {
|
||||
ShortintEngine::with_thread_local_mut(|engine| {
|
||||
@@ -150,8 +150,8 @@ impl PublicKey {
|
||||
/// let msg = 6;
|
||||
/// let ct = pk.encrypt_without_padding(msg);
|
||||
///
|
||||
/// let dec = cks.decrypt_message_and_carry_without_padding(&ct);
|
||||
/// assert_eq!(msg, dec);
|
||||
/// let dec = cks.decrypt_decode_without_padding(&ct);
|
||||
/// assert_eq!(msg, dec.carry_and_msg);
|
||||
/// ```
|
||||
pub fn encrypt_without_padding(&self, message: u64) -> Ciphertext {
|
||||
ShortintEngine::with_thread_local_mut(|engine| {
|
||||
|
||||
@@ -700,8 +700,8 @@ impl ServerKey {
|
||||
/// // | 0 0 | 1 0 |
|
||||
///
|
||||
/// // Decrypt:
|
||||
/// let res = cks.decrypt_message_and_carry(&ct);
|
||||
/// assert_eq!(2, res);
|
||||
/// let res = cks.decrypt_decode_padding(&ct);
|
||||
/// assert_eq!(2, res.carry_and_msg);
|
||||
/// ```
|
||||
pub fn carry_extract_assign(&self, ct: &mut Ciphertext) {
|
||||
ShortintEngine::with_thread_local_mut(|engine| engine.carry_extract_assign(self, ct))
|
||||
|
||||
@@ -265,9 +265,8 @@ impl ServerKey {
|
||||
/// assert!(ct_res.is_ok());
|
||||
///
|
||||
/// let ct_res = ct_res.unwrap();
|
||||
/// let clear_res = cks.decrypt_message_and_carry(&ct_res);
|
||||
/// let modulus = cks.parameters.message_modulus().0 as u64;
|
||||
/// assert_eq!(clear_res % modulus, 2);
|
||||
/// let clear_res = cks.decrypt(&ct_res);
|
||||
/// assert_eq!(clear_res, 2);
|
||||
///
|
||||
/// let (cks, sks) = gen_keys(PARAM_MESSAGE_2_CARRY_2_PBS_KS);
|
||||
///
|
||||
@@ -281,9 +280,8 @@ impl ServerKey {
|
||||
/// assert!(ct_res.is_ok());
|
||||
///
|
||||
/// let ct_res = ct_res.unwrap();
|
||||
/// let clear_res = cks.decrypt_message_and_carry(&ct_res);
|
||||
/// let modulus = cks.parameters.message_modulus().0 as u64;
|
||||
/// assert_eq!(clear_res % modulus, 2);
|
||||
/// let clear_res = cks.decrypt(&ct_res);
|
||||
/// assert_eq!(clear_res, 2);
|
||||
/// ```
|
||||
pub fn checked_mul_lsb(
|
||||
&self,
|
||||
@@ -327,9 +325,8 @@ impl ServerKey {
|
||||
///
|
||||
/// assert!(ct_res.is_ok());
|
||||
///
|
||||
/// let clear_res = cks.decrypt_message_and_carry(&ct_1);
|
||||
/// let modulus = cks.parameters.message_modulus().0 as u64;
|
||||
/// assert_eq!(clear_res % modulus, 2);
|
||||
/// let clear_res = cks.decrypt(&ct_1);
|
||||
/// assert_eq!(clear_res, 2);
|
||||
///
|
||||
/// let (cks, sks) = gen_keys(PARAM_MESSAGE_2_CARRY_2_PBS_KS);
|
||||
///
|
||||
@@ -342,9 +339,8 @@ impl ServerKey {
|
||||
///
|
||||
/// assert!(ct_res.is_ok());
|
||||
///
|
||||
/// let clear_res = cks.decrypt_message_and_carry(&ct_1);
|
||||
/// let modulus = cks.parameters.message_modulus().0 as u64;
|
||||
/// assert_eq!(clear_res % modulus, 2);
|
||||
/// let clear_res = cks.decrypt(&ct_1);
|
||||
/// assert_eq!(clear_res, 2);
|
||||
/// ```
|
||||
pub fn checked_mul_lsb_assign(
|
||||
&self,
|
||||
@@ -599,7 +595,7 @@ impl ServerKey {
|
||||
/// let ct_res = ct_res.unwrap();
|
||||
/// let clear_res = cks.decrypt(&ct_res);
|
||||
/// let modulus = cks.parameters.message_modulus().0 as u64;
|
||||
/// assert_eq!(clear_res % modulus, (msg_1 * msg_2) % modulus);
|
||||
/// assert_eq!(clear_res, (msg_1 * msg_2) % modulus);
|
||||
///
|
||||
/// let (cks, sks) = gen_keys(PARAM_MESSAGE_2_CARRY_2_PBS_KS);
|
||||
///
|
||||
@@ -615,7 +611,7 @@ impl ServerKey {
|
||||
/// let ct_res = ct_res.unwrap();
|
||||
/// let clear_res = cks.decrypt(&ct_res);
|
||||
/// let modulus = cks.parameters.message_modulus().0 as u64;
|
||||
/// assert_eq!(clear_res % modulus, (msg_1 * msg_2) % modulus);
|
||||
/// assert_eq!(clear_res, (msg_1 * msg_2) % modulus);
|
||||
/// ```
|
||||
pub fn checked_mul_lsb_with_small_carry(
|
||||
&self,
|
||||
@@ -829,7 +825,7 @@ impl ServerKey {
|
||||
///
|
||||
/// let res = cks.decrypt(&ct_1);
|
||||
/// let modulus = sks.message_modulus.0 as u64;
|
||||
/// assert_eq!(res % modulus, (msg1 * msg2) % modulus);
|
||||
/// assert_eq!(res, (msg1 * msg2) % modulus);
|
||||
///
|
||||
/// // Generate the client key and the server key:
|
||||
/// let (cks, sks) = gen_keys(PARAM_MESSAGE_2_CARRY_2_PBS_KS);
|
||||
@@ -843,7 +839,7 @@ impl ServerKey {
|
||||
///
|
||||
/// let res = cks.decrypt(&ct_1);
|
||||
/// let modulus = sks.message_modulus.0 as u64;
|
||||
/// assert_eq!(res % modulus, (msg1 * msg2) % modulus);
|
||||
/// assert_eq!(res, (msg1 * msg2) % modulus);
|
||||
/// ```
|
||||
pub fn mul_lsb_assign(&self, ct_left: &mut Ciphertext, ct_right: &Ciphertext) {
|
||||
let tmp_rhs: Ciphertext;
|
||||
@@ -909,7 +905,7 @@ impl ServerKey {
|
||||
///
|
||||
/// let res = cks.decrypt(&ct_1);
|
||||
/// let modulus = sks.message_modulus.0 as u64;
|
||||
/// assert_eq!(res % modulus, (msg1 * msg2) % modulus);
|
||||
/// assert_eq!(res, (msg1 * msg2) % modulus);
|
||||
///
|
||||
/// // Generate the client key and the server key:
|
||||
/// let (cks, sks) = gen_keys(PARAM_MESSAGE_2_CARRY_2_PBS_KS);
|
||||
@@ -923,7 +919,7 @@ impl ServerKey {
|
||||
///
|
||||
/// let res = cks.decrypt(&ct_1);
|
||||
/// let modulus = sks.message_modulus.0 as u64;
|
||||
/// assert_eq!(res % modulus, (msg1 * msg2) % modulus);
|
||||
/// assert_eq!(res, (msg1 * msg2) % modulus);
|
||||
/// ```
|
||||
pub fn mul_assign(&self, ct_left: &mut Ciphertext, ct_right: &Ciphertext) {
|
||||
self.mul_lsb_assign(ct_left, ct_right)
|
||||
@@ -1088,7 +1084,7 @@ impl ServerKey {
|
||||
///
|
||||
/// let res = cks.decrypt(&ct_1);
|
||||
/// let modulus = sks.message_modulus.0 as u64;
|
||||
/// assert_eq!(res % modulus, (msg1 * msg2) % modulus);
|
||||
/// assert_eq!(res, (msg1 * msg2) % modulus);
|
||||
///
|
||||
/// // Generate the client key and the server key:
|
||||
/// let (cks, sks) = gen_keys(PARAM_MESSAGE_2_CARRY_2_PBS_KS);
|
||||
@@ -1102,7 +1098,7 @@ impl ServerKey {
|
||||
///
|
||||
/// let res = cks.decrypt(&ct_1);
|
||||
/// let modulus = sks.message_modulus.0 as u64;
|
||||
/// assert_eq!(res % modulus, (msg1 * msg2) % modulus);
|
||||
/// assert_eq!(res, (msg1 * msg2) % modulus);
|
||||
/// ```
|
||||
pub fn smart_mul_lsb_assign(&self, ct_left: &mut Ciphertext, ct_right: &mut Ciphertext) {
|
||||
ShortintEngine::with_thread_local_mut(|engine| {
|
||||
|
||||
@@ -391,8 +391,7 @@ impl ServerKey {
|
||||
///
|
||||
/// // Our result is what we expect
|
||||
/// let clear = cks.decrypt(&ct_res);
|
||||
/// let modulus = cks.parameters.message_modulus().0 as u64;
|
||||
/// assert_eq!(2, clear % modulus);
|
||||
/// assert_eq!(2, clear);
|
||||
///
|
||||
/// let (cks, sks) = gen_keys(PARAM_MESSAGE_2_CARRY_2_PBS_KS);
|
||||
///
|
||||
@@ -407,8 +406,7 @@ impl ServerKey {
|
||||
///
|
||||
/// // Our result is what we expect
|
||||
/// let clear = cks.decrypt(&ct_res);
|
||||
/// let modulus = cks.parameters.message_modulus().0 as u64;
|
||||
/// assert_eq!(2, clear % modulus);
|
||||
/// assert_eq!(2, clear);
|
||||
/// ```
|
||||
pub fn smart_scalar_add(&self, ct: &mut Ciphertext, scalar: u8) -> Ciphertext {
|
||||
ShortintEngine::with_thread_local_mut(|engine| engine.smart_scalar_add(self, ct, scalar))
|
||||
@@ -438,24 +436,24 @@ impl ServerKey {
|
||||
/// // Encrypt a message
|
||||
/// let mut ct = cks.encrypt(msg);
|
||||
///
|
||||
/// // Compute homomorphically a scalar multiplication:
|
||||
/// // Compute homomorphically a scalar addition:
|
||||
/// sks.smart_scalar_add_assign(&mut ct, scalar);
|
||||
///
|
||||
/// // Our result is what we expect
|
||||
/// let clear = cks.decrypt_message_and_carry(&ct);
|
||||
/// assert_eq!(6, clear);
|
||||
/// let clear = cks.decrypt_decode_padding(&ct);
|
||||
/// assert_eq!(6, clear.carry_and_msg);
|
||||
///
|
||||
/// let (cks, sks) = gen_keys(PARAM_MESSAGE_2_CARRY_2_PBS_KS);
|
||||
///
|
||||
/// // Encrypt a message
|
||||
/// let mut ct = cks.encrypt(msg);
|
||||
///
|
||||
/// // Compute homomorphically a scalar multiplication:
|
||||
/// // Compute homomorphically a scalar addition:
|
||||
/// sks.smart_scalar_add_assign(&mut ct, scalar);
|
||||
///
|
||||
/// // Our result is what we expect
|
||||
/// let clear = cks.decrypt_message_and_carry(&ct);
|
||||
/// assert_eq!(6, clear);
|
||||
/// let clear = cks.decrypt_decode_padding(&ct);
|
||||
/// assert_eq!(6, clear.carry_and_msg);
|
||||
/// ```
|
||||
pub fn smart_scalar_add_assign(&self, ct: &mut Ciphertext, scalar: u8) {
|
||||
ShortintEngine::with_thread_local_mut(|engine| {
|
||||
|
||||
@@ -146,7 +146,7 @@ impl ServerKey {
|
||||
/// let ct_res = sks.scalar_bitor(&ct1, msg2 as u8);
|
||||
///
|
||||
/// // Decrypt:
|
||||
/// let res: u64 = cks.decrypt(&ct_res);
|
||||
/// let res = cks.decrypt(&ct_res);
|
||||
/// assert_eq!(msg1 | msg2, res);
|
||||
/// ```
|
||||
pub fn scalar_bitor(&self, lhs: &Ciphertext, rhs: u8) -> Ciphertext {
|
||||
|
||||
@@ -437,8 +437,7 @@ impl ServerKey {
|
||||
///
|
||||
/// // Our result is what we expect
|
||||
/// let clear = cks.decrypt(&ct_res);
|
||||
/// let modulus = cks.parameters.message_modulus().0 as u64;
|
||||
/// assert_eq!(3, clear % modulus);
|
||||
/// assert_eq!(3, clear);
|
||||
///
|
||||
/// let (cks, sks) = gen_keys(PARAM_MESSAGE_2_CARRY_2_PBS_KS);
|
||||
///
|
||||
@@ -453,8 +452,7 @@ impl ServerKey {
|
||||
///
|
||||
/// // Our result is what we expect
|
||||
/// let clear = cks.decrypt(&ct_res);
|
||||
/// let modulus = cks.parameters.message_modulus().0 as u64;
|
||||
/// assert_eq!(3, clear % modulus);
|
||||
/// assert_eq!(3, clear);
|
||||
/// ```
|
||||
pub fn smart_scalar_mul(&self, ct: &mut Ciphertext, scalar: u8) -> Ciphertext {
|
||||
ShortintEngine::with_thread_local_mut(|engine| engine.smart_scalar_mul(self, ct, scalar))
|
||||
|
||||
@@ -47,7 +47,6 @@ impl ServerKey {
|
||||
///
|
||||
/// // Decrypt:
|
||||
/// let dec = cks.decrypt(&ct_res);
|
||||
/// let modulus = cks.parameters.message_modulus().0 as u64;
|
||||
/// assert_eq!(msg >> shift, dec);
|
||||
///
|
||||
/// let (cks, sks) = gen_keys(PARAM_MESSAGE_2_CARRY_2_PBS_KS);
|
||||
@@ -69,7 +68,6 @@ impl ServerKey {
|
||||
///
|
||||
/// // Decrypt:
|
||||
/// let dec = cks.decrypt(&ct_res);
|
||||
/// let modulus = cks.parameters.message_modulus().0 as u64;
|
||||
/// assert_eq!(msg >> shift, dec);
|
||||
/// ```
|
||||
pub fn scalar_right_shift(&self, ct: &Ciphertext, shift: u8) -> Ciphertext {
|
||||
@@ -117,7 +115,6 @@ impl ServerKey {
|
||||
///
|
||||
/// // Decrypt:
|
||||
/// let dec = cks.decrypt(&ct);
|
||||
/// let modulus = cks.parameters.message_modulus().0 as u64;
|
||||
/// assert_eq!(msg >> shift, dec);
|
||||
///
|
||||
/// let (cks, sks) = gen_keys(PARAM_MESSAGE_2_CARRY_2_PBS_KS);
|
||||
@@ -139,7 +136,6 @@ impl ServerKey {
|
||||
///
|
||||
/// // Decrypt:
|
||||
/// let dec = cks.decrypt(&ct);
|
||||
/// let modulus = cks.parameters.message_modulus().0 as u64;
|
||||
/// assert_eq!(msg >> shift, dec);
|
||||
/// ```
|
||||
pub fn scalar_right_shift_assign(&self, ct: &mut Ciphertext, shift: u8) {
|
||||
@@ -179,7 +175,6 @@ impl ServerKey {
|
||||
///
|
||||
/// // Decrypt:
|
||||
/// let dec = cks.decrypt(&ct_res);
|
||||
/// let modulus = cks.parameters.message_modulus().0 as u64;
|
||||
/// assert_eq!(msg >> shift, dec);
|
||||
///
|
||||
/// let (cks, sks) = gen_keys(PARAM_MESSAGE_2_CARRY_2_PBS_KS);
|
||||
@@ -201,7 +196,6 @@ impl ServerKey {
|
||||
///
|
||||
/// // Decrypt:
|
||||
/// let dec = cks.decrypt(&ct_res);
|
||||
/// let modulus = cks.parameters.message_modulus().0 as u64;
|
||||
/// assert_eq!(msg >> shift, dec);
|
||||
/// ```
|
||||
pub fn unchecked_scalar_right_shift(&self, ct: &Ciphertext, shift: u8) -> Ciphertext {
|
||||
@@ -240,7 +234,6 @@ impl ServerKey {
|
||||
///
|
||||
/// // Decrypt:
|
||||
/// let dec = cks.decrypt(&ct);
|
||||
/// let modulus = cks.parameters.message_modulus().0 as u64;
|
||||
/// assert_eq!(msg >> shift, dec);
|
||||
///
|
||||
/// let (cks, sks) = gen_keys(PARAM_MESSAGE_2_CARRY_2_PBS_KS);
|
||||
@@ -262,7 +255,6 @@ impl ServerKey {
|
||||
///
|
||||
/// // Decrypt:
|
||||
/// let dec = cks.decrypt(&ct);
|
||||
/// let modulus = cks.parameters.message_modulus().0 as u64;
|
||||
/// assert_eq!(msg >> shift, dec);
|
||||
/// ```
|
||||
pub fn unchecked_scalar_right_shift_assign(&self, ct: &mut Ciphertext, shift: u8) {
|
||||
@@ -309,10 +301,10 @@ impl ServerKey {
|
||||
/// // | 0 0 | 1 0 |
|
||||
///
|
||||
/// // Decrypt:
|
||||
/// let msg_only = cks.decrypt(&ct_res);
|
||||
/// let res = cks.decrypt(&ct_res);
|
||||
/// let modulus = cks.parameters.message_modulus().0 as u64;
|
||||
///
|
||||
/// assert_eq!((msg << shift) % modulus, msg_only);
|
||||
/// assert_eq!((msg << shift) % modulus, res);
|
||||
///
|
||||
/// let (cks, sks) = gen_keys(PARAM_MESSAGE_2_CARRY_2_PBS_KS);
|
||||
///
|
||||
@@ -331,10 +323,9 @@ impl ServerKey {
|
||||
/// // | 0 0 | 1 0 |
|
||||
///
|
||||
/// // Decrypt:
|
||||
/// let msg_only = cks.decrypt(&ct_res);
|
||||
/// let modulus = cks.parameters.message_modulus().0 as u64;
|
||||
/// let res = cks.decrypt(&ct_res);
|
||||
///
|
||||
/// assert_eq!((msg << shift) % modulus, msg_only);
|
||||
/// assert_eq!((msg << shift) % modulus, res);
|
||||
/// ```
|
||||
pub fn scalar_left_shift(&self, ct: &Ciphertext, shift: u8) -> Ciphertext {
|
||||
let mut ct_res = ct.clone();
|
||||
@@ -380,10 +371,10 @@ impl ServerKey {
|
||||
/// // | 0 0 | 1 0 |
|
||||
///
|
||||
/// // Decrypt:
|
||||
/// let msg_only = cks.decrypt(&ct);
|
||||
/// let res = cks.decrypt(&ct);
|
||||
/// let modulus = cks.parameters.message_modulus().0 as u64;
|
||||
///
|
||||
/// assert_eq!((msg << shift) % modulus, msg_only);
|
||||
/// assert_eq!((msg << shift) % modulus, res);
|
||||
///
|
||||
/// let (cks, sks) = gen_keys(PARAM_MESSAGE_2_CARRY_2_PBS_KS);
|
||||
///
|
||||
@@ -402,10 +393,9 @@ impl ServerKey {
|
||||
/// // | 0 0 | 1 0 |
|
||||
///
|
||||
/// // Decrypt:
|
||||
/// let msg_only = cks.decrypt(&ct);
|
||||
/// let modulus = cks.parameters.message_modulus().0 as u64;
|
||||
/// let res = cks.decrypt(&ct);
|
||||
///
|
||||
/// assert_eq!((msg << shift) % modulus, msg_only);
|
||||
/// assert_eq!((msg << shift) % modulus, res);
|
||||
/// ```
|
||||
pub fn scalar_left_shift_assign(&self, ct: &mut Ciphertext, shift: u8) {
|
||||
let modulus = self.message_modulus.0 as u64;
|
||||
@@ -444,12 +434,11 @@ impl ServerKey {
|
||||
/// // | 0 1 | 0 0 |
|
||||
///
|
||||
/// // Decrypt:
|
||||
/// let msg_and_carry = cks.decrypt_message_and_carry(&ct_res);
|
||||
/// let msg_only = cks.decrypt(&ct_res);
|
||||
/// let res = cks.decrypt_decode_padding(&ct_res);
|
||||
/// let modulus = cks.parameters.message_modulus().0 as u64;
|
||||
///
|
||||
/// assert_eq!(msg << shift, msg_and_carry);
|
||||
/// assert_eq!((msg << shift) % modulus, msg_only);
|
||||
/// assert_eq!(msg << shift, res.carry_and_msg);
|
||||
/// assert_eq!((msg << shift) % modulus, res.msg);
|
||||
///
|
||||
/// let (cks, sks) = gen_keys(PARAM_MESSAGE_2_CARRY_2_PBS_KS);
|
||||
///
|
||||
@@ -469,12 +458,11 @@ impl ServerKey {
|
||||
/// // | 0 1 | 0 0 |
|
||||
///
|
||||
/// // Decrypt:
|
||||
/// let msg_and_carry = cks.decrypt_message_and_carry(&ct_res);
|
||||
/// let msg_only = cks.decrypt(&ct_res);
|
||||
/// let res = cks.decrypt_decode_padding(&ct_res);
|
||||
/// let modulus = cks.parameters.message_modulus().0 as u64;
|
||||
///
|
||||
/// assert_eq!(msg << shift, msg_and_carry);
|
||||
/// assert_eq!((msg << shift) % modulus, msg_only);
|
||||
/// assert_eq!(msg << shift, res.carry_and_msg);
|
||||
/// assert_eq!((msg << shift) % modulus, res.msg);
|
||||
/// ```
|
||||
pub fn unchecked_scalar_left_shift(&self, ct: &Ciphertext, shift: u8) -> Ciphertext {
|
||||
ShortintEngine::with_thread_local_mut(|engine| {
|
||||
@@ -511,12 +499,11 @@ impl ServerKey {
|
||||
/// // | 0 1 | 0 0 |
|
||||
///
|
||||
/// // Decrypt:
|
||||
/// let msg_and_carry = cks.decrypt_message_and_carry(&ct);
|
||||
/// let msg_only = cks.decrypt(&ct);
|
||||
/// let res = cks.decrypt_decode_padding(&ct);
|
||||
/// let modulus = cks.parameters.message_modulus().0 as u64;
|
||||
///
|
||||
/// assert_eq!(msg << shift, msg_and_carry);
|
||||
/// assert_eq!((msg << shift) % modulus, msg_only);
|
||||
/// assert_eq!(msg << shift, res.carry_and_msg);
|
||||
/// assert_eq!((msg << shift) % modulus, res.msg);
|
||||
///
|
||||
/// let (cks, sks) = gen_keys(PARAM_MESSAGE_2_CARRY_2_PBS_KS);
|
||||
///
|
||||
@@ -536,12 +523,10 @@ impl ServerKey {
|
||||
/// // | 0 1 | 0 0 |
|
||||
///
|
||||
/// // Decrypt:
|
||||
/// let msg_and_carry = cks.decrypt_message_and_carry(&ct);
|
||||
/// let msg_only = cks.decrypt(&ct);
|
||||
/// let modulus = cks.parameters.message_modulus().0 as u64;
|
||||
/// let res = cks.decrypt_decode_padding(&ct);
|
||||
///
|
||||
/// assert_eq!(msg << shift, msg_and_carry);
|
||||
/// assert_eq!((msg << shift) % modulus, msg_only);
|
||||
/// assert_eq!(msg << shift, res.carry_and_msg);
|
||||
/// assert_eq!((msg << shift) % modulus, res.msg);
|
||||
/// ```
|
||||
pub fn unchecked_scalar_left_shift_assign(&self, ct: &mut Ciphertext, shift: u8) {
|
||||
ShortintEngine::with_thread_local_mut(|engine| {
|
||||
@@ -628,12 +613,11 @@ impl ServerKey {
|
||||
/// // | 1 0 | 0 0 |
|
||||
///
|
||||
/// // Decrypt:
|
||||
/// let msg_and_carry = cks.decrypt_message_and_carry(&ct_res);
|
||||
/// let msg_only = cks.decrypt(&ct_res);
|
||||
/// let res = cks.decrypt_decode_padding(&ct_res);
|
||||
/// let modulus = cks.parameters.message_modulus().0 as u64;
|
||||
///
|
||||
/// assert_eq!(msg << shift, msg_and_carry);
|
||||
/// assert_eq!((msg << shift) % modulus, msg_only);
|
||||
/// assert_eq!(msg << shift, res.carry_and_msg);
|
||||
/// assert_eq!((msg << shift) % modulus, res.msg);
|
||||
///
|
||||
/// let (cks, sks) = gen_keys(PARAM_MESSAGE_2_CARRY_2_PBS_KS);
|
||||
///
|
||||
@@ -659,12 +643,10 @@ impl ServerKey {
|
||||
/// // | 1 0 | 0 0 |
|
||||
///
|
||||
/// // Decrypt:
|
||||
/// let msg_and_carry = cks.decrypt_message_and_carry(&ct_res);
|
||||
/// let msg_only = cks.decrypt(&ct_res);
|
||||
/// let modulus = cks.parameters.message_modulus().0 as u64;
|
||||
/// let res = cks.decrypt_decode_padding(&ct_res);
|
||||
///
|
||||
/// assert_eq!(msg << shift, msg_and_carry);
|
||||
/// assert_eq!((msg << shift) % modulus, msg_only);
|
||||
/// assert_eq!(msg << shift, res.carry_and_msg);
|
||||
/// assert_eq!((msg << shift) % modulus, res.msg);
|
||||
/// ```
|
||||
pub fn checked_scalar_left_shift(
|
||||
&self,
|
||||
@@ -723,12 +705,10 @@ impl ServerKey {
|
||||
/// // | 0 1 | 0 0 |
|
||||
///
|
||||
/// // Decrypt:
|
||||
/// let msg_and_carry = cks.decrypt_message_and_carry(&ct_res);
|
||||
/// let msg_only = cks.decrypt(&ct_res);
|
||||
/// let res = cks.decrypt_decode_padding(&ct_res);
|
||||
/// let modulus = cks.parameters.message_modulus().0 as u64;
|
||||
///
|
||||
/// assert_eq!(msg << shift, msg_and_carry);
|
||||
/// assert_eq!((msg << shift) % modulus, msg_only);
|
||||
/// assert_eq!(msg << shift, res.carry_and_msg);
|
||||
/// assert_eq!((msg << shift) % modulus, res.msg);
|
||||
///
|
||||
/// let (cks, sks) = gen_keys(PARAM_MESSAGE_2_CARRY_2_PBS_KS);
|
||||
///
|
||||
@@ -747,12 +727,10 @@ impl ServerKey {
|
||||
/// // | 0 1 | 0 0 |
|
||||
///
|
||||
/// // Decrypt:
|
||||
/// let msg_and_carry = cks.decrypt_message_and_carry(&ct_res);
|
||||
/// let msg_only = cks.decrypt(&ct_res);
|
||||
/// let modulus = cks.parameters.message_modulus().0 as u64;
|
||||
/// let res = cks.decrypt_decode_padding(&ct_res);
|
||||
///
|
||||
/// assert_eq!(msg << shift, msg_and_carry);
|
||||
/// assert_eq!((msg << shift) % modulus, msg_only);
|
||||
/// assert_eq!(msg << shift, res.carry_and_msg);
|
||||
/// assert_eq!((msg << shift) % modulus, res.msg);
|
||||
/// ```
|
||||
pub fn smart_scalar_left_shift(&self, ct: &mut Ciphertext, shift: u8) -> Ciphertext {
|
||||
ShortintEngine::with_thread_local_mut(|engine| {
|
||||
|
||||
@@ -34,8 +34,7 @@ impl ServerKey {
|
||||
/// let ct_res = sks.sub(&mut ct_1, &ct_2);
|
||||
///
|
||||
/// let clear_res = cks.decrypt(&ct_res);
|
||||
/// let modulus = cks.parameters.message_modulus().0 as u64;
|
||||
/// assert_eq!(clear_res % modulus, 2);
|
||||
/// assert_eq!(clear_res, 2);
|
||||
/// ```
|
||||
pub fn sub(&self, ct_left: &Ciphertext, ct_right: &Ciphertext) -> Ciphertext {
|
||||
let mut ct_res = ct_left.clone();
|
||||
@@ -70,8 +69,7 @@ impl ServerKey {
|
||||
///
|
||||
/// // Compute homomorphically a subtraction:
|
||||
/// sks.sub_assign(&mut ct_1, &ct_2);
|
||||
/// let modulus = cks.parameters.message_modulus().0 as u64;
|
||||
/// assert_eq!(cks.decrypt(&ct_1) % modulus, 2);
|
||||
/// assert_eq!(cks.decrypt(&ct_1), 2);
|
||||
/// ```
|
||||
pub fn sub_assign(&self, ct_left: &mut Ciphertext, ct_right: &Ciphertext) {
|
||||
let tmp_rhs: Ciphertext;
|
||||
@@ -115,7 +113,6 @@ impl ServerKey {
|
||||
/// let ct_res = sks.unchecked_sub(&ct_1, &ct_2);
|
||||
///
|
||||
/// // Decrypt:
|
||||
/// let modulus = cks.parameters.message_modulus().0 as u64;
|
||||
/// assert_eq!(cks.decrypt(&ct_res), 2 - 1);
|
||||
/// ```
|
||||
pub fn unchecked_sub(&self, ct_left: &Ciphertext, ct_right: &Ciphertext) -> Ciphertext {
|
||||
@@ -148,8 +145,7 @@ impl ServerKey {
|
||||
/// sks.unchecked_sub_assign(&mut ct_1, &ct_2);
|
||||
///
|
||||
/// // Decrypt:
|
||||
/// let modulus = cks.parameters.message_modulus().0 as u64;
|
||||
/// assert_eq!(cks.decrypt(&ct_1) % modulus, 1);
|
||||
/// assert_eq!(cks.decrypt(&ct_1), 1);
|
||||
/// ```
|
||||
pub fn unchecked_sub_assign(&self, ct_left: &mut Ciphertext, ct_right: &Ciphertext) {
|
||||
ShortintEngine::with_thread_local_mut(|engine| {
|
||||
@@ -212,9 +208,8 @@ impl ServerKey {
|
||||
/// let ct_res = sks.checked_sub(&ct_1, &ct_2);
|
||||
///
|
||||
/// assert!(ct_res.is_ok());
|
||||
/// let modulus = cks.parameters.message_modulus().0 as u64;
|
||||
/// let clear_res = cks.decrypt(&ct_res.unwrap());
|
||||
/// assert_eq!(clear_res % modulus, 2);
|
||||
/// assert_eq!(clear_res, 2);
|
||||
/// ```
|
||||
pub fn checked_sub(
|
||||
&self,
|
||||
@@ -252,9 +247,8 @@ impl ServerKey {
|
||||
/// let res = sks.checked_sub_assign(&mut ct_1, &ct_2);
|
||||
///
|
||||
/// assert!(res.is_ok());
|
||||
/// let modulus = cks.parameters.message_modulus().0 as u64;
|
||||
/// let clear_res = cks.decrypt(&ct_1);
|
||||
/// assert_eq!(clear_res % modulus, 2);
|
||||
/// assert_eq!(clear_res, 2);
|
||||
/// ```
|
||||
pub fn checked_sub_assign(
|
||||
&self,
|
||||
@@ -292,8 +286,7 @@ impl ServerKey {
|
||||
/// let ct_res = sks.smart_sub(&mut ct_1, &mut ct_2);
|
||||
///
|
||||
/// let clear_res = cks.decrypt(&ct_res);
|
||||
/// let modulus = cks.parameters.message_modulus().0 as u64;
|
||||
/// assert_eq!(clear_res % modulus, 2);
|
||||
/// assert_eq!(clear_res, 2);
|
||||
/// ```
|
||||
pub fn smart_sub(&self, ct_left: &mut Ciphertext, ct_right: &mut Ciphertext) -> Ciphertext {
|
||||
ShortintEngine::with_thread_local_mut(|engine| engine.smart_sub(self, ct_left, ct_right))
|
||||
@@ -319,8 +312,7 @@ impl ServerKey {
|
||||
///
|
||||
/// // Compute homomorphically a subtraction:
|
||||
/// sks.smart_sub_assign(&mut ct_1, &mut ct_2);
|
||||
/// let modulus = cks.parameters.message_modulus().0 as u64;
|
||||
/// assert_eq!(cks.decrypt(&ct_1) % modulus, 2);
|
||||
/// assert_eq!(cks.decrypt(&ct_1), 2);
|
||||
/// ```
|
||||
pub fn smart_sub_assign(&self, ct_left: &mut Ciphertext, ct_right: &mut Ciphertext) {
|
||||
ShortintEngine::with_thread_local_mut(|engine| {
|
||||
|
||||
@@ -353,10 +353,10 @@ where
|
||||
let ct = cks.encrypt_without_padding(clear);
|
||||
|
||||
// decryption of ct_zero
|
||||
let dec = cks.decrypt_message_and_carry_without_padding(&ct);
|
||||
let dec = cks.decrypt_decode_without_padding(&ct);
|
||||
|
||||
// assert
|
||||
assert_eq!(clear, dec);
|
||||
assert_eq!(clear, dec.carry_and_msg);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -389,7 +389,7 @@ where
|
||||
failures += 1;
|
||||
}
|
||||
// assert
|
||||
// assert_eq!(clear_0, dec_res);
|
||||
// assert_eq!(clear_0, dec_res.msg);
|
||||
}
|
||||
|
||||
println!("fail_rate = {failures}/{NB_TEST}");
|
||||
@@ -482,14 +482,15 @@ where
|
||||
let ct_carry = sks.carry_extract(&ctxt);
|
||||
|
||||
// decryption of message and carry
|
||||
let dec = cks.decrypt_message_and_carry(&ct_carry);
|
||||
let dec = cks.decrypt_decode_padding(&ct_carry);
|
||||
|
||||
// assert
|
||||
println!(
|
||||
"msg = {clear}, modulus = {msg_modulus}, msg/modulus = {}",
|
||||
clear / msg_modulus
|
||||
);
|
||||
assert_eq!(clear / msg_modulus, dec);
|
||||
assert_eq!(clear / msg_modulus, dec.msg);
|
||||
assert_eq!(dec.carry, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2708,7 +2709,7 @@ where
|
||||
|
||||
let mut clear = (clear_0 - clear_1) % modulus;
|
||||
|
||||
// let dec_res = cks.decrypt(&ct_res);
|
||||
// let dec_res = cks.decrypt_decode_padding(&ct_res);
|
||||
// println!("clear_0 = {}, clear_1 = {}, dec = {}, clear = {}", clear_0, clear_1, dec_res,
|
||||
// clear);
|
||||
|
||||
@@ -3010,7 +3011,7 @@ where
|
||||
|
||||
// Check the correctness
|
||||
let clear_result = (clear1 - clear2) % modulus;
|
||||
assert_eq!(clear_result, dec % modulus);
|
||||
assert_eq!(clear_result, dec);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3108,7 +3109,7 @@ where
|
||||
let dec_res = cks.decrypt(&ct_res);
|
||||
|
||||
// assert
|
||||
assert_eq!((clear_0 * clear_1) % modulus, dec_res % modulus);
|
||||
assert_eq!((clear_0 * clear_1) % modulus, dec_res);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3137,14 +3138,16 @@ where
|
||||
|
||||
println!("MUL SMALL CARRY:: clear1 = {clear1}, clear2 = {clear2}, mod = {modulus}");
|
||||
let ct_res = sks.unchecked_mul_lsb_small_carry(&ct1, &ct2);
|
||||
assert_eq!(
|
||||
(clear1 * clear2) % modulus,
|
||||
cks.decrypt_message_and_carry(&ct_res) % modulus
|
||||
);
|
||||
|
||||
let res = cks.decrypt_decode_padding(&ct_res);
|
||||
assert_eq!(clear1 * clear2, res.carry_and_msg);
|
||||
|
||||
println!("ADD:: clear1 = {clear1}, clear2 = {clear2}, mod = {modulus}");
|
||||
let ct_res = sks.unchecked_add(&ct1, &ct2);
|
||||
assert_eq!((clear1 + clear2), cks.decrypt_message_and_carry(&ct_res));
|
||||
|
||||
let res = cks.decrypt_decode_padding(&ct_res);
|
||||
|
||||
assert_eq!((clear1 + clear2), res.carry_and_msg);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3183,17 +3186,11 @@ where
|
||||
|
||||
println!("MUL SMALL CARRY:: clear1 = {clear1}, clear2 = {clear2}, msg_mod = {msg_modulus}, carry_mod = {carry_modulus}");
|
||||
let ct_res = sks.unchecked_mul_lsb_small_carry(&ct1, &ct2);
|
||||
assert_eq!(
|
||||
(clear1 * clear2) % msg_modulus,
|
||||
cks.decrypt_message_and_carry(&ct_res) % msg_modulus
|
||||
);
|
||||
assert_eq!((clear1 * clear2) % msg_modulus, cks.decrypt(&ct_res));
|
||||
|
||||
println!("ADD:: clear1 = {clear1}, clear2 = {clear2}, msg_mod = {msg_modulus}, carry_mod = {carry_modulus}");
|
||||
let ct_res = sks.unchecked_add(&ct1, &ct2);
|
||||
assert_eq!(
|
||||
(clear1 + clear2) % modulus,
|
||||
cks.decrypt_message_and_carry(&ct_res) % modulus
|
||||
);
|
||||
assert_eq!((clear1 + clear2) % modulus, cks.decrypt(&ct_res));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -279,8 +279,8 @@ impl WopbsKey {
|
||||
/// let ct = cks.encrypt_without_padding(m);
|
||||
/// let lut = wopbs_key.generate_lut(&ct, |x| x * x % message_modulus);
|
||||
/// let ct_res = wopbs_key.programmable_bootstrapping_without_padding(&ct, &lut);
|
||||
/// let res = cks.decrypt_without_padding(&ct_res);
|
||||
/// assert_eq!(res, (m * m) % message_modulus);
|
||||
/// let res = cks.decrypt_decode_without_padding(&ct_res);
|
||||
/// assert_eq!(res.msg, (m * m) % message_modulus);
|
||||
/// ```
|
||||
pub fn generate_lut_without_padding<F>(&self, ct: &Ciphertext, f: F) -> Vec<u64>
|
||||
where
|
||||
@@ -357,7 +357,7 @@ impl WopbsKey {
|
||||
/// let ct = cks.encrypt(rng.gen::<u64>() % message_modulus as u64);
|
||||
/// let lut = vec![(1_u64 << 59); wopbs_key.param.polynomial_size.0].into();
|
||||
/// let ct_res = wopbs_key.programmable_bootstrapping(&sks, &ct, &lut);
|
||||
/// let res = cks.decrypt_message_and_carry(&ct_res);
|
||||
/// let res = cks.decrypt(&ct_res);
|
||||
/// assert_eq!(res, 1);
|
||||
/// ```
|
||||
pub fn programmable_bootstrapping(
|
||||
@@ -394,7 +394,7 @@ impl WopbsKey {
|
||||
/// let ct = cks.encrypt(rng.gen::<u64>() % message_modulus as u64);
|
||||
/// let lut = vec![(1_u64 << 59); wopbs_key.param.polynomial_size.0].into();
|
||||
/// let ct_res = wopbs_key.wopbs(&ct, &lut);
|
||||
/// let res = cks.decrypt_message_and_carry(&ct_res);
|
||||
/// let res = cks.decrypt(&ct_res);
|
||||
/// assert_eq!(res, 1);
|
||||
/// ```
|
||||
pub fn wopbs(&self, ct_in: &Ciphertext, lut: &ShortintWopbsLUT) -> Ciphertext {
|
||||
@@ -419,8 +419,8 @@ impl WopbsKey {
|
||||
/// let ct = cks.encrypt_without_padding(rng.gen::<u64>() % 2);
|
||||
/// let lut = vec![(1_u64 << 63); wopbs_key.param.polynomial_size.0].into();
|
||||
/// let ct_res = wopbs_key.programmable_bootstrapping_without_padding(&ct, &lut);
|
||||
/// let res = cks.decrypt_message_and_carry_without_padding(&ct_res);
|
||||
/// assert_eq!(res, 1);
|
||||
/// let res = cks.decrypt_decode_without_padding(&ct_res);
|
||||
/// assert_eq!(res.msg, 1);
|
||||
/// ```
|
||||
pub fn programmable_bootstrapping_without_padding(
|
||||
&self,
|
||||
|
||||
Reference in New Issue
Block a user