fedimint_server/config/
distributedgen.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
use std::collections::BTreeMap;
use std::fmt::Debug;
use std::io::Write;

use anyhow::{ensure, Context};
use async_trait::async_trait;
use bitcoin::hashes::sha256::{Hash as Sha256, HashEngine};
use bitcoin::hashes::Hash as BitcoinHash;
use bls12_381::Scalar;
use fedimint_core::config::{DkgGroup, DkgMessage, ISupportedDkgMessage, P2PMessage};
use fedimint_core::encoding::{Decodable, Encodable};
use fedimint_core::module::registry::ModuleDecoderRegistry;
use fedimint_core::module::PeerHandle;
use fedimint_core::net::peers::{DynP2PConnections, Recipient};
use fedimint_core::{NumPeers, PeerId};
use rand::rngs::OsRng;
use rand::SeedableRng;
use rand_chacha::ChaChaRng;
use threshold_crypto::ff::Field;
use threshold_crypto::group::Curve;
use threshold_crypto::{G1Affine, G1Projective, G2Affine, G2Projective};

struct Dkg<G> {
    num_peers: NumPeers,
    identity: PeerId,
    generator: G,
    f1_poly: Vec<Scalar>,
    f2_poly: Vec<Scalar>,
    hashed_commits: BTreeMap<PeerId, Sha256>,
    commitments: BTreeMap<PeerId, Vec<G>>,
    sk_shares: BTreeMap<PeerId, Scalar>,
    pk_shares: BTreeMap<PeerId, Vec<G>>,
}

/// Implementation of "Secure Distributed Key Generation for Discrete-Log Based
/// Cryptosystems" by Rosario Gennaro and Stanislaw Jarecki and Hugo Krawczyk
/// and Tal Rabin
///
/// Prevents any manipulation of the secret key, but fails with any
/// non-cooperative peers
impl<G: DkgGroup> Dkg<G> {
    /// Creates the DKG and the first step of the algorithm
    pub fn new(num_peers: NumPeers, identity: PeerId, generator: G) -> (Self, DkgMessage<G>) {
        let f1_poly = random_coefficients(num_peers.threshold() - 1);
        let f2_poly = random_coefficients(num_peers.threshold() - 1);

        let mut dkg = Dkg {
            num_peers,
            identity,
            generator,
            f1_poly,
            f2_poly,
            hashed_commits: BTreeMap::new(),
            commitments: BTreeMap::new(),
            sk_shares: BTreeMap::new(),
            pk_shares: BTreeMap::new(),
        };

        // broadcast our commitment to the polynomials
        let commit: Vec<G> = dkg
            .f1_poly
            .iter()
            .map(|c| dkg.generator * *c)
            .zip(dkg.f2_poly.iter().map(|c| gen_h::<G>() * *c))
            .map(|(g, h)| g + h)
            .collect();

        let hashed = Dkg::hash(&commit);

        dkg.commitments.insert(identity, commit);
        dkg.hashed_commits.insert(identity, hashed);

        (dkg, DkgMessage::HashedCommit(hashed))
    }

