lighthouse/crypto/kzg/src/kzg_proof.rs

55 lines
1.4 KiB
Rust
Raw Normal View History

2022-10-28 14:25:48 +00:00
use serde::{Deserialize, Serialize};
use serde_big_array::BigArray;
use ssz_derive::{Decode, Encode};
2022-10-04 01:48:02 +00:00
use std::fmt;
2022-10-03 14:06:04 +00:00
use tree_hash::{PackedEncoding, TreeHash};
2022-09-17 07:49:12 +00:00
const KZG_PROOF_BYTES_LEN: usize = 48;
#[derive(Debug, PartialEq, Hash, Clone, Copy, Encode, Decode, Serialize, Deserialize)]
2022-09-17 07:49:12 +00:00
#[serde(transparent)]
#[ssz(struct_behaviour = "transparent")]
2022-10-28 14:18:04 +00:00
pub struct KzgProof(#[serde(with = "BigArray")] pub [u8; KZG_PROOF_BYTES_LEN]);
2022-09-17 07:49:12 +00:00
impl fmt::Display for KzgProof {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", eth2_serde_utils::hex::encode(self.0))
2022-09-17 07:49:12 +00:00
}
}
2022-09-17 09:26:18 +00:00
impl Default for KzgProof {
fn default() -> Self {
KzgProof([0; 48])
}
}
2022-09-17 07:49:12 +00:00
impl From<[u8; KZG_PROOF_BYTES_LEN]> for KzgProof {
fn from(bytes: [u8; KZG_PROOF_BYTES_LEN]) -> Self {
Self(bytes)
}
}
impl Into<[u8; KZG_PROOF_BYTES_LEN]> for KzgProof {
fn into(self) -> [u8; KZG_PROOF_BYTES_LEN] {
self.0
}
}
impl TreeHash for KzgProof {
fn tree_hash_type() -> tree_hash::TreeHashType {
<[u8; KZG_PROOF_BYTES_LEN]>::tree_hash_type()
}
2022-10-03 14:06:04 +00:00
fn tree_hash_packed_encoding(&self) -> PackedEncoding {
2022-09-17 07:49:12 +00:00
self.0.tree_hash_packed_encoding()
}
fn tree_hash_packing_factor() -> usize {
<[u8; KZG_PROOF_BYTES_LEN]>::tree_hash_packing_factor()
}
fn tree_hash_root(&self) -> tree_hash::Hash256 {
self.0.tree_hash_root()
}
}