|
| 1 | +use rand::Rng; |
| 2 | +use rand_core::{CryptoRngCore, SeedableRng}; |
| 3 | + |
| 4 | +use threshold_signatures::{ |
| 5 | + ReconstructionThreshold, |
| 6 | + confidential_key_derivation::{ |
| 7 | + self as ckd, |
| 8 | + ciphersuite::{Field as _, Group as _}, |
| 9 | + }, |
| 10 | + participants::Participant, |
| 11 | + protocol::Protocol, |
| 12 | + test_utils::{MockCryptoRng, generate_participants_with_random_ids, run_keygen}, |
| 13 | +}; |
| 14 | + |
| 15 | +use super::MAX_MALICIOUS; |
| 16 | + |
| 17 | +pub fn prepare_ckd<R: CryptoRngCore + SeedableRng + Send + 'static>( |
| 18 | + threshold: ReconstructionThreshold, |
| 19 | + rng: &mut R, |
| 20 | +) -> PreparedCkdPackage { |
| 21 | + let num_participants = threshold.value(); |
| 22 | + // collect all participants |
| 23 | + let participants = generate_participants_with_random_ids(num_participants, rng); |
| 24 | + let key_packages = run_keygen(&participants, *MAX_MALICIOUS + 1, rng); |
| 25 | + |
| 26 | + // choose a coordinator at random |
| 27 | + let coordinator_index = rng.gen_range(0..num_participants); |
| 28 | + let coordinator = participants[coordinator_index]; |
| 29 | + |
| 30 | + let mut protocols = Vec::with_capacity(participants.len()); |
| 31 | + |
| 32 | + let mut app_id: [u8; 32] = [0u8; 32]; |
| 33 | + rng.fill_bytes(&mut app_id); |
| 34 | + let app_id = ckd::AppId::try_new(app_id).expect("cannot fail"); |
| 35 | + |
| 36 | + let scalar_rng = MockCryptoRng::seed_from_u64(rng.next_u64()); |
| 37 | + let app_sk = ckd::Scalar::random(scalar_rng); |
| 38 | + let app_pk = ckd::ElementG1::generator() * app_sk; |
| 39 | + |
| 40 | + for (p, keygen_out) in &key_packages { |
| 41 | + let rng_p = MockCryptoRng::seed_from_u64(rng.next_u64()); |
| 42 | + let protocol = ckd::protocol::ckd( |
| 43 | + &participants, |
| 44 | + coordinator, |
| 45 | + *p, |
| 46 | + keygen_out.clone(), |
| 47 | + app_id.clone(), |
| 48 | + app_pk, |
| 49 | + rng_p, |
| 50 | + ) |
| 51 | + .map(|ckd| Box::new(ckd) as Box<dyn Protocol<Output = ckd::CKDOutputOption>>) |
| 52 | + .expect("Ckd should succeed"); |
| 53 | + protocols.push((*p, protocol)); |
| 54 | + } |
| 55 | + |
| 56 | + PreparedCkdPackage { |
| 57 | + protocols, |
| 58 | + index: coordinator_index, |
| 59 | + key_packages, |
| 60 | + app_id, |
| 61 | + app_pk, |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +pub struct PreparedCkdPackage { |
| 66 | + pub protocols: Vec<( |
| 67 | + Participant, |
| 68 | + Box<dyn Protocol<Output = ckd::CKDOutputOption>>, |
| 69 | + )>, |
| 70 | + pub index: usize, |
| 71 | + pub key_packages: Vec<(Participant, ckd::KeygenOutput)>, |
| 72 | + pub app_id: ckd::AppId, |
| 73 | + pub app_pk: ckd::ElementG1, |
| 74 | +} |
0 commit comments