Skip to content

Commit a067f65

Browse files
authored
Merge pull request #6 from lambdaclass/integrate_mina_operator
Integrate mina operator
2 parents 2a2ed6d + ca12b7c commit a067f65

27 files changed

Lines changed: 362 additions & 325 deletions

Makefile

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,31 @@ batcher_send_halo2_kzg_task_burst_5: batcher/target/release/aligned
331331
--repetitions 5 \
332332
--proof_generator_addr 0x66f9664f97F2b50F62D13eA064982f936dE76657
333333

334+
batcher_send_mina_task:
335+
@echo "Sending Mina state task to Batcher..."
336+
@cd batcher/aligned/ && cargo run --release -- submit \
337+
--proving_system Mina \
338+
--proof test_files/mina/protocol_state_proof.proof \
339+
--public_input test_files/mina/protocol_state_hash.pub \
340+
--proof_generator_addr 0x66f9664f97F2b50F62D13eA064982f936dE76657
341+
342+
batcher_send_mina_task_bad:
343+
@echo "Sending Mina state task to Batcher..."
344+
@cd batcher/aligned/ && cargo run --release -- submit \
345+
--proving_system Mina \
346+
--proof test_files/mina/protocol_state_proof.proof \
347+
--public_input test_files/mina/bad_protocol_state_hash.pub \
348+
--proof_generator_addr 0x66f9664f97F2b50F62D13eA064982f936dE76657
349+
350+
batcher_send_mina_burst:
351+
@echo "Sending Mina state task to Batcher..."
352+
@cd batcher/aligned/ && cargo run --release -- submit \
353+
--proving_system Mina \
354+
--proof test_files/mina/protocol_state_proof.proof \
355+
--public_input test_files/mina/protocol_state_hash.pub \
356+
--repetitions 15 \
357+
--proof_generator_addr 0x66f9664f97F2b50F62D13eA064982f936dE76657
358+
334359
__TASK_SENDERS__:
335360
# TODO add a default proving system
336361

@@ -666,11 +691,11 @@ generate_halo2_ipa_proof:
666691
__MINA_FFI__: ##
667692
build_mina_macos:
668693
@cd operator/mina/lib && cargo build --release
669-
@cp operator/mina/lib/target/release/libkimchi_verifier_ffi.dylib operator/mina/lib/libkimchi_verifier.dylib
694+
@cp operator/mina/lib/target/release/libmina_state_verifier_ffi.dylib operator/mina/lib/libmina_state_verifier.dylib
670695

671696
build_mina_linux:
672697
@cd operator/mina/lib && cargo build --release
673-
@cp operator/mina/lib/target/release/libkimchi_verifier_ffi.so operator/mina/lib/libkimchi_verifier.so
698+
@cp operator/mina/lib/target/release/libmina_state_verifier_ffi.so operator/mina/lib/libmina_state_verifier.so
674699

675700
test_mina_rust_ffi:
676701
@echo "Testing Mina Rust FFI source code..."

batcher/Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

batcher/aligned-batcher/Cargo.toml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,19 @@ bytes = "1.6.0"
1919
hex = "0.4.3"
2020
dotenv = "0.15.0"
2121
anyhow = "1.0.83"
22-
ethers = { tag = "v2.0.15-fix-reconnections", features = ["ws", "rustls"], git = "https://github.com/yetanotherco/ethers-rs.git" }
22+
ethers = { tag = "v2.0.15-fix-reconnections", features = [
23+
"ws",
24+
"rustls",
25+
], git = "https://github.com/yetanotherco/ethers-rs.git" }
2326
lambdaworks-crypto = { version = "0.7.0", features = ["serde"] }
2427
serde_yaml = "0.9.34"
2528
sp1-sdk = { git = "https://github.com/succinctlabs/sp1.git", rev = "v1.0.5-testnet" }
26-
risc0-zkvm = { git = "https://github.com/risc0/risc0", tag="v1.0.1" }
29+
risc0-zkvm = { git = "https://github.com/risc0/risc0", tag = "v1.0.1" }
2730
halo2curves = { version = "0.6.0", default-features = false }
28-
halo2_backend = { git = "https://github.com/yetanotherco/yet-another-halo2-fork.git", branch = "feat/serde_constraint_system"}
31+
halo2_backend = { git = "https://github.com/yetanotherco/yet-another-halo2-fork.git", branch = "feat/serde_constraint_system" }
2932
halo2_proofs = { git = "https://github.com/yetanotherco/yet-another-halo2-fork.git", branch = "feat/serde_constraint_system" }
33+
aligned-batcher-lib = { path = "../aligned-batcher-lib" }
3034
lazy_static = "1.4.0"
3135
bincode = "1.3.3"
32-
aligned-batcher-lib = { path = "../aligned-batcher-lib"}
36+
base64 = "0.22.1"
37+
bs58 = "0.5.1"

