Skip to content

Commit

Permalink
feat: add crs downloading to co-noir binary
Browse files Browse the repository at this point in the history
  • Loading branch information
florin5f committed Dec 19, 2024
1 parent 499e20a commit 3692d4d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 7 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,
circuit_finalized: bool,
pub circuit_finalized: bool,
pub contains_recursive_proof: bool,
pub recursive_proof_public_input_indices: AggregationObjectPubInputIndices,
rom_arrays: Vec<RomTranscript>,
Expand Down
66 changes: 61 additions & 5 deletions co-noir/co-noir/src/bin/co-noir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -726,7 +757,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 builder = Rep3CoBuilder::<Bn254, Rep3MpcNet>::create_circuit(
let mut builder = Rep3CoBuilder::<Bn254, Rep3MpcNet>::create_circuit(
constraint_system,
false, // We don't support recursive atm
0,
Expand All @@ -736,6 +767,13 @@ 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 @@ -772,7 +810,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 builder = ShamirCoBuilder::<Bn254, ShamirMpcNet>::create_circuit(
let mut builder = ShamirCoBuilder::<Bn254, ShamirMpcNet>::create_circuit(
constraint_system,
false, // We don't support recursive atm
0,
Expand All @@ -782,6 +820,13 @@ 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 @@ -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::<Bn254, Rep3MpcNet>::create_circuit(
let mut builder = Rep3CoBuilder::<Bn254, Rep3MpcNet>::create_circuit(
constraint_system,
false, // We don't support recursive atm
0,
Expand All @@ -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::<Rep3UltraHonkDriver<Rep3MpcNet>, _>::get_prover_crs(
Expand Down Expand Up @@ -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::<Bn254, ShamirMpcNet>::create_circuit(
let mut builder = ShamirCoBuilder::<Bn254, ShamirMpcNet>::create_circuit(
constraint_system,
false, // We don't support recursive atm
0,
Expand All @@ -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::<ShamirUltraHonkDriver<_, ShamirMpcNet>, _>::get_prover_crs(
Expand Down
5 changes: 4 additions & 1 deletion co-noir/co-ultrahonk/src/key/proving_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ impl<T: NoirUltraHonkProver<P>, P: Pairing> ProvingKey<T, P> {
driver: &mut U,
) -> HonkProofResult<Self> {
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();

Expand Down

0 comments on commit 3692d4d

Please sign in to comment.