fix uint tests

This commit is contained in:
collin 2020-07-30 15:41:03 -07:00
parent 1b6a2b55d4
commit 2f983180b7
100 changed files with 494 additions and 537 deletions

View File

@ -46,6 +46,7 @@ fn test_add() {
("b", Some(InputValue::Field(b_string))), ("b", Some(InputValue::Field(b_string))),
("c", Some(InputValue::Field(c_string))), ("c", Some(InputValue::Field(c_string))),
]); ]);
program.set_main_inputs(main_inputs); program.set_main_inputs(main_inputs);
assert_satisfied(program) assert_satisfied(program)

View File

@ -1,23 +1,32 @@
use crate::{get_error, EdwardsTestCompiler}; use crate::{expect_compiler_error, EdwardsTestCompiler};
use leo_compiler::errors::{CompilerError, FunctionError, IntegerError}; use leo_compiler::errors::{CompilerError, ExpressionError, FunctionError, IntegerError, StatementError, ValueError};
pub trait IntegerTester { pub trait IntegerTester {
/// Tests use of the integer in a function input /// Tests defining the smalled value that can be represented by the integer type
fn test_input(); fn test_min();
/// Tests a wrapping addition /// Tests defining the smallest value - 1
fn test_min_fail();
/// Tests defining the largest value that can be represented by the integer type
fn test_max();
/// Tests defining the largest value + 1
fn test_max_fail();
/// Tests a non-wrapping addition
fn test_add(); fn test_add();
/// Tests a wrapping subtraction /// Tests a non-wrapping subtraction
fn test_sub(); fn test_sub();
/// Tests a wrapping multiplication /// Tests a non-wrapping multiplication
fn test_mul(); fn test_mul();
/// Tests a non-wrapping division /// Tests a non-wrapping division
fn test_div(); fn test_div();
/// Tests a wrapping exponentiation /// Tests a non-wrapping exponentiation
fn test_pow(); fn test_pow();
/// Tests == evaluation /// Tests == evaluation
@ -42,9 +51,11 @@ pub trait IntegerTester {
fn test_ternary(); fn test_ternary();
} }
pub(crate) fn fail_integer(program: EdwardsTestCompiler) { pub(crate) fn expect_fail(program: EdwardsTestCompiler) {
match get_error(program) { match expect_compiler_error(program) {
CompilerError::FunctionError(FunctionError::IntegerError(IntegerError::Error(_string))) => {} CompilerError::FunctionError(FunctionError::StatementError(StatementError::ExpressionError(
error => panic!("Expected invalid boolean error, got {}", error), ExpressionError::ValueError(ValueError::IntegerError(IntegerError::Error(_))),
))) => {}
error => panic!("Expected invalid boolean error, got {:?}", error),
} }
} }

View File

@ -14,8 +14,8 @@ pub mod u32;
pub mod u64; pub mod u64;
pub mod u8; pub mod u8;
pub mod i128; // pub mod i128;
pub mod i16; // pub mod i16;
pub mod i32; // pub mod i32;
pub mod i64; // pub mod i64;
pub mod i8; // pub mod i8;

View File

@ -1,3 +1,3 @@
function main(a: u128, b: u128) -> u128 { function main(a: u128, b: u128, c: u128) {
return a + b assert_eq!(a + b, c);
} }

View File

@ -1,3 +1,3 @@
function main(a: u128, b: u128) -> u128 { function main(a: u128, b: u128, c: u128) {
return a / b assert_eq!(a / b, c);
} }

View File

@ -1,3 +1,3 @@
function main(a: u128, b: u128) -> bool { function main(a: u128, b: u128, c: bool) {
return a == b assert_eq!(a == b, c);
} }

View File

@ -1,3 +1,3 @@
function main(a: u128, b: u128) -> bool { function main(a: u128, b: u128, c: bool) {
return a >= b assert_eq!(a >= b, c);
} }

View File

@ -1,3 +1,3 @@
function main(a: u128, b: u128) -> bool { function main(a: u128, b: u128, c: bool) {
return a > b assert_eq!(a > b, c);
} }

View File

@ -1,3 +1,3 @@
function main(x: u128) -> u128 { function main(a: u128, b: u128) {
return x assert_eq!(a, b);
} }

View File

@ -1,3 +1,3 @@
function main(a: u128, b: u128) -> bool { function main(a: u128, b: u128, c: bool) {
return a <= b assert_eq!(a <= b, c);
} }

View File

@ -1,3 +1,3 @@
function main(a: u128, b: u128) -> bool { function main(a: u128, b: u128, c: bool) {
return a < b assert_eq!(a < b, c);
} }

View File

@ -1,3 +1,3 @@
function main() -> u128 { function main() {
return 340282366920938463463374607431768211455 let a: u128 = 340282366920938463463374607431768211455;
} }

View File

@ -0,0 +1,3 @@
function main() {
let a: u128 = 340282366920938463463374607431768211456;
}

View File

@ -1,3 +1,3 @@
function main() -> u128 { function main() {
return 0 let a: u32 = 0;
} }

View File

@ -0,0 +1,3 @@
function main() {
let a: u32 = -1;
}

View File

@ -1,48 +1,33 @@
use crate::{ use crate::{
boolean::{output_expected_boolean, output_false, output_true}, assert_satisfied,
get_error, expect_synthesis_error,
get_output, generate_main_inputs,
integers::{fail_integer, IntegerTester}, integers::{expect_fail, IntegerTester},
parse_program, parse_program,
EdwardsConstrainedValue,
EdwardsTestCompiler,
}; };
use leo_compiler::{ConstrainedValue, Integer};
use leo_inputs::types::{IntegerType, U128Type}; use leo_inputs::types::{IntegerType, U128Type};
use leo_types::InputValue; use leo_types::InputValue;
use snarkos_curves::edwards_bls12::Fq;
use snarkos_models::gadgets::{
r1cs::TestConstraintSystem,
utilities::{alloc::AllocGadget, uint::UInt128},
};
fn output_expected_allocated(program: EdwardsTestCompiler, expected: UInt128) {
let output = get_output(program);
match output {
EdwardsConstrainedValue::Return(vec) => match vec.as_slice() {
[ConstrainedValue::Integer(Integer::U128(actual))] => assert_eq!(*actual, expected),
_ => panic!("program output unknown return value"),
},
_ => panic!("program output unknown return value"),
}
}
test_uint!(TestU128, u128, IntegerType::U128Type(U128Type {}), UInt128); test_uint!(TestU128, u128, IntegerType::U128Type(U128Type {}), UInt128);
#[test] #[test]
fn test_u128_min() { fn test_u128_min() {
TestU128::test_min(std::u128::MIN); TestU128::test_min();
}
#[test]
fn test_u128_min_fail() {
TestU128::test_min_fail();
} }
#[test] #[test]
fn test_u128_max() { fn test_u128_max() {
TestU128::test_max(std::u128::MAX); TestU128::test_max();
} }
#[test] #[test]
fn test_u128_input() { fn test_u128_max_fail() {
TestU128::test_input(); TestU128::test_max_fail();
} }
#[test] #[test]
@ -55,18 +40,17 @@ fn test_u128_sub() {
TestU128::test_sub(); TestU128::test_sub();
} }
#[test] // this test take ~1 min #[test]
fn test_u128_mul() { fn test_u128_mul() {
TestU128::test_mul(); TestU128::test_mul();
} }
#[test] // this test takes ~30 sec #[test]
fn test_u128_div() { fn test_u128_div() {
TestU128::test_div(); TestU128::test_div();
} }
#[test] #[test]
#[ignore] // this test takes ~10 mins
fn test_u128_pow() { fn test_u128_pow() {
TestU128::test_pow(); TestU128::test_pow();
} }

