Upgrade to signature scheme 0.6.0

This commit is contained in:
Kirk Baird 2019-03-12 16:01:09 +11:00
parent efd56ebe37
commit c92f867cd8
No known key found for this signature in database
GPG Key ID: BF864B7ED0BEA33F
5 changed files with 23 additions and 26 deletions

View File

@ -58,7 +58,7 @@ impl KeypairsFile for Vec<Keypair> {
let pk_start = sk_end; let pk_start = sk_end;
let pk_end = pk_start + PUBLIC_KEY_BYTES_LEN; 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")) .map_err(|_| Error::new(ErrorKind::Other, "Invalid PublicKey bytes"))
.unwrap(); .unwrap();

View File

@ -5,7 +5,7 @@ authors = ["Paul Hauner <paul@paulhauner.com>"]
edition = "2018" edition = "2018"
[dependencies] [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" } hashing = { path = "../hashing" }
hex = "0.3" hex = "0.3"
serde = "1.0" serde = "1.0"

View File

@ -48,15 +48,9 @@ impl AggregateSignature {
domain: u64, domain: u64,
aggregate_public_keys: &[&AggregatePublicKey], aggregate_public_keys: &[&AggregatePublicKey],
) -> bool { ) -> bool {
// TODO: the API for `RawAggregatePublicKey` shoudn't need to take an owned let aggregate_public_keys: Vec<&RawAggregatePublicKey> = aggregate_public_keys
// `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<RawAggregatePublicKey> = aggregate_public_keys
.iter() .iter()
.map(|pk| pk.as_raw()) .map(|pk| pk.as_raw())
.cloned()
.collect(); .collect();
// Messages are concatenated into one long message. // Messages are concatenated into one long message.

View File

@ -1,6 +1,6 @@
use super::serde_vistors::HexVisitor; use super::serde_vistors::HexVisitor;
use super::SecretKey; 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 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};
@ -22,18 +22,22 @@ impl PublicKey {
PublicKey(RawPublicKey::from_secret_key(secret_key.as_raw())) PublicKey(RawPublicKey::from_secret_key(secret_key.as_raw()))
} }
/// Instantiate a PublicKey from existing bytes. /// Returns the underlying signature.
///
/// Note: this is _not_ SSZ decoding.
pub fn from_bytes(bytes: &[u8]) -> Result<PublicKey, BlsDecodeError> {
Ok(Self(RawPublicKey::from_bytes(bytes)?))
}
/// Returns the underlying public key.
pub fn as_raw(&self) -> &RawPublicKey { pub fn as_raw(&self) -> &RawPublicKey {
&self.0 &self.0
} }
/// Returns the PublicKey as (x, y) bytes
pub fn as_uncompressed_bytes(&mut self) -> Vec<u8> {
RawPublicKey::as_uncompressed_bytes(&mut self.0)
}
/// Converts (x, y) bytes to PublicKey
pub fn from_uncompressed_bytes(bytes: &[u8]) -> Result<Self, DecodeError> {
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. /// 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. /// Useful for providing a short identifier to the user.
@ -100,11 +104,7 @@ impl PartialEq for PublicKey {
impl Hash for PublicKey { impl Hash for PublicKey {
fn hash<H: Hasher>(&self, state: &mut H) { fn hash<H: Hasher>(&self, state: &mut H) {
// Note: this is not necessarily the consensus-ready hash. Instead, it is designed to be ssz_encode(self).hash(state)
// optimally fast for internal usage.
//
// To hash for consensus purposes, use the SSZ-encoded bytes.
self.0.as_bytes().hash(state)
} }
} }

View File

@ -54,9 +54,12 @@ impl Signature {
/// Returns a new empty signature. /// Returns a new empty signature.
pub fn empty_signature() -> Self { 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<u8> = vec![0; 96]; let mut empty: Vec<u8> = vec![0; 96];
// TODO: Modify the way flags are used (b_flag should not be used for empty_signature in the future) // Sets C_flag and B_flag to 1 and all else to 0
empty[0] += u8::pow(2, 6); empty[0] += u8::pow(2, 6) + u8::pow(2, 7);
Signature(RawSignature::from_bytes(&empty).unwrap()) Signature(RawSignature::from_bytes(&empty).unwrap())
} }
} }
@ -129,7 +132,7 @@ mod tests {
assert_eq!(sig_as_bytes.len(), 96); assert_eq!(sig_as_bytes.len(), 96);
for (i, one_byte) in sig_as_bytes.iter().enumerate() { for (i, one_byte) in sig_as_bytes.iter().enumerate() {
if i == 0 { if i == 0 {
assert_eq!(*one_byte, u8::pow(2, 6)); assert_eq!(*one_byte, u8::pow(2, 6) + u8::pow(2, 7));
} else { } else {
assert_eq!(*one_byte, 0); assert_eq!(*one_byte, 0);
} }