Skip to content

Commit

Permalink
Refactor signing facility into a SigningScheme
Browse files Browse the repository at this point in the history
  • Loading branch information
romac committed Oct 27, 2023
1 parent 0882e26 commit 62c47df
Show file tree
Hide file tree
Showing 16 changed files with 165 additions and 141 deletions.
1 change: 1 addition & 0 deletions Code/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ edition = "2021"
publish = false

[dependencies]
secrecy = "0.8.0"
signature = "2.1.0"
13 changes: 7 additions & 6 deletions Code/common/src/consensus.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
Address, Height, PrivateKey, Proposal, PublicKey, Round, Signature, SignedVote, Validator,
ValidatorSet, Value, ValueId, Vote,
Address, Height, PrivateKey, Proposal, PublicKey, Round, Signature, SignedVote, SigningScheme,
Validator, ValidatorSet, Value, ValueId, Vote,
};

/// This trait allows to abstract over the various datatypes
Expand All @@ -12,21 +12,22 @@ where
type Address: Address;
type Height: Height;
type Proposal: Proposal<Self>;
type PrivateKey: PrivateKey<PublicKey = Self::PublicKey>;
type PublicKey: PublicKey<Signature = Signature<Self>>;
type Validator: Validator<Self>;
type ValidatorSet: ValidatorSet<Self>;
type Value: Value;
type Vote: Vote<Self>;
type SigningScheme: SigningScheme; // TODO: Do we need to support multiple signing schemes?

// FIXME: Remove altogether
const DUMMY_VALUE: Self::Value;

/// Sign the given vote using the given private key.
fn sign_vote(vote: &Self::Vote, private_key: &Self::PrivateKey) -> Signature<Self>;
/// TODO: Maybe move this as concrete methods in `SignedVote`?
fn sign_vote(vote: &Self::Vote, private_key: &PrivateKey<Self>) -> Signature<Self>;

/// Verify the given vote's signature using the given public key.
fn verify_signed_vote(signed_vote: &SignedVote<Self>, public_key: &Self::PublicKey) -> bool;
/// TODO: Maybe move this as concrete methods in `SignedVote`?
fn verify_signed_vote(signed_vote: &SignedVote<Self>, public_key: &PublicKey<Self>) -> bool;

/// Build a new proposal for the given value at the given height, round and POL round.
fn new_proposal(
Expand Down
9 changes: 7 additions & 2 deletions Code/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,21 @@ mod validator_set;
mod value;
mod vote;

// Re-export `signature` crate for convenience
pub use ::signature;

/// Type alias to make it easier to refer the `ValueId` type of a given `Consensus` engine.
pub type ValueId<C> = <<C as Consensus>::Value as Value>::Id;
pub type Signature<C> = <<C as Consensus>::PrivateKey as PrivateKey>::Signature;
pub type PublicKey<C> = <<C as Consensus>::SigningScheme as SigningScheme>::PublicKey;
pub type PrivateKey<C> = <<C as Consensus>::SigningScheme as SigningScheme>::PrivateKey;
pub type Signature<C> = <<C as Consensus>::SigningScheme as SigningScheme>::Signature;

pub use consensus::Consensus;
pub use height::Height;
pub use proposal::Proposal;
pub use round::Round;
pub use signed_vote::SignedVote;
pub use signing::{PrivateKey, PublicKey};
pub use signing::SigningScheme;
pub use timeout::{Timeout, TimeoutStep};
pub use validator_set::{Address, Validator, ValidatorSet, VotingPower};
pub use value::Value;
Expand Down
10 changes: 3 additions & 7 deletions Code/common/src/signed_vote.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Consensus, PublicKey};
use crate::{Consensus, Signature};

// TODO: Do we need to abstract over `SignedVote` as well?

Expand All @@ -9,18 +9,14 @@ where
{
pub vote: C::Vote,
pub address: C::Address,
pub signature: <C::PublicKey as PublicKey>::Signature,
pub signature: Signature<C>,
}

