From bf6b983263c5bacaacc9b9553e163576a0e64d60 Mon Sep 17 00:00:00 2001 From: Roman Walch <9820846+rw0x0@users.noreply.github.com> Date: Thu, 9 Jan 2025 15:54:23 +0100 Subject: [PATCH] wip: adapt to make downloading the CRS a custom command in co-noir --- co-noir/co-builder/src/builder.rs | 2 +- co-noir/co-builder/src/crs/parse.rs | 12 +++- co-noir/co-noir/src/bin/co-noir.rs | 66 ++------------------- co-noir/co-noir/src/lib.rs | 38 +++++++++++- co-noir/co-ultrahonk/src/key/proving_key.rs | 5 +- 5 files changed, 55 insertions(+), 68 deletions(-) diff --git a/co-noir/co-builder/src/builder.rs b/co-noir/co-builder/src/builder.rs index 06acf344e..f0309a509 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, - pub circuit_finalized: bool, + circuit_finalized: bool, pub contains_recursive_proof: bool, pub recursive_proof_public_input_indices: AggregationObjectPubInputIndices, rom_arrays: Vec, diff --git a/co-noir/co-builder/src/crs/parse.rs b/co-noir/co-builder/src/crs/parse.rs index fe69e2eba..5517f673f 100644 --- a/co-noir/co-builder/src/crs/parse.rs +++ b/co-noir/co-builder/src/crs/parse.rs @@ -93,7 +93,17 @@ impl FileProcessor

for NewFileStructure

{ let file = File::open(path)?; let mut file = file.take(g1_buffer_size as u64); assert!(Path::new(&path).exists()); - file.read_exact(&mut buffer[..])?; + let res = file.read_exact(&mut buffer[..]); + if res.is_err() { + tracing::warn!( + "Failed to read enough points in the CRS. Needed {} points.", + degree + ); + eyre::bail!( + "Failed to read enough points in the CRS. Needed {} points.", + degree + ); + } // We must pass the size actually read to the second call, not the desired // g1_buffer_size as the file may have been smaller than this. let monomials = &mut monomials[0..]; diff --git a/co-noir/co-noir/src/bin/co-noir.rs b/co-noir/co-noir/src/bin/co-noir.rs index 1d448bc0d..56794af55 100644 --- a/co-noir/co-noir/src/bin/co-noir.rs +++ b/co-noir/co-noir/src/bin/co-noir.rs @@ -63,38 +63,7 @@ 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 { @@ -757,7 +726,7 @@ fn run_build_proving_key(config: BuildProvingKeyConfig) -> color_eyre::Result::create_circuit( + let builder = Rep3CoBuilder::::create_circuit( constraint_system, false, // We don't support recursive atm 0, @@ -767,13 +736,6 @@ fn run_build_proving_key(config: BuildProvingKeyConfig) -> color_eyre::Result, _>::get_prover_crs( &builder, @@ -810,7 +772,7 @@ fn run_build_proving_key(config: BuildProvingKeyConfig) -> color_eyre::Result::create_circuit( + let builder = ShamirCoBuilder::::create_circuit( constraint_system, false, // We don't support recursive atm 0, @@ -820,13 +782,6 @@ fn run_build_proving_key(config: BuildProvingKeyConfig) -> color_eyre::Result, _>::get_prover_crs( @@ -1053,7 +1008,7 @@ fn run_build_and_generate_proof( // Create the circuit tracing::info!("Party {}: starting to generate proving key..", id); let start = Instant::now(); - let mut builder = Rep3CoBuilder::::create_circuit( + let builder = Rep3CoBuilder::::create_circuit( constraint_system, false, // We don't support recursive atm 0, @@ -1062,12 +1017,6 @@ 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( @@ -1136,7 +1085,7 @@ fn run_build_and_generate_proof( // Create the circuit tracing::info!("Party {}: starting to generate proving key..", id); let start = Instant::now(); - let mut builder = ShamirCoBuilder::::create_circuit( + let builder = ShamirCoBuilder::::create_circuit( constraint_system, false, // We don't support recursive atm 0, @@ -1145,12 +1094,7 @@ 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-noir/src/lib.rs b/co-noir/co-noir/src/lib.rs index 4884b52cf..37217eb5b 100644 --- a/co-noir/co-noir/src/lib.rs +++ b/co-noir/co-noir/src/lib.rs @@ -10,6 +10,7 @@ use co_acvm::{ solver::{partial_abi::PublicMarker, Rep3CoSolver}, Rep3AcvmType, ShamirAcvmType, }; +use color_eyre::eyre::{eyre, Context}; use figment::{ providers::{Env, Format, Serialized, Toml}, Figment, @@ -25,7 +26,7 @@ use mpc_net::config::NetworkConfigFile; use noirc_abi::Abi; use rand::{CryptoRng, Rng}; use serde::{Deserialize, Serialize}; -use std::{array, collections::BTreeMap, path::PathBuf}; +use std::{array, collections::BTreeMap, fs::File, io::Write, path::PathBuf}; #[derive(Clone, Debug)] pub enum PubShared { @@ -764,3 +765,38 @@ pub fn convert_witness_to_vec_rep3( } wv } + +// This funciton is basically copied from Barretenberg +/// Downloads the CRS with num_points points to the crs_path. +pub fn download_g1_crs(num_points: usize, crs_path: &PathBuf) -> color_eyre::Result<()> { + tracing::info!("Downloading CRS with {} points", num_points); + 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 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(()) +} diff --git a/co-noir/co-ultrahonk/src/key/proving_key.rs b/co-noir/co-ultrahonk/src/key/proving_key.rs index d9f154f3a..7d2a00da6 100644 --- a/co-noir/co-ultrahonk/src/key/proving_key.rs +++ b/co-noir/co-ultrahonk/src/key/proving_key.rs @@ -59,10 +59,7 @@ impl, P: Pairing> ProvingKey { driver: &mut U, ) -> HonkProofResult { tracing::trace!("ProvingKey create"); - // TODO: is this a problem? - if !circuit.circuit_finalized { - circuit.finalize_circuit(true, driver)?; - } + circuit.finalize_circuit(true, driver)?; let dyadic_circuit_size = circuit.compute_dyadic_size();