2020-06-02 04:35:43 +03:00
|
|
|
use crate::{
|
2020-07-30 22:54:34 +03:00
|
|
|
assert_satisfied,
|
|
|
|
expect_synthesis_error,
|
|
|
|
field::field_to_decimal_string,
|
|
|
|
generate_main_inputs,
|
2020-06-09 04:39:10 +03:00
|
|
|
parse_program,
|
2020-07-30 22:54:34 +03:00
|
|
|
parse_program_with_inputs,
|
2020-06-02 04:35:43 +03:00
|
|
|
};
|
2020-06-08 07:26:49 +03:00
|
|
|
use leo_types::InputValue;
|
2020-05-31 03:05:07 +03:00
|
|
|
|
2020-07-30 22:54:34 +03:00
|
|
|
use snarkos_curves::edwards_bls12::EdwardsAffine;
|
2020-06-02 00:20:14 +03:00
|
|
|
|
2020-07-30 22:54:34 +03:00
|
|
|
use rand::{Rng, SeedableRng};
|
|
|
|
use rand_xorshift::XorShiftRng;
|
2020-05-31 01:37:11 +03:00
|
|
|
|
2020-07-30 22:54:34 +03:00
|
|
|
pub fn group_to_decimal_string(g: EdwardsAffine) -> String {
|
|
|
|
let x = field_to_decimal_string(g.x);
|
|
|
|
let y = field_to_decimal_string(g.y);
|
2020-07-07 12:08:48 +03:00
|
|
|
|
2020-07-30 22:54:34 +03:00
|
|
|
format!("({}, {})", x, y)
|
2020-07-07 12:08:48 +03:00
|
|
|
}
|
|
|
|
|
2020-05-31 01:37:11 +03:00
|
|
|
#[test]
|
|
|
|
fn test_zero() {
|
2020-06-09 04:39:10 +03:00
|
|
|
let bytes = include_bytes!("zero.leo");
|
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-07-30 22:54:34 +03:00
|
|
|
assert_satisfied(program);
|
2020-05-31 01:37:11 +03:00
|
|
|
}
|
|
|
|
|
2020-07-07 12:08:48 +03:00
|
|
|
#[test]
|
|
|
|
fn test_one() {
|
|
|
|
let bytes = include_bytes!("one.leo");
|
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-07-30 22:54:34 +03:00
|
|
|
assert_satisfied(program)
|
2020-07-07 12:08:48 +03:00
|
|
|
}
|
|
|
|
|
2020-05-31 03:05:07 +03:00
|
|
|
#[test]
|
|
|
|
fn test_point() {
|
2020-06-09 04:39:10 +03:00
|
|
|
let bytes = include_bytes!("point.leo");
|
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-07-30 22:54:34 +03:00
|
|
|
assert_satisfied(program);
|
2020-05-31 03:05:07 +03:00
|
|
|
}
|
|
|
|
|
2020-06-03 02:06:25 +03:00
|
|
|
#[test]
|
2020-07-30 22:54:34 +03:00
|
|
|
fn test_inputs() {
|
|
|
|
let program_bytes = include_bytes!("input.leo");
|
|
|
|
let input_bytes_pass = include_bytes!("inputs/one_one.in");
|
|
|
|
let input_bytes_fail = include_bytes!("inputs/one_zero.in");
|
|
|
|
|
|
|
|
let program = parse_program_with_inputs(program_bytes, input_bytes_pass).unwrap();
|
2020-06-09 04:39:10 +03:00
|
|
|
|
2020-07-30 22:54:34 +03:00
|
|
|
assert_satisfied(program);
|
2020-06-03 02:06:25 +03:00
|
|
|
|
2020-07-30 22:54:34 +03:00
|
|
|
let program = parse_program_with_inputs(program_bytes, input_bytes_fail).unwrap();
|
2020-06-03 02:06:25 +03:00
|
|
|
|
2020-07-30 22:54:34 +03:00
|
|
|
expect_synthesis_error(program);
|
2020-06-03 02:06:25 +03:00
|
|
|
}
|
|
|
|
|
2020-05-31 01:37:11 +03:00
|
|
|
#[test]
|
|
|
|
fn test_add() {
|
2020-05-31 03:05:07 +03:00
|
|
|
use std::ops::Add;
|
|
|
|
|
2020-07-30 22:54:34 +03:00
|
|
|
let mut rng = XorShiftRng::seed_from_u64(1231275789u64);
|
2020-05-31 03:05:07 +03:00
|
|
|
|
2020-07-30 22:54:34 +03:00
|
|
|
for _ in 0..10 {
|
|
|
|
let a: EdwardsAffine = rng.gen();
|
|
|
|
let b: EdwardsAffine = rng.gen();
|
|
|
|
let c = a.add(&b);
|
2020-05-31 03:05:07 +03:00
|
|
|
|
2020-07-30 22:54:34 +03:00
|
|
|
let a_string = group_to_decimal_string(a);
|
|
|
|
let b_string = group_to_decimal_string(b);
|
|
|
|
let c_string = group_to_decimal_string(c);
|
|
|
|
|
|
|
|
let bytes = include_bytes!("add.leo");
|
|
|
|
let mut program = parse_program(bytes).unwrap();
|
2020-06-09 04:39:10 +03:00
|
|
|
|
2020-07-30 22:54:34 +03:00
|
|
|
let main_inputs = generate_main_inputs(vec![
|
|
|
|
("a", Some(InputValue::Group(a_string))),
|
|
|
|
("b", Some(InputValue::Group(b_string))),
|
|
|
|
("c", Some(InputValue::Group(c_string))),
|
|
|
|
]);
|
|
|
|
program.set_main_inputs(main_inputs);
|
|
|
|
|
|
|
|
assert_satisfied(program)
|
|
|
|
}
|
2020-05-31 01:37:11 +03:00
|
|
|
}
|
2020-05-31 03:19:26 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_sub() {
|
|
|
|
use std::ops::Sub;
|
|
|
|
|
2020-07-30 22:54:34 +03:00
|
|
|
let mut rng = XorShiftRng::seed_from_u64(1231275789u64);
|
2020-05-31 03:19:26 +03:00
|
|
|
|
2020-07-30 22:54:34 +03:00
|
|
|
for _ in 0..10 {
|
|
|
|
let a: EdwardsAffine = rng.gen();
|
|
|
|
let b: EdwardsAffine = rng.gen();
|
|
|
|
let c = a.sub(&b);
|
2020-05-31 03:19:26 +03:00
|
|
|
|
2020-07-30 22:54:34 +03:00
|
|
|
let a_string = group_to_decimal_string(a);
|
|
|
|
let b_string = group_to_decimal_string(b);
|
|
|
|
let c_string = group_to_decimal_string(c);
|
2020-06-09 04:39:10 +03:00
|
|
|
|
2020-07-30 22:54:34 +03:00
|
|
|
let bytes = include_bytes!("sub.leo");
|
|
|
|
let mut program = parse_program(bytes).unwrap();
|
2020-06-01 22:15:49 +03:00
|
|
|
|
2020-07-30 22:54:34 +03:00
|
|
|
let main_inputs = generate_main_inputs(vec![
|
|
|
|
("a", Some(InputValue::Group(a_string))),
|
|
|
|
("b", Some(InputValue::Group(b_string))),
|
|
|
|
("c", Some(InputValue::Group(c_string))),
|
|
|
|
]);
|
|
|
|
program.set_main_inputs(main_inputs);
|
2020-06-09 04:39:10 +03:00
|
|
|
|
2020-07-30 22:54:34 +03:00
|
|
|
assert_satisfied(program)
|
|
|
|
}
|
2020-06-01 22:15:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-07-30 22:54:34 +03:00
|
|
|
fn test_assert_eq_pass() {
|
|
|
|
let mut rng = XorShiftRng::seed_from_u64(1231275789u64);
|
|
|
|
|
|
|
|
for _ in 0..10 {
|
|
|
|
let a: EdwardsAffine = rng.gen();
|
|
|
|
|
|
|
|
let a_string = group_to_decimal_string(a);
|
|
|
|
|
|
|
|
let bytes = include_bytes!("assert_eq.leo");
|
|
|
|
let mut program = parse_program(bytes).unwrap();
|
2020-06-09 04:39:10 +03:00
|
|
|
|
2020-07-30 22:54:34 +03:00
|
|
|
let main_inputs = generate_main_inputs(vec![
|
|
|
|
("a", Some(InputValue::Group(a_string.clone()))),
|
|
|
|
("b", Some(InputValue::Group(a_string))),
|
|
|
|
]);
|
|
|
|
|
|
|
|
program.set_main_inputs(main_inputs);
|
|
|
|
|
|
|
|
assert_satisfied(program);
|
|
|
|
}
|
2020-06-01 22:15:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-07-30 22:54:34 +03:00
|
|
|
fn test_assert_eq_fail() {
|
|
|
|
let mut rng = XorShiftRng::seed_from_u64(1231275789u64);
|
|
|
|
|
|
|
|
for _ in 0..10 {
|
|
|
|
let a: EdwardsAffine = rng.gen();
|
|
|
|
let b: EdwardsAffine = rng.gen();
|
|
|
|
|
|
|
|
if a == b {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let a_string = group_to_decimal_string(a);
|
|
|
|
let b_string = group_to_decimal_string(b);
|
|
|
|
|
|
|
|
let bytes = include_bytes!("assert_eq.leo");
|
|
|
|
let mut program = parse_program(bytes).unwrap();
|
|
|
|
|
|
|
|
let main_inputs = generate_main_inputs(vec![
|
|
|
|
("a", Some(InputValue::Group(a_string))),
|
|
|
|
("b", Some(InputValue::Group(b_string))),
|
|
|
|
]);
|
|
|
|
|
|
|
|
program.set_main_inputs(main_inputs);
|
|
|
|
|
|
|
|
expect_synthesis_error(program);
|
|
|
|
}
|
2020-06-01 22:15:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-07-30 22:54:34 +03:00
|
|
|
fn test_eq() {
|
|
|
|
let mut rng = XorShiftRng::seed_from_u64(1231275789u64);
|
|
|
|
|
|
|
|
for _ in 0..10 {
|
|
|
|
let a: EdwardsAffine = rng.gen();
|
|
|
|
let b: EdwardsAffine = rng.gen();
|
|
|
|
|
|
|
|
let a_string = group_to_decimal_string(a);
|
|
|
|
let b_string = group_to_decimal_string(b);
|
|
|
|
|
|
|
|
// test equal
|
|
|
|
|
|
|
|
let bytes = include_bytes!("eq.leo");
|
|
|
|
let mut program = parse_program(bytes).unwrap();
|
|
|
|
|
|
|
|
let main_inputs = generate_main_inputs(vec![
|
|
|
|
("a", Some(InputValue::Group(a_string.clone()))),
|
|
|
|
("b", Some(InputValue::Group(a_string.clone()))),
|
|
|
|
("c", Some(InputValue::Boolean(true))),
|
|
|
|
]);
|
|
|
|
|
|
|
|
program.set_main_inputs(main_inputs);
|
2020-06-09 04:39:10 +03:00
|
|
|
|
2020-07-30 22:54:34 +03:00
|
|
|
assert_satisfied(program);
|
|
|
|
|
|
|
|
// test not equal
|
|
|
|
|
|
|
|
let c = a.eq(&b);
|
|
|
|
|
|
|
|
let mut program = parse_program(bytes).unwrap();
|
|
|
|
|
|
|
|
let main_inputs = generate_main_inputs(vec![
|
|
|
|
("a", Some(InputValue::Group(a_string))),
|
|
|
|
("b", Some(InputValue::Group(b_string))),
|
|
|
|
("c", Some(InputValue::Boolean(c))),
|
|
|
|
]);
|
|
|
|
|
|
|
|
program.set_main_inputs(main_inputs);
|
|
|
|
|
|
|
|
assert_satisfied(program);
|
|
|
|
}
|
2020-06-01 22:15:49 +03:00
|
|
|
}
|
2020-06-02 00:20:14 +03:00
|
|
|
|
2020-06-02 03:23:13 +03:00
|
|
|
#[test]
|
|
|
|
fn test_ternary() {
|
2020-07-30 22:54:34 +03:00
|
|
|
let mut rng = XorShiftRng::seed_from_u64(1231275789u64);
|
|
|
|
|
|
|
|
let a: EdwardsAffine = rng.gen();
|
|
|
|
let b: EdwardsAffine = rng.gen();
|
|
|
|
|
|
|
|
let a_string = group_to_decimal_string(a);
|
|
|
|
let b_string = group_to_decimal_string(b);
|
|
|
|
|
2020-06-09 04:39:10 +03:00
|
|
|
let bytes = include_bytes!("ternary.leo");
|
2020-07-30 22:54:34 +03:00
|
|
|
let mut program = parse_program(bytes).unwrap();
|
2020-06-09 04:39:10 +03:00
|
|
|
|
2020-07-30 22:54:34 +03:00
|
|
|
// true -> field a
|
|
|
|
let main_inputs = generate_main_inputs(vec![
|
|
|
|
("s", Some(InputValue::Boolean(true))),
|
|
|
|
("a", Some(InputValue::Group(a_string.clone()))),
|
|
|
|
("b", Some(InputValue::Group(b_string.clone()))),
|
|
|
|
("c", Some(InputValue::Group(a_string.clone()))),
|
|
|
|
]);
|
2020-06-02 03:23:13 +03:00
|
|
|
|
2020-07-30 22:54:34 +03:00
|
|
|
program.set_main_inputs(main_inputs);
|
|
|
|
|
|
|
|
assert_satisfied(program);
|
|
|
|
|
|
|
|
let mut program = parse_program(bytes).unwrap();
|
2020-06-02 03:23:13 +03:00
|
|
|
|
2020-07-30 22:54:34 +03:00
|
|
|
// false -> field b
|
|
|
|
let main_inputs = generate_main_inputs(vec![
|
|
|
|
("s", Some(InputValue::Boolean(false))),
|
|
|
|
("a", Some(InputValue::Group(a_string))),
|
|
|
|
("b", Some(InputValue::Group(b_string.clone()))),
|
|
|
|
("c", Some(InputValue::Group(b_string))),
|
|
|
|
]);
|
2020-06-02 03:23:13 +03:00
|
|
|
|
2020-07-30 22:54:34 +03:00
|
|
|
program.set_main_inputs(main_inputs);
|
2020-06-02 03:23:13 +03:00
|
|
|
|
2020-07-30 22:54:34 +03:00
|
|
|
assert_satisfied(program);
|
2020-06-02 00:20:14 +03:00
|
|
|
}
|