1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use super::ciphertext::Ciphertext;
use super::keys::PublicKey;
use super::poly::Poly;
use super::random_source;
use rand::{CryptoRng, RngCore};
/// A BFV12 Plaintext (an encoded message)
#[derive(Debug, PartialEq)]
pub struct Plaintext {
/// The polynomial representing the list of plaintext values
poly: Poly,
/// The modulus of the plaintext space
t: i64,
}
impl Plaintext {
/// Instantiate a new Plaintext
///
/// * `poly`: the polynomial set to the input vector
/// * `t`: the plaintext modulus
///
/// ```rust
/// use bfv12::Plaintext;
/// let pt = Plaintext::new(vec![0, 1, 2, 3], 4);
/// ```
pub fn new(val: Vec<i64>, t: i64) -> Plaintext {
Plaintext::new_from_poly(Poly::new(val), t)
}
pub(crate) fn new_from_poly(poly: Poly, t: i64) -> Plaintext {
// The plaintext space is taken as R_t for some integer t > 1.
assert!(t > 1);
Plaintext { poly, t }
}
/// Instantiate a new random Plaintext uniformly over [0, t) with length `degree`
///
/// * `degree`: the degree (length) of the newly generated plaintext
/// * `t`: the plaintext modulus
/// # `rng`: the RNG used to generate randomness. Any Rng that imlements RngCore + CryptoRng can be used.
///
/// ```rust
/// # use rand::SeedableRng;
/// # let mut rng = rand::rngs::StdRng::seed_from_u64(18);
/// #
/// use bfv12::Plaintext;
/// let rand_pt = Plaintext::rand(10, 4, &mut rng);
/// ```
pub fn rand<T: RngCore + CryptoRng>(degree: usize, t: i64, rng: &mut T) -> Plaintext {
assert!(t > 1);
Plaintext {
poly: random_source::get_uniform(t, degree, rng),
t,
}
}
/// Return the polynomial that represents the encoded messeage
///
/// ```rust
/// use bfv12::Plaintext;
/// let pt = Plaintext::new(vec![0, 1, 2, 3], 4);
/// let pt_poly = pt.poly();
/// assert_eq!(pt_poly.val(), vec![0, 1, 2, 3])
/// ```
pub fn poly(&self) -> Poly {
self.poly.clone()
}
/// Encrypt a plaintext with a given public key
///
/// * `pub_key`: the public key used to encrypt plaintext
/// * `std_dev`: the standard deviation used for generating the error in the encryption
/// # `rng`: the RNG used to generate randomness. Any Rng that imlements RngCore + CryptoRng can be used.
///
/// ```rust
/// # use rand::SeedableRng;
/// # let mut rng = rand::rngs::StdRng::seed_from_u64(18);
/// # let std_dev = 3.2;
/// # let degree = 4;
/// # let q = 65536;
/// # let t = 4;
/// #
/// 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);
/// ```
pub fn encrypt<T: RngCore + CryptoRng>(
&self,
pub_key: &PublicKey,
std_dev: f64,
rng: &mut T,
) -> Ciphertext {
assert_eq!(self.poly.degree(), pub_key.p_0.degree());
let q = pub_key.q;
let degree = self.poly.degree();
let m = self.poly.clone();
let u = random_source::get_uniform(2, degree, rng);
let e_1 = random_source::get_gaussian(std_dev, degree, rng);
let e_2 = random_source::get_gaussian(std_dev, degree, rng);
let delta = (q as f64 / self.t as f64).floor() as i64;
let c_0 = (pub_key.p_0.clone() * u.clone() + e_1 + m * delta) % (q, degree);
let c_1 = (pub_key.p_1.clone() * u + e_2) % (q, degree);
Ciphertext {
c_0,
c_1,
q,
t: self.t,
}
}
}