mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-16 19:32:55 +00:00
* Start updating types * WIP * Signature hacking * Existing EF tests passing with fake_crypto * Updates * Delete outdated API spec * The refactor continues * It compiles * WIP test fixes * All release tests passing bar genesis state parsing * Update and test YamlConfig * Update to spec v0.10 compatible BLS * Updates to BLS EF tests * Add EF test for AggregateVerify And delete unused hash2curve tests for uncompressed points * Update EF tests to v0.10.1 * Use optional block root correctly in block proc * Use genesis fork in deposit domain. All tests pass * Cargo fmt * Fast aggregate verify test * Update REST API docs * Cargo fmt * Fix unused import * Bump spec tags to v0.10.1 * Add `seconds_per_eth1_block` to chainspec * Update to timestamp based eth1 voting scheme * Return None from `get_votes_to_consider` if block cache is empty * Handle overflows in `is_candidate_block` * Revert to failing tests * Fix eth1 data sets test * Choose default vote according to spec * Fix collect_valid_votes tests * Fix `get_votes_to_consider` to choose all eligible blocks * Uncomment winning_vote tests * Add comments; remove unused code * Reduce seconds_per_eth1_block for simulation * Addressed review comments * Add test for default vote case * Fix logs * Remove unused functions * Meter default eth1 votes * Fix comments * Address review comments; remove unused dependency * Disable/delete two outdated tests * Bump eth1 default vote warn to error * Delete outdated eth1 test Co-authored-by: Pawan Dhananjay <pawandhananjay@gmail.com>
157 lines
4.4 KiB
Rust
157 lines
4.4 KiB
Rust
use super::{PublicKey, SecretKey, BLS_SIG_BYTE_SIZE};
|
|
use milagro_bls::Signature as RawSignature;
|
|
use serde::de::{Deserialize, Deserializer};
|
|
use serde::ser::{Serialize, Serializer};
|
|
use serde_hex::{encode as hex_encode, PrefixedHexVisitor};
|
|
use ssz::{ssz_encode, Decode, DecodeError, Encode};
|
|
|
|
/// 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)]
|
|
pub struct Signature {
|
|
signature: RawSignature,
|
|
is_empty: bool,
|
|
}
|
|
|
|
impl Signature {
|
|
/// Instantiate a new Signature from a message and a SecretKey.
|
|
pub fn new(msg: &[u8], sk: &SecretKey) -> Self {
|
|
Signature {
|
|
signature: RawSignature::new(msg, sk.as_raw()),
|
|
is_empty: false,
|
|
}
|
|
}
|
|
|
|
/// Verify the Signature against a PublicKey.
|
|
pub fn verify(&self, msg: &[u8], pk: &PublicKey) -> bool {
|
|
if self.is_empty {
|
|
return false;
|
|
}
|
|
self.signature.verify(msg, pk.as_raw())
|
|
}
|
|
|
|
/// Returns the underlying signature.
|
|
pub fn as_raw(&self) -> &RawSignature {
|
|
&self.signature
|
|
}
|
|
|
|
/// Returns a new empty signature.
|
|
pub fn empty_signature() -> Self {
|
|
// Set RawSignature = infinity
|
|
let mut empty: Vec<u8> = vec![0; BLS_SIG_BYTE_SIZE];
|
|
empty[0] += u8::pow(2, 6) + u8::pow(2, 7);
|
|
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 {
|
|
let raw_signature = RawSignature::from_bytes(&bytes).map_err(|_| {
|
|
DecodeError::BytesInvalid(format!("Invalid Signature bytes: {:?}", bytes))
|
|
})?;
|
|
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
|
|
}
|
|
|
|
/// Display a signature as a hex string of its bytes.
|
|
#[cfg(test)]
|
|
pub fn as_hex_string(&self) -> String {
|
|
hex_encode(self.as_bytes())
|
|
}
|
|
}
|
|
|
|
impl_ssz!(Signature, BLS_SIG_BYTE_SIZE, "Signature");
|
|
|
|
impl_tree_hash!(Signature, BLS_SIG_BYTE_SIZE);
|
|
|
|
impl Serialize for Signature {
|
|
/// Serde serialization is compliant the Ethereum YAML test format.
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
where
|
|
S: Serializer,
|
|
{
|
|
serializer.serialize_str(&hex_encode(ssz_encode(self)))
|
|
}
|
|
}
|
|
|
|
impl<'de> Deserialize<'de> for Signature {
|
|
/// Serde serialization is compliant the Ethereum YAML test format.
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
where
|
|
D: Deserializer<'de>,
|
|
{
|
|
let bytes = deserializer.deserialize_str(PrefixedHexVisitor)?;
|
|
let signature = Self::from_ssz_bytes(&bytes[..])
|
|
.map_err(|e| serde::de::Error::custom(format!("invalid ssz ({:?})", e)))?;
|
|
Ok(signature)
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::super::Keypair;
|
|
use super::*;
|
|
use ssz::ssz_encode;
|
|
|
|
#[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::from_ssz_bytes(&bytes).unwrap();
|
|
|
|
assert_eq!(original, decoded);
|
|
}
|
|
|
|
#[test]
|
|
pub fn test_byte_size() {
|
|
let keypair = Keypair::random();
|
|
|
|
let signature = Signature::new(&[42, 42], &keypair.sk);
|
|
let bytes = ssz_encode(&signature);
|
|
assert_eq!(bytes.len(), BLS_SIG_BYTE_SIZE);
|
|
}
|
|
|
|
#[test]
|
|
pub fn test_empty_signature() {
|
|
let sig = Signature::empty_signature();
|
|
|
|
let sig_as_bytes: Vec<u8> = sig.as_raw().as_bytes();
|
|
|
|
assert_eq!(sig_as_bytes.len(), BLS_SIG_BYTE_SIZE);
|
|
for (i, one_byte) in sig_as_bytes.iter().enumerate() {
|
|
if i == 0 {
|
|
assert_eq!(*one_byte, u8::pow(2, 6) + u8::pow(2, 7));
|
|
} else {
|
|
assert_eq!(*one_byte, 0);
|
|
}
|
|
}
|
|
}
|
|
}
|