2019-03-25 07:02:37 +00:00
|
|
|
use super::{PublicKey, SecretKey, BLS_SIG_BYTE_SIZE};
|
2019-02-14 01:09:18 +00:00
|
|
|
use bls_aggregates::Signature as RawSignature;
|
2019-04-26 05:24:18 +00:00
|
|
|
use cached_tree_hash::cached_tree_hash_ssz_encoding_as_vector;
|
2019-03-08 02:15:41 +00:00
|
|
|
use hex::encode as hex_encode;
|
|
|
|
use serde::de::{Deserialize, Deserializer};
|
2019-02-14 01:09:18 +00:00
|
|
|
use serde::ser::{Serialize, Serializer};
|
2019-03-26 04:44:01 +00:00
|
|
|
use serde_hex::HexVisitor;
|
2019-04-16 04:25:43 +00:00
|
|
|
use ssz::{decode, ssz_encode, Decodable, DecodeError, Encodable, SszStream};
|
2019-04-17 01:57:57 +00:00
|
|
|
use tree_hash::tree_hash_ssz_encoding_as_vector;
|
2019-02-14 01:09:18 +00:00
|
|
|
|
|
|
|
/// A single BLS signature.
|
|
|
|
///
|
|
|
|
/// This struct is a wrapper upon a base type and provides helper functions (e.g., SSZ
|
|
|
|
/// serialization).
|
|
|
|
#[derive(Debug, PartialEq, Clone, Eq)]
|
2019-03-22 03:51:49 +00:00
|
|
|
pub struct Signature {
|
|
|
|
signature: RawSignature,
|
|
|
|
is_empty: bool,
|
|
|
|
}
|
2019-02-14 01:09:18 +00:00
|
|
|
|
|
|
|
impl Signature {
|
|
|
|
/// Instantiate a new Signature from a message and a SecretKey.
|
2019-02-15 02:58:14 +00:00
|
|
|
pub fn new(msg: &[u8], domain: u64, sk: &SecretKey) -> Self {
|
2019-03-22 03:51:49 +00:00
|
|
|
Signature {
|
|
|
|
signature: RawSignature::new(msg, domain, sk.as_raw()),
|
|
|
|
is_empty: false,
|
|
|
|
}
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Instantiate a new Signature from a message and a SecretKey, where the message has already
|
|
|
|
/// been hashed.
|
2019-02-15 02:58:14 +00:00
|
|
|
pub fn new_hashed(x_real_hashed: &[u8], x_imaginary_hashed: &[u8], sk: &SecretKey) -> Self {
|
2019-03-22 03:51:49 +00:00
|
|
|
Signature {
|
2019-03-22 03:52:37 +00:00
|
|
|
signature: RawSignature::new_hashed(x_real_hashed, x_imaginary_hashed, sk.as_raw()),
|
2019-03-22 03:51:49 +00:00
|
|
|
is_empty: false,
|
|
|
|
}
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Verify the Signature against a PublicKey.
|
2019-02-15 02:58:14 +00:00
|
|
|
pub fn verify(&self, msg: &[u8], domain: u64, pk: &PublicKey) -> bool {
|
2019-03-22 03:51:49 +00:00
|
|
|
if self.is_empty {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
self.signature.verify(msg, domain, pk.as_raw())
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Verify the Signature against a PublicKey, where the message has already been hashed.
|
2019-02-17 23:50:40 +00:00
|
|
|
pub fn verify_hashed(
|
|
|
|
&self,
|
|
|
|
x_real_hashed: &[u8],
|
|
|
|
x_imaginary_hashed: &[u8],
|
|
|
|
pk: &PublicKey,
|
|
|
|
) -> bool {
|
2019-03-22 03:51:49 +00:00
|
|
|
self.signature
|
2019-02-17 23:50:40 +00:00
|
|
|
.verify_hashed(x_real_hashed, x_imaginary_hashed, pk.as_raw())
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the underlying signature.
|
|
|
|
pub fn as_raw(&self) -> &RawSignature {
|
2019-03-22 03:51:49 +00:00
|
|
|
&self.signature
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a new empty signature.
|
|
|
|
pub fn empty_signature() -> Self {
|
2019-03-22 03:51:49 +00:00
|
|
|
// Set RawSignature = infinity
|
2019-03-25 07:02:37 +00:00
|
|
|
let mut empty: Vec<u8> = vec![0; BLS_SIG_BYTE_SIZE];
|
2019-03-12 05:01:09 +00:00
|
|
|
empty[0] += u8::pow(2, 6) + u8::pow(2, 7);
|
2019-03-22 03:51:49 +00:00
|
|
|
Signature {
|
|
|
|
signature: RawSignature::from_bytes(&empty).unwrap(),
|
|
|
|
is_empty: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Converts a BLS Signature to bytes
|
|
|
|
pub fn as_bytes(&self) -> Vec<u8> {
|
|
|
|
if self.is_empty {
|
|
|
|
return vec![0; 96];
|
|
|
|
}
|
|
|
|
self.signature.as_bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert bytes to BLS Signature
|
|
|
|
pub fn from_bytes(bytes: &[u8]) -> Result<Self, DecodeError> {
|
|
|
|
for byte in bytes {
|
|
|
|
if *byte != 0 {
|
2019-03-22 03:52:37 +00:00
|
|
|
let raw_signature =
|
|
|
|
RawSignature::from_bytes(&bytes).map_err(|_| DecodeError::Invalid)?;
|
2019-03-22 03:51:49 +00:00
|
|
|
return Ok(Signature {
|
|
|
|
signature: raw_signature,
|
|
|
|
is_empty: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(Signature::empty_signature())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for empty Signature
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.is_empty
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Encodable for Signature {
|
|
|
|
fn ssz_append(&self, s: &mut SszStream) {
|
2019-03-26 05:45:25 +00:00
|
|
|
s.append_encoded_raw(&self.as_bytes());
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Decodable for Signature {
|
|
|
|
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
|
2019-03-26 05:45:25 +00:00
|
|
|
if bytes.len() - i < BLS_SIG_BYTE_SIZE {
|
|
|
|
return Err(DecodeError::TooShort);
|
|
|
|
}
|
|
|
|
let signature = Signature::from_bytes(&bytes[i..(i + BLS_SIG_BYTE_SIZE)])?;
|
|
|
|
Ok((signature, i + BLS_SIG_BYTE_SIZE))
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-17 01:57:57 +00:00
|
|
|
tree_hash_ssz_encoding_as_vector!(Signature);
|
2019-04-26 05:24:18 +00:00
|
|
|
cached_tree_hash_ssz_encoding_as_vector!(Signature, 96);
|
2019-02-14 01:09:18 +00:00
|
|
|
|
|
|
|
impl Serialize for Signature {
|
2019-03-25 07:02:37 +00:00
|
|
|
/// Serde serialization is compliant the Ethereum YAML test format.
|
2019-02-14 01:09:18 +00:00
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
2019-03-08 02:15:41 +00:00
|
|
|
serializer.serialize_str(&hex_encode(ssz_encode(self)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'de> Deserialize<'de> for Signature {
|
2019-03-25 07:02:37 +00:00
|
|
|
/// Serde serialization is compliant the Ethereum YAML test format.
|
2019-03-08 02:15:41 +00:00
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
|
|
where
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
{
|
|
|
|
let bytes = deserializer.deserialize_str(HexVisitor)?;
|
2019-03-26 05:45:25 +00:00
|
|
|
let signature = decode(&bytes[..])
|
2019-03-08 02:15:41 +00:00
|
|
|
.map_err(|e| serde::de::Error::custom(format!("invalid ssz ({:?})", e)))?;
|
2019-03-25 07:02:37 +00:00
|
|
|
Ok(signature)
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::super::Keypair;
|
|
|
|
use super::*;
|
|
|
|
use ssz::ssz_encode;
|
2019-04-26 05:24:18 +00:00
|
|
|
use tree_hash::TreeHash;
|
2019-02-14 01:09:18 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn test_ssz_round_trip() {
|
|
|
|
let keypair = Keypair::random();
|
|
|
|
|
2019-02-15 02:58:14 +00:00
|
|
|
let original = Signature::new(&[42, 42], 0, &keypair.sk);
|
2019-02-14 01:09:18 +00:00
|
|
|
|
|
|
|
let bytes = ssz_encode(&original);
|
2019-03-18 07:11:46 +00:00
|
|
|
let decoded = decode::<Signature>(&bytes).unwrap();
|
2019-02-14 01:09:18 +00:00
|
|
|
|
|
|
|
assert_eq!(original, decoded);
|
|
|
|
}
|
|
|
|
|
2019-04-26 05:24:18 +00:00
|
|
|
#[test]
|
|
|
|
pub fn test_cached_tree_hash() {
|
|
|
|
let keypair = Keypair::random();
|
|
|
|
let original = Signature::new(&[42, 42], 0, &keypair.sk);
|
|
|
|
|
2019-04-29 07:46:01 +00:00
|
|
|
let mut cache = cached_tree_hash::TreeHashCache::new(&original).unwrap();
|
2019-04-26 05:24:18 +00:00
|
|
|
|
2019-04-29 07:46:01 +00:00
|
|
|
assert_eq!(
|
|
|
|
cache.tree_hash_root().unwrap().to_vec(),
|
|
|
|
original.tree_hash_root()
|
|
|
|
);
|
2019-04-26 05:24:18 +00:00
|
|
|
|
|
|
|
let modified = Signature::new(&[99, 99], 0, &keypair.sk);
|
|
|
|
|
2019-04-29 07:46:01 +00:00
|
|
|
cache.update(&modified).unwrap();
|
2019-04-26 05:24:18 +00:00
|
|
|
|
2019-04-29 07:46:01 +00:00
|
|
|
assert_eq!(
|
|
|
|
cache.tree_hash_root().unwrap().to_vec(),
|
|
|
|
modified.tree_hash_root()
|
|
|
|
);
|
2019-04-26 05:24:18 +00:00
|
|
|
}
|
|
|
|
|
2019-02-14 01:09:18 +00:00
|
|
|
#[test]
|
|
|
|
pub fn test_empty_signature() {
|
|
|
|
let sig = Signature::empty_signature();
|
|
|
|
|
|
|
|
let sig_as_bytes: Vec<u8> = sig.as_raw().as_bytes();
|
|
|
|
|
2019-03-25 07:02:37 +00:00
|
|
|
assert_eq!(sig_as_bytes.len(), BLS_SIG_BYTE_SIZE);
|
2019-02-17 23:50:40 +00:00
|
|
|
for (i, one_byte) in sig_as_bytes.iter().enumerate() {
|
|
|
|
if i == 0 {
|
2019-03-12 05:01:09 +00:00
|
|
|
assert_eq!(*one_byte, u8::pow(2, 6) + u8::pow(2, 7));
|
2019-02-17 23:50:40 +00:00
|
|
|
} else {
|
|
|
|
assert_eq!(*one_byte, 0);
|
|
|
|
}
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|