mirror of
https://github.com/sigp/lighthouse.git
synced 2026-05-01 03:33:47 +00:00
add serialize_uncompressed to GenericSignature
This commit is contained in:
@@ -14,6 +14,9 @@ use tree_hash::TreeHash;
|
|||||||
/// The byte-length of a BLS signature when serialized in compressed form.
|
/// The byte-length of a BLS signature when serialized in compressed form.
|
||||||
pub const SIGNATURE_BYTES_LEN: usize = 96;
|
pub const SIGNATURE_BYTES_LEN: usize = 96;
|
||||||
|
|
||||||
|
/// The byte-length of a BLS signature when serialized in uncompressed form.
|
||||||
|
pub const SIGNATURE_UNCOMPRESSED_BYTES_LEN: usize = 192;
|
||||||
|
|
||||||
/// Represents the signature at infinity.
|
/// Represents the signature at infinity.
|
||||||
pub const INFINITY_SIGNATURE: [u8; SIGNATURE_BYTES_LEN] = [
|
pub const INFINITY_SIGNATURE: [u8; SIGNATURE_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,
|
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,
|
||||||
@@ -31,6 +34,9 @@ pub trait TSignature<GenericPublicKey>: Sized + Clone {
|
|||||||
/// Serialize `self` as compressed bytes.
|
/// Serialize `self` as compressed bytes.
|
||||||
fn serialize(&self) -> [u8; SIGNATURE_BYTES_LEN];
|
fn serialize(&self) -> [u8; SIGNATURE_BYTES_LEN];
|
||||||
|
|
||||||
|
/// Serialize `self` as uncompressed bytes.
|
||||||
|
fn serialize_uncompressed(&self) -> [u8; SIGNATURE_UNCOMPRESSED_BYTES_LEN];
|
||||||
|
|
||||||
/// Deserialize `self` from compressed bytes.
|
/// Deserialize `self` from compressed bytes.
|
||||||
fn deserialize(bytes: &[u8]) -> Result<Self, Error>;
|
fn deserialize(bytes: &[u8]) -> Result<Self, Error>;
|
||||||
|
|
||||||
@@ -115,6 +121,11 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Serialize `self` as compressed bytes.
|
||||||
|
pub fn serialize_uncompressed(&self) -> Option<[u8; SIGNATURE_UNCOMPRESSED_BYTES_LEN]> {
|
||||||
|
self.point.as_ref().map(|point| point.serialize_uncompressed())
|
||||||
|
}
|
||||||
|
|
||||||
/// Deserialize `self` from compressed bytes.
|
/// Deserialize `self` from compressed bytes.
|
||||||
pub fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
|
pub fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
|
||||||
let point = if bytes == &NONE_SIGNATURE[..] {
|
let point = if bytes == &NONE_SIGNATURE[..] {
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ use crate::{
|
|||||||
pub use blst::min_pk as blst_core;
|
pub use blst::min_pk as blst_core;
|
||||||
use blst::{blst_scalar, BLST_ERROR};
|
use blst::{blst_scalar, BLST_ERROR};
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
use crate::generic_signature::SIGNATURE_UNCOMPRESSED_BYTES_LEN;
|
||||||
|
|
||||||
pub const DST: &[u8] = b"BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_";
|
pub const DST: &[u8] = b"BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_";
|
||||||
pub const RAND_BITS: usize = 64;
|
pub const RAND_BITS: usize = 64;
|
||||||
@@ -189,6 +190,10 @@ impl TSignature<blst_core::PublicKey> for blst_core::Signature {
|
|||||||
self.to_bytes()
|
self.to_bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn serialize_uncompressed(&self) -> [u8; SIGNATURE_UNCOMPRESSED_BYTES_LEN] {
|
||||||
|
self.serialize()
|
||||||
|
}
|
||||||
|
|
||||||
fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
|
fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
|
||||||
Self::from_bytes(bytes).map_err(Into::into)
|
Self::from_bytes(bytes).map_err(Into::into)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ use crate::{
|
|||||||
generic_signature::{TSignature, SIGNATURE_BYTES_LEN},
|
generic_signature::{TSignature, SIGNATURE_BYTES_LEN},
|
||||||
Error, Hash256, ZeroizeHash, INFINITY_PUBLIC_KEY, INFINITY_SIGNATURE,
|
Error, Hash256, ZeroizeHash, INFINITY_PUBLIC_KEY, INFINITY_SIGNATURE,
|
||||||
};
|
};
|
||||||
|
use crate::generic_signature::SIGNATURE_UNCOMPRESSED_BYTES_LEN;
|
||||||
|
|
||||||
/// Provides the externally-facing, core BLS types.
|
/// Provides the externally-facing, core BLS types.
|
||||||
pub mod types {
|
pub mod types {
|
||||||
@@ -106,6 +107,12 @@ impl TSignature<PublicKey> for Signature {
|
|||||||
self.0
|
self.0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn serialize_uncompressed(&self) -> [u8; SIGNATURE_UNCOMPRESSED_BYTES_LEN] {
|
||||||
|
let mut ret = [0; SIGNATURE_UNCOMPRESSED_BYTES_LEN];
|
||||||
|
ret[0..SIGNATURE_BYTES_LEN].copy_from_slice(&self.0);
|
||||||
|
ret
|
||||||
|
}
|
||||||
|
|
||||||
fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
|
fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
|
||||||
let mut signature = Self::infinity();
|
let mut signature = Self::infinity();
|
||||||
signature.0[..].copy_from_slice(&bytes[0..SIGNATURE_BYTES_LEN]);
|
signature.0[..].copy_from_slice(&bytes[0..SIGNATURE_BYTES_LEN]);
|
||||||
|
|||||||
Reference in New Issue
Block a user