Skip to content

Commit

Permalink
Merge branch 'main' into release-v0.8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lispc authored Sep 6, 2023
2 parents 66adc6e + cde2eda commit 6fb53d3
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 4 deletions.
79 changes: 79 additions & 0 deletions prover/src/aggregator/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ impl Prover {
let real_chunk_count = chunk_hashes_proofs.len();
assert!((1..=MAX_AGG_SNARKS).contains(&real_chunk_count));

check_chunk_hashes(name, &chunk_hashes_proofs)?;
let (mut chunk_hashes, chunk_proofs): (Vec<_>, Vec<_>) =
chunk_hashes_proofs.into_iter().unzip();

Expand Down Expand Up @@ -167,3 +168,81 @@ impl Prover {
}
}
}

macro_rules! compare_field {
($name:expr, $idx:expr, $field:ident, $lhs:ident, $rhs:ident) => {
if $lhs.$field != $rhs.$field {
bail!(
"{} chunk-no-{}, different {}: {} != {}",
$name,
$idx,
stringify!($field),
$lhs.$field,
$rhs.$field
);
}
};
}

fn check_chunk_hashes(name: &str, chunk_hashes_proofs: &[(ChunkHash, ChunkProof)]) -> Result<()> {
for (idx, (in_arg, chunk_proof)) in chunk_hashes_proofs.iter().enumerate() {
if let Some(in_proof) = chunk_proof.chunk_hash {
compare_field!(name, idx, chain_id, in_arg, in_proof);
compare_field!(name, idx, prev_state_root, in_arg, in_proof);
compare_field!(name, idx, post_state_root, in_arg, in_proof);
compare_field!(name, idx, withdraw_root, in_arg, in_proof);
compare_field!(name, idx, data_hash, in_arg, in_proof);
}
}

Ok(())
}

#[cfg(test)]
mod tests {
use super::*;
use eth_types::H256;

#[test]
fn test_check_chunk_hashes() {
let chunk_hashes_proofs = vec![
(ChunkHash::default(), ChunkProof::default()),
(
ChunkHash {
chain_id: 1,
prev_state_root: H256::zero(),
data_hash: [100; 32].into(),
..Default::default()
},
ChunkProof {
chunk_hash: Some(ChunkHash {
chain_id: 1,
prev_state_root: [0; 32].into(),
data_hash: [100; 32].into(),
..Default::default()
}),
..Default::default()
},
),
(
ChunkHash {
post_state_root: H256::zero(),
..Default::default()
},
ChunkProof {
chunk_hash: Some(ChunkHash {
post_state_root: [1; 32].into(),
..Default::default()
}),
..Default::default()
},
),
];

let result = check_chunk_hashes("test-batch", &chunk_hashes_proofs);
assert_eq!(
result.unwrap_err().downcast_ref::<String>().unwrap(),
"test-batch chunk-no-2, different post_state_root: 0x0000…0000 != 0x0101…0101"
);
}
}
2 changes: 1 addition & 1 deletion prover/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ pub fn serialize_verify_circuit_final_pair(pair: &(G1Affine, G1Affine, Vec<Fr>))

pub fn write_snark(file_path: &str, snark: &Snark) {
let mut fd = std::fs::File::create(file_path).unwrap();
serde_json::to_writer_pretty(&mut fd, snark).unwrap()
serde_json::to_writer(&mut fd, snark).unwrap()
}

pub fn load_snark(file_path: &str) -> anyhow::Result<Option<Snark>> {
Expand Down
4 changes: 2 additions & 2 deletions prover/src/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub use batch::BatchProof;
pub use chunk::ChunkProof;
pub use evm::EvmProof;

#[derive(Clone, Debug, Deserialize, Serialize)]
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct Proof {
#[serde(with = "base64")]
proof: Vec<u8>,
Expand Down Expand Up @@ -118,7 +118,7 @@ impl Proof {
pub fn dump_as_json<P: serde::Serialize>(dir: &str, filename: &str, proof: &P) -> Result<()> {
// Write full proof as json.
let mut fd = File::create(dump_proof_path(dir, filename))?;
serde_json::to_writer_pretty(&mut fd, proof)?;
serde_json::to_writer(&mut fd, proof)?;

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion prover/src/proof/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use serde_derive::{Deserialize, Serialize};
use snark_verifier::Protocol;
use snark_verifier_sdk::Snark;

#[derive(Clone, Debug, Deserialize, Serialize)]
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct ChunkProof {
#[serde(with = "base64")]
pub storage_trace: Vec<u8>,
Expand Down

0 comments on commit 6fb53d3

Please sign in to comment.