View File

@ -1,3 +1,3 @@
function main(a: u128, b: u128) -> u128 { function main(a: u128, b: u128, c: u128) {
return a * b assert_eq!(a * b, c);
} }

View File

@ -1,3 +0,0 @@
function main() -> u128 {
return 1
}

View File

@ -1,3 +1,3 @@
function main(a: u128, b: u128) -> u128 { function main(a: u128, b: u128, c: u128) {
return a ** b assert_eq!(a ** b, c);
} }

View File

@ -1,3 +1,3 @@
function main(a: u128, b: u128) -> u128 { function main(a: u128, b: u128, c: u128) {
return a - b assert_eq!(a - b, c);
} }

View File

@ -1,3 +1,5 @@
function main(b: bool, x: u128, y: u128) -> u128 { function main(s: bool, a: u128, b: u128, c: u128) {
return if b ? x : y let r = if s ? a : b;
assert_eq!(r, c);
} }

View File

@ -1,3 +0,0 @@
function main() -> u128 {
return 0
}

View File

@ -1,3 +1,3 @@
function main(a: u16, b: u16) -> u16 { function main(a: u16, b: u16, c: u16) {
return a + b assert_eq!(a + b, c);
} }

View File

@ -1,3 +1,3 @@
function main(a: u16, b: u16) -> u16 { function main(a: u16, b: u16, c: u16) {
return a / b assert_eq!(a / b, c);
} }

View File

@ -1,3 +1,3 @@
function main(a: u16, b: u16) -> bool { function main(a: u16, b: u16, c: bool) {
return a == b assert_eq!(a == b, c);
} }

View File

@ -1,3 +1,3 @@
function main(a: u16, b: u16) -> bool { function main(a: u16, b: u16, c: bool) {
return a >= b assert_eq!(a >= b, c);
} }

View File

@ -1,3 +1,3 @@
function main(a: u16, b: u16) -> bool { function main(a: u16, b: u16, c: bool) {
return a > b assert_eq!(a > b, c);
} }

View File

@ -1,3 +1,3 @@
function main(x: u16) -> u16 { function main(a: u16, b: u16) {
return x assert_eq!(a, b);
} }

View File

@ -1,3 +1,3 @@
function main(a: u16, b: u16) -> bool { function main(a: u16, b: u16, c: bool) {
return a <= b assert_eq!(a <= b, c);
} }

View File

@ -1,3 +1,3 @@
function main(a: u16, b: u16) -> bool { function main(a: u16, b: u16, c: bool) {
return a < b assert_eq!(a < b, c);
} }

View File

@ -1,3 +1,3 @@
function main() -> u16 { function main() {
return 65535 let a: u16 = 65535;
} }

View File

@ -0,0 +1,3 @@
function main() {
let a: u16 = 65536;
}

View File

@ -1,3 +1,3 @@
function main() -> u16 { function main() {
return 0 let a: u32 = 0;
} }

View File

@ -0,0 +1,3 @@
function main() {
let a: u32 = -1;
}

View File

@ -1,49 +1,33 @@
use crate::{ use crate::{
boolean::{output_expected_boolean, output_false, output_true}, assert_satisfied,
get_error, expect_synthesis_error,
get_output, generate_main_inputs,
integers::{fail_integer, IntegerTester}, integers::{expect_fail, IntegerTester},
parse_program, parse_program,
EdwardsConstrainedValue,
EdwardsTestCompiler,
}; };
use leo_compiler::{ConstrainedValue, Integer};
use leo_inputs::types::{IntegerType, U16Type}; use leo_inputs::types::{IntegerType, U16Type};
use leo_types::InputValue; use leo_types::InputValue;
use snarkos_curves::edwards_bls12::Fq;
use snarkos_models::gadgets::{
r1cs::TestConstraintSystem,
utilities::{alloc::AllocGadget, uint::UInt16},
};
fn output_expected_allocated(program: EdwardsTestCompiler, expected: UInt16) {
let output = get_output(program);
match output {
EdwardsConstrainedValue::Return(vec) => match vec.as_slice() {
[ConstrainedValue::Integer(Integer::U16(actual))] => assert_eq!(*actual, expected),
_ => panic!("program output unknown return value"),
},
_ => panic!("program output unknown return value"),
}
}
test_uint!(TestU16, u16, IntegerType::U16Type(U16Type {}), UInt16); test_uint!(TestU16, u16, IntegerType::U16Type(U16Type {}), UInt16);
#[test] #[test]
fn test_u16_min() { fn test_u16_min() {
TestU16::test_min(std::u16::MIN); TestU16::test_min();
}
#[test]
fn test_u16_min_fail() {
TestU16::test_min_fail();
} }
#[test] #[test]
fn test_u16_max() { fn test_u16_max() {
TestU16::test_max(std::u16::MAX); TestU16::test_max();
} }
#[test] #[test]
fn test_u16_input() { fn test_u16_max_fail() {
TestU16::test_input(); TestU16::test_max_fail();
} }
#[test] #[test]

