mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 12:19:24 +00:00
Create dedicated message structures for FROST messages (#140)
* Create message types for FROST key gen Taking in reader borrows absolutely wasn't feasible. Now, proper types which can be read (and then passed directly, without a mutable borrow) exist for key_gen. sign coming next. * Move FROST signing to messages, not Readers/Writers/Vec<u8> Also takes the nonce handling code and makes a dedicated file for it, aiming to resolve complex types and make the code more legible by replacing its previously inlined state. * clippy * Update FROST tests * read_signature_share * Update the Monero library to the new FROST packages * Update processor to latest FROST * Tweaks to terminology and documentation
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use std::{marker::Send, io::Cursor, collections::HashMap};
|
||||
use std::{marker::Send, collections::HashMap};
|
||||
|
||||
use async_trait::async_trait;
|
||||
use thiserror::Error;
|
||||
@@ -18,7 +18,7 @@ pub enum NetworkError {}
|
||||
|
||||
#[async_trait]
|
||||
pub trait Network: Send {
|
||||
async fn round(&mut self, data: Vec<u8>) -> Result<HashMap<u16, Cursor<Vec<u8>>>, NetworkError>;
|
||||
async fn round(&mut self, data: Vec<u8>) -> Result<HashMap<u16, Vec<u8>>, NetworkError>;
|
||||
}
|
||||
|
||||
#[derive(Clone, Error, Debug)]
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
use std::{
|
||||
io::Cursor,
|
||||
sync::{Arc, RwLock},
|
||||
collections::HashMap,
|
||||
};
|
||||
@@ -19,7 +18,7 @@ struct LocalNetwork {
|
||||
i: u16,
|
||||
size: u16,
|
||||
round: usize,
|
||||
rounds: Arc<RwLock<Vec<HashMap<u16, Cursor<Vec<u8>>>>>>,
|
||||
rounds: Arc<RwLock<Vec<HashMap<u16, Vec<u8>>>>>,
|
||||
}
|
||||
|
||||
impl LocalNetwork {
|
||||
@@ -35,13 +34,13 @@ impl LocalNetwork {
|
||||
|
||||
#[async_trait]
|
||||
impl Network for LocalNetwork {
|
||||
async fn round(&mut self, data: Vec<u8>) -> Result<HashMap<u16, Cursor<Vec<u8>>>, NetworkError> {
|
||||
async fn round(&mut self, data: Vec<u8>) -> Result<HashMap<u16, Vec<u8>>, NetworkError> {
|
||||
{
|
||||
let mut rounds = self.rounds.write().unwrap();
|
||||
if rounds.len() == self.round {
|
||||
rounds.push(HashMap::new());
|
||||
}
|
||||
rounds[self.round].insert(self.i, Cursor::new(data));
|
||||
rounds[self.round].insert(self.i, data);
|
||||
}
|
||||
|
||||
while {
|
||||
|
||||
@@ -7,8 +7,8 @@ use group::GroupEncoding;
|
||||
use transcript::{Transcript, RecommendedTranscript};
|
||||
use frost::{
|
||||
curve::Curve,
|
||||
FrostKeys,
|
||||
sign::{PreprocessMachine, SignMachine, SignatureMachine},
|
||||
FrostError, FrostKeys,
|
||||
sign::{Writable, PreprocessMachine, SignMachine, SignatureMachine},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
@@ -343,10 +343,44 @@ impl<D: CoinDb, C: Coin> Wallet<D, C> {
|
||||
self.coin.attempt_send(prepared, &included).await.map_err(SignError::CoinError)?;
|
||||
|
||||
let (attempt, commitments) = attempt.preprocess(&mut OsRng);
|
||||
let commitments = network.round(commitments).await.map_err(SignError::NetworkError)?;
|
||||
let commitments = network
|
||||
.round({
|
||||
let mut buf = vec![];
|
||||
commitments.write(&mut buf).unwrap();
|
||||
buf
|
||||
})
|
||||
.await
|
||||
.map_err(SignError::NetworkError)?
|
||||
.drain()
|
||||
.map(|(validator, preprocess)| {
|
||||
Ok((
|
||||
validator,
|
||||
attempt
|
||||
.read_preprocess::<&[u8]>(&mut preprocess.as_ref())
|
||||
.map_err(|_| SignError::FrostError(FrostError::InvalidPreprocess(validator)))?,
|
||||
))
|
||||
})
|
||||
.collect::<Result<HashMap<_, _>, _>>()?;
|
||||
|
||||
let (attempt, share) = attempt.sign(commitments, b"").map_err(SignError::FrostError)?;
|
||||
let shares = network.round(share).await.map_err(SignError::NetworkError)?;
|
||||
let shares = network
|
||||
.round({
|
||||
let mut buf = vec![];
|
||||
share.write(&mut buf).unwrap();
|
||||
buf
|
||||
})
|
||||
.await
|
||||
.map_err(SignError::NetworkError)?
|
||||
.drain()
|
||||
.map(|(validator, share)| {
|
||||
Ok((
|
||||
validator,
|
||||
attempt
|
||||
.read_share::<&[u8]>(&mut share.as_ref())
|
||||
.map_err(|_| SignError::FrostError(FrostError::InvalidShare(validator)))?,
|
||||
))
|
||||
})
|
||||
.collect::<Result<HashMap<_, _>, _>>()?;
|
||||
|
||||
let tx = attempt.complete(shares).map_err(SignError::FrostError)?;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user