2020-06-02 04:35:43 +03:00
|
|
|
use crate::{
|
2020-07-30 22:54:34 +03:00
|
|
|
assert_satisfied,
|
2020-08-16 02:31:07 +03:00
|
|
|
expect_compiler_error,
|
2020-07-30 22:54:34 +03:00
|
|
|
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-16 03:21:18 +03:00
|
|
|
use leo_typed::{GroupCoordinate, GroupValue, InputValue, Span};
|
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-08-16 03:21:18 +03:00
|
|
|
pub fn group_element_to_input_value(g: EdwardsAffine) -> GroupValue {
|
2020-07-30 22:54:34 +03:00
|
|
|
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-08-16 03:21:18 +03:00
|
|
|
format!("({}, {})", x, y);
|
|
|
|
|
|
|
|
let fake_span = Span {
|
|
|
|
text: "".to_string(),
|
|
|
|
line: 0,
|
|
|
|
start: 0,
|
|
|
|
end: 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
GroupValue {
|
|
|
|
x: GroupCoordinate::Number(x, fake_span.clone()),
|
|
|
|
y: GroupCoordinate::Number(y, fake_span.clone()),
|
|
|
|
span: fake_span,
|
|
|
|
}
|
2020-07-07 12:08:48 +03:00
|
|
|
}
|
|
|
|
|
2020-05-31 01:37:11 +03:00
|
|
|
#[test]
|
2020-08-16 02:31:07 +03:00
|
|
|
fn test_point() {
|
|
|
|
let bytes = include_bytes!("point.leo");
|
2020-06-09 04:39:10 +03:00
|
|
|
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]
|
2020-08-16 02:31:07 +03:00
|
|
|
fn test_x_sign_high() {
|
|
|
|
let bytes = include_bytes!("x_sign_high.leo");
|
2020-07-07 12:08:48 +03:00
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-08-16 02:31:07 +03:00
|
|
|
assert_satisfied(program);
|
2020-07-07 12:08:48 +03:00
|
|
|
}
|
|
|
|
|
2020-05-31 03:05:07 +03:00
|
|
|
#[test]
|
2020-08-16 02:31:07 +03:00
|
|
|
fn test_x_sign_low() {
|
|
|
|
let bytes = include_bytes!("x_sign_low.leo");
|
2020-06-09 04:39:10 +03:00
|
|
|
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-08-16 02:31:07 +03:00
|
|
|
#[test]
|
|
|
|
fn test_x_sign_inferred() {
|
|
|
|
let bytes = include_bytes!("x_sign_inferred.leo");
|
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
|
|
|
assert_satisfied(program);
|
|
|
|
}
|
|
|
|
|
|
|
|
// #[test]
|
|
|
|
// fn test_y_sign_high() {
|
|
|
|
// let bytes = include_bytes!("y_sign_high.leo");
|
|
|
|
// let program = parse_program(bytes).unwrap();
|
|
|
|
//
|
|
|
|
// assert_satisfied(program);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// #[test]
|
|
|
|
// fn test_y_sign_low() {
|
|
|
|
// let bytes = include_bytes!("y_sign_low.leo");
|
|
|
|
// let program = parse_program(bytes).unwrap();
|
|
|
|
//
|
|
|
|
// assert_satisfied(program);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// #[test]
|
|
|
|
// fn test_y_sign_inferred() {
|
|
|
|
// let bytes = include_bytes!("y_sign_inferred.leo");
|
|
|
|
// let program = parse_program(bytes).unwrap();
|
|
|
|
//
|
|
|
|
// assert_satisfied(program);
|
|
|
|
// }
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_double_high() {
|
|
|
|
let bytes = include_bytes!("double_high.leo");
|
|
|
|
|
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
|
|
|
expect_compiler_error(program);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_double_low() {
|
|
|
|
let bytes = include_bytes!("double_low.leo");
|
|
|
|
|
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
|
|
|
expect_compiler_error(program);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_double_inferred() {
|
|
|
|
let bytes = include_bytes!("double_inferred.leo");
|
|
|
|
|
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
|
|
|
expect_compiler_error(program);
|
|
|
|
}
|
|
|
|
|
2020-08-13 21:37:48 +03:00
|
|
|
#[test]
|
|
|
|
fn test_point_input() {
|
|
|
|
let program_bytes = include_bytes!("point_input.leo");
|
|
|
|
let input_bytes = include_bytes!("input/point.in");
|
|
|
|
|
|
|
|
let program = parse_program_with_input(program_bytes, input_bytes).unwrap();
|
|
|
|
|
|
|
|
assert_satisfied(program);
|
|
|
|
}
|
|
|
|
|
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-16 03:21:18 +03:00
|
|
|
let input_bytes_pass = include_bytes!("input/valid.in");
|
|
|
|
let input_bytes_fail = include_bytes!("input/invalid.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-08-16 03:21:18 +03:00
|
|
|
expect_compiler_error(program);
|
2020-06-03 02:06:25 +03:00
|
|
|
}
|
|
|
|
|
2020-08-05 07:37:09 +03:00
|
|
|
#[test]
|
|
|
|
fn test_negate() {
|
|
|
|
use std::ops::Neg;
|
|
|
|
|
|
|
|
let mut rng = XorShiftRng::seed_from_u64(1231275789u64);
|
|
|
|
|
|
|
|
for _ in 0..10 {
|
|
|
|
let a: EdwardsAffine = rng.gen();
|
|
|
|
let b = a.neg();
|
|
|
|
|
2020-08-16 03:21:18 +03:00
|
|
|
let a_element = group_element_to_input_value(a);
|
|
|
|
let b_element = group_element_to_input_value(b);
|
2020-08-05 07:37:09 +03:00
|
|
|
|
|
|
|
let bytes = include_bytes!("negate.leo");
|
|
|
|
let mut program = parse_program(bytes).unwrap();
|
|
|
|
|
|
|
|
let main_input = generate_main_input(vec![
|
2020-08-16 03:21:18 +03:00
|
|
|
("a", Some(InputValue::Group(a_element))),
|
|
|
|
("b", Some(InputValue::Group(b_element))),
|
2020-08-05 07:37:09 +03:00
|
|
|
]);
|
|
|
|
program.set_main_input(main_input);
|
|
|
|
|
|
|
|
assert_satisfied(program)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-08-16 03:21:18 +03:00
|
|
|
let a_element = group_element_to_input_value(a);
|
|
|
|
let b_element = group_element_to_input_value(b);
|
|
|
|
let c_element = group_element_to_input_value(c);
|
2020-07-30 22:54:34 +03:00
|
|
|
|
|
|
|
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-08-16 03:21:18 +03:00
|
|
|
("a", Some(InputValue::Group(a_element))),
|
|
|
|
("b", Some(InputValue::Group(b_element))),
|
|
|
|
("c", Some(InputValue::Group(c_element))),
|
2020-07-30 22:54:34 +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)
|
|
|
|
}
|
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-08-16 03:21:18 +03:00
|
|
|
let a_element = group_element_to_input_value(a);
|
|
|
|
let b_element = group_element_to_input_value(b);
|
|
|
|
let c_element = group_element_to_input_value(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-08-16 03:21:18 +03:00
|
|
|
("a", Some(InputValue::Group(a_element))),
|
|
|
|
("b", Some(InputValue::Group(b_element))),
|
|
|
|
("c", Some(InputValue::Group(c_element))),
|
2020-07-30 22:54:34 +03:00
|
|
|
]);
|
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();
|
|
|
|
|
2020-08-16 03:21:18 +03:00
|
|
|
let a_element = group_element_to_input_value(a);
|
2020-07-30 22:54:34 +03:00
|
|
|
|
|
|
|
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-08-16 03:21:18 +03:00
|
|
|
("a", Some(InputValue::Group(a_element.clone()))),
|
|
|
|
("b", Some(InputValue::Group(a_element))),
|
2020-07-30 22:54:34 +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);
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2020-08-16 03:21:18 +03:00
|
|
|
let a_element = group_element_to_input_value(a);
|
|
|
|
let b_element = group_element_to_input_value(b);
|
2020-07-30 22:54:34 +03:00
|
|
|
|
|
|
|
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-08-16 03:21:18 +03:00
|
|
|
("a", Some(InputValue::Group(a_element))),
|
|
|
|
("b", Some(InputValue::Group(b_element))),
|
2020-07-30 22:54:34 +03:00
|
|
|
]);
|
|
|
|
|
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-08-11 00:46:36 +03:00
|
|
|
#[ignore]
|
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();
|
|
|
|
|
2020-08-16 03:21:18 +03:00
|
|
|
let a_element = group_element_to_input_value(a);
|
|
|
|
let b_element = group_element_to_input_value(b);
|
2020-07-30 22:54:34 +03:00
|
|
|
|
|
|
|
// 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-08-16 03:21:18 +03:00
|
|
|
("a", Some(InputValue::Group(a_element.clone()))),
|
|
|
|
("b", Some(InputValue::Group(a_element.clone()))),
|
2020-07-30 22:54:34 +03:00
|
|
|
("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-08-16 03:21:18 +03:00
|
|
|
("a", Some(InputValue::Group(a_element))),
|
|
|
|
("b", Some(InputValue::Group(b_element))),
|
2020-07-30 22:54:34 +03:00
|
|
|
("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();
|
|
|
|
|
2020-08-16 03:21:18 +03:00
|
|
|
let a_element = group_element_to_input_value(a);
|
|
|
|
let b_element = group_element_to_input_value(b);
|
2020-07-30 22:54:34 +03:00
|
|
|
|
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))),
|
2020-08-16 03:21:18 +03:00
|
|
|
("a", Some(InputValue::Group(a_element.clone()))),
|
|
|
|
("b", Some(InputValue::Group(b_element.clone()))),
|
|
|
|
("c", Some(InputValue::Group(a_element.clone()))),
|
2020-07-30 22:54:34 +03:00
|
|
|
]);
|
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))),
|
2020-08-16 03:21:18 +03:00
|
|
|
("a", Some(InputValue::Group(a_element))),
|
|
|
|
("b", Some(InputValue::Group(b_element.clone()))),
|
|
|
|
("c", Some(InputValue::Group(b_element))),
|
2020-07-30 22:54:34 +03:00
|
|
|
]);
|
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
|
|
|
}
|