    /// Runs a single step of the DKG algorithm, processing a `msg` from `peer`
    pub fn step(&mut self, peer: PeerId, msg: DkgMessage<G>) -> anyhow::Result<DkgStep<G>> {
        match msg {
            DkgMessage::HashedCommit(hashed) => {
                ensure!(
                    self.hashed_commits.insert(peer, hashed).is_none(),
                    "DKG: peer {peer} sent us two hash commitments."
                );

                if self.hashed_commits.len() == self.num_peers.total() {
                    let commit = self
                        .commitments
                        .get(&self.identity)
                        .expect("DKG hash commitment not found for identity.")
                        .clone();

                    return Ok(DkgStep::Broadcast(DkgMessage::Commit(commit)));
                }
            }
            DkgMessage::Commit(commit) => {
                ensure!(
                    self.num_peers.threshold() == commit.len(),
                    "DKG: polynomial commitment from peer {peer} is of wrong degree."
                );

                let hash_commitment = *self
                    .hashed_commits
                    .get(&peer)
                    .context("DKG: hash commitment not found for peer {peer}")?;

                ensure!(
                    Self::hash(&commit) == hash_commitment,
                    "DKG: polynomial commitment from peer {peer} has invalid hash."
                );

                ensure!(
                    self.commitments.insert(peer, commit).is_none(),
                    "DKG: peer {peer} sent us two commitments."
                );

                // once everyone has made commitments, send out shares
                if self.commitments.len() == self.num_peers.total() {
                    let mut messages = vec![];

                    for peer in self.num_peers.peer_ids() {
                        let s1 = eval_poly_scalar(&self.f1_poly, &scalar(&peer));
                        let s2 = eval_poly_scalar(&self.f2_poly, &scalar(&peer));

                        if peer == self.identity {
                            self.sk_shares.insert(self.identity, s1);
                        } else {
                            messages.push((peer, DkgMessage::Share(s1, s2)));
                        }
                    }

                    return Ok(DkgStep::Messages(messages));
                }
            }
            // Pedersen-VSS verifies the shares match the commitments
            DkgMessage::Share(s1, s2) => {
                let share_product: G = (self.generator * s1) + (gen_h::<G>() * s2);

                let commitment = self
                    .commitments
                    .get(&peer)
                    .context("DKG: polynomial commitment not found for peer {peer}.")?;

                let commit_product: G = commitment
                    .iter()
                    .enumerate()
                    .map(|(idx, commit)| {
                        *commit * scalar(&self.identity).pow(&[idx as u64, 0, 0, 0])
                    })
                    .reduce(|a, b| a + b)
                    .expect("DKG: polynomial commitment from peer {peer} is empty.");

                ensure!(
                    share_product == commit_product,
                    "DKG: share from {peer} is invalid."
                );

                ensure!(
                    self.sk_shares.insert(peer, s1).is_none(),
                    "Peer {peer} sent us two shares."
                );

                if self.sk_shares.len() == self.num_peers.total() {
                    let extract = self
                        .f1_poly
                        .iter()
                        .map(|c| self.generator * *c)
                        .collect::<Vec<G>>();

                    self.pk_shares.insert(self.identity, extract.clone());

                    return Ok(DkgStep::Broadcast(DkgMessage::Extract(extract)));
                }
            }
            // Feldman-VSS exposes the public key shares
            DkgMessage::Extract(extract) => {
                let share = self
                    .sk_shares
                    .get(&peer)
                    .context("DKG share not found for peer {peer}.")?;

                let extract_product: G = extract
                    .iter()
                    .enumerate()
                    .map(|(idx, commit)| {
                        *commit * scalar(&self.identity).pow(&[idx as u64, 0, 0, 0])
                    })
                    .reduce(|a, b| a + b)
                    .expect("sums");

                ensure!(
                    self.generator * *share == extract_product,
                    "DKG: extract from {peer} is invalid."
                );

                ensure!(
                    self.num_peers.threshold() == extract.len(),
                    "wrong degree from {peer}."
                );

                ensure!(
                    self.pk_shares.insert(peer, extract).is_none(),
                    "DKG: peer {peer} sent us two extracts."
                );

                if self.pk_shares.len() == self.num_peers.total() {
                    let sks = self.sk_shares.values().sum();

                    let pks: Vec<G> = (0..self.num_peers.threshold())
                        .map(|i| {
                            self.pk_shares
                                .values()
                                .map(|shares| shares[i])
                                .reduce(|a, b| a + b)
                                .expect("DKG: pk shares are empty.")
                        })
                        .collect();

                    return Ok(DkgStep::Result((pks, sks)));
                }
            }
        }

        Ok(DkgStep::Messages(vec![]))
    }

    fn hash(poly: &[G]) -> Sha256 {
        let mut engine = HashEngine::default();

        for element in poly {
            engine
                .write_all(element.to_bytes().as_ref())
                .expect("Writing to a hash engine cannot fail.");
        }

        Sha256::from_engine(engine)
    }
}

fn gen_h<G: DkgGroup>() -> G {
    G::random(&mut ChaChaRng::from_seed([42; 32]))
}

// `PeerId`s are offset by 1, since evaluating a poly at 0 reveals the secret
fn scalar(peer: &PeerId) -> Scalar {
    Scalar::from(peer.to_usize() as u64 + 1)
}

