impl array, basic field, basic group tests

This commit is contained in:
collin 2020-05-19 17:08:38 -07:00
parent 9a9e37e0f4
commit 28451ee269
28 changed files with 340 additions and 181 deletions

View File

@ -267,7 +267,7 @@ impl fmt::Display for IntegerType {
match *self {
IntegerType::U8 => write!(f, "u8"),
IntegerType::U16 => write!(f, "u16"),
IntegerType::U32 => write!(f, "integers.u32"),
IntegerType::U32 => write!(f, "u32"),
IntegerType::U64 => write!(f, "u64"),
IntegerType::U128 => write!(f, "u128"),
}

View File

@ -788,7 +788,6 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::InputModel<'ast>>
for types::InputModel<F, G>
{
fn from(parameter: ast::InputModel<'ast>) -> Self {
println!("{}", parameter.mutable.is_some());
types::InputModel {
identifier: types::Identifier::from(parameter.identifier),
mutable: parameter.mutable.is_some(),

View File

@ -0,0 +1,3 @@
function main() -> u32[3] {
return [1u32; 3]
}

View File

@ -0,0 +1,3 @@
function main() -> u32[3] {
return [1u32, 1u32, 1u32]
}

View File

@ -0,0 +1,3 @@
function main(arr: u32[3]) -> u32[3] {
return arr
}

119
compiler/tests/array/mod.rs Normal file
View File

@ -0,0 +1,119 @@
use crate::{compile_program, get_error, get_output};
use leo_compiler::errors::IntegerError;
use leo_compiler::{
compiler::Compiler,
errors::{CompilerError, FunctionError},
ConstrainedValue, InputValue, Integer,
};
use snarkos_curves::{bls12_377::Fr, edwards_bls12::EdwardsProjective};
use snarkos_models::gadgets::utilities::uint32::UInt32;
const DIRECTORY_NAME: &str = "tests/array/";
// [1, 1, 1]
fn output_ones(program: Compiler<Fr, EdwardsProjective>) {
let output = get_output(program);
assert_eq!(
ConstrainedValue::<Fr, EdwardsProjective>::Return(vec![ConstrainedValue::Array(
vec![ConstrainedValue::Integer(Integer::U32(UInt32::constant(1u32))); 3]
)]),
output
);
}
// [[0, 0, 0],
// [0, 0, 0]]
fn output_multi(program: Compiler<Fr, EdwardsProjective>) {
let output = get_output(program);
assert_eq!(
ConstrainedValue::<Fr, EdwardsProjective>::Return(vec![ConstrainedValue::Array(
vec![
ConstrainedValue::Array(vec![
ConstrainedValue::Integer(Integer::U32(
UInt32::constant(0u32)
));
3
]);
2
]
)]),
output
)
}
fn fail_array(program: Compiler<Fr, EdwardsProjective>) {
match get_error(program) {
CompilerError::FunctionError(FunctionError::InvalidArray(_string)) => {}
error => panic!("Expected invalid array error, got {}", error),
}
}
fn fail_synthesis(program: Compiler<Fr, EdwardsProjective>) {
match get_error(program) {
CompilerError::FunctionError(FunctionError::IntegerError(
IntegerError::SynthesisError(_string),
)) => {}
error => panic!("Expected synthesis error, got {}", error),
}
}
// Expressions
#[test]
fn test_inline() {
let program = compile_program(DIRECTORY_NAME, "inline.leo").unwrap();
output_ones(program);
}
#[test]
fn test_initializer() {
let program = compile_program(DIRECTORY_NAME, "initializer.leo").unwrap();
output_ones(program);
}
#[test]
fn test_spread() {
let program = compile_program(DIRECTORY_NAME, "spread.leo").unwrap();
output_ones(program);
}
#[test]
fn test_slice() {
let program = compile_program(DIRECTORY_NAME, "slice.leo").unwrap();
output_ones(program);
}
#[test]
fn test_multi() {
let program = compile_program(DIRECTORY_NAME, "multi.leo").unwrap();
output_multi(program);
}
// Inputs
#[test]
fn test_input_array() {
let mut program = compile_program(DIRECTORY_NAME, "input_array.leo").unwrap();
program.set_inputs(vec![Some(InputValue::Array(vec![
InputValue::Integer(
1usize
);
3
]))]);
output_ones(program)
}
#[test]
fn test_input_array_fail() {
let mut program = compile_program(DIRECTORY_NAME, "input_array.leo").unwrap();
program.set_inputs(vec![Some(InputValue::Integer(1usize))]);
fail_array(program);
}
#[test]
fn test_input_field_none() {
let mut program = compile_program(DIRECTORY_NAME, "input_array.leo").unwrap();
program.set_inputs(vec![None]);
fail_synthesis(program)
}

View File

@ -0,0 +1,8 @@
// Multidimensional array syntax in leo
function main() -> u32[3][2] {
let m = [[0u32, 0u32, 0u32], [0u32, 0u32, 0u32]]; // inline
let m: u32[3][2] = [[0; 3]; 2]; // initializer
return m
}

View File

@ -0,0 +1,6 @@
// `{from}..{to}` copies the elements of one array into another exclusively
function main() -> u32[3] {
let a = [1u32; 4];
return a[0..3]
}

View File

@ -0,0 +1,6 @@
// A spread operator `...` copies the elements of one array into another
function main() -> u32[3] {
let a = [1u32, 1u32];
return [1u32, ...a]
}

View File

@ -0,0 +1,3 @@
function main(b: bool) -> bool{
return b
}

View File

@ -4,7 +4,7 @@ use leo_compiler::errors::{BooleanError, ExpressionError};
use leo_compiler::{
compiler::Compiler,
errors::{CompilerError, FunctionError, StatementError},
ConstrainedValue,
ConstrainedValue, InputValue,
};
use snarkos_curves::{bls12_377::Fr, edwards_bls12::EdwardsProjective};
use snarkos_models::gadgets::utilities::boolean::Boolean;
@ -53,6 +53,24 @@ fn fail_enforce(program: Compiler<Fr, EdwardsProjective>) {
}
}
fn fail_boolean(program: Compiler<Fr, EdwardsProjective>) {
match get_error(program) {
CompilerError::FunctionError(FunctionError::BooleanError(
BooleanError::InvalidBoolean(_string),
)) => {}
error => panic!("Expected invalid boolean error, got {}", error),
}
}
fn fail_synthesis(program: Compiler<Fr, EdwardsProjective>) {
match get_error(program) {
CompilerError::FunctionError(FunctionError::BooleanError(
BooleanError::SynthesisError(_string),
)) => {}
error => panic!("Expected synthesis error, got {}", error),
}
}
#[test]
fn test_true() {
let program = compile_program(DIRECTORY_NAME, "true.leo").unwrap();
@ -65,6 +83,20 @@ fn test_false() {
output_false(program);
}
#[test]
fn test_input_bool_field() {
let mut program = compile_program(DIRECTORY_NAME, "input_bool.leo").unwrap();
program.set_inputs(vec![Some(InputValue::Integer(1usize))]);
fail_boolean(program);
}
#[test]
fn test_input_bool_none() {
let mut program = compile_program(DIRECTORY_NAME, "input_bool.leo").unwrap();
program.set_inputs(vec![None]);
fail_synthesis(program);
}
// Boolean not !
#[test]

View File

@ -0,0 +1,3 @@
function main(f: field) -> field{
return f
}

View File

@ -0,0 +1,76 @@
use crate::{compile_program, get_error, get_output};
use leo_compiler::errors::FieldElementError;
use leo_compiler::{
compiler::Compiler,
errors::{CompilerError, FunctionError},
ConstrainedValue, FieldElement, InputValue,
};
use snarkos_curves::{bls12_377::Fr, edwards_bls12::EdwardsProjective};
use snarkos_models::curves::Field;
const DIRECTORY_NAME: &str = "tests/field_element/";
fn output_zero(program: Compiler<Fr, EdwardsProjective>) {
let output = get_output(program);
assert_eq!(
ConstrainedValue::<Fr, EdwardsProjective>::Return(vec![ConstrainedValue::FieldElement(
FieldElement::Constant(Fr::zero())
)]),
output
);
}
fn output_one(program: Compiler<Fr, EdwardsProjective>) {
let output = get_output(program);
assert_eq!(
ConstrainedValue::<Fr, EdwardsProjective>::Return(vec![ConstrainedValue::FieldElement(
FieldElement::Constant(Fr::one())
)]),
output
);
}
fn fail_field(program: Compiler<Fr, EdwardsProjective>) {
match get_error(program) {
CompilerError::FunctionError(FunctionError::FieldElementError(
FieldElementError::InvalidField(_string),
)) => {}
error => panic!("Expected invalid field error, got {}", error),
}
}
fn fail_synthesis(program: Compiler<Fr, EdwardsProjective>) {
match get_error(program) {
CompilerError::FunctionError(FunctionError::FieldElementError(
FieldElementError::SynthesisError(_string),
)) => {}
error => panic!("Expected synthesis error, got {}", error),
}
}
#[test]
fn test_zero() {
let program = compile_program(DIRECTORY_NAME, "zero.leo").unwrap();
output_zero(program);
}
#[test]
fn test_one() {
let program = compile_program(DIRECTORY_NAME, "one.leo").unwrap();
output_one(program);
}
#[test]
fn test_input_field_bool() {
let mut program = compile_program(DIRECTORY_NAME, "input_field.leo").unwrap();
program.set_inputs(vec![Some(InputValue::Boolean(true))]);
fail_field(program);
}
#[test]
fn test_input_field_none() {
let mut program = compile_program(DIRECTORY_NAME, "input_field.leo").unwrap();
program.set_inputs(vec![None]);
fail_synthesis(program);
}

View File

@ -0,0 +1,3 @@
function main() -> field {
return 1field
}

View File

@ -0,0 +1,3 @@
function main() -> field {
return 0field
}

View File

@ -0,0 +1,23 @@
use crate::{compile_program, get_output};
use leo_compiler::{compiler::Compiler, ConstrainedValue};
use snarkos_curves::{bls12_377::Fr, edwards_bls12::EdwardsProjective};
use snarkos_models::curves::Group;
const DIRECTORY_NAME: &str = "tests/group_element/";
fn output_zero(program: Compiler<Fr, EdwardsProjective>) {
let output = get_output(program);
assert_eq!(
ConstrainedValue::<Fr, EdwardsProjective>::Return(vec![ConstrainedValue::GroupElement(
EdwardsProjective::zero()
)]),
output
);
}
#[test]
fn test_zero() {
let program = compile_program(DIRECTORY_NAME, "zero.leo").unwrap();
output_zero(program);
}

View File

@ -0,0 +1,3 @@
function main() -> group {
return 0group
}

View File

@ -0,0 +1 @@
pub mod u32;

View File

@ -0,0 +1,3 @@
function main(x: u32) -> u32 {
return x
}

View File

@ -1,15 +1,12 @@
use crate::{
compile_program,
// get_error,
get_output,
};
use crate::{compile_program, get_error, get_output};
use leo_compiler::{compiler::Compiler, types::Integer, ConstrainedValue};
use leo_compiler::{compiler::Compiler, types::Integer, ConstrainedValue, InputValue};
use leo_compiler::errors::{CompilerError, FunctionError, IntegerError};
use snarkos_curves::{bls12_377::Fr, edwards_bls12::EdwardsProjective};
use snarkos_models::gadgets::utilities::uint32::UInt32;
const DIRECTORY_NAME: &str = "tests/integers/u32/";
const DIRECTORY_NAME: &str = "tests/integer/u32/";
fn output_zero(program: Compiler<Fr, EdwardsProjective>) {
let output = get_output(program);
@ -41,6 +38,38 @@ fn output_two(program: Compiler<Fr, EdwardsProjective>) {
)
}
fn fail_integer(program: Compiler<Fr, EdwardsProjective>) {
match get_error(program) {
CompilerError::FunctionError(FunctionError::IntegerError(
IntegerError::InvalidInteger(_string),
)) => {}
error => panic!("Expected invalid boolean error, got {}", error),
}
}
fn fail_synthesis(program: Compiler<Fr, EdwardsProjective>) {
match get_error(program) {
CompilerError::FunctionError(FunctionError::IntegerError(
IntegerError::SynthesisError(_string),
)) => {}
error => panic!("Expected synthesis error, got {}", error),
}
}
#[test]
fn test_input_u32_bool() {
let mut program = compile_program(DIRECTORY_NAME, "input_u32.leo").unwrap();
program.set_inputs(vec![Some(InputValue::Boolean(true))]);
fail_integer(program);
}
#[test]
fn test_input_u32_none() {
let mut program = compile_program(DIRECTORY_NAME, "input_u32.leo").unwrap();
program.set_inputs(vec![None]);
fail_synthesis(program);
}
#[test]
fn test_zero() {
let program = compile_program(DIRECTORY_NAME, "zero.leo").unwrap();

View File

@ -1,169 +0,0 @@
pub mod u32;
// use crate::compile_program;
//
// use leo_compiler::{
// ConstrainedValue,
// compiler::Compiler,
// errors::{
// CompilerError,
// FunctionError,
// StatementError
// }
// };
// use snarkos_curves::{bls12_377::Fr, edwards_bls12::EdwardsProjective};
// use snarkos_models::gadgets::{
// r1cs::TestConstraintSystem
// };
// use snarkos_models::gadgets::utilities::boolean::Boolean;
// use leo_compiler::errors::{ExpressionError, BooleanError};
//
// const DIRECTORY_NAME: &str = "tests/integers/";
//
// fn get_output(program: Compiler<Fr, EdwardsProjective>) -> ConstrainedValue<Fr, EdwardsProjective>{
// let mut cs = TestConstraintSystem::<Fr>::new();
// let output = program.compile_constraints(&mut cs).unwrap();
// assert!(cs.is_satisfied());
// output
// }
//
// fn output_true(program: Compiler<Fr, EdwardsProjective>) {
// let output = get_output(program);
// assert_eq!(
// ConstrainedValue::<Fr, EdwardsProjective>::Return(vec![ConstrainedValue::Boolean(
// Boolean::Constant(true)
// )]),
// output
// );
// }
//
// fn output_false(program: Compiler<Fr, EdwardsProjective>) {
// let output = get_output(program);
// assert_eq!(
// ConstrainedValue::<Fr, EdwardsProjective>::Return(vec![ConstrainedValue::Boolean(
// Boolean::Constant(false)
// )]),
// output
// );
// }
//
// fn get_error(program: Compiler<Fr, EdwardsProjective>) -> CompilerError {
// let mut cs = TestConstraintSystem::<Fr>::new();
// program.compile_constraints(&mut cs).unwrap_err()
// }
//
// fn fail_evaluate(program: Compiler<Fr, EdwardsProjective>) {
// match get_error(program) {
// CompilerError::FunctionError(
// FunctionError::StatementError(
// StatementError::ExpressionError(
// ExpressionError::BooleanError(
// BooleanError::CannotEvaluate(_string))))) => {},
// error => panic!("Expected evaluate error, got {}", error),
// }
// }
//
// fn fail_enforce(program: Compiler<Fr, EdwardsProjective>) {
// match get_error(program) {
// CompilerError::FunctionError(
// FunctionError::StatementError(
// StatementError::ExpressionError(
// ExpressionError::BooleanError(
// BooleanError::CannotEnforce(_string))))) => {},
// error => panic!("Expected evaluate error, got {}", error),
// }
// }
//
// #[test]
// fn test_true() {
// let program = compile_program(DIRECTORY_NAME, "true.leo").unwrap();
// output_true(program);
// }
//
// #[test]
// fn test_false() {
// let program = compile_program(DIRECTORY_NAME, "false.leo").unwrap();
// output_false(program);
// }
//
// // Boolean not !
//
// #[test]
// fn test_not_true() {
// let program = compile_program(DIRECTORY_NAME, "not_true.leo").unwrap();
// output_false(program);
// }
//
// #[test]
// fn test_not_false() {
// let program = compile_program(DIRECTORY_NAME, "not_false.leo").unwrap();
// output_true(program);
// }
//
// #[test]
// fn test_not_u32() {
// let program = compile_program(DIRECTORY_NAME, "not_u32.leo").unwrap();
// fail_evaluate(program);
// }
//
// // Boolean or ||
//
// #[test]
// fn test_true_or_true() {
// let program = compile_program(DIRECTORY_NAME, "true_||_true.leo").unwrap();
// output_true(program);
// }
//
// #[test]
// fn test_true_or_false() {
// let program = compile_program(DIRECTORY_NAME, "true_||_false.leo").unwrap();
// output_true(program);
// }
//
// #[test]
// fn test_false_or_false() {
// let program = compile_program(DIRECTORY_NAME, "false_||_false.leo").unwrap();
// output_false(program);
// }
//
// #[test]
// fn test_true_or_u32() {
// let program = compile_program(DIRECTORY_NAME, "true_||_u32.leo").unwrap();
// fail_enforce(program);
// }
//
// // Boolean and &&
//
// #[test]
// fn test_true_and_true() {
// let program = compile_program(DIRECTORY_NAME, "true_&&_true.leo").unwrap();
// output_true(program);
// }
//
// #[test]
// fn test_true_and_false() {
// let program = compile_program(DIRECTORY_NAME, "true_&&_false.leo").unwrap();
// output_false(program);
// }
//
// #[test]
// fn test_false_and_false() {
// let program = compile_program(DIRECTORY_NAME, "false_&&_false.leo").unwrap();
// output_false(program);
// }
//
// #[test]
// fn test_true_and_u32() {
// let program = compile_program(DIRECTORY_NAME, "true_&&_u32.leo").unwrap();
// fail_enforce(program);
// }
//
// // All
//
// #[test]
// fn test_all() {
// let program = compile_program(DIRECTORY_NAME, "all.leo").unwrap();
// output_false(program);
// }
//
//

View File

@ -1,5 +1,8 @@
pub mod array;
pub mod boolean;
pub mod integers;
pub mod field_element;
pub mod group_element;
pub mod integer;
pub mod mutability;
use leo_compiler::{compiler::Compiler, errors::CompilerError, ConstrainedValue};

View File

@ -14,7 +14,6 @@ const DIRECTORY_NAME: &str = "tests/mutability/";
fn mut_success(program: Compiler<Fr, EdwardsProjective>) {
let mut cs = TestConstraintSystem::<Fr>::new();
let output = program.compile_constraints(&mut cs).unwrap();
println!("{}", output);
assert!(cs.is_satisfied());
assert_eq!(