-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathmod.rs
More file actions
304 lines (272 loc) · 11.8 KB
/
mod.rs
File metadata and controls
304 lines (272 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
pub mod config;
pub mod fetcher;
mod merkle_tree;
mod retry;
mod s3;
mod types;
use crate::aggregators::{AlignedProof, ProofAggregationError, ZKVMEngine};
use alloy::{
consensus::{BlobTransactionSidecar, EnvKzgSettings, EthereumTxEnvelope, TxEip4844WithSidecar},
eips::{eip4844::BYTES_PER_BLOB, eip7594::BlobTransactionSidecarEip7594, Encodable2718},
hex,
network::EthereumWallet,
primitives::Address,
providers::{PendingTransactionError, Provider, ProviderBuilder},
rpc::types::TransactionReceipt,
signers::local::LocalSigner,
};
use config::Config;
use fetcher::{ProofsFetcher, ProofsFetcherError};
use merkle_tree::compute_proofs_merkle_root;
use risc0_ethereum_contracts::encode_seal;
use std::str::FromStr;
use tracing::{error, info, warn};
use types::{AlignedProofAggregationService, AlignedProofAggregationServiceContract};
#[derive(Debug)]
pub enum AggregatedProofSubmissionError {
BuildingBlobCommitment,
BuildingBlobProof,
BuildingBlobVersionedHash,
Risc0EncodingSeal(String),
SendVerifyAggregatedProofTransaction(String),
ReceiptError(PendingTransactionError),
FetchingProofs(ProofsFetcherError),
ZKVMAggregation(ProofAggregationError),
BuildingMerkleRoot,
MerkleRootMisMatch,
}
pub struct ProofAggregator {
engine: ZKVMEngine,
proof_aggregation_service: AlignedProofAggregationServiceContract,
fetcher: ProofsFetcher,
config: Config,
sp1_chunk_aggregator_vk_hash_bytes: [u8; 32],
risc0_chunk_aggregator_image_id_bytes: [u8; 32],
}
impl ProofAggregator {
pub fn new(config: Config) -> Self {
let rpc_url = config.eth_rpc_url.parse().expect("RPC URL should be valid");
let signer = LocalSigner::decrypt_keystore(
config.ecdsa.private_key_store_path.clone(),
config.ecdsa.private_key_store_password.clone(),
)
.expect("Keystore signer should be `cast wallet` compliant");
let wallet = EthereumWallet::from(signer);
let rpc_provider = ProviderBuilder::new().wallet(wallet).connect_http(rpc_url);
let proof_aggregation_service = AlignedProofAggregationService::new(
Address::from_str(&config.proof_aggregation_service_address)
.expect("AlignedProofAggregationService address should be valid"),
rpc_provider,
);
let engine =
ZKVMEngine::from_env().expect("AGGREGATOR env variable to be set to one of sp1|risc0");
let fetcher = ProofsFetcher::new(&config);
let sp1_chunk_aggregator_vk_hash_bytes: [u8; 32] =
hex::decode(&config.sp1_chunk_aggregator_vk_hash)
.expect("Failed to decode SP1 chunk aggregator VK hash")
.try_into()
.expect("SP1 chunk aggregator VK hash must be 32 bytes");
let risc0_chunk_aggregator_image_id_bytes: [u8; 32] =
hex::decode(&config.risc0_chunk_aggregator_image_id)
.expect("Failed to decode Risc0 chunk aggregator image id")
.try_into()
.expect("Risc0 chunk aggregator image id must be 32 bytes");
Self {
engine,
proof_aggregation_service,
fetcher,
config,
sp1_chunk_aggregator_vk_hash_bytes,
risc0_chunk_aggregator_image_id_bytes,
}
}
pub async fn start(&mut self) {
info!("Starting proof aggregator service");
info!("About to aggregate and submit proof to be verified on chain");
let res = self.aggregate_and_submit_proofs_on_chain().await;
match res {
Ok(()) => {
self.config
.update_last_aggregated_block(self.fetcher.get_last_aggregated_block())
.unwrap();
info!("Process finished successfully");
}
Err(err) => {
error!("Error while aggregating and submitting proofs: {:?}", err);
}
}
}
async fn aggregate_and_submit_proofs_on_chain(
&mut self,
) -> Result<(), AggregatedProofSubmissionError> {
let proofs = self
.fetcher
.fetch(self.engine.clone(), self.config.total_proofs_limit)
.await
.map_err(AggregatedProofSubmissionError::FetchingProofs)?;
if proofs.is_empty() {
warn!("No proofs collected, skipping aggregation...");
return Ok(());
}
info!("Proofs fetched, constructing merkle root...");
let (merkle_tree, leaves) = compute_proofs_merkle_root(&proofs)
.ok_or(AggregatedProofSubmissionError::BuildingMerkleRoot)?;
let merkle_root = merkle_tree.root;
info!("Merkle root constructed: 0x{}", hex::encode(merkle_root));
info!("Starting proof aggregation program...");
let (aggregated_proof, zkvm_merkle_root) = self
.engine
.aggregate_proofs(proofs, self.config.proofs_per_chunk)
.map_err(AggregatedProofSubmissionError::ZKVMAggregation)?;
info!("Proof aggregation program finished");
info!("Starting Merkle root verification: comparing ZKVM output with off-VM computation");
if zkvm_merkle_root != merkle_root {
error!(
"Merkle root mismatch detected: ZKVM = {zkvm_merkle_root:?}, off-VM = {merkle_root:?}"
);
return Err(AggregatedProofSubmissionError::MerkleRootMisMatch);
}
info!("Merkle root verification successful: roots match");
info!("Constructing blob...");
let (blob, blob_versioned_hash) = self.construct_blob(leaves).await?;
info!(
"Blob constructed, versioned hash: {}",
hex::encode(blob_versioned_hash)
);
info!("Sending proof to ProofAggregationService contract...");
let receipt = self
.send_proof_to_verify_on_chain(blob, blob_versioned_hash, aggregated_proof)
.await?;
info!(
"Proof sent and verified, tx hash {:?}",
receipt.transaction_hash
);
Ok(())
}
async fn send_proof_to_verify_on_chain(
&self,
blob: BlobTransactionSidecar,
blob_versioned_hash: [u8; 32],
aggregated_proof: AlignedProof,
) -> Result<TransactionReceipt, AggregatedProofSubmissionError> {
let tx_req = match aggregated_proof {
AlignedProof::SP1(proof) => self
.proof_aggregation_service
.verifyAggregationSP1(
blob_versioned_hash.into(),
proof.proof_with_pub_values.public_values.to_vec().into(),
proof.proof_with_pub_values.bytes().into(),
self.sp1_chunk_aggregator_vk_hash_bytes.into(),
)
.sidecar(blob)
.into_transaction_request(),
AlignedProof::Risc0(proof) => {
let encoded_seal = encode_seal(&proof.receipt).map_err(|e| {
AggregatedProofSubmissionError::Risc0EncodingSeal(e.to_string())
})?;
self.proof_aggregation_service
.verifyAggregationRisc0(
blob_versioned_hash.into(),
encoded_seal.into(),
proof.receipt.journal.bytes.into(),
self.risc0_chunk_aggregator_image_id_bytes.into(),
)
.sidecar(blob)
.into_transaction_request()
}
};
let provider = self.proof_aggregation_service.provider();
let envelope = provider
.fill(tx_req)
.await
.map_err(Self::send_verify_aggregated_proof_err)?
.try_into_envelope()
.map_err(Self::send_verify_aggregated_proof_err)?;
let tx: EthereumTxEnvelope<TxEip4844WithSidecar<BlobTransactionSidecarEip7594>> = envelope
.try_into_pooled()
.map_err(Self::send_verify_aggregated_proof_err)?
.try_map_eip4844(|tx| {
tx.try_map_sidecar(|sidecar| sidecar.try_into_7594(EnvKzgSettings::Default.get()))
})
.map_err(Self::send_verify_aggregated_proof_err)?;
let encoded_tx = tx.encoded_2718();
let pending_tx = provider
.send_raw_transaction(&encoded_tx)
.await
.map_err(Self::send_verify_aggregated_proof_err)?;
let receipt = pending_tx
.get_receipt()
.await
.map_err(Self::send_verify_aggregated_proof_err)?;
Ok(receipt)
}
fn send_verify_aggregated_proof_err<E: ToString>(err: E) -> AggregatedProofSubmissionError {
AggregatedProofSubmissionError::SendVerifyAggregatedProofTransaction(err.to_string())
}
/// ### Blob capacity
///
/// As dictated in [EIP-4844](https://eips.ethereum.org/EIPS/eip-4844), each blob can hold:
///
/// - `FIELD_ELEMENTS_PER_BLOB = 4096`
/// - `BYTES_PER_FIELD_ELEMENT = 32`
///
/// This gives a total theoretical capacity of:
///
/// `FIELD_ELEMENTS_PER_BLOB * BYTES_PER_FIELD_ELEMENT = 4096 * 32 = 131072 bytes`
///
/// However, this full capacity isn't usable due to the encoding of KZG commitments to elliptic curve points.
/// Specifically:
///
/// - Ethereum uses the BLS12-381 curve, whose scalar field modulus is slightly less than `2^256`
/// (closer to `2^255`).
/// - Therefore, 32-byte field elements can't represent all 256-bit values.
/// - To ensure values are within the field modulus, we **pad with a leading `0x00` byte**,
/// effectively capping values below the modulus.
/// - This reduces the usable payload to **31 bytes per field element**.
///
/// So, the _actual usable capacity_ per blob is:
///
/// `4096 * 31 = 126976 bytes`
///
/// Meaning that we can send as much as 126976 / 32 = 3968 proofs per blob
async fn construct_blob(
&self,
leaves: Vec<[u8; 32]>,
) -> Result<(BlobTransactionSidecar, [u8; 32]), AggregatedProofSubmissionError> {
let data: Vec<u8> = leaves.iter().flat_map(|arr| arr.iter().copied()).collect();
let mut blob_data: [u8; BYTES_PER_BLOB] = [0u8; BYTES_PER_BLOB];
// We pad the data with 0x0 byte every 31 bytes so that the field elements
// constructed from the bytes are less than BLS_MODULUS.
//
// See https://github.com/ethereum/consensus-specs/blob/86fb82b221474cc89387fa6436806507b3849d88/specs/deneb/polynomial-commitments.md#bytes_to_bls_field
let mut offset = 0;
for chunk in data.chunks(31) {
blob_data[offset] = 0x00;
let start = offset + 1;
let end = start + chunk.len();
blob_data[start..end].copy_from_slice(chunk);
offset += 32;
}
// calculate kzg commitments for blob
// This parameter is the optimal balance between performance and memory usage to load the trusted setup
// Source: https://github.com/ethereum/c-kzg-4844?tab=readme-ov-file#precompute
let settings = c_kzg::ethereum_kzg_settings(8);
let blob = c_kzg::Blob::new(blob_data);
let commitment = settings
.blob_to_kzg_commitment(&blob)
.map_err(|_| AggregatedProofSubmissionError::BuildingBlobCommitment)?;
let proof = settings
.compute_blob_kzg_proof(&blob, &commitment.to_bytes())
.map_err(|_| AggregatedProofSubmissionError::BuildingBlobProof)?;
let blob = BlobTransactionSidecar::from_kzg(
vec![blob],
vec![commitment.to_bytes()],
vec![proof.to_bytes()],
);
let blob_versioned_hash = blob
.versioned_hash_for_blob(0)
.ok_or(AggregatedProofSubmissionError::BuildingBlobVersionedHash)?
.0;
Ok((blob, blob_versioned_hash))
}
}