View File

@ -1,3 +1,3 @@
function main(a: u16, b: u16) -> u16 { function main(a: u16, b: u16, c: u16) {
return a * b assert_eq!(a * b, c);
} }

View File

@ -1,3 +0,0 @@
function main() -> u16 {
return 1
}

View File

@ -1,3 +1,3 @@
function main(a: u16, b: u16) -> u16 { function main(a: u16, b: u16, c: u16) {
return a ** b assert_eq!(a ** b, c);
} }

View File

@ -1,3 +1,3 @@
function main(a: u16, b: u16) -> u16 { function main(a: u16, b: u16, c: u16) {
return a - b assert_eq!(a - b, c);
} }

View File

@ -1,3 +1,5 @@
function main(b: bool, x: u16, y: u16) -> u16 { function main(s: bool, a: u16, b: u16, c: u16) {
return if b ? x : y let r = if s ? a : b;
assert_eq!(r, c);
} }

View File

@ -1,3 +0,0 @@
function main() -> u16 {
return 0
}

View File

@ -1,3 +1,3 @@
function main(a: u32, b: u32) -> u32 { function main(a: u32, b: u32, c: u32) {
return a + b assert_eq!(a + b, c);
} }

View File

@ -1,3 +1,3 @@
function main(a: u32, b: u32) -> u32 { function main(a: u32, b: u32, c: u32) {
return a / b assert_eq!(a / b, c);
} }

View File

@ -1,3 +1,3 @@
function main(a: u32, b: u32) -> bool { function main(a: u32, b: u32, c: bool) {
return a == b assert_eq!(a == b, c);
} }

View File

@ -1,3 +1,3 @@
function main(a: u32, b: u32) -> bool { function main(a: u32, b: u32, c: bool) {
return a >= b assert_eq!(a >= b, c);
} }

View File

@ -1,3 +1,3 @@
function main(a: u32, b: u32) -> bool { function main(a: u32, b: u32, c: bool) {
return a > b assert_eq!(a > b, c);
} }

View File

@ -1,3 +1,3 @@
function main(x: u32) -> u32 { function main(a: u32, b: u32) {
return x assert_eq!(a, b);
} }

View File

@ -1,3 +1,3 @@
function main(a: u32, b: u32) -> bool { function main(a: u32, b: u32, c: bool) {
return a <= b assert_eq!(a <= b, c);
} }

View File

@ -1,3 +1,3 @@
function main(a: u32, b: u32) -> bool { function main(a: u32, b: u32, c: bool) {
return a < b assert_eq!(a < b, c);
} }

View File

@ -1,3 +1,3 @@
function main() -> u32 { function main() {
return 4294967295 let a: u32 = 4294967295;
} }

View File

@ -0,0 +1,3 @@
function main() {
let a: u32 = 4294967296;
}

View File

@ -1,3 +1,3 @@
function main() -> u32 { function main() {
return 0 let a: u32 = 0;
} }

View File

@ -0,0 +1,3 @@
function main() {
let a: u32 = -1;
}

View File

@ -1,66 +1,33 @@
use crate::{ use crate::{
boolean::{output_expected_boolean, output_false, output_true}, assert_satisfied,
get_error, expect_synthesis_error,
get_output, generate_main_inputs,
integers::{fail_integer, IntegerTester}, integers::{expect_fail, IntegerTester},
parse_program, parse_program,
EdwardsConstrainedValue,
EdwardsTestCompiler,
}; };
use leo_compiler::{ConstrainedValue, Integer};
use leo_inputs::types::{IntegerType, U32Type}; use leo_inputs::types::{IntegerType, U32Type};
use leo_types::InputValue; use leo_types::InputValue;
use snarkos_curves::edwards_bls12::Fq;
use snarkos_models::gadgets::{
r1cs::TestConstraintSystem,
utilities::{alloc::AllocGadget, uint::UInt32},
};
fn output_expected_allocated(program: EdwardsTestCompiler, expected: UInt32) {
let output = get_output(program);
match output {
EdwardsConstrainedValue::Return(vec) => match vec.as_slice() {
[ConstrainedValue::Integer(Integer::U32(actual))] => assert_eq!(*actual, expected),
_ => panic!("program output unknown return value"),
},
_ => panic!("program output unknown return value"),
}
}
pub(crate) fn output_number(program: EdwardsTestCompiler, number: u32) {
let output = get_output(program);
assert_eq!(
EdwardsConstrainedValue::Return(vec![ConstrainedValue::Integer(Integer::U32(UInt32::constant(number)))])
.to_string(),
output.to_string()
)
}
pub(crate) fn output_zero(program: EdwardsTestCompiler) {
output_number(program, 0u32);
}
pub(crate) fn output_one(program: EdwardsTestCompiler) {
output_number(program, 1u32);
}
test_uint!(TestU32, u32, IntegerType::U32Type(U32Type {}), UInt32); test_uint!(TestU32, u32, IntegerType::U32Type(U32Type {}), UInt32);
#[test] #[test]
fn test_u32_min() { fn test_u32_min() {
TestU32::test_min(std::u32::MIN); TestU32::test_min();
}
#[test]
fn test_u32_min_fail() {
TestU32::test_min_fail();
} }
#[test] #[test]
fn test_u32_max() { fn test_u32_max() {
TestU32::test_max(std::u32::MAX); TestU32::test_max();
} }
#[test] #[test]
fn test_u32_input() { fn test_u32_max_fail() {
TestU32::test_input(); TestU32::test_max_fail();
} }
#[test] #[test]

View File

@ -1,3 +1,3 @@
function main(a: u32, b: u32) -> u32 { function main(a: u32, b: u32, c: u32) {
return a * b assert_eq!(a * b, c);
} }

View File

@ -1,3 +0,0 @@
function main() -> u32 {
return 1
}

View File

