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
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
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::{Path, PathBuf};

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());
}
}
}
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
66 changes: 21 additions & 45 deletions crates/noirc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,9 @@ impl Driver {
/// 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);
driver.add_std_lib();
pub fn file_compiles(&mut self) -> bool {
let mut errs = vec![];
CrateDefMap::collect_defs(LOCAL_CRATE, &mut driver.context, &mut errs);
CrateDefMap::collect_defs(LOCAL_CRATE, &mut self.context, &mut errs);
for errors in &errs {
dbg!(errors);
}
Expand Down Expand Up @@ -104,10 +101,26 @@ impl Driver {
.expect("cyclic dependency triggered");
}

/// Adds the standard library to the dep graph
/// and statically analyses the local crate
/// Propagates a given dependency to every other crate.
pub fn propagate_dep(&mut self, dep_to_propagate: CrateId, dep_to_propagate_name: &CrateName) {
let crate_ids: Vec<_> = self
.context
.crate_graph
.iter_keys()
.filter(|crate_id| *crate_id != dep_to_propagate)
.collect();

for crate_id in crate_ids {
self.context
.crate_graph
.add_dep(crate_id, dep_to_propagate_name.clone(), dep_to_propagate)
.expect("ice: cyclic error triggered with std library");
}
}

// NOTE: Maybe build could be skipped given that now it is a pass through method.
/// Statically analyses the local crate
pub fn build(&mut self) {
self.add_std_lib();
self.analyse_crate()
}

Expand Down Expand Up @@ -180,47 +193,10 @@ impl Driver {

CompiledProgram { circuit, abi: Some(abi) }
}

#[cfg(not(feature = "std"))]
pub fn add_std_lib(&mut self) {
// TODO: Currently, we do not load the standard library when the program
// TODO: is compiled using the wasm version of noir
}

#[cfg(feature = "std")]
/// XXX: It is sub-optimal to add the std as a regular crate right now because
/// we have no way to determine whether a crate has been compiled already.
/// XXX: We Ideally need a way to check if we've already compiled a crate and not re-compile it
pub fn add_std_lib(&mut self) {
let path_to_std_lib_file = path_to_stdlib().join("lib.nr");

let std_crate_id = self.create_non_local_crate(path_to_std_lib_file, CrateType::Library);

let name = CrateName::new("std").unwrap();

let crate_ids: Vec<_> = self
.context
.crate_graph
.iter_keys()
.filter(|crate_id| *crate_id != std_crate_id)
.collect();
// Add std as a crate dependency to every other crate
for crate_id in crate_ids {
self.context
.crate_graph
.add_dep(crate_id, name.clone(), std_crate_id)
.expect("ice: cyclic error triggered with std library");
}
}
}

impl Default for Driver {
fn default() -> Self {
Self::new()
}
}

#[cfg(feature = "std")]
fn path_to_stdlib() -> PathBuf {
dirs::config_dir().unwrap().join("noir-lang").join("std")
}
27 changes: 0 additions & 27 deletions crates/noirc_driver/tests/tests.rs

This file was deleted.

1 change: 0 additions & 1 deletion crates/noirc_frontend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ edition = "2021"
acvm = { git = "https://github.com/noir-lang/noir" }
noirc_abi = { path = "../noirc_abi" }
noirc_errors = { path = "../noirc_errors" }
std_lib = { path = "../std_lib" }
fm = { path = "../fm" }
arena = { path = "../arena" }
chumsky = { git = "https://github.com/jfecher/chumsky", rev = "ad9d312" }
Expand Down
Loading