Skip to content

Commit

Permalink
merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
vezenovm committed Feb 16, 2023
2 parents 3c9e953 + 87d0610 commit 189defd
Show file tree
Hide file tree
Showing 34 changed files with 889 additions and 225 deletions.
246 changes: 212 additions & 34 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion crates/nargo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ edition.workspace = true
[build-dependencies]
dirs.workspace = true
rustc_version = "0.4.0"
build-data = "0.1.3"

[dependencies]
dirs.workspace = true
Expand All @@ -25,7 +26,6 @@ serde.workspace = true
thiserror.workspace = true
clap = "2.33.3"
const_format = "0.2.30"
git-version = "0.3.5"
hex = "0.4.2"
termcolor = "1.1.2"
tempdir = "0.3.7"
Expand Down
5 changes: 5 additions & 0 deletions crates/nargo/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ pub fn copy<U: AsRef<Path>, V: AsRef<Path>>(from: U, to: V) -> Result<(), std::i

fn main() {
check_rustc_version();

build_data::set_GIT_COMMIT();
build_data::set_GIT_DIRTY();
build_data::no_debug_rebuilds();

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");
Expand Down
20 changes: 15 additions & 5 deletions crates/nargo/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use acvm::{
pub use check_cmd::check_from_path;
use clap::{App, AppSettings, Arg};
use const_format::formatcp;
use git_version::git_version;
use noirc_abi::{
input_parser::{Format, InputValue},
Abi,
Expand Down Expand Up @@ -39,8 +38,12 @@ mod prove_cmd;
mod test_cmd;
mod verify_cmd;

const SHORT_GIT_HASH: &str = git_version!(prefix = "git:");
const VERSION_STRING: &str = formatcp!("{} ({})", env!("CARGO_PKG_VERSION"), SHORT_GIT_HASH);
const GIT_HASH: &str = env!("GIT_COMMIT");
const IS_DIRTY: &str = env!("GIT_DIRTY");
const CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION");

static VERSION_STRING: &str =
formatcp!("{} (git version hash: {}, is dirty: {})", CARGO_PKG_VERSION, GIT_HASH, IS_DIRTY);

/// A map from the fields in an TOML/JSON file which correspond to some ABI to their values
pub type InputMap = BTreeMap<String, InputValue>;
Expand Down Expand Up @@ -89,7 +92,9 @@ pub fn start_cli() {
)
.subcommand(
App::new("prove")
.about("Create proof for this program")
.about(
"Create proof for this program. The proof is returned as a hex encoded string.",
)
.arg(Arg::with_name("proof_name").help("The name of the proof"))
.arg(Arg::with_name("circuit_name").help(
"The name of the circuit build files (ACIR, proving and verification keys)",
Expand All @@ -105,7 +110,12 @@ pub fn start_cli() {
Arg::with_name("test_name")
.help("If given, only tests with names containing this string will be run"),
)
.arg(allow_warnings.clone()),
.arg(allow_warnings.clone())
.arg(
Arg::with_name("show-logs")
.long("show-logs")
.help("Display output of println statements during tests"),
),
)
.subcommand(
App::new("compile")
Expand Down
17 changes: 12 additions & 5 deletions crates/nargo/src/cli/test_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,19 @@ 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");
let show_output = args.is_present("show-logs");
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)
run_tests(&program_dir, test_name, allow_warnings, show_output)
}

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

let mut driver = Resolver::resolve_root_config(program_dir, backend.np_language())?;
Expand All @@ -43,10 +49,10 @@ fn run_tests(program_dir: &Path, test_name: &str, allow_warnings: bool) -> Resul

for test_function in test_functions {
let test_name = driver.function_name(test_function);
write!(writer, "Testing {test_name}... ").expect("Failed to write to stdout");
writeln!(writer, "Testing {test_name}...").expect("Failed to write to stdout");
writer.flush().ok();

match run_test(test_name, test_function, &driver, allow_warnings) {
match run_test(test_name, test_function, &driver, allow_warnings, show_output) {
Ok(_) => {
writer.set_color(ColorSpec::new().set_fg(Some(Color::Green))).ok();
writeln!(writer, "ok").ok();
Expand Down Expand Up @@ -76,12 +82,13 @@ fn run_test(
main: FuncId,
driver: &Driver,
allow_warnings: bool,
show_output: bool,
) -> Result<(), CliError> {
let backend = crate::backends::ConcreteBackend;
let language = backend.np_language();

let program = driver
.compile_no_check(language, false, allow_warnings, Some(main))
.compile_no_check(language, false, allow_warnings, Some(main), show_output)
.map_err(|_| CliError::Generic(format!("Test '{test_name}' failed to compile")))?;

let mut solved_witness = BTreeMap::new();
Expand Down
3 changes: 2 additions & 1 deletion crates/nargo/tests/test_data/strings/Prover.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
message = "hello world"
y = 5
hex_as_string = "0x41"
hex_as_field = "0x41"
hex_as_field = "0x41"
46 changes: 45 additions & 1 deletion crates/nargo/tests/test_data/strings/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,12 +1,56 @@
fn main(message : pub str<11>, hex_as_string : str<4>, hex_as_field : Field) {
use dep::std;

fn main(message : pub str<11>, y : Field, hex_as_string : str<4>, hex_as_field : Field) {
let mut bad_message = "hello world";

constrain message == "hello world";
bad_message = "helld world";
let x = 10;
let z = x * 5;
std::println(10);

std::println(z); // x * 5 in println not yet supported
std::println(x);

let array = [1, 2, 3, 5, 8];
constrain y == 5; // Change to y != 5 to see how the later print statements are not called
std::println(array);

std::println(bad_message);
constrain message != bad_message;

let hash = std::hash::pedersen([x]);
std::println(hash);

constrain hex_as_string == "0x41";
// constrain hex_as_string != 0x41; This will fail with a type mismatch between str[4] and Field
constrain hex_as_field == 0x41;
}

#[test]
fn test_prints_strings() {
let message = "hello world!";

std::println(message);
std::println("goodbye world");
}

#[test]
fn test_prints_array() {
let array = [1, 2, 3, 5, 8];

// TODO: Printing structs currently not supported
// let s = Test { a: 1, b: 2, c: [3, 4] };
// std::println(s);

std::println(array);

let hash = std::hash::pedersen(array);
std::println(hash);
}

struct Test {
a: Field,
b: Field,
c: [Field; 2],
}
31 changes: 17 additions & 14 deletions crates/noirc_abi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use std::{collections::BTreeMap, convert::TryInto, str};
use acvm::FieldElement;
use errors::AbiError;
use input_parser::InputValue;
use iter_extended::vecmap;
use serde::{Deserialize, Serialize};

// This is the ABI used to bridge the different TOML formats for the initial
// witness, the partial witness generator and the interpreter.
//
Expand Down Expand Up @@ -260,19 +260,10 @@ impl Abi {
InputValue::Vec(field_elements)
}
AbiType::String { length } => {
let string_as_slice: Vec<u8> = field_iterator
.take(*length as usize)
.map(|e| {
let mut field_as_bytes = e.to_be_bytes();
let char_byte = field_as_bytes.pop().unwrap(); // A character in a string is represented by a u8, thus we just want the last byte of the element
assert!(field_as_bytes.into_iter().all(|b| b == 0)); // Assert that the rest of the field element's bytes are empty
char_byte
})
.collect();

let final_string = str::from_utf8(&string_as_slice).unwrap();

InputValue::String(final_string.to_owned())
let field_elements: Vec<FieldElement> =
field_iterator.take(*length as usize).collect();

InputValue::String(decode_string_value(&field_elements))
}
AbiType::Struct { fields, .. } => {
let mut struct_map = BTreeMap::new();
Expand All @@ -290,3 +281,15 @@ impl Abi {
Ok(value)
}
}

pub fn decode_string_value(field_elements: &[FieldElement]) -> String {
let string_as_slice = vecmap(field_elements, |e| {
let mut field_as_bytes = e.to_be_bytes();
let char_byte = field_as_bytes.pop().unwrap(); // A character in a string is represented by a u8, thus we just want the last byte of the element
assert!(field_as_bytes.into_iter().all(|b| b == 0)); // Assert that the rest of the field element's bytes are empty
char_byte
});

let final_string = str::from_utf8(&string_as_slice).unwrap();
final_string.to_owned()
}
5 changes: 3 additions & 2 deletions crates/noirc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ impl Driver {
allow_warnings: bool,
) -> Result<CompiledProgram, ReportedError> {
self.check_crate(allow_warnings)?;
self.compile_no_check(np_language, show_ssa, allow_warnings, None)
self.compile_no_check(np_language, show_ssa, allow_warnings, None, true)
}

/// Compile the current crate. Assumes self.check_crate is called beforehand!
Expand All @@ -163,6 +163,7 @@ impl Driver {
allow_warnings: bool,
// Optional override to provide a different `main` function to start execution
main_function: Option<FuncId>,
show_output: bool,
) -> Result<CompiledProgram, ReportedError> {
// Find the local crate, one should always be present
let local_crate = self.context.def_map(LOCAL_CRATE).unwrap();
Expand All @@ -187,7 +188,7 @@ impl Driver {
let program = monomorphize(main_function, &self.context.def_interner);

let blackbox_supported = acvm::default_is_black_box_supported(np_language.clone());
match create_circuit(program, np_language, blackbox_supported, show_ssa) {
match create_circuit(program, np_language, blackbox_supported, show_ssa, show_output) {
Ok(circuit) => Ok(CompiledProgram { circuit, abi: Some(abi) }),
Err(err) => {
// The FileId here will be the file id of the file with the main file
Expand Down
6 changes: 4 additions & 2 deletions crates/noirc_evaluator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ pub fn create_circuit(
np_language: Language,
is_blackbox_supported: IsBlackBoxSupported,
enable_logging: bool,
show_output: bool,
) -> Result<Circuit, RuntimeError> {
let mut evaluator = Evaluator::new();

// First evaluate the main function
evaluator.evaluate_main_alt(program, enable_logging)?;
evaluator.evaluate_main_alt(program, enable_logging, show_output)?;

let witness_index = evaluator.current_witness_index();

Expand Down Expand Up @@ -110,6 +111,7 @@ impl Evaluator {
&mut self,
program: Program,
enable_logging: bool,
show_output: bool,
) -> Result<(), RuntimeError> {
let mut ir_gen = IrGenerator::new(program);
self.parse_abi_alt(&mut ir_gen);
Expand All @@ -118,7 +120,7 @@ impl Evaluator {
ir_gen.ssa_gen_main()?;

//Generates ACIR representation:
ir_gen.context.ir_to_acir(self, enable_logging)?;
ir_gen.context.ir_to_acir(self, enable_logging, show_output)?;
Ok(())
}

Expand Down
17 changes: 14 additions & 3 deletions crates/noirc_evaluator/src/ssa/acir_gen.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::ssa::{
builtin,
context::SsaContext,
node::{Instruction, Operation},
};
Expand Down Expand Up @@ -34,6 +35,7 @@ impl Acir {
ins: &Instruction,
evaluator: &mut Evaluator,
ctx: &SsaContext,
show_output: bool,
) -> Result<(), RuntimeErrorKind> {
use operations::{
binary, condition, constrain, intrinsics, load, not, r#return, store, truncate,
Expand All @@ -57,7 +59,16 @@ impl Acir {
truncate::evaluate(value, *bit_size, *max_bit_size, var_cache, evaluator, ctx)
}
Operation::Intrinsic(opcode, args) => {
intrinsics::evaluate(args, ins, *opcode, var_cache, acir_mem, ctx, evaluator)
let opcode = match opcode {
builtin::Opcode::Println(print_info) => {
builtin::Opcode::Println(builtin::PrintlnInfo {
is_string_output: print_info.is_string_output,
show_output,
})
}
_ => *opcode,
};
intrinsics::evaluate(args, ins, opcode, var_cache, acir_mem, ctx, evaluator)
}
Operation::Return(node_ids) => {
r#return::evaluate(node_ids, acir_mem, var_cache, evaluator, ctx)?
Expand All @@ -68,8 +79,8 @@ impl Acir {
Operation::Load { array_id, index } => {
load::evaluate(*array_id, *index, acir_mem, var_cache, evaluator, ctx)
}
Operation::Store { array_id, index, value } => {
store::evaluate(*array_id, *index, *value, acir_mem, var_cache, evaluator, ctx)
Operation::Store { .. } => {
store::evaluate(&ins.operation, acir_mem, var_cache, evaluator, ctx)
}
Operation::Nop => None,
i @ Operation::Jne(..)
Expand Down
3 changes: 3 additions & 0 deletions crates/noirc_evaluator/src/ssa/acir_gen/internal_var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ impl InternalVar {
pub(crate) fn set_id(&mut self, id: NodeId) {
self.id = Some(id)
}
pub(crate) fn cached_witness(&self) -> &Option<Witness> {
&self.cached_witness
}

pub fn to_expression(&self) -> Expression {
if let Some(w) = self.cached_witness {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl InternalVarCache {
let field_value = FieldElement::from_be_bytes_reduce(&c.value.to_bytes_be());
InternalVar::from_constant(field_value)
}
NodeObject::Obj(variable) => {
NodeObject::Variable(variable) => {
let variable_type = variable.get_type();
match variable_type {
ObjectType::Boolean
Expand Down
24 changes: 17 additions & 7 deletions crates/noirc_evaluator/src/ssa/acir_gen/operations/condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
},
Evaluator,
};
use acvm::FieldElement;
use acvm::{acir::native_types::Expression, FieldElement};

pub(crate) fn evaluate(
condition: NodeId,
Expand All @@ -19,11 +19,21 @@ pub(crate) fn evaluate(
let cond = var_cache.get_or_compute_internal_var_unwrap(condition, evaluator, ctx);
let l_c = var_cache.get_or_compute_internal_var_unwrap(lhs, evaluator, ctx);
let r_c = var_cache.get_or_compute_internal_var_unwrap(rhs, evaluator, ctx);
let sub = constraints::subtract(l_c.expression(), FieldElement::one(), r_c.expression());
let result = constraints::add(
&constraints::mul_with_witness(evaluator, cond.expression(), &sub),
FieldElement::one(),
r_c.expression(),
);
let result =
evaluate_expression(cond.expression(), l_c.expression(), r_c.expression(), evaluator);
Some(result.into())
}

pub fn evaluate_expression(
condition: &Expression,
lhs: &Expression,
rhs: &Expression,
evaluator: &mut Evaluator,
) -> Expression {
let sub = constraints::subtract(lhs, FieldElement::one(), rhs);
constraints::add(
&constraints::mul_with_witness(evaluator, condition, &sub),
FieldElement::one(),
rhs,
)
}
Loading

0 comments on commit 189defd

Please sign in to comment.