mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-20 05:14:35 +00:00
Implement KZG EF Tests (#4274)
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
use super::*;
|
||||
use crate::case_result::compare_result;
|
||||
use beacon_chain::kzg_utils::blob_to_kzg_commitment;
|
||||
use kzg::KzgCommitment;
|
||||
use serde_derive::Deserialize;
|
||||
use std::marker::PhantomData;
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct KZGBlobToKZGCommitmentInput {
|
||||
pub blob: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[serde(bound = "E: EthSpec")]
|
||||
pub struct KZGBlobToKZGCommitment<E: EthSpec> {
|
||||
pub input: KZGBlobToKZGCommitmentInput,
|
||||
pub output: Option<String>,
|
||||
#[serde(skip)]
|
||||
_phantom: PhantomData<E>,
|
||||
}
|
||||
|
||||
impl<E: EthSpec> LoadCase for KZGBlobToKZGCommitment<E> {
|
||||
fn load_from_dir(path: &Path, _fork_name: ForkName) -> Result<Self, Error> {
|
||||
decode::yaml_decode_file(path.join("data.yaml").as_path())
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: EthSpec> Case for KZGBlobToKZGCommitment<E> {
|
||||
fn is_enabled_for_fork(fork_name: ForkName) -> bool {
|
||||
fork_name == ForkName::Deneb
|
||||
}
|
||||
|
||||
fn result(&self, _case_index: usize, _fork_name: ForkName) -> Result<(), Error> {
|
||||
let kzg = get_kzg()?;
|
||||
|
||||
let commitment = parse_blob::<E>(&self.input.blob).and_then(|blob| {
|
||||
blob_to_kzg_commitment::<E>(&kzg, blob).map_err(|e| {
|
||||
Error::InternalError(format!("Failed to compute kzg commitment: {:?}", e))
|
||||
})
|
||||
});
|
||||
|
||||
let expected = self.output.as_ref().and_then(|s| parse_commitment(s).ok());
|
||||
|
||||
compare_result::<KzgCommitment, _>(&commitment, &expected)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
use super::*;
|
||||
use crate::case_result::compare_result;
|
||||
use beacon_chain::kzg_utils::compute_blob_kzg_proof;
|
||||
use kzg::KzgProof;
|
||||
use serde_derive::Deserialize;
|
||||
use std::marker::PhantomData;
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct KZGComputeBlobKZGProofInput {
|
||||
pub blob: String,
|
||||
pub commitment: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[serde(bound = "E: EthSpec")]
|
||||
pub struct KZGComputeBlobKZGProof<E: EthSpec> {
|
||||
pub input: KZGComputeBlobKZGProofInput,
|
||||
pub output: Option<String>,
|
||||
#[serde(skip)]
|
||||
_phantom: PhantomData<E>,
|
||||
}
|
||||
|
||||
impl<E: EthSpec> LoadCase for KZGComputeBlobKZGProof<E> {
|
||||
fn load_from_dir(path: &Path, _fork_name: ForkName) -> Result<Self, Error> {
|
||||
decode::yaml_decode_file(path.join("data.yaml").as_path())
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: EthSpec> Case for KZGComputeBlobKZGProof<E> {
|
||||
fn is_enabled_for_fork(fork_name: ForkName) -> bool {
|
||||
fork_name == ForkName::Deneb
|
||||
}
|
||||
|
||||
fn result(&self, _case_index: usize, _fork_name: ForkName) -> Result<(), Error> {
|
||||
let parse_input = |input: &KZGComputeBlobKZGProofInput| -> Result<_, Error> {
|
||||
let blob = parse_blob::<E>(&input.blob)?;
|
||||
let commitment = parse_commitment(&input.commitment)?;
|
||||
Ok((blob, commitment))
|
||||
};
|
||||
|
||||
let kzg = get_kzg()?;
|
||||
let proof = parse_input(&self.input).and_then(|(blob, commitment)| {
|
||||
compute_blob_kzg_proof::<E>(&kzg, &blob, commitment)
|
||||
.map_err(|e| Error::InternalError(format!("Failed to compute kzg proof: {:?}", e)))
|
||||
});
|
||||
|
||||
let expected = self.output.as_ref().and_then(|s| parse_proof(s).ok());
|
||||
|
||||
compare_result::<KzgProof, _>(&proof, &expected)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
use super::*;
|
||||
use crate::case_result::compare_result;
|
||||
use beacon_chain::kzg_utils::compute_kzg_proof;
|
||||
use kzg::KzgProof;
|
||||
use serde_derive::Deserialize;
|
||||
use std::marker::PhantomData;
|
||||
use std::str::FromStr;
|
||||
use types::Hash256;
|
||||
|
||||
pub fn parse_point(point: &str) -> Result<Hash256, Error> {
|
||||
Hash256::from_str(&point[2..])
|
||||
.map_err(|e| Error::FailedToParseTest(format!("Failed to parse point: {:?}", e)))
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct KZGComputeKZGProofInput {
|
||||
pub blob: String,
|
||||
pub z: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[serde(bound = "E: EthSpec")]
|
||||
pub struct KZGComputeKZGProof<E: EthSpec> {
|
||||
pub input: KZGComputeKZGProofInput,
|
||||
pub output: Option<(String, Hash256)>,
|
||||
#[serde(skip)]
|
||||
_phantom: PhantomData<E>,
|
||||
}
|
||||
|
||||
impl<E: EthSpec> LoadCase for KZGComputeKZGProof<E> {
|
||||
fn load_from_dir(path: &Path, _fork_name: ForkName) -> Result<Self, Error> {
|
||||
decode::yaml_decode_file(path.join("data.yaml").as_path())
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: EthSpec> Case for KZGComputeKZGProof<E> {
|
||||
fn is_enabled_for_fork(fork_name: ForkName) -> bool {
|
||||
fork_name == ForkName::Deneb
|
||||
}
|
||||
|
||||
fn result(&self, _case_index: usize, _fork_name: ForkName) -> Result<(), Error> {
|
||||
let parse_input = |input: &KZGComputeKZGProofInput| -> Result<_, Error> {
|
||||
let blob = parse_blob::<E>(&input.blob)?;
|
||||
let z = parse_point(&input.z)?;
|
||||
Ok((blob, z))
|
||||
};
|
||||
|
||||
let kzg = get_kzg()?;
|
||||
let proof = parse_input(&self.input).and_then(|(blob, z)| {
|
||||
compute_kzg_proof::<E>(&kzg, blob, z)
|
||||
.map_err(|e| Error::InternalError(format!("Failed to compute kzg proof: {:?}", e)))
|
||||
});
|
||||
|
||||
let expected = self
|
||||
.output
|
||||
.as_ref()
|
||||
.and_then(|(s, z)| parse_proof(s).ok().map(|proof| (proof, *z)));
|
||||
|
||||
compare_result::<(KzgProof, Hash256), _>(&proof, &expected)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
use super::*;
|
||||
use crate::case_result::compare_result;
|
||||
use beacon_chain::kzg_utils::validate_blob;
|
||||
use eth2_network_config::TRUSTED_SETUP;
|
||||
use kzg::{Kzg, KzgCommitment, KzgProof, TrustedSetup};
|
||||
use serde_derive::Deserialize;
|
||||
use std::convert::TryInto;
|
||||
use std::marker::PhantomData;
|
||||
use types::Blob;
|
||||
|
||||
pub fn get_kzg() -> Result<Kzg, Error> {
|
||||
let trusted_setup: TrustedSetup = serde_json::from_reader(TRUSTED_SETUP)
|
||||
.map_err(|e| Error::InternalError(format!("Failed to initialize kzg: {:?}", e)))?;
|
||||
Kzg::new_from_trusted_setup(trusted_setup)
|
||||
.map_err(|e| Error::InternalError(format!("Failed to initialize kzg: {:?}", e)))
|
||||
}
|
||||
|
||||
pub fn parse_proof(proof: &str) -> Result<KzgProof, Error> {
|
||||
hex::decode(&proof[2..])
|
||||
.map_err(|e| Error::FailedToParseTest(format!("Failed to parse proof: {:?}", e)))
|
||||
.and_then(|bytes| {
|
||||
bytes
|
||||
.try_into()
|
||||
.map_err(|e| Error::FailedToParseTest(format!("Failed to parse proof: {:?}", e)))
|
||||
})
|
||||
.map(KzgProof)
|
||||
}
|
||||
|
||||
pub fn parse_commitment(commitment: &str) -> Result<KzgCommitment, Error> {
|
||||
hex::decode(&commitment[2..])
|
||||
.map_err(|e| Error::FailedToParseTest(format!("Failed to parse commitment: {:?}", e)))
|
||||
.and_then(|bytes| {
|
||||
bytes.try_into().map_err(|e| {
|
||||
Error::FailedToParseTest(format!("Failed to parse commitment: {:?}", e))
|
||||
})
|
||||
})
|
||||
.map(KzgCommitment)
|
||||
}
|
||||
|
||||
pub fn parse_blob<E: EthSpec>(blob: &str) -> Result<Blob<E>, Error> {
|
||||
hex::decode(&blob[2..])
|
||||
.map_err(|e| Error::FailedToParseTest(format!("Failed to parse blob: {:?}", e)))
|
||||
.and_then(|bytes| {
|
||||
Blob::<E>::new(bytes)
|
||||
.map_err(|e| Error::FailedToParseTest(format!("Failed to parse blob: {:?}", e)))
|
||||
})
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct KZGVerifyBlobKZGProofInput {
|
||||
pub blob: String,
|
||||
pub commitment: String,
|
||||
pub proof: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[serde(bound = "E: EthSpec")]
|
||||
pub struct KZGVerifyBlobKZGProof<E: EthSpec> {
|
||||
pub input: KZGVerifyBlobKZGProofInput,
|
||||
pub output: Option<bool>,
|
||||
#[serde(skip)]
|
||||
_phantom: PhantomData<E>,
|
||||
}
|
||||
|
||||
impl<E: EthSpec> LoadCase for KZGVerifyBlobKZGProof<E> {
|
||||
fn load_from_dir(path: &Path, _fork_name: ForkName) -> Result<Self, Error> {
|
||||
decode::yaml_decode_file(path.join("data.yaml").as_path())
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: EthSpec> Case for KZGVerifyBlobKZGProof<E> {
|
||||
fn is_enabled_for_fork(fork_name: ForkName) -> bool {
|
||||
fork_name == ForkName::Deneb
|
||||
}
|
||||
|
||||
fn result(&self, _case_index: usize, _fork_name: ForkName) -> Result<(), Error> {
|
||||
let parse_input = |input: &KZGVerifyBlobKZGProofInput| -> Result<(Blob<E>, KzgCommitment, KzgProof), Error> {
|
||||
let blob = parse_blob::<E>(&input.blob)?;
|
||||
let commitment = parse_commitment(&input.commitment)?;
|
||||
let proof = parse_proof(&input.proof)?;
|
||||
Ok((blob, commitment, proof))
|
||||
};
|
||||
|
||||
let kzg = get_kzg()?;
|
||||
let result = parse_input(&self.input).and_then(|(blob, commitment, proof)| {
|
||||
validate_blob::<E>(&kzg, blob, commitment, proof)
|
||||
.map_err(|e| Error::InternalError(format!("Failed to validate blob: {:?}", e)))
|
||||
});
|
||||
|
||||
compare_result::<bool, _>(&result, &self.output)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
use super::*;
|
||||
use crate::case_result::compare_result;
|
||||
use beacon_chain::kzg_utils::validate_blobs;
|
||||
use serde_derive::Deserialize;
|
||||
use std::marker::PhantomData;
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct KZGVerifyBlobKZGProofBatchInput {
|
||||
pub blobs: Vec<String>,
|
||||
pub commitments: Vec<String>,
|
||||
pub proofs: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[serde(bound = "E: EthSpec")]
|
||||
pub struct KZGVerifyBlobKZGProofBatch<E: EthSpec> {
|
||||
pub input: KZGVerifyBlobKZGProofBatchInput,
|
||||
pub output: Option<bool>,
|
||||
#[serde(skip)]
|
||||
_phantom: PhantomData<E>,
|
||||
}
|
||||
|
||||
impl<E: EthSpec> LoadCase for KZGVerifyBlobKZGProofBatch<E> {
|
||||
fn load_from_dir(path: &Path, _fork_name: ForkName) -> Result<Self, Error> {
|
||||
decode::yaml_decode_file(path.join("data.yaml").as_path())
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: EthSpec> Case for KZGVerifyBlobKZGProofBatch<E> {
|
||||
fn is_enabled_for_fork(fork_name: ForkName) -> bool {
|
||||
fork_name == ForkName::Deneb
|
||||
}
|
||||
|
||||
fn result(&self, _case_index: usize, _fork_name: ForkName) -> Result<(), Error> {
|
||||
let parse_input = |input: &KZGVerifyBlobKZGProofBatchInput| -> Result<_, Error> {
|
||||
let blobs = input
|
||||
.blobs
|
||||
.iter()
|
||||
.map(|s| parse_blob::<E>(s))
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
let commitments = input
|
||||
.commitments
|
||||
.iter()
|
||||
.map(|s| parse_commitment(s))
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
let proofs = input
|
||||
.proofs
|
||||
.iter()
|
||||
.map(|s| parse_proof(s))
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
Ok((commitments, blobs, proofs))
|
||||
};
|
||||
|
||||
let kzg = get_kzg()?;
|
||||
let result = parse_input(&self.input).and_then(|(commitments, blobs, proofs)| {
|
||||
validate_blobs::<E>(&kzg, &commitments, &blobs, &proofs)
|
||||
.map_err(|e| Error::InternalError(format!("Failed to validate blobs: {:?}", e)))
|
||||
});
|
||||
|
||||
compare_result::<bool, _>(&result, &self.output)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
use super::*;
|
||||
use crate::case_result::compare_result;
|
||||
use beacon_chain::kzg_utils::verify_kzg_proof;
|
||||
use serde_derive::Deserialize;
|
||||
use std::marker::PhantomData;
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct KZGVerifyKZGProofInput {
|
||||
pub commitment: String,
|
||||
pub z: String,
|
||||
pub y: String,
|
||||
pub proof: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[serde(bound = "E: EthSpec")]
|
||||
pub struct KZGVerifyKZGProof<E: EthSpec> {
|
||||
pub input: KZGVerifyKZGProofInput,
|
||||
pub output: Option<bool>,
|
||||
#[serde(skip)]
|
||||
_phantom: PhantomData<E>,
|
||||
}
|
||||
|
||||
impl<E: EthSpec> LoadCase for KZGVerifyKZGProof<E> {
|
||||
fn load_from_dir(path: &Path, _fork_name: ForkName) -> Result<Self, Error> {
|
||||
decode::yaml_decode_file(path.join("data.yaml").as_path())
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: EthSpec> Case for KZGVerifyKZGProof<E> {
|
||||
fn is_enabled_for_fork(fork_name: ForkName) -> bool {
|
||||
fork_name == ForkName::Deneb
|
||||
}
|
||||
|
||||
fn result(&self, _case_index: usize, _fork_name: ForkName) -> Result<(), Error> {
|
||||
let parse_input = |input: &KZGVerifyKZGProofInput| -> Result<_, Error> {
|
||||
let commitment = parse_commitment(&input.commitment)?;
|
||||
let z = parse_point(&input.z)?;
|
||||
let y = parse_point(&input.y)?;
|
||||
let proof = parse_proof(&input.proof)?;
|
||||
Ok((commitment, z, y, proof))
|
||||
};
|
||||
|
||||
let kzg = get_kzg()?;
|
||||
let result = parse_input(&self.input).and_then(|(commitment, z, y, proof)| {
|
||||
verify_kzg_proof::<E>(&kzg, commitment, proof, z, y)
|
||||
.map_err(|e| Error::InternalError(format!("Failed to validate proof: {:?}", e)))
|
||||
});
|
||||
|
||||
compare_result::<bool, _>(&result, &self.output)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user