From b4c163ce4f5ff13320a27b33ddda35d3a219254c Mon Sep 17 00:00:00 2001 From: collin Date: Tue, 2 Jun 2020 16:06:25 -0700 Subject: [PATCH] add field gadget tests --- compiler/tests/field/assert_eq.leo | 3 + compiler/tests/field/eq.leo | 3 + compiler/tests/field/mod.rs | 133 ++++++++++++++++++++++++++--- compiler/tests/field/ternary.leo | 3 + compiler/tests/group/mod.rs | 49 ++++------- compiler/tests/mod.rs | 13 ++- 6 files changed, 162 insertions(+), 42 deletions(-) create mode 100644 compiler/tests/field/assert_eq.leo create mode 100644 compiler/tests/field/eq.leo create mode 100644 compiler/tests/field/ternary.leo diff --git a/compiler/tests/field/assert_eq.leo b/compiler/tests/field/assert_eq.leo new file mode 100644 index 0000000000..ebf7eecb1a --- /dev/null +++ b/compiler/tests/field/assert_eq.leo @@ -0,0 +1,3 @@ +function main(a: field, b: field) { + assert_eq!(a, b); +} \ No newline at end of file diff --git a/compiler/tests/field/eq.leo b/compiler/tests/field/eq.leo new file mode 100644 index 0000000000..839bf5d64e --- /dev/null +++ b/compiler/tests/field/eq.leo @@ -0,0 +1,3 @@ +function main(a: field, b: field) -> bool { + return a == b +} \ No newline at end of file diff --git a/compiler/tests/field/mod.rs b/compiler/tests/field/mod.rs index b471b678b1..f17efe98db 100644 --- a/compiler/tests/field/mod.rs +++ b/compiler/tests/field/mod.rs @@ -1,3 +1,4 @@ +use crate::boolean::{output_false, output_true}; use crate::{compile_program, get_error, get_output, EdwardsConstrainedValue, EdwardsTestCompiler}; use leo_compiler::{ errors::{CompilerError, FieldError, FunctionError}, @@ -7,7 +8,10 @@ use leo_compiler::{ use snarkos_curves::edwards_bls12::Fq; use snarkos_gadgets::curves::edwards_bls12::FqGadget; use snarkos_models::curves::{Field, PrimeField}; -use snarkos_models::gadgets::{curves::field::FieldGadget, r1cs::TestConstraintSystem}; +use snarkos_models::gadgets::{ + curves::field::FieldGadget, + r1cs::{ConstraintSystem, TestConstraintSystem}, +}; use snarkos_utilities::biginteger::BigInteger256; const DIRECTORY_NAME: &str = "tests/field/"; @@ -141,10 +145,10 @@ fn test_sub() { let f1: Fq = Fq::from_repr(b1); let f2: Fq = Fq::from_repr(b2); - let sum = f1.sub(&f2); + let difference = f1.sub(&f2); let cs = TestConstraintSystem::::new(); - let sum_allocated = FqGadget::from(cs, &sum); + let difference_allocated = FqGadget::from(cs, &difference); let mut program = compile_program(DIRECTORY_NAME, "sub.leo").unwrap(); program.set_inputs(vec![ @@ -152,7 +156,7 @@ fn test_sub() { Some(InputValue::Field(r2.to_string())), ]); - output_expected_allocated(program, sum_allocated); + output_expected_allocated(program, difference_allocated); } } @@ -170,10 +174,10 @@ fn test_mul() { let f1: Fq = Fq::from_repr(b1); let f2: Fq = Fq::from_repr(b2); - let sum = f1.mul(&f2); + let product = f1.mul(&f2); let cs = TestConstraintSystem::::new(); - let sum_allocated = FqGadget::from(cs, &sum); + let product_allocated = FqGadget::from(cs, &product); let mut program = compile_program(DIRECTORY_NAME, "mul.leo").unwrap(); program.set_inputs(vec![ @@ -181,7 +185,7 @@ fn test_mul() { Some(InputValue::Field(r2.to_string())), ]); - output_expected_allocated(program, sum_allocated); + output_expected_allocated(program, product_allocated); } } @@ -199,10 +203,10 @@ fn test_div() { let f1: Fq = Fq::from_repr(b1); let f2: Fq = Fq::from_repr(b2); - let sum = f1.div(&f2); + let quotient = f1.div(&f2); let cs = TestConstraintSystem::::new(); - let sum_allocated = FqGadget::from(cs, &sum); + let quotient_allocated = FqGadget::from(cs, "ient); let mut program = compile_program(DIRECTORY_NAME, "div.leo").unwrap(); program.set_inputs(vec![ @@ -210,6 +214,115 @@ fn test_div() { Some(InputValue::Field(r2.to_string())), ]); - output_expected_allocated(program, sum_allocated); + output_expected_allocated(program, quotient_allocated); } } + +#[test] +fn test_eq_true() { + for _ in 0..10 { + let r1: u64 = rand::random(); + + let mut program = compile_program(DIRECTORY_NAME, "eq.leo").unwrap(); + program.set_inputs(vec![ + Some(InputValue::Field(r1.to_string())), + Some(InputValue::Field(r1.to_string())), + ]); + + output_true(program) + } +} + +#[test] +fn test_eq_false() { + for _ in 0..10 { + let r1: u64 = rand::random(); + let r2: u64 = rand::random(); + + if r1 == r2 { + continue; + } + + let mut program = compile_program(DIRECTORY_NAME, "eq.leo").unwrap(); + program.set_inputs(vec![ + Some(InputValue::Field(r1.to_string())), + Some(InputValue::Field(r2.to_string())), + ]); + + output_false(program) + } +} + +#[test] +fn test_assert_eq_pass() { + for _ in 0..10 { + let r1: u64 = rand::random(); + + let mut program = compile_program(DIRECTORY_NAME, "assert_eq.leo").unwrap(); + program.set_inputs(vec![ + Some(InputValue::Field(r1.to_string())), + Some(InputValue::Field(r1.to_string())), + ]); + + let _ = get_output(program); + } +} + +#[test] +fn test_assert_eq_fail() { + for _ in 0..10 { + let r1: u64 = rand::random(); + let r2: u64 = rand::random(); + + if r1 == r2 { + continue; + } + + let mut program = compile_program(DIRECTORY_NAME, "assert_eq.leo").unwrap(); + program.set_inputs(vec![ + Some(InputValue::Field(r1.to_string())), + Some(InputValue::Field(r2.to_string())), + ]); + + let mut cs = TestConstraintSystem::::new(); + let _ = program.compile_constraints(&mut cs).unwrap(); + assert!(!cs.is_satisfied()); + } +} + +#[test] +fn test_ternary() { + let r1: u64 = rand::random(); + let r2: u64 = rand::random(); + + let b1 = BigInteger256::from(r1); + let b2 = BigInteger256::from(r2); + + 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 mut program_1 = compile_program(DIRECTORY_NAME, "ternary.leo").unwrap(); + let mut program_2 = program_1.clone(); + + // true -> field 1 + program_1.set_inputs(vec![ + Some(InputValue::Boolean(true)), + Some(InputValue::Field(r1.to_string())), + Some(InputValue::Field(r2.to_string())), + ]); + + output_expected_allocated(program_1, g1); + + // false -> field 2 + program_2.set_inputs(vec![ + Some(InputValue::Boolean(false)), + Some(InputValue::Field(r1.to_string())), + Some(InputValue::Field(r2.to_string())), + ]); + + output_expected_allocated(program_2, g2); +} diff --git a/compiler/tests/field/ternary.leo b/compiler/tests/field/ternary.leo new file mode 100644 index 0000000000..c85ac6a764 --- /dev/null +++ b/compiler/tests/field/ternary.leo @@ -0,0 +1,3 @@ +function main(b: bool, f1: field, f2: field) -> field { + return if b ? f1 : f2 +} \ No newline at end of file diff --git a/compiler/tests/group/mod.rs b/compiler/tests/group/mod.rs index fcb3b7b10f..02d8effda8 100644 --- a/compiler/tests/group/mod.rs +++ b/compiler/tests/group/mod.rs @@ -1,12 +1,8 @@ use crate::{ boolean::{output_false, output_true}, - compile_program, get_error, get_output, EdwardsConstrainedValue, EdwardsTestCompiler, -}; -use leo_compiler::{ - errors::{CompilerError, FunctionError, StatementError}, - group::edwards_bls12::EdwardsGroupType, - ConstrainedValue, InputValue, + compile_program, fail_enforce, get_output, EdwardsConstrainedValue, EdwardsTestCompiler, }; +use leo_compiler::{group::edwards_bls12::EdwardsGroupType, ConstrainedValue, InputValue}; use snarkos_curves::edwards_bls12::{EdwardsAffine, Fq}; use snarkos_gadgets::curves::edwards_bls12::EdwardsBlsGadget; @@ -50,15 +46,6 @@ fn output_zero(program: EdwardsTestCompiler) { output_expected_constant(program, EdwardsAffine::zero()) } -fn fail_enforce(program: EdwardsTestCompiler) { - match get_error(program) { - CompilerError::FunctionError(FunctionError::StatementError( - StatementError::SynthesisError(_), - )) => {} - error => panic!("Expected evaluate error, got {}", error), - } -} - #[test] fn test_zero() { let program = compile_program(DIRECTORY_NAME, "zero.leo").unwrap(); @@ -72,6 +59,20 @@ fn test_point() { output_expected_constant(program, point); } +#[test] +fn test_input() { + let mut program = compile_program(DIRECTORY_NAME, "input.leo").unwrap(); + program.set_inputs(vec![Some(InputValue::Group(TEST_POINT_1.into()))]); + + let mut cs = TestConstraintSystem::::new(); + let constant_point = EdwardsAffine::from_str(TEST_POINT_1).unwrap(); + let allocated_point = + >::alloc(&mut cs, || Ok(constant_point)) + .unwrap(); + + output_expected_allocated(program, allocated_point); +} + #[test] fn test_add() { use std::ops::Add; @@ -111,31 +112,17 @@ fn test_eq_false() { } #[test] -fn test_assert_eq_true() { +fn test_assert_eq_pass() { let program = compile_program(DIRECTORY_NAME, "assert_eq_true.leo").unwrap(); let _res = get_output(program); } #[test] -fn test_assert_eq_false() { +fn test_assert_eq_fail() { let program = compile_program(DIRECTORY_NAME, "assert_eq_false.leo").unwrap(); fail_enforce(program); } -#[test] -fn test_input() { - let mut program = compile_program(DIRECTORY_NAME, "input.leo").unwrap(); - program.set_inputs(vec![Some(InputValue::Group(TEST_POINT_1.into()))]); - - let mut cs = TestConstraintSystem::::new(); - let constant_point = EdwardsAffine::from_str(TEST_POINT_1).unwrap(); - let allocated_point = - >::alloc(&mut cs, || Ok(constant_point)) - .unwrap(); - - output_expected_allocated(program, allocated_point); -} - #[test] fn test_ternary() { let mut program_1 = compile_program(DIRECTORY_NAME, "ternary.leo").unwrap(); diff --git a/compiler/tests/mod.rs b/compiler/tests/mod.rs index 62262d85d4..66c167a10b 100644 --- a/compiler/tests/mod.rs +++ b/compiler/tests/mod.rs @@ -10,7 +10,9 @@ pub mod mutability; pub mod statement; use leo_compiler::{ - compiler::Compiler, errors::CompilerError, group::edwards_bls12::EdwardsGroupType, + compiler::Compiler, + errors::{CompilerError, FunctionError, StatementError}, + group::edwards_bls12::EdwardsGroupType, ConstrainedValue, }; @@ -33,6 +35,15 @@ pub(crate) fn get_error(program: EdwardsTestCompiler) -> CompilerError { program.compile_constraints(&mut cs).unwrap_err() } +pub(crate) fn fail_enforce(program: EdwardsTestCompiler) { + match get_error(program) { + CompilerError::FunctionError(FunctionError::StatementError( + StatementError::SynthesisError(_), + )) => {} + error => panic!("Expected evaluate error, got {}", error), + } +} + pub(crate) fn compile_program( directory_name: &str, file_name: &str,