2022-06-05 07:33:15 -04:00
|
|
|
use std::{sync::Arc, collections::HashMap};
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-05-25 00:28:57 -04:00
|
|
|
use rand_core::{RngCore, CryptoRng};
|
2022-04-23 03:49:30 -04:00
|
|
|
|
2022-06-19 06:36:47 -04:00
|
|
|
use group::ff::Field;
|
2022-05-30 16:37:51 -04:00
|
|
|
|
2022-05-25 00:28:57 -04:00
|
|
|
use crate::{
|
2022-04-21 21:36:18 -04:00
|
|
|
Curve,
|
|
|
|
|
MultisigParams, MultisigKeys,
|
2022-05-30 16:37:51 -04:00
|
|
|
lagrange,
|
2022-06-24 08:40:14 -04:00
|
|
|
key_gen::KeyGenMachine,
|
2022-05-25 00:28:57 -04:00
|
|
|
algorithm::Algorithm,
|
2022-06-24 08:40:14 -04:00
|
|
|
sign::{PreprocessMachine, SignMachine, SignatureMachine, AlgorithmMachine}
|
2022-04-21 21:36:18 -04:00
|
|
|
};
|
|
|
|
|
|
2022-05-25 00:28:57 -04:00
|
|
|
// Test suites for public usage
|
|
|
|
|
pub mod curve;
|
2022-06-03 19:08:25 -04:00
|
|
|
pub mod schnorr;
|
2022-06-03 01:25:46 -04:00
|
|
|
pub mod vectors;
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-05-25 00:28:57 -04:00
|
|
|
// Literal test definitions to run during `cargo test`
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod literal;
|
|
|
|
|
|
|
|
|
|
pub const PARTICIPANTS: u16 = 5;
|
|
|
|
|
pub const THRESHOLD: u16 = ((PARTICIPANTS / 3) * 2) + 1;
|
|
|
|
|
|
|
|
|
|
pub fn clone_without<K: Clone + std::cmp::Eq + std::hash::Hash, V: Clone>(
|
2022-05-24 21:41:14 -04:00
|
|
|
map: &HashMap<K, V>,
|
|
|
|
|
without: &K
|
|
|
|
|
) -> HashMap<K, V> {
|
|
|
|
|
let mut res = map.clone();
|
|
|
|
|
res.remove(without).unwrap();
|
|
|
|
|
res
|
2022-04-21 21:36:18 -04:00
|
|
|
}
|
|
|
|
|
|
2022-05-25 00:28:57 -04:00
|
|
|
pub fn key_gen<R: RngCore + CryptoRng, C: Curve>(
|
|
|
|
|
rng: &mut R
|
2022-06-05 07:33:15 -04:00
|
|
|
) -> HashMap<u16, Arc<MultisigKeys<C>>> {
|
2022-05-24 21:41:14 -04:00
|
|
|
let mut machines = HashMap::new();
|
|
|
|
|
let mut commitments = HashMap::new();
|
2022-04-21 21:36:18 -04:00
|
|
|
for i in 1 ..= PARTICIPANTS {
|
2022-06-24 08:40:14 -04:00
|
|
|
let machine = KeyGenMachine::<C>::new(
|
|
|
|
|
MultisigParams::new(THRESHOLD, PARTICIPANTS, i).unwrap(),
|
|
|
|
|
"FROST Test key_gen".to_string()
|
2022-05-24 21:41:14 -04:00
|
|
|
);
|
2022-06-24 08:40:14 -04:00
|
|
|
let (machine, these_commitments) = machine.generate_coefficients(rng);
|
|
|
|
|
machines.insert(i, machine);
|
|
|
|
|
commitments.insert(i, these_commitments);
|
2022-04-21 21:36:18 -04:00
|
|
|
}
|
|
|
|
|
|
2022-05-24 21:41:14 -04:00
|
|
|
let mut secret_shares = HashMap::new();
|
2022-06-24 08:40:14 -04:00
|
|
|
let mut machines = machines.drain().map(|(l, machine)| {
|
|
|
|
|
let (machine, shares) = machine.generate_secret_shares(
|
|
|
|
|
rng,
|
2022-05-26 03:52:45 -04:00
|
|
|
// clone_without isn't necessary, as this machine's own data will be inserted without
|
|
|
|
|
// conflict, yet using it ensures the machine's own data is actually inserted as expected
|
2022-06-24 08:40:14 -04:00
|
|
|
clone_without(&commitments, &l)
|
|
|
|
|
).unwrap();
|
|
|
|
|
secret_shares.insert(l, shares);
|
|
|
|
|
(l, machine)
|
|
|
|
|
}).collect::<HashMap<_, _>>();
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-05-24 21:41:14 -04:00
|
|
|
let mut verification_shares = None;
|
2022-04-21 21:36:18 -04:00
|
|
|
let mut group_key = None;
|
2022-06-24 08:40:14 -04:00
|
|
|
machines.drain().map(|(i, machine)| {
|
2022-05-24 21:41:14 -04:00
|
|
|
let mut our_secret_shares = HashMap::new();
|
|
|
|
|
for (l, shares) in &secret_shares {
|
2022-06-24 08:40:14 -04:00
|
|
|
if i == *l {
|
2022-05-24 21:41:14 -04:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
our_secret_shares.insert(*l, shares[&i].clone());
|
|
|
|
|
}
|
2022-05-27 00:52:44 -04:00
|
|
|
let these_keys = machine.complete(rng, our_secret_shares).unwrap();
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-05-25 00:28:57 -04:00
|
|
|
// Verify the verification_shares are agreed upon
|
2022-05-24 21:41:14 -04:00
|
|
|
if verification_shares.is_none() {
|
|
|
|
|
verification_shares = Some(these_keys.verification_shares());
|
2022-04-21 21:36:18 -04:00
|
|
|
}
|
2022-05-24 21:41:14 -04:00
|
|
|
assert_eq!(verification_shares.as_ref().unwrap(), &these_keys.verification_shares());
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-05-25 00:28:57 -04:00
|
|
|
// Verify the group keys are agreed upon
|
2022-04-21 21:36:18 -04:00
|
|
|
if group_key.is_none() {
|
|
|
|
|
group_key = Some(these_keys.group_key());
|
|
|
|
|
}
|
|
|
|
|
assert_eq!(group_key.unwrap(), these_keys.group_key());
|
2022-05-24 21:41:14 -04:00
|
|
|
|
2022-06-24 08:40:14 -04:00
|
|
|
(i, Arc::new(these_keys))
|
|
|
|
|
}).collect::<HashMap<_, _>>()
|
2022-05-24 21:41:14 -04:00
|
|
|
}
|
|
|
|
|
|
2022-05-30 16:37:51 -04:00
|
|
|
pub fn recover<C: Curve>(keys: &HashMap<u16, MultisigKeys<C>>) -> C::F {
|
|
|
|
|
let first = keys.values().next().expect("no keys provided");
|
|
|
|
|
assert!(keys.len() >= first.params().t().into(), "not enough keys provided");
|
|
|
|
|
let included = keys.keys().cloned().collect::<Vec<_>>();
|
|
|
|
|
|
|
|
|
|
let group_private = keys.iter().fold(
|
|
|
|
|
C::F::zero(),
|
|
|
|
|
|accum, (i, keys)| accum + (keys.secret_share() * lagrange::<C::F>(*i, &included))
|
|
|
|
|
);
|
2022-06-03 23:22:08 -04:00
|
|
|
assert_eq!(C::GENERATOR_TABLE * group_private, first.group_key(), "failed to recover keys");
|
2022-05-30 16:37:51 -04:00
|
|
|
group_private
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-25 00:28:57 -04:00
|
|
|
pub fn algorithm_machines<R: RngCore, C: Curve, A: Algorithm<C>>(
|
|
|
|
|
rng: &mut R,
|
2022-05-24 21:41:14 -04:00
|
|
|
algorithm: A,
|
2022-06-05 07:33:15 -04:00
|
|
|
keys: &HashMap<u16, Arc<MultisigKeys<C>>>,
|
2022-05-25 00:28:57 -04:00
|
|
|
) -> HashMap<u16, AlgorithmMachine<C, A>> {
|
|
|
|
|
let mut included = vec![];
|
|
|
|
|
while included.len() < usize::from(keys[&1].params().t()) {
|
|
|
|
|
let n = u16::try_from((rng.next_u64() % u64::try_from(keys.len()).unwrap()) + 1).unwrap();
|
|
|
|
|
if included.contains(&n) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
included.push(n);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
keys.iter().filter_map(
|
|
|
|
|
|(i, keys)| if included.contains(&i) {
|
|
|
|
|
Some((
|
|
|
|
|
*i,
|
|
|
|
|
AlgorithmMachine::new(
|
|
|
|
|
algorithm.clone(),
|
|
|
|
|
keys.clone(),
|
|
|
|
|
&included.clone()
|
|
|
|
|
).unwrap()
|
|
|
|
|
))
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
).collect()
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-24 08:40:14 -04:00
|
|
|
pub fn sign<R: RngCore + CryptoRng, M: PreprocessMachine>(
|
2022-05-25 00:28:57 -04:00
|
|
|
rng: &mut R,
|
|
|
|
|
mut machines: HashMap<u16, M>,
|
|
|
|
|
msg: &[u8]
|
|
|
|
|
) -> M::Signature {
|
2022-05-24 21:41:14 -04:00
|
|
|
let mut commitments = HashMap::new();
|
2022-06-24 08:40:14 -04:00
|
|
|
let mut machines = machines.drain().map(|(i, machine)| {
|
|
|
|
|
let (machine, preprocess) = machine.preprocess(rng);
|
|
|
|
|
commitments.insert(i, preprocess);
|
|
|
|
|
(i, machine)
|
|
|
|
|
}).collect::<HashMap<_, _>>();
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-05-24 21:41:14 -04:00
|
|
|
let mut shares = HashMap::new();
|
2022-06-24 08:40:14 -04:00
|
|
|
let mut machines = machines.drain().map(|(i, machine)| {
|
|
|
|
|
let (machine, share) = machine.sign(clone_without(&commitments, &i), msg).unwrap();
|
|
|
|
|
shares.insert(i, share);
|
|
|
|
|
(i, machine)
|
|
|
|
|
}).collect::<HashMap<_, _>>();
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-05-24 21:41:14 -04:00
|
|
|
let mut signature = None;
|
2022-06-24 08:40:14 -04:00
|
|
|
for (i, machine) in machines.drain() {
|
|
|
|
|
let sig = machine.complete(clone_without(&shares, &i)).unwrap();
|
2022-05-24 21:41:14 -04:00
|
|
|
if signature.is_none() {
|
2022-05-25 00:28:57 -04:00
|
|
|
signature = Some(sig.clone());
|
2022-05-24 21:41:14 -04:00
|
|
|
}
|
2022-05-25 00:28:57 -04:00
|
|
|
assert_eq!(&sig, signature.as_ref().unwrap());
|
2022-05-24 21:41:14 -04:00
|
|
|
}
|
2022-05-25 00:28:57 -04:00
|
|
|
signature.unwrap()
|
2022-04-21 21:36:18 -04:00
|
|
|
}
|