Define a signature scheme trait

This commit is contained in:
Luke Parker 2022-10-16 09:42:33 -04:00
parent a0bc9dc3e5
commit 85962c00a9
No known key found for this signature in database
GPG key ID: F9F1386DB1E119B6
2 changed files with 66 additions and 24 deletions

View file

@ -12,18 +12,16 @@ pub struct BlockNumber(pub u32);
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct Round(pub u16);
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum BlockError {
// Invalid behavior entirely
Fatal,
// Potentially valid behavior dependent on unsynchronized state
Temporal,
}
pub trait SignatureScheme {
type ValidatorId: ValidatorId;
type Signature: Clone + Copy + PartialEq;
type AggregateSignature: Clone + PartialEq;
pub trait Block: Send + Sync + Clone + PartialEq + Debug {
type Id: Send + Sync + Copy + Clone + PartialEq + Debug;
fn id(&self) -> Self::Id;
fn sign(&self, msg: &[u8]) -> Self::Signature;
#[must_use]
fn verify(&self, validator: Self::ValidatorId, msg: &[u8], sig: Self::Signature) -> bool;
// Intended to be a BLS signature, a Schnorr signature half-aggregation, or a Vec<Signature>.
fn aggregate(signatures: &[Self::Signature]) -> Self::AggregateSignature;
}
pub trait Weights: Send + Sync {
@ -42,15 +40,31 @@ pub trait Weights: Send + Sync {
fn proposer(&self, number: BlockNumber, round: Round) -> Self::ValidatorId;
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum BlockError {
// Invalid behavior entirely
Fatal,
// Potentially valid behavior dependent on unsynchronized state
Temporal,
}
pub trait Block: Send + Sync + Clone + PartialEq + Debug {
type Id: Send + Sync + Copy + Clone + PartialEq + Debug;
fn id(&self) -> Self::Id;
}
#[async_trait::async_trait]
pub trait Network: Send + Sync {
type ValidatorId: ValidatorId;
type SignatureScheme: SignatureScheme;
type Weights: Weights<ValidatorId = Self::ValidatorId>;
type Block: Block;
// Block time in seconds
const BLOCK_TIME: u32;
fn signature_scheme(&self) -> Arc<Self::SignatureScheme>;
fn weights(&self) -> Arc<Self::Weights>;
async fn broadcast(&mut self, msg: Message<Self::ValidatorId, Self::Block>);

View file

@ -7,17 +7,25 @@ use tendermint_machine::{ext::*, Message, TendermintMachine, TendermintHandle};
type TestValidatorId = u16;
type TestBlockId = u32;
#[derive(Clone, PartialEq, Debug)]
struct TestBlock {
id: TestBlockId,
valid: Result<(), BlockError>,
}
struct TestSignatureScheme(u16);
impl SignatureScheme for TestSignatureScheme {
type ValidatorId = TestValidatorId;
type Signature = [u8; 32];
type AggregateSignature = Vec<[u8; 32]>;
impl Block for TestBlock {
type Id = TestBlockId;
fn sign(&self, msg: &[u8]) -> [u8; 32] {
let mut sig = [0; 32];
sig[.. 2].copy_from_slice(&self.0.to_le_bytes());
sig[2 .. (2 + 30.min(msg.len()))].copy_from_slice(msg);
sig
}
fn id(&self) -> TestBlockId {
self.id
fn verify(&self, validator: u16, msg: &[u8], sig: [u8; 32]) -> bool {
(sig[.. 2] == validator.to_le_bytes()) && (&sig[2 ..] == &[msg, &[0; 30]].concat()[.. 30])
}
fn aggregate(sigs: &[[u8; 32]]) -> Vec<[u8; 32]> {
sigs.to_vec()
}
}
@ -37,22 +45,41 @@ impl Weights for TestWeights {
}
}
struct TestNetwork(Arc<RwLock<Vec<TendermintHandle<Self>>>>);
#[derive(Clone, PartialEq, Debug)]
struct TestBlock {
id: TestBlockId,
valid: Result<(), BlockError>,
}
impl Block for TestBlock {
type Id = TestBlockId;
fn id(&self) -> TestBlockId {
self.id
}
}
struct TestNetwork(u16, Arc<RwLock<Vec<TendermintHandle<Self>>>>);
#[async_trait::async_trait]
impl Network for TestNetwork {
type ValidatorId = TestValidatorId;
type SignatureScheme = TestSignatureScheme;
type Weights = TestWeights;
type Block = TestBlock;
const BLOCK_TIME: u32 = 1;
fn signature_scheme(&self) -> Arc<TestSignatureScheme> {
Arc::new(TestSignatureScheme(self.0))
}
fn weights(&self) -> Arc<TestWeights> {
Arc::new(TestWeights)
}
async fn broadcast(&mut self, msg: Message<TestValidatorId, Self::Block>) {
for handle in self.0.write().await.iter_mut() {
for handle in self.1.write().await.iter_mut() {
handle.messages.send(msg.clone()).await.unwrap();
}
}
@ -79,9 +106,10 @@ impl TestNetwork {
{
let mut write = arc.write().await;
for i in 0 .. validators {
let i = u16::try_from(i).unwrap();
write.push(TendermintMachine::new(
TestNetwork(arc.clone()),
u16::try_from(i).unwrap(),
TestNetwork(i, arc.clone()),
i,
BlockNumber(1),
TestBlock { id: 1, valid: Ok(()) },
));