Skip to content

Commit

Permalink
wip: adapt to make downloading the CRS a custom command in co-noir
Browse files Browse the repository at this point in the history
  • Loading branch information
rw0x0 committed Jan 9, 2025
1 parent d5eb74c commit bf6b983
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 68 deletions.
2 changes: 1 addition & 1 deletion co-noir/co-builder/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ pub struct GenericUltraCircuitBuilder<P: Pairing, T: NoirWitnessExtensionProtoco
one_idx: u32,
pub blocks: GateBlocks<P::ScalarField>, // 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<RomTranscript>,
Expand Down
12 changes: 11 additions & 1 deletion co-noir/co-builder/src/crs/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,17 @@ impl<P: Pairing> FileProcessor<P> for NewFileStructure<P> {
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..];
Expand Down
66 changes: 5 additions & 61 deletions co-noir/co-noir/src/bin/co-noir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -757,7 +726,7 @@ fn run_build_proving_key(config: BuildProvingKeyConfig) -> color_eyre::Result<Ex
// Create the circuit
tracing::info!("Party {}: starting to generate proving key..", id);
let start = Instant::now();
let mut builder = Rep3CoBuilder::<Bn254, Rep3MpcNet>::create_circuit(
let builder = Rep3CoBuilder::<Bn254, Rep3MpcNet>::create_circuit(
constraint_system,
false, // We don't support recursive atm
0,
Expand All @@ -767,13 +736,6 @@ fn run_build_proving_key(config: BuildProvingKeyConfig) -> color_eyre::Result<Ex
&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::<Rep3UltraHonkDriver<Rep3MpcNet>, _>::get_prover_crs(
&builder,
Expand Down Expand Up @@ -810,7 +772,7 @@ fn run_build_proving_key(config: BuildProvingKeyConfig) -> color_eyre::Result<Ex
// Create the circuit
tracing::info!("Party {}: starting to generate proving key..", id);
let start = Instant::now();
let mut builder = ShamirCoBuilder::<Bn254, ShamirMpcNet>::create_circuit(
let builder = ShamirCoBuilder::<Bn254, ShamirMpcNet>::create_circuit(
constraint_system,
false, // We don't support recursive atm
0,
Expand All @@ -820,13 +782,6 @@ fn run_build_proving_key(config: BuildProvingKeyConfig) -> color_eyre::Result<Ex
&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::<ShamirUltraHonkDriver<_, ShamirMpcNet>, _>::get_prover_crs(
Expand Down Expand Up @@ -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::<Bn254, Rep3MpcNet>::create_circuit(
let builder = Rep3CoBuilder::<Bn254, Rep3MpcNet>::create_circuit(
constraint_system,
false, // We don't support recursive atm
0,
Expand All @@ -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::<Rep3UltraHonkDriver<Rep3MpcNet>, _>::get_prover_crs(
Expand Down Expand Up @@ -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::<Bn254, ShamirMpcNet>::create_circuit(
let builder = ShamirCoBuilder::<Bn254, ShamirMpcNet>::create_circuit(
constraint_system,
false, // We don't support recursive atm
0,
Expand All @@ -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::<ShamirUltraHonkDriver<_, ShamirMpcNet>, _>::get_prover_crs(
Expand Down
38 changes: 37 additions & 1 deletion co-noir/co-noir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<F: Clone> {
Expand Down Expand Up @@ -764,3 +765,38 @@ pub fn convert_witness_to_vec_rep3<F: PrimeField>(
}
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(())
}
5 changes: 1 addition & 4 deletions co-noir/co-ultrahonk/src/key/proving_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ impl<T: NoirUltraHonkProver<P>, P: Pairing> ProvingKey<T, P> {
driver: &mut U,
) -> HonkProofResult<Self> {
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();

Expand Down

0 comments on commit bf6b983

Please sign in to comment.