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 nargo preprocess command #912

Merged
merged 5 commits into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 4 additions & 18 deletions crates/nargo/src/cli/compile_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use acvm::{acir::circuit::Circuit, ProofSystemCompiler};
use acvm::ProofSystemCompiler;
use iter_extended::{try_btree_map, try_vecmap};
use noirc_driver::{CompileOptions, CompiledProgram, Driver};
use noirc_frontend::{hir::def_map::Contract, node_interner::FuncId};
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::path::Path;

use clap::Args;

use crate::{constants::TARGET_DIR, errors::CliError, resolver::Resolver};

use super::fs::{keys::save_key_to_dir, program::save_program_to_file};
use super::fs::program::save_program_to_file;
use super::preprocess_cmd::preprocess_with_path;
use super::{add_std_lib, NargoConfig};

/// Compile the program and its secret execution trace into ACIR format
Expand Down Expand Up @@ -125,18 +126,3 @@ pub(crate) fn compile_circuit(
let mut driver = setup_driver(program_dir)?;
driver.compile_main(compile_options).map_err(|_| CliError::CompilationError)
}

fn preprocess_with_path<P: AsRef<Path>>(
key_name: &str,
preprocess_dir: P,
circuit: &Circuit,
) -> Result<(PathBuf, PathBuf), CliError> {
let backend = crate::backends::ConcreteBackend;

let (proving_key, verification_key) = backend.preprocess(circuit);

let pk_path = save_key_to_dir(proving_key, key_name, &preprocess_dir, true)?;
let vk_path = save_key_to_dir(verification_key, key_name, preprocess_dir, false)?;

Ok((pk_path, vk_path))
}
19 changes: 13 additions & 6 deletions crates/nargo/src/cli/fs/program.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::path::{Path, PathBuf};

use acvm::hash_constraint_system;
use acvm::{acir::circuit::Circuit, hash_constraint_system};
use noirc_driver::CompiledProgram;

use crate::errors::CliError;
Expand All @@ -18,14 +18,21 @@ pub(crate) fn save_program_to_file<P: AsRef<Path>>(

write_to_file(&serde_json::to_vec(compiled_program).unwrap(), &circuit_path);

// Save a checksum of the circuit to compare against during proving and verification
let acir_hash = hash_constraint_system(&compiled_program.circuit);
circuit_path.set_extension("json.checksum");
write_to_file(hex::encode(acir_hash).as_bytes(), &circuit_path);

circuit_path
}

pub(crate) fn save_acir_hash_to_dir<P: AsRef<Path>>(
circuit: &Circuit,
hash_name: &str,
hash_dir: P,
) -> PathBuf {
let acir_hash = hash_constraint_system(circuit);
let hash_path = hash_dir.as_ref().join(hash_name).with_extension("json.sha256");
write_to_file(hex::encode(acir_hash).as_bytes(), &hash_path);

hash_path
}

pub(crate) fn read_program_from_file<P: AsRef<Path>>(
circuit_path: P,
) -> Result<CompiledProgram, CliError> {
Expand Down
3 changes: 3 additions & 0 deletions crates/nargo/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod compile_cmd;
mod execute_cmd;
mod gates_cmd;
mod new_cmd;
mod preprocess_cmd;
mod prove_cmd;
mod test_cmd;
mod verify_cmd;
Expand Down Expand Up @@ -53,6 +54,7 @@ enum NargoCommand {
Execute(execute_cmd::ExecuteCommand),
Prove(prove_cmd::ProveCommand),
Verify(verify_cmd::VerifyCommand),
Preprocess(preprocess_cmd::PreprocessCommand),
Test(test_cmd::TestCommand),
Gates(gates_cmd::GatesCommand),
}
Expand All @@ -67,6 +69,7 @@ pub fn start_cli() -> eyre::Result<()> {
NargoCommand::Execute(args) => execute_cmd::run(args, matches.config),
NargoCommand::Prove(args) => prove_cmd::run(args, matches.config),
NargoCommand::Verify(args) => verify_cmd::run(args, matches.config),
NargoCommand::Preprocess(args) => preprocess_cmd::run(args, matches.config),
NargoCommand::Test(args) => test_cmd::run(args, matches.config),
NargoCommand::Gates(args) => gates_cmd::run(args, matches.config),
NargoCommand::CodegenVerifier(args) => codegen_verifier_cmd::run(args, matches.config),
Expand Down
48 changes: 48 additions & 0 deletions crates/nargo/src/cli/preprocess_cmd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use acvm::acir::circuit::Circuit;
use acvm::ProofSystemCompiler;
use std::path::{Path, PathBuf};

use clap::Args;

use crate::{constants::TARGET_DIR, errors::CliError};

use super::fs::{
keys::save_key_to_dir,
program::{read_program_from_file, save_acir_hash_to_dir},
};
use super::NargoConfig;

/// Generate proving and verification keys for a circuit.
#[derive(Debug, Clone, Args)]
pub(crate) struct PreprocessCommand {
/// The name of the program build artifact.
artifact_name: String,
}

pub(crate) fn run(args: PreprocessCommand, config: NargoConfig) -> Result<(), CliError> {
let circuit_dir = config.program_dir.join(TARGET_DIR);

let program = read_program_from_file(circuit_dir.join(&args.artifact_name))?;

preprocess_with_path(&args.artifact_name, circuit_dir, &program.circuit)?;

Ok(())
}

pub(crate) fn preprocess_with_path<P: AsRef<Path>>(
key_name: &str,
preprocess_dir: P,
circuit: &Circuit,
) -> Result<(PathBuf, PathBuf), CliError> {
let backend = crate::backends::ConcreteBackend;
let (proving_key, verification_key) = backend.preprocess(circuit);

// Save a checksum of the circuit to compare against during proving and verification.
// If hash doesn't match then the circuit has been updated and keys are stale.
save_acir_hash_to_dir(circuit, key_name, &preprocess_dir);

let pk_path = save_key_to_dir(proving_key, key_name, &preprocess_dir, true)?;
let vk_path = save_key_to_dir(verification_key, key_name, preprocess_dir, false)?;

Ok((pk_path, vk_path))
}