2020-06-06 00:09:12 +03:00
|
|
|
macro_rules! test_uint {
|
2020-06-11 03:53:38 +03:00
|
|
|
($name: ident, $_type: ty, $integer_type: expr, $gadget: ty) => {
|
2020-06-06 00:09:12 +03:00
|
|
|
pub struct $name {}
|
|
|
|
|
|
|
|
impl $name {
|
|
|
|
fn test_min(min: $_type) {
|
|
|
|
let min_allocated = <$gadget>::constant(min);
|
|
|
|
|
2020-06-09 05:13:47 +03:00
|
|
|
let bytes = include_bytes!("min.leo");
|
|
|
|
let program = parse_program(bytes).unwrap();
|
2020-06-06 00:09:12 +03:00
|
|
|
|
|
|
|
output_expected_allocated(program, min_allocated);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_max(max: $_type) {
|
|
|
|
let max_allocated = <$gadget>::constant(max);
|
|
|
|
|
2020-06-09 05:13:47 +03:00
|
|
|
let bytes = include_bytes!("max.leo");
|
|
|
|
let program = parse_program(bytes).unwrap();
|
2020-06-06 00:09:12 +03:00
|
|
|
|
|
|
|
output_expected_allocated(program, max_allocated);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IntegerTester for $name {
|
|
|
|
fn test_input() {
|
|
|
|
// valid input
|
|
|
|
let num: $_type = rand::random();
|
|
|
|
let expected = <$gadget>::constant(num);
|
|
|
|
|
2020-06-09 05:13:47 +03:00
|
|
|
let bytes = include_bytes!("input.leo");
|
|
|
|
let mut program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-06-11 03:53:38 +03:00
|
|
|
program.set_inputs(vec![Some(InputValue::Integer($integer_type, num as u128))]);
|
2020-06-06 00:09:12 +03:00
|
|
|
|
|
|
|
output_expected_allocated(program, expected);
|
|
|
|
|
|
|
|
// invalid input
|
2020-06-09 05:13:47 +03:00
|
|
|
let mut program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-06-06 00:09:12 +03:00
|
|
|
program.set_inputs(vec![Some(InputValue::Boolean(true))]);
|
|
|
|
fail_integer(program);
|
|
|
|
|
|
|
|
// None input
|
2020-06-09 05:13:47 +03:00
|
|
|
let mut program = parse_program(bytes).unwrap();
|
2020-06-06 00:09:12 +03:00
|
|
|
program.set_inputs(vec![None]);
|
2020-06-21 01:24:46 +03:00
|
|
|
fail_integer(program);
|
2020-06-06 00:09:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_add() {
|
|
|
|
for _ in 0..10 {
|
|
|
|
let r1: $_type = rand::random();
|
|
|
|
let r2: $_type = rand::random();
|
|
|
|
|
|
|
|
let sum = r1.wrapping_add(r2);
|
|
|
|
|
|
|
|
let cs = TestConstraintSystem::<Fq>::new();
|
|
|
|
let sum_allocated = <$gadget>::alloc(cs, || Ok(sum)).unwrap();
|
|
|
|
|
2020-06-09 05:13:47 +03:00
|
|
|
let bytes = include_bytes!("add.leo");
|
|
|
|
let mut program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-06-06 00:09:12 +03:00
|
|
|
program.set_inputs(vec![
|
2020-06-11 03:53:38 +03:00
|
|
|
Some(InputValue::Integer($integer_type, r1 as u128)),
|
|
|
|
Some(InputValue::Integer($integer_type, r2 as u128)),
|
2020-06-06 00:09:12 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
output_expected_allocated(program, sum_allocated);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_sub() {
|
|
|
|
for _ in 0..10 {
|
|
|
|
let r1: $_type = rand::random();
|
|
|
|
let r2: $_type = rand::random();
|
|
|
|
|
2020-06-06 02:35:50 +03:00
|
|
|
let difference = r1.wrapping_sub(r2);
|
2020-06-06 00:09:12 +03:00
|
|
|
|
|
|
|
let cs = TestConstraintSystem::<Fq>::new();
|
2020-06-06 02:35:50 +03:00
|
|
|
let difference_allocated = <$gadget>::alloc(cs, || Ok(difference)).unwrap();
|
2020-06-06 00:09:12 +03:00
|
|
|
|
2020-06-09 05:13:47 +03:00
|
|
|
let bytes = include_bytes!("sub.leo");
|
|
|
|
let mut program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-06-06 00:09:12 +03:00
|
|
|
program.set_inputs(vec![
|
2020-06-11 03:53:38 +03:00
|
|
|
Some(InputValue::Integer($integer_type, r1 as u128)),
|
|
|
|
Some(InputValue::Integer($integer_type, r2 as u128)),
|
2020-06-06 00:09:12 +03:00
|
|
|
]);
|
|
|
|
|
2020-06-06 02:35:50 +03:00
|
|
|
output_expected_allocated(program, difference_allocated);
|
2020-06-06 00:09:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_mul() {
|
|
|
|
for _ in 0..10 {
|
|
|
|
let r1: $_type = rand::random();
|
|
|
|
let r2: $_type = rand::random();
|
|
|
|
|
2020-06-06 02:35:50 +03:00
|
|
|
let product = r1.wrapping_mul(r2);
|
2020-06-06 00:09:12 +03:00
|
|
|
|
|
|
|
let cs = TestConstraintSystem::<Fq>::new();
|
2020-06-06 02:35:50 +03:00
|
|
|
let product_allocated = <$gadget>::alloc(cs, || Ok(product)).unwrap();
|
2020-06-06 00:09:12 +03:00
|
|
|
|
2020-06-09 05:13:47 +03:00
|
|
|
let bytes = include_bytes!("mul.leo");
|
|
|
|
let mut program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-06-06 00:09:12 +03:00
|
|
|
program.set_inputs(vec![
|
2020-06-11 03:53:38 +03:00
|
|
|
Some(InputValue::Integer($integer_type, r1 as u128)),
|
|
|
|
Some(InputValue::Integer($integer_type, r2 as u128)),
|
2020-06-06 00:09:12 +03:00
|
|
|
]);
|
|
|
|
|
2020-06-06 02:35:50 +03:00
|
|
|
output_expected_allocated(program, product_allocated);
|
2020-06-06 00:09:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_div() {
|
|
|
|
for _ in 0..10 {
|
|
|
|
let r1: $_type = rand::random();
|
|
|
|
let r2: $_type = rand::random();
|
|
|
|
|
2020-06-09 05:13:47 +03:00
|
|
|
let bytes = include_bytes!("div.leo");
|
|
|
|
let mut program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-06-06 00:09:12 +03:00
|
|
|
program.set_inputs(vec![
|
2020-06-11 03:53:38 +03:00
|
|
|
Some(InputValue::Integer($integer_type, r1 as u128)),
|
|
|
|
Some(InputValue::Integer($integer_type, r2 as u128)),
|
2020-06-06 00:09:12 +03:00
|
|
|
]);
|
|
|
|
|
2020-06-14 03:43:59 +03:00
|
|
|
// 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();
|
|
|
|
|
|
|
|
output_expected_allocated(program, quotient_allocated);
|
|
|
|
}
|
2020-06-06 00:09:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_pow() {
|
2020-06-06 02:35:50 +03:00
|
|
|
// 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
|
2020-06-06 00:09:12 +03:00
|
|
|
|
2020-06-06 02:35:50 +03:00
|
|
|
let result = r1.wrapping_pow(r2);
|
2020-06-06 00:09:12 +03:00
|
|
|
|
2020-06-06 02:35:50 +03:00
|
|
|
let cs = TestConstraintSystem::<Fq>::new();
|
|
|
|
let result_allocated = <$gadget>::alloc(cs, || Ok(result)).unwrap();
|
2020-06-06 00:09:12 +03:00
|
|
|
|
2020-06-09 05:13:47 +03:00
|
|
|
let bytes = include_bytes!("pow.leo");
|
|
|
|
let mut program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-06-06 02:35:50 +03:00
|
|
|
program.set_inputs(vec![
|
2020-06-11 03:53:38 +03:00
|
|
|
Some(InputValue::Integer($integer_type, r1 as u128)),
|
|
|
|
Some(InputValue::Integer($integer_type, r2 as u128)),
|
2020-06-06 02:35:50 +03:00
|
|
|
]);
|
2020-06-06 00:09:12 +03:00
|
|
|
|
2020-06-06 02:35:50 +03:00
|
|
|
output_expected_allocated(program, result_allocated);
|
|
|
|
// }
|
2020-06-06 00:09:12 +03:00
|
|
|
}
|
2020-06-06 01:34:06 +03:00
|
|
|
|
|
|
|
fn test_eq() {
|
|
|
|
for _ in 0..10 {
|
|
|
|
let r1: $_type = rand::random();
|
|
|
|
|
|
|
|
// test equal
|
2020-06-09 05:13:47 +03:00
|
|
|
let bytes = include_bytes!("eq.leo");
|
|
|
|
let mut program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-06-06 01:34:06 +03:00
|
|
|
program.set_inputs(vec![
|
2020-06-11 03:53:38 +03:00
|
|
|
Some(InputValue::Integer($integer_type, r1 as u128)),
|
|
|
|
Some(InputValue::Integer($integer_type, r1 as u128)),
|
2020-06-06 01:34:06 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
output_true(program);
|
|
|
|
|
|
|
|
// test not equal
|
|
|
|
let r2: $_type = rand::random();
|
|
|
|
|
|
|
|
let result = r1.eq(&r2);
|
|
|
|
|
2020-06-09 05:13:47 +03:00
|
|
|
let mut program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-06-06 01:34:06 +03:00
|
|
|
program.set_inputs(vec![
|
2020-06-11 03:53:38 +03:00
|
|
|
Some(InputValue::Integer($integer_type, r1 as u128)),
|
|
|
|
Some(InputValue::Integer($integer_type, r2 as u128)),
|
2020-06-06 01:34:06 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
output_expected_boolean(program, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_ge() {
|
|
|
|
for _ in 0..10 {
|
|
|
|
let r1: $_type = rand::random();
|
|
|
|
|
|
|
|
// test equal
|
2020-06-09 05:13:47 +03:00
|
|
|
let bytes = include_bytes!("ge.leo");
|
|
|
|
let mut program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-06-06 01:34:06 +03:00
|
|
|
program.set_inputs(vec![
|
2020-06-11 03:53:38 +03:00
|
|
|
Some(InputValue::Integer($integer_type, r1 as u128)),
|
|
|
|
Some(InputValue::Integer($integer_type, r1 as u128)),
|
2020-06-06 01:34:06 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
output_true(program);
|
|
|
|
|
|
|
|
// test not equal
|
|
|
|
let r2: $_type = rand::random();
|
|
|
|
|
|
|
|
let result = r1.ge(&r2);
|
|
|
|
|
2020-06-09 05:13:47 +03:00
|
|
|
let mut program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-06-06 01:34:06 +03:00
|
|
|
program.set_inputs(vec![
|
2020-06-11 03:53:38 +03:00
|
|
|
Some(InputValue::Integer($integer_type, r1 as u128)),
|
|
|
|
Some(InputValue::Integer($integer_type, r2 as u128)),
|
2020-06-06 01:34:06 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
output_expected_boolean(program, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_gt() {
|
|
|
|
for _ in 0..10 {
|
|
|
|
let r1: $_type = rand::random();
|
|
|
|
|
|
|
|
// test equal
|
2020-06-09 05:13:47 +03:00
|
|
|
let bytes = include_bytes!("gt.leo");
|
|
|
|
let mut program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-06-06 01:34:06 +03:00
|
|
|
program.set_inputs(vec![
|
2020-06-11 03:53:38 +03:00
|
|
|
Some(InputValue::Integer($integer_type, r1 as u128)),
|
|
|
|
Some(InputValue::Integer($integer_type, r1 as u128)),
|
2020-06-06 01:34:06 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
output_false(program);
|
|
|
|
|
|
|
|
// test not equal
|
|
|
|
let r2: $_type = rand::random();
|
|
|
|
|
|
|
|
let result = r1.gt(&r2);
|
|
|
|
|
2020-06-09 05:13:47 +03:00
|
|
|
let mut program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-06-06 01:34:06 +03:00
|
|
|
program.set_inputs(vec![
|
2020-06-11 03:53:38 +03:00
|
|
|
Some(InputValue::Integer($integer_type, r1 as u128)),
|
|
|
|
Some(InputValue::Integer($integer_type, r2 as u128)),
|
2020-06-06 01:34:06 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
output_expected_boolean(program, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_le() {
|
|
|
|
for _ in 0..10 {
|
|
|
|
let r1: $_type = rand::random();
|
|
|
|
|
|
|
|
// test equal
|
2020-06-09 05:13:47 +03:00
|
|
|
let bytes = include_bytes!("le.leo");
|
|
|
|
let mut program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-06-06 01:34:06 +03:00
|
|
|
program.set_inputs(vec![
|
2020-06-11 03:53:38 +03:00
|
|
|
Some(InputValue::Integer($integer_type, r1 as u128)),
|
|
|
|
Some(InputValue::Integer($integer_type, r1 as u128)),
|
2020-06-06 01:34:06 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
output_true(program);
|
|
|
|
|
|
|
|
// test not equal
|
|
|
|
let r2: $_type = rand::random();
|
|
|
|
|
|
|
|
let result = r1.le(&r2);
|
|
|
|
|
2020-06-09 05:13:47 +03:00
|
|
|
let mut program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-06-06 01:34:06 +03:00
|
|
|
program.set_inputs(vec![
|
2020-06-11 03:53:38 +03:00
|
|
|
Some(InputValue::Integer($integer_type, r1 as u128)),
|
|
|
|
Some(InputValue::Integer($integer_type, r2 as u128)),
|
2020-06-06 01:34:06 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
output_expected_boolean(program, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_lt() {
|
|
|
|
for _ in 0..10 {
|
|
|
|
let r1: $_type = rand::random();
|
|
|
|
|
|
|
|
// test equal
|
2020-06-09 05:13:47 +03:00
|
|
|
let bytes = include_bytes!("lt.leo");
|
|
|
|
let mut program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-06-06 01:34:06 +03:00
|
|
|
program.set_inputs(vec![
|
2020-06-11 03:53:38 +03:00
|
|
|
Some(InputValue::Integer($integer_type, r1 as u128)),
|
|
|
|
Some(InputValue::Integer($integer_type, r1 as u128)),
|
2020-06-06 01:34:06 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
output_false(program);
|
|
|
|
|
|
|
|
// test not equal
|
|
|
|
let r2: $_type = rand::random();
|
|
|
|
|
|
|
|
let result = r1.lt(&r2);
|
|
|
|
|
2020-06-09 05:13:47 +03:00
|
|
|
let mut program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-06-06 01:34:06 +03:00
|
|
|
program.set_inputs(vec![
|
2020-06-11 03:53:38 +03:00
|
|
|
Some(InputValue::Integer($integer_type, r1 as u128)),
|
|
|
|
Some(InputValue::Integer($integer_type, r2 as u128)),
|
2020-06-06 01:34:06 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
output_expected_boolean(program, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_assert_eq() {
|
|
|
|
for _ in 0..10 {
|
|
|
|
let r1: $_type = rand::random();
|
|
|
|
|
|
|
|
// test equal
|
2020-06-09 05:13:47 +03:00
|
|
|
let bytes = include_bytes!("assert_eq.leo");
|
|
|
|
let mut program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-06-06 01:34:06 +03:00
|
|
|
program.set_inputs(vec![
|
2020-06-11 03:53:38 +03:00
|
|
|
Some(InputValue::Integer($integer_type, r1 as u128)),
|
|
|
|
Some(InputValue::Integer($integer_type, r1 as u128)),
|
2020-06-06 01:34:06 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
let _ = get_output(program);
|
|
|
|
|
|
|
|
// test not equal
|
|
|
|
let r2: $_type = rand::random();
|
|
|
|
|
|
|
|
if r1 == r2 {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-06-09 05:13:47 +03:00
|
|
|
let mut program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-06-06 01:34:06 +03:00
|
|
|
program.set_inputs(vec![
|
2020-06-11 03:53:38 +03:00
|
|
|
Some(InputValue::Integer($integer_type, r1 as u128)),
|
|
|
|
Some(InputValue::Integer($integer_type, r2 as u128)),
|
2020-06-06 01:34:06 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
let mut cs = TestConstraintSystem::<Fq>::new();
|
|
|
|
let _ = program.compile_constraints(&mut cs).unwrap();
|
|
|
|
assert!(!cs.is_satisfied());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_ternary() {
|
|
|
|
let r1: $_type = rand::random();
|
|
|
|
let r2: $_type = rand::random();
|
|
|
|
|
|
|
|
let g1 = <$gadget>::constant(r1);
|
|
|
|
let g2 = <$gadget>::constant(r2);
|
|
|
|
|
2020-06-09 05:13:47 +03:00
|
|
|
let bytes = include_bytes!("ternary.leo");
|
|
|
|
let mut program_1 = parse_program(bytes).unwrap();
|
|
|
|
|
2020-06-06 01:34:06 +03:00
|
|
|
let mut program_2 = program_1.clone();
|
|
|
|
|
|
|
|
// true -> field 1
|
|
|
|
program_1.set_inputs(vec![
|
|
|
|
Some(InputValue::Boolean(true)),
|
2020-06-11 03:53:38 +03:00
|
|
|
Some(InputValue::Integer($integer_type, r1 as u128)),
|
|
|
|
Some(InputValue::Integer($integer_type, r2 as u128)),
|
2020-06-06 01:34:06 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
output_expected_allocated(program_1, g1);
|
|
|
|
|
|
|
|
// false -> field 2
|
|
|
|
program_2.set_inputs(vec![
|
|
|
|
Some(InputValue::Boolean(false)),
|
2020-06-11 03:53:38 +03:00
|
|
|
Some(InputValue::Integer($integer_type, r1 as u128)),
|
|
|
|
Some(InputValue::Integer($integer_type, r2 as u128)),
|
2020-06-06 01:34:06 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
output_expected_allocated(program_2, g2);
|
|
|
|
}
|
2020-06-06 00:09:12 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|