diff --git a/eth2/types/src/test_utils/keypairs_file.rs b/eth2/types/src/test_utils/keypairs_file.rs index b0ac8424f..5804b9696 100644 --- a/eth2/types/src/test_utils/keypairs_file.rs +++ b/eth2/types/src/test_utils/keypairs_file.rs @@ -58,7 +58,7 @@ impl KeypairsFile for Vec { let pk_start = sk_end; let pk_end = pk_start + PUBLIC_KEY_BYTES_LEN; - let pk = PublicKey::from_bytes(&buf[pk_start..pk_end]) + let pk = PublicKey::from_uncompressed_bytes(&buf[pk_start..pk_end]) .map_err(|_| Error::new(ErrorKind::Other, "Invalid PublicKey bytes")) .unwrap(); diff --git a/eth2/utils/bls/Cargo.toml b/eth2/utils/bls/Cargo.toml index 5ac38595a..468ed8050 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.5.2" } +bls-aggregates = { git = "https://github.com/sigp/signature-schemes", tag = "0.6.0" } 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 2d8776353..fa3628a89 100644 --- a/eth2/utils/bls/src/aggregate_signature.rs +++ b/eth2/utils/bls/src/aggregate_signature.rs @@ -48,15 +48,9 @@ impl AggregateSignature { domain: u64, aggregate_public_keys: &[&AggregatePublicKey], ) -> bool { - // TODO: the API for `RawAggregatePublicKey` shoudn't need to take an owned - // `AggregatePublicKey`. There is an issue to fix this, but in the meantime we need to - // clone. - // - // https://github.com/sigp/signature-schemes/issues/10 - let aggregate_public_keys: Vec = aggregate_public_keys + let aggregate_public_keys: Vec<&RawAggregatePublicKey> = aggregate_public_keys .iter() .map(|pk| pk.as_raw()) - .cloned() .collect(); // Messages are concatenated into one long message. diff --git a/eth2/utils/bls/src/public_key.rs b/eth2/utils/bls/src/public_key.rs index ecdfce3eb..eaf2c9d3f 100644 --- a/eth2/utils/bls/src/public_key.rs +++ b/eth2/utils/bls/src/public_key.rs @@ -1,6 +1,6 @@ use super::serde_vistors::HexVisitor; use super::SecretKey; -use bls_aggregates::{DecodeError as BlsDecodeError, PublicKey as RawPublicKey}; +use bls_aggregates::PublicKey as RawPublicKey; use hex::encode as hex_encode; use serde::de::{Deserialize, Deserializer}; use serde::ser::{Serialize, Serializer}; @@ -22,18 +22,22 @@ impl PublicKey { PublicKey(RawPublicKey::from_secret_key(secret_key.as_raw())) } - /// Instantiate a PublicKey from existing bytes. - /// - /// Note: this is _not_ SSZ decoding. - pub fn from_bytes(bytes: &[u8]) -> Result { - Ok(Self(RawPublicKey::from_bytes(bytes)?)) - } - - /// Returns the underlying public key. + /// Returns the underlying signature. pub fn as_raw(&self) -> &RawPublicKey { &self.0 } + /// Returns the PublicKey as (x, y) bytes + pub fn as_uncompressed_bytes(&mut self) -> Vec { + RawPublicKey::as_uncompressed_bytes(&mut self.0) + } + + /// Converts (x, y) bytes to PublicKey + pub fn from_uncompressed_bytes(bytes: &[u8]) -> Result { + let pubkey = RawPublicKey::from_uncompressed_bytes(&bytes).map_err(|_| DecodeError::Invalid)?; + Ok(PublicKey(pubkey)) + } + /// Returns the last 6 bytes of the SSZ encoding of the public key, as a hex string. /// /// Useful for providing a short identifier to the user. @@ -100,11 +104,7 @@ impl PartialEq for PublicKey { impl Hash for PublicKey { fn hash(&self, state: &mut H) { - // Note: this is not necessarily the consensus-ready hash. Instead, it is designed to be - // optimally fast for internal usage. - // - // To hash for consensus purposes, use the SSZ-encoded bytes. - self.0.as_bytes().hash(state) + ssz_encode(self).hash(state) } } diff --git a/eth2/utils/bls/src/signature.rs b/eth2/utils/bls/src/signature.rs index 86c54cba7..760b0018a 100644 --- a/eth2/utils/bls/src/signature.rs +++ b/eth2/utils/bls/src/signature.rs @@ -54,9 +54,12 @@ impl Signature { /// Returns a new empty signature. pub fn empty_signature() -> Self { + // Empty Signature is currently being represented as BLS::Signature.point_at_infinity() + // However it should be represented as vec![0; 96] but this + // would require all signatures to be represented in byte form as opposed to Signature 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); + // Sets C_flag and B_flag to 1 and all else to 0 + empty[0] += u8::pow(2, 6) + u8::pow(2, 7); Signature(RawSignature::from_bytes(&empty).unwrap()) } } @@ -129,7 +132,7 @@ mod tests { 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)); + assert_eq!(*one_byte, u8::pow(2, 6) + u8::pow(2, 7)); } else { assert_eq!(*one_byte, 0); }