leo/tests/compiler/group/mod.rs

408 lines
11 KiB
Rust
Raw Normal View History

2021-02-02 07:26:56 +03:00
// Copyright (C) 2019-2021 Aleo Systems Inc.
2020-08-18 13:50:26 +03:00
// 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::{
2021-02-02 07:26:56 +03:00
assert_satisfied, expect_compiler_error, expect_synthesis_error, field::field_to_decimal_string,
generate_main_input, parse_program, parse_program_with_input,
2020-06-02 04:35:43 +03:00
};
2020-10-31 03:31:09 +03:00
use leo_ast::{GroupCoordinate, GroupTuple, GroupValue, InputValue, Span};
2020-05-31 03:05:07 +03:00
2020-12-30 19:40:45 +03:00
use snarkvm_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
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
format!("({}, {})", x, y);
let fake_span = Span {
text: "".to_string(),
line: 0,
start: 0,
end: 0,
};
GroupValue::Tuple(GroupTuple {
2021-03-03 20:59:24 +03:00
x: GroupCoordinate::Number(x, fake_span),
y: GroupCoordinate::Number(y, fake_span),
span: fake_span,
})
}
2021-04-05 19:54:27 +03:00
#[test]
fn test_no_space_between_literal() {
let program_string = include_str!("no_space_between_literal.leo");
let mut program = parse_program(program_string).unwrap();
expect_compiler_error(program)
}
#[test]
fn test_one() {
2020-12-04 23:48:43 +03:00
let program_string = include_str!("one.leo");
let program = parse_program(program_string).unwrap();
assert_satisfied(program);
}
#[test]
fn test_zero() {
2020-12-04 23:48:43 +03:00
let program_string = include_str!("zero.leo");
let program = parse_program(program_string).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() {
2020-12-04 23:48:43 +03:00
let program_string = include_str!("point.leo");
let program = parse_program(program_string).unwrap();
2020-06-09 04:39:10 +03:00
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() {
2020-12-04 23:48:43 +03:00
let program_string = include_str!("x_sign_high.leo");
let program = parse_program(program_string).unwrap();
2020-07-07 12:08:48 +03:00
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() {
2020-12-04 23:48:43 +03:00
let program_string = include_str!("x_sign_low.leo");
let program = parse_program(program_string).unwrap();
2020-06-09 04:39:10 +03:00
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() {
2020-12-04 23:48:43 +03:00
let program_string = include_str!("x_sign_inferred.leo");
let program = parse_program(program_string).unwrap();
2020-08-16 02:31:07 +03:00
assert_satisfied(program);
}
2020-08-16 08:59:26 +03:00
#[test]
fn test_y_sign_high() {
2020-12-04 23:48:43 +03:00
let program_string = include_str!("y_sign_high.leo");
let program = parse_program(program_string).unwrap();
2020-08-16 08:59:26 +03:00
assert_satisfied(program);
}
#[test]
fn test_y_sign_low() {
2020-12-04 23:48:43 +03:00
let program_string = include_str!("y_sign_low.leo");
let program = parse_program(program_string).unwrap();
2020-08-16 08:59:26 +03:00
assert_satisfied(program);
}
#[test]
fn test_y_sign_inferred() {
2020-12-04 23:48:43 +03:00
let program_string = include_str!("y_sign_inferred.leo");
let program = parse_program(program_string).unwrap();
2020-08-16 08:59:26 +03:00
assert_satisfied(program);
}
2020-08-16 02:31:07 +03:00
#[test]
fn test_both_sign_high() {
2020-12-04 23:48:43 +03:00
let program_string = include_str!("both_sign_high.leo");
2020-08-16 02:31:07 +03:00
2020-12-04 23:48:43 +03:00
let program = parse_program(program_string).unwrap();
2020-08-16 02:31:07 +03:00
expect_compiler_error(program);
}
#[test]
fn test_both_sign_low() {
2020-12-04 23:48:43 +03:00
let program_string = include_str!("both_sign_low.leo");
2020-08-16 02:31:07 +03:00
2020-12-04 23:48:43 +03:00
let program = parse_program(program_string).unwrap();
2020-08-16 02:31:07 +03:00
expect_compiler_error(program);
}
#[test]
fn test_both_sign_inferred() {
2020-12-04 23:48:43 +03:00
let program_string = include_str!("both_sign_inferred.leo");
2020-08-16 02:31:07 +03:00
2020-12-04 23:48:43 +03:00
let program = parse_program(program_string).unwrap();
2020-08-16 02:31:07 +03:00
expect_compiler_error(program);
}
2020-08-13 21:37:48 +03:00
#[test]
fn test_point_input() {
2020-12-04 23:48:43 +03:00
let program_string = include_str!("point_input.leo");
2020-12-04 23:34:51 +03:00
let input_bytes = include_str!("input/point.in");
2020-08-13 21:37:48 +03:00
2020-12-04 23:48:43 +03:00
let program = parse_program_with_input(program_string, input_bytes).unwrap();
2020-08-13 21:37:48 +03:00
assert_satisfied(program);
}
2020-06-03 02:06:25 +03:00
#[test]
2020-08-01 06:59:50 +03:00
fn test_input() {
2020-12-04 23:48:43 +03:00
let program_string = include_str!("input.leo");
let input_string_pass = include_str!("input/valid.in");
let input_string_fail = include_str!("input/invalid.in");
2020-07-30 22:54:34 +03:00
2020-12-04 23:48:43 +03:00
let program = parse_program_with_input(program_string, input_string_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-12-04 23:48:43 +03:00
let program = parse_program_with_input(program_string, input_string_fail).unwrap();
2020-06-03 02:06:25 +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();
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
2020-12-04 23:48:43 +03:00
let program_string = include_str!("negate.leo");
let mut program = parse_program(program_string).unwrap();
2020-08-05 07:37:09 +03:00
let main_input = generate_main_input(vec![
("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
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
2020-12-04 23:48:43 +03:00
let program_string = include_str!("add.leo");
let mut program = parse_program(program_string).unwrap();
2020-06-09 04:39:10 +03:00
2020-08-01 06:59:50 +03:00
let main_input = generate_main_input(vec![
("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
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-12-04 23:48:43 +03:00
let program_string = include_str!("sub.leo");
let mut program = parse_program(program_string).unwrap();
2020-08-01 06:59:50 +03:00
let main_input = generate_main_input(vec![
("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)
}
}
#[test]
2020-08-17 05:14:26 +03:00
fn test_console_assert_pass() {
2020-07-30 22:54:34 +03:00
let mut rng = XorShiftRng::seed_from_u64(1231275789u64);
for _ in 0..10 {
let a: EdwardsAffine = rng.gen();
let a_element = group_element_to_input_value(a);
2020-07-30 22:54:34 +03:00
2020-12-04 23:48:43 +03:00
let program_string = include_str!("assert_eq.leo");
let mut program = parse_program(program_string).unwrap();
2020-06-09 04:39:10 +03:00
2020-08-01 06:59:50 +03:00
let main_input = generate_main_input(vec![
("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);
}
}
#[test]
2020-08-17 05:14:26 +03:00
fn test_console_assert_fail() {
2020-07-30 22:54:34 +03:00
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_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-12-04 23:48:43 +03:00
let program_string = include_str!("assert_eq.leo");
let mut program = parse_program(program_string).unwrap();
2020-07-30 22:54:34 +03:00
2020-08-01 06:59:50 +03:00
let main_input = generate_main_input(vec![
("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
2020-08-17 05:14:26 +03:00
expect_compiler_error(program);
2020-07-30 22:54:34 +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_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
2020-12-04 23:48:43 +03:00
let program_string = include_str!("eq.leo");
let mut program = parse_program(program_string).unwrap();
2020-07-30 22:54:34 +03:00
2020-08-01 06:59:50 +03:00
let main_input = generate_main_input(vec![
("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);
2020-12-04 23:48:43 +03:00
let mut program = parse_program(program_string).unwrap();
2020-07-30 22:54:34 +03:00
2020-08-01 06:59:50 +03:00
let main_input = generate_main_input(vec![
("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-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_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-12-04 23:48:43 +03:00
let program_string = include_str!("ternary.leo");
let mut program = parse_program(program_string).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_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);
2020-12-04 23:48:43 +03:00
let mut program = parse_program(program_string).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_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
}
#[test]
fn test_positive_and_negative() {
let program_string = include_str!("positive_and_negative.leo");
let program = parse_program(program_string).unwrap();
assert_satisfied(program);
}