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))),
("c", Some(InputValue::Field(c_string))),
]);
program.set_main_inputs(main_inputs);
assert_satisfied(program)

View File

@ -1,23 +1,32 @@
use crate::{get_error, EdwardsTestCompiler};
use leo_compiler::errors::{CompilerError, FunctionError, IntegerError};
use crate::{expect_compiler_error, EdwardsTestCompiler};
use leo_compiler::errors::{CompilerError, ExpressionError, FunctionError, IntegerError, StatementError, ValueError};
pub trait IntegerTester {
/// Tests use of the integer in a function input
fn test_input();
/// Tests defining the smalled value that can be represented by the integer type
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();
/// Tests a wrapping subtraction
/// Tests a non-wrapping subtraction
fn test_sub();
/// Tests a wrapping multiplication
/// Tests a non-wrapping multiplication
fn test_mul();
/// Tests a non-wrapping division
fn test_div();
/// Tests a wrapping exponentiation
/// Tests a non-wrapping exponentiation
fn test_pow();
/// Tests == evaluation
@ -42,9 +51,11 @@ pub trait IntegerTester {
fn test_ternary();
}
pub(crate) fn fail_integer(program: EdwardsTestCompiler) {
match get_error(program) {
CompilerError::FunctionError(FunctionError::IntegerError(IntegerError::Error(_string))) => {}
error => panic!("Expected invalid boolean error, got {}", error),
pub(crate) fn expect_fail(program: EdwardsTestCompiler) {
match expect_compiler_error(program) {
CompilerError::FunctionError(FunctionError::StatementError(StatementError::ExpressionError(
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 u8;
pub mod i128;
pub mod i16;
pub mod i32;
pub mod i64;
pub mod i8;
// pub mod i128;
// pub mod i16;
// pub mod i32;
// pub mod i64;
// pub mod i8;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,3 +1,3 @@
function main() -> u128 {
return 340282366920938463463374607431768211455
function main() {
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 {
return 0
function main() {
let a: u32 = 0;
}

View File

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

View File

@ -1,48 +1,33 @@
use crate::{
boolean::{output_expected_boolean, output_false, output_true},
get_error,
get_output,
integers::{fail_integer, IntegerTester},
assert_satisfied,
expect_synthesis_error,
generate_main_inputs,
integers::{expect_fail, IntegerTester},
parse_program,
EdwardsConstrainedValue,
EdwardsTestCompiler,
};
use leo_compiler::{ConstrainedValue, Integer};
use leo_inputs::types::{IntegerType, U128Type};
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]
fn test_u128_min() {
TestU128::test_min(std::u128::MIN);
TestU128::test_min();
}
#[test]
fn test_u128_min_fail() {
TestU128::test_min_fail();
}
#[test]
fn test_u128_max() {
TestU128::test_max(std::u128::MAX);
TestU128::test_max();
}
#[test]
fn test_u128_input() {
TestU128::test_input();
fn test_u128_max_fail() {
TestU128::test_max_fail();
}
#[test]
@ -55,18 +40,17 @@ fn test_u128_sub() {
TestU128::test_sub();
}
#[test] // this test take ~1 min
#[test]
fn test_u128_mul() {
TestU128::test_mul();
}
#[test] // this test takes ~30 sec
#[test]
fn test_u128_div() {
TestU128::test_div();
}
#[test]
#[ignore] // this test takes ~10 mins
fn test_u128_pow() {
TestU128::test_pow();
}

View File

@ -1,3 +1,3 @@
function main(a: u128, b: u128) -> u128 {
return a * b
function main(a: u128, b: u128, c: u128) {
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 {
return a ** b
function main(a: u128, b: u128, c: u128) {
assert_eq!(a ** b, c);
}

View File

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

View File

@ -1,3 +1,5 @@
function main(b: bool, x: u128, y: u128) -> u128 {
return if b ? x : y
function main(s: bool, a: u128, b: u128, c: u128) {
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 {
return a + b
function main(a: u16, b: u16, c: u16) {
assert_eq!(a + b, c);
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,3 +1,3 @@
function main() -> u16 {
return 65535
function main() {
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 {
return 0
function main() {
let a: u32 = 0;
}

View File

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

View File

@ -1,49 +1,33 @@
use crate::{
boolean::{output_expected_boolean, output_false, output_true},
get_error,
get_output,
integers::{fail_integer, IntegerTester},
assert_satisfied,
expect_synthesis_error,
generate_main_inputs,
integers::{expect_fail, IntegerTester},
parse_program,
EdwardsConstrainedValue,
EdwardsTestCompiler,
};
use leo_compiler::{ConstrainedValue, Integer};
use leo_inputs::types::{IntegerType, U16Type};
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]
fn test_u16_min() {
TestU16::test_min(std::u16::MIN);
TestU16::test_min();
}
#[test]
fn test_u16_min_fail() {
TestU16::test_min_fail();
}
#[test]
fn test_u16_max() {
TestU16::test_max(std::u16::MAX);
TestU16::test_max();
}
#[test]
fn test_u16_input() {
TestU16::test_input();
fn test_u16_max_fail() {
TestU16::test_max_fail();
}
#[test]

View File

@ -1,3 +1,3 @@
function main(a: u16, b: u16) -> u16 {
return a * b
function main(a: u16, b: u16, c: u16) {
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 {
return a ** b
function main(a: u16, b: u16, c: u16) {
assert_eq!(a ** b, c);
}

View File

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

View File

@ -1,3 +1,5 @@
function main(b: bool, x: u16, y: u16) -> u16 {
return if b ? x : y
function main(s: bool, a: u16, b: u16, c: u16) {
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 {
return a + b
function main(a: u32, b: u32, c: u32) {
assert_eq!(a + b, c);
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,3 +1,3 @@
function main() -> u32 {
return 4294967295
function main() {
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 {
return 0
function main() {
let a: u32 = 0;
}

View File

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

View File

@ -1,66 +1,33 @@
use crate::{
boolean::{output_expected_boolean, output_false, output_true},
get_error,
get_output,
integers::{fail_integer, IntegerTester},
assert_satisfied,
expect_synthesis_error,
generate_main_inputs,
integers::{expect_fail, IntegerTester},
parse_program,
EdwardsConstrainedValue,
EdwardsTestCompiler,
};
use leo_compiler::{ConstrainedValue, Integer};
use leo_inputs::types::{IntegerType, U32Type};
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]
fn test_u32_min() {
TestU32::test_min(std::u32::MIN);
TestU32::test_min();
}
#[test]
fn test_u32_min_fail() {
TestU32::test_min_fail();
}
#[test]
fn test_u32_max() {
TestU32::test_max(std::u32::MAX);
TestU32::test_max();
}
#[test]
fn test_u32_input() {
TestU32::test_input();
fn test_u32_max_fail() {
TestU32::test_max_fail();
}
#[test]

View File

@ -1,3 +1,3 @@
function main(a: u32, b: u32) -> u32 {
return a * b
function main(a: u32, b: u32, c: u32) {
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 {
return a ** b
function main(a: u32, b: u32, c: u32) {
assert_eq!(a ** b, c);
}

View File

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

View File

@ -1,3 +1,5 @@
function main(b: bool, x: u32, y: u32) -> u32 {
return if b ? x : y
function main(s: bool, a: u32, b: u32, c: u32) {
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 {
return a + b
function main(a: u64, b: u64, c: u64) {
assert_eq!(a + b, c);
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,3 +1,3 @@
function main() -> u64 {
return 18446744073709551615
function main() {
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 {
return 0
function main() {
let a: u32 = 0;
}

View File

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

View File

@ -1,49 +1,33 @@
use crate::{
boolean::{output_expected_boolean, output_false, output_true},
get_error,
get_output,
integers::{fail_integer, IntegerTester},
assert_satisfied,
expect_synthesis_error,
generate_main_inputs,
integers::{expect_fail, IntegerTester},
parse_program,
EdwardsConstrainedValue,
EdwardsTestCompiler,
};
use leo_compiler::{ConstrainedValue, Integer};
use leo_inputs::types::{IntegerType, U64Type};
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]
fn test_u64_min() {
TestU64::test_min(std::u64::MIN);
TestU64::test_min();
}
#[test]
fn test_u64_min_fail() {
TestU64::test_min_fail();
}
#[test]
fn test_u64_max() {
TestU64::test_max(std::u64::MAX);
TestU64::test_max();
}
#[test]
fn test_u64_input() {
TestU64::test_input();
fn test_u64_max_fail() {
TestU64::test_max_fail();
}
#[test]
@ -67,7 +51,6 @@ fn test_u64_div() {
}
#[test]
#[ignore] // this test takes ~7 mins
fn test_u64_pow() {
TestU64::test_pow();
}

View File

@ -1,3 +1,3 @@
function main(a: u64, b: u64) -> u64 {
return a * b
function main(a: u64, b: u64, c: u64) {
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 {
return a ** b
function main(a: u64, b: u64, c: u64) {
assert_eq!(a ** b, c);
}

View File

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

View File

@ -1,3 +1,5 @@
function main(b: bool, x: u64, y: u64) -> u64 {
return if b ? x : y
function main(s: bool, a: u64, b: u64, c: u64) {
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 {
return a + b
function main(a: u8, b: u8, c: u8) {
assert_eq!(a + b, c);
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,3 +1,3 @@
function main() -> u8 {
return 255
function main() {
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 {
return 0
function main() {
let a: u32 = 0;
}

View File

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

View File

@ -1,49 +1,33 @@
use crate::{
boolean::{output_expected_boolean, output_false, output_true},
get_error,
get_output,
integers::{fail_integer, IntegerTester},
assert_satisfied,
expect_synthesis_error,
generate_main_inputs,
integers::{expect_fail, IntegerTester},
parse_program,
EdwardsConstrainedValue,
EdwardsTestCompiler,
};
use leo_compiler::{ConstrainedValue, Integer};
use leo_inputs::types::{IntegerType, U8Type};
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]
fn test_u8_min() {
TestU8::test_min(std::u8::MIN);
TestU8::test_min();
}
#[test]
fn test_u8_min_fail() {
TestU8::test_min_fail();
}
#[test]
fn test_u8_max() {
TestU8::test_max(std::u8::MAX);
TestU8::test_max();
}
#[test]
fn test_u8_input() {
TestU8::test_input();
fn test_u8_max_fail() {
TestU8::test_max_fail();
}
#[test]

View File

@ -1,3 +1,3 @@
function main(a: u8, b: u8) -> u8 {
return a * b
function main(a: u8, b: u8, c: u8) {
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 {
return a ** b
function main(a: u8, b: u8, c: u8) {
assert_eq!(a ** b, c);
}

View File

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

View File

@ -1,3 +1,5 @@
function main(b: bool, x: u8, y: u8) -> u8 {
return if b ? x : y
function main(s: bool, a: u8, b: u8, c: u8) {
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) => {
pub struct $name {}
impl $name {
fn test_min(min: $type_) {
let min_allocated = <$gadget>::constant(min);
impl IntegerTester for $name {
fn test_min() {
let bytes = include_bytes!("min.leo");
let program = parse_program(bytes).unwrap();
output_expected_allocated(program, min_allocated);
assert_satisfied(program);
}
fn test_max(max: $type_) {
let max_allocated = <$gadget>::constant(max);
fn test_min_fail() {
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 program = parse_program(bytes).unwrap();
output_expected_allocated(program, max_allocated);
assert_satisfied(program);
}
}
impl IntegerTester for $name {
fn test_input() {
// valid input
let num: $type_ = rand::random();
let expected = <$gadget>::constant(num);
fn test_max_fail() {
let bytes = include_bytes!("max_fail.leo");
let program = parse_program(bytes).unwrap();
let bytes = include_bytes!("input.leo");
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);
expect_fail(program);
}
fn test_add() {
for _ in 0..10 {
let r1: $type_ = rand::random();
let r2: $type_ = rand::random();
let a: $type_ = rand::random();
let b: $type_ = rand::random();
let sum = r1.wrapping_add(r2);
let cs = TestConstraintSystem::<Fq>::new();
let sum_allocated = <$gadget>::alloc(cs, || Ok(sum)).unwrap();
let c = match a.checked_add(b) {
Some(valid) => valid,
None => continue,
};
let bytes = include_bytes!("add.leo");
let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![
Some(InputValue::Integer($integer_type, r1.to_string())),
Some(InputValue::Integer($integer_type, r2.to_string())),
let main_inputs = generate_main_inputs(vec![
("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()))),
]);
output_expected_allocated(program, sum_allocated);
program.set_main_inputs(main_inputs);
assert_satisfied(program);
}
}
fn test_sub() {
for _ in 0..10 {
let r1: $type_ = rand::random();
let r2: $type_ = rand::random();
let a: $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,
None => continue,
};
let cs = TestConstraintSystem::<Fq>::new();
let difference_allocated = <$gadget>::alloc(cs, || Ok(difference)).unwrap();
let bytes = include_bytes!("sub.leo");
let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![
Some(InputValue::Integer($integer_type, r1.to_string())),
Some(InputValue::Integer($integer_type, r2.to_string())),
let main_inputs = generate_main_inputs(vec![
("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()))),
]);
output_expected_allocated(program, difference_allocated);
program.set_main_inputs(main_inputs);
assert_satisfied(program);
}
}
fn test_mul() {
for _ in 0..10 {
let r1: $type_ = rand::random();
let r2: $type_ = rand::random();
let a: $type_ = rand::random();
let b: $type_ = rand::random();
let product = r1.wrapping_mul(r2);
let cs = TestConstraintSystem::<Fq>::new();
let product_allocated = <$gadget>::alloc(cs, || Ok(product)).unwrap();
let c = match a.checked_mul(b) {
Some(valid) => valid,
None => continue,
};
let bytes = include_bytes!("mul.leo");
let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![
Some(InputValue::Integer($integer_type, r1.to_string())),
Some(InputValue::Integer($integer_type, r2.to_string())),
let main_inputs = generate_main_inputs(vec![
("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()))),
]);
output_expected_allocated(program, product_allocated);
program.set_main_inputs(main_inputs);
assert_satisfied(program);
}
}
fn test_div() {
// for _ in 0..10 {// these loops take an excessive amount of time
let r1: $type_ = rand::random();
let r2: $type_ = rand::random();
for _ in 0..10 {
let a: $type_ = rand::random();
let b: $type_ = rand::random();
let bytes = include_bytes!("div.leo");
let mut program = parse_program(bytes).unwrap();
let c = match a.checked_div(b) {
Some(valid) => valid,
None => continue,
};
program.set_inputs(vec![
Some(InputValue::Integer($integer_type, r1.to_string())),
Some(InputValue::Integer($integer_type, r2.to_string())),
]);
let bytes = include_bytes!("div.leo");
let mut program = parse_program(bytes).unwrap();
// expect an error when dividing by zero
if r2 == 0 {
let _err = get_error(program);
} else {
let cs = TestConstraintSystem::<Fq>::new();
let quotient = r1.wrapping_div(r2);
let quotient_allocated = <$gadget>::alloc(cs, || Ok(quotient)).unwrap();
let main_inputs = generate_main_inputs(vec![
("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()))),
]);
output_expected_allocated(program, quotient_allocated);
program.set_main_inputs(main_inputs);
assert_satisfied(program);
}
// }
}
fn test_pow() {
// for _ in 0..10 {// these loops take an excessive amount of time
let r1: $type_ = rand::random();
let r2: $type_ = rand::random();
let r2 = r2 as u32; // we cast to u32 here because of rust pow() requirements
for _ in 0..10 {
let a: $type_ = rand::random();
let b: $type_ = rand::random();
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 result_allocated = <$gadget>::alloc(cs, || Ok(result)).unwrap();
let bytes = include_bytes!("pow.leo");
let mut program = parse_program(bytes).unwrap();
let bytes = include_bytes!("pow.leo");
let mut program = parse_program(bytes).unwrap();
let main_inputs = generate_main_inputs(vec![
("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![
Some(InputValue::Integer($integer_type, r1.to_string())),
Some(InputValue::Integer($integer_type, r2.to_string())),
]);
program.set_main_inputs(main_inputs);
output_expected_allocated(program, result_allocated);
// }
assert_satisfied(program);
}
}
fn test_eq() {
for _ in 0..10 {
let r1: $type_ = rand::random();
let a: $type_ = rand::random();
let b: $type_ = rand::random();
// test equal
let bytes = include_bytes!("eq.leo");
let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![
Some(InputValue::Integer($integer_type, r1.to_string())),
Some(InputValue::Integer($integer_type, r1.to_string())),
let main_inputs = generate_main_inputs(vec![
("a", Some(InputValue::Integer($integer_type, a.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
let r2: $type_ = rand::random();
let result = r1.eq(&r2);
let c = a.eq(&b);
let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![
Some(InputValue::Integer($integer_type, r1.to_string())),
Some(InputValue::Integer($integer_type, r2.to_string())),
let main_inputs = generate_main_inputs(vec![
("a", Some(InputValue::Integer($integer_type, a.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() {
for _ in 0..10 {
let r1: $type_ = rand::random();
let a: $type_ = rand::random();
let b: $type_ = rand::random();
// test equal
let bytes = include_bytes!("ge.leo");
let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![
Some(InputValue::Integer($integer_type, r1.to_string())),
Some(InputValue::Integer($integer_type, r1.to_string())),
let main_inputs = generate_main_inputs(vec![
("a", Some(InputValue::Integer($integer_type, a.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
let r2: $type_ = rand::random();
assert_satisfied(program);
let result = r1.ge(&r2);
// test greater or equal
let c = a.ge(&b);
let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![
Some(InputValue::Integer($integer_type, r1.to_string())),
Some(InputValue::Integer($integer_type, r2.to_string())),
let main_inputs = generate_main_inputs(vec![
("a", Some(InputValue::Integer($integer_type, a.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() {
for _ in 0..10 {
let r1: $type_ = rand::random();
let a: $type_ = rand::random();
let b: $type_ = rand::random();
// test equal
let bytes = include_bytes!("gt.leo");
let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![
Some(InputValue::Integer($integer_type, r1.to_string())),
Some(InputValue::Integer($integer_type, r1.to_string())),
let main_inputs = generate_main_inputs(vec![
("a", Some(InputValue::Integer($integer_type, a.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
let r2: $type_ = rand::random();
assert_satisfied(program);
let result = r1.gt(&r2);
// test greater than
let c = a.gt(&b);
let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![
Some(InputValue::Integer($integer_type, r1.to_string())),
Some(InputValue::Integer($integer_type, r2.to_string())),
let main_inputs = generate_main_inputs(vec![
("a", Some(InputValue::Integer($integer_type, a.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() {
for _ in 0..10 {
let r1: $type_ = rand::random();
let a: $type_ = rand::random();
let b: $type_ = rand::random();
// test equal
let bytes = include_bytes!("le.leo");
let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![
Some(InputValue::Integer($integer_type, r1.to_string())),
Some(InputValue::Integer($integer_type, r1.to_string())),
let main_inputs = generate_main_inputs(vec![
("a", Some(InputValue::Integer($integer_type, a.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
let r2: $type_ = rand::random();
assert_satisfied(program);
let result = r1.le(&r2);
// test less or equal
let c = a.le(&b);
let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![
Some(InputValue::Integer($integer_type, r1.to_string())),
Some(InputValue::Integer($integer_type, r2.to_string())),
let main_inputs = generate_main_inputs(vec![
("a", Some(InputValue::Integer($integer_type, a.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() {
for _ in 0..10 {
let r1: $type_ = rand::random();
let a: $type_ = rand::random();
let b: $type_ = rand::random();
// test equal
let bytes = include_bytes!("lt.leo");
let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![
Some(InputValue::Integer($integer_type, r1.to_string())),
Some(InputValue::Integer($integer_type, r1.to_string())),
let main_inputs = generate_main_inputs(vec![
("a", Some(InputValue::Integer($integer_type, a.to_string()))),
("b", Some(InputValue::Integer($integer_type, a.to_string()))),
("c", Some(InputValue::Boolean(false))),
]);
output_false(program);
// test not equal
let r2: $type_ = rand::random();
program.set_main_inputs(main_inputs);
let result = r1.lt(&r2);
assert_satisfied(program);
// test less or equal
let c = a.lt(&b);
let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![
Some(InputValue::Integer($integer_type, r1.to_string())),
Some(InputValue::Integer($integer_type, r2.to_string())),
let main_inputs = generate_main_inputs(vec![
("a", Some(InputValue::Integer($integer_type, a.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() {
for _ in 0..10 {
let r1: $type_ = rand::random();
let a: $type_ = rand::random();
// test equal
let bytes = include_bytes!("assert_eq.leo");
let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![
Some(InputValue::Integer($integer_type, r1.to_string())),
Some(InputValue::Integer($integer_type, r1.to_string())),
let main_inputs = generate_main_inputs(vec![
("a", Some(InputValue::Integer($integer_type, a.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
let r2: $type_ = rand::random();
let b: $type_ = rand::random();
if r1 == r2 {
if a == b {
continue;
}
let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![
Some(InputValue::Integer($integer_type, r1.to_string())),
Some(InputValue::Integer($integer_type, r2.to_string())),
let main_inputs = generate_main_inputs(vec![
("a", Some(InputValue::Integer($integer_type, a.to_string()))),
("b", Some(InputValue::Integer($integer_type, b.to_string()))),
]);
let mut cs = TestConstraintSystem::<Fq>::new();
let _ = program.compile_constraints(&mut cs).unwrap();
assert!(!cs.is_satisfied());
program.set_main_inputs(main_inputs);
expect_synthesis_error(program);
}
}
fn test_ternary() {
let r1: $type_ = rand::random();
let r2: $type_ = rand::random();
let g1 = <$gadget>::constant(r1);
let g2 = <$gadget>::constant(r2);
let a: $type_ = rand::random();
let b: $type_ = rand::random();
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_inputs(vec![
Some(InputValue::Boolean(true)),
Some(InputValue::Integer($integer_type, r1.to_string())),
Some(InputValue::Integer($integer_type, r2.to_string())),
let main_inputs = generate_main_inputs(vec![
("s", Some(InputValue::Boolean(true))),
("a", Some(InputValue::Integer($integer_type, a.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
program_2.set_inputs(vec![
Some(InputValue::Boolean(false)),
Some(InputValue::Integer($integer_type, r1.to_string())),
Some(InputValue::Integer($integer_type, r2.to_string())),
let mut program = parse_program(bytes).unwrap();
let main_inputs = generate_main_inputs(vec![
("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 import;
pub mod inputs;
// pub mod integers;
pub mod integers;
// pub mod macros;
// pub mod mutability;
// pub mod statements;