leo/compiler/tests/field/mod.rs

362 lines
10 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-08-17 05:14:26 +03:00
use crate::{assert_satisfied, expect_compiler_error, generate_main_input, parse_program};
2020-10-31 03:31:09 +03:00
use leo_ast::InputValue;
2020-06-03 01:33:09 +03:00
2020-12-30 19:40:45 +03:00
use snarkvm_curves::edwards_bls12::Fq;
use snarkvm_utilities::bytes::ToBytes;
2020-07-17 22:44:08 +03:00
use num_bigint::BigUint;
use rand::{Rng, SeedableRng};
use rand_xorshift::XorShiftRng;
2020-06-03 01:33:09 +03:00
// Helper function to convert field element into decimal base 10 string
2020-07-30 22:54:34 +03:00
pub fn field_to_decimal_string(f: Fq) -> String {
// write field to buffer
2020-06-03 01:33:09 +03:00
let mut buf = Vec::new();
2020-06-03 01:33:09 +03:00
f.write(&mut buf).unwrap();
2020-06-09 04:27:11 +03:00
// convert to big integer
2020-06-03 01:33:09 +03:00
let f_bigint = BigUint::from_bytes_le(&buf);
2020-06-09 04:27:11 +03:00
f_bigint.to_str_radix(10)
2020-06-03 01:33:09 +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: Fq = rng.gen();
let b = a.neg();
let a_string = field_to_decimal_string(a);
let b_string = field_to_decimal_string(b);
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::Field(a_string))),
("b", Some(InputValue::Field(b_string))),
]);
program.set_main_input(main_input);
assert_satisfied(program)
}
}
2021-02-12 23:20:58 +03:00
#[test]
fn test_field() {
let program_string = include_str!("field.leo");
2021-02-12 23:20:58 +03:00
let mut program = parse_program(program_string).unwrap();
assert_satisfied(program)
}
2020-06-03 01:33:09 +03:00
#[test]
fn test_add() {
use std::ops::Add;
2020-07-17 22:44:08 +03:00
let mut rng = XorShiftRng::seed_from_u64(1231275789u64);
2020-06-03 01:33:09 +03:00
for _ in 0..10 {
let a: Fq = rng.gen();
let b: Fq = rng.gen();
let c = a.add(&b);
2020-06-03 01:33:09 +03:00
let a_string = field_to_decimal_string(a);
let b_string = field_to_decimal_string(b);
let c_string = field_to_decimal_string(c);
2020-06-03 01:33:09 +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:27:11 +03:00
2020-08-01 06:59:50 +03:00
let main_input = generate_main_input(vec![
("a", Some(InputValue::Field(a_string))),
("b", Some(InputValue::Field(b_string))),
("c", Some(InputValue::Field(c_string))),
2020-06-03 01:33:09 +03:00
]);
2020-07-31 01:41:03 +03:00
2020-08-01 06:59:50 +03:00
program.set_main_input(main_input);
2020-06-03 01:33:09 +03:00
assert_satisfied(program)
2020-06-03 01:33:09 +03:00
}
}
#[test]
fn test_sub() {
use std::ops::Sub;
2020-07-17 22:44:08 +03:00
let mut rng = XorShiftRng::seed_from_u64(1231275789u64);
2020-06-03 01:33:09 +03:00
for _ in 0..10 {
let a: Fq = rng.gen();
let b: Fq = rng.gen();
let c = a.sub(&b);
2020-06-03 01:33:09 +03:00
let a_string = field_to_decimal_string(a);
let b_string = field_to_decimal_string(b);
let c_string = field_to_decimal_string(c);
2020-06-03 01:33:09 +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-06-09 04:27:11 +03:00
2020-08-01 06:59:50 +03:00
let main_input = generate_main_input(vec![
("a", Some(InputValue::Field(a_string))),
("b", Some(InputValue::Field(b_string))),
("c", Some(InputValue::Field(c_string))),
2020-06-03 01:33:09 +03:00
]);
2020-08-01 06:59:50 +03:00
program.set_main_input(main_input);
2020-06-03 01:33:09 +03:00
assert_satisfied(program)
2020-06-03 01:33:09 +03:00
}
}
#[test]
fn test_div() {
use std::ops::Div;
2020-06-03 01:33:09 +03:00
2020-07-17 22:44:08 +03:00
let mut rng = XorShiftRng::seed_from_u64(1231275789u64);
2020-06-03 01:33:09 +03:00
for _ in 0..10 {
let a: Fq = rng.gen();
let b: Fq = rng.gen();
let c = a.div(&b);
2020-07-17 22:44:08 +03:00
let a_string = field_to_decimal_string(a);
let b_string = field_to_decimal_string(b);
let c_string = field_to_decimal_string(c);
2020-06-03 01:33:09 +03:00
2020-12-04 23:48:43 +03:00
let program_string = include_str!("div.leo");
let mut program = parse_program(program_string).unwrap();
2020-06-09 04:27:11 +03:00
2020-08-01 06:59:50 +03:00
let main_input = generate_main_input(vec![
("a", Some(InputValue::Field(a_string))),
("b", Some(InputValue::Field(b_string))),
("c", Some(InputValue::Field(c_string))),
2020-06-03 01:33:09 +03:00
]);
2020-08-01 06:59:50 +03:00
program.set_main_input(main_input);
2020-06-03 01:33:09 +03:00
assert_satisfied(program)
2020-06-03 01:33:09 +03:00
}
}
#[test]
fn test_mul() {
use std::ops::Mul;
2020-06-03 01:33:09 +03:00
2020-07-17 22:44:08 +03:00
let mut rng = XorShiftRng::seed_from_u64(1231275789u64);
2020-06-03 01:33:09 +03:00
for _ in 0..10 {
let a: Fq = rng.gen();
let b: Fq = rng.gen();
let c = a.mul(&b);
2020-06-03 01:33:09 +03:00
let a_string = field_to_decimal_string(a);
let b_string = field_to_decimal_string(b);
let c_string = field_to_decimal_string(c);
2020-06-03 01:33:09 +03:00
2020-12-04 23:48:43 +03:00
let program_string = include_str!("mul.leo");
let mut program = parse_program(program_string).unwrap();
2020-06-09 04:27:11 +03:00
2020-08-01 06:59:50 +03:00
let main_input = generate_main_input(vec![
("a", Some(InputValue::Field(a_string))),
("b", Some(InputValue::Field(b_string))),
("c", Some(InputValue::Field(c_string))),
2020-06-03 01:33:09 +03:00
]);
2020-08-01 06:59:50 +03:00
program.set_main_input(main_input);
assert_satisfied(program)
2020-06-03 02:06:25 +03:00
}
}
#[test]
2020-06-03 02:56:11 +03:00
fn test_eq() {
2020-07-17 22:44:08 +03:00
let mut rng = XorShiftRng::seed_from_u64(1231275789u64);
2020-06-03 02:06:25 +03:00
for _ in 0..10 {
let a: Fq = rng.gen();
let b: Fq = rng.gen();
2020-07-17 22:44:08 +03:00
let a_string = field_to_decimal_string(a);
let b_string = field_to_decimal_string(b);
2020-06-03 02:06:25 +03:00
2020-06-03 02:56:11 +03:00
// test equal
2020-07-17 22:44:08 +03:00
2020-12-04 23:48:43 +03:00
let program_string = include_str!("eq.leo");
let mut program = parse_program(program_string).unwrap();
2020-06-09 04:27:11 +03:00
2020-08-01 06:59:50 +03:00
let main_input = generate_main_input(vec![
("a", Some(InputValue::Field(a_string.clone()))),
("b", Some(InputValue::Field(a_string.clone()))),
("c", Some(InputValue::Boolean(true))),
2020-06-03 02:06:25 +03:00
]);
2020-08-01 06:59:50 +03:00
program.set_main_input(main_input);
assert_satisfied(program);
2020-06-03 02:56:11 +03:00
// test not equal
let c = a.eq(&b);
2020-06-03 02:56:11 +03:00
2020-12-04 23:48:43 +03:00
let mut program = parse_program(program_string).unwrap();
2020-06-09 04:27:11 +03:00
2020-08-01 06:59:50 +03:00
let main_input = generate_main_input(vec![
("a", Some(InputValue::Field(a_string))),
("b", Some(InputValue::Field(b_string))),
("c", Some(InputValue::Boolean(c))),
2020-06-03 02:56:11 +03:00
]);
2020-08-01 06:59:50 +03:00
program.set_main_input(main_input);
assert_satisfied(program);
2020-06-03 02:06:25 +03:00
}
}
#[test]
2020-08-17 05:14:26 +03:00
fn test_console_assert_pass() {
2020-07-17 22:44:08 +03:00
let mut rng = XorShiftRng::seed_from_u64(1231275789u64);
2020-06-03 02:06:25 +03:00
for _ in 0..10 {
let a: Fq = rng.gen();
let a_string = field_to_decimal_string(a);
2020-06-03 02:06:25 +03:00
2020-12-04 23:48:43 +03:00
let program_string = include_str!("console_assert.leo");
let mut program = parse_program(program_string).unwrap();
2020-06-09 04:27:11 +03:00
2020-08-01 06:59:50 +03:00
let main_input = generate_main_input(vec![
("a", Some(InputValue::Field(a_string.clone()))),
("b", Some(InputValue::Field(a_string))),
2020-06-03 02:06:25 +03:00
]);
2020-08-01 06:59:50 +03:00
program.set_main_input(main_input);
assert_satisfied(program);
2020-06-03 01:33:09 +03:00
}
}
2020-06-03 02:06:25 +03:00
#[test]
2020-08-17 05:14:26 +03:00
fn test_console_assert_fail() {
2020-07-17 22:44:08 +03:00
let mut rng = XorShiftRng::seed_from_u64(1231275789u64);
2020-06-03 02:06:25 +03:00
for _ in 0..10 {
let a: Fq = rng.gen();
let b: Fq = rng.gen();
2020-07-17 22:44:08 +03:00
if a == b {
2020-06-03 02:06:25 +03:00
continue;
}
let a_string = field_to_decimal_string(a);
let b_string = field_to_decimal_string(b);
2020-12-04 23:48:43 +03:00
let program_string = include_str!("console_assert.leo");
let mut program = parse_program(program_string).unwrap();
2020-06-09 04:27:11 +03:00
2020-08-01 06:59:50 +03:00
let main_input = generate_main_input(vec![
("a", Some(InputValue::Field(a_string))),
("b", Some(InputValue::Field(b_string))),
2020-06-03 02:06:25 +03:00
]);
2020-08-01 06:59:50 +03:00
program.set_main_input(main_input);
2020-08-17 05:14:26 +03:00
expect_compiler_error(program);
2020-06-03 02:06:25 +03:00
}
}
#[test]
fn test_ternary() {
let mut rng = XorShiftRng::seed_from_u64(1231275789u64);
2020-06-03 02:06:25 +03:00
let a: Fq = rng.gen();
let b: Fq = rng.gen();
2020-06-03 02:06:25 +03:00
let a_string = field_to_decimal_string(a);
let b_string = field_to_decimal_string(b);
2020-06-03 02:06:25 +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();
// true -> field a
2020-08-01 06:59:50 +03:00
let main_input = generate_main_input(vec![
("s", Some(InputValue::Boolean(true))),
("a", Some(InputValue::Field(a_string.clone()))),
("b", Some(InputValue::Field(b_string.clone()))),
("c", Some(InputValue::Field(a_string.clone()))),
2020-06-03 02:06:25 +03:00
]);
2020-08-01 06:59:50 +03:00
program.set_main_input(main_input);
2020-06-03 02:06:25 +03:00
assert_satisfied(program);
2020-12-04 23:48:43 +03:00
let mut program = parse_program(program_string).unwrap();
// false -> field b
2020-08-01 06:59:50 +03:00
let main_input = generate_main_input(vec![
("s", Some(InputValue::Boolean(false))),
("a", Some(InputValue::Field(a_string))),
("b", Some(InputValue::Field(b_string.clone()))),
("c", Some(InputValue::Field(b_string))),
2020-06-03 02:06:25 +03:00
]);
2020-08-01 06:59:50 +03:00
program.set_main_input(main_input);
assert_satisfied(program);
2020-06-03 02:06:25 +03:00
}
//
// pub fn output_one(program: EdwardsTestCompiler) {
2020-12-04 23:34:51 +03:00
// let expected = include_str!("output_/register_one.out");
2020-08-01 07:15:33 +03:00
// let actual = get_output(program);
//
2020-12-04 23:48:43 +03:00
// assert_eq!(expected, actual.program_string().as_slice());
// }
//
// pub fn output_zero(program: EdwardsTestCompiler) {
2020-12-04 23:34:51 +03:00
// let expected = include_str!("output_/register_zero.out");
2020-08-01 07:15:33 +03:00
// let actual = get_output(program);
//
2020-12-04 23:48:43 +03:00
// assert_eq!(expected, actual.program_string().as_slice());
// }
//
// #[test]
// fn test_registers() {
2020-12-04 23:34:51 +03:00
// let program_bytes = include_str!("output_register.leo");
// let one_input_bytes = include_str!("input/register_one.in");
// let zero_input_bytes = include_str!("input/register_zero.in");
//
// // test 1field input register => 1field output register
2020-08-01 06:59:50 +03:00
// let program = parse_program_with_input(program_bytes, one_input_bytes).unwrap();
//
// output_one(program);
//
// // test 0field input register => 0field output register
2020-08-01 06:59:50 +03:00
// let program = parse_program_with_input(program_bytes, zero_input_bytes).unwrap();
//
// output_zero(program);
// }