diff --git a/crates/common/Cargo.toml b/crates/common/Cargo.toml index 7931d285e5f..aa2a5175785 100644 --- a/crates/common/Cargo.toml +++ b/crates/common/Cargo.toml @@ -38,7 +38,8 @@ hex-literal.workspace = true [features] default = [] c-kzg = ["ethrex-crypto/c-kzg"] - +risc0 = ["ethrex-crypto/risc0"] + [lib] path = "./common.rs" diff --git a/crates/common/crypto/Cargo.toml b/crates/common/crypto/Cargo.toml index d1de2c2e5be..346f99cf22c 100644 --- a/crates/common/crypto/Cargo.toml +++ b/crates/common/crypto/Cargo.toml @@ -18,3 +18,4 @@ thiserror.workspace = true [features] default = [] c-kzg = ["dep:c-kzg"] +risc0 = ["c-kzg"] diff --git a/crates/common/crypto/kzg.rs b/crates/common/crypto/kzg.rs index 016ea209893..d86bfc33a2b 100644 --- a/crates/common/crypto/kzg.rs +++ b/crates/common/crypto/kzg.rs @@ -10,6 +10,15 @@ pub const FIELD_ELEMENTS_PER_EXT_BLOB: usize = 2 * FIELD_ELEMENTS_PER_BLOB; pub const FIELD_ELEMENTS_PER_CELL: usize = 64; pub const BYTES_PER_CELL: usize = FIELD_ELEMENTS_PER_CELL * BYTES_PER_FIELD_ELEMENT; pub const CELLS_PER_EXT_BLOB: usize = FIELD_ELEMENTS_PER_EXT_BLOB / FIELD_ELEMENTS_PER_CELL; + +// https://github.com/ethereum/c-kzg-4844?tab=readme-ov-file#precompute +// For Risc0 we need this parameter to be 0. +// For the rest we keep the value 8 due to optimizations. +#[cfg(not(feature = "risc0"))] +pub const KZG_PRECOMPUTE: u64 = 8; +#[cfg(feature = "risc0")] +pub const KZG_PRECOMPUTE: u64 = 0; + type Bytes48 = [u8; 48]; type Blob = [u8; BYTES_PER_BLOB]; type Commitment = Bytes48; @@ -54,7 +63,7 @@ pub fn verify_cell_kzg_proof_batch( ))); #[cfg(feature = "c-kzg")] { - let c_kzg_settings = c_kzg::ethereum_kzg_settings(8); + let c_kzg_settings = c_kzg::ethereum_kzg_settings(KZG_PRECOMPUTE); let mut cells = Vec::new(); for blob in blobs { cells.extend(c_kzg_settings.compute_cells(&(*blob).into())?.into_iter()); @@ -98,7 +107,7 @@ pub fn verify_blob_kzg_proof( #[cfg(feature = "c-kzg")] { c_kzg::KzgSettings::verify_blob_kzg_proof( - c_kzg::ethereum_kzg_settings(8), + c_kzg::ethereum_kzg_settings(KZG_PRECOMPUTE), &blob.into(), &commitment.into(), &proof.into(), @@ -128,7 +137,7 @@ pub fn verify_kzg_proof( #[cfg(feature = "c-kzg")] { c_kzg::KzgSettings::verify_kzg_proof( - c_kzg::ethereum_kzg_settings(8), + c_kzg::ethereum_kzg_settings(KZG_PRECOMPUTE), &commitment_bytes.into(), &z.into(), &y.into(), @@ -142,12 +151,14 @@ pub fn verify_kzg_proof( pub fn blob_to_kzg_commitment_and_proof(blob: &Blob) -> Result<(Commitment, Proof), KzgError> { let blob: c_kzg::Blob = (*blob).into(); - let commitment = - c_kzg::KzgSettings::blob_to_kzg_commitment(c_kzg::ethereum_kzg_settings(8), &blob)?; + let commitment = c_kzg::KzgSettings::blob_to_kzg_commitment( + c_kzg::ethereum_kzg_settings(KZG_PRECOMPUTE), + &blob, + )?; let commitment_bytes = commitment.to_bytes(); let proof = c_kzg::KzgSettings::compute_blob_kzg_proof( - c_kzg::ethereum_kzg_settings(8), + c_kzg::ethereum_kzg_settings(KZG_PRECOMPUTE), &blob, &commitment_bytes, )?; diff --git a/crates/l2/prover/src/guest_program/src/risc0/Cargo.toml b/crates/l2/prover/src/guest_program/src/risc0/Cargo.toml index f4b0cea8cee..1878b885654 100644 --- a/crates/l2/prover/src/guest_program/src/risc0/Cargo.toml +++ b/crates/l2/prover/src/guest_program/src/risc0/Cargo.toml @@ -20,7 +20,7 @@ guest_program = { path = "../../", default-features = false, features = [ rkyv = { version = "0.8.10", features = ["unaligned"] } -ethrex-common = { path = "../../../../../../common", default-features = false } +ethrex-common = { path = "../../../../../../common", default-features = false, features = ["risc0"] } ethrex-storage = { path = "../../../../../../storage", default-features = false } ethrex-rlp = { path = "../../../../../../common/rlp" } ethrex-vm = { path = "../../../../../../vm", default-features = false, features = [