2022-05-24 21:41:14 -04:00
|
|
|
use std::{rc::Rc, 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-05-25 00:28:57 -04:00
|
|
|
use crate::{
|
2022-04-21 21:36:18 -04:00
|
|
|
Curve,
|
|
|
|
|
MultisigParams, MultisigKeys,
|
|
|
|
|
key_gen,
|
2022-05-25 00:28:57 -04:00
|
|
|
algorithm::Algorithm,
|
2022-04-29 22:36:43 -04:00
|
|
|
sign::{StateMachine, AlgorithmMachine}
|
2022-04-21 21:36:18 -04:00
|
|
|
};
|
|
|
|
|
|
2022-05-25 00:28:57 -04:00
|
|
|
// Internal tests
|
|
|
|
|
mod schnorr;
|
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-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
|
|
|
|
|
) -> HashMap<u16, Rc<MultisigKeys<C>>> {
|
2022-05-24 21:41:14 -04:00
|
|
|
let mut params = HashMap::new();
|
|
|
|
|
let mut machines = HashMap::new();
|
|
|
|
|
|
|
|
|
|
let mut commitments = HashMap::new();
|
2022-04-21 21:36:18 -04:00
|
|
|
for i in 1 ..= PARTICIPANTS {
|
2022-05-24 21:41:14 -04:00
|
|
|
params.insert(
|
|
|
|
|
i,
|
2022-04-21 21:36:18 -04:00
|
|
|
MultisigParams::new(
|
2022-05-25 00:28:57 -04:00
|
|
|
THRESHOLD,
|
2022-04-21 21:36:18 -04:00
|
|
|
PARTICIPANTS,
|
|
|
|
|
i
|
|
|
|
|
).unwrap()
|
|
|
|
|
);
|
2022-05-24 21:41:14 -04:00
|
|
|
machines.insert(
|
|
|
|
|
i,
|
|
|
|
|
key_gen::StateMachine::<C>::new(
|
|
|
|
|
params[&i],
|
|
|
|
|
"FROST test key_gen".to_string()
|
2022-04-21 21:36:18 -04:00
|
|
|
)
|
|
|
|
|
);
|
2022-05-24 21:41:14 -04:00
|
|
|
commitments.insert(
|
|
|
|
|
i,
|
2022-05-25 00:28:57 -04:00
|
|
|
machines.get_mut(&i).unwrap().generate_coefficients(rng).unwrap()
|
2022-05-24 21:41:14 -04:00
|
|
|
);
|
2022-04-21 21:36:18 -04:00
|
|
|
}
|
|
|
|
|
|
2022-05-24 21:41:14 -04:00
|
|
|
let mut secret_shares = HashMap::new();
|
|
|
|
|
for (l, machine) in machines.iter_mut() {
|
|
|
|
|
secret_shares.insert(
|
|
|
|
|
*l,
|
2022-05-25 00:28:57 -04:00
|
|
|
machine.generate_secret_shares(rng, clone_without(&commitments, l)).unwrap()
|
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-05-24 21:41:14 -04:00
|
|
|
let mut keys = HashMap::new();
|
|
|
|
|
for (i, machine) in machines.iter_mut() {
|
|
|
|
|
let mut our_secret_shares = HashMap::new();
|
|
|
|
|
for (l, shares) in &secret_shares {
|
|
|
|
|
if i == l {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
our_secret_shares.insert(*l, shares[&i].clone());
|
|
|
|
|
}
|
|
|
|
|
let these_keys = machine.complete(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-05-25 00:28:57 -04:00
|
|
|
keys.insert(*i, Rc::new(these_keys));
|
2022-05-24 21:41:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
keys
|
|
|
|
|
}
|
|
|
|
|
|
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-05-25 00:28:57 -04:00
|
|
|
keys: &HashMap<u16, Rc<MultisigKeys<C>>>,
|
|
|
|
|
) -> 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()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn sign<R: RngCore + CryptoRng, M: StateMachine>(
|
|
|
|
|
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-05-25 00:28:57 -04:00
|
|
|
for (i, machine) in machines.iter_mut() {
|
|
|
|
|
commitments.insert(*i, machine.preprocess(rng).unwrap());
|
2022-04-21 21:36:18 -04:00
|
|
|
}
|
|
|
|
|
|
2022-05-24 21:41:14 -04:00
|
|
|
let mut shares = HashMap::new();
|
|
|
|
|
for (i, machine) in machines.iter_mut() {
|
|
|
|
|
shares.insert(
|
|
|
|
|
*i,
|
2022-05-25 00:28:57 -04:00
|
|
|
machine.sign(clone_without(&commitments, i), msg).unwrap()
|
2022-05-24 21:41:14 -04:00
|
|
|
);
|
|
|
|
|
}
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-05-24 21:41:14 -04:00
|
|
|
let mut signature = None;
|
|
|
|
|
for (i, machine) in machines.iter_mut() {
|
|
|
|
|
let sig = machine.complete(clone_without(&shares, i)).unwrap();
|
|
|
|
|
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
|
|
|
}
|