2022-04-22 01:36:18 +00:00
|
|
|
#![cfg(feature = "multisig")]
|
|
|
|
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
2022-04-29 19:28:04 +00:00
|
|
|
use rand_core::{RngCore, CryptoRng};
|
2022-04-22 01:36:18 +00:00
|
|
|
use rand::rngs::OsRng;
|
|
|
|
|
|
|
|
use ff::Field;
|
2022-04-29 19:28:04 +00:00
|
|
|
use dalek_ff_group::{ED25519_BASEPOINT_TABLE, Scalar, EdwardsPoint};
|
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-04-29 01:47:25 +00:00
|
|
|
key_gen, algorithm::Algorithm, sign::{self, lagrange}
|
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-04-29 01:47:25 +00:00
|
|
|
pub const THRESHOLD: usize = 5;
|
|
|
|
pub const PARTICIPANTS: usize = 8;
|
|
|
|
|
2022-04-29 19:28:04 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct DummyAlgorithm;
|
|
|
|
impl Algorithm<Ed25519> for DummyAlgorithm {
|
|
|
|
type Signature = ();
|
|
|
|
|
|
|
|
fn addendum_commit_len() -> usize { unimplemented!() }
|
|
|
|
|
|
|
|
fn preprocess_addendum<R: RngCore + CryptoRng>(
|
|
|
|
_: &mut R,
|
|
|
|
_: &sign::ParamsView<Ed25519>,
|
|
|
|
_: &[Scalar; 2],
|
|
|
|
) -> Vec<u8> { unimplemented!() }
|
|
|
|
|
|
|
|
fn process_addendum(
|
|
|
|
&mut self,
|
|
|
|
_: &sign::ParamsView<Ed25519>,
|
|
|
|
_: usize,
|
|
|
|
_: &[EdwardsPoint; 2],
|
|
|
|
_: &[u8],
|
|
|
|
) -> Result<(), FrostError> { unimplemented!() }
|
|
|
|
|
|
|
|
fn context(&self) -> Vec<u8> { unimplemented!() }
|
|
|
|
|
|
|
|
fn sign_share(
|
|
|
|
&mut self,
|
|
|
|
_: &sign::ParamsView<Ed25519>,
|
|
|
|
_: EdwardsPoint,
|
|
|
|
_: Scalar,
|
2022-04-30 02:03:34 +00:00
|
|
|
_: Scalar,
|
2022-04-29 19:28:04 +00:00
|
|
|
_: &[u8],
|
|
|
|
) -> Scalar { unimplemented!() }
|
|
|
|
|
|
|
|
fn verify(&self, _: EdwardsPoint, _: EdwardsPoint, _: Scalar) -> Option<Self::Signature> { unimplemented!() }
|
|
|
|
|
|
|
|
fn verify_share(
|
|
|
|
&self,
|
|
|
|
_: EdwardsPoint,
|
|
|
|
_: EdwardsPoint,
|
|
|
|
_: Scalar,
|
|
|
|
) -> bool { unimplemented!() }
|
|
|
|
}
|
|
|
|
|
2022-04-29 01:47:25 +00:00
|
|
|
pub fn generate_keys() -> (Vec<Rc<MultisigKeys<Ed25519>>>, Scalar) {
|
2022-04-22 01:36:18 +00:00
|
|
|
let mut params = vec![];
|
|
|
|
let mut machines = vec![];
|
|
|
|
let mut commitments = vec![vec![]];
|
2022-04-29 01:47:25 +00:00
|
|
|
for i in 1 ..= PARTICIPANTS {
|
2022-04-22 01:36:18 +00:00
|
|
|
params.push(
|
2022-04-29 01:47:25 +00:00
|
|
|
MultisigParams::new(THRESHOLD, PARTICIPANTS, i).unwrap()
|
2022-04-22 01:36:18 +00:00
|
|
|
);
|
|
|
|
machines.push(
|
|
|
|
key_gen::StateMachine::<Ed25519>::new(
|
|
|
|
params[i - 1],
|
|
|
|
"monero-sign-rs test suite".to_string()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
commitments.push(machines[i - 1].generate_coefficients(&mut OsRng).unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut secret_shares = vec![];
|
2022-04-29 01:47:25 +00:00
|
|
|
for i in 1 ..= PARTICIPANTS {
|
2022-04-22 01:36:18 +00:00
|
|
|
secret_shares.push(
|
|
|
|
machines[i - 1].generate_secret_shares(
|
|
|
|
&mut OsRng,
|
|
|
|
commitments
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(idx, commitments)| if idx == i { vec![] } else { commitments.to_vec() })
|
|
|
|
.collect()
|
|
|
|
).unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut keys = vec![];
|
2022-04-29 01:47:25 +00:00
|
|
|
for i in 1 ..= PARTICIPANTS {
|
2022-04-22 01:36:18 +00:00
|
|
|
let mut our_secret_shares = vec![vec![]];
|
|
|
|
our_secret_shares.extend(
|
|
|
|
secret_shares.iter().map(|shares| shares[i].clone()).collect::<Vec<Vec<u8>>>()
|
|
|
|
);
|
|
|
|
keys.push(Rc::new(machines[i - 1].complete(our_secret_shares).unwrap().clone()));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut group_private = Scalar::zero();
|
2022-04-29 01:47:25 +00:00
|
|
|
for i in 1 ..= THRESHOLD {
|
|
|
|
group_private += keys[i - 1].secret_share() * lagrange::<Scalar>(
|
|
|
|
i,
|
|
|
|
&(1 ..= THRESHOLD).collect::<Vec<usize>>()
|
|
|
|
);
|
2022-04-22 01:36:18 +00:00
|
|
|
}
|
|
|
|
assert_eq!(&ED25519_BASEPOINT_TABLE * group_private, keys[0].group_key());
|
|
|
|
|
|
|
|
(keys, group_private)
|
|
|
|
}
|
2022-04-29 01:47:25 +00:00
|
|
|
|
|
|
|
#[allow(dead_code)] // Currently has some false positive
|
2022-04-30 02:36:43 +00:00
|
|
|
pub fn sign<S, M: sign::StateMachine<Signature = S>>(
|
|
|
|
machines: &mut Vec<M>,
|
2022-04-29 01:47:25 +00:00
|
|
|
keys: Vec<Rc<MultisigKeys<Ed25519>>>
|
|
|
|
) -> Vec<S> {
|
2022-04-30 02:36:43 +00:00
|
|
|
assert!(machines.len() >= THRESHOLD);
|
|
|
|
assert!(keys.len() >= machines.len());
|
2022-04-29 01:47:25 +00:00
|
|
|
|
|
|
|
let mut commitments = Vec::with_capacity(PARTICIPANTS + 1);
|
|
|
|
commitments.resize(PARTICIPANTS + 1, None);
|
|
|
|
for i in 1 ..= THRESHOLD {
|
|
|
|
commitments[i] = Some(machines[i - 1].preprocess(&mut OsRng).unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut shares = Vec::with_capacity(PARTICIPANTS + 1);
|
|
|
|
shares.resize(PARTICIPANTS + 1, None);
|
|
|
|
for i in 1 ..= THRESHOLD {
|
|
|
|
shares[i] = Some(
|
|
|
|
machines[i - 1].sign(
|
|
|
|
&commitments
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(idx, value)| if idx == i { None } else { value.to_owned() })
|
|
|
|
.collect::<Vec<Option<Vec<u8>>>>(),
|
|
|
|
&vec![]
|
|
|
|
).unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut res = Vec::with_capacity(THRESHOLD);
|
|
|
|
for i in 1 ..= THRESHOLD {
|
|
|
|
res.push(
|
|
|
|
machines[i - 1].complete(
|
|
|
|
&shares
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(idx, value)| if idx == i { None } else { value.to_owned() })
|
|
|
|
.collect::<Vec<Option<Vec<u8>>>>()
|
|
|
|
).unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|