Revert "Move PublicKey
to store uncomp. bytes."
This reverts commit bfa2e71b46
.
This commit is contained in:
parent
587be831b5
commit
b2fb2afb28
@ -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.
|
||||
|
@ -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<u8>,
|
||||
}
|
||||
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<Self, DecodeError> {
|
||||
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<u8> {
|
||||
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<Self, DecodeError> {
|
||||
// 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<u8> {
|
||||
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<H: Hasher>(&self, state: &mut H) {
|
||||
self.bytes.hash(state)
|
||||
self.as_uncompressed_bytes().hash(state)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user