/// Runs the DKG algorithms with our peers. We do not handle any unexpected
/// messages and all peers are expected to be cooperative.
pub async fn run_dkg<G: DkgGroup>(
    num_peers: NumPeers,
    identity: PeerId,
    generator: G,
    connections: &DynP2PConnections<P2PMessage>,
) -> anyhow::Result<(Vec<G>, Scalar)>
where
    DkgMessage<G>: ISupportedDkgMessage,
{
    let (mut dkg, initial_message) = Dkg::new(num_peers, identity, generator);

    connections
        .send(
            Recipient::Everyone,
            P2PMessage::DistributedGen(initial_message.to_msg()),
        )
        .await;

    loop {
        for peer in num_peers.peer_ids().filter(|p| *p != identity) {
            let message = connections
                .receive_from_peer(peer)
                .await
                .context("Unexpected shutdown of p2p connections")?;

            let message = match message {
                P2PMessage::DistributedGen(message) => message,
                _ => anyhow::bail!("Wrong message received: {message:?}"),
            };

            match dkg.step(peer, ISupportedDkgMessage::from_msg(message)?)? {
                DkgStep::Broadcast(message) => {
                    connections
                        .send(
                            Recipient::Everyone,
                            P2PMessage::DistributedGen(message.to_msg()),
                        )
                        .await;
                }
                DkgStep::Messages(messages) => {
                    for (peer, message) in messages {
                        connections
                            .send(
                                Recipient::Peer(peer),
                                P2PMessage::DistributedGen(message.to_msg()),
                            )
                            .await;
                    }
                }
                DkgStep::Result(result) => {
                    return Ok(result);
                }
            }
        }
    }
}

fn random_coefficients(degree: usize) -> Vec<Scalar> {
    (0..=degree).map(|_| Scalar::random(&mut OsRng)).collect()
}

fn eval_poly_scalar(coefficients: &[Scalar], x: &Scalar) -> Scalar {
    coefficients
        .iter()
        .copied()
        .rev()
        .reduce(|acc, coefficient| acc * x + coefficient)
        .expect("We have at least one coefficient")
}

#[derive(Debug, Clone)]
pub enum DkgStep<G: DkgGroup> {
    Broadcast(DkgMessage<G>),
    Messages(Vec<(PeerId, DkgMessage<G>)>),
    Result((Vec<G>, Scalar)),
}

pub fn eval_poly_g1(coefficients: &[G1Projective], peer: &PeerId) -> G1Affine {
    coefficients
        .iter()
        .copied()
        .rev()
        .reduce(|acc, coefficient| acc * scalar(peer) + coefficient)
        .expect("We have at least one coefficient")
        .to_affine()
}

pub fn eval_poly_g2(coefficients: &[G2Projective], peer: &PeerId) -> G2Affine {
    coefficients
        .iter()
        .copied()
        .rev()
        .reduce(|acc, coefficient| acc * scalar(peer) + coefficient)
        .expect("We have at least one coefficient")
        .to_affine()
}

// TODO: this trait is only needed to break the `DkgHandle` impl
// from it's definition that is still in `fedimint-core`
#[async_trait]
pub trait PeerHandleOps {
    async fn run_dkg_g1(&self) -> anyhow::Result<(Vec<G1Projective>, Scalar)>;

    async fn run_dkg_g2(&self) -> anyhow::Result<(Vec<G2Projective>, Scalar)>;

    /// Exchanges a `DkgPeerMsg::Module(Vec<u8>)` with all peers. All peers are
    /// required to be online and submit a response for this to return
    /// properly. The caller's message will be included in the returned
    /// `BTreeMap` under the `PeerId` of this peer. This allows modules to
    /// exchange arbitrary data during distributed key generation.
    async fn exchange_encodable<T: Encodable + Decodable + Send + Sync>(
        &self,
        data: T,
    ) -> anyhow::Result<BTreeMap<PeerId, T>>;
}

#[async_trait]
impl<'a> PeerHandleOps for PeerHandle<'a> {
    async fn run_dkg_g1(&self) -> anyhow::Result<(Vec<G1Projective>, Scalar)> {
        run_dkg(
            self.num_peers,
            self.identity,
            G1Projective::generator(),
            self.connections,
        )
        .await
    }

