Remove ssz encoding of length from; Signature, AggregateSiganture, PublicKey, SecretKey

This commit is contained in:
Kirk Baird 2019-03-26 16:45:25 +11:00
parent 336b3a8a59
commit fd2f9d0d15
No known key found for this signature in database
GPG Key ID: BF864B7ED0BEA33F
7 changed files with 43 additions and 46 deletions

View File

@ -4,8 +4,8 @@ use bls_aggregates::{
}; };
use serde::de::{Deserialize, Deserializer}; use serde::de::{Deserialize, Deserializer};
use serde::ser::{Serialize, Serializer}; use serde::ser::{Serialize, Serializer};
use serde_hex::{encode as hex_encode, PrefixedHexVisitor}; use serde_hex::{encode as hex_encode, HexVisitor};
use ssz::{decode_ssz_list, hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; use ssz::{decode, hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
/// A BLS aggregate signature. /// A BLS aggregate signature.
/// ///
@ -121,22 +121,18 @@ impl AggregateSignature {
impl Encodable for AggregateSignature { impl Encodable for AggregateSignature {
fn ssz_append(&self, s: &mut SszStream) { fn ssz_append(&self, s: &mut SszStream) {
s.append_vec(&self.as_bytes()); s.append_encoded_raw(&self.as_bytes());
} }
} }
impl Decodable for AggregateSignature { impl Decodable for AggregateSignature {
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> { fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
let (sig_bytes, i) = decode_ssz_list(bytes, i)?; if bytes.len() - i < BLS_AGG_SIG_BYTE_SIZE {
let raw_sig = return Err(DecodeError::TooShort);
RawAggregateSignature::from_bytes(&sig_bytes).map_err(|_| DecodeError::Invalid)?; }
Ok(( let agg_sig = AggregateSignature::from_bytes(&bytes[i..(i + BLS_AGG_SIG_BYTE_SIZE)])
Self { .map_err(|_| DecodeError::Invalid)?;
aggregate_signature: raw_sig, Ok((agg_sig, i + BLS_AGG_SIG_BYTE_SIZE))
is_empty: false,
},
i,
))
} }
} }
@ -156,8 +152,8 @@ impl<'de> Deserialize<'de> for AggregateSignature {
where where
D: Deserializer<'de>, D: Deserializer<'de>,
{ {
let bytes = deserializer.deserialize_str(PrefixedHexVisitor)?; let bytes = deserializer.deserialize_str(HexVisitor)?;
let agg_sig = AggregateSignature::from_bytes(&bytes[..]) let agg_sig = decode(&bytes[..])
.map_err(|e| serde::de::Error::custom(format!("invalid ssz ({:?})", e)))?; .map_err(|e| serde::de::Error::custom(format!("invalid ssz ({:?})", e)))?;
Ok(agg_sig) Ok(agg_sig)
} }

View File

@ -17,6 +17,8 @@ pub use crate::signature::Signature;
pub const BLS_AGG_SIG_BYTE_SIZE: usize = 96; pub const BLS_AGG_SIG_BYTE_SIZE: usize = 96;
pub const BLS_SIG_BYTE_SIZE: usize = 96; pub const BLS_SIG_BYTE_SIZE: usize = 96;
pub const BLS_SECRET_KEY_BYTE_SIZE: usize = 48;
pub const BLS_PUBLIC_KEY_BYTE_SIZE: usize = 48;
use hashing::hash; use hashing::hash;
use ssz::ssz_encode; use ssz::ssz_encode;

View File

@ -1,12 +1,9 @@
use super::SecretKey; use super::{SecretKey, BLS_PUBLIC_KEY_BYTE_SIZE};
use bls_aggregates::PublicKey as RawPublicKey; use bls_aggregates::PublicKey as RawPublicKey;
use serde::de::{Deserialize, Deserializer}; use serde::de::{Deserialize, Deserializer};
use serde::ser::{Serialize, Serializer}; use serde::ser::{Serialize, Serializer};
use serde_hex::{encode as hex_encode, PrefixedHexVisitor}; use serde_hex::{encode as hex_encode, HexVisitor};
use ssz::{ use ssz::{decode, hash, ssz_encode, Decodable, DecodeError, Encodable, SszStream, TreeHash};
decode, decode_ssz_list, hash, ssz_encode, Decodable, DecodeError, Encodable, SszStream,
TreeHash,
};
use std::default; use std::default;
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
@ -64,15 +61,18 @@ impl default::Default for PublicKey {
impl Encodable for PublicKey { impl Encodable for PublicKey {
fn ssz_append(&self, s: &mut SszStream) { fn ssz_append(&self, s: &mut SszStream) {
s.append_vec(&self.0.as_bytes()); s.append_encoded_raw(&self.0.as_bytes());
} }
} }
impl Decodable for PublicKey { impl Decodable for PublicKey {
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> { fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
let (sig_bytes, i) = decode_ssz_list(bytes, i)?; if bytes.len() - i < BLS_PUBLIC_KEY_BYTE_SIZE {
let raw_sig = RawPublicKey::from_bytes(&sig_bytes).map_err(|_| DecodeError::TooShort)?; return Err(DecodeError::TooShort);
Ok((PublicKey(raw_sig), i)) }
let raw_sig = RawPublicKey::from_bytes(&bytes[i..(i + BLS_PUBLIC_KEY_BYTE_SIZE)])
.map_err(|_| DecodeError::TooShort)?;
Ok((PublicKey(raw_sig), i + BLS_PUBLIC_KEY_BYTE_SIZE))
} }
} }
@ -90,8 +90,8 @@ impl<'de> Deserialize<'de> for PublicKey {
where where
D: Deserializer<'de>, D: Deserializer<'de>,
{ {
let bytes = deserializer.deserialize_str(PrefixedHexVisitor)?; let bytes = deserializer.deserialize_str(HexVisitor)?;
let pubkey = PublicKey::from_bytes(&bytes[..]) let pubkey = decode(&bytes[..])
.map_err(|e| serde::de::Error::custom(format!("invalid pubkey ({:?})", e)))?; .map_err(|e| serde::de::Error::custom(format!("invalid pubkey ({:?})", e)))?;
Ok(pubkey) Ok(pubkey)
} }

View File

@ -1,11 +1,10 @@
use super::BLS_SECRET_KEY_BYTE_SIZE;
use bls_aggregates::{DecodeError as BlsDecodeError, SecretKey as RawSecretKey}; use bls_aggregates::{DecodeError as BlsDecodeError, SecretKey as RawSecretKey};
use hex::encode as hex_encode; use hex::encode as hex_encode;
use serde::de::{Deserialize, Deserializer}; use serde::de::{Deserialize, Deserializer};
use serde::ser::{Serialize, Serializer}; use serde::ser::{Serialize, Serializer};
use serde_hex::HexVisitor; use serde_hex::HexVisitor;
use ssz::{ use ssz::{decode, ssz_encode, Decodable, DecodeError, Encodable, SszStream, TreeHash};
decode, decode_ssz_list, ssz_encode, Decodable, DecodeError, Encodable, SszStream, TreeHash,
};
/// A single BLS signature. /// A single BLS signature.
/// ///
@ -34,15 +33,18 @@ impl SecretKey {
impl Encodable for SecretKey { impl Encodable for SecretKey {
fn ssz_append(&self, s: &mut SszStream) { fn ssz_append(&self, s: &mut SszStream) {
s.append_vec(&self.0.as_bytes()); s.append_encoded_raw(&self.0.as_bytes());
} }
} }
impl Decodable for SecretKey { impl Decodable for SecretKey {
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> { fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
let (sig_bytes, i) = decode_ssz_list(bytes, i)?; if bytes.len() - i < BLS_SECRET_KEY_BYTE_SIZE {
let raw_sig = RawSecretKey::from_bytes(&sig_bytes).map_err(|_| DecodeError::TooShort)?; return Err(DecodeError::TooShort);
Ok((SecretKey(raw_sig), i)) }
let raw_sig = RawSecretKey::from_bytes(&bytes[i..(i + BLS_SECRET_KEY_BYTE_SIZE)])
.map_err(|_| DecodeError::TooShort)?;
Ok((SecretKey(raw_sig), i + BLS_SECRET_KEY_BYTE_SIZE))
} }
} }

View File

@ -4,10 +4,7 @@ use hex::encode as hex_encode;
use serde::de::{Deserialize, Deserializer}; use serde::de::{Deserialize, Deserializer};
use serde::ser::{Serialize, Serializer}; use serde::ser::{Serialize, Serializer};
use serde_hex::HexVisitor; use serde_hex::HexVisitor;
use ssz::{ use ssz::{decode, hash, ssz_encode, Decodable, DecodeError, Encodable, SszStream, TreeHash};
decode, decode_ssz_list, hash, ssz_encode, Decodable, DecodeError, Encodable, SszStream,
TreeHash,
};
/// A single BLS signature. /// A single BLS signature.
/// ///
@ -103,15 +100,17 @@ impl Signature {
impl Encodable for Signature { impl Encodable for Signature {
fn ssz_append(&self, s: &mut SszStream) { fn ssz_append(&self, s: &mut SszStream) {
s.append_vec(&self.as_bytes()); s.append_encoded_raw(&self.as_bytes());
} }
} }
impl Decodable for Signature { impl Decodable for Signature {
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> { fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
let (sig_bytes, i) = decode_ssz_list(bytes, i)?; if bytes.len() - i < BLS_SIG_BYTE_SIZE {
let signature = Signature::from_bytes(&sig_bytes)?; return Err(DecodeError::TooShort);
Ok((signature, i)) }
let signature = Signature::from_bytes(&bytes[i..(i + BLS_SIG_BYTE_SIZE)])?;
Ok((signature, i + BLS_SIG_BYTE_SIZE))
} }
} }
@ -138,7 +137,7 @@ impl<'de> Deserialize<'de> for Signature {
D: Deserializer<'de>, D: Deserializer<'de>,
{ {
let bytes = deserializer.deserialize_str(HexVisitor)?; let bytes = deserializer.deserialize_str(HexVisitor)?;
let signature = Signature::from_bytes(&bytes[..]) let signature = decode(&bytes[..])
.map_err(|e| serde::de::Error::custom(format!("invalid ssz ({:?})", e)))?; .map_err(|e| serde::de::Error::custom(format!("invalid ssz ({:?})", e)))?;
Ok(signature) Ok(signature)
} }

View File

@ -145,10 +145,7 @@ impl std::ops::BitAnd for BooleanBitfield {
} }
impl Encodable for BooleanBitfield { impl Encodable for BooleanBitfield {
<<<<<<< HEAD
=======
// ssz_append encodes Self according to the `ssz` spec. // ssz_append encodes Self according to the `ssz` spec.
>>>>>>> v0.5.0-state-transition-tests
fn ssz_append(&self, s: &mut ssz::SszStream) { fn ssz_append(&self, s: &mut ssz::SszStream) {
s.append_vec(&self.to_bytes()) s.append_vec(&self.to_bytes())
} }

View File

@ -15,6 +15,7 @@ pub trait Decodable: Sized {
/// ///
/// The single ssz encoded value/container/list will be decoded as the given type, /// The single ssz encoded value/container/list will be decoded as the given type,
/// by recursively calling `ssz_decode`. /// by recursively calling `ssz_decode`.
/// Check on totality for underflowing the length of bytes and overflow checks done per container
pub fn decode<T>(ssz_bytes: &[u8]) -> Result<(T), DecodeError> pub fn decode<T>(ssz_bytes: &[u8]) -> Result<(T), DecodeError>
where where
T: Decodable, T: Decodable,