From b2fb2afb2813c4288a462c4a84bf6bf9ea7f7528 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Wed, 13 Mar 2019 16:51:37 +1100 Subject: [PATCH] Revert "Move `PublicKey` to store uncomp. bytes." This reverts commit bfa2e71b468e34eedcf847076aa29f3a53038bfd. --- eth2/utils/bls/src/aggregate_public_key.rs | 2 +- eth2/utils/bls/src/public_key.rs | 56 ++++++---------------- eth2/utils/bls/src/signature.rs | 4 +- 3 files changed, 18 insertions(+), 44 deletions(-) diff --git a/eth2/utils/bls/src/aggregate_public_key.rs b/eth2/utils/bls/src/aggregate_public_key.rs index 47c95d6c9..2174a43cb 100644 --- a/eth2/utils/bls/src/aggregate_public_key.rs +++ b/eth2/utils/bls/src/aggregate_public_key.rs @@ -14,7 +14,7 @@ impl AggregatePublicKey { } pub fn add(&mut self, public_key: &PublicKey) { - self.0.add(&public_key.as_raw()) + self.0.add(public_key.as_raw()) } /// Returns the underlying signature. diff --git a/eth2/utils/bls/src/public_key.rs b/eth2/utils/bls/src/public_key.rs index 3e2bff19e..c85760bbf 100644 --- a/eth2/utils/bls/src/public_key.rs +++ b/eth2/utils/bls/src/public_key.rs @@ -10,58 +10,39 @@ use ssz::{ use std::default; use std::hash::{Hash, Hasher}; -/// A single BLS public key. -/// -/// This struct stores an uncompressed public key as a byte vec. The reason we store bytes instead -/// of the `RawPublicKey` struct is because it allows for building a hashmap of `PublicKey` much -/// faster. -/// -/// Storing as uncompressed bytes costs ~0.02% more time when adding a `PublicKey` to an -/// `AggregateKey`, however it saves ~0.5ms each time you need to add a pubkey to a hashmap. +/// A single BLS signature. /// /// This struct is a wrapper upon a base type and provides helper functions (e.g., SSZ /// serialization). #[derive(Debug, Clone, Eq)] -pub struct PublicKey { - bytes: Vec, -} +pub struct PublicKey(RawPublicKey); impl PublicKey { pub fn from_secret_key(secret_key: &SecretKey) -> Self { - let mut raw_key = RawPublicKey::from_secret_key(secret_key.as_raw()); - let uncompressed_bytes = raw_key.as_uncompressed_bytes(); - Self { - bytes: uncompressed_bytes, - } + PublicKey(RawPublicKey::from_secret_key(secret_key.as_raw())) } /// Returns the underlying signature. - pub fn as_raw(&self) -> RawPublicKey { - RawPublicKey::from_uncompressed_bytes(&self.bytes).expect("PublicKey in invalid state") + pub fn as_raw(&self) -> &RawPublicKey { + &self.0 } /// Converts compressed bytes to PublicKey pub fn from_bytes(bytes: &[u8]) -> Result { - let mut pubkey = RawPublicKey::from_bytes(&bytes).map_err(|_| DecodeError::Invalid)?; - Ok(Self { - bytes: pubkey.as_uncompressed_bytes(), - }) + let pubkey = RawPublicKey::from_bytes(&bytes).map_err(|_| DecodeError::Invalid)?; + Ok(PublicKey(pubkey)) } /// Returns the PublicKey as (x, y) bytes pub fn as_uncompressed_bytes(&self) -> Vec { - self.bytes.clone() + RawPublicKey::as_uncompressed_bytes(&mut self.0.clone()) } /// Converts (x, y) bytes to PublicKey pub fn from_uncompressed_bytes(bytes: &[u8]) -> Result { - // Do a conversion to check the bytes are valid. - let _pubkey = + let pubkey = RawPublicKey::from_uncompressed_bytes(&bytes).map_err(|_| DecodeError::Invalid)?; - - Ok(Self { - bytes: bytes.to_vec(), - }) + Ok(PublicKey(pubkey)) } /// Returns the last 6 bytes of the SSZ encoding of the public key, as a hex string. @@ -83,22 +64,15 @@ impl default::Default for PublicKey { impl Encodable for PublicKey { fn ssz_append(&self, s: &mut SszStream) { - s.append_vec(&self.as_raw().as_bytes()); + s.append_vec(&self.0.as_bytes()); } } impl Decodable for PublicKey { fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> { let (sig_bytes, i) = decode_ssz_list(bytes, i)?; - let mut raw_sig = - RawPublicKey::from_bytes(&sig_bytes).map_err(|_| DecodeError::TooShort)?; - - Ok(( - Self { - bytes: raw_sig.as_uncompressed_bytes(), - }, - i, - )) + let raw_sig = RawPublicKey::from_bytes(&sig_bytes).map_err(|_| DecodeError::TooShort)?; + Ok((PublicKey(raw_sig), i)) } } @@ -125,7 +99,7 @@ impl<'de> Deserialize<'de> for PublicKey { impl TreeHash for PublicKey { fn hash_tree_root(&self) -> Vec { - hash(&self.as_raw().as_bytes()) + hash(&self.0.as_bytes()) } } @@ -143,7 +117,7 @@ impl Hash for PublicKey { /// /// Use `ssz::Encode` to obtain the bytes required for consensus hashing. fn hash(&self, state: &mut H) { - self.bytes.hash(state) + self.as_uncompressed_bytes().hash(state) } } diff --git a/eth2/utils/bls/src/signature.rs b/eth2/utils/bls/src/signature.rs index 47e1dad2e..47598bc66 100644 --- a/eth2/utils/bls/src/signature.rs +++ b/eth2/utils/bls/src/signature.rs @@ -33,7 +33,7 @@ impl Signature { /// Verify the Signature against a PublicKey. pub fn verify(&self, msg: &[u8], domain: u64, pk: &PublicKey) -> bool { - self.0.verify(msg, domain, &pk.as_raw()) + self.0.verify(msg, domain, pk.as_raw()) } /// Verify the Signature against a PublicKey, where the message has already been hashed. @@ -44,7 +44,7 @@ impl Signature { pk: &PublicKey, ) -> bool { self.0 - .verify_hashed(x_real_hashed, x_imaginary_hashed, &pk.as_raw()) + .verify_hashed(x_real_hashed, x_imaginary_hashed, pk.as_raw()) } /// Returns the underlying signature.