frost.rs (1904B)
1 use secp256kfun::Scalar; 2 3 use schnorr_fun::{ 4 frost::{Frost, ScalarPoly, XOnlyFrostKey}, 5 nonce::Deterministic, 6 }; 7 8 use schnorr_fun::Schnorr; 9 use sha2::Sha256; 10 11 pub fn frost_keygen(threshold: usize, n_parties: usize) -> (Vec<Scalar>, Vec<XOnlyFrostKey>) { 12 let frost = Frost::new(Schnorr::<Sha256, Deterministic<Sha256>>::new( 13 Deterministic::<Sha256>::default(), 14 )); 15 assert!(threshold <= n_parties); 16 17 // create some scalar polynomial for each party 18 let mut rng = rand::rngs::ThreadRng::default(); 19 let scalar_polys = (0..n_parties) 20 .map(|_| ScalarPoly::random(threshold, &mut rng)) 21 .collect::<Vec<_>>(); 22 let point_polys = scalar_polys 23 .iter() 24 .map(ScalarPoly::to_point_poly) 25 .collect::<Vec<_>>(); 26 let keygen = frost.new_keygen(point_polys).unwrap(); 27 let (shares, proofs_of_possesion): (Vec<_>, Vec<_>) = scalar_polys 28 .into_iter() 29 .map(|sp| frost.create_shares(&keygen, sp)) 30 .unzip(); 31 // collect the received shares for each party 32 let received_shares = (0..n_parties) 33 .map(|party_index| { 34 (0..n_parties) 35 .map(|share_index| shares[share_index][party_index].clone()) 36 .collect() 37 }) 38 .collect::<Vec<Vec<_>>>(); 39 40 // finish keygen for each party 41 let (secret_shares, frost_keys): (Vec<_>, Vec<_>) = (0..n_parties) 42 .map(|party_index| { 43 let (secret_share, frost_key) = frost 44 .finish_keygen( 45 keygen.clone(), 46 party_index, 47 received_shares[party_index].clone(), 48 proofs_of_possesion.clone(), 49 ) 50 .unwrap(); 51 52 let xonly_frost_key = frost_key.into_xonly_key(); 53 (secret_share, xonly_frost_key) 54 }) 55 .unzip(); 56 57 (secret_shares, frost_keys) 58 }