Skip to content
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
267 changes: 161 additions & 106 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ members = [
"crates/noirc_errors",
"crates/noirc_driver",
"crates/acir",
"crates/std_lib",
"crates/nargo",
"crates/fm",
"crates/arena",
Expand Down
3 changes: 3 additions & 0 deletions crates/nargo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[build-dependencies]
dirs = "4"

[dependencies]
dirs = "3.0.1"
url = "2.2.0"
Expand Down
15 changes: 1 addition & 14 deletions crates/std_lib/build.rs → crates/nargo/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ pub fn copy<U: AsRef<Path>, V: AsRef<Path>>(from: U, to: V) -> Result<(), std::i
let entry = entry?;
let path = entry.path();

if is_rs_file(&path) {
continue;
}

if path.is_dir() {
stack.push(path);
} else {
Expand All @@ -66,17 +62,8 @@ pub fn copy<U: AsRef<Path>, V: AsRef<Path>>(from: U, to: V) -> Result<(), std::i
Ok(())
}

// We use lib.rs to lean on Rusts build system, but we do not want it or any other rust files to be copied
fn is_rs_file(src: &Path) -> bool {
// assert_eq!("rs", path.extension().unwrap());
match src.extension() {
Some(ext) => ext == "rs",
None => false,
}
}

fn main() {
let stdlib_src_dir = Path::new("src/");
let stdlib_src_dir = Path::new("noir_stdlib/");
rerun_if_stdlib_changes(stdlib_src_dir);
let target = dirs::config_dir().unwrap().join("noir-lang").join("std");
copy(stdlib_src_dir, &target).unwrap();
Expand Down
6 changes: 6 additions & 0 deletions crates/nargo/noir_stdlib/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "std"
authors = [""]
compiler_version = "0.1"

[dependencies]
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
33 changes: 32 additions & 1 deletion crates/nargo/src/cli/build_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::{Path, PathBuf};

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

use super::{write_to_file, PROVER_INPUT_FILE, VERIFIER_INPUT_FILE};
use super::{add_std_lib, write_to_file, PROVER_INPUT_FILE, VERIFIER_INPUT_FILE};

pub(crate) fn run(_args: ArgMatches) -> Result<(), CliError> {
let package_dir = std::env::current_dir().unwrap();
Expand All @@ -14,6 +14,7 @@ pub(crate) fn run(_args: ArgMatches) -> Result<(), CliError> {
// This is exposed so that we can run the examples and verify that they pass
pub fn build_from_path<P: AsRef<Path>>(p: P) -> Result<(), CliError> {
let mut driver = Resolver::resolve_root_config(p.as_ref())?;
add_std_lib(&mut driver);
driver.build();
// XXX: We can have a --overwrite flag to determine if you want to overwrite the Prover/Verifier.toml files
if let Some(x) = driver.compute_abi() {
Expand Down Expand Up @@ -41,3 +42,33 @@ pub fn build_from_path<P: AsRef<Path>>(p: P) -> Result<(), CliError> {
}
Ok(())
}

#[cfg(test)]
mod tests {
const TEST_DATA_DIR: &str = "tests/build_tests_data";

#[test]
fn pass() {
let mut pass_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
pass_dir.push(&format!("{TEST_DATA_DIR}/pass"));

let paths = std::fs::read_dir(pass_dir).unwrap();
for path in paths.flatten() {
let path = path.path();
assert!(super::build_from_path(path.clone()).is_ok(), "path: {}", path.display());
}
}

#[test]
#[ignore = "This test fails because the reporter exits the process with 1"]
fn fail() {
let mut fail_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
fail_dir.push(&format!("{TEST_DATA_DIR}/fail"));

let paths = std::fs::read_dir(fail_dir).unwrap();
for path in paths.flatten() {
let path = path.path();
assert!(super::build_from_path(path.clone()).is_err(), "path: {}", path.display());
}
}
}
5 changes: 3 additions & 2 deletions crates/nargo/src/cli/compile_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::path::Path;

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

use super::{create_named_dir, write_to_file, BUILD_DIR};
use super::{add_std_lib, create_named_dir, write_to_file, BUILD_DIR};

pub(crate) fn run(args: ArgMatches) -> Result<(), CliError> {
let args = args.subcommand_matches("compile").unwrap();
Expand Down Expand Up @@ -61,8 +61,9 @@ pub fn compile_circuit<P: AsRef<Path>>(
program_dir: P,
show_ssa: bool,
) -> Result<noirc_driver::CompiledProgram, CliError> {
let driver = Resolver::resolve_root_config(program_dir.as_ref())?;
let mut driver = Resolver::resolve_root_config(program_dir.as_ref())?;
let backend = crate::backends::ConcreteBackend;
add_std_lib(&mut driver);
let compiled_program = driver.into_compiled_program(backend.np_language(), show_ssa);

Ok(compiled_program)
Expand Down
3 changes: 2 additions & 1 deletion crates/nargo/src/cli/contract_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ pub(crate) fn run(args: ArgMatches) -> Result<(), CliError> {
Some(path) => std::path::PathBuf::from(path),
None => std::env::current_dir().unwrap(),
};
let driver = Resolver::resolve_root_config(&package_dir)?;
let mut driver = Resolver::resolve_root_config(&package_dir)?;

let backend = crate::backends::ConcreteBackend;
super::add_std_lib(&mut driver);
let compiled_program = driver.into_compiled_program(backend.np_language(), false);

let smart_contract_string = backend.eth_contract_from_cs(compiled_program.circuit);
Expand Down
3 changes: 2 additions & 1 deletion crates/nargo/src/cli/gates_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ pub fn count_gates_with_path<P: AsRef<Path>>(
program_dir: P,
show_ssa: bool,
) -> Result<(), CliError> {
let driver = Resolver::resolve_root_config(program_dir.as_ref())?;
let mut driver = Resolver::resolve_root_config(program_dir.as_ref())?;
let backend = crate::backends::ConcreteBackend;

super::add_std_lib(&mut driver);
let compiled_program = driver.into_compiled_program(backend.np_language(), show_ssa);
let gates = compiled_program.circuit.gates;

Expand Down
58 changes: 58 additions & 0 deletions crates/nargo/src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
pub use build_cmd::build_from_path;
use clap::{App, Arg};
use noirc_driver::Driver;
use noirc_frontend::graph::{CrateName, CrateType};
use std::{
fs::File,
io::Write,
Expand Down Expand Up @@ -139,3 +141,59 @@ pub fn prove_and_verify(proof_name: &str, prg_dir: &Path, show_ssa: bool) -> boo

verify_cmd::verify_with_path(prg_dir, &proof_path, show_ssa).unwrap()
}

fn add_std_lib(driver: &mut Driver) {
let path_to_std_lib_file = path_to_stdlib().join("lib.nr");
let std_crate = driver.create_non_local_crate(path_to_std_lib_file, CrateType::Library);
let std_crate_name = "std";
driver.propagate_dep(std_crate, &CrateName::new(std_crate_name).unwrap());
}

fn path_to_stdlib() -> PathBuf {
dirs::config_dir().unwrap().join("noir-lang").join("std/src")
}

// FIXME: I not sure that this is the right place for this tests.
#[cfg(test)]
mod tests {
use noirc_driver::Driver;
use noirc_frontend::graph::CrateType;

use std::path::{PathBuf, Path};

const TEST_DATA_DIR: &str = "tests/compile_tests_data";

/// Compiles a file and returns true if compilation was successful
///
/// This is used for tests.
pub fn file_compiles<P: AsRef<Path>>(root_file: P) -> bool {
let mut driver = Driver::new();
driver.create_local_crate(&root_file, CrateType::Binary);
super::add_std_lib(&mut driver);
driver.file_compiles()
}

#[test]
fn compilation_pass() {
let mut pass_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
pass_dir.push(&format!("{TEST_DATA_DIR}/pass"));

let paths = std::fs::read_dir(pass_dir).unwrap();
for path in paths.flatten() {
let path = path.path();
assert!(file_compiles(&path), "path: {}", path.display());
}
}

#[test]
fn compilation_fail() {
let mut fail_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
fail_dir.push(&format!("{TEST_DATA_DIR}/fail"));

let paths = std::fs::read_dir(fail_dir).unwrap();
for path in paths.flatten() {
let path = path.path();
assert!(!file_compiles(&path), "path: {}", path.display());
}
}
}
3 changes: 2 additions & 1 deletion crates/nargo/src/cli/verify_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ pub fn verify_with_path<P: AsRef<Path>>(
proof_path: P,
show_ssa: bool,
) -> Result<bool, CliError> {
let driver = Resolver::resolve_root_config(program_dir.as_ref())?;
let mut driver = Resolver::resolve_root_config(program_dir.as_ref())?;
let backend = crate::backends::ConcreteBackend;

super::add_std_lib(&mut driver);
let compiled_program = driver.into_compiled_program(backend.np_language(), show_ssa);

let public_abi = compiled_program.abi.clone().unwrap().public_abi();
Expand Down
5 changes: 5 additions & 0 deletions crates/nargo/tests/build_tests_data/fail/basic/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
authors = [""]
compiler_version = "0.1"

[dependencies]
5 changes: 5 additions & 0 deletions crates/nargo/tests/build_tests_data/fail/dup_func/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
authors = [""]
compiler_version = "0.1"

[dependencies]
5 changes: 5 additions & 0 deletions crates/nargo/tests/build_tests_data/pass/basic/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
authors = [""]
compiler_version = "0.1"

[dependencies]
2 changes: 2 additions & 0 deletions crates/nargo/tests/build_tests_data/pass/basic/Prover.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x = ""
y = ""
Empty file.
5 changes: 5 additions & 0 deletions crates/nargo/tests/build_tests_data/pass/import/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
authors = [""]
compiler_version = "0.1"

[dependencies]
2 changes: 2 additions & 0 deletions crates/nargo/tests/build_tests_data/pass/import/Prover.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x = ""
y = ""
Empty file.
7 changes: 7 additions & 0 deletions crates/nargo/tests/compile_tests_data/fail/basic.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

// This should not compile as the keyword
// is `constrain` and not `constrai`

fn main(x : Field, y : Field) {
constrai x != y;
}
8 changes: 8 additions & 0 deletions crates/nargo/tests/compile_tests_data/fail/dup_func.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Duplicate functions should not compile
fn hello(x : Field) -> Field {
x
}

fn hello(x : Field) -> Field {
x
}
4 changes: 4 additions & 0 deletions crates/nargo/tests/compile_tests_data/pass/basic.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

fn main(x : Field, y : Field) {
constrain x != y;
}
11 changes: 11 additions & 0 deletions crates/nargo/tests/compile_tests_data/pass/basic_import.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
mod import;
use dep::std;

use crate::import::hello;

fn main(x : Field, y : Field) {
let _k = std::hash::pedersen([x]);
let _l = hello(x);

constrain x != import::hello(y);
}
4 changes: 4 additions & 0 deletions crates/nargo/tests/compile_tests_data/pass/import.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

fn hello(x : Field) -> Field {
x
}
1 change: 0 additions & 1 deletion crates/noirc_driver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ noirc_frontend = { path = "../noirc_frontend" }
noirc_evaluator = { path = "../noirc_evaluator" }
noirc_abi = { path = "../noirc_abi" }
acvm = { git = "https://github.com/noir-lang/noir", rev = "cc5ee63072e09779bebd7e7dd054ae16be307d7f" }
std_lib = { path = "../std_lib" }
fm = { path = "../fm" }
serde = { version = "1.0.136", features = ["derive"] }

Expand Down
Loading