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
//! Participants in a DKG/Resharing procedure that receive dealings from dealers
//! and eventually maintain a share of a shared secret.
use crate::{
bls12381::{
dkg::{
ops::{recover_public_with_weights, verify_commitment, verify_share},
Error,
},
primitives::{
group::{self, Element, Share},
poly::{self, Eval},
variant::Variant,
},
},
PublicKey,
};
use commonware_utils::{quorum, set::Ordered};
use std::collections::{btree_map::Entry, BTreeMap};
/// Output of a DKG/Resharing procedure.
#[derive(Clone)]
pub struct Output<V: Variant> {
/// The group polynomial output by the DKG/Resharing procedure.
pub public: poly::Public<V>,
/// The player's share of the shared secret that corresponds to
/// the group polynomial. Any `2f + 1` players can combine their
/// shares to recover the shared secret.
pub share: Share,
}
/// Track commitments and dealings distributed by dealers.
pub struct Player<P: PublicKey, V: Variant> {
me: u32,
dealer_threshold: u32,
player_threshold: u32,
previous: Option<poly::Public<V>>,
concurrency: usize,
dealers: Ordered<P>,
dealings: BTreeMap<u32, (poly::Public<V>, Share)>,
}
impl<P: PublicKey, V: Variant> Player<P, V> {
/// Create a new player for a DKG/Resharing procedure.
pub fn new(
me: P,
previous: Option<poly::Public<V>>,
dealers: Ordered<P>,
recipients: Ordered<P>,
concurrency: usize,
) -> Self {
let me_idx = recipients.position(&me).expect("player not in recipients") as u32;
Self {
me: me_idx,
dealer_threshold: quorum(dealers.len() as u32),
player_threshold: quorum(recipients.len() as u32),
previous,
concurrency,
dealers,
dealings: BTreeMap::new(),
}
}
/// Verify and track a commitment from a dealer.
pub fn share(
&mut self,
dealer: P,
commitment: poly::Public<V>,
share: Share,
) -> Result<(), Error> {
// Ensure dealer is valid
let dealer_idx = match self.dealers.position(&dealer) {
Some(contributor) => contributor,
None => return Err(Error::DealerInvalid),
} as u32;
// Check that share is valid
if share.index != self.me {
return Err(Error::MisdirectedShare);
}
// If already have commitment from dealer, check if matches
if let Some((existing_commitment, existing_share)) = self.dealings.get(&dealer_idx) {
if existing_commitment != &commitment {
return Err(Error::MismatchedCommitment);
}
if existing_share != &share {
return Err(Error::MismatchedShare);
}
return Err(Error::DuplicateShare);
}
// Verify that commitment is valid
verify_commitment::<V>(
self.previous.as_ref(),
&commitment,
dealer_idx,
self.player_threshold,
)?;
// Verify that share is valid
verify_share::<V>(&commitment, share.index, &share)?;
// Store dealings
self.dealings.insert(dealer_idx, (commitment, share));
Ok(())
}
/// If we are tracking shares for all provided `commitments`, recover
/// the new group public polynomial and our share.
pub fn finalize(
mut self,
commitments: BTreeMap<u32, poly::Public<V>>,
mut reveals: BTreeMap<u32, Share>,
) -> Result<Output<V>, Error> {
// Ensure commitments equals required commitment count
let dealer_threshold = self.dealer_threshold as usize;
if commitments.len() != dealer_threshold {
return Err(Error::InvalidCommitments);
}
// Remove unnecessary dealings
self.dealings.retain(|idx, _| commitments.contains_key(idx));
// Iterate over selected commitments and confirm they match what we've acknowledged
// or that we have received a reveal.
for (idx, commitment) in commitments {
match self.dealings.entry(idx) {
Entry::Occupied(mut entry) => {
// If our stored commitment matches the one we are receiving,
// we do nothing (as our share is valid).
let (stored_commitment, stored_share) = entry.get_mut();
if stored_commitment == &commitment {
continue;
}
// If our stored commitment does not match the one we are receiving,
// we must have received a reveal for this commitment (this is dealer
// equivocation).
verify_commitment::<V>(
self.previous.as_ref(),
&commitment,
idx,
self.player_threshold,
)?;
let share = reveals.remove(&idx).ok_or(Error::MissingShare)?;
// Check that reveal is valid (updating stored commitment and share, if so)
verify_share::<V>(&commitment, self.me, &share)?;
*stored_commitment = commitment;
*stored_share = share;
}
Entry::Vacant(entry) => {
// We must have received a reveal for this commitment
verify_commitment::<V>(
self.previous.as_ref(),
&commitment,
idx,
self.player_threshold,
)?;
let share = reveals.remove(&idx).ok_or(Error::MissingShare)?;
// Check that reveal is valid
verify_share::<V>(&commitment, self.me, &share)?;
entry.insert((commitment, share));
}
}
}
if self.dealings.len() != dealer_threshold {
return Err(Error::MissingShare);
}
// Construct secret
let mut public = poly::Public::<V>::zero();
let mut secret = group::Private::zero();
match self.previous {
None => {
// Add all valid commitments/dealings
for (commitment, private) in self.dealings.values() {
public.add(commitment);
secret.add(private.as_ref());
}
}
Some(previous) => {
// Construct commitments and shares
let mut indices = Vec::with_capacity(self.dealings.len());
let mut commitments = BTreeMap::new();
let mut dealings = Vec::with_capacity(self.dealings.len());
for (dealer, (commitment, share)) in self.dealings.into_iter() {
indices.push(dealer);
commitments.insert(dealer, commitment);
dealings.push(Eval {
index: dealer,
value: share.private,
});
}
// Compute weights
let weights = poly::compute_weights(indices)
.map_err(|_| Error::PublicKeyInterpolationFailed)?;
// Recover public via interpolation
//
// While it is tempting to remove this work (given we only need the secret
// to generate a threshold signature), this polynomial is required to verify
// dealings of future resharings.
public = recover_public_with_weights::<V>(
&previous,
&commitments,
&weights,
self.player_threshold,
self.concurrency,
)?;
// Recover share via interpolation
secret = match poly::Private::recover_with_weights(&weights, &dealings) {
Ok(share) => share,
Err(_) => return Err(Error::ShareInterpolationFailed),
};
}
}
// Return the public polynomial and share
Ok(Output {
public,
share: Share {
index: self.me,
private: secret,
},
})
}
}