Skip to content

Commit 5ad8919

Browse files
committed
feat: [WIP] agg mode is proof verified first version
1 parent 2893ef8 commit 5ad8919

2 files changed

Lines changed: 127 additions & 0 deletions

File tree

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
use crate::{
2+
beacon::{BeaconClient, BeaconClientError},
3+
core::types::{Network, ProvingSystemId, VerificationData},
4+
eth::aligned_proof_agg_service::aligned_proof_aggregation_service,
5+
};
6+
use ethers::{
7+
providers::{Http, Middleware, Provider},
8+
types::Filter,
9+
};
10+
use sha3::{Digest, Keccak256};
11+
12+
pub enum ProofVerificationAggModeError {
13+
ProvingSystemNotSupportedInAggMode,
14+
EthereumProviderError(String),
15+
BeaconClient(BeaconClientError),
16+
}
17+
18+
/// Given aligned verification data, it verifies if the proof was verified in the last aggregated proof
19+
/// Currently, this in Beta mode so there isn't a way to know exactly to which proof it belongs
20+
/// So currently we check if included in the last one and verify the merkle root commitment
21+
pub async fn is_proof_verified_in_aggregation_mode(
22+
aligned_verification_data: &VerificationData,
23+
network: Network,
24+
eth_rpc_url: String,
25+
beacon_client_url: String,
26+
from_block: u64,
27+
) -> Result<bool, ProofVerificationAggModeError> {
28+
let supported = match aligned_verification_data.proving_system {
29+
ProvingSystemId::SP1 => true,
30+
_ => false,
31+
};
32+
33+
if !supported {
34+
return Err(ProofVerificationAggModeError::ProvingSystemNotSupportedInAggMode);
35+
}
36+
37+
// TODO: check if the from_block is past 18 days as the blob_data won't be available anymore
38+
39+
let proof_hash: [u8; 32] = match aligned_verification_data.proving_system {
40+
ProvingSystemId::SP1 => {
41+
let mut hasher = Keccak256::new();
42+
let vk = aligned_verification_data.verification_key.clone().unwrap();
43+
let public_inputs = aligned_verification_data.pub_input.clone().unwrap();
44+
hasher.update(&vk);
45+
hasher.update(&public_inputs);
46+
hasher.finalize().into()
47+
}
48+
// we already filter the supported ones
49+
_ => unreachable!(),
50+
};
51+
52+
/// We have to
53+
/// 1. Query the blob versioned hash of latest event from aligned proof aggregation service contract
54+
/// 2. Get the beacon block via the block parent beacon root
55+
/// 3. Fetch the blobs for that slot
56+
/// 4. Filter the blob with the blob versioned hash
57+
/// 5. Decode the blobs proofs
58+
/// 6. Find if the proofs hash is inside the blob proofs
59+
/// 7. Construct merkle root and verify it matches the one in the contract
60+
let eth_rpc_provider = Provider::<Http>::try_from(eth_rpc_url)
61+
.map_err(|e| ProofVerificationAggModeError::EthereumProviderError(e.to_string()))?;
62+
63+
let filter = Filter::new()
64+
.event("AggregatedProofVerified(bytes32,bytes32)")
65+
.from_block(from_block);
66+
67+
let mut to_check: Vec<(String, String, u64)> = vec![];
68+
69+
let logs = eth_rpc_provider.get_logs(&filter).await.unwrap();
70+
for log in logs {
71+
let blob_versioned_hash = String::from_utf8(log.data[0..66].to_vec()).unwrap();
72+
let merkle_root = String::from_utf8(log.topics.get(1).unwrap().0.to_vec()).unwrap();
73+
to_check.push((
74+
blob_versioned_hash,
75+
merkle_root,
76+
log.block_number.unwrap().0[0],
77+
));
78+
}
79+
80+
let beacon_client = BeaconClient::new(beacon_client_url);
81+
82+
// Start checking each log and blob versioned hash
83+
for (blob_versioned_hash, merkle_root, block_number) in to_check {
84+
let block = eth_rpc_provider
85+
.get_block(block_number)
86+
.await
87+
.unwrap()
88+
.unwrap();
89+
let beacon_parent_root = block.parent_beacon_block_root.unwrap();
90+
91+
let block = beacon_client
92+
.get_block_header_from_parent_hash(beacon_parent_root.0)
93+
.await
94+
.map_err(ProofVerificationAggModeError::BeaconClient)?
95+
.unwrap();
96+
97+
let blob = beacon_client
98+
.get_blob_by_versioned_hash(block.header.message.slot, blob_versioned_hash.clone())
99+
.await
100+
.map_err(ProofVerificationAggModeError::BeaconClient)?
101+
.unwrap();
102+
103+
let proof_hashes = decoded_blob(blob.blob.into());
104+
105+
// decoded blob and get all leaves and see if it the has is inside
106+
if proof_hashes.contains(&blob_versioned_hash) {
107+
return Ok(verify_merkle_root(proof_hashes, merkle_root));
108+
} else {
109+
continue;
110+
}
111+
}
112+
113+
Ok(false)
114+
}
115+
116+
fn decoded_blob(blob_data: Vec<u8>) -> Vec<String> {
117+
let proof_hashes = vec![];
118+
119+
proof_hashes
120+
}
121+
122+
fn verify_merkle_root(proof_hashes: Vec<String>, merkle_root: String) -> bool {
123+
true
124+
}

batcher/aligned-sdk/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ pub mod communication {
1212
}
1313

1414
pub mod eth {
15+
pub mod aligned_proof_agg_service;
1516
pub mod aligned_service_manager;
1617
pub mod batcher_payment_service;
1718
}
1819

20+
mod agg_mode;
21+
mod beacon;
1922
pub mod sdk;

0 commit comments

Comments
 (0)