/
feldman_dvss_dkg.rs
373 lines (340 loc) · 14.7 KB
/
feldman_dvss_dkg.rs
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
//! Feldman Distributed Verifiable secret sharing and distributed key generation.
use crate::{
common::{lagrange_basis_at_0, CommitmentToCoefficients, ParticipantId, Share, ShareId},
error::SSError,
};
use ark_ec::{AffineRepr, CurveGroup, VariableBaseMSM};
use ark_ff::{PrimeField, Zero};
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use ark_std::{cfg_iter, collections::BTreeMap, vec, vec::Vec};
use serde::{Deserialize, Serialize};
use zeroize::Zeroize;
#[cfg(feature = "parallel")]
use rayon::prelude::*;
/// Used by a participant to store received shares and commitment coefficients.
#[derive(
Clone, Debug, PartialEq, Eq, CanonicalSerialize, CanonicalDeserialize, Serialize, Deserialize,
)]
#[serde(bound = "")]
pub struct SharesAccumulator<G: AffineRepr> {
pub participant_id: ParticipantId,
pub threshold: ShareId,
pub shares: BTreeMap<ParticipantId, Share<G::ScalarField>>,
pub coeff_comms: BTreeMap<ParticipantId, CommitmentToCoefficients<G>>,
}
impl<G: AffineRepr> Zeroize for SharesAccumulator<G> {
fn zeroize(&mut self) {
self.shares.values_mut().for_each(|v| v.zeroize())
}
}
impl<G: AffineRepr> Drop for SharesAccumulator<G> {
fn drop(&mut self) {
self.zeroize()
}
}
impl<G: AffineRepr> SharesAccumulator<G> {
pub fn new(id: ParticipantId, threshold: ShareId) -> Self {
Self {
participant_id: id,
threshold,
shares: Default::default(),
coeff_comms: Default::default(),
}
}
/// Called by a participant when it creates a share for itself
pub fn add_self_share(
&mut self,
share: Share<G::ScalarField>,
commitment_coeffs: CommitmentToCoefficients<G>,
) {
self.update_unchecked(self.participant_id, share, commitment_coeffs)
}
/// Called by a participant when it receives a share from another participant
pub fn add_received_share<'a>(
&mut self,
sender_id: ParticipantId,
share: Share<G::ScalarField>,
commitment_coeffs: CommitmentToCoefficients<G>,
ck: impl Into<&'a G>,
) -> Result<(), SSError> {
if sender_id == self.participant_id {
return Err(SSError::SenderIdSameAsReceiver(
sender_id,
self.participant_id,
));
}
if self.shares.contains_key(&sender_id) {
return Err(SSError::AlreadyProcessedFromSender(sender_id));
}
self.update(sender_id, share, commitment_coeffs, ck.into())
}
/// Called by a participant when it has received shares from all participants. Computes the final
/// share of the distributed secret, own public key and the threshold public key
pub fn finalize<'a>(
mut self,
ck: impl Into<&'a G> + Clone,
) -> Result<(Share<G::ScalarField>, G, G), SSError> {
let shares = core::mem::take(&mut self.shares);
let comms = core::mem::take(&mut self.coeff_comms);
Self::gen_final_share_and_public_key(self.participant_id, self.threshold, shares, comms, ck)
}
/// Compute the final share after receiving shares from all other participants. Also returns
/// own public key and the threshold public key
pub fn gen_final_share_and_public_key<'a>(
participant_id: ParticipantId,
threshold: ShareId,
shares: BTreeMap<ParticipantId, Share<G::ScalarField>>,
coeff_comms: BTreeMap<ParticipantId, CommitmentToCoefficients<G>>,
ck: impl Into<&'a G> + Clone,
) -> Result<(Share<G::ScalarField>, G, G), SSError> {
// Check early that sufficient shares present
let len = shares.len() as ShareId;
if threshold > len {
return Err(SSError::BelowThreshold(threshold, len));
}
let mut final_share = G::ScalarField::zero();
let mut final_comm_coeffs = vec![G::Group::zero(); threshold as usize];
for (_, share) in shares {
final_share += share.share;
}
let mut threshold_pk = G::Group::zero();
for comm in coeff_comms.values() {
for i in 0..threshold as usize {
final_comm_coeffs[i] += comm.0[i];
}
threshold_pk += comm.commitment_to_secret();
}
let comm_coeffs = G::Group::normalize_batch(&final_comm_coeffs).into();
let final_share = Share {
id: participant_id,
threshold,
share: final_share,
};
final_share.verify(&comm_coeffs, ck.clone())?;
let pk = ck
.into()
.mul_bigint(final_share.share.into_bigint())
.into_affine();
Ok((final_share, pk, threshold_pk.into_affine()))
}
/// Update accumulator on share sent by another party. If the share verifies, stores it.
fn update(
&mut self,
id: ParticipantId,
share: Share<G::ScalarField>,
commitment_coeffs: CommitmentToCoefficients<G>,
ck: &G,
) -> Result<(), SSError> {
if self.participant_id != share.id {
return Err(SSError::UnequalParticipantAndShareId(
self.participant_id,
share.id,
));
}
if self.threshold != share.threshold {
return Err(SSError::UnequalThresholdInReceivedShare(
self.threshold,
share.threshold,
));
}
share.verify(&commitment_coeffs, ck)?;
self.update_unchecked(id, share, commitment_coeffs);
Ok(())
}
/// Update accumulator on share created by self. Assumes the share is valid
fn update_unchecked(
&mut self,
id: ParticipantId,
share: Share<G::ScalarField>,
commitment_coeffs: CommitmentToCoefficients<G>,
) {
self.shares.insert(id, share);
self.coeff_comms.insert(id, commitment_coeffs);
}
}
/// Reconstruct threshold key using the individual public keys. Multiplies each public key with its
/// Lagrange coefficient and adds the result
pub fn reconstruct_threshold_public_key<G: AffineRepr>(
public_keys: Vec<(ShareId, G)>,
threshold: ShareId,
) -> Result<G, SSError> {
let len = public_keys.len() as ShareId;
if threshold > len {
return Err(SSError::BelowThreshold(threshold, len));
}
let pkt = &public_keys[0..threshold as usize];
let pk_ids = pkt.iter().map(|(i, _)| *i).collect::<Vec<_>>();
let pks = pkt.iter().map(|(_, pk)| *pk).collect::<Vec<_>>();
let lcs = cfg_iter!(pk_ids)
.map(|i| lagrange_basis_at_0::<G::ScalarField>(&pk_ids, *i))
.collect::<Vec<_>>();
Ok(G::Group::msm_unchecked(&pks, &lcs).into_affine())
}
#[cfg(test)]
pub mod tests {
use super::*;
use crate::{common::Shares, feldman_vss::deal_random_secret};
use ark_ec::Group;
use ark_std::{
rand::{rngs::StdRng, SeedableRng},
UniformRand,
};
use test_utils::{test_serialization, G1, G2};
#[test]
fn feldman_distributed_verifiable_secret_sharing() {
let mut rng = StdRng::seed_from_u64(0u64);
let g1 = G1::rand(&mut rng);
let g2 = G2::rand(&mut rng);
fn check<G: AffineRepr>(rng: &mut StdRng, g: &G) {
for (threshold, total) in vec![
(2, 2),
(2, 3),
(2, 4),
(2, 5),
(3, 3),
(3, 4),
(3, 5),
(4, 5),
(4, 8),
(4, 9),
(4, 12),
(5, 5),
(5, 7),
(5, 10),
(5, 13),
(7, 10),
(7, 15),
] {
// There are `total` number of participants
let mut accumulators = (1..=total)
.map(|i| SharesAccumulator::new(i as ParticipantId, threshold as ShareId))
.collect::<Vec<_>>();
let mut secrets = vec![];
let mut final_shares = vec![];
// Each participant creates a secret and secret-shares it with other participants
for sender_id in 1..=total {
// Participant creates a secret and its shares
let (secret, shares, commitments, _) =
deal_random_secret::<_, G>(rng, threshold as ShareId, total as ShareId, g)
.unwrap();
secrets.push(secret);
// The participant sends other participants their respective shares and stores its own share as well
for receiver_id in 1..=total {
if sender_id != receiver_id {
// Participant rejects invalid received shares
let mut share_with_wrong_id = shares.0[receiver_id - 1].clone();
share_with_wrong_id.id = share_with_wrong_id.id + 1;
assert!(accumulators[receiver_id - 1]
.add_received_share(
sender_id as ParticipantId,
share_with_wrong_id,
commitments.clone(),
g,
)
.is_err());
let mut share_with_wrong_threshold = shares.0[receiver_id - 1].clone();
share_with_wrong_threshold.threshold =
share_with_wrong_threshold.threshold + 1;
assert!(accumulators[receiver_id - 1]
.add_received_share(
sender_id as ParticipantId,
share_with_wrong_threshold,
commitments.clone(),
g,
)
.is_err());
let mut wrong_commitments = commitments.clone();
wrong_commitments.0.remove(0);
assert!(accumulators[receiver_id - 1]
.add_received_share(
sender_id as ParticipantId,
shares.0[receiver_id - 1].clone(),
wrong_commitments,
g,
)
.is_err());
let mut wrong_commitments = commitments.clone();
wrong_commitments.0[0] =
wrong_commitments.0[0].into_group().double().into_affine();
assert!(accumulators[receiver_id - 1]
.add_received_share(
sender_id as ParticipantId,
shares.0[receiver_id - 1].clone(),
wrong_commitments,
g,
)
.is_err());
// Participant processes a received share
accumulators[receiver_id - 1]
.add_received_share(
sender_id as ParticipantId,
shares.0[receiver_id - 1].clone(),
commitments.clone(),
g,
)
.unwrap();
// Adding duplicate share not allowed
assert!(accumulators[receiver_id - 1]
.add_received_share(
sender_id as ParticipantId,
shares.0[receiver_id - 1].clone(),
commitments.clone(),
g,
)
.is_err());
} else {
// Participant processes its own share for its created secret
accumulators[receiver_id - 1].add_self_share(
shares.0[receiver_id - 1].clone(),
commitments.clone(),
);
// Cannot add share with own id
assert!(accumulators[receiver_id - 1]
.add_received_share(
sender_id as ParticipantId,
shares.0[receiver_id - 1].clone(),
commitments.clone(),
g,
)
.is_err());
}
// Cannot create the final share when having shares from less than threshold number of participants
if (accumulators[receiver_id - 1].shares.len() as ShareId) < threshold {
assert!(accumulators[receiver_id - 1].clone().finalize(g).is_err());
}
}
}
test_serialization!(SharesAccumulator<G>, accumulators[0].clone());
let mut tk = None;
let mut all_pk = vec![];
// Each participant computes its share of the final secret
for accumulator in accumulators {
let (share, pk, t_pk) = accumulator.finalize(g).unwrap();
assert_eq!(g.mul_bigint(share.share.into_bigint()).into_affine(), pk);
if tk.is_none() {
tk = Some(t_pk);
} else {
// All generate the same threshold key
assert_eq!(tk, Some(t_pk));
}
all_pk.push(pk);
final_shares.push(share);
}
let final_secret = secrets.iter().sum::<G::ScalarField>();
let final_shares = Shares(final_shares);
assert_eq!(final_shares.reconstruct_secret().unwrap(), final_secret);
let pk_with_ids = all_pk
.into_iter()
.enumerate()
.map(|(i, pk)| ((i + 1) as ShareId, pk))
.collect::<Vec<_>>();
assert_eq!(
tk,
Some(reconstruct_threshold_public_key(pk_with_ids, threshold).unwrap())
);
}
}
check(&mut rng, &g1);
check(&mut rng, &g2);
}
}