Minor SignatureScheme API changes

This commit is contained in:
Luke Parker 2022-10-20 03:40:46 -04:00
parent 3c6ea6e55d
commit 9db42f7d83
No known key found for this signature in database
GPG key ID: F9F1386DB1E119B6
3 changed files with 15 additions and 12 deletions

View file

@ -46,7 +46,7 @@ pub trait SignatureScheme: Send + Sync {
fn sign(&self, msg: &[u8]) -> Self::Signature;
/// Verify a signature from the validator in question.
#[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.
fn aggregate(sigs: &[Self::Signature]) -> Self::AggregateSignature;
@ -54,8 +54,8 @@ pub trait SignatureScheme: Send + Sync {
#[must_use]
fn verify_aggregate(
&self,
msg: &[u8],
signers: &[Self::ValidatorId],
msg: &[u8],
sig: &Self::AggregateSignature,
) -> bool;
}
@ -142,8 +142,8 @@ pub trait Network: Send + Sync {
commit: &Commit<Self::SignatureScheme>,
) -> bool {
if !self.signature_scheme().verify_aggregate(
&commit_msg(commit.round, id.as_ref()),
&commit.validators,
&commit_msg(commit.round, id.as_ref()),
&commit.signature,
) {
return false;

View file

@ -278,7 +278,7 @@ impl<N: Network + 'static> TendermintMachine<N> {
loop {
match msg_recv.try_recv() {
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;
}
machine.queue.push((false, msg.msg));
@ -345,7 +345,7 @@ impl<N: Network + 'static> TendermintMachine<N> {
) -> Result<Option<N::Block>, TendermintError<N::ValidatorId>> {
// Verify the signature if this is a precommit
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
Err(TendermintError::Malicious(msg.sender))?;
}

View file

@ -1,8 +1,11 @@
use std::{sync::Arc, time::SystemTime};
use std::{
sync::Arc,
time::{SystemTime, Duration},
};
use parity_scale_codec::{Encode, Decode};
use tokio::sync::RwLock;
use tokio::{sync::RwLock, time::sleep};
use tendermint_machine::{ext::*, SignedMessage, TendermintMachine, TendermintHandle};
@ -23,7 +26,7 @@ impl SignatureScheme for TestSignatureScheme {
}
#[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])
}
@ -34,13 +37,13 @@ impl SignatureScheme for TestSignatureScheme {
#[must_use]
fn verify_aggregate(
&self,
msg: &[u8],
signers: &[TestValidatorId],
msg: &[u8],
sigs: &Vec<[u8; 32]>,
) -> bool {
assert_eq!(signers.len(), sigs.len());
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
}
@ -140,7 +143,7 @@ impl TestNetwork {
#[tokio::test]
async fn test() {
TestNetwork::new(4).await;
for _ in 0 .. 100 {
tokio::task::yield_now().await;
for _ in 0 .. 10 {
sleep(Duration::from_secs(1)).await;
}
}