leo/compiler/tests/field/mod.rs

354 lines
9.6 KiB
Rust
Raw Normal View History

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-08-17 05:14:26 +03:00
use crate::{assert_satisfied, expect_compiler_error, generate_main_input, parse_program};
2020-08-03 06:56:22 +03:00
use leo_typed::InputValue;
2020-06-03 01:33:09 +03:00
use snarkos_curves::edwards_bls12::Fq;
use snarkos_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);
let bytes = include_bytes!("negate.leo");
let mut program = parse_program(bytes).unwrap();
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)
}
}
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-06-09 04:27:11 +03:00
let bytes = include_bytes!("add.leo");
let mut program = parse_program(bytes).unwrap();
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-06-09 04:27:11 +03:00
let bytes = include_bytes!("sub.leo");
let mut program = parse_program(bytes).unwrap();
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
let bytes = include_bytes!("div.leo");
2020-06-09 04:27:11 +03:00
let mut program = parse_program(bytes).unwrap();
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
let bytes = include_bytes!("mul.leo");
2020-06-09 04:27:11 +03:00
let mut program = parse_program(bytes).unwrap();
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-06-09 04:27:11 +03:00
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![
("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-06-09 04:27:11 +03:00
let mut program = parse_program(bytes).unwrap();
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-08-17 05:14:26 +03:00
let bytes = include_bytes!("console_assert.leo");
2020-06-09 04:27:11 +03:00
let mut program = parse_program(bytes).unwrap();
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-08-17 05:14:26 +03:00
let bytes = include_bytes!("console_assert.leo");
2020-06-09 04:27:11 +03:00
let mut program = parse_program(bytes).unwrap();
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-06-09 04:27:11 +03:00
let bytes = include_bytes!("ternary.leo");
let mut program = parse_program(bytes).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);
let mut program = parse_program(bytes).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-08-01 07:15:33 +03:00
// let expected = include_bytes!("output_/register_one.out");
// let actual = get_output(program);
//
// assert_eq!(expected, actual.bytes().as_slice());
// }
//
// pub fn output_zero(program: EdwardsTestCompiler) {
2020-08-01 07:15:33 +03:00
// let expected = include_bytes!("output_/register_zero.out");
// let actual = get_output(program);
//
// assert_eq!(expected, actual.bytes().as_slice());
// }
//
// #[test]
// fn test_registers() {
// let program_bytes = include_bytes!("output_register.leo");
2020-08-01 06:59:50 +03:00
// let one_input_bytes = include_bytes!("input/register_one.in");
// let zero_input_bytes = include_bytes!("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);
// }