From a4448cdd12fc7ced34f3af9c0a1ceb0988f1c8c3 Mon Sep 17 00:00:00 2001 From: collin Date: Wed, 29 Jul 2020 23:32:21 -0700 Subject: [PATCH] add manual main input method for testing. fix field tests --- Cargo.lock | 30 -- compiler/Cargo.toml | 3 +- compiler/src/compiler.rs | 7 +- .../inputs/{false_false.in => true_false.in} | 0 compiler/tests/boolean/mod.rs | 19 +- compiler/tests/field/add.leo | 4 +- compiler/tests/field/div.leo | 4 +- compiler/tests/field/eq.leo | 4 +- compiler/tests/field/input.leo | 3 - compiler/tests/field/inputs/register_one.in | 2 + compiler/tests/field/inputs/register_zero.in | 2 + compiler/tests/field/mod.rs | 434 +++++++----------- compiler/tests/field/mul.leo | 4 +- compiler/tests/field/one.leo | 3 - compiler/tests/field/output_register.leo | 3 + compiler/tests/field/sub.leo | 4 +- compiler/tests/field/ternary.leo | 6 +- compiler/tests/field/zero.leo | 3 - compiler/tests/mod.rs | 50 +- .../src/inputs/program_inputs/main_inputs.rs | 6 +- 20 files changed, 239 insertions(+), 352 deletions(-) rename compiler/tests/boolean/inputs/{false_false.in => true_false.in} (100%) delete mode 100644 compiler/tests/field/input.leo create mode 100644 compiler/tests/field/inputs/register_one.in create mode 100644 compiler/tests/field/inputs/register_zero.in delete mode 100644 compiler/tests/field/one.leo create mode 100644 compiler/tests/field/output_register.leo delete mode 100644 compiler/tests/field/zero.leo diff --git a/Cargo.lock b/Cargo.lock index 55dfce8b79..8e9b978e62 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -597,7 +597,6 @@ dependencies = [ "snarkos-models", "snarkos-objects", "snarkos-utilities", - "tempfile", "thiserror", ] @@ -953,12 +952,6 @@ dependencies = [ "num_cpus", ] -[[package]] -name = "redox_syscall" -version = "0.1.57" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce" - [[package]] name = "regex" version = "1.3.9" @@ -977,15 +970,6 @@ version = "0.6.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "26412eb97c6b088a6997e05f69403a802a92d520de2f8e63c2b65f9e0f47c4e8" -[[package]] -name = "remove_dir_all" -version = "0.5.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" -dependencies = [ - "winapi", -] - [[package]] name = "rustc-demangle" version = "0.1.16" @@ -1296,20 +1280,6 @@ dependencies = [ "unicode-xid 0.2.0", ] -[[package]] -name = "tempfile" -version = "3.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9" -dependencies = [ - "cfg-if", - "libc", - "rand", - "redox_syscall", - "remove_dir_all", - "winapi", -] - [[package]] name = "termcolor" version = "1.1.0" diff --git a/compiler/Cargo.toml b/compiler/Cargo.toml index 853f06f72c..f28d145118 100644 --- a/compiler/Cargo.toml +++ b/compiler/Cargo.toml @@ -30,5 +30,4 @@ sha2 = { version = "0.9" } thiserror = { version = "1.0" } [dev-dependencies] -num-bigint = { version = "0.3" } -tempfile = { version = "3.1.0" } \ No newline at end of file +num-bigint = { version = "0.3" } \ No newline at end of file diff --git a/compiler/src/compiler.rs b/compiler/src/compiler.rs index 503be374c4..c78845f4ad 100644 --- a/compiler/src/compiler.rs +++ b/compiler/src/compiler.rs @@ -10,7 +10,7 @@ use crate::{ }; use leo_ast::LeoParser; use leo_inputs::LeoInputsParser; -use leo_types::{Inputs, Program}; +use leo_types::{Inputs, MainInputs, Program}; use snarkos_errors::gadgets::SynthesisError; use snarkos_models::{ @@ -114,6 +114,11 @@ impl> Compiler { Ok(LeoParser::load_file(&self.main_file_path)?) } + /// Manually sets main function inputs + pub fn set_main_inputs(&mut self, inputs: MainInputs) { + self.program_inputs.set_main_inputs(inputs); + } + pub fn checksum(&self) -> Result { // Read in the main file as string let unparsed_file = fs::read_to_string(&self.main_file_path) diff --git a/compiler/tests/boolean/inputs/false_false.in b/compiler/tests/boolean/inputs/true_false.in similarity index 100% rename from compiler/tests/boolean/inputs/false_false.in rename to compiler/tests/boolean/inputs/true_false.in diff --git a/compiler/tests/boolean/mod.rs b/compiler/tests/boolean/mod.rs index 6dd6cd8320..f3cc713878 100644 --- a/compiler/tests/boolean/mod.rs +++ b/compiler/tests/boolean/mod.rs @@ -1,21 +1,13 @@ use crate::{ assert_satisfied, - fail_enforce, get_compiler_error, get_outputs, get_synthesis_error, parse_program, parse_program_with_inputs, - EdwardsConstrainedValue, EdwardsTestCompiler, }; -use leo_compiler::{ - errors::{BooleanError, CompilerError, ExpressionError, FunctionError, StatementError}, - ConstrainedValue, -}; -use leo_types::InputValue; - -use snarkos_models::gadgets::utilities::boolean::Boolean; +use leo_compiler::errors::{BooleanError, CompilerError, ExpressionError, FunctionError, StatementError}; pub fn output_true(program: EdwardsTestCompiler) { let expected = include_bytes!("outputs/register_true.out"); @@ -31,13 +23,6 @@ pub fn output_false(program: EdwardsTestCompiler) { assert_eq!(expected, actual.bytes().as_slice()); } -fn fail_boolean(program: EdwardsTestCompiler) { - match get_compiler_error(program) { - CompilerError::FunctionError(FunctionError::BooleanError(BooleanError::Error(_))) => {} - error => panic!("Expected boolean error, got {}", error), - } -} - fn fail_boolean_statement(program: EdwardsTestCompiler) { match get_compiler_error(program) { CompilerError::FunctionError(FunctionError::StatementError(StatementError::ExpressionError( @@ -60,7 +45,7 @@ fn test_input_pass() { #[test] fn test_input_fail() { let program_bytes = include_bytes!("assert_eq_input.leo"); - let input_bytes = include_bytes!("inputs/false_false.in"); + let input_bytes = include_bytes!("inputs/true_false.in"); let program = parse_program_with_inputs(program_bytes, input_bytes).unwrap(); diff --git a/compiler/tests/field/add.leo b/compiler/tests/field/add.leo index 90a5d093b1..df9cdec45a 100644 --- a/compiler/tests/field/add.leo +++ b/compiler/tests/field/add.leo @@ -1,3 +1,3 @@ -function main(a: field, b: field) -> field { - return a + b +function main(a: field, b: field, c: field) { + assert_eq!(a + b, c); } \ No newline at end of file diff --git a/compiler/tests/field/div.leo b/compiler/tests/field/div.leo index f4b3378ebb..6bb4fd4bc5 100644 --- a/compiler/tests/field/div.leo +++ b/compiler/tests/field/div.leo @@ -1,3 +1,3 @@ -function main(a: field, b: field) -> field { - return a / b +function main(a: field, b: field, c: field) { + assert_eq!(a / b, c); } \ No newline at end of file diff --git a/compiler/tests/field/eq.leo b/compiler/tests/field/eq.leo index 0739f2baab..52699c0383 100644 --- a/compiler/tests/field/eq.leo +++ b/compiler/tests/field/eq.leo @@ -1,3 +1,3 @@ -function main(a: field, b: field) -> bool { - return a == b +function main(a: field, b: field, c: bool) { + assert_eq!(a == b, c); } \ No newline at end of file diff --git a/compiler/tests/field/input.leo b/compiler/tests/field/input.leo deleted file mode 100644 index 1b49fb06e3..0000000000 --- a/compiler/tests/field/input.leo +++ /dev/null @@ -1,3 +0,0 @@ -function main(f: field) -> field{ - return f -} \ No newline at end of file diff --git a/compiler/tests/field/inputs/register_one.in b/compiler/tests/field/inputs/register_one.in new file mode 100644 index 0000000000..50ca7c02dd --- /dev/null +++ b/compiler/tests/field/inputs/register_one.in @@ -0,0 +1,2 @@ +[registers] +r: field = 1; \ No newline at end of file diff --git a/compiler/tests/field/inputs/register_zero.in b/compiler/tests/field/inputs/register_zero.in new file mode 100644 index 0000000000..15d3fe0c68 --- /dev/null +++ b/compiler/tests/field/inputs/register_zero.in @@ -0,0 +1,2 @@ +[registers] +r: field = 0; \ No newline at end of file diff --git a/compiler/tests/field/mod.rs b/compiler/tests/field/mod.rs index e5da123b09..a7b35849fb 100644 --- a/compiler/tests/field/mod.rs +++ b/compiler/tests/field/mod.rs @@ -1,112 +1,26 @@ -use crate::{ - boolean::{output_expected_boolean, output_true}, - get_error, - get_output, - parse_program, - EdwardsConstrainedValue, - EdwardsTestCompiler, -}; -use leo_compiler::{ - errors::{CompilerError, FieldError, FunctionError}, - ConstrainedValue, - FieldType, -}; +use crate::{assert_satisfied, generate_main_inputs, get_synthesis_error, parse_program}; use leo_types::InputValue; +use snarkos_curves::edwards_bls12::Fq; +use snarkos_utilities::bytes::ToBytes; + use num_bigint::BigUint; use rand::{Rng, SeedableRng}; use rand_xorshift::XorShiftRng; -use snarkos_curves::edwards_bls12::Fq; -use snarkos_gadgets::curves::edwards_bls12::FqGadget; -use snarkos_models::{ - curves::{One, PrimeField, Zero}, - gadgets::{ - curves::field::FieldGadget, - r1cs::{ConstraintSystem, TestConstraintSystem}, - }, -}; -use snarkos_utilities::{biginteger::BigInteger256, bytes::ToBytes}; -fn output_expected_constant(program: EdwardsTestCompiler, expected: Fq) { - let output = get_output(program); - assert_eq!( - EdwardsConstrainedValue::Return(vec![ConstrainedValue::Field(FieldType::Constant(expected))]).to_string(), - output.to_string() - ); -} +// Helper function to convert field element into decimal base 10 string +fn field_to_decimal_string(f: Fq) -> String { + // write field to buffer -fn output_expected_allocated(program: EdwardsTestCompiler, expected: FqGadget) { - let output = get_output(program); + let mut buf = Vec::new(); - match output { - EdwardsConstrainedValue::Return(vec) => match vec.as_slice() { - [ConstrainedValue::Field(FieldType::Allocated(fp_gadget))] => assert_eq!(*fp_gadget, expected as FqGadget), - _ => panic!("program output unknown return value"), - }, - _ => panic!("program output unknown return value"), - } -} + f.write(&mut buf).unwrap(); -fn output_zero(program: EdwardsTestCompiler) { - output_expected_constant(program, Fq::zero()) -} + // convert to big integer -fn output_one(program: EdwardsTestCompiler) { - output_expected_constant(program, Fq::one()) -} + let f_bigint = BigUint::from_bytes_le(&buf); -fn fail_field(program: EdwardsTestCompiler) { - match get_error(program) { - CompilerError::FunctionError(FunctionError::FieldError(FieldError::Error(_string))) => {} - error => panic!("Expected invalid field error, got {}", error), - } -} - -#[test] -fn test_zero() { - let bytes = include_bytes!("zero.leo"); - let program = parse_program(bytes).unwrap(); - - output_zero(program); -} - -#[test] -fn test_one() { - let bytes = include_bytes!("one.leo"); - let program = parse_program(bytes).unwrap(); - - output_one(program); -} - -#[test] -fn test_input_pass() { - let bytes = include_bytes!("input.leo"); - let mut program = parse_program(bytes).unwrap(); - - program.set_main_inputs(vec![Some(InputValue::Field("1".into()))]); - - let cs = TestConstraintSystem::::new(); - let expected = FqGadget::one(cs).unwrap(); - - output_expected_allocated(program, expected) -} - -#[test] -fn test_input_fail_bool() { - let bytes = include_bytes!("input.leo"); - let mut program = parse_program(bytes).unwrap(); - - program.set_main_inputs(vec![Some(InputValue::Boolean(true))]); - fail_field(program); -} - -#[test] -fn test_input_fail_none() { - let bytes = include_bytes!("input.leo"); - let mut program = parse_program(bytes).unwrap(); - - program.set_main_inputs(vec![None]); - fail_field(program); + f_bigint.to_str_radix(10) } #[test] @@ -116,32 +30,25 @@ fn test_add() { let mut rng = XorShiftRng::seed_from_u64(1231275789u64); for _ in 0..10 { - let r1: Fq = rng.gen(); - let r2: Fq = rng.gen(); + let a: Fq = rng.gen(); + let b: Fq = rng.gen(); + let c = a.add(&b); - let mut r1_buf = Vec::new(); - let mut r2_buf = Vec::new(); - - r1.write(&mut r1_buf).unwrap(); - r2.write(&mut r2_buf).unwrap(); - - let r1_bigint = BigUint::from_bytes_le(&r1_buf); - let r2_bigint = BigUint::from_bytes_le(&r2_buf); - - let sum = r1.add(&r2); - - let cs = TestConstraintSystem::::new(); - let sum_allocated = FqGadget::from(cs, &sum); + let a_string = field_to_decimal_string(a); + let b_string = field_to_decimal_string(b); + let c_string = field_to_decimal_string(c); let bytes = include_bytes!("add.leo"); let mut program = parse_program(bytes).unwrap(); - program.set_main_inputs(vec![ - Some(InputValue::Field(r1_bigint.to_str_radix(10))), - Some(InputValue::Field(r2_bigint.to_str_radix(10))), + let main_inputs = generate_main_inputs(vec![ + ("a", Some(InputValue::Field(a_string))), + ("b", Some(InputValue::Field(b_string))), + ("c", Some(InputValue::Field(c_string))), ]); + program.set_main_inputs(main_inputs); - output_expected_allocated(program, sum_allocated); + assert_satisfied(program) } } @@ -152,68 +59,25 @@ fn test_sub() { let mut rng = XorShiftRng::seed_from_u64(1231275789u64); for _ in 0..10 { - let r1: Fq = rng.gen(); - let r2: Fq = rng.gen(); + let a: Fq = rng.gen(); + let b: Fq = rng.gen(); + let c = a.sub(&b); - let mut r1_buf = Vec::new(); - let mut r2_buf = Vec::new(); - - r1.write(&mut r1_buf).unwrap(); - r2.write(&mut r2_buf).unwrap(); - - let r1_bigint = BigUint::from_bytes_le(&r1_buf); - let r2_bigint = BigUint::from_bytes_le(&r2_buf); - - let difference = r1.sub(&r2); - - let cs = TestConstraintSystem::::new(); - let difference_allocated = FqGadget::from(cs, &difference); + let a_string = field_to_decimal_string(a); + let b_string = field_to_decimal_string(b); + let c_string = field_to_decimal_string(c); let bytes = include_bytes!("sub.leo"); let mut program = parse_program(bytes).unwrap(); - program.set_main_inputs(vec![ - Some(InputValue::Field(r1_bigint.to_str_radix(10))), - Some(InputValue::Field(r2_bigint.to_str_radix(10))), + let main_inputs = generate_main_inputs(vec![ + ("a", Some(InputValue::Field(a_string))), + ("b", Some(InputValue::Field(b_string))), + ("c", Some(InputValue::Field(c_string))), ]); + program.set_main_inputs(main_inputs); - output_expected_allocated(program, difference_allocated); - } -} - -#[test] -fn test_mul() { - use std::ops::Mul; - - let mut rng = XorShiftRng::seed_from_u64(1231275789u64); - - for _ in 0..10 { - let r1: Fq = rng.gen(); - let r2: Fq = rng.gen(); - - let mut r1_buf = Vec::new(); - let mut r2_buf = Vec::new(); - - r1.write(&mut r1_buf).unwrap(); - r2.write(&mut r2_buf).unwrap(); - - let r1_bigint = BigUint::from_bytes_le(&r1_buf); - let r2_bigint = BigUint::from_bytes_le(&r2_buf); - - let product = r1.mul(&r2); - - let cs = TestConstraintSystem::::new(); - let product_allocated = FqGadget::from(cs, &product); - - let bytes = include_bytes!("mul.leo"); - let mut program = parse_program(bytes).unwrap(); - - program.set_main_inputs(vec![ - Some(InputValue::Field(r1_bigint.to_str_radix(10))), - Some(InputValue::Field(r2_bigint.to_str_radix(10))), - ]); - - output_expected_allocated(program, product_allocated); + assert_satisfied(program) } } @@ -224,32 +88,55 @@ fn test_div() { let mut rng = XorShiftRng::seed_from_u64(1231275789u64); for _ in 0..10 { - let r1: Fq = rng.gen(); - let r2: Fq = rng.gen(); + let a: Fq = rng.gen(); + let b: Fq = rng.gen(); + let c = a.div(&b); - let mut r1_buf = Vec::new(); - let mut r2_buf = Vec::new(); - - r1.write(&mut r1_buf).unwrap(); - r2.write(&mut r2_buf).unwrap(); - - let r1_bigint = BigUint::from_bytes_le(&r1_buf); - let r2_bigint = BigUint::from_bytes_le(&r2_buf); - - let quotient = r1.div(&r2); - - let cs = TestConstraintSystem::::new(); - let quotient_allocated = FqGadget::from(cs, "ient); + let a_string = field_to_decimal_string(a); + let b_string = field_to_decimal_string(b); + let c_string = field_to_decimal_string(c); let bytes = include_bytes!("div.leo"); let mut program = parse_program(bytes).unwrap(); - program.set_main_inputs(vec![ - Some(InputValue::Field(r1_bigint.to_str_radix(10))), - Some(InputValue::Field(r2_bigint.to_str_radix(10))), + let main_inputs = generate_main_inputs(vec![ + ("a", Some(InputValue::Field(a_string))), + ("b", Some(InputValue::Field(b_string))), + ("c", Some(InputValue::Field(c_string))), + ]); + program.set_main_inputs(main_inputs); + + assert_satisfied(program) + } +} + +#[test] +fn test_mul() { + use std::ops::Mul; + + let mut rng = XorShiftRng::seed_from_u64(1231275789u64); + + for _ in 0..10 { + let a: Fq = rng.gen(); + let b: Fq = rng.gen(); + let c = a.mul(&b); + + let a_string = field_to_decimal_string(a); + let b_string = field_to_decimal_string(b); + let c_string = field_to_decimal_string(c); + + let bytes = include_bytes!("mul.leo"); + let mut program = parse_program(bytes).unwrap(); + + let main_inputs = generate_main_inputs(vec![ + ("a", Some(InputValue::Field(a_string))), + ("b", Some(InputValue::Field(b_string))), + ("c", Some(InputValue::Field(c_string))), ]); - output_expected_allocated(program, quotient_allocated); + program.set_main_inputs(main_inputs); + + assert_satisfied(program) } } @@ -258,42 +145,42 @@ fn test_eq() { let mut rng = XorShiftRng::seed_from_u64(1231275789u64); for _ in 0..10 { - let r1: Fq = rng.gen(); - let r2: Fq = rng.gen(); + let a: Fq = rng.gen(); + let b: Fq = rng.gen(); - let mut r1_buf = Vec::new(); - let mut r2_buf = Vec::new(); - - r1.write(&mut r1_buf).unwrap(); - r2.write(&mut r2_buf).unwrap(); - - let r1_bigint = BigUint::from_bytes_le(&r1_buf); - let r2_bigint = BigUint::from_bytes_le(&r2_buf); + let a_string = field_to_decimal_string(a); + let b_string = field_to_decimal_string(b); // test equal let bytes = include_bytes!("eq.leo"); let mut program = parse_program(bytes).unwrap(); - program.set_main_inputs(vec![ - Some(InputValue::Field(r1_bigint.to_str_radix(10))), - Some(InputValue::Field(r1_bigint.to_str_radix(10))), + let main_inputs = generate_main_inputs(vec![ + ("a", Some(InputValue::Field(a_string.clone()))), + ("b", Some(InputValue::Field(a_string.clone()))), + ("c", Some(InputValue::Boolean(true))), ]); - output_true(program); + program.set_main_inputs(main_inputs); + + assert_satisfied(program); // test not equal - let result = r1.eq(&r2); + let c = a.eq(&b); let mut program = parse_program(bytes).unwrap(); - program.set_main_inputs(vec![ - Some(InputValue::Field(r1_bigint.to_str_radix(10))), - Some(InputValue::Field(r2_bigint.to_str_radix(10))), + let main_inputs = generate_main_inputs(vec![ + ("a", Some(InputValue::Field(a_string))), + ("b", Some(InputValue::Field(b_string))), + ("c", Some(InputValue::Boolean(c))), ]); - output_expected_boolean(program, result) + program.set_main_inputs(main_inputs); + + assert_satisfied(program); } } @@ -302,20 +189,21 @@ fn test_assert_eq_pass() { let mut rng = XorShiftRng::seed_from_u64(1231275789u64); for _ in 0..10 { - let r1: Fq = rng.gen(); - let mut r1_buf = Vec::new(); - r1.write(&mut r1_buf).unwrap(); - let r1_bigint = BigUint::from_bytes_le(&r1_buf); + let a: Fq = rng.gen(); + + let a_string = field_to_decimal_string(a); let bytes = include_bytes!("assert_eq.leo"); let mut program = parse_program(bytes).unwrap(); - program.set_main_inputs(vec![ - Some(InputValue::Field(r1_bigint.to_str_radix(10))), - Some(InputValue::Field(r1_bigint.to_str_radix(10))), + let main_inputs = generate_main_inputs(vec![ + ("a", Some(InputValue::Field(a_string.clone()))), + ("b", Some(InputValue::Field(a_string))), ]); - let _ = get_output(program); + program.set_main_inputs(main_inputs); + + assert_satisfied(program); } } @@ -324,68 +212,98 @@ fn test_assert_eq_fail() { let mut rng = XorShiftRng::seed_from_u64(1231275789u64); for _ in 0..10 { - let r1: Fq = rng.gen(); - let r2: Fq = rng.gen(); + let a: Fq = rng.gen(); + let b: Fq = rng.gen(); - let mut r1_buf = Vec::new(); - let mut r2_buf = Vec::new(); - r1.write(&mut r1_buf).unwrap(); - r2.write(&mut r2_buf).unwrap(); - let r1_bigint = BigUint::from_bytes_le(&r1_buf); - let r2_bigint = BigUint::from_bytes_le(&r2_buf); - - if r1 == r2 { + if a == b { continue; } + let a_string = field_to_decimal_string(a); + let b_string = field_to_decimal_string(b); + let bytes = include_bytes!("assert_eq.leo"); let mut program = parse_program(bytes).unwrap(); - program.set_main_inputs(vec![ - Some(InputValue::Field(r1_bigint.to_str_radix(10))), - Some(InputValue::Field(r2_bigint.to_str_radix(10))), + let main_inputs = generate_main_inputs(vec![ + ("a", Some(InputValue::Field(a_string))), + ("b", Some(InputValue::Field(b_string))), ]); - let mut cs = TestConstraintSystem::::new(); - let _ = program.compile_constraints(&mut cs).unwrap(); - assert!(!cs.is_satisfied()); + program.set_main_inputs(main_inputs); + + get_synthesis_error(program); } } #[test] fn test_ternary() { - let r1: u64 = rand::random(); - let r2: u64 = rand::random(); + let mut rng = XorShiftRng::seed_from_u64(1231275789u64); - let b1 = BigInteger256::from(r1); - let b2 = BigInteger256::from(r2); + let a: Fq = rng.gen(); + let b: Fq = rng.gen(); - let f1: Fq = Fq::from_repr(b1); - let f2: Fq = Fq::from_repr(b2); - - let mut cs = TestConstraintSystem::::new(); - let g1 = FqGadget::from(cs.ns(|| "g1"), &f1); - let g2 = FqGadget::from(cs.ns(|| "g2"), &f2); + let a_string = field_to_decimal_string(a); + let b_string = field_to_decimal_string(b); let bytes = include_bytes!("ternary.leo"); - let mut program_1 = parse_program(bytes).unwrap(); - let mut program_2 = program_1.clone(); + let mut program = parse_program(bytes).unwrap(); - // true -> field 1 - program_1.set_main_inputs(vec![ - Some(InputValue::Boolean(true)), - Some(InputValue::Field(r1.to_string())), - Some(InputValue::Field(r2.to_string())), + // true -> field a + let main_inputs = generate_main_inputs(vec![ + ("s", Some(InputValue::Boolean(true))), + ("a", Some(InputValue::Field(a_string.clone()))), + ("b", Some(InputValue::Field(b_string.clone()))), + ("c", Some(InputValue::Field(a_string.clone()))), ]); - output_expected_allocated(program_1, g1); + program.set_main_inputs(main_inputs); - // false -> field 2 - program_2.set_main_inputs(vec![ - Some(InputValue::Boolean(false)), - Some(InputValue::Field(r1.to_string())), - Some(InputValue::Field(r2.to_string())), + assert_satisfied(program); + + let mut program = parse_program(bytes).unwrap(); + + // false -> field b + let main_inputs = generate_main_inputs(vec![ + ("s", Some(InputValue::Boolean(false))), + ("a", Some(InputValue::Field(a_string))), + ("b", Some(InputValue::Field(b_string.clone()))), + ("c", Some(InputValue::Field(b_string))), ]); - output_expected_allocated(program_2, g2); + program.set_main_inputs(main_inputs); + + assert_satisfied(program); } + +// +// pub fn output_one(program: EdwardsTestCompiler) { +// let expected = include_bytes!("outputs/register_one.out"); +// let actual = get_outputs(program); +// +// assert_eq!(expected, actual.bytes().as_slice()); +// } +// +// pub fn output_zero(program: EdwardsTestCompiler) { +// let expected = include_bytes!("outputs/register_zero.out"); +// let actual = get_outputs(program); +// +// assert_eq!(expected, actual.bytes().as_slice()); +// } +// +// #[test] +// fn test_registers() { +// let program_bytes = include_bytes!("output_register.leo"); +// let one_input_bytes = include_bytes!("inputs/register_one.in"); +// let zero_input_bytes = include_bytes!("inputs/register_zero.in"); +// +// // test 1field input register => 1field output register +// let program = parse_program_with_inputs(program_bytes, one_input_bytes).unwrap(); +// +// output_one(program); +// +// // test 0field input register => 0field output register +// let program = parse_program_with_inputs(program_bytes, zero_input_bytes).unwrap(); +// +// output_zero(program); +// } diff --git a/compiler/tests/field/mul.leo b/compiler/tests/field/mul.leo index 5a383183e8..c96104aa23 100644 --- a/compiler/tests/field/mul.leo +++ b/compiler/tests/field/mul.leo @@ -1,3 +1,3 @@ -function main(a: field, b: field) -> field { - return a * b +function main(a: field, b: field, c: field) { + assert_eq!(a * b, c); } \ No newline at end of file diff --git a/compiler/tests/field/one.leo b/compiler/tests/field/one.leo deleted file mode 100644 index d97c60e14f..0000000000 --- a/compiler/tests/field/one.leo +++ /dev/null @@ -1,3 +0,0 @@ -function main() -> field { - return 1field -} \ No newline at end of file diff --git a/compiler/tests/field/output_register.leo b/compiler/tests/field/output_register.leo new file mode 100644 index 0000000000..258a6f2c74 --- /dev/null +++ b/compiler/tests/field/output_register.leo @@ -0,0 +1,3 @@ +function main(registers) -> field { + return registers.r +} \ No newline at end of file diff --git a/compiler/tests/field/sub.leo b/compiler/tests/field/sub.leo index c304200040..2dbca87c07 100644 --- a/compiler/tests/field/sub.leo +++ b/compiler/tests/field/sub.leo @@ -1,3 +1,3 @@ -function main(a: field, b: field) -> field { - return a - b +function main(a: field, b: field, c: field) { + assert_eq!(a - b, c); } \ No newline at end of file diff --git a/compiler/tests/field/ternary.leo b/compiler/tests/field/ternary.leo index 108ec6cdb1..aeb196400e 100644 --- a/compiler/tests/field/ternary.leo +++ b/compiler/tests/field/ternary.leo @@ -1,3 +1,5 @@ -function main(b: bool, f1: field, f2: field) -> field { - return if b ? f1 : f2 +function main(s: bool, a: field, b: field, c: field) { + let r = if s ? a : b; + + assert_eq!(r, c); } \ No newline at end of file diff --git a/compiler/tests/field/zero.leo b/compiler/tests/field/zero.leo deleted file mode 100644 index 9a12e8aac0..0000000000 --- a/compiler/tests/field/zero.leo +++ /dev/null @@ -1,3 +0,0 @@ -function main() -> field { - return 0field -} \ No newline at end of file diff --git a/compiler/tests/mod.rs b/compiler/tests/mod.rs index a099c6b494..2d5c0a3e3f 100644 --- a/compiler/tests/mod.rs +++ b/compiler/tests/mod.rs @@ -2,7 +2,7 @@ // pub mod array; pub mod boolean; // pub mod circuits; -// pub mod field; +pub mod field; // pub mod function; // pub mod group; // pub mod import; @@ -15,8 +15,7 @@ pub mod boolean; use leo_compiler::{ compiler::Compiler, - constraints::generate_constraints, - errors::{CompilerError, FunctionError, StatementError}, + errors::CompilerError, group::targets::edwards_bls12::EdwardsGroupType, ConstrainedValue, OutputBytes, @@ -24,13 +23,12 @@ use leo_compiler::{ use leo_types::{InputValue, MainInputs}; use snarkos_curves::edwards_bls12::Fq; -use snarkos_models::gadgets::r1cs::{ConstraintSynthesizer, TestConstraintSystem}; +use snarkos_models::gadgets::r1cs::TestConstraintSystem; -use std::{env::temp_dir, fs::File, io::Read, path::PathBuf}; +use std::path::PathBuf; pub const TEST_OUTPUTS_DIRECTORY: &str = "/outputs/"; pub const TEST_OUTPUTS_FILE_NAME: &str = "/outputs/test.out"; - const EMPTY_FILE: &str = ""; pub type EdwardsTestCompiler = Compiler; @@ -54,11 +52,9 @@ pub fn parse_program_with_inputs( fn new_compiler() -> EdwardsTestCompiler { let program_name = "test".to_string(); let path = PathBuf::from("/test/src/main.leo"); - // create temporary output directory - let outputs_directory = temp_dir(); - let mut compiler = EdwardsTestCompiler::new(program_name, path, outputs_directory); + let outputs_dir = PathBuf::from(TEST_OUTPUTS_DIRECTORY); - compiler + EdwardsTestCompiler::new(program_name, path, outputs_dir) } pub(crate) fn parse_program(bytes: &[u8]) -> Result { @@ -70,14 +66,14 @@ pub(crate) fn parse_program(bytes: &[u8]) -> Result Result { - let mut compiler = new_compiler(); - let inputs_string = String::from_utf8_lossy(bytes); - - compiler.parse_inputs(&inputs_string, "")?; - - Ok(compiler) -} +// pub(crate) fn parse_inputs(bytes: &[u8]) -> Result { +// let mut compiler = new_compiler(); +// let inputs_string = String::from_utf8_lossy(bytes); +// +// compiler.parse_inputs(&inputs_string, "")?; +// +// Ok(compiler) +// } pub(crate) fn get_outputs(program: EdwardsTestCompiler) -> OutputBytes { // synthesize the circuit on the test constraint system @@ -110,9 +106,19 @@ pub(crate) fn get_synthesis_error(program: EdwardsTestCompiler) { assert!(!cs.is_satisfied()); } -pub(crate) fn fail_enforce(program: EdwardsTestCompiler) { - match get_compiler_error(program) { - CompilerError::FunctionError(FunctionError::StatementError(StatementError::Error(_))) => {} - error => panic!("Expected evaluate error, got {}", error), +// pub(crate) fn fail_enforce(program: EdwardsTestCompiler) { +// match get_compiler_error(program) { +// CompilerError::FunctionError(FunctionError::StatementError(StatementError::Error(_))) => {} +// error => panic!("Expected evaluate error, got {}", error), +// } +// } + +pub(crate) fn generate_main_inputs(inputs: Vec<(&str, Option)>) -> MainInputs { + let mut main_inputs = MainInputs::new(); + + for (name, value) in inputs { + main_inputs.insert(name.to_string(), value); } + + main_inputs } diff --git a/types/src/inputs/program_inputs/main_inputs.rs b/types/src/inputs/program_inputs/main_inputs.rs index 089978bfef..e74cbd9f72 100644 --- a/types/src/inputs/program_inputs/main_inputs.rs +++ b/types/src/inputs/program_inputs/main_inputs.rs @@ -28,13 +28,17 @@ impl MainInputs { self.inputs.len() } + pub fn insert(&mut self, key: String, value: Option) { + self.inputs.insert(key, value); + } + /// Parses main input definitions and stores them in `self`. pub fn parse(&mut self, definitions: Vec) -> Result<(), InputParserError> { for definition in definitions { let name = definition.parameter.variable.value; let value = InputValue::from_expression(definition.parameter.type_, definition.expression)?; - self.inputs.insert(name, Some(value)); + self.insert(name, Some(value)); } Ok(())