diff --git a/beacon-light-client/plonky2/circuits_executables/bin/balance_verification_circuit_data_generation.rs b/beacon-light-client/plonky2/circuits_executables/bin/balance_verification_circuit_data_generation.rs index 5f163d867..1a275b474 100644 --- a/beacon-light-client/plonky2/circuits_executables/bin/balance_verification_circuit_data_generation.rs +++ b/beacon-light-client/plonky2/circuits_executables/bin/balance_verification_circuit_data_generation.rs @@ -1,4 +1,4 @@ -use std::{fs, marker::PhantomData, println, time::Instant}; +use std::{fs, marker::PhantomData}; use anyhow::Result; use circuits::{ @@ -7,6 +7,7 @@ use circuits::{ generator_serializer::{DendrETHGateSerializer, DendrETHGeneratorSerializer}, }; +use clap::{App, Arg}; use futures_lite::future; use jemallocator::Jemalloc; @@ -24,13 +25,33 @@ fn main() -> Result<()> { future::block_on(async_main()) } -async fn async_main() -> Result<()> { - let start = Instant::now(); +pub async fn async_main() -> Result<()> { + let matches = App::new("") + .arg( + Arg::with_name("circuit_level") + .short('l') + .long("level") + .value_name("LEVEL") + .help("Sets the circuit level") + .takes_value(true) + .default_value("all") + .validator(|x| { + if x == "all" || x.parse::().is_ok() { + Ok(()) + } else { + Err(String::from("The level must be a number or 'all'")) + } + }), + ) + .get_matches(); + + let level = match matches.value_of("circuit_level").unwrap() { + "all" => None, + x => Some(x.parse::().unwrap()), + }; + let (validators_balance_verification_targets, first_level_data) = build_validator_balance_circuit(8); - let elapsed = start.elapsed(); - - println!("Circuit generation took: {:?}", elapsed); let gate_serializer = DendrETHGateSerializer; @@ -38,50 +59,118 @@ async fn async_main() -> Result<()> { _phantom: PhantomData::, }; + if level == None || level == Some(0) { + write_first_level_circuit( + &first_level_data, + &gate_serializer, + &generator_serializer, + validators_balance_verification_targets, + ); + } + + if level == Some(0) { + return Ok(()); + } + + let mut prev_circuit_data = first_level_data; + + for i in 1..39 { + let (targets, data) = build_balance_inner_circuit(&prev_circuit_data); + + if level == Some(i) || level == None { + let circuit_bytes = data + .to_bytes(&gate_serializer, &generator_serializer) + .unwrap(); + + write_to_file(&format!("{}.plonky2_circuit", i), &circuit_bytes).unwrap(); + + let mut inner_level_targets = Vec::::new(); + + inner_level_targets + .write_target_proof_with_public_inputs(&targets.proof1) + .unwrap(); + inner_level_targets + .write_target_proof_with_public_inputs(&targets.proof2) + .unwrap(); + inner_level_targets + .write_target_verifier_circuit(&targets.verifier_circuit_target) + .unwrap(); + inner_level_targets + .write_target_bool(targets.is_zero) + .unwrap(); + + write_to_file(&format!("{}.plonky2_targets", i), &inner_level_targets).unwrap(); + } + + if level == Some(i) { + return Ok(()); + } + + prev_circuit_data = data; + } + + Ok(()) +} + +fn write_first_level_circuit( + first_level_data: &plonky2::plonk::circuit_data::CircuitData< + plonky2::field::goldilocks_field::GoldilocksField, + PoseidonGoldilocksConfig, + 2, + >, + gate_serializer: &DendrETHGateSerializer, + generator_serializer: &DendrETHGeneratorSerializer, + validators_balance_verification_targets: circuits::validator_balance_circuit::ValidatorBalanceVerificationTargets, +) { let circuit_bytes = first_level_data - .to_bytes(&gate_serializer, &generator_serializer) + .to_bytes(gate_serializer, generator_serializer) .unwrap(); write_to_file(&format!("{}.plonky2_circuit", 0), &circuit_bytes).unwrap(); let mut validator_balance_verification_targets_bytes = Vec::::new(); - for i in 0..validators_balance_verification_targets.balances.len() { validator_balance_verification_targets_bytes .write_target_bool_vec(&validators_balance_verification_targets.balances[i]) .unwrap(); } - for i in 0..validators_balance_verification_targets.validators.len() { validator_balance_verification_targets_bytes .write_target_vec(&validators_balance_verification_targets.validators[i].pubkey) .unwrap(); + validator_balance_verification_targets_bytes .write_target_vec( &validators_balance_verification_targets.validators[i].withdrawal_credentials, ) .unwrap(); + validator_balance_verification_targets_bytes .write_target_vec( &validators_balance_verification_targets.validators[i].effective_balance, ) .unwrap(); + validator_balance_verification_targets_bytes .write_target_vec(&validators_balance_verification_targets.validators[i].slashed) .unwrap(); + validator_balance_verification_targets_bytes .write_target_vec( &validators_balance_verification_targets.validators[i].activation_eligibility_epoch, ) .unwrap(); + validator_balance_verification_targets_bytes .write_target_vec( &validators_balance_verification_targets.validators[i].activation_epoch, ) .unwrap(); + validator_balance_verification_targets_bytes .write_target_vec(&validators_balance_verification_targets.validators[i].exit_epoch) .unwrap(); + validator_balance_verification_targets_bytes .write_target_vec( &validators_balance_verification_targets.validators[i].withdrawable_epoch, @@ -98,44 +187,4 @@ async fn async_main() -> Result<()> { &validator_balance_verification_targets_bytes, ) .unwrap(); - - println!("Circuit size: {}", circuit_bytes.len()); - - let mut prev_circuit_data = first_level_data; - - for i in 1..39 { - let start = Instant::now(); - let (targets, data) = build_balance_inner_circuit(&prev_circuit_data); - let elapsed = start.elapsed(); - - println!("Circuit generation took: {:?}", elapsed); - - let circuit_bytes = data - .to_bytes(&gate_serializer, &generator_serializer) - .unwrap(); - - write_to_file(&format!("{}.plonky2_circuit", i), &circuit_bytes).unwrap(); - - let mut inner_level_targets = Vec::::new(); - - inner_level_targets - .write_target_proof_with_public_inputs(&targets.proof1) - .unwrap(); - inner_level_targets - .write_target_proof_with_public_inputs(&targets.proof2) - .unwrap(); - inner_level_targets - .write_target_verifier_circuit(&targets.verifier_circuit_target) - .unwrap(); - inner_level_targets - .write_target_bool(targets.is_zero) - .unwrap(); - - write_to_file(&format!("{}.plonky2_targets", i), &inner_level_targets).unwrap(); - - println!("Circuit size: {}", circuit_bytes.len()); - prev_circuit_data = data; - } - - Ok(()) }