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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
use curve25519_dalek::ristretto::{CompressedRistretto, RistrettoPoint};
use curve25519_dalek::scalar::Scalar;
use curve25519_dalek::traits::MultiscalarMul;
use clear_on_drop::clear::Clear;
use errors::MPCError;
use generators::{BulletproofGens, PedersenGens};
use rand;
use std::iter;
use util;
use super::messages::*;
pub struct Party {}
impl Party {
pub fn new<'a>(
bp_gens: &'a BulletproofGens,
pc_gens: &'a PedersenGens,
v: u64,
v_blinding: Scalar,
n: usize,
) -> Result<PartyAwaitingPosition<'a>, MPCError> {
if !(n == 8 || n == 16 || n == 32 || n == 64) {
return Err(MPCError::InvalidBitsize);
}
if bp_gens.gens_capacity < n {
return Err(MPCError::InvalidGeneratorsLength);
}
let V = pc_gens.commit(v.into(), v_blinding).compress();
Ok(PartyAwaitingPosition {
bp_gens,
pc_gens,
n,
v,
v_blinding,
V,
})
}
}
pub struct PartyAwaitingPosition<'a> {
bp_gens: &'a BulletproofGens,
pc_gens: &'a PedersenGens,
n: usize,
v: u64,
v_blinding: Scalar,
V: CompressedRistretto,
}
impl<'a> PartyAwaitingPosition<'a> {
pub fn assign_position(
self,
j: usize,
) -> Result<(PartyAwaitingBitChallenge<'a>, BitCommitment), MPCError> {
let mut rng = rand::thread_rng();
if self.bp_gens.party_capacity <= j {
return Err(MPCError::InvalidGeneratorsLength);
}
let bp_share = self.bp_gens.share(j);
let a_blinding = Scalar::random(&mut rng);
let mut A = self.pc_gens.B_blinding * a_blinding;
use subtle::{Choice, ConditionallySelectable};
let mut i = 0;
for (G_i, H_i) in bp_share.G(self.n).zip(bp_share.H(self.n)) {
let v_i = Choice::from(((self.v >> i) & 1) as u8);
let mut point = -H_i;
point.conditional_assign(G_i, v_i);
A += point;
i += 1;
}
let s_blinding = Scalar::random(&mut rng);
let s_L: Vec<Scalar> = (0..self.n).map(|_| Scalar::random(&mut rng)).collect();
let s_R: Vec<Scalar> = (0..self.n).map(|_| Scalar::random(&mut rng)).collect();
let S = RistrettoPoint::multiscalar_mul(
iter::once(&s_blinding).chain(s_L.iter()).chain(s_R.iter()),
iter::once(&self.pc_gens.B_blinding)
.chain(bp_share.G(self.n))
.chain(bp_share.H(self.n)),
);
let bit_commitment = BitCommitment {
V_j: self.V,
A_j: A,
S_j: S,
};
let next_state = PartyAwaitingBitChallenge {
n: self.n,
v: self.v,
v_blinding: self.v_blinding,
pc_gens: self.pc_gens,
j,
a_blinding,
s_blinding,
s_L,
s_R,
};
Ok((next_state, bit_commitment))
}
}
impl<'a> Drop for PartyAwaitingPosition<'a> {
fn drop(&mut self) {
self.v.clear();
self.v_blinding.clear();
}
}
pub struct PartyAwaitingBitChallenge<'a> {
n: usize,
v: u64,
v_blinding: Scalar,
j: usize,
pc_gens: &'a PedersenGens,
a_blinding: Scalar,
s_blinding: Scalar,
s_L: Vec<Scalar>,
s_R: Vec<Scalar>,
}
impl<'a> PartyAwaitingBitChallenge<'a> {
pub fn apply_challenge(
self,
vc: &BitChallenge,
) -> (PartyAwaitingPolyChallenge, PolyCommitment) {
let mut rng = rand::thread_rng();
let n = self.n;
let offset_y = util::scalar_exp_vartime(&vc.y, (self.j * n) as u64);
let offset_z = util::scalar_exp_vartime(&vc.z, self.j as u64);
let mut l_poly = util::VecPoly1::zero(n);
let mut r_poly = util::VecPoly1::zero(n);
let zz = vc.z * vc.z;
let mut exp_y = offset_y;
let mut exp_2 = Scalar::one();
for i in 0..n {
let a_L_i = Scalar::from((self.v >> i) & 1);
let a_R_i = a_L_i - Scalar::one();
l_poly.0[i] = a_L_i - vc.z;
l_poly.1[i] = self.s_L[i];
r_poly.0[i] = exp_y * (a_R_i + vc.z) + zz * offset_z * exp_2;
r_poly.1[i] = exp_y * self.s_R[i];
exp_y *= vc.y;
exp_2 = exp_2 + exp_2;
}
let t_poly = l_poly.inner_product(&r_poly);
let t_1_blinding = Scalar::random(&mut rng);
let t_2_blinding = Scalar::random(&mut rng);
let T_1 = self.pc_gens.commit(t_poly.1, t_1_blinding);
let T_2 = self.pc_gens.commit(t_poly.2, t_2_blinding);
let poly_commitment = PolyCommitment {
T_1_j: T_1,
T_2_j: T_2,
};
let papc = PartyAwaitingPolyChallenge {
v_blinding: self.v_blinding,
a_blinding: self.a_blinding,
s_blinding: self.s_blinding,
z: vc.z,
offset_z,
l_poly,
r_poly,
t_poly,
t_1_blinding,
t_2_blinding,
};
(papc, poly_commitment)
}
}
impl<'a> Drop for PartyAwaitingBitChallenge<'a> {
fn drop(&mut self) {
self.v.clear();
self.v_blinding.clear();
self.a_blinding.clear();
self.s_blinding.clear();
for e in self.s_L.iter_mut() {
e.clear();
}
for e in self.s_R.iter_mut() {
e.clear();
}
}
}
pub struct PartyAwaitingPolyChallenge {
z: Scalar,
offset_z: Scalar,
l_poly: util::VecPoly1,
r_poly: util::VecPoly1,
t_poly: util::Poly2,
v_blinding: Scalar,
a_blinding: Scalar,
s_blinding: Scalar,
t_1_blinding: Scalar,
t_2_blinding: Scalar,
}
impl PartyAwaitingPolyChallenge {
pub fn apply_challenge(self, pc: &PolyChallenge) -> Result<ProofShare, MPCError> {
if pc.x == Scalar::zero() {
return Err(MPCError::MaliciousDealer);
}
let t_blinding_poly = util::Poly2(
self.z * self.z * self.offset_z * self.v_blinding,
self.t_1_blinding,
self.t_2_blinding,
);
let t_x = self.t_poly.eval(pc.x);
let t_x_blinding = t_blinding_poly.eval(pc.x);
let e_blinding = self.a_blinding + self.s_blinding * &pc.x;
let l_vec = self.l_poly.eval(pc.x);
let r_vec = self.r_poly.eval(pc.x);
Ok(ProofShare {
t_x_blinding,
t_x,
e_blinding,
l_vec,
r_vec,
})
}
}
impl Drop for PartyAwaitingPolyChallenge {
fn drop(&mut self) {
self.v_blinding.clear();
self.a_blinding.clear();
self.s_blinding.clear();
self.t_1_blinding.clear();
self.t_2_blinding.clear();
}
}