mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-10 05:09:22 +00:00
Minor SignatureScheme API changes
This commit is contained in:
@@ -46,7 +46,7 @@ pub trait SignatureScheme: Send + Sync {
|
|||||||
fn sign(&self, msg: &[u8]) -> Self::Signature;
|
fn sign(&self, msg: &[u8]) -> Self::Signature;
|
||||||
/// Verify a signature from the validator in question.
|
/// Verify a signature from the validator in question.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
fn verify(&self, validator: Self::ValidatorId, msg: &[u8], sig: Self::Signature) -> bool;
|
fn verify(&self, validator: Self::ValidatorId, msg: &[u8], sig: &Self::Signature) -> bool;
|
||||||
|
|
||||||
/// Aggregate signatures.
|
/// Aggregate signatures.
|
||||||
fn aggregate(sigs: &[Self::Signature]) -> Self::AggregateSignature;
|
fn aggregate(sigs: &[Self::Signature]) -> Self::AggregateSignature;
|
||||||
@@ -54,8 +54,8 @@ pub trait SignatureScheme: Send + Sync {
|
|||||||
#[must_use]
|
#[must_use]
|
||||||
fn verify_aggregate(
|
fn verify_aggregate(
|
||||||
&self,
|
&self,
|
||||||
msg: &[u8],
|
|
||||||
signers: &[Self::ValidatorId],
|
signers: &[Self::ValidatorId],
|
||||||
|
msg: &[u8],
|
||||||
sig: &Self::AggregateSignature,
|
sig: &Self::AggregateSignature,
|
||||||
) -> bool;
|
) -> bool;
|
||||||
}
|
}
|
||||||
@@ -142,8 +142,8 @@ pub trait Network: Send + Sync {
|
|||||||
commit: &Commit<Self::SignatureScheme>,
|
commit: &Commit<Self::SignatureScheme>,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
if !self.signature_scheme().verify_aggregate(
|
if !self.signature_scheme().verify_aggregate(
|
||||||
&commit_msg(commit.round, id.as_ref()),
|
|
||||||
&commit.validators,
|
&commit.validators,
|
||||||
|
&commit_msg(commit.round, id.as_ref()),
|
||||||
&commit.signature,
|
&commit.signature,
|
||||||
) {
|
) {
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -278,7 +278,7 @@ impl<N: Network + 'static> TendermintMachine<N> {
|
|||||||
loop {
|
loop {
|
||||||
match msg_recv.try_recv() {
|
match msg_recv.try_recv() {
|
||||||
Ok(msg) => {
|
Ok(msg) => {
|
||||||
if !machine.signer.verify(msg.msg.sender, &msg.msg.encode(), msg.sig) {
|
if !machine.signer.verify(msg.msg.sender, &msg.msg.encode(), &msg.sig) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
machine.queue.push((false, msg.msg));
|
machine.queue.push((false, msg.msg));
|
||||||
@@ -345,7 +345,7 @@ impl<N: Network + 'static> TendermintMachine<N> {
|
|||||||
) -> Result<Option<N::Block>, TendermintError<N::ValidatorId>> {
|
) -> Result<Option<N::Block>, TendermintError<N::ValidatorId>> {
|
||||||
// Verify the signature if this is a precommit
|
// Verify the signature if this is a precommit
|
||||||
if let Data::Precommit(Some((id, sig))) = &msg.data {
|
if let Data::Precommit(Some((id, sig))) = &msg.data {
|
||||||
if !self.signer.verify(msg.sender, &commit_msg(msg.round, id.as_ref()), sig.clone()) {
|
if !self.signer.verify(msg.sender, &commit_msg(msg.round, id.as_ref()), sig) {
|
||||||
// Since we verified this validator actually sent the message, they're malicious
|
// Since we verified this validator actually sent the message, they're malicious
|
||||||
Err(TendermintError::Malicious(msg.sender))?;
|
Err(TendermintError::Malicious(msg.sender))?;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
use std::{sync::Arc, time::SystemTime};
|
use std::{
|
||||||
|
sync::Arc,
|
||||||
|
time::{SystemTime, Duration},
|
||||||
|
};
|
||||||
|
|
||||||
use parity_scale_codec::{Encode, Decode};
|
use parity_scale_codec::{Encode, Decode};
|
||||||
|
|
||||||
use tokio::sync::RwLock;
|
use tokio::{sync::RwLock, time::sleep};
|
||||||
|
|
||||||
use tendermint_machine::{ext::*, SignedMessage, TendermintMachine, TendermintHandle};
|
use tendermint_machine::{ext::*, SignedMessage, TendermintMachine, TendermintHandle};
|
||||||
|
|
||||||
@@ -23,7 +26,7 @@ impl SignatureScheme for TestSignatureScheme {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
fn verify(&self, validator: u16, msg: &[u8], sig: [u8; 32]) -> bool {
|
fn verify(&self, validator: u16, msg: &[u8], sig: &[u8; 32]) -> bool {
|
||||||
(sig[.. 2] == validator.to_le_bytes()) && (&sig[2 ..] == &[msg, &[0; 30]].concat()[.. 30])
|
(sig[.. 2] == validator.to_le_bytes()) && (&sig[2 ..] == &[msg, &[0; 30]].concat()[.. 30])
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,13 +37,13 @@ impl SignatureScheme for TestSignatureScheme {
|
|||||||
#[must_use]
|
#[must_use]
|
||||||
fn verify_aggregate(
|
fn verify_aggregate(
|
||||||
&self,
|
&self,
|
||||||
msg: &[u8],
|
|
||||||
signers: &[TestValidatorId],
|
signers: &[TestValidatorId],
|
||||||
|
msg: &[u8],
|
||||||
sigs: &Vec<[u8; 32]>,
|
sigs: &Vec<[u8; 32]>,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
assert_eq!(signers.len(), sigs.len());
|
assert_eq!(signers.len(), sigs.len());
|
||||||
for sig in signers.iter().zip(sigs.iter()) {
|
for sig in signers.iter().zip(sigs.iter()) {
|
||||||
assert!(self.verify(*sig.0, msg, *sig.1));
|
assert!(self.verify(*sig.0, msg, sig.1));
|
||||||
}
|
}
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
@@ -140,7 +143,7 @@ impl TestNetwork {
|
|||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test() {
|
async fn test() {
|
||||||
TestNetwork::new(4).await;
|
TestNetwork::new(4).await;
|
||||||
for _ in 0 .. 100 {
|
for _ in 0 .. 10 {
|
||||||
tokio::task::yield_now().await;
|
sleep(Duration::from_secs(1)).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user