Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add command to download a CRS with a given number of points to the co-noir binary #301

Merged
merged 5 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion co-noir/co-builder/src/keys/proving_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl<P: Pairing> ProvingKey<P> {
fn get_crs_size<T: NoirWitnessExtensionProtocol<P::ScalarField>>(
circuit: &GenericUltraCircuitBuilder<P, T>,
) -> usize {
const EXTRA_SRS_POINTS_FOR_ECCVM_IPA: usize = 1;
const EXTRA_SRS_POINTS_FOR_ECCVM_IPA: usize = 0; // Is 1 in barrettenberg, but we don't need it with UltraHonk

let num_extra_gates =
UltraCircuitBuilder::<P>::get_num_gates_added_to_ensure_nonzero_polynomials();
Expand Down
21 changes: 15 additions & 6 deletions co-noir/co-noir/src/bin/co-noir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ use co_acvm::{
ShamirAcvmType,
};
use co_noir::{
convert_witness_to_vec_rep3, file_utils, share_input_rep3, share_rep3, share_shamir,
translate_witness_share_rep3, BuildAndGenerateProofCli, BuildAndGenerateProofConfig,
BuildProvingKeyCLi, BuildProvingKeyConfig, CreateVKCli, CreateVKConfig, GenerateProofCli,
GenerateProofConfig, GenerateWitnessCli, GenerateWitnessConfig, MPCProtocol,
MergeInputSharesCli, MergeInputSharesConfig, PubShared, SplitInputCli, SplitInputConfig,
SplitProvingKeyCli, SplitProvingKeyConfig, SplitWitnessCli, SplitWitnessConfig, TranscriptHash,
convert_witness_to_vec_rep3, download_g1_crs, file_utils, share_input_rep3, share_rep3,
share_shamir, translate_witness_share_rep3, BuildAndGenerateProofCli,
BuildAndGenerateProofConfig, BuildProvingKeyCLi, BuildProvingKeyConfig, CreateVKCli,
CreateVKConfig, DownloadCrsCLi, DownloadCrsConfig, GenerateProofCli, GenerateProofConfig,
GenerateWitnessCli, GenerateWitnessConfig, MPCProtocol, MergeInputSharesCli,
MergeInputSharesConfig, PubShared, SplitInputCli, SplitInputConfig, SplitProvingKeyCli,
SplitProvingKeyConfig, SplitWitnessCli, SplitWitnessConfig, TranscriptHash,
TranslateProvingKeyCli, TranslateProvingKeyConfig, TranslateWitnessCli, TranslateWitnessConfig,
VerifyCli, VerifyConfig,
};
Expand Down Expand Up @@ -97,6 +98,8 @@ enum Commands {
CreateVK(CreateVKCli),
/// Verification of a Noir proof.
Verify(VerifyCli),
/// Download a CRS with a given number of points
DownloadCrs(DownloadCrsCLi),
}

fn main() -> color_eyre::Result<ExitCode> {
Expand Down Expand Up @@ -155,6 +158,12 @@ fn main() -> color_eyre::Result<ExitCode> {
let config = VerifyConfig::parse(cli).context("while parsing config")?;
run_verify(config)
}
Commands::DownloadCrs(cli) => {
let config = DownloadCrsConfig::parse(cli).context("while parsing config")?;
download_g1_crs(config.num_points, &config.crs)?;
tracing::info!("Downloaded CRS successfully");
Ok(ExitCode::SUCCESS)
}
}
}

Expand Down
64 changes: 63 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 @@ -615,6 +616,31 @@ pub struct VerifyConfig {
pub crs: PathBuf,
}

/// Cli arguments for `verify`
#[derive(Debug, Serialize, Args)]
pub struct DownloadCrsCLi {
/// The path to the config file
#[arg(long)]
#[serde(skip_serializing_if = "::std::option::Option::is_none")]
pub config: Option<PathBuf>,
/// The path to the prover crs file
#[arg(long)]
#[serde(skip_serializing_if = "::std::option::Option::is_none")]
pub crs: Option<PathBuf>,
/// The number of points to download
#[arg(short, long, default_value_t = 1)]
pub num_points: usize,
}

/// Config for `verify`
#[derive(Debug, Deserialize)]
pub struct DownloadCrsConfig {
/// The path to the prover crs file
pub crs: PathBuf,
/// The number of points to download
pub num_points: usize,
}

/// Prefix for config env variables
pub const CONFIG_ENV_PREFIX: &str = "CONOIR_";

Expand Down Expand Up @@ -657,6 +683,7 @@ impl_config!(GenerateProofCli, GenerateProofConfig);
impl_config!(BuildAndGenerateProofCli, BuildAndGenerateProofConfig);
impl_config!(CreateVKCli, CreateVKConfig);
impl_config!(VerifyCli, VerifyConfig);
impl_config!(DownloadCrsCLi, DownloadCrsConfig);

pub fn share_rep3<F: PrimeField, R: Rng + CryptoRng>(
witness: Vec<PubShared<F>>,
Expand Down Expand Up @@ -764,3 +791,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 * 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(())
}
Loading