2022-04-22 01:36:18 +00:00
|
|
|
#![cfg(feature = "multisig")]
|
|
|
|
|
2022-05-25 01:41:14 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2022-04-22 01:36:18 +00:00
|
|
|
use rand::rngs::OsRng;
|
|
|
|
|
|
|
|
use ff::Field;
|
2022-04-30 08:32:19 +00:00
|
|
|
use dalek_ff_group::{ED25519_BASEPOINT_TABLE, Scalar};
|
2022-04-22 01:36:18 +00:00
|
|
|
|
2022-04-30 02:36:43 +00:00
|
|
|
pub use frost::{
|
2022-04-29 19:28:04 +00:00
|
|
|
FrostError, MultisigParams, MultisigKeys,
|
2022-05-25 01:41:14 +00:00
|
|
|
lagrange, key_gen, algorithm::Algorithm, sign
|
2022-04-22 01:36:18 +00:00
|
|
|
};
|
|
|
|
|
2022-04-28 02:48:58 +00:00
|
|
|
use monero_serai::frost::Ed25519;
|
2022-04-22 01:36:18 +00:00
|
|
|
|
2022-05-25 01:41:14 +00:00
|
|
|
pub const THRESHOLD: u16 = 3;
|
|
|
|
pub const PARTICIPANTS: u16 = 5;
|
|
|
|
|
|
|
|
fn clone_without<K: Clone + std::cmp::Eq + std::hash::Hash, V: Clone>(
|
|
|
|
map: &HashMap<K, V>,
|
|
|
|
without: &K
|
|
|
|
) -> HashMap<K, V> {
|
|
|
|
let mut res = map.clone();
|
|
|
|
res.remove(without).unwrap();
|
|
|
|
res
|
|
|
|
}
|
2022-04-29 19:28:04 +00:00
|
|
|
|
2022-05-25 01:41:14 +00:00
|
|
|
pub fn generate_keys() -> (HashMap<u16, MultisigKeys<Ed25519>>, Scalar) {
|
|
|
|
let mut params = HashMap::new();
|
|
|
|
let mut machines = HashMap::new();
|
|
|
|
let mut commitments = HashMap::new();
|
2022-04-29 01:47:25 +00:00
|
|
|
for i in 1 ..= PARTICIPANTS {
|
2022-05-25 01:41:14 +00:00
|
|
|
params.insert(
|
|
|
|
i,
|
2022-04-29 01:47:25 +00:00
|
|
|
MultisigParams::new(THRESHOLD, PARTICIPANTS, i).unwrap()
|
2022-04-22 01:36:18 +00:00
|
|
|
);
|
2022-05-25 01:41:14 +00:00
|
|
|
machines.insert(
|
|
|
|
i,
|
2022-04-22 01:36:18 +00:00
|
|
|
key_gen::StateMachine::<Ed25519>::new(
|
2022-05-25 01:41:14 +00:00
|
|
|
params[&i],
|
2022-04-22 01:36:18 +00:00
|
|
|
"monero-sign-rs test suite".to_string()
|
|
|
|
)
|
|
|
|
);
|
2022-05-25 01:41:14 +00:00
|
|
|
commitments.insert(i, machines.get_mut(&i).unwrap().generate_coefficients(&mut OsRng).unwrap());
|
2022-04-22 01:36:18 +00:00
|
|
|
}
|
|
|
|
|
2022-05-25 01:41:14 +00:00
|
|
|
let mut secret_shares = HashMap::new();
|
|
|
|
for (i, machine) in machines.iter_mut() {
|
|
|
|
secret_shares.insert(
|
|
|
|
*i,
|
|
|
|
machine.generate_secret_shares(&mut OsRng, clone_without(&commitments, i)).unwrap()
|
2022-04-22 01:36:18 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-05-25 01:41:14 +00: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());
|
|
|
|
}
|
2022-05-27 04:52:44 +00:00
|
|
|
keys.insert(*i, machine.complete(&mut OsRng, our_secret_shares).unwrap().clone());
|
2022-04-22 01:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut group_private = Scalar::zero();
|
2022-04-29 01:47:25 +00:00
|
|
|
for i in 1 ..= THRESHOLD {
|
2022-05-25 01:41:14 +00:00
|
|
|
group_private += keys[&i].secret_share() * lagrange::<Scalar>(i, &(1 ..= THRESHOLD).collect::<Vec<_>>());
|
2022-04-22 01:36:18 +00:00
|
|
|
}
|
2022-05-25 01:41:14 +00:00
|
|
|
assert_eq!(&ED25519_BASEPOINT_TABLE * group_private, keys[&1].group_key());
|
2022-04-22 01:36:18 +00:00
|
|
|
|
|
|
|
(keys, group_private)
|
|
|
|
}
|
2022-04-29 01:47:25 +00:00
|
|
|
|
2022-05-25 01:41:14 +00:00
|
|
|
pub fn sign<S, M: sign::StateMachine<Signature = S>>(machines: &mut HashMap<u16, M>, msg: &[u8]) -> Vec<S> {
|
|
|
|
assert!(machines.len() >= THRESHOLD.into());
|
2022-04-29 01:47:25 +00:00
|
|
|
|
2022-05-25 01:41:14 +00:00
|
|
|
let mut commitments = HashMap::new();
|
|
|
|
for (i, machine) in machines.iter_mut() {
|
|
|
|
commitments.insert(*i, machine.preprocess(&mut OsRng).unwrap());
|
2022-04-29 01:47:25 +00:00
|
|
|
}
|
|
|
|
|
2022-05-25 01:41:14 +00:00
|
|
|
let mut shares = HashMap::new();
|
|
|
|
for (i, machine) in machines.iter_mut() {
|
|
|
|
shares.insert(*i, machine.sign(clone_without(&commitments, i), msg).unwrap());
|
2022-04-29 01:47:25 +00:00
|
|
|
}
|
|
|
|
|
2022-05-25 01:41:14 +00:00
|
|
|
let mut res = vec![];
|
|
|
|
for (i, machine) in machines.iter_mut() {
|
|
|
|
res.push(machine.complete(clone_without(&shares, i)).unwrap())
|
2022-04-29 01:47:25 +00:00
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|