@ -1,3 +1,3 @@
function main(a: u32, b: u32) -> u32 { function main(a: u32, b: u32, c: u32) {
return a ** b assert_eq!(a ** b, c);
} }

View File

@ -1,3 +1,3 @@
function main(a: u32, b: u32) -> u32 { function main(a: u32, b: u32, c: u32) {
return a - b assert_eq!(a - b, c);
} }

View File

@ -1,3 +1,5 @@
function main(b: bool, x: u32, y: u32) -> u32 { function main(s: bool, a: u32, b: u32, c: u32) {
return if b ? x : y let r = if s ? a : b;
assert_eq!(r, c);
} }

View File

@ -1,3 +0,0 @@
function main() -> u32 {
return 0
}

View File

@ -1,3 +1,3 @@
function main(a: u64, b: u64) -> u64 { function main(a: u64, b: u64, c: u64) {
return a + b assert_eq!(a + b, c);
} }

View File

@ -1,3 +1,3 @@
function main(a: u64, b: u64) -> u64 { function main(a: u64, b: u64, c: u64) {
return a / b assert_eq!(a / b, c);
} }

View File

@ -1,3 +1,3 @@
function main(a: u64, b: u64) -> bool { function main(a: u64, b: u64, c: bool) {
return a == b assert_eq!(a == b, c);
} }

View File

@ -1,3 +1,3 @@
function main(a: u64, b: u64) -> bool { function main(a: u64, b: u64, c: bool) {
return a >= b assert_eq!(a >= b, c);
} }

View File

@ -1,3 +1,3 @@
function main(a: u64, b: u64) -> bool { function main(a: u64, b: u64, c: bool) {
return a > b assert_eq!(a > b, c);
} }

View File

@ -1,3 +1,3 @@
function main(x: u64) -> u64 { function main(a: u64, b: u64) {
return x assert_eq!(a, b);
} }

View File

@ -1,3 +1,3 @@
function main(a: u64, b: u64) -> bool { function main(a: u64, b: u64, c: bool) {
return a <= b assert_eq!(a <= b, c);
} }

View File

@ -1,3 +1,3 @@
function main(a: u64, b: u64) -> bool { function main(a: u64, b: u64, c: bool) {
return a < b assert_eq!(a < b, c);
} }

View File

@ -1,3 +1,3 @@
function main() -> u64 { function main() {
return 18446744073709551615 let a: u64 = 18446744073709551615;
} }

View File

@ -0,0 +1,3 @@
function main() {
let a: u64 = 18446744073709551616;
}

View File

@ -1,3 +1,3 @@
function main() -> u64 { function main() {
return 0 let a: u32 = 0;
} }

View File

@ -0,0 +1,3 @@
function main() {
let a: u32 = -1;
}

View File

@ -1,49 +1,33 @@
use crate::{ use crate::{
boolean::{output_expected_boolean, output_false, output_true}, assert_satisfied,
get_error, expect_synthesis_error,
get_output, generate_main_inputs,
integers::{fail_integer, IntegerTester}, integers::{expect_fail, IntegerTester},
parse_program, parse_program,
EdwardsConstrainedValue,
EdwardsTestCompiler,
}; };
use leo_compiler::{ConstrainedValue, Integer};
use leo_inputs::types::{IntegerType, U64Type}; use leo_inputs::types::{IntegerType, U64Type};
use leo_types::InputValue; use leo_types::InputValue;
use snarkos_curves::edwards_bls12::Fq;
use snarkos_models::gadgets::{
r1cs::TestConstraintSystem,
utilities::{alloc::AllocGadget, uint::UInt64},
};
fn output_expected_allocated(program: EdwardsTestCompiler, expected: UInt64) {
let output = get_output(program);
match output {
EdwardsConstrainedValue::Return(vec) => match vec.as_slice() {
[ConstrainedValue::Integer(Integer::U64(actual))] => assert_eq!(*actual, expected),
_ => panic!("program output unknown return value"),
},
_ => panic!("program output unknown return value"),
}
}
test_uint!(TestU64, u64, IntegerType::U64Type(U64Type {}), UInt64); test_uint!(TestU64, u64, IntegerType::U64Type(U64Type {}), UInt64);
#[test] #[test]
fn test_u64_min() { fn test_u64_min() {
TestU64::test_min(std::u64::MIN); TestU64::test_min();
}
#[test]
fn test_u64_min_fail() {
TestU64::test_min_fail();
} }
#[test] #[test]
fn test_u64_max() { fn test_u64_max() {
TestU64::test_max(std::u64::MAX); TestU64::test_max();
} }
#[test] #[test]
fn test_u64_input() { fn test_u64_max_fail() {
TestU64::test_input(); TestU64::test_max_fail();
} }
#[test] #[test]
@ -67,7 +51,6 @@ fn test_u64_div() {
} }
#[test] #[test]
#[ignore] // this test takes ~7 mins
fn test_u64_pow() { fn test_u64_pow() {
TestU64::test_pow(); TestU64::test_pow();
} }

View File

@ -1,3 +1,3 @@
function main(a: u64, b: u64) -> u64 { function main(a: u64, b: u64, c: u64) {
return a * b assert_eq!(a * b, c);
} }

View File

@ -1,3 +0,0 @@
function main() -> u64 {
return 1
}

View File

@ -1,3 +1,3 @@
function main(a: u64, b: u64) -> u64 { function main(a: u64, b: u64, c: u64) {
return a ** b assert_eq!(a ** b, c);
} }

View File

@ -1,3 +1,3 @@
function main(a: u64, b: u64) -> u64 { function main(a: u64, b: u64, c: u64) {
return a - b assert_eq!(a - b, c);
} }

View File

@ -1,3 +1,5 @@
function main(b: bool, x: u64, y: u64) -> u64 { function main(s: bool, a: u64, b: u64, c: u64) {
return if b ? x : y let r = if s ? a : b;
assert_eq!(r, c);
} }

View File

@ -1,3 +0,0 @@
function main() -> u64 {
return 0
}

View File

@ -1,3 +1,3 @@
function main(a: u8, b: u8) -> u8 { function main(a: u8, b: u8, c: u8) {
return a + b assert_eq!(a + b, c);
} }

View File

