diff --git a/Cargo.toml b/Cargo.toml index 778df551d0..eaca0fd616 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ authors = ["Paul Hauner "] # TODO: remove "blake2" in favor of "blake2-rfc" blake2 = "^0.7.1" blake2-rfc = "0.2.18" -bls = { git = "https://github.com/sigp/bls" } +bls-aggregates = { git = "https://github.com/sigp/signature-schemes" } boolean-bitfield = { path = "boolean-bitfield" } bytes = "" crypto-mac = "^0.6.2" diff --git a/lighthouse/bls/mod.rs b/lighthouse/bls/mod.rs new file mode 100644 index 0000000000..fff21940cb --- /dev/null +++ b/lighthouse/bls/mod.rs @@ -0,0 +1,9 @@ +extern crate bls_aggregates; + +pub use self::bls_aggregates::AggregateSignature; +pub use self::bls_aggregates::AggregatePublicKey; +pub use self::bls_aggregates::Signature; +pub use self::bls_aggregates::Keypair; +pub use self::bls_aggregates::PublicKey; + +pub const BLS_AGG_SIG_BYTE_SIZE: usize = 97; diff --git a/lighthouse/main.rs b/lighthouse/main.rs index 1631cc4e74..12444f538b 100644 --- a/lighthouse/main.rs +++ b/lighthouse/main.rs @@ -2,6 +2,7 @@ extern crate slog; extern crate slog_term; extern crate slog_async; +extern crate ssz; extern crate clap; extern crate network_libp2p; extern crate futures; diff --git a/lighthouse/state/attestation_record.rs b/lighthouse/state/attestation_record.rs index 73ed5e543e..a1997613fc 100644 --- a/lighthouse/state/attestation_record.rs +++ b/lighthouse/state/attestation_record.rs @@ -1,5 +1,8 @@ use super::utils::types::{ Hash256, Bitfield }; -use super::utils::bls::{ AggregateSignature }; +use super::bls::{ + AggregateSignature, + BLS_AGG_SIG_BYTE_SIZE, +}; use super::ssz::{ Encodable, Decodable, @@ -16,7 +19,7 @@ pub const MIN_SSZ_ATTESTION_RECORD_LENGTH: usize = { 5 + // attester_bitfield (assuming 1 byte of bitfield) 8 + // justified_slot 32 + // justified_block_hash - 4 + (2 * 8) // aggregate sig (two 256 bit points) + 4 + BLS_AGG_SIG_BYTE_SIZE // aggregate sig (two 256 bit points) }; #[derive(Debug)] @@ -28,7 +31,7 @@ pub struct AttestationRecord { pub attester_bitfield: Bitfield, pub justified_slot: u64, pub justified_block_hash: Hash256, - pub aggregate_sig: Option, + pub aggregate_sig: AggregateSignature, } impl Encodable for AttestationRecord { @@ -40,8 +43,7 @@ impl Encodable for AttestationRecord { s.append_vec(&self.attester_bitfield.to_be_vec()); s.append(&self.justified_slot); s.append(&self.justified_block_hash); - // TODO: encode the aggregate sig correctly - s.append_vec(&vec![0_u8; 16]) + s.append_vec(&self.aggregate_sig.as_bytes()); } } @@ -57,7 +59,10 @@ impl Decodable for AttestationRecord { let (justified_slot, i) = u64::ssz_decode(bytes, i)?; let (justified_block_hash, i) = Hash256::ssz_decode(bytes, i)?; // Do aggregate sig decoding properly. - let aggregate_sig = None; let i = i + 20; + let (agg_sig_bytes, i) = decode_ssz_list(bytes, i)?; + let aggregate_sig = AggregateSignature::from_bytes(&agg_sig_bytes) + .map_err(|_| DecodeError::OutOfBounds)?; + let attestation_record = Self { slot, shard_id, @@ -82,7 +87,7 @@ impl AttestationRecord { attester_bitfield: Bitfield::new(), justified_slot: 0, justified_block_hash: Hash256::zero(), - aggregate_sig: None, + aggregate_sig: AggregateSignature::new(), } } } @@ -113,7 +118,7 @@ mod tests { attester_bitfield: Bitfield::from(&vec![17; 42][..]), justified_slot: 19, justified_block_hash: Hash256::from(&vec![15; 32][..]), - aggregate_sig: None, + aggregate_sig: AggregateSignature::new(), }; let mut ssz_stream = SszStream::new(); diff --git a/lighthouse/state/block/ssz_block.rs b/lighthouse/state/block/ssz_block.rs index 577857a19e..3c430c251a 100644 --- a/lighthouse/state/block/ssz_block.rs +++ b/lighthouse/state/block/ssz_block.rs @@ -235,9 +235,9 @@ mod tests { // will tell us if the hash changes, not that it matches some // canonical reference. let expected_hash = [ - 195, 180, 208, 144, 113, 20, 129, 108, 14, 128, 166, 170, - 137, 15, 191, 186, 34, 171, 79, 214, 74, 86, 89, 202, 255, - 9, 100, 170, 149, 160, 93, 59 + 64, 176, 117, 210, 228, 229, 237, 100, 66, 66, 98, + 252, 31, 111, 218, 27, 160, 57, 164, 12, 15, 164, + 66, 102, 142, 36, 2, 196, 121, 54, 242, 3 ]; assert_eq!(hash, expected_hash); diff --git a/lighthouse/state/mod.rs b/lighthouse/state/mod.rs index fbb789490c..532d8345dc 100644 --- a/lighthouse/state/mod.rs +++ b/lighthouse/state/mod.rs @@ -4,9 +4,10 @@ extern crate blake2_rfc as blake2; extern crate bytes; extern crate ssz; +use super::bls; +use super::db; use super::Logger; use super::utils; -use super::db; pub mod active_state; pub mod attestation_record; diff --git a/lighthouse/state/validation/attestation_validation.rs b/lighthouse/state/validation/attestation_validation.rs index 9dfa943fb6..47b6f83fbb 100644 --- a/lighthouse/state/validation/attestation_validation.rs +++ b/lighthouse/state/validation/attestation_validation.rs @@ -6,7 +6,7 @@ use super::attestation_parent_hashes::{ use super::db::ClientDB; use super::db::stores::BlockStore; use super::ssz::SszStream; -use super::utils::bls::{ +use super::bls::{ AggregateSignature, PublicKey, }; diff --git a/lighthouse/state/validation/mod.rs b/lighthouse/state/validation/mod.rs index 39663dba11..70c3757548 100644 --- a/lighthouse/state/validation/mod.rs +++ b/lighthouse/state/validation/mod.rs @@ -6,6 +6,7 @@ use super::block::Block; use super::chain_config::ChainConfig; */ use super::block; +use super::bls; use super::Logger; use super::db; use super::attestation_record::AttestationRecord; diff --git a/lighthouse/state/validator_record.rs b/lighthouse/state/validator_record.rs index e69b169c11..9961447da6 100644 --- a/lighthouse/state/validator_record.rs +++ b/lighthouse/state/validator_record.rs @@ -1,7 +1,7 @@ extern crate rand; use super::utils::types::{ Hash256, Address, U256 }; -use super::utils::bls::{ PublicKey, Keypair }; +use super::bls::{ PublicKey, Keypair }; use self::rand::thread_rng; @@ -21,10 +21,9 @@ impl ValidatorRecord { /// /// Returns the new instance and new keypair. pub fn zero_with_thread_rand_keypair() -> (Self, Keypair) { - let mut rng = thread_rng(); - let keypair = Keypair::generate(&mut rng); + let keypair = Keypair::random(); let s = Self { - pubkey: keypair.public.clone(), + pubkey: keypair.pk.clone(), withdrawal_shard: 0, withdrawal_address: Address::zero(), randao_commitment: Hash256::zero(), diff --git a/lighthouse/utils/bls.rs b/lighthouse/utils/bls.rs deleted file mode 100644 index c6b5dae569..0000000000 --- a/lighthouse/utils/bls.rs +++ /dev/null @@ -1,13 +0,0 @@ -extern crate bls; -extern crate pairing; - -use self::bls::AggregateSignature as GenericAggregateSignature; -use self::bls::Signature as GenericSignature; -use self::bls::Keypair as GenericKeypair; -use self::bls::PublicKey as GenericPublicKey; -use self::pairing::bls12_381::Bls12; - -pub type AggregateSignature = GenericAggregateSignature; -pub type Signature = GenericSignature; -pub type Keypair = GenericKeypair; -pub type PublicKey = GenericPublicKey; diff --git a/lighthouse/utils/mod.rs b/lighthouse/utils/mod.rs index 4afe15c454..16e986e227 100644 --- a/lighthouse/utils/mod.rs +++ b/lighthouse/utils/mod.rs @@ -7,9 +7,5 @@ extern crate boolean_bitfield; pub mod macros; pub mod hash; pub mod types; -pub mod bls; -pub mod test_helpers; pub mod logging; pub mod errors; - - diff --git a/lighthouse/utils/test_helpers.rs b/lighthouse/utils/test_helpers.rs deleted file mode 100644 index 8a18496882..0000000000 --- a/lighthouse/utils/test_helpers.rs +++ /dev/null @@ -1,12 +0,0 @@ -extern crate rand; - -use super::bls::Keypair; -use self::rand::thread_rng; - -// Returns a keypair for use in testing purposes. -// It is dangerous because we provide no guarantees -// that the private key is unique or in-fact private. -pub fn get_dangerous_test_keypair() -> Keypair { - let mut rng = thread_rng(); - Keypair::generate(&mut rng) -}