mirror of
https://github.com/sigp/lighthouse.git
synced 2026-04-28 02:03:32 +00:00
Support multiple BLS implementations (#1335)
## Issue Addressed NA ## Proposed Changes - Refactor the `bls` crate to support multiple BLS "backends" (e.g., milagro, blst, etc). - Removes some duplicate, unused code in `common/rest_types/src/validator.rs`. - Removes the old "upgrade legacy keypairs" functionality (these were unencrypted keys that haven't been supported for a few testnets, no one should be using them anymore). ## Additional Info Most of the files changed are just inconsequential changes to function names. ## TODO - [x] Optimization levels - [x] Infinity point: https://github.com/supranational/blst/issues/11 - [x] Ensure milagro *and* blst are tested via CI - [x] What to do with unsafe code? - [x] Test infinity point in signature sets
This commit is contained in:
115
crypto/bls/src/generic_public_key.rs
Normal file
115
crypto/bls/src/generic_public_key.rs
Normal file
@@ -0,0 +1,115 @@
|
||||
use crate::Error;
|
||||
use serde::de::{Deserialize, Deserializer};
|
||||
use serde::ser::{Serialize, Serializer};
|
||||
use serde_hex::{encode as hex_encode, PrefixedHexVisitor};
|
||||
use ssz::{Decode, Encode};
|
||||
use std::fmt;
|
||||
use std::hash::{Hash, Hasher};
|
||||
use tree_hash::TreeHash;
|
||||
|
||||
/// The byte-length of a BLS public key when serialized in compressed form.
|
||||
pub const PUBLIC_KEY_BYTES_LEN: usize = 48;
|
||||
|
||||
/// Represents the public key at infinity.
|
||||
pub const INFINITY_PUBLIC_KEY: [u8; PUBLIC_KEY_BYTES_LEN] = [
|
||||
0xc0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
];
|
||||
|
||||
/// Implemented on some struct from a BLS library so it may be used as the `point` in a
|
||||
/// `GenericPublicKey`.
|
||||
pub trait TPublicKey: Sized + Clone {
|
||||
/// Serialize `self` as compressed bytes.
|
||||
fn serialize(&self) -> [u8; PUBLIC_KEY_BYTES_LEN];
|
||||
|
||||
/// Deserialize `self` from compressed bytes.
|
||||
fn deserialize(bytes: &[u8]) -> Result<Self, Error>;
|
||||
}
|
||||
|
||||
/// A BLS aggregate public key that is generic across some BLS point (`Pub`).
|
||||
///
|
||||
/// Provides generic functionality whilst deferring all serious cryptographic operations to `Pub`.
|
||||
#[derive(Clone)]
|
||||
pub struct GenericPublicKey<Pub> {
|
||||
/// The underlying point which performs *actual* cryptographic operations.
|
||||
point: Pub,
|
||||
/// True if this point is equal to the `INFINITY_PUBLIC_KEY`.
|
||||
pub(crate) is_infinity: bool,
|
||||
}
|
||||
|
||||
impl<Pub> GenericPublicKey<Pub>
|
||||
where
|
||||
Pub: TPublicKey,
|
||||
{
|
||||
/// Instantiates `Self` from a `point`.
|
||||
pub(crate) fn from_point(point: Pub, is_infinity: bool) -> Self {
|
||||
Self { point, is_infinity }
|
||||
}
|
||||
|
||||
/// Returns a reference to the underlying BLS point.
|
||||
pub(crate) fn point(&self) -> &Pub {
|
||||
&self.point
|
||||
}
|
||||
|
||||
/// Returns `self.serialize()` as a `0x`-prefixed hex string.
|
||||
pub fn to_hex_string(&self) -> String {
|
||||
format!("{:?}", self)
|
||||
}
|
||||
|
||||
/// Serialize `self` as compressed bytes.
|
||||
pub fn serialize(&self) -> [u8; PUBLIC_KEY_BYTES_LEN] {
|
||||
self.point.serialize()
|
||||
}
|
||||
|
||||
/// Deserialize `self` from compressed bytes.
|
||||
pub fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
|
||||
Ok(Self {
|
||||
point: Pub::deserialize(bytes)?,
|
||||
is_infinity: bytes == &INFINITY_PUBLIC_KEY[..],
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<Pub: TPublicKey> Eq for GenericPublicKey<Pub> {}
|
||||
|
||||
impl<Pub: TPublicKey> PartialEq for GenericPublicKey<Pub> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.serialize()[..] == other.serialize()[..]
|
||||
}
|
||||
}
|
||||
|
||||
/// Hashes the `self.serialize()` bytes.
|
||||
impl<Pub: TPublicKey> Hash for GenericPublicKey<Pub> {
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
self.serialize()[..].hash(state);
|
||||
}
|
||||
}
|
||||
|
||||
impl<Pub: TPublicKey> Encode for GenericPublicKey<Pub> {
|
||||
impl_ssz_encode!(PUBLIC_KEY_BYTES_LEN);
|
||||
}
|
||||
|
||||
impl<Pub: TPublicKey> Decode for GenericPublicKey<Pub> {
|
||||
impl_ssz_decode!(PUBLIC_KEY_BYTES_LEN);
|
||||
}
|
||||
|
||||
impl<Pub: TPublicKey> TreeHash for GenericPublicKey<Pub> {
|
||||
impl_tree_hash!(PUBLIC_KEY_BYTES_LEN);
|
||||
}
|
||||
|
||||
impl<Pub: TPublicKey> Serialize for GenericPublicKey<Pub> {
|
||||
impl_serde_serialize!();
|
||||
}
|
||||
|
||||
impl<'de, Pub: TPublicKey> Deserialize<'de> for GenericPublicKey<Pub> {
|
||||
impl_serde_deserialize!();
|
||||
}
|
||||
|
||||
impl<Pub: TPublicKey> fmt::Debug for GenericPublicKey<Pub> {
|
||||
impl_debug!();
|
||||
}
|
||||
|
||||
#[cfg(feature = "arbitrary")]
|
||||
impl<Pub: TPublicKey + 'static> arbitrary::Arbitrary for GenericPublicKey<Pub> {
|
||||
impl_arbitrary!(PUBLIC_KEY_BYTES_LEN);
|
||||
}
|
||||
Reference in New Issue
Block a user