@ -1,3 +1,3 @@
function main(a: u8, b: u8) -> u8 { function main(a: u8, b: u8, c: u8) {
return a / b assert_eq!(a / b, c);
} }

View File

@ -1,3 +1,3 @@
function main(a: u8, b: u8) -> bool { function main(a: u8, b: u8, c: bool) {
return a == b assert_eq!(a == b, c);
} }

View File

@ -1,3 +1,3 @@
function main(a: u8, b: u8) -> bool { function main(a: u8, b: u8, c: bool) {
return a >= b assert_eq!(a >= b, c);
} }

View File

@ -1,3 +1,3 @@
function main(a: u8, b: u8) -> bool { function main(a: u8, b: u8, c: bool) {
return a > b assert_eq!(a > b, c);
} }

View File

@ -1,3 +1,3 @@
function main(x: u8) -> u8 { function main(a: u8, b: u8) {
return x assert_eq!(a, b);
} }

View File

@ -1,3 +1,3 @@
function main(a: u8, b: u8) -> bool { function main(a: u8, b: u8, c: bool) {
return a <= b assert_eq!(a <= b, c);
} }

View File

@ -1,3 +1,3 @@
function main(a: u8, b: u8) -> bool { function main(a: u8, b: u8, c: bool) {
return a < b assert_eq!(a < b, c);
} }

View File

@ -1,3 +1,3 @@
function main() -> u8 { function main() {
return 255 let a: u8 = 255;
} }

View File

@ -0,0 +1,3 @@
function main() {
let a: u8 = 256;
}

View File

@ -1,3 +1,3 @@
function main() -> u8 { function main() {
return 0 let a: u32 = 0;
} }

View File

@ -0,0 +1,3 @@
function main() {
let a: u32 = -1;
}

View File

@ -1,49 +1,33 @@
use crate::{ use crate::{
boolean::{output_expected_boolean, output_false, output_true}, assert_satisfied,
get_error, expect_synthesis_error,
get_output, generate_main_inputs,
integers::{fail_integer, IntegerTester}, integers::{expect_fail, IntegerTester},
parse_program, parse_program,
EdwardsConstrainedValue,
EdwardsTestCompiler,
}; };
use leo_compiler::{ConstrainedValue, Integer};
use leo_inputs::types::{IntegerType, U8Type}; use leo_inputs::types::{IntegerType, U8Type};
use leo_types::InputValue; use leo_types::InputValue;
use snarkos_curves::edwards_bls12::Fq;
use snarkos_models::gadgets::{
r1cs::TestConstraintSystem,
utilities::{alloc::AllocGadget, uint::UInt8},
};
fn output_expected_allocated(program: EdwardsTestCompiler, expected: UInt8) {
let output = get_output(program);
match output {
EdwardsConstrainedValue::Return(vec) => match vec.as_slice() {
[ConstrainedValue::Integer(Integer::U8(actual))] => assert_eq!(*actual, expected),
_ => panic!("program output unknown return value"),
},
_ => panic!("program output unknown return value"),
}
}
test_uint!(TestU8, u8, IntegerType::U8Type(U8Type {}), UInt8); test_uint!(TestU8, u8, IntegerType::U8Type(U8Type {}), UInt8);
#[test] #[test]
fn test_u8_min() { fn test_u8_min() {
TestU8::test_min(std::u8::MIN); TestU8::test_min();
}
#[test]
fn test_u8_min_fail() {
TestU8::test_min_fail();
} }
#[test] #[test]
fn test_u8_max() { fn test_u8_max() {
TestU8::test_max(std::u8::MAX); TestU8::test_max();
} }
#[test] #[test]
fn test_u8_input() { fn test_u8_max_fail() {
TestU8::test_input(); TestU8::test_max_fail();
} }
#[test] #[test]

View File

@ -1,3 +1,3 @@
function main(a: u8, b: u8) -> u8 { function main(a: u8, b: u8, c: u8) {
return a * b assert_eq!(a * b, c);
} }

View File

@ -1,3 +0,0 @@
function main() -> u8 {
return 1
}

View File

@ -1,3 +1,3 @@
function main(a: u8, b: u8) -> u8 { function main(a: u8, b: u8, c: u8) {
return a ** b assert_eq!(a ** b, c);
} }

View File

@ -1,3 +1,3 @@
function main(a: u8, b: u8) -> u8 { function main(a: u8, b: u8, c: u8) {
return a - b assert_eq!(a - b, c);
} }

View File

@ -1,3 +1,5 @@
function main(b: bool, x: u8, y: u8) -> u8 { function main(s: bool, a: u8, b: u8, c: u8) {
return if b ? x : y let r = if s ? a : b;
assert_eq!(r, c);
} }

View File

@ -1,3 +0,0 @@
function main() -> u8 {
return 0
}

View File

