Skip to content

Commit

Permalink
fix lwe ciphertext reuse bug
Browse files Browse the repository at this point in the history
  • Loading branch information
rickwebiii committed Apr 4, 2024
1 parent f2000c0 commit 5abe4e3
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion sunscreen_tfhe/src/ops/encryption/lwe_encryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ where
params.assert_valid();

let (a, b) = ct.a_b_mut(params);
*b = Torus::zero();

for (a_i, d_i) in a.iter_mut().zip(sk.as_slice().iter()) {
*a_i = uniform_torus::<S>();
Expand Down Expand Up @@ -76,7 +77,7 @@ where
#[cfg(test)]
mod tests {

use crate::{high_level::*, PlaintextBits};
use crate::{high_level::*, ops::encryption::encode_and_encrypt_lwe_ciphertext, PlaintextBits};

#[test]
fn can_encrypt_decrypt() {
Expand All @@ -91,6 +92,26 @@ mod tests {
assert_eq!(pt, 4);
}

#[test]
fn can_reuse_lwe_ciphertext() {
let params = TEST_LWE_DEF_1;
let bits = PlaintextBits(4);

let sk = keygen::generate_binary_lwe_sk(&params);

for _ in 0..10 {
let mut ct = encryption::encrypt_lwe_secret(4, &sk, &params, bits);
let pt = encryption::decrypt_lwe(&ct, &sk, &params, bits);

assert_eq!(pt, 4);

encode_and_encrypt_lwe_ciphertext(&mut ct, &sk, 3, &params, bits);

assert_eq!(encryption::decrypt_lwe(&ct, &sk, &params, bits), 3);

}
}

#[test]
fn can_encrypt_decrypt_uniform() {
let params = TEST_LWE_DEF_1;
Expand Down

0 comments on commit 5abe4e3

Please sign in to comment.