diff --git a/co-noir/co-builder/src/builder.rs b/co-noir/co-builder/src/builder.rs index 23a6b6489..74f4b8ae2 100644 --- a/co-noir/co-builder/src/builder.rs +++ b/co-noir/co-builder/src/builder.rs @@ -149,7 +149,7 @@ pub struct GenericUltraCircuitBuilder, // Storage for wires and selectors for all gate types num_gates: usize, - circuit_finalized: bool, + pub circuit_finalized: bool, pub contains_recursive_proof: bool, pub recursive_proof_public_input_indices: AggregationObjectPubInputIndices, rom_arrays: Vec, diff --git a/co-noir/co-noir/src/bin/co-noir.rs b/co-noir/co-noir/src/bin/co-noir.rs index 56794af55..1d448bc0d 100644 --- a/co-noir/co-noir/src/bin/co-noir.rs +++ b/co-noir/co-noir/src/bin/co-noir.rs @@ -63,7 +63,38 @@ fn install_tracing() { .with(fmt_layer) .init(); } +pub fn download_g1_crs(num_points: usize, crs_path: &PathBuf) -> color_eyre::Result<()> { + tracing::info!("Downloading larger CRS since current one is too small for circuit size"); + let g1_end = (num_points + 1) * 64 - 1; + + let url = "https://aztec-ignition.s3.amazonaws.com/MAIN%20IGNITION/flat/g1.dat"; + let command = format!("curl -s -H \"Range: bytes=0-{}\" '{}'", g1_end, url); + let output = std::process::Command::new("sh") + .arg("-c") + .arg(&command) + .output() + .wrap_err("Failed to execute curl command")?; + + if !output.status.success() { + let stderr = String::from_utf8_lossy(&output.stderr); + return Err(eyre!("Could not download larger CRS: {}", stderr)); + } + + let data = output.stdout; + let mut file = File::create(crs_path).wrap_err("Failed to create CRS file")?; + file.write_all(&data) + .wrap_err("Failed to write data to CRS file")?; + if data.len() < (g1_end + 1) { + return Err(eyre!( + "Downloaded CRS is incomplete: expected {} bytes, got {} bytes", + g1_end + 1, + data.len() + )); + } + + Ok(()) +} #[derive(Parser)] #[command(version, about, long_about = None)] struct Cli { @@ -726,7 +757,7 @@ fn run_build_proving_key(config: BuildProvingKeyConfig) -> color_eyre::Result::create_circuit( + let mut builder = Rep3CoBuilder::::create_circuit( constraint_system, false, // We don't support recursive atm 0, @@ -736,6 +767,13 @@ fn run_build_proving_key(config: BuildProvingKeyConfig) -> color_eyre::Result, _>::get_prover_crs( &builder, @@ -772,7 +810,7 @@ fn run_build_proving_key(config: BuildProvingKeyConfig) -> color_eyre::Result::create_circuit( + let mut builder = ShamirCoBuilder::::create_circuit( constraint_system, false, // We don't support recursive atm 0, @@ -782,6 +820,13 @@ fn run_build_proving_key(config: BuildProvingKeyConfig) -> color_eyre::Result, _>::get_prover_crs( @@ -1008,7 +1053,7 @@ fn run_build_and_generate_proof( // Create the circuit tracing::info!("Party {}: starting to generate proving key..", id); let start = Instant::now(); - let builder = Rep3CoBuilder::::create_circuit( + let mut builder = Rep3CoBuilder::::create_circuit( constraint_system, false, // We don't support recursive atm 0, @@ -1017,6 +1062,12 @@ fn run_build_and_generate_proof( false, &mut circuit_driver, )?; + builder.finalize_circuit(true, &mut circuit_driver)?; //necessary to get circuit size (I think) + let dyadic_circuit_size = builder.compute_dyadic_size(); + if std::fs::metadata(&crs_path)?.len() < (dyadic_circuit_size * 64).try_into().unwrap() + { + download_g1_crs(dyadic_circuit_size, &crs_path)?; + } // parse the crs let prover_crs = ProvingKey::, _>::get_prover_crs( @@ -1085,7 +1136,7 @@ fn run_build_and_generate_proof( // Create the circuit tracing::info!("Party {}: starting to generate proving key..", id); let start = Instant::now(); - let builder = ShamirCoBuilder::::create_circuit( + let mut builder = ShamirCoBuilder::::create_circuit( constraint_system, false, // We don't support recursive atm 0, @@ -1094,7 +1145,12 @@ fn run_build_and_generate_proof( false, &mut circuit_driver, )?; - + builder.finalize_circuit(true, &mut circuit_driver)?; + let dyadic_circuit_size = builder.compute_dyadic_size(); + if std::fs::metadata(&crs_path)?.len() < (dyadic_circuit_size * 64).try_into().unwrap() + { + download_g1_crs(dyadic_circuit_size, &crs_path)?; + } // parse the crs let prover_crs = ProvingKey::, _>::get_prover_crs( diff --git a/co-noir/co-ultrahonk/src/key/proving_key.rs b/co-noir/co-ultrahonk/src/key/proving_key.rs index 7d2a00da6..d9f154f3a 100644 --- a/co-noir/co-ultrahonk/src/key/proving_key.rs +++ b/co-noir/co-ultrahonk/src/key/proving_key.rs @@ -59,7 +59,10 @@ impl, P: Pairing> ProvingKey { driver: &mut U, ) -> HonkProofResult { tracing::trace!("ProvingKey create"); - circuit.finalize_circuit(true, driver)?; + // TODO: is this a problem? + if !circuit.circuit_finalized { + circuit.finalize_circuit(true, driver)?; + } let dyadic_circuit_size = circuit.compute_dyadic_size();