@ -2,387 +2,420 @@ macro_rules! test_uint {
($name: ident, $type_: ty, $integer_type: expr, $gadget: ty) => { ($name: ident, $type_: ty, $integer_type: expr, $gadget: ty) => {
pub struct $name {} pub struct $name {}
impl $name { impl IntegerTester for $name {
fn test_min(min: $type_) { fn test_min() {
let min_allocated = <$gadget>::constant(min);
let bytes = include_bytes!("min.leo"); let bytes = include_bytes!("min.leo");
let program = parse_program(bytes).unwrap(); let program = parse_program(bytes).unwrap();
output_expected_allocated(program, min_allocated); assert_satisfied(program);
} }
fn test_max(max: $type_) { fn test_min_fail() {
let max_allocated = <$gadget>::constant(max); let bytes = include_bytes!("min_fail.leo");
let program = parse_program(bytes).unwrap();
expect_fail(program);
}
fn test_max() {
let bytes = include_bytes!("max.leo"); let bytes = include_bytes!("max.leo");
let program = parse_program(bytes).unwrap(); let program = parse_program(bytes).unwrap();
output_expected_allocated(program, max_allocated); assert_satisfied(program);
} }
}
impl IntegerTester for $name { fn test_max_fail() {
fn test_input() { let bytes = include_bytes!("max_fail.leo");
// valid input let program = parse_program(bytes).unwrap();
let num: $type_ = rand::random();
let expected = <$gadget>::constant(num);
let bytes = include_bytes!("input.leo"); expect_fail(program);
let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![Some(InputValue::Integer($integer_type, num.to_string()))]);
output_expected_allocated(program, expected);
// invalid input
let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![Some(InputValue::Boolean(true))]);
fail_integer(program);
// None input
let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![None]);
fail_integer(program);
} }
fn test_add() { fn test_add() {
for _ in 0..10 { for _ in 0..10 {
let r1: $type_ = rand::random(); let a: $type_ = rand::random();
let r2: $type_ = rand::random(); let b: $type_ = rand::random();
let sum = r1.wrapping_add(r2); let c = match a.checked_add(b) {
Some(valid) => valid,
let cs = TestConstraintSystem::<Fq>::new(); None => continue,
let sum_allocated = <$gadget>::alloc(cs, || Ok(sum)).unwrap(); };
let bytes = include_bytes!("add.leo"); let bytes = include_bytes!("add.leo");
let mut program = parse_program(bytes).unwrap(); let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![ let main_inputs = generate_main_inputs(vec![
Some(InputValue::Integer($integer_type, r1.to_string())), ("a", Some(InputValue::Integer($integer_type, a.to_string()))),
Some(InputValue::Integer($integer_type, r2.to_string())), ("b", Some(InputValue::Integer($integer_type, b.to_string()))),
("c", Some(InputValue::Integer($integer_type, c.to_string()))),
]); ]);
output_expected_allocated(program, sum_allocated); program.set_main_inputs(main_inputs);
assert_satisfied(program);
} }
} }
fn test_sub() { fn test_sub() {
for _ in 0..10 { for _ in 0..10 {
let r1: $type_ = rand::random(); let a: $type_ = rand::random();
let r2: $type_ = rand::random(); let b: $type_ = rand::random();
let difference = match r1.checked_sub(r2) { let c = match a.checked_sub(b) {
Some(valid) => valid, Some(valid) => valid,
None => continue, None => continue,
}; };
let cs = TestConstraintSystem::<Fq>::new();
let difference_allocated = <$gadget>::alloc(cs, || Ok(difference)).unwrap();
let bytes = include_bytes!("sub.leo"); let bytes = include_bytes!("sub.leo");
let mut program = parse_program(bytes).unwrap(); let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![ let main_inputs = generate_main_inputs(vec![
Some(InputValue::Integer($integer_type, r1.to_string())), ("a", Some(InputValue::Integer($integer_type, a.to_string()))),
Some(InputValue::Integer($integer_type, r2.to_string())), ("b", Some(InputValue::Integer($integer_type, b.to_string()))),
("c", Some(InputValue::Integer($integer_type, c.to_string()))),
]); ]);
output_expected_allocated(program, difference_allocated); program.set_main_inputs(main_inputs);
assert_satisfied(program);
} }
} }
fn test_mul() { fn test_mul() {
for _ in 0..10 { for _ in 0..10 {
let r1: $type_ = rand::random(); let a: $type_ = rand::random();
let r2: $type_ = rand::random(); let b: $type_ = rand::random();
let product = r1.wrapping_mul(r2); let c = match a.checked_mul(b) {
Some(valid) => valid,
let cs = TestConstraintSystem::<Fq>::new(); None => continue,
let product_allocated = <$gadget>::alloc(cs, || Ok(product)).unwrap(); };
let bytes = include_bytes!("mul.leo"); let bytes = include_bytes!("mul.leo");
let mut program = parse_program(bytes).unwrap(); let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![ let main_inputs = generate_main_inputs(vec![
Some(InputValue::Integer($integer_type, r1.to_string())), ("a", Some(InputValue::Integer($integer_type, a.to_string()))),
Some(InputValue::Integer($integer_type, r2.to_string())), ("b", Some(InputValue::Integer($integer_type, b.to_string()))),
("c", Some(InputValue::Integer($integer_type, c.to_string()))),
]); ]);
output_expected_allocated(program, product_allocated); program.set_main_inputs(main_inputs);
assert_satisfied(program);
} }
} }
fn test_div() { fn test_div() {
// for _ in 0..10 {// these loops take an excessive amount of time for _ in 0..10 {
let r1: $type_ = rand::random(); let a: $type_ = rand::random();
let r2: $type_ = rand::random(); let b: $type_ = rand::random();
let bytes = include_bytes!("div.leo"); let c = match a.checked_div(b) {
let mut program = parse_program(bytes).unwrap(); Some(valid) => valid,
None => continue,
};
program.set_inputs(vec![ let bytes = include_bytes!("div.leo");
Some(InputValue::Integer($integer_type, r1.to_string())), let mut program = parse_program(bytes).unwrap();
Some(InputValue::Integer($integer_type, r2.to_string())),
]);
// expect an error when dividing by zero let main_inputs = generate_main_inputs(vec![
if r2 == 0 { ("a", Some(InputValue::Integer($integer_type, a.to_string()))),
let _err = get_error(program); ("b", Some(InputValue::Integer($integer_type, b.to_string()))),
} else { ("c", Some(InputValue::Integer($integer_type, c.to_string()))),
let cs = TestConstraintSystem::<Fq>::new(); ]);
let quotient = r1.wrapping_div(r2);
let quotient_allocated = <$gadget>::alloc(cs, || Ok(quotient)).unwrap();
output_expected_allocated(program, quotient_allocated); program.set_main_inputs(main_inputs);
assert_satisfied(program);
} }
// }
} }
fn test_pow() { fn test_pow() {
// for _ in 0..10 {// these loops take an excessive amount of time for _ in 0..10 {
let r1: $type_ = rand::random(); let a: $type_ = rand::random();
let r2: $type_ = rand::random(); let b: $type_ = rand::random();
let r2 = r2 as u32; // we cast to u32 here because of rust pow() requirements
let result = r1.wrapping_pow(r2); // rust specific conversion see https://doc.rust-lang.org/std/primitive.u8.html#method.checked_pow
let c = match a.checked_pow(b as u32) {
Some(valid) => valid,
None => continue,
};
let cs = TestConstraintSystem::<Fq>::new(); let bytes = include_bytes!("pow.leo");
let result_allocated = <$gadget>::alloc(cs, || Ok(result)).unwrap(); let mut program = parse_program(bytes).unwrap();
let bytes = include_bytes!("pow.leo"); let main_inputs = generate_main_inputs(vec![
let mut program = parse_program(bytes).unwrap(); ("a", Some(InputValue::Integer($integer_type, a.to_string()))),
("b", Some(InputValue::Integer($integer_type, b.to_string()))),
("c", Some(InputValue::Integer($integer_type, c.to_string()))),
]);
program.set_inputs(vec![ program.set_main_inputs(main_inputs);
Some(InputValue::Integer($integer_type, r1.to_string())),
Some(InputValue::Integer($integer_type, r2.to_string())),
]);
output_expected_allocated(program, result_allocated); assert_satisfied(program);
// } }
} }
fn test_eq() { fn test_eq() {
for _ in 0..10 { for _ in 0..10 {
let r1: $type_ = rand::random(); let a: $type_ = rand::random();
let b: $type_ = rand::random();
// test equal // test equal
let bytes = include_bytes!("eq.leo"); let bytes = include_bytes!("eq.leo");
let mut program = parse_program(bytes).unwrap(); let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![ let main_inputs = generate_main_inputs(vec![
Some(InputValue::Integer($integer_type, r1.to_string())), ("a", Some(InputValue::Integer($integer_type, a.to_string()))),
Some(InputValue::Integer($integer_type, r1.to_string())), ("b", Some(InputValue::Integer($integer_type, a.to_string()))),
("c", Some(InputValue::Boolean(true))),
]); ]);
output_true(program); program.set_main_inputs(main_inputs);
assert_satisfied(program);
// test not equal // test not equal
let r2: $type_ = rand::random();
let result = r1.eq(&r2); let c = a.eq(&b);
let mut program = parse_program(bytes).unwrap(); let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![ let main_inputs = generate_main_inputs(vec![
Some(InputValue::Integer($integer_type, r1.to_string())), ("a", Some(InputValue::Integer($integer_type, a.to_string()))),
Some(InputValue::Integer($integer_type, r2.to_string())), ("b", Some(InputValue::Integer($integer_type, b.to_string()))),
("c", Some(InputValue::Boolean(c))),
]); ]);
output_expected_boolean(program, result); program.set_main_inputs(main_inputs);
assert_satisfied(program);
} }
} }
fn test_ge() { fn test_ge() {
for _ in 0..10 { for _ in 0..10 {
let r1: $type_ = rand::random(); let a: $type_ = rand::random();
let b: $type_ = rand::random();
// test equal // test equal
let bytes = include_bytes!("ge.leo"); let bytes = include_bytes!("ge.leo");
let mut program = parse_program(bytes).unwrap(); let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![ let main_inputs = generate_main_inputs(vec![
Some(InputValue::Integer($integer_type, r1.to_string())), ("a", Some(InputValue::Integer($integer_type, a.to_string()))),
Some(InputValue::Integer($integer_type, r1.to_string())), ("b", Some(InputValue::Integer($integer_type, a.to_string()))),
("c", Some(InputValue::Boolean(true))),
]); ]);
output_true(program); program.set_main_inputs(main_inputs);
// test not equal assert_satisfied(program);
let r2: $type_ = rand::random();
let result = r1.ge(&r2); // test greater or equal
let c = a.ge(&b);
let mut program = parse_program(bytes).unwrap(); let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![ let main_inputs = generate_main_inputs(vec![
Some(InputValue::Integer($integer_type, r1.to_string())), ("a", Some(InputValue::Integer($integer_type, a.to_string()))),
Some(InputValue::Integer($integer_type, r2.to_string())), ("b", Some(InputValue::Integer($integer_type, b.to_string()))),
("c", Some(InputValue::Boolean(c))),
]); ]);
output_expected_boolean(program, result); program.set_main_inputs(main_inputs);
assert_satisfied(program);
} }
} }
fn test_gt() { fn test_gt() {
for _ in 0..10 { for _ in 0..10 {
let r1: $type_ = rand::random(); let a: $type_ = rand::random();
let b: $type_ = rand::random();
// test equal // test equal
let bytes = include_bytes!("gt.leo"); let bytes = include_bytes!("gt.leo");
let mut program = parse_program(bytes).unwrap(); let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![ let main_inputs = generate_main_inputs(vec![
Some(InputValue::Integer($integer_type, r1.to_string())), ("a", Some(InputValue::Integer($integer_type, a.to_string()))),
Some(InputValue::Integer($integer_type, r1.to_string())), ("b", Some(InputValue::Integer($integer_type, a.to_string()))),
("c", Some(InputValue::Boolean(false))),
]); ]);
output_false(program); program.set_main_inputs(main_inputs);
// test not equal assert_satisfied(program);
let r2: $type_ = rand::random();
let result = r1.gt(&r2); // test greater than
let c = a.gt(&b);
let mut program = parse_program(bytes).unwrap(); let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![ let main_inputs = generate_main_inputs(vec![
Some(InputValue::Integer($integer_type, r1.to_string())), ("a", Some(InputValue::Integer($integer_type, a.to_string()))),
Some(InputValue::Integer($integer_type, r2.to_string())), ("b", Some(InputValue::Integer($integer_type, b.to_string()))),
("c", Some(InputValue::Boolean(c))),
]); ]);
output_expected_boolean(program, result); program.set_main_inputs(main_inputs);
assert_satisfied(program);
} }
} }
fn test_le() { fn test_le() {
for _ in 0..10 { for _ in 0..10 {
let r1: $type_ = rand::random(); let a: $type_ = rand::random();
let b: $type_ = rand::random();
// test equal // test equal
let bytes = include_bytes!("le.leo"); let bytes = include_bytes!("le.leo");
let mut program = parse_program(bytes).unwrap(); let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![ let main_inputs = generate_main_inputs(vec![
Some(InputValue::Integer($integer_type, r1.to_string())), ("a", Some(InputValue::Integer($integer_type, a.to_string()))),
Some(InputValue::Integer($integer_type, r1.to_string())), ("b", Some(InputValue::Integer($integer_type, a.to_string()))),
("c", Some(InputValue::Boolean(true))),
]); ]);
output_true(program); program.set_main_inputs(main_inputs);
// test not equal assert_satisfied(program);
let r2: $type_ = rand::random();
let result = r1.le(&r2); // test less or equal
let c = a.le(&b);
let mut program = parse_program(bytes).unwrap(); let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![ let main_inputs = generate_main_inputs(vec![
Some(InputValue::Integer($integer_type, r1.to_string())), ("a", Some(InputValue::Integer($integer_type, a.to_string()))),
Some(InputValue::Integer($integer_type, r2.to_string())), ("b", Some(InputValue::Integer($integer_type, b.to_string()))),
("c", Some(InputValue::Boolean(c))),
]); ]);
output_expected_boolean(program, result); program.set_main_inputs(main_inputs);
assert_satisfied(program);
} }
} }
fn test_lt() { fn test_lt() {
for _ in 0..10 { for _ in 0..10 {
let r1: $type_ = rand::random(); let a: $type_ = rand::random();
let b: $type_ = rand::random();
// test equal // test equal
let bytes = include_bytes!("lt.leo"); let bytes = include_bytes!("lt.leo");
let mut program = parse_program(bytes).unwrap(); let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![ let main_inputs = generate_main_inputs(vec![
Some(InputValue::Integer($integer_type, r1.to_string())), ("a", Some(InputValue::Integer($integer_type, a.to_string()))),
Some(InputValue::Integer($integer_type, r1.to_string())), ("b", Some(InputValue::Integer($integer_type, a.to_string()))),
("c", Some(InputValue::Boolean(false))),
]); ]);
output_false(program);
// test not equal program.set_main_inputs(main_inputs);
let r2: $type_ = rand::random();
let result = r1.lt(&r2); assert_satisfied(program);
// test less or equal
let c = a.lt(&b);
let mut program = parse_program(bytes).unwrap(); let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![ let main_inputs = generate_main_inputs(vec![
Some(InputValue::Integer($integer_type, r1.to_string())), ("a", Some(InputValue::Integer($integer_type, a.to_string()))),
Some(InputValue::Integer($integer_type, r2.to_string())), ("b", Some(InputValue::Integer($integer_type, b.to_string()))),
("c", Some(InputValue::Boolean(c))),
]); ]);
output_expected_boolean(program, result); program.set_main_inputs(main_inputs);
assert_satisfied(program);
} }
} }
fn test_assert_eq() { fn test_assert_eq() {
for _ in 0..10 { for _ in 0..10 {
let r1: $type_ = rand::random(); let a: $type_ = rand::random();
// test equal // test equal
let bytes = include_bytes!("assert_eq.leo"); let bytes = include_bytes!("assert_eq.leo");
let mut program = parse_program(bytes).unwrap(); let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![ let main_inputs = generate_main_inputs(vec![
Some(InputValue::Integer($integer_type, r1.to_string())), ("a", Some(InputValue::Integer($integer_type, a.to_string()))),
Some(InputValue::Integer($integer_type, r1.to_string())), ("b", Some(InputValue::Integer($integer_type, a.to_string()))),
]); ]);
let _ = get_output(program); program.set_main_inputs(main_inputs);
assert_satisfied(program);
// test not equal // test not equal
let r2: $type_ = rand::random(); let b: $type_ = rand::random();
if r1 == r2 { if a == b {
continue; continue;
} }
let mut program = parse_program(bytes).unwrap(); let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![ let main_inputs = generate_main_inputs(vec![
Some(InputValue::Integer($integer_type, r1.to_string())), ("a", Some(InputValue::Integer($integer_type, a.to_string()))),
Some(InputValue::Integer($integer_type, r2.to_string())), ("b", Some(InputValue::Integer($integer_type, b.to_string()))),
]); ]);
let mut cs = TestConstraintSystem::<Fq>::new(); program.set_main_inputs(main_inputs);
let _ = program.compile_constraints(&mut cs).unwrap();
assert!(!cs.is_satisfied()); expect_synthesis_error(program);
} }
} }
fn test_ternary() { fn test_ternary() {
let r1: $type_ = rand::random(); let a: $type_ = rand::random();
let r2: $type_ = rand::random(); let b: $type_ = rand::random();
let g1 = <$gadget>::constant(r1);
let g2 = <$gadget>::constant(r2);
let bytes = include_bytes!("ternary.leo"); let bytes = include_bytes!("ternary.leo");
let mut program_1 = parse_program(bytes).unwrap(); let mut program = parse_program(bytes).unwrap();
let mut program_2 = program_1.clone();
// true -> field 1 // true -> field 1
program_1.set_inputs(vec![ let main_inputs = generate_main_inputs(vec![
Some(InputValue::Boolean(true)), ("s", Some(InputValue::Boolean(true))),
Some(InputValue::Integer($integer_type, r1.to_string())), ("a", Some(InputValue::Integer($integer_type, a.to_string()))),
Some(InputValue::Integer($integer_type, r2.to_string())), ("b", Some(InputValue::Integer($integer_type, b.to_string()))),
("c", Some(InputValue::Integer($integer_type, a.to_string()))),
]); ]);
output_expected_allocated(program_1, g1); program.set_main_inputs(main_inputs);
assert_satisfied(program);
// false -> field 2 // false -> field 2
program_2.set_inputs(vec![ let mut program = parse_program(bytes).unwrap();
Some(InputValue::Boolean(false)),
Some(InputValue::Integer($integer_type, r1.to_string())), let main_inputs = generate_main_inputs(vec![
Some(InputValue::Integer($integer_type, r2.to_string())), ("s", Some(InputValue::Boolean(false))),
("a", Some(InputValue::Integer($integer_type, a.to_string()))),
("b", Some(InputValue::Integer($integer_type, b.to_string()))),
("c", Some(InputValue::Integer($integer_type, b.to_string()))),
]); ]);
output_expected_allocated(program_2, g2); program.set_main_inputs(main_inputs);
assert_satisfied(program);
} }
} }
}; };

View File

@ -7,7 +7,7 @@ pub mod function;
pub mod group; pub mod group;
pub mod import; pub mod import;
pub mod inputs; pub mod inputs;
// pub mod integers; pub mod integers;
// pub mod macros; // pub mod macros;
// pub mod mutability; // pub mod mutability;
// pub mod statements; // pub mod statements;