Skip to content

Commit c8a7d96

Browse files
committed
feat: include vk and public inputs in args
this way we users don't need to know the internals of the agg_mode proof commitment which is still unstable
1 parent ab20668 commit c8a7d96

2 files changed

Lines changed: 61 additions & 15 deletions

File tree

batcher/aligned-sdk/src/agg_mode.rs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,34 @@ use ethers::{
66
providers::{Http, Middleware, Provider},
77
types::Filter,
88
};
9+
use log::warn;
910
use sha3::{Digest, Keccak256};
1011

1112
/// How much to go back from current block if from_block is not provided
1213
/// 7500 blocks = 25hr
1314
const FROM_BLOCKS_AGO_DEFAULT: u64 = 7500;
1415

16+
#[derive(Debug)]
17+
pub enum ProofData {
18+
SP1 {
19+
vk: [u8; 32],
20+
public_inputs: Vec<u8>,
21+
},
22+
}
23+
24+
impl ProofData {
25+
fn commitment(&self) -> [u8; 32] {
26+
match self {
27+
ProofData::SP1 { vk, public_inputs } => {
28+
let mut hasher = Keccak256::new();
29+
hasher.update(vk);
30+
hasher.update(public_inputs);
31+
hasher.finalize().into()
32+
}
33+
}
34+
}
35+
}
36+
1537
#[derive(Debug)]
1638
pub enum ProofVerificationAggModeError {
1739
ProvingSystemNotSupportedInAggMode,
@@ -41,7 +63,7 @@ pub enum ProofVerificationAggModeError {
4163
/// 6. Checking if the given proof hash exists within the blob’s proofs
4264
/// 7. Reconstructing the Merkle root and verifying it against the commitment stored in the contract
4365
pub async fn is_proof_verified_in_aggregation_mode(
44-
proof_hash: [u8; 32],
66+
proof_data: ProofData,
4567
network: Network,
4668
eth_rpc_url: String,
4769
beacon_client_url: String,
@@ -114,10 +136,10 @@ pub async fn is_proof_verified_in_aggregation_mode(
114136
};
115137

116138
let blob_data = hex::decode(blob.blob.replace("0x", "")).expect("A valid hex encoded data");
117-
let proof_hashes = decoded_blob(blob_data);
139+
let proof_commitments = decoded_blob(blob_data);
118140

119-
if proof_hashes.contains(&proof_hash) {
120-
if verify_blob_merkle_root(proof_hashes, merkle_root) {
141+
if proof_commitments.contains(&proof_data.commitment()) {
142+
if verify_blob_merkle_root(proof_commitments, merkle_root) {
121143
return Ok(merkle_root);
122144
} else {
123145
return Err(ProofVerificationAggModeError::UnmatchedBlobAndEventMerkleRoot);
@@ -171,9 +193,9 @@ pub fn combine_hashes(hash_a: &[u8; 32], hash_b: &[u8; 32]) -> [u8; 32] {
171193
hasher.finalize().into()
172194
}
173195

174-
fn verify_blob_merkle_root(mut proof_hashes: Vec<[u8; 32]>, merkle_root: [u8; 32]) -> bool {
175-
while proof_hashes.len() > 1 {
176-
proof_hashes = proof_hashes
196+
fn verify_blob_merkle_root(mut commitments: Vec<[u8; 32]>, merkle_root: [u8; 32]) -> bool {
197+
while commitments.len() > 1 {
198+
commitments = commitments
177199
.chunks(2)
178200
.map(|chunk| match chunk {
179201
[a, b] => combine_hashes(a, b),
@@ -183,5 +205,5 @@ fn verify_blob_merkle_root(mut proof_hashes: Vec<[u8; 32]>, merkle_root: [u8; 32
183205
.collect()
184206
}
185207

186-
proof_hashes[0] == merkle_root
208+
commitments[0] == merkle_root
187209
}

batcher/aligned/src/main.rs

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::path::PathBuf;
66
use std::str::FromStr;
77

88
use aligned_sdk::agg_mode;
9+
use aligned_sdk::agg_mode::ProofData;
910
use aligned_sdk::communication::serialization::cbor_deserialize;
1011
use aligned_sdk::core::types::FeeEstimationType;
1112
use aligned_sdk::core::{
@@ -294,15 +295,19 @@ pub struct VerifyProofInAggModeArgs {
294295
eth_rpc_url: String,
295296
#[arg(name = "Ethereum Beacon client url", long = "beacon_url")]
296297
beacon_client_url: String,
297-
#[arg(name = "Proof commitment", long = "proof-commitment")]
298-
proof_commitment: String,
299298
#[clap(flatten)]
300299
network: NetworkArg,
301300
#[arg(
302301
name = "From which block to start, if not provided it defaults to fetch logs from the past 25hs",
303302
long = "from-block"
304303
)]
305304
from_block: Option<u64>,
305+
#[arg(name = "Proving system", long = "proving_system")]
306+
proving_system: ProvingSystemArg,
307+
#[arg(name = "Public input file name", long = "public_input")]
308+
pub_input_file_name: Option<PathBuf>,
309+
#[arg(name = "Verification key hash", long = "vk")]
310+
verification_key_hash: Option<PathBuf>,
306311
}
307312

308313
#[derive(Args, Debug)]
@@ -758,13 +763,32 @@ async fn main() -> Result<(), AlignedError> {
758763
return Ok(());
759764
}
760765
AlignedCommands::VerifyProofInAggMode(args) => {
761-
let proof_hash_bytes: [u8; 32] = hex::decode(args.proof_commitment.replace("0x", ""))
762-
.expect("Proof to be a valid hex encoded hash")
763-
.try_into()
764-
.expect("Proof be raw bytes to be of len 32");
766+
let proof_data = match args.proving_system {
767+
ProvingSystemArg::SP1 => {
768+
let Some(vk_hash) = args.verification_key_hash else {
769+
error!("VK hash is necessary for sp1");
770+
return Ok(());
771+
};
772+
let vk = read_file(vk_hash)?
773+
.try_into()
774+
.expect("Invalid hexadecimal encoded vk hash");
775+
776+
let Some(pub_inputs_file_name) = args.pub_input_file_name else {
777+
error!("Public input file not provided");
778+
return Ok(());
779+
};
780+
let public_inputs = read_file(pub_inputs_file_name)?;
781+
782+
ProofData::SP1 { vk, public_inputs }
783+
}
784+
_ => {
785+
error!("Proving system not supported in aggregation mode");
786+
return Ok(());
787+
}
788+
};
765789

766790
match agg_mode::is_proof_verified_in_aggregation_mode(
767-
proof_hash_bytes,
791+
proof_data,
768792
args.network.into(),
769793
args.eth_rpc_url,
770794
args.beacon_client_url,

0 commit comments

Comments
 (0)