From 68ae91b5fa419c84341d75a0e9ef6ba3b7891ef0 Mon Sep 17 00:00:00 2001 From: monyarm Date: Fri, 3 Nov 2023 13:14:57 +0200 Subject: [PATCH] Optimize balance_verification_circuit_data_generation, change maximum level to 37, add error messages --- ...ce_verification_circuit_data_generation.rs | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) 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 6be846ac8..2547c40e3 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,5 +1,5 @@ -use std::{fs, marker::PhantomData}; - +use std::{fs, marker::PhantomData, ops::RangeInclusive, path::Path, process}; +use num::clamp; use anyhow::Result; use circuits::{ build_balance_inner_level_circuit::build_inner_level_circuit, @@ -44,7 +44,7 @@ pub async fn async_main() -> Result<()> { }), ) .get_matches(); - + let level_str = matches.value_of("circuit_level").unwrap(); let level = match matches.value_of("circuit_level").unwrap() { "all" => None, x => Some(x.parse::().unwrap()), @@ -59,6 +59,11 @@ pub async fn async_main() -> Result<()> { _phantom: PhantomData::, }; + if level != None && level.unwrap() > 37 { + eprintln!("\x1b[31mError: Supplied level {} is larger than the maximum allowed level 38\x1b[0m", level.unwrap()); + process::exit(1); + } + if level == None || level == Some(0) { write_first_level_circuit( &first_level_data, @@ -73,10 +78,10 @@ pub async fn async_main() -> Result<()> { } let mut prev_circuit_data = first_level_data; - - for i in 1..38 { + let level_range = if level == None { 1..=37 } else { RangeInclusive::new(1,clamp(level.unwrap(),1,37)) }; + for i in level_range { let (targets, data) = build_inner_level_circuit(&prev_circuit_data); - + println!("{}", i); if level == Some(i) || level == None { let circuit_bytes = data .to_bytes(&gate_serializer, &generator_serializer) @@ -96,6 +101,17 @@ pub async fn async_main() -> Result<()> { prev_circuit_data = data; } + let mut exists = false; + for i in 1..=37 { + if Path::new(&format!("{}.plonky2_circuit",i)).exists() || Path::new(&format!("{}.plonky2_targets",i)).exists() { + exists = true; + } + } + if !exists { + eprintln!("\x1b[31mError: No plonky2 output created. Level used was: {}\x1b[0m", level_str); + process::exit(1); + } + Ok(()) }