    async fn run_dkg_g2(&self) -> anyhow::Result<(Vec<G2Projective>, Scalar)> {
        run_dkg(
            self.num_peers,
            self.identity,
            G2Projective::generator(),
            self.connections,
        )
        .await
    }

    async fn exchange_encodable<T: Encodable + Decodable + Send + Sync>(
        &self,
        data: T,
    ) -> anyhow::Result<BTreeMap<PeerId, T>> {
        let mut peer_data: BTreeMap<PeerId, T> = BTreeMap::new();

        self.connections
            .send(
                Recipient::Everyone,
                P2PMessage::Encodable(data.consensus_encode_to_vec()),
            )
            .await;

        peer_data.insert(self.identity, data);

        for peer in self.num_peers.peer_ids().filter(|p| *p != self.identity) {
            let message = self
                .connections
                .receive_from_peer(peer)
                .await
                .context("Unexpected shutdown of p2p connections")?;

            match message {
                P2PMessage::Encodable(bytes) => {
                    peer_data.insert(
                        peer,
                        T::consensus_decode_whole(&bytes, &ModuleDecoderRegistry::default())?,
                    );
                }
                message => {
                    anyhow::bail!("Invalid message from {peer}: {message:?}");
                }
            }
        }

        Ok(peer_data)
    }
}

#[cfg(test)]
mod tests {
    use std::collections::{HashMap, VecDeque};

    use bls12_381::Scalar;
    use fedimint_core::{NumPeersExt, PeerId};
    use tbs::derive_pk_share;
    use threshold_crypto::poly::Commitment;
    use threshold_crypto::serde_impl::SerdeSecret;
    use threshold_crypto::{G1Projective, G2Projective, PublicKeySet, SecretKeyShare};

    use crate::config::distributedgen::{eval_poly_g2, Dkg, DkgGroup, DkgStep};

    #[test_log::test]
    fn test_dkg() {
        for (peer, (polynomial, mut sks)) in run(G1Projective::generator()) {
            let public_key_set = PublicKeySet::from(Commitment::from(polynomial));
            let secret_key_share = SerdeSecret(SecretKeyShare::from_mut(&mut sks));

            assert_eq!(public_key_set.threshold(), 2);
            assert_eq!(
                public_key_set.public_key_share(peer.to_usize()),
                secret_key_share.public_key_share()
            );
        }

        for (peer, (polynomial, sks)) in run(G2Projective::generator()) {
            assert_eq!(polynomial.len(), 3);
            assert_eq!(
                eval_poly_g2(&polynomial, &peer),
                derive_pk_share(&tbs::SecretKeyShare(sks)).0
            );
        }
    }

    fn run<G: DkgGroup>(group: G) -> HashMap<PeerId, (Vec<G>, Scalar)> {
        let peers = (0..4_u16).map(PeerId::from).collect::<Vec<_>>();

        let mut steps: VecDeque<(PeerId, DkgStep<G>)> = VecDeque::new();
        let mut dkgs: HashMap<PeerId, Dkg<G>> = HashMap::new();
        let mut keys: HashMap<PeerId, (Vec<G>, Scalar)> = HashMap::new();

        for peer in &peers {
            let (dkg, initial_message) = Dkg::new(peers.to_num_peers(), *peer, group);
            dkgs.insert(*peer, dkg);
            steps.push_back((*peer, DkgStep::Broadcast(initial_message)));
        }

        while keys.len() < peers.len() {
            match steps.pop_front() {
                Some((peer, DkgStep::Broadcast(message))) => {
                    for receive_peer in peers.iter().filter(|p| **p != peer) {
                        let receive_dkg = dkgs.get_mut(receive_peer).unwrap();
                        let step = receive_dkg.step(peer, message.clone());
                        steps.push_back((*receive_peer, step.unwrap()));
                    }
                }
                Some((peer, DkgStep::Messages(messages))) => {
                    for (receive_peer, messages) in messages {
                        let receive_dkg = dkgs.get_mut(&receive_peer).unwrap();
                        let step = receive_dkg.step(peer, messages);
                        steps.push_back((receive_peer, step.unwrap()));
                    }
                }
                Some((peer, DkgStep::Result(step_keys))) => {
                    keys.insert(peer, step_keys);
                }
                _ => {}
            }
        }

        assert!(steps.is_empty());

        keys
    }
}