Struct bfv12::Ciphertext
source · [−]pub struct Ciphertext { /* private fields */ }
Expand description
A BFV12 Ciphertext
c_0
=[p_0 * u + e_1 + delta * m]_q
c_1
=[p_1 * u + e_2]_q
q
= the ciphertext modulust
= the plaintext modulus
Implementations
sourceimpl Ciphertext
impl Ciphertext
sourcepub fn decrypt(&self, secret_key: &SecretKey) -> Plaintext
pub fn decrypt(&self, secret_key: &SecretKey) -> Plaintext
Decrypt a ciphertext to recover a plaintext, given a secret key
use bfv12::{Plaintext, SecretKey};
let pt = Plaintext::new(vec![0, 1, 2, 3], t);
let secret_key = SecretKey::generate(degree, &mut rng);
let public_key = secret_key.public_key_gen(q, std_dev, &mut rng);
let ct = pt.encrypt(&public_key, std_dev, &mut rng);
let decrypted = ct.decrypt(&secret_key);
assert_eq!(decrypted, pt);
Trait Implementations
sourceimpl Add<Ciphertext> for Ciphertext
impl Add<Ciphertext> for Ciphertext
Add two ciphertexts. They can be of different degrees.
let secret_key = SecretKey::generate(degree, &mut rng);
let public_key = secret_key.public_key_gen(q, std_dev, &mut rng);
let pt_1 = Plaintext::rand(degree, t, &mut rng);
let pt_2 = Plaintext::rand(degree, t, &mut rng);
let ct_1 = pt_1.encrypt(&public_key, std_dev, &mut rng);
let ct_2 = pt_2.encrypt(&public_key, std_dev, &mut rng);
// Add the ciphertexts: ct_1 + ct_2
let add_ct = ct_1 + ct_2;
// Decrypt the result of the addition
let add_pt = add_ct.decrypt(&secret_key);
// Compare the expected output to the decrypted output
let expected_pt = (pt_1.poly() + pt_2.poly()) % (t, degree);
assert_eq!(add_pt.poly(), expected_pt)
sourceimpl Clone for Ciphertext
impl Clone for Ciphertext
sourcefn clone(&self) -> Ciphertext
fn clone(&self) -> Ciphertext
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl Debug for Ciphertext
impl Debug for Ciphertext
sourceimpl Mul<(Ciphertext, &'_ RelinearizationKey1)> for Ciphertext
impl Mul<(Ciphertext, &'_ RelinearizationKey1)> for Ciphertext
Multiply two ciphertexts, using Relinearization Version 1. Since multiplication requires a relinearization key, you must multiply a ciphertext with a tuple of (Ciphertext, &RelinearizationKey1). The type of the relinearization key determines whether the multiplication uses Relinearization Version 1 or 2.
let secret_key = SecretKey::generate(degree, &mut rng);
let public_key = secret_key.public_key_gen(q, std_dev, &mut rng);
let rlk_1 = secret_key.relin_key_gen_1(q, std_dev, &mut rng, rlk_base);
let pt_1 = Plaintext::rand(degree, t, &mut rng);
let pt_2 = Plaintext::rand(degree, t, &mut rng);
let ct_1 = pt_1.encrypt(&public_key, std_dev, &mut rng);
let ct_2 = pt_2.encrypt(&public_key, std_dev, &mut rng);
// Multiply the ciphertexts: ct_1 * ct_2
let mul_ct = ct_1 * (ct_2, &rlk_1);
// Decrypt the result of the multiplication
let mul_pt = mul_ct.decrypt(&secret_key);
// Compare the expected output to the decrypted output
let expected_pt = (pt_1.poly() * pt_2.poly()) % (t, degree);
assert_eq!(mul_pt.poly(), expected_pt)
type Output = Self
type Output = Self
The resulting type after applying the *
operator.
sourcefn mul(self, other: (Ciphertext, &RelinearizationKey1)) -> Self::Output
fn mul(self, other: (Ciphertext, &RelinearizationKey1)) -> Self::Output
Performs the *
operation. Read more
sourceimpl Mul<(Ciphertext, &'_ RelinearizationKey2)> for Ciphertext
impl Mul<(Ciphertext, &'_ RelinearizationKey2)> for Ciphertext
Multiply two ciphertexts, using Relinearization Version 2. Since multiplication requires a relinearization key, you must multiply a ciphertext with a tuple of (Ciphertext, &RelinearizationKey2). The type of the relinearization key determines whether the multiplication uses Relinearization Version 1 or 2.
let secret_key = SecretKey::generate(degree, &mut rng);
let public_key = secret_key.public_key_gen(q, std_dev, &mut rng);
// p = the amount to scale the modulus, during modulus switching
// Technically p should be >= q^3 for security (see paper discussion on Relinearization Version 2),
// but setting p = q^3 results in an overflow when taking p * q so we will test with a smaller p.
let p = 2_i64.pow(13) * q;
let rlk_2 = secret_key.relin_key_gen_2(q, std_dev, &mut rng, p);
let pt_1 = Plaintext::rand(degree, t, &mut rng);
let pt_2 = Plaintext::rand(degree, t, &mut rng);
let ct_1 = pt_1.encrypt(&public_key, std_dev, &mut rng);
let ct_2 = pt_2.encrypt(&public_key, std_dev, &mut rng);
// Multiply the ciphertexts: ct_1 * ct_2
let mul_ct = ct_1 * (ct_2, &rlk_2);
// Decrypt the result of the multiplication
let mul_pt = mul_ct.decrypt(&secret_key);
// Compare the expected output to the decrypted output
let expected_pt = (pt_1.poly() * pt_2.poly()) % (t, degree);
assert_eq!(mul_pt.poly(), expected_pt)
type Output = Self
type Output = Self
The resulting type after applying the *
operator.
sourcefn mul(self, other: (Ciphertext, &RelinearizationKey2)) -> Self::Output
fn mul(self, other: (Ciphertext, &RelinearizationKey2)) -> Self::Output
Performs the *
operation. Read more
sourceimpl Neg for Ciphertext
impl Neg for Ciphertext
Take the negation of a ciphertext.
let secret_key = SecretKey::generate(degree, &mut rng);
let public_key = secret_key.public_key_gen(q, std_dev, &mut rng);
let pt = Plaintext::rand(degree, t, &mut rng);
let ct = pt.encrypt(&public_key, std_dev, &mut rng);
// Negate: -ct
let neg_ct = -ct;
// Decrypt the result of the negation
let neg_pt = neg_ct.decrypt(&secret_key);
// Compare the expected output to the decrypted output
let expected_pt = -pt.poly() % (t, degree);
assert_eq!(neg_pt.poly(), expected_pt)
sourceimpl Sub<Ciphertext> for Ciphertext
impl Sub<Ciphertext> for Ciphertext
Subtract one ciphertext from another. They can be of different degrees.
let secret_key = SecretKey::generate(degree, &mut rng);
let public_key = secret_key.public_key_gen(q, std_dev, &mut rng);
let pt_1 = Plaintext::rand(degree, t, &mut rng);
let pt_2 = Plaintext::rand(degree, t, &mut rng);
let ct_1 = pt_1.encrypt(&public_key, std_dev, &mut rng);
let ct_2 = pt_2.encrypt(&public_key, std_dev, &mut rng);
// Subtract: ct_1 - ct_2
let sub_ct = ct_1 - ct_2;
// Decrypt the result of the subtraction
let sub_pt = sub_ct.decrypt(&secret_key);
// Compare the expected output to the decrypted output
let expected_pt = (pt_1.poly() - pt_2.poly()) % (t, degree);
assert_eq!(sub_pt.poly(), expected_pt)
Auto Trait Implementations
impl RefUnwindSafe for Ciphertext
impl Send for Ciphertext
impl Sync for Ciphertext
impl Unpin for Ciphertext
impl UnwindSafe for Ciphertext
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more