2020-08-18 13:50:26 +03:00
|
|
|
// Copyright (C) 2019-2020 Aleo Systems Inc.
|
|
|
|
// This file is part of the Leo library.
|
|
|
|
|
|
|
|
// The Leo library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// The Leo library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
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-18 23:28:08 +03:00
|
|
|
use leo_typed::{GroupCoordinate, GroupTuple, 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,
|
|
|
|
};
|
|
|
|
|
2020-08-18 23:28:08 +03:00
|
|
|
GroupValue::Tuple(GroupTuple {
|
2020-08-16 03:21:18 +03:00
|
|
|
x: GroupCoordinate::Number(x, fake_span.clone()),
|
|
|
|
y: GroupCoordinate::Number(y, fake_span.clone()),
|
|
|
|
span: fake_span,
|
2020-08-18 23:28:08 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_one() {
|
|
|
|
let bytes = include_bytes!("one.leo");
|
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
|
|
|
assert_satisfied(program);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_zero() {
|
|
|
|
let bytes = include_bytes!("zero.leo");
|
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
|
|
|
assert_satisfied(program);
|
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);
|
|
|
|
}
|
|
|
|
|
2020-08-16 08:59:26 +03:00
|
|
|
#[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);
|
|
|
|
}
|
2020-08-16 02:31:07 +03:00
|
|
|
|
|
|
|
#[test]
|
2020-08-18 23:28:08 +03:00
|
|
|
fn test_both_sign_high() {
|
|
|
|
let bytes = include_bytes!("both_sign_high.leo");
|
2020-08-16 02:31:07 +03:00
|
|
|
|
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
|
|
|
expect_compiler_error(program);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-08-18 23:28:08 +03:00
|
|
|
fn test_both_sign_low() {
|
|
|
|
let bytes = include_bytes!("both_sign_low.leo");
|
2020-08-16 02:31:07 +03:00
|
|
|
|
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
|
|
|
expect_compiler_error(program);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-08-18 23:28:08 +03:00
|
|
|
fn test_both_sign_inferred() {
|
|
|
|
let bytes = include_bytes!("both_sign_inferred.leo");
|
2020-08-16 02:31:07 +03:00
|
|
|
|
|
|
|
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
|
|
|
}
|