Skip to content

Commit

Permalink
feat: Introduce codegen-verifier section to Nargo.toml (noir-lang#1009)
Browse files Browse the repository at this point in the history
  • Loading branch information
longfin committed Apr 13, 2023
1 parent 7528f59 commit 31577ac
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
16 changes: 15 additions & 1 deletion crates/nargo/src/manifest/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use serde::Deserialize;
use std::collections::BTreeMap;
use std::{collections::BTreeMap, path::PathBuf};

mod errors;
pub use self::errors::InvalidPackageError;
Expand All @@ -8,6 +8,8 @@ pub use self::errors::InvalidPackageError;
pub struct PackageManifest {
pub package: PackageMetadata,
pub dependencies: BTreeMap<String, Dependency>,
#[serde(rename(deserialize = "codegen-verifier"))]
pub codegen_verifier: Option<CodegenVerifierMetadata>,
}

impl PackageManifest {
Expand Down Expand Up @@ -39,6 +41,15 @@ pub struct PackageMetadata {
license: Option<String>,
}

#[allow(dead_code)]
#[derive(Debug, Deserialize, Clone)]
pub struct CodegenVerifierMetadata {
/// Path to the directory in which to save the generated verifier.
///
/// This path is assumed to be relative to the package root.
pub out: PathBuf,
}

#[derive(Debug, Deserialize, Clone)]
#[serde(untagged)]
/// Enum representing the different types of ways to
Expand All @@ -60,6 +71,9 @@ fn parse_standard_toml() {
rand = { tag = "next", git = "https://github.com/rust-lang-nursery/rand"}
cool = { tag = "next", git = "https://github.com/rust-lang-nursery/rand"}
hello = {path = "./noir_driver"}
[codegen-verifier]
out = "../hardhat_project/contracts/verifier"
"#;

assert!(PackageManifest::from_toml_str(src).is_ok());
Expand Down
21 changes: 18 additions & 3 deletions crates/nargo_cli/src/cli/codegen_verifier_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use super::fs::{create_named_dir, write_to_file};
use super::NargoConfig;
use crate::{cli::compile_cmd::compile_circuit, constants::CONTRACT_DIR, errors::CliError};
use crate::constants::PKG_FILE;
use crate::manifest;
use crate::{cli::compile_cmd::compile_circuit, errors::CliError};
use clap::Args;
use nargo::manifest::PackageManifest;
use nargo::ops::{codegen_verifier, preprocess_program};
use noirc_driver::CompileOptions;
use std::path::PathBuf;

/// Generates a Solidity verifier smart contract for the program
#[derive(Debug, Clone, Args)]
Expand All @@ -20,11 +24,22 @@ pub(crate) fn run(args: CodegenVerifierCommand, config: NargoConfig) -> Result<(

let smart_contract_string = codegen_verifier(&backend, &preprocessed_program.verification_key)?;

let contract_dir = config.program_dir.join(CONTRACT_DIR);
create_named_dir(&contract_dir, "contract");
let manifest_path = config.program_dir.join(PKG_FILE);
let manifest = manifest::parse(manifest_path)?;
let contract_dir = verifier_out_dir(manifest, config.program_dir);
create_named_dir(&contract_dir, contract_dir.file_name().unwrap().to_str().unwrap());
let contract_path = contract_dir.join("plonk_vk").with_extension("sol");

let path = write_to_file(smart_contract_string.as_bytes(), &contract_path);
println!("Contract successfully created and located at {path}");
Ok(())
}

/// TODO: Move this function to `PackageManifest` after adding track the root directory.
/// See also: https://github.com/noir-lang/noir/pull/1138#discussion_r1165351237
fn verifier_out_dir(manifest: PackageManifest, package_root: PathBuf) -> PathBuf {
return package_root.join(match manifest.codegen_verifier {
Some(cv) => cv.out,
None => "verifier.".into(),
});
}
2 changes: 0 additions & 2 deletions crates/nargo_cli/src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// Directories
/// The directory for the `nargo contract` command output
pub(crate) const CONTRACT_DIR: &str = "contract";
/// The directory to store serialized circuit proofs.
pub(crate) const PROOFS_DIR: &str = "proofs";
/// The directory to store Noir source files
Expand Down
6 changes: 5 additions & 1 deletion crates/nargo_cli/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use hex::FromHexError;
use nargo::NargoError;
use nargo::{manifest::InvalidPackageError, NargoError};
use noirc_abi::errors::{AbiError, InputParserError};
use std::path::PathBuf;
use thiserror::Error;
Expand Down Expand Up @@ -42,4 +42,8 @@ pub(crate) enum CliError {
/// Error from Nargo
#[error(transparent)]
NargoError(#[from] NargoError),

/// Error while loading package manifest.
#[error(transparent)]
InvalidManifest(#[from] InvalidPackageError),
}

0 comments on commit 31577ac

Please sign in to comment.