mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-06 10:11:44 +00:00
Simplifications for ./crypto (#4677)
This commit is contained in:
@@ -18,9 +18,9 @@ hex = "0.4.2"
|
||||
ethereum_hashing = "1.0.0-beta.2"
|
||||
c-kzg = { git = "https://github.com/ethereum/c-kzg-4844", rev = "fa3c62989527073fdce8b2138bb27a52bb2407c5" , features = ["mainnet-spec"]}
|
||||
c_kzg_min = { package = "c-kzg", git = "https://github.com/ethereum//c-kzg-4844", rev = "fa3c62989527073fdce8b2138bb27a52bb2407c5", features = ["minimal-spec"], optional = true }
|
||||
arbitrary = { version = "1.0", features = ["derive"], optional = true }
|
||||
arbitrary = { version = "1.0", features = ["derive"] }
|
||||
|
||||
[features]
|
||||
# TODO(deneb): enabled by default for convenience, would need more cfg magic to disable
|
||||
default = ["c_kzg_min"]
|
||||
minimal-spec = ["c_kzg_min"]
|
||||
minimal-spec = ["c_kzg_min"]
|
||||
|
||||
@@ -9,7 +9,7 @@ use std::fmt::{Debug, Display, Formatter};
|
||||
use std::str::FromStr;
|
||||
use tree_hash::{Hash256, PackedEncoding, TreeHash};
|
||||
|
||||
pub const BLOB_COMMITMENT_VERSION_KZG: u8 = 0x01;
|
||||
pub const VERSIONED_HASH_VERSION_KZG: u8 = 0x01;
|
||||
|
||||
#[derive(Derivative, Clone, Copy, Encode, Decode)]
|
||||
#[derivative(PartialEq, Eq, Hash)]
|
||||
@@ -19,9 +19,13 @@ pub struct KzgCommitment(pub [u8; c_kzg::BYTES_PER_COMMITMENT]);
|
||||
impl KzgCommitment {
|
||||
pub fn calculate_versioned_hash(&self) -> Hash256 {
|
||||
let mut versioned_hash = hash_fixed(&self.0);
|
||||
versioned_hash[0] = BLOB_COMMITMENT_VERSION_KZG;
|
||||
versioned_hash[0] = VERSIONED_HASH_VERSION_KZG;
|
||||
Hash256::from_slice(versioned_hash.as_slice())
|
||||
}
|
||||
|
||||
pub fn empty_for_testing() -> Self {
|
||||
KzgCommitment([0; c_kzg::BYTES_PER_COMMITMENT])
|
||||
}
|
||||
}
|
||||
|
||||
impl From<KzgCommitment> for c_kzg::Bytes48 {
|
||||
@@ -42,12 +46,6 @@ impl Display for KzgCommitment {
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for KzgCommitment {
|
||||
fn default() -> Self {
|
||||
KzgCommitment([0; BYTES_PER_COMMITMENT])
|
||||
}
|
||||
}
|
||||
|
||||
impl TreeHash for KzgCommitment {
|
||||
fn tree_hash_type() -> tree_hash::TreeHashType {
|
||||
<[u8; BYTES_PER_COMMITMENT] as TreeHash>::tree_hash_type()
|
||||
@@ -80,25 +78,8 @@ impl<'de> Deserialize<'de> for KzgCommitment {
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
pub struct StringVisitor;
|
||||
|
||||
impl<'de> serde::de::Visitor<'de> for StringVisitor {
|
||||
type Value = String;
|
||||
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
formatter.write_str("a hex string with 0x prefix")
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
|
||||
where
|
||||
E: serde::de::Error,
|
||||
{
|
||||
Ok(value.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
let string = deserializer.deserialize_str(StringVisitor)?;
|
||||
<Self as std::str::FromStr>::from_str(&string).map_err(serde::de::Error::custom)
|
||||
let string = String::deserialize(deserializer)?;
|
||||
Self::from_str(&string).map_err(serde::de::Error::custom)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,7 +112,6 @@ impl Debug for KzgCommitment {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "arbitrary")]
|
||||
impl arbitrary::Arbitrary<'_> for KzgCommitment {
|
||||
fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
|
||||
let mut bytes = [0u8; BYTES_PER_COMMITMENT];
|
||||
|
||||
@@ -37,12 +37,6 @@ impl fmt::Display for KzgProof {
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for KzgProof {
|
||||
fn default() -> Self {
|
||||
KzgProof([0; BYTES_PER_PROOF])
|
||||
}
|
||||
}
|
||||
|
||||
impl From<[u8; BYTES_PER_PROOF]> for KzgProof {
|
||||
fn from(bytes: [u8; BYTES_PER_PROOF]) -> Self {
|
||||
Self(bytes)
|
||||
@@ -87,25 +81,8 @@ impl<'de> Deserialize<'de> for KzgProof {
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
pub struct StringVisitor;
|
||||
|
||||
impl<'de> serde::de::Visitor<'de> for StringVisitor {
|
||||
type Value = String;
|
||||
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
formatter.write_str("a hex string with 0x prefix")
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
|
||||
where
|
||||
E: serde::de::Error,
|
||||
{
|
||||
Ok(value.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
let string = deserializer.deserialize_str(StringVisitor)?;
|
||||
<Self as std::str::FromStr>::from_str(&string).map_err(serde::de::Error::custom)
|
||||
let string = String::deserialize(deserializer)?;
|
||||
Self::from_str(&string).map_err(serde::de::Error::custom)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -138,7 +115,6 @@ impl Debug for KzgProof {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "arbitrary")]
|
||||
impl arbitrary::Arbitrary<'_> for KzgProof {
|
||||
fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
|
||||
let mut bytes = [0u8; BYTES_PER_PROOF];
|
||||
|
||||
Reference in New Issue
Block a user