batcher/aligned-batcher/src/lib.rs

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,13 @@ mod config;
3434
mod eth;
3535
pub mod gnark;
3636
pub mod halo2;
37+
pub mod mina;
3738
pub mod risc_zero;
3839
pub mod s3;
3940
pub mod sp1;
4041
pub mod types;
4142
mod zk_utils;
4243

43-
const S3_BUCKET_NAME: &str = "storage.alignedlayer.com";
44-
4544
pub struct Batcher {
4645
s3_client: S3Client,
4746
eth_ws_provider: Provider<Ws>,
@@ -306,7 +305,7 @@ impl Batcher {
306305
{
307306
let mut last_uploaded_batch_block = self.last_uploaded_batch_block.lock().await;
308307
self.submit_batch(&batch_bytes, &batch_merkle_tree.root)
309-
.await;
308+
.await?;
310309
// update last uploaded batch block
311310
*last_uploaded_batch_block = block_number;
312311
}
@@ -341,26 +340,52 @@ impl Batcher {
341340
}
342341

343342
/// Post batch to s3 and submit new task to Ethereum
344-
async fn submit_batch(&self, batch_bytes: &[u8], batch_merkle_root: &[u8; 32]) {
343+
async fn submit_batch(
344+
&self,
345+
batch_bytes: &[u8],
346+
batch_merkle_root: &[u8; 32],
347+
) -> Result<(), BatcherError> {
348+
let s3_bucket_name: String = std::env::var("AWS_BUCKET_NAME")
349+
.map_err(|e| BatcherError::S3EnvVariableError("AWS_BUCKET_NAME".to_owned(), e))?;
350+
let s3_bucket_name_is_domain = std::env::var("AWS_BUCKET_NAME_IS_DOMAIN").is_ok();
351+
345352
let s3_client = self.s3_client.clone();
346353
let batch_merkle_root_hex = hex::encode(batch_merkle_root);
347354
info!("Batch merkle root: {}", batch_merkle_root_hex);
348355
let file_name = batch_merkle_root_hex.clone() + ".json";
349356

350357
info!("Uploading batch to S3...");
351-
s3::upload_object(&s3_client, S3_BUCKET_NAME, batch_bytes.to_vec(), &file_name)
352-
.await
353-
.expect("Failed to upload object to S3");
358+
s3::upload_object(
359+
&s3_client,
360+
&s3_bucket_name,
361+
batch_bytes.to_vec(),
362+
&file_name,
363+
)
364+
.await
365+
.expect("Failed to upload object to S3");
354366

355367
info!("Batch sent to S3 with name: {}", file_name);
356368

357369
info!("Uploading batch to contract");
358370
let service_manager = &self.service_manager;
359-
let batch_data_pointer = "https://".to_owned() + S3_BUCKET_NAME + "/" + &file_name;
371+
372+
let batch_data_pointer = if s3_bucket_name_is_domain {
373+
"https://".to_owned() + &s3_bucket_name + "/" + &file_name
374+
} else {
375+
let s3_region: String = std::env::var("AWS_REGION")
376+
.map_err(|e| BatcherError::S3EnvVariableError("AWS_REGION".to_owned(), e))?;
377+
"https://".to_owned()
378+
+ &s3_bucket_name
379+
+ ".s3."
380+
+ &s3_region
381+
+ ".amazonaws.com/"
382+
+ &file_name
383+
};
360384
match eth::create_new_task(service_manager, *batch_merkle_root, batch_data_pointer).await {
361385
Ok(_) => info!("Batch verification task created on Aligned contract"),
362386
Err(e) => error!("Failed to create batch verification task: {}", e),
363387
}
388+
Ok(())
364389
}
365390
}
366391
/// Await for the `BatchVerified` event emitted by the Aligned contract and then send responses.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use base64::prelude::*;
2+
use log::{debug, warn};
3+
4+
pub fn verify_protocol_state_proof_integrity(proof: &[u8], public_input: &[u8]) -> bool {
5+
debug!("Reading Mina protocol state proof base64");
6+
let protocol_state_proof_base64 =
7+
if let Ok(protocol_state_proof_base64) = std::str::from_utf8(proof) {
8+
protocol_state_proof_base64
9+
} else {
10+
return false;
11+
};
12+
debug!("Reading Mina protocol state hash base58");
13+
let protocol_state_hash_base58 =
14+
if let Ok(protocol_state_hash_base58) = std::str::from_utf8(public_input) {
15+
protocol_state_hash_base58
16+
} else {
17+
return false;
18+
};
19+
20+
debug!("Decoding Mina protocol state proof base64");
21+
if BASE64_URL_SAFE
22+
.decode(protocol_state_proof_base64.trim_end())
23+
.is_err()
24+
{
25+
warn!("Failed to decode Mina protocol state proof base64");
26+
return false;
27+
}
28+
29+
debug!("Decoding Mina protocol state hash base58");
30+
if bs58::decode(protocol_state_hash_base58.trim_end())
31+
.into_vec()
32+
.is_err()
33+
{
34+
warn!("Failed to decode Mina protocol state hash base58");
35+
return false;
36+
}
37+
38+
true
39+
}
40+
41+
#[cfg(test)]
42+
mod test {
43+
use super::verify_protocol_state_proof_integrity;
44+
45+
const PROTOCOL_STATE_PROOF_BYTES: &[u8] =
46+
include_bytes!("../../../../batcher/aligned/test_files/mina/protocol_state_proof.proof");
47+
const PROTOCOL_STATE_HASH_BYTES: &[u8] =
48+
include_bytes!("../../../../batcher/aligned/test_files/mina/protocol_state_hash.pub");
49+
50+
#[test]
51+
fn verify_protocol_state_proof_integrity_does_not_fail() {
52+
assert!(verify_protocol_state_proof_integrity(
53+
PROTOCOL_STATE_PROOF_BYTES,
54+
PROTOCOL_STATE_HASH_BYTES,
55+
));
56+
}
57+
}

batcher/aligned-batcher/src/types/errors.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
use std::fmt;
1+
use std::{env::VarError, fmt};
22

33
use tokio_tungstenite::tungstenite;
44

55
pub enum BatcherError {
66
ConnectionError(tungstenite::Error),
77
BatchVerifiedEventStreamError(String),
88
EthereumSubscriptionError(String),
9+
S3EnvVariableError(String, VarError),
910
}
1011

1112
impl From<tungstenite::Error> for BatcherError {
@@ -26,6 +27,9 @@ impl fmt::Debug for BatcherError {
2627
BatcherError::EthereumSubscriptionError(e) => {
2728
write!(f, "Ethereum subscription was not successful: {}", e)
2829
}
30+
BatcherError::S3EnvVariableError(v, e) => {
31+
write!(f, "Error while fetching the {} env variable: {}", v, e)
32+
}
2933
}
3034
}
3135
}

batcher/aligned-batcher/src/zk_utils/mod.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use crate::gnark::verify_gnark;
21
use crate::halo2::ipa::verify_halo2_ipa;
32
use crate::halo2::kzg::verify_halo2_kzg;
43
use crate::risc_zero::verify_risc_zero_proof;
54
use crate::sp1::verify_sp1_proof;
5+
use crate::{gnark::verify_gnark, mina::verify_protocol_state_proof_integrity};
66
use aligned_batcher_lib::types::{ProvingSystemId, VerificationData};
77
use log::{debug, warn};
88

@@ -73,6 +73,15 @@ pub(crate) fn verify(verification_data: &VerificationData) -> bool {
7373
debug!("Gnark proof is valid: {}", is_valid);
7474
is_valid
7575
}
76-
ProvingSystemId::Mina => todo!(),
76+
ProvingSystemId::Mina => {
77+
let pub_input = verification_data
78+
.pub_input
79+
.as_ref()
80+
.expect("Public input is required");
81+
verify_protocol_state_proof_integrity(&verification_data.proof, pub_input)
82+
// TODO(xqft): add Pickles aggregator checks which are run alongside the Kimchi
83+
// verifier. These checks are fast and if they aren't successful then the Pickles proof
84+
// isn't valid.
85+
}
7786
}
7887
}

batcher/aligned/src/main.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,12 @@ fn verification_data_from_args(args: SubmitArgs) -> Result<VerificationData, Bat
387387
args.pub_input_file_name,
388388
)?);
389389
}
390-
ProvingSystemId::Mina => todo!(),
390+
ProvingSystemId::Mina => {
391+
pub_input = Some(read_file_option(
392+
"--public_input",
393+
args.pub_input_file_name,
394+
)?)
395+
}
391396
}
392397

393398
let proof_generator_addr = Address::from_str(&args.proof_generator_addr).unwrap();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3NLAmT67BvsADswuJ6xpt3yNEVgmn5R8pkJTFvQtsZQD54aLhFmU
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
cat mina_devnet_protocol_query.json | jq -r '.data.bestChain.[0].protocolState.previousStateHash' >protocol_state_hash.pub
2+
cat mina_devnet_protocol_query.json | jq -r '.data.block.protocolStateProof.base64' >protocol_state_proof.proof

0 commit comments

Comments
 (0)