From 9c4a1f1d1f6814dee9f1761ec2ae4cbb815c8ca7 Mon Sep 17 00:00:00 2001 From: Kirk Baird Date: Mon, 18 Feb 2019 10:50:40 +1100 Subject: [PATCH] Update to signature-scheme 0.5.2 --- .../src/attestation_aggregator.rs | 9 +++--- eth2/attester/src/lib.rs | 6 ++-- eth2/block_producer/src/lib.rs | 13 ++++---- eth2/fork_choice/src/optimised_lmd_ghost.rs | 7 ++--- eth2/fork_choice/src/protolambda_lmd_ghost.rs | 1 + eth2/types/src/attestation.rs | 7 +++-- eth2/types/src/beacon_state.rs | 20 +++++++------ eth2/types/src/fork.rs | 2 +- eth2/utils/bls/Cargo.toml | 2 +- eth2/utils/bls/src/aggregate_signature.rs | 7 ++++- eth2/utils/bls/src/lib.rs | 1 - eth2/utils/bls/src/signature.rs | 30 ++++++++++++++----- 12 files changed, 67 insertions(+), 38 deletions(-) diff --git a/beacon_node/beacon_chain/src/attestation_aggregator.rs b/beacon_node/beacon_chain/src/attestation_aggregator.rs index e8576276c..abedf62f6 100644 --- a/beacon_node/beacon_chain/src/attestation_aggregator.rs +++ b/beacon_node/beacon_chain/src/attestation_aggregator.rs @@ -110,10 +110,11 @@ impl AttestationAggregator { Message::BadValidatorIndex ); - if !free_attestation - .signature - .verify(&signable_message, spec.domain_attestation, &validator_record.pubkey) - { + if !free_attestation.signature.verify( + &signable_message, + spec.domain_attestation, + &validator_record.pubkey, + ) { return Ok(Outcome { valid: false, message: Message::BadSignature, diff --git a/eth2/attester/src/lib.rs b/eth2/attester/src/lib.rs index f2bbd6db3..13a1d72bb 100644 --- a/eth2/attester/src/lib.rs +++ b/eth2/attester/src/lib.rs @@ -137,8 +137,10 @@ impl Attester Option { self.store_produce(attestation_data); - self.signer - .sign_attestation_message(&attestation_data.signable_message(PHASE_0_CUSTODY_BIT)[..], DOMAIN_ATTESTATION) + self.signer.sign_attestation_message( + &attestation_data.signable_message(PHASE_0_CUSTODY_BIT)[..], + DOMAIN_ATTESTATION, + ) } /// Returns `true` if signing some attestation_data is safe (non-slashable). diff --git a/eth2/block_producer/src/lib.rs b/eth2/block_producer/src/lib.rs index e5651780a..fefaa7c04 100644 --- a/eth2/block_producer/src/lib.rs +++ b/eth2/block_producer/src/lib.rs @@ -134,7 +134,10 @@ impl BlockProducer return Ok(PollOutcome::SignerRejection(slot)), Some(signature) => signature, } @@ -166,10 +169,10 @@ impl BlockProducer Option { self.store_produce(&block); - match self - .signer - .sign_block_proposal(&block.proposal_root(&self.spec)[..], self.spec.domain_proposal) - { + match self.signer.sign_block_proposal( + &block.proposal_root(&self.spec)[..], + self.spec.domain_proposal, + ) { None => None, Some(signature) => { block.signature = signature; diff --git a/eth2/fork_choice/src/optimised_lmd_ghost.rs b/eth2/fork_choice/src/optimised_lmd_ghost.rs index 6b73c2a8f..dcf9c8380 100644 --- a/eth2/fork_choice/src/optimised_lmd_ghost.rs +++ b/eth2/fork_choice/src/optimised_lmd_ghost.rs @@ -30,11 +30,8 @@ use fast_math::log2_raw; use std::collections::HashMap; use std::sync::Arc; use types::{ - readers::BeaconBlockReader, - slot_epoch::Slot, - slot_height::SlotHeight, - validator_registry::get_active_validator_indices, - BeaconBlock, Hash256, + readers::BeaconBlockReader, slot_epoch::Slot, slot_height::SlotHeight, + validator_registry::get_active_validator_indices, BeaconBlock, Hash256, }; //TODO: Pruning - Children diff --git a/eth2/fork_choice/src/protolambda_lmd_ghost.rs b/eth2/fork_choice/src/protolambda_lmd_ghost.rs index e69de29bb..8b1378917 100644 --- a/eth2/fork_choice/src/protolambda_lmd_ghost.rs +++ b/eth2/fork_choice/src/protolambda_lmd_ghost.rs @@ -0,0 +1 @@ + diff --git a/eth2/types/src/attestation.rs b/eth2/types/src/attestation.rs index be0b12d9e..2c4281fff 100644 --- a/eth2/types/src/attestation.rs +++ b/eth2/types/src/attestation.rs @@ -27,8 +27,11 @@ impl Attestation { custody_bit: bool, domain: u64, ) -> bool { - self.aggregate_signature - .verify(&self.signable_message(custody_bit), domain, group_public_key) + self.aggregate_signature.verify( + &self.signable_message(custody_bit), + domain, + group_public_key, + ) } } diff --git a/eth2/types/src/beacon_state.rs b/eth2/types/src/beacon_state.rs index 278569609..34d0a5a1f 100644 --- a/eth2/types/src/beacon_state.rs +++ b/eth2/types/src/beacon_state.rs @@ -1,8 +1,8 @@ use crate::test_utils::TestRandom; use crate::{ validator::StatusFlags, validator_registry::get_active_validator_indices, AttestationData, - Bitfield, ChainSpec, Crosslink, Deposit, DepositInput, Epoch, Eth1Data, Eth1DataVote, Fork, Hash256, - PendingAttestation, PublicKey, Signature, Slot, Validator, + Bitfield, ChainSpec, Crosslink, Deposit, DepositInput, Epoch, Eth1Data, Eth1DataVote, Fork, + Hash256, PendingAttestation, PublicKey, Signature, Slot, Validator, }; use honey_badger_split::SplitExt; use rand::RngCore; @@ -593,7 +593,7 @@ impl BeaconState { pubkey: PublicKey, proof_of_possession: Signature, withdrawal_credentials: Hash256, - spec: &ChainSpec + spec: &ChainSpec, ) -> bool { let proof_of_possession_data = DepositInput { pubkey: pubkey.clone(), @@ -603,15 +603,12 @@ impl BeaconState { proof_of_possession.verify( &proof_of_possession_data.hash_tree_root(), - self.fork.get_domain( - self.slot.epoch(spec.epoch_length), - spec.domain_deposit, - ), + self.fork + .get_domain(self.slot.epoch(spec.epoch_length), spec.domain_deposit), &pubkey, ) } - /// Process a validator deposit, returning the validator index if the deposit is valid. /// /// Spec v0.2.0 @@ -623,7 +620,12 @@ impl BeaconState { withdrawal_credentials: Hash256, spec: &ChainSpec, ) -> Result { - if !self.validate_proof_of_possession(pubkey.clone(), proof_of_possession, withdrawal_credentials, &spec) { + if !self.validate_proof_of_possession( + pubkey.clone(), + proof_of_possession, + withdrawal_credentials, + &spec, + ) { return Err(()); } diff --git a/eth2/types/src/fork.rs b/eth2/types/src/fork.rs index c103a2653..67a8c90eb 100644 --- a/eth2/types/src/fork.rs +++ b/eth2/types/src/fork.rs @@ -22,7 +22,7 @@ impl Fork { /// Get the domain number that represents the fork meta and signature domain. pub fn get_domain(&self, epoch: Epoch, domain_type: u64) -> u64 { let fork_version = self.get_fork_version(epoch); - fork_version * u64::pow(2,32) + domain_type + fork_version * u64::pow(2, 32) + domain_type } } diff --git a/eth2/utils/bls/Cargo.toml b/eth2/utils/bls/Cargo.toml index c8204ca7a..7a436307b 100644 --- a/eth2/utils/bls/Cargo.toml +++ b/eth2/utils/bls/Cargo.toml @@ -5,7 +5,7 @@ authors = ["Paul Hauner "] edition = "2018" [dependencies] -bls-aggregates = { git = "https://github.com/sigp/signature-schemes", tag = "0.4.1" } +bls-aggregates = { git = "https://github.com/sigp/signature-schemes", tag = "0.5.2" } hashing = { path = "../hashing" } hex = "0.3" serde = "1.0" diff --git a/eth2/utils/bls/src/aggregate_signature.rs b/eth2/utils/bls/src/aggregate_signature.rs index b684c2b5b..8463b26b3 100644 --- a/eth2/utils/bls/src/aggregate_signature.rs +++ b/eth2/utils/bls/src/aggregate_signature.rs @@ -27,7 +27,12 @@ impl AggregateSignature { /// /// Only returns `true` if the set of keys in the `AggregatePublicKey` match the set of keys /// that signed the `AggregateSignature`. - pub fn verify(&self, msg: &[u8], domain: u64, aggregate_public_key: &AggregatePublicKey) -> bool { + pub fn verify( + &self, + msg: &[u8], + domain: u64, + aggregate_public_key: &AggregatePublicKey, + ) -> bool { self.0.verify(msg, domain, aggregate_public_key) } } diff --git a/eth2/utils/bls/src/lib.rs b/eth2/utils/bls/src/lib.rs index 39d4a95f2..074929b32 100644 --- a/eth2/utils/bls/src/lib.rs +++ b/eth2/utils/bls/src/lib.rs @@ -30,7 +30,6 @@ fn extend_if_needed(hash: &mut Vec) { /// For some signature and public key, ensure that the signature message was the public key and it /// was signed by the secret key that corresponds to that public key. - pub fn create_proof_of_possession(keypair: &Keypair) -> Signature { Signature::new(&ssz_encode(&keypair.pk), 0, &keypair.sk) } diff --git a/eth2/utils/bls/src/signature.rs b/eth2/utils/bls/src/signature.rs index 61440498e..23b0c0834 100644 --- a/eth2/utils/bls/src/signature.rs +++ b/eth2/utils/bls/src/signature.rs @@ -21,7 +21,11 @@ impl Signature { /// Instantiate a new Signature from a message and a SecretKey, where the message has already /// been hashed. pub fn new_hashed(x_real_hashed: &[u8], x_imaginary_hashed: &[u8], sk: &SecretKey) -> Self { - Signature(RawSignature::new_hashed(x_real_hashed, x_imaginary_hashed, sk.as_raw())) + Signature(RawSignature::new_hashed( + x_real_hashed, + x_imaginary_hashed, + sk.as_raw(), + )) } /// Verify the Signature against a PublicKey. @@ -30,8 +34,14 @@ impl Signature { } /// Verify the Signature against a PublicKey, where the message has already been hashed. - pub fn verify_hashed(&self, x_real_hashed: &[u8], x_imaginary_hashed: &[u8], pk: &PublicKey) -> bool { - self.0.verify_hashed(x_real_hashed, x_imaginary_hashed, pk.as_raw()) + pub fn verify_hashed( + &self, + x_real_hashed: &[u8], + x_imaginary_hashed: &[u8], + pk: &PublicKey, + ) -> bool { + self.0 + .verify_hashed(x_real_hashed, x_imaginary_hashed, pk.as_raw()) } /// Returns the underlying signature. @@ -41,7 +51,9 @@ impl Signature { /// Returns a new empty signature. pub fn empty_signature() -> Self { - let empty: Vec = vec![0; 96]; + let mut empty: Vec = vec![0; 96]; + // TODO: Modify the way flags are used (b_flag should not be used for empty_signature in the future) + empty[0] += u8::pow(2, 6); Signature(RawSignature::from_bytes(&empty).unwrap()) } } @@ -99,9 +111,13 @@ mod tests { let sig_as_bytes: Vec = sig.as_raw().as_bytes(); - assert_eq!(sig_as_bytes.len(), 97); - for one_byte in sig_as_bytes.iter() { - assert_eq!(*one_byte, 0); + assert_eq!(sig_as_bytes.len(), 96); + for (i, one_byte) in sig_as_bytes.iter().enumerate() { + if i == 0 { + assert_eq!(*one_byte, u8::pow(2, 6)); + } else { + assert_eq!(*one_byte, 0); + } } } }