Skip to content

Commit

Permalink
Merge branch 'master' into jf/fix-759
Browse files Browse the repository at this point in the history
  • Loading branch information
kevaundray authored Feb 11, 2023
2 parents f87e192 + 1f2d2b7 commit 34585e9
Show file tree
Hide file tree
Showing 40 changed files with 923 additions and 742 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ name: Rust
on: [push, pull_request]

jobs:
check_n_test:
name: cargo check & test
test:
name: Cargo test
uses: noir-lang/.github/.github/workflows/rust-test.yml@main

clippy:
name: cargo clippy
name: Cargo clippy
uses: noir-lang/.github/.github/workflows/rust-clippy.yml@main

format:
name: cargo fmt
name: Cargo fmt
uses: noir-lang/.github/.github/workflows/rust-format.yml@main

spellcheck:
name: spellcheck
name: Spellcheck
uses: noir-lang/.github/.github/workflows/spellcheck.yml@main
6 changes: 4 additions & 2 deletions crates/nargo/src/cli/check_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ pub(crate) fn run(args: ArgMatches) -> Result<(), CliError> {
let args = args.subcommand_matches("check").unwrap();
let allow_warnings = args.is_present("allow-warnings");

let package_dir = std::env::current_dir().unwrap();
check_from_path(package_dir, allow_warnings)?;
let program_dir =
args.value_of("path").map_or_else(|| std::env::current_dir().unwrap(), PathBuf::from);

check_from_path(program_dir, allow_warnings)?;
println!("Constraint system successfully built!");
Ok(())
}
Expand Down
7 changes: 4 additions & 3 deletions crates/nargo/src/cli/compile_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ pub(crate) fn run(args: ArgMatches) -> Result<(), CliError> {
let circuit_name = args.value_of("circuit_name").unwrap();
let witness = args.is_present("witness");
let allow_warnings = args.is_present("allow-warnings");
let program_dir =
args.value_of("path").map_or_else(|| std::env::current_dir().unwrap(), PathBuf::from);

let current_dir = std::env::current_dir().unwrap();
let mut circuit_path = PathBuf::new();
let mut circuit_path = program_dir.clone();
circuit_path.push(TARGET_DIR);

generate_circuit_and_witness_to_disk(
circuit_name,
current_dir,
program_dir,
circuit_path,
witness,
allow_warnings,
Expand Down
18 changes: 10 additions & 8 deletions crates/nargo/src/cli/contract_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
use std::path::PathBuf;

use super::{create_named_dir, write_to_file};
use crate::{cli::compile_cmd::compile_circuit, constants::CONTRACT_DIR, errors::CliError};
use acvm::SmartContract;
use clap::ArgMatches;

pub(crate) fn run(args: ArgMatches) -> Result<(), CliError> {
let cmd = args.subcommand_matches("contract").unwrap();

let package_dir = match cmd.value_of("path") {
Some(path) => std::path::PathBuf::from(path),
None => std::env::current_dir().unwrap(),
};
let args = args.subcommand_matches("contract").unwrap();

let allow_warnings = args.is_present("allow-warnings");
let compiled_program = compile_circuit(package_dir, false, allow_warnings)?;
let program_dir =
args.value_of("path").map_or_else(|| std::env::current_dir().unwrap(), PathBuf::from);

let compiled_program = compile_circuit(program_dir.clone(), false, allow_warnings)?;

let backend = crate::backends::ConcreteBackend;
let smart_contract_string = backend.eth_contract_from_cs(compiled_program.circuit);

let mut contract_path = create_named_dir(CONTRACT_DIR.as_ref(), "contract");
let mut contract_dir = program_dir;
contract_dir.push(CONTRACT_DIR);
let mut contract_path = create_named_dir(contract_dir.as_ref(), "contract");
contract_path.push("plonk_vk");
contract_path.set_extension("sol");

Expand Down
21 changes: 7 additions & 14 deletions crates/nargo/src/cli/execute_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use noirc_driver::CompiledProgram;
use super::{create_named_dir, read_inputs_from_file, write_to_file};
use super::{InputMap, WitnessMap};
use crate::{
cli::compile_cmd::compile_circuit,
constants::{PROVER_INPUT_FILE, TARGET_DIR, WITNESS_EXT},
errors::CliError,
};
Expand All @@ -21,14 +22,18 @@ pub(crate) fn run(args: ArgMatches) -> Result<(), CliError> {
let witness_name = args.value_of("witness_name");
let show_ssa = args.is_present("show-ssa");
let allow_warnings = args.is_present("allow-warnings");
let (return_value, solved_witness) = execute(show_ssa, allow_warnings)?;
let program_dir =
args.value_of("path").map_or_else(|| std::env::current_dir().unwrap(), PathBuf::from);

let compiled_program = compile_circuit(&program_dir, show_ssa, allow_warnings)?;
let (return_value, solved_witness) = execute_program(&program_dir, &compiled_program)?;

println!("Circuit witness successfully solved");
if let Some(return_value) = return_value {
println!("Circuit output: {return_value:?}");
}
if let Some(witness_name) = witness_name {
let mut witness_dir = std::env::current_dir().unwrap();
let mut witness_dir = program_dir;
witness_dir.push(TARGET_DIR);

let witness_path = save_witness_to_dir(solved_witness, witness_name, witness_dir)?;
Expand All @@ -42,18 +47,6 @@ pub(crate) fn run(args: ArgMatches) -> Result<(), CliError> {
/// So when we add witness values, their index start from 1.
const WITNESS_OFFSET: u32 = 1;

fn execute(
show_ssa: bool,
allow_warnings: bool,
) -> Result<(Option<InputValue>, WitnessMap), CliError> {
let current_dir = std::env::current_dir().unwrap();

let compiled_program =
super::compile_cmd::compile_circuit(&current_dir, show_ssa, allow_warnings)?;

execute_program(current_dir, &compiled_program)
}

pub(crate) fn execute_program<P: AsRef<Path>>(
inputs_dir: P,
compiled_program: &CompiledProgram,
Expand Down
10 changes: 4 additions & 6 deletions crates/nargo/src/cli/gates_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use acvm::ProofSystemCompiler;
use clap::ArgMatches;
use std::path::Path;
use std::path::{Path, PathBuf};

use crate::cli::compile_cmd::compile_circuit;
use crate::errors::CliError;
Expand All @@ -9,12 +9,10 @@ pub(crate) fn run(args: ArgMatches) -> Result<(), CliError> {
let args = args.subcommand_matches("gates").unwrap();
let show_ssa = args.is_present("show-ssa");
let allow_warnings = args.is_present("allow-warnings");
count_gates(show_ssa, allow_warnings)
}
let program_dir =
args.value_of("path").map_or_else(|| std::env::current_dir().unwrap(), PathBuf::from);

pub fn count_gates(show_ssa: bool, allow_warnings: bool) -> Result<(), CliError> {
let current_dir = std::env::current_dir().unwrap();
count_gates_with_path(current_dir, show_ssa, allow_warnings)
count_gates_with_path(program_dir, show_ssa, allow_warnings)
}

pub fn count_gates_with_path<P: AsRef<Path>>(
Expand Down
3 changes: 2 additions & 1 deletion crates/nargo/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ pub fn start_cli() {
.arg(
Arg::with_name("witness_name")
.long("witness_name")
.help("Write the execution witness to named file"),
.help("Write the execution witness to named file")
.takes_value(true),
)
.arg(show_ssa)
.arg(allow_warnings),
Expand Down
9 changes: 4 additions & 5 deletions crates/nargo/src/cli/new_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@ use crate::{

use super::{create_named_dir, write_to_file};
use clap::ArgMatches;
use std::path::Path;
use std::path::{Path, PathBuf};

pub(crate) fn run(args: ArgMatches) -> Result<(), CliError> {
let cmd = args.subcommand_matches("new").unwrap();

let package_name = cmd.value_of("package_name").unwrap();

let mut package_dir = match cmd.value_of("path") {
Some(path) => std::path::PathBuf::from(path),
None => std::env::current_dir().unwrap(),
};
let mut package_dir =
args.value_of("path").map_or_else(|| std::env::current_dir().unwrap(), PathBuf::from);

package_dir.push(Path::new(package_name));
if package_dir.exists() {
return Err(CliError::DestinationAlreadyExists(package_dir));
Expand Down
11 changes: 4 additions & 7 deletions crates/nargo/src/cli/prove_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@ pub(crate) fn run(args: ArgMatches) -> Result<(), CliError> {
let show_ssa = args.is_present("show-ssa");
let allow_warnings = args.is_present("allow-warnings");

prove(proof_name, show_ssa, allow_warnings)
}

fn prove(proof_name: Option<&str>, show_ssa: bool, allow_warnings: bool) -> Result<(), CliError> {
let current_dir = std::env::current_dir().unwrap();
let program_dir =
args.value_of("path").map_or_else(|| std::env::current_dir().unwrap(), PathBuf::from);

let mut proof_dir = PathBuf::new();
let mut proof_dir = program_dir.clone();
proof_dir.push(PROOFS_DIR);

prove_with_path(proof_name, current_dir, proof_dir, show_ssa, allow_warnings)?;
prove_with_path(proof_name, program_dir, proof_dir, show_ssa, allow_warnings)?;

Ok(())
}
Expand Down
16 changes: 11 additions & 5 deletions crates/nargo/src/cli/test_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use std::{collections::BTreeMap, io::Write};
use std::{
collections::BTreeMap,
io::Write,
path::{Path, PathBuf},
};

use acvm::{PartialWitnessGenerator, ProofSystemCompiler};
use clap::ArgMatches;
Expand All @@ -14,14 +18,16 @@ pub(crate) fn run(args: ArgMatches) -> Result<(), CliError> {
let args = args.subcommand_matches("test").unwrap();
let test_name = args.value_of("test_name").unwrap_or("");
let allow_warnings = args.is_present("allow-warnings");
run_tests(test_name, allow_warnings)
let program_dir =
args.value_of("path").map_or_else(|| std::env::current_dir().unwrap(), PathBuf::from);

run_tests(&program_dir, test_name, allow_warnings)
}

fn run_tests(test_name: &str, allow_warnings: bool) -> Result<(), CliError> {
fn run_tests(program_dir: &Path, test_name: &str, allow_warnings: bool) -> Result<(), CliError> {
let backend = crate::backends::ConcreteBackend;

let package_dir = std::env::current_dir().unwrap();
let mut driver = Resolver::resolve_root_config(&package_dir, backend.np_language())?;
let mut driver = Resolver::resolve_root_config(program_dir, backend.np_language())?;
add_std_lib(&mut driver);

if driver.check_crate(allow_warnings).is_err() {
Expand Down
22 changes: 9 additions & 13 deletions crates/nargo/src/cli/verify_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,29 @@ use clap::ArgMatches;
use noirc_abi::errors::AbiError;
use noirc_abi::input_parser::Format;
use noirc_driver::CompiledProgram;
use std::{collections::BTreeMap, path::Path, path::PathBuf};
use std::{
collections::BTreeMap,
path::{Path, PathBuf},
};

pub(crate) fn run(args: ArgMatches) -> Result<(), CliError> {
let args = args.subcommand_matches("verify").unwrap();

let proof_name = args.value_of("proof").unwrap();
let mut proof_path = std::path::PathBuf::new();
proof_path.push(Path::new(PROOFS_DIR));
let program_dir =
args.value_of("path").map_or_else(|| std::env::current_dir().unwrap(), PathBuf::from);

let mut proof_path = program_dir.clone();
proof_path.push(Path::new(PROOFS_DIR));
proof_path.push(Path::new(proof_name));
proof_path.set_extension(PROOF_EXT);

let allow_warnings = args.is_present("allow-warnings");
let result = verify(proof_name, allow_warnings)?;
let result = verify_with_path(program_dir, proof_path, false, allow_warnings)?;
println!("Proof verified : {result}\n");
Ok(())
}

fn verify(proof_name: &str, allow_warnings: bool) -> Result<bool, CliError> {
let current_dir = std::env::current_dir().unwrap();
let mut proof_path = PathBuf::new(); //or cur_dir?
proof_path.push(PROOFS_DIR);
proof_path.push(Path::new(proof_name));
proof_path.set_extension(PROOF_EXT);
verify_with_path(&current_dir, &proof_path, false, allow_warnings)
}

pub fn verify_with_path<P: AsRef<Path>>(
program_dir: P,
proof_path: P,
Expand Down
35 changes: 35 additions & 0 deletions crates/nargo/tests/test_data/generics/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,45 @@ fn foo<T>(bar: Bar<T>) {
constrain bar.one == bar.two;
}

struct BigInt<N> {
limbs: [u32; N],
}

impl<N> BigInt<N> {
// `N` is in scope of all methods in the impl
fn first(first: BigInt<N>, second: BigInt<N>) -> Self {
constrain first.limbs != second.limbs;
first
}

fn second(first: BigInt<N>, second: Self) -> Self {
constrain first.limbs != second.limbs;
second
}
}

impl Bar<Field> {
fn get_other(self) -> Field {
self.other
}
}

fn main(x: Field, y: Field) {
let bar1: Bar<Field> = Bar { one: x, two: y, other: 0 };
let bar2 = Bar { one: x, two: y, other: [0] };

foo(bar1);
foo(bar2);

// Test generic impls
let int1 = BigInt { limbs: [1] };
let int2 = BigInt { limbs: [2] };
let BigInt { limbs } = int1.second(int2).first(int1);
constrain limbs == int2.limbs;

// Test impl exclusively for Bar<Field>
constrain bar1.get_other() == bar1.other;

// Expected type error
// constrain bar2.get_other() == bar2.other;
}
32 changes: 7 additions & 25 deletions crates/noirc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use acvm::acir::circuit::Circuit;
use acvm::Language;
use fm::FileType;
use noirc_abi::Abi;
use noirc_errors::{DiagnosableError, ReportedError, Reporter};
use noirc_errors::{reporter, ReportedError};
use noirc_evaluator::create_circuit;
use noirc_frontend::graph::{CrateId, CrateName, CrateType, LOCAL_CRATE};
use noirc_frontend::hir::def_map::CrateDefMap;
Expand Down Expand Up @@ -47,14 +47,7 @@ impl Driver {
pub fn file_compiles(&mut self) -> bool {
let mut errs = vec![];
CrateDefMap::collect_defs(LOCAL_CRATE, &mut self.context, &mut errs);
for errors in &errs {
Reporter::with_diagnostics(
Some(errors.file_id),
&self.context.file_manager,
&errors.errors,
false,
);
}
reporter::report_all(&self.context.file_manager, &errs, false);
errs.is_empty()
}

Expand Down Expand Up @@ -135,17 +128,8 @@ impl Driver {
pub fn check_crate(&mut self, allow_warnings: bool) -> Result<(), ReportedError> {
let mut errs = vec![];
CrateDefMap::collect_defs(LOCAL_CRATE, &mut self.context, &mut errs);
let mut error_count = 0;
for errors in &errs {
error_count += Reporter::with_diagnostics(
Some(errors.file_id),
&self.context.file_manager,
&errors.errors,
allow_warnings,
);
}

Reporter::finish(error_count)
let error_count = reporter::report_all(&self.context.file_manager, &errs, allow_warnings);
reporter::finish_report(error_count)
}

pub fn compute_abi(&self) -> Option<Abi> {
Expand Down Expand Up @@ -207,12 +191,10 @@ impl Driver {
Err(err) => {
// The FileId here will be the file id of the file with the main file
// Errors will be shown at the call site without a stacktrace
let file_id = err.location.map(|loc| loc.file);
let error = &[err.to_diagnostic()];
let file = err.location.map(|loc| loc.file);
let files = &self.context.file_manager;

let error_count = Reporter::with_diagnostics(file_id, files, error, allow_warnings);
Reporter::finish(error_count)?;
let error = reporter::report(files, &err.into(), file, allow_warnings);
reporter::finish_report(error as u32)?;
Err(ReportedError)
}
}
Expand Down
Loading

0 comments on commit 34585e9

Please sign in to comment.