2018-12-29 03:31:16 +00:00
|
|
|
use super::{PublicKey, SecretKey};
|
|
|
|
use bls_aggregates::Signature as RawSignature;
|
2019-01-25 21:25:31 +00:00
|
|
|
use serde::ser::{Serialize, Serializer};
|
|
|
|
use ssz::{
|
|
|
|
decode_ssz_list, hash, ssz_encode, Decodable, DecodeError, Encodable, SszStream, TreeHash,
|
|
|
|
};
|
2018-12-18 06:04:54 +00:00
|
|
|
|
|
|
|
/// A single BLS signature.
|
|
|
|
///
|
|
|
|
/// This struct is a wrapper upon a base type and provides helper functions (e.g., SSZ
|
|
|
|
/// serialization).
|
2019-01-16 09:34:24 +00:00
|
|
|
#[derive(Debug, PartialEq, Clone, Eq)]
|
2018-12-18 06:04:54 +00:00
|
|
|
pub struct Signature(RawSignature);
|
|
|
|
|
|
|
|
impl Signature {
|
|
|
|
/// Instantiate a new Signature from a message and a SecretKey.
|
|
|
|
pub fn new(msg: &[u8], sk: &SecretKey) -> Self {
|
2018-12-29 03:31:16 +00:00
|
|
|
Signature(RawSignature::new(msg, sk.as_raw()))
|
2018-12-18 06:04:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Instantiate a new Signature from a message and a SecretKey, where the message has already
|
|
|
|
/// been hashed.
|
|
|
|
pub fn new_hashed(msg_hashed: &[u8], sk: &SecretKey) -> Self {
|
2018-12-29 03:31:16 +00:00
|
|
|
Signature(RawSignature::new_hashed(msg_hashed, sk.as_raw()))
|
2018-12-18 06:04:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Verify the Signature against a PublicKey.
|
|
|
|
pub fn verify(&self, msg: &[u8], pk: &PublicKey) -> bool {
|
2018-12-28 08:45:59 +00:00
|
|
|
self.0.verify(msg, pk.as_raw())
|
2018-12-18 06:04:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Verify the Signature against a PublicKey, where the message has already been hashed.
|
|
|
|
pub fn verify_hashed(&self, msg_hash: &[u8], pk: &PublicKey) -> bool {
|
2018-12-28 08:45:59 +00:00
|
|
|
self.0.verify_hashed(msg_hash, pk.as_raw())
|
2018-12-18 06:04:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the underlying signature.
|
|
|
|
pub fn as_raw(&self) -> &RawSignature {
|
|
|
|
&self.0
|
|
|
|
}
|
2019-01-15 23:39:02 +00:00
|
|
|
|
|
|
|
/// Returns a new empty signature.
|
2019-01-17 22:59:07 +00:00
|
|
|
pub fn empty_signature() -> Self {
|
2019-01-15 23:39:02 +00:00
|
|
|
let empty: Vec<u8> = vec![0; 97];
|
|
|
|
Signature(RawSignature::from_bytes(&empty).unwrap())
|
|
|
|
}
|
2018-12-18 06:04:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Encodable for Signature {
|
|
|
|
fn ssz_append(&self, s: &mut SszStream) {
|
|
|
|
s.append_vec(&self.0.as_bytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Decodable for Signature {
|
|
|
|
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
|
|
|
|
let (sig_bytes, i) = decode_ssz_list(bytes, i)?;
|
|
|
|
let raw_sig = RawSignature::from_bytes(&sig_bytes).map_err(|_| DecodeError::TooShort)?;
|
|
|
|
Ok((Signature(raw_sig), i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-25 01:22:56 +00:00
|
|
|
impl TreeHash for Signature {
|
|
|
|
fn hash_tree_root(&self) -> Vec<u8> {
|
|
|
|
hash(&self.0.as_bytes())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-25 21:25:31 +00:00
|
|
|
impl Serialize for Signature {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
|
|
|
serializer.serialize_bytes(&ssz_encode(self))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-18 06:04:54 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::super::Keypair;
|
|
|
|
use super::*;
|
2019-01-25 05:46:16 +00:00
|
|
|
use ssz::ssz_encode;
|
2018-12-18 06:04:54 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn test_ssz_round_trip() {
|
|
|
|
let keypair = Keypair::random();
|
|
|
|
|
|
|
|
let original = Signature::new(&[42, 42], &keypair.sk);
|
|
|
|
|
|
|
|
let bytes = ssz_encode(&original);
|
|
|
|
let (decoded, _) = Signature::ssz_decode(&bytes, 0).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(original, decoded);
|
|
|
|
}
|
2019-01-15 23:39:02 +00:00
|
|
|
|
|
|
|
#[test]
|
2019-01-17 22:59:07 +00:00
|
|
|
pub fn test_empty_signature() {
|
|
|
|
let sig = Signature::empty_signature();
|
2019-01-15 23:39:02 +00:00
|
|
|
|
|
|
|
let sig_as_bytes: Vec<u8> = sig.as_raw().as_bytes();
|
|
|
|
|
|
|
|
assert_eq!(sig_as_bytes.len(), 97);
|
|
|
|
for one_byte in sig_as_bytes.iter() {
|
|
|
|
assert_eq!(*one_byte, 0);
|
|
|
|
}
|
|
|
|
}
|
2018-12-18 06:04:54 +00:00
|
|
|
}
|