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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
use crate::encryption::GroupEncryptionParameters;
use snarkos_errors::algorithms::EncryptionError;
use snarkos_models::{
algorithms::EncryptionScheme,
curves::{AffineCurve, Field, Group, One, PrimeField, ProjectiveCurve, Zero},
};
use snarkos_utilities::{bytes_to_bits, rand::UniformRand, to_bytes, FromBytes, ToBytes};
use itertools::Itertools;
use rand::Rng;
use std::io::{Read, Result as IoResult, Write};
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct GroupEncryptionPublicKey<G: Group + ProjectiveCurve>(pub G);
impl<G: Group + ProjectiveCurve> ToBytes for GroupEncryptionPublicKey<G> {
#[inline]
fn write<W: Write>(&self, mut writer: W) -> IoResult<()> {
let affine = self.0.into_affine();
let x_coordinate = affine.to_x_coordinate();
x_coordinate.write(&mut writer)
}
}
impl<G: Group + ProjectiveCurve> FromBytes for GroupEncryptionPublicKey<G> {
#[inline]
fn read<R: Read>(mut reader: R) -> IoResult<Self> {
let x_coordinate = <G::Affine as AffineCurve>::BaseField::read(&mut reader)?;
if let Some(element) = <G as ProjectiveCurve>::Affine::from_x_coordinate(x_coordinate, true) {
if element.is_in_correct_subgroup_assuming_on_curve() {
return Ok(Self(element.into_projective()));
}
}
if let Some(element) = <G as ProjectiveCurve>::Affine::from_x_coordinate(x_coordinate, false) {
if element.is_in_correct_subgroup_assuming_on_curve() {
return Ok(Self(element.into_projective()));
}
}
Err(EncryptionError::Message("Failed to read encryption public key".into()).into())
}
}
impl<G: Group + ProjectiveCurve> Default for GroupEncryptionPublicKey<G> {
fn default() -> Self {
Self(G::default())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GroupEncryption<G: Group + ProjectiveCurve> {
pub parameters: GroupEncryptionParameters<G>,
}
impl<G: Group + ProjectiveCurve> EncryptionScheme for GroupEncryption<G> {
type BlindingExponent = <G as Group>::ScalarField;
type Parameters = GroupEncryptionParameters<G>;
type PrivateKey = <G as Group>::ScalarField;
type PublicKey = GroupEncryptionPublicKey<G>;
type Randomness = <G as Group>::ScalarField;
type Text = G;
fn setup<R: Rng>(rng: &mut R) -> Self {
Self {
parameters: Self::Parameters::setup(rng, Self::PrivateKey::size_in_bits()),
}
}
fn generate_private_key<R: Rng>(&self, rng: &mut R) -> Self::PrivateKey {
let keygen_time = start_timer!(|| "GroupEncryption::generate_private_key");
let private_key = <G as Group>::ScalarField::rand(rng);
end_timer!(keygen_time);
private_key
}
fn generate_public_key(&self, private_key: &Self::PrivateKey) -> Result<Self::PublicKey, EncryptionError> {
let keygen_time = start_timer!(|| "GroupEncryption::generate_public_key");
let mut public_key = G::zero();
for (bit, base_power) in bytes_to_bits(&to_bytes![private_key]?)
.iter()
.zip_eq(&self.parameters.generator_powers)
{
if *bit {
public_key += &base_power;
}
}
end_timer!(keygen_time);
Ok(GroupEncryptionPublicKey(public_key))
}
fn generate_randomness<R: Rng>(
&self,
public_key: &Self::PublicKey,
rng: &mut R,
) -> Result<Self::Randomness, EncryptionError> {
let mut y = Self::Randomness::zero();
let mut z_bytes = vec![];
while Self::Randomness::read(&z_bytes[..]).is_err() {
y = Self::Randomness::rand(rng);
let affine = public_key.0.mul(&y).into_affine();
debug_assert!(affine.is_in_correct_subgroup_assuming_on_curve());
z_bytes = to_bytes![affine.to_x_coordinate()]?;
}
Ok(y)
}
fn generate_blinding_exponents(
&self,
public_key: &Self::PublicKey,
randomness: &Self::Randomness,
message_length: usize,
) -> Result<Vec<Self::BlindingExponent>, EncryptionError> {
let record_view_key = public_key.0.mul(&randomness);
let affine = record_view_key.into_affine();
debug_assert!(affine.is_in_correct_subgroup_assuming_on_curve());
let z_bytes = to_bytes![affine.to_x_coordinate()]?;
let z = Self::Randomness::read(&z_bytes[..])?;
let one = Self::Randomness::one();
let mut i = Self::Randomness::one();
let mut blinding_exponents = vec![];
for _ in 0..message_length {
match (z + &i).inverse() {
Some(val) => blinding_exponents.push(val),
None => return Err(EncryptionError::MissingInverse),
};
i += &one;
}
Ok(blinding_exponents)
}
fn encrypt(
&self,
public_key: &Self::PublicKey,
randomness: &Self::Randomness,
message: &Vec<Self::Text>,
) -> Result<Vec<Self::Text>, EncryptionError> {
let record_view_key = public_key.0.mul(&randomness);
let mut c_0 = G::zero();
for (bit, base_power) in bytes_to_bits(&to_bytes![randomness]?)
.iter()
.zip_eq(&self.parameters.generator_powers)
{
if *bit {
c_0 += &base_power;
}
}
let mut ciphertext = vec![c_0];
let one = Self::Randomness::one();
let mut i = Self::Randomness::one();
let blinding_exponents = self.generate_blinding_exponents(public_key, randomness, message.len())?;
for (m_i, blinding_exp) in message.iter().zip_eq(blinding_exponents) {
let h_i = record_view_key.mul(&blinding_exp);
let c_i = h_i + m_i;
ciphertext.push(c_i);
i += &one;
}
Ok(ciphertext)
}
fn decrypt(
&self,
private_key: &Self::PrivateKey,
ciphertext: &Vec<Self::Text>,
) -> Result<Vec<Self::Text>, EncryptionError> {
assert!(ciphertext.len() > 0);
let c_0 = &ciphertext[0];
let record_view_key = c_0.mul(&private_key);
let affine = record_view_key.into_affine();
debug_assert!(affine.is_in_correct_subgroup_assuming_on_curve());
let z_bytes = to_bytes![affine.to_x_coordinate()]?;
let z = Self::Randomness::read(&z_bytes[..])?;
let one = Self::Randomness::one();
let mut plaintext = vec![];
let mut i = Self::Randomness::one();
for c_i in ciphertext.iter().skip(1) {
let h_i = match &(z + &i).inverse() {
Some(val) => record_view_key.mul(val),
None => return Err(EncryptionError::MissingInverse),
};
let m_i = *c_i - &h_i;
plaintext.push(m_i);
i += &one;
}
Ok(plaintext)
}
fn parameters(&self) -> &Self::Parameters {
&self.parameters
}
fn private_key_size_in_bits() -> usize {
Self::PrivateKey::size_in_bits()
}
}
impl<G: Group + ProjectiveCurve> From<GroupEncryptionParameters<G>> for GroupEncryption<G> {
fn from(parameters: GroupEncryptionParameters<G>) -> Self {
Self { parameters }
}
}