Skip to content

Commit

Permalink
Include trusted setup bytes into binary for use in eip_7594 module
Browse files Browse the repository at this point in the history
  • Loading branch information
povi committed May 13, 2024
1 parent bb341fd commit 6eb480c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions eip_7594/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ anyhow = { workspace = true }
c-kzg = { workspace = true }
hashing = { workspace = true }
helper_functions = { workspace = true }
kzg = { workspace = true }
ssz = { workspace = true}
sha3 = { workspace = true }
try_from_iterator = { workspace = true }
Expand Down
37 changes: 25 additions & 12 deletions eip_7594/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
path::Path,
};

use anyhow::Result;
use anyhow::{anyhow, Result};
use c_kzg::{
Blob as CKzgBlob, Bytes32, Bytes48, Cell as CKzgCell, KzgProof as CKzgProof, KzgSettings,
};
Expand All @@ -15,6 +15,7 @@ use helper_functions::{
predicates::{index_at_commitment_depth, is_valid_merkle_branch},
signing::SignForSingleFork,
};
use kzg::eip_4844::{load_trusted_setup_string, BYTES_PER_G1, BYTES_PER_G2};
use sha3::{Digest, Sha3_256};
use ssz::{
ByteVector, ContiguousList, ContiguousVector, MerkleElements, MerkleTree, SszHash, SszWrite,
Expand Down Expand Up @@ -77,8 +78,7 @@ pub fn verify_kzg_proofs<P: Preset>(data_column_sidecar: &DataColumnSidecar<P>)
row_ids.push(i as u64);
}

let trusted_setup_file = Path::new("kzg_utils/trusted_setup.txt");
let kzg_settings = KzgSettings::load_trusted_setup_file(trusted_setup_file).unwrap();
let kzg_settings = load_kzg_settings()?;

let column = column
.clone()
Expand Down Expand Up @@ -141,8 +141,7 @@ pub fn verify_data_column_sidecar_kzg_proofs<P: Preset>(
row_ids.push(i as u64);
}

let trusted_setup_file = Path::new("kzg_utils/trusted_setup.txt");
let kzg_settings = KzgSettings::load_trusted_setup_file(trusted_setup_file).unwrap();
let kzg_settings = load_kzg_settings()?;

let column = sidecar
.column
Expand Down Expand Up @@ -236,9 +235,7 @@ pub fn get_custody_columns(node_id: NodeId, custody_subnet_count: u64) -> Vec<Co
pub fn compute_extended_matrix(blobs: Vec<CKzgBlob>) -> Result<ExtendedMatrix> {
let mut extended_matrix: Vec<CKzgCell> = Vec::new();

let trusted_setup_file = Path::new("kzg_utils/trusted_setup.txt");

let kzg_settings = KzgSettings::load_trusted_setup_file(trusted_setup_file).unwrap();
let kzg_settings = load_kzg_settings()?;

for blob in blobs {
let cells = *CKzgCell::compute_cells(&blob, &kzg_settings)?;
Expand All @@ -255,8 +252,7 @@ fn recover_matrix(
cells_dict: &HashMap<(BlobIndex, CellID), CKzgCell>,
blob_count: usize,
) -> Result<ExtendedMatrix> {
let trusted_setup_file = Path::new("kzg_utils/trusted_setup.txt");
let kzg_settings = KzgSettings::load_trusted_setup_file(trusted_setup_file).unwrap();
let kzg_settings = load_kzg_settings()?;

let mut extended_matrix = Vec::new();
for blob_index in 0..blob_count {
Expand Down Expand Up @@ -290,8 +286,7 @@ pub fn get_data_column_sidecars<P: Preset>(
let kzg_commitments_inclusion_proof =
kzg_commitment_inclusion_proof(post_deneb_beacon_block_body);

let trusted_setup_file = Path::new("kzg_utils/trusted_setup.txt");
let kzg_settings = KzgSettings::load_trusted_setup_file(trusted_setup_file).unwrap();
let kzg_settings = load_kzg_settings()?;

let signed_block_header = SignedBeaconBlockHeader {
message: beacon_block.to_header(),
Expand Down Expand Up @@ -392,6 +387,24 @@ fn kzg_commitment_inclusion_proof<P: Preset>(
proof
}

fn load_kzg_settings() -> Result<KzgSettings> {
let contents = include_str!("../../kzg_utils/src/trusted_setup.txt");
let (g1_bytes, g2_bytes) =
load_trusted_setup_string(contents).map_err(|error| anyhow!(error))?;

KzgSettings::load_trusted_setup(
&g1_bytes
.chunks_exact(BYTES_PER_G1)
.map(|chunk| TryInto::<[u8; BYTES_PER_G1]>::try_into(chunk).map_err(Into::into))
.collect::<Result<Vec<_>>>()?,
&g2_bytes
.chunks_exact(BYTES_PER_G2)
.map(|chunk| TryInto::<[u8; BYTES_PER_G2]>::try_into(chunk).map_err(Into::into))
.collect::<Result<Vec<_>>>()?,
)
.map_err(|error| anyhow!(error))
}

#[cfg(test)]
mod tests {
use duplicate::duplicate_item;
Expand Down

0 comments on commit 6eb480c

Please sign in to comment.