mirror of
https://github.com/serai-dex/serai.git
synced 2025-03-22 15:19:06 +00:00
Remove Monero's test FROST file in favor of FROST"s exposed test files
Moves private key recovery for given keys into FROST.
This commit is contained in:
parent
ce4c899422
commit
bfa15283f5
3 changed files with 23 additions and 105 deletions
|
@ -1,95 +0,0 @@
|
||||||
#![cfg(feature = "multisig")]
|
|
||||||
|
|
||||||
use std::collections::HashMap;
|
|
||||||
|
|
||||||
use rand::rngs::OsRng;
|
|
||||||
|
|
||||||
use ff::Field;
|
|
||||||
use dalek_ff_group::{ED25519_BASEPOINT_TABLE, Scalar};
|
|
||||||
|
|
||||||
pub use frost::{
|
|
||||||
FrostError, MultisigParams, MultisigKeys,
|
|
||||||
lagrange, key_gen, algorithm::Algorithm, sign
|
|
||||||
};
|
|
||||||
|
|
||||||
use monero_serai::frost::Ed25519;
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn generate_keys() -> (HashMap<u16, MultisigKeys<Ed25519>>, Scalar) {
|
|
||||||
let mut params = HashMap::new();
|
|
||||||
let mut machines = HashMap::new();
|
|
||||||
let mut commitments = HashMap::new();
|
|
||||||
for i in 1 ..= PARTICIPANTS {
|
|
||||||
params.insert(
|
|
||||||
i,
|
|
||||||
MultisigParams::new(THRESHOLD, PARTICIPANTS, i).unwrap()
|
|
||||||
);
|
|
||||||
machines.insert(
|
|
||||||
i,
|
|
||||||
key_gen::StateMachine::<Ed25519>::new(
|
|
||||||
params[&i],
|
|
||||||
"monero-sign-rs test suite".to_string()
|
|
||||||
)
|
|
||||||
);
|
|
||||||
commitments.insert(i, machines.get_mut(&i).unwrap().generate_coefficients(&mut OsRng).unwrap());
|
|
||||||
}
|
|
||||||
|
|
||||||
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()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
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());
|
|
||||||
}
|
|
||||||
keys.insert(*i, machine.complete(&mut OsRng, our_secret_shares).unwrap().clone());
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut group_private = Scalar::zero();
|
|
||||||
for i in 1 ..= THRESHOLD {
|
|
||||||
group_private += keys[&i].secret_share() * lagrange::<Scalar>(i, &(1 ..= THRESHOLD).collect::<Vec<_>>());
|
|
||||||
}
|
|
||||||
assert_eq!(&ED25519_BASEPOINT_TABLE * group_private, keys[&1].group_key());
|
|
||||||
|
|
||||||
(keys, group_private)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn sign<S, M: sign::StateMachine<Signature = S>>(machines: &mut HashMap<u16, M>, msg: &[u8]) -> Vec<S> {
|
|
||||||
assert!(machines.len() >= THRESHOLD.into());
|
|
||||||
|
|
||||||
let mut commitments = HashMap::new();
|
|
||||||
for (i, machine) in machines.iter_mut() {
|
|
||||||
commitments.insert(*i, machine.preprocess(&mut OsRng).unwrap());
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut shares = HashMap::new();
|
|
||||||
for (i, machine) in machines.iter_mut() {
|
|
||||||
shares.insert(*i, machine.sign(clone_without(&commitments, i), msg).unwrap());
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut res = vec![];
|
|
||||||
for (i, machine) in machines.iter_mut() {
|
|
||||||
res.push(machine.complete(clone_without(&shares, i)).unwrap())
|
|
||||||
}
|
|
||||||
res
|
|
||||||
}
|
|
|
@ -8,8 +8,11 @@ use rand::rngs::OsRng;
|
||||||
use blake2::{digest::Update, Digest, Blake2b512};
|
use blake2::{digest::Update, Digest, Blake2b512};
|
||||||
|
|
||||||
use curve25519_dalek::constants::ED25519_BASEPOINT_TABLE;
|
use curve25519_dalek::constants::ED25519_BASEPOINT_TABLE;
|
||||||
|
|
||||||
#[cfg(feature = "multisig")]
|
#[cfg(feature = "multisig")]
|
||||||
use dalek_ff_group::Scalar;
|
use dalek_ff_group::Scalar;
|
||||||
|
#[cfg(feature = "multisig")]
|
||||||
|
use frost::tests::{THRESHOLD, key_gen, sign};
|
||||||
|
|
||||||
use monero::{
|
use monero::{
|
||||||
network::Network,
|
network::Network,
|
||||||
|
@ -22,9 +25,7 @@ mod rpc;
|
||||||
use crate::rpc::{rpc, mine_block};
|
use crate::rpc::{rpc, mine_block};
|
||||||
|
|
||||||
#[cfg(feature = "multisig")]
|
#[cfg(feature = "multisig")]
|
||||||
mod frost;
|
use monero_serai::frost::Ed25519;
|
||||||
#[cfg(feature = "multisig")]
|
|
||||||
use crate::frost::{THRESHOLD, generate_keys, sign};
|
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref SEQUENTIAL: Mutex<()> = Mutex::new(());
|
static ref SEQUENTIAL: Mutex<()> = Mutex::new(());
|
||||||
|
@ -59,7 +60,7 @@ async fn send_core(test: usize, multisig: bool) {
|
||||||
let mut spend_pub = &spend * &ED25519_BASEPOINT_TABLE;
|
let mut spend_pub = &spend * &ED25519_BASEPOINT_TABLE;
|
||||||
|
|
||||||
#[cfg(feature = "multisig")]
|
#[cfg(feature = "multisig")]
|
||||||
let (keys, _) = generate_keys();
|
let keys = key_gen::<_, Ed25519>(&mut OsRng);
|
||||||
|
|
||||||
if multisig {
|
if multisig {
|
||||||
#[cfg(not(feature = "multisig"))]
|
#[cfg(not(feature = "multisig"))]
|
||||||
|
@ -148,17 +149,13 @@ async fn send_core(test: usize, multisig: bool) {
|
||||||
&mut OsRng,
|
&mut OsRng,
|
||||||
&rpc,
|
&rpc,
|
||||||
rpc.get_height().await.unwrap() - 10,
|
rpc.get_height().await.unwrap() - 10,
|
||||||
keys[&i].clone(),
|
(*keys[&i]).clone(),
|
||||||
(1 ..= THRESHOLD).collect::<Vec<_>>()
|
(1 ..= THRESHOLD).collect::<Vec<_>>()
|
||||||
).await.unwrap()
|
).await.unwrap()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut txs = sign(&mut machines, &vec![]);
|
tx = Some(sign(&mut OsRng, machines, &vec![]));
|
||||||
for s in 1 .. THRESHOLD {
|
|
||||||
assert_eq!(txs[usize::from(s)].hash(), txs[0].hash());
|
|
||||||
}
|
|
||||||
tx = Some(txs.swap_remove(0));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,9 +2,12 @@ use std::{rc::Rc, collections::HashMap};
|
||||||
|
|
||||||
use rand_core::{RngCore, CryptoRng};
|
use rand_core::{RngCore, CryptoRng};
|
||||||
|
|
||||||
|
use ff::Field;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Curve,
|
Curve,
|
||||||
MultisigParams, MultisigKeys,
|
MultisigParams, MultisigKeys,
|
||||||
|
lagrange,
|
||||||
key_gen,
|
key_gen,
|
||||||
algorithm::Algorithm,
|
algorithm::Algorithm,
|
||||||
sign::{StateMachine, AlgorithmMachine}
|
sign::{StateMachine, AlgorithmMachine}
|
||||||
|
@ -102,6 +105,19 @@ pub fn key_gen<R: RngCore + CryptoRng, C: Curve>(
|
||||||
keys
|
keys
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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))
|
||||||
|
);
|
||||||
|
assert_eq!(C::generator_table() * group_private, first.group_key(), "failed to recover keys");
|
||||||
|
group_private
|
||||||
|
}
|
||||||
|
|
||||||
pub fn algorithm_machines<R: RngCore, C: Curve, A: Algorithm<C>>(
|
pub fn algorithm_machines<R: RngCore, C: Curve, A: Algorithm<C>>(
|
||||||
rng: &mut R,
|
rng: &mut R,
|
||||||
algorithm: A,
|
algorithm: A,
|
||||||
|
|
Loading…
Reference in a new issue