impl<C> SignedVote<C>
where
C: Consensus,
{
pub fn new(
vote: C::Vote,
address: C::Address,
signature: <C::PublicKey as PublicKey>::Signature,
) -> Self {
pub fn new(vote: C::Vote, address: C::Address, signature: Signature<C>) -> Self {
Self {
vote,
address,
Expand Down
26 changes: 12 additions & 14 deletions Code/common/src/signing.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
use core::fmt::Debug;

use signature::{Signer, Verifier};
use secrecy::{CloneableSecret, DebugSecret, Zeroize};
use signature::{Keypair, Signer, Verifier};

/// Defines the requirements for a private key type.
pub trait PrivateKey
pub trait SigningScheme
where
Self: Clone + Debug + Signer<Self::Signature>,
Self: Clone + Debug + Eq,
{
type Signature: Clone + Debug + PartialEq + Eq;
type PublicKey: PublicKey<Signature = Self::Signature>;
type Signature: Clone + Debug + Eq;

fn public_key(&self) -> Self::PublicKey;
}
type PublicKey: Clone + Debug + Eq + Verifier<Self::Signature>;

/// Defines the requirements for a public key type.
pub trait PublicKey
where
Self: Clone + Debug + PartialEq + Eq + Verifier<Self::Signature>,
{
type Signature: Clone + Debug + PartialEq + Eq;
type PrivateKey: Clone
+ Signer<Self::Signature>
+ Keypair<VerifyingKey = Self::PublicKey>
+ Zeroize
+ DebugSecret
+ CloneableSecret;
}
6 changes: 3 additions & 3 deletions Code/common/src/validator_set.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::fmt::Debug;

use crate::Consensus;
use crate::{Consensus, PublicKey};

/// Voting power held by a validator.
///
Expand All @@ -26,7 +26,7 @@ where
fn address(&self) -> &C::Address;

/// The public key of the validator, used to verify signatures.
fn public_key(&self) -> &C::PublicKey;
fn public_key(&self) -> &PublicKey<C>;

/// The voting power held by the validaror.
fn voting_power(&self) -> VotingPower;
Expand All @@ -46,7 +46,7 @@ where
fn get_proposer(&self) -> C::Validator;

/// Get the validator with the given public key.
fn get_by_public_key(&self, public_key: &C::PublicKey) -> Option<&C::Validator>;
fn get_by_public_key(&self, public_key: &PublicKey<C>) -> Option<&C::Validator>;

/// Get the validator with the given address.
fn get_by_address(&self, address: &C::Address) -> Option<&C::Validator>;
Expand Down
1 change: 1 addition & 0 deletions Code/consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ publish = false
malachite-common = { version = "0.1.0", path = "../common" }
malachite-round = { version = "0.1.0", path = "../round" }
malachite-vote = { version = "0.1.0", path = "../vote" }
secrecy = "0.8.0"
15 changes: 9 additions & 6 deletions Code/consensus/src/executor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use std::collections::BTreeMap;

use secrecy::{ExposeSecret, Secret};

use malachite_common::signature::Keypair;
use malachite_common::{
Consensus, PrivateKey, Proposal, Round, SignedVote, Timeout, TimeoutStep, Validator,
ValidatorSet, Value, Vote, VoteType,
Expand All @@ -18,7 +21,7 @@ where
C: Consensus,
{
height: C::Height,
key: C::PrivateKey,
key: Secret<PrivateKey<C>>,
validator_set: C::ValidatorSet,
round: Round,
votes: VoteKeeper<C>,
Expand All @@ -29,7 +32,7 @@ impl<C> Executor<C>
where
C: Consensus,
{
pub fn new(height: C::Height, validator_set: C::ValidatorSet, key: C::PrivateKey) -> Self {
pub fn new(height: C::Height, validator_set: C::ValidatorSet, key: PrivateKey<C>) -> Self {
let votes = VoteKeeper::new(
height.clone(),
Round::INITIAL,
Expand All @@ -38,7 +41,7 @@ where

Self {
height,
key,
key: Secret::new(key),
validator_set,
round: Round::INITIAL,
votes,
Expand Down Expand Up @@ -75,11 +78,11 @@ where
RoundMessage::Vote(vote) => {
let address = self
.validator_set
.get_by_public_key(&self.key.public_key())?
.get_by_public_key(&self.key.expose_secret().verifying_key())?
.address()
.clone();

let signature = C::sign_vote(&vote, &self.key);
let signature = C::sign_vote(&vote, self.key.expose_secret());
let signed_vote = SignedVote::new(vote, address, signature);

Some(Message::Vote(signed_vote))
Expand Down Expand Up @@ -109,7 +112,7 @@ where
fn apply_new_round(&mut self, round: Round) -> Option<RoundMessage<C>> {
let proposer = self.validator_set.get_proposer();

let event = if proposer.public_key() == &self.key.public_key() {
let event = if proposer.public_key() == &self.key.expose_secret().verifying_key() {
let value = self.get_value();
RoundEvent::NewRoundProposer(value)
} else {
Expand Down
1 change: 1 addition & 0 deletions Code/test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ ed25519-consensus = "2.1.0"
signature = "2.1.0"
rand = { version = "0.8.5", features = ["std_rng"] }
sha2 = "0.10.8"
secrecy = "0.8.0"
9 changes: 4 additions & 5 deletions Code/test/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use malachite_common::SignedVote;

use crate::height::*;
use crate::proposal::*;
use crate::public_key::{Ed25519PrivateKey, Ed25519PublicKey, Ed25519Signature};
use crate::signing::{Ed25519, PrivateKey, PublicKey, Signature};
use crate::validator_set::*;
use crate::value::*;
use crate::vote::*;
Expand All @@ -16,21 +16,20 @@ impl Consensus for TestConsensus {
type Address = Address;
type Height = Height;
type Proposal = Proposal;
type PublicKey = Ed25519PublicKey;
type PrivateKey = Ed25519PrivateKey;
type ValidatorSet = ValidatorSet;
type Validator = Validator;
type Value = Value;
type Vote = Vote;
type SigningScheme = Ed25519;

const DUMMY_VALUE: Self::Value = Value::new(9999);

fn sign_vote(vote: &Self::Vote, private_key: &Self::PrivateKey) -> Ed25519Signature {
fn sign_vote(vote: &Self::Vote, private_key: &PrivateKey) -> Signature {
use signature::Signer;
private_key.sign(&vote.to_bytes())
}

fn verify_signed_vote(signed_vote: &SignedVote<Self>, public_key: &Ed25519PublicKey) -> bool {
fn verify_signed_vote(signed_vote: &SignedVote<Self>, public_key: &PublicKey) -> bool {
use signature::Verifier;
public_key
.verify(&signed_vote.vote.to_bytes(), &signed_vote.signature)
Expand Down
4 changes: 2 additions & 2 deletions Code/test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
mod consensus;
mod height;
mod proposal;
mod public_key;
mod signing;
mod validator_set;
mod value;
mod vote;

pub use crate::consensus::*;
pub use crate::height::*;
pub use crate::proposal::*;
pub use crate::public_key::*;
pub use crate::signing::*;
pub use crate::validator_set::*;
pub use crate::value::*;
pub use crate::vote::*;
68 changes: 0 additions & 68 deletions Code/test/src/public_key.rs

This file was deleted.

Loading

0 comments on commit 62c47df

Please sign in to comment.