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,
|
2020-08-01 05:39:30 +03:00
|
|
|
generate_main_input,
|
2020-06-09 04:39:10 +03:00
|
|
|
parse_program,
|
2020-08-01 05:39:30 +03:00
|
|
|
parse_program_with_input,
|
2020-06-02 04:35:43 +03:00
|
|
|
};
|
2020-08-03 06:56:22 +03:00
|
|
|
use leo_typed::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-08-01 06:59:50 +03:00
|
|
|
fn test_input() {
|
2020-07-30 22:54:34 +03:00
|
|
|
let program_bytes = include_bytes!("input.leo");
|
2020-08-01 06:59:50 +03:00
|
|
|
let input_bytes_pass = include_bytes!("input/one_one.in");
|
|
|
|
let input_bytes_fail = include_bytes!("input/one_zero.in");
|
2020-07-30 22:54:34 +03:00
|
|
|
|
2020-08-01 05:39:30 +03:00
|
|
|
let program = parse_program_with_input(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-08-01 05:39:30 +03:00
|
|
|
let program = parse_program_with_input(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-08-01 06:59:50 +03:00
|
|
|
let main_input = generate_main_input(vec![
|
2020-07-30 22:54:34 +03:00
|
|
|
("a", Some(InputValue::Group(a_string))),
|
|
|
|
("b", Some(InputValue::Group(b_string))),
|
|
|
|
("c", Some(InputValue::Group(c_string))),
|
|
|
|
]);
|
2020-08-01 06:59:50 +03:00
|
|
|
program.set_main_input(main_input);
|
2020-07-30 22:54:34 +03:00
|
|
|
|
|
|
|
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-08-01 06:59:50 +03:00
|
|
|
let main_input = generate_main_input(vec![
|
2020-07-30 22:54:34 +03:00
|
|
|
("a", Some(InputValue::Group(a_string))),
|
|
|
|
("b", Some(InputValue::Group(b_string))),
|
|
|
|
("c", Some(InputValue::Group(c_string))),
|
|
|
|
]);
|
2020-08-01 06:59:50 +03:00
|
|
|
program.set_main_input(main_input);
|
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-08-01 06:59:50 +03:00
|
|
|
let main_input = generate_main_input(vec![
|
2020-07-30 22:54:34 +03:00
|
|
|
("a", Some(InputValue::Group(a_string.clone()))),
|
|
|
|
("b", Some(InputValue::Group(a_string))),
|
|
|
|
]);
|
|
|
|
|
2020-08-01 06:59:50 +03:00
|
|
|
program.set_main_input(main_input);
|
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_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();
|
|
|
|
|
2020-08-01 06:59:50 +03:00
|
|
|
let main_input = generate_main_input(vec![
|
2020-07-30 22:54:34 +03:00
|
|
|
("a", Some(InputValue::Group(a_string))),
|
|
|
|
("b", Some(InputValue::Group(b_string))),
|
|
|
|
]);
|
|
|
|
|
2020-08-01 06:59:50 +03:00
|
|
|
program.set_main_input(main_input);
|
2020-07-30 22:54:34 +03:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
2020-08-01 06:59:50 +03:00
|
|
|
let main_input = generate_main_input(vec![
|
2020-07-30 22:54:34 +03:00
|
|
|
("a", Some(InputValue::Group(a_string.clone()))),
|
|
|
|
("b", Some(InputValue::Group(a_string.clone()))),
|
|
|
|
("c", Some(InputValue::Boolean(true))),
|
|
|
|
]);
|
|
|
|
|
2020-08-01 06:59:50 +03:00
|
|
|
program.set_main_input(main_input);
|
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();
|
|
|
|
|
2020-08-01 06:59:50 +03:00
|
|
|
let main_input = generate_main_input(vec![
|
2020-07-30 22:54:34 +03:00
|
|
|
("a", Some(InputValue::Group(a_string))),
|
|
|
|
("b", Some(InputValue::Group(b_string))),
|
|
|
|
("c", Some(InputValue::Boolean(c))),
|
|
|
|
]);
|
|
|
|
|
2020-08-01 06:59:50 +03:00
|
|
|
program.set_main_input(main_input);
|
2020-07-30 22:54:34 +03:00
|
|
|
|
|
|
|
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
|
2020-08-01 06:59:50 +03:00
|
|
|
let main_input = generate_main_input(vec![
|
2020-07-30 22:54:34 +03:00
|
|
|
("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-08-01 06:59:50 +03:00
|
|
|
program.set_main_input(main_input);
|
2020-07-30 22:54:34 +03:00
|
|
|
|
|
|
|
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
|
2020-08-01 06:59:50 +03:00
|
|
|
let main_input = generate_main_input(vec![
|
2020-07-30 22:54:34 +03:00
|
|
|
("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-08-01 06:59:50 +03:00
|
|
|
program.set_main_input(main_input);
|
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
|
|
|
}
|