add .in and .state tests

This commit is contained in:
collin 2020-07-30 18:17:55 -07:00
parent e62a2a066a
commit 02833b80e0
26 changed files with 283 additions and 33 deletions

View File

@ -2,7 +2,6 @@
use crate::{
errors::FunctionError,
function::check_arguments_length,
program::{new_scope, ConstrainedProgram},
value::ConstrainedValue,
GroupType,
@ -30,9 +29,6 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
match input_value {
Some(InputValue::Array(arr)) => {
// Check the dimension of the array
check_arguments_length(expected_length, arr.len(), span.clone())?;
// Allocate each value in the current row
for (i, value) in arr.into_iter().enumerate() {
let value_name = new_scope(name.clone(), i.to_string());

View File

@ -2,7 +2,6 @@
use crate::{
errors::FunctionError,
function::check_arguments_length,
program::{new_scope, ConstrainedProgram},
GroupType,
OutputBytes,
@ -26,9 +25,6 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
let function_name = new_scope(scope.clone(), function.get_name());
let registers = inputs.get_registers();
// Make sure we are given the correct number of inputs
check_arguments_length(function.inputs.len(), inputs.len(), function.span.clone())?;
// Iterate over main function inputs and allocate new passed-by variable values
let mut input_variables = vec![];
for input_model in function.inputs.clone().into_iter() {

View File

@ -0,0 +1,3 @@
mod program_inputs;
mod program_inputs_and_state;
mod state;

View File

@ -0,0 +1,17 @@
function main(
registers,
state,
record,
state_leaf,
data: u8[32]
) {
assert_eq!(registers.value_balance, 0u64);
assert_eq!(state.leaf_index, 0u32);
assert_eq!(record.value, 5u64);
assert_eq!(state_leaf.network_id, 0u8);
assert_eq!(data, [0u8; 32]);
}

View File

@ -0,0 +1,5 @@
[main]
a: bool = true;
[registers]
b: bool = true;

View File

@ -0,0 +1,12 @@
[[public]]
[state]
a: bool = true;
[[private]]
[record]
a: bool = true;
[state_leaf]
a: bool = true;

View File

@ -0,0 +1,6 @@
[main]
data: u8[32] = [0u8; 32];
[registers]
token_id: u8[32] = [0u8; 32];
value_balance: u64 = 0;

View File

@ -0,0 +1,24 @@
[[public]]
[state]
leaf_index: u32 = 0;
root: u8[32] = [0u8; 32];
[[private]]
[record]
serial_number: u8[32] = [0u8; 32];
commitment: u8[32] = [0u8; 32];
owner: address = aleo1qnr4dkkvkgfqph0vzc3y6z2eu975wnpz2925ntjccd5cfqxtyu8sta57j8;
value: u64 = 5;
payload: u8[32] = [0u8; 32];
birth_program_id: u8[32] = [0u8; 32];
death_program_id: u8[32] = [0u8; 32];
serial_number_nonce: u8[32] = [0u8; 32];
commitment_randomness: u8[32] = [0u8; 32];
[state_leaf]
path: u8[32][2] = [ [0u8; 32], [0u8; 32] ];
memo: u8[32] = [0u8; 32];
network_id: u8 = 0;
leaf_randomness: u8[32] = [0u8; 32];

View File

@ -0,0 +1,28 @@
use crate::{assert_satisfied, parse_inputs_and_state, parse_program_with_inputs_and_state};
#[test]
fn test_basic() {
let inputs_bytes = include_bytes!("inputs/basic.in");
let state_bytes = include_bytes!("inputs/basic.state");
parse_inputs_and_state(inputs_bytes, state_bytes).unwrap();
}
#[test]
fn test_full() {
let inputs_bytes = include_bytes!("inputs/token_withdraw.in");
let state_bytes = include_bytes!("inputs/token_withdraw.state");
parse_inputs_and_state(inputs_bytes, state_bytes).unwrap();
}
#[test]
fn test_access() {
let program_bytes = include_bytes!("access.leo");
let inputs_bytes = include_bytes!("inputs/token_withdraw.in");
let state_bytes = include_bytes!("inputs/token_withdraw.state");
let program = parse_program_with_inputs_and_state(program_bytes, inputs_bytes, state_bytes).unwrap();
assert_satisfied(program);
}

View File

@ -0,0 +1,8 @@
function main(state, record, state_leaf) {
assert_eq!(state.root, [0u8; 32]);
let expected: address = aleo1qnr4dkkvkgfqph0vzc3y6z2eu975wnpz2925ntjccd5cfqxtyu8sta57j8;
assert_eq!(record.owner, expected);
assert_eq!(state_leaf.network_id, 0u8);
}

View File

@ -0,0 +1,3 @@
function main(state) {
assert_eq!(state.root, [0u8; 32]);
}

View File

@ -0,0 +1,10 @@
[[public]]
[state]
root: bool = true;
[[private]]
[record]
id: bool = false;
[state_leaf]
leaf: bool = true;

View File

@ -0,0 +1,4 @@
[[public]]
[record]
a: bool = true;

View File

@ -0,0 +1,4 @@
[[public]]
[invalid]
a: bool = true;

View File

@ -0,0 +1,24 @@
[[public]]
[state]
leaf_index: u32 = 0;
root: u8[32] = [0u8; 32];
[[private]]
[record]
serial_number: u8[32] = [0u8; 32];
commitment: u8[32] = [0u8; 32];
owner: address = aleo1qnr4dkkvkgfqph0vzc3y6z2eu975wnpz2925ntjccd5cfqxtyu8sta57j8;
value: u64 = 5;
payload: u8[32] = [0u8; 32];
birth_program_id: u8[32] = [0u8; 32];
death_program_id: u8[32] = [0u8; 32];
serial_number_nonce: u8[32] = [0u8; 32];
commitment_randomness: u8[32] = [0u8; 32];
[state_leaf]
path: u8[32][2] = [ [0u8; 32], [0u8; 32] ];
memo: u8[32] = [0u8; 32];
network_id: u8 = 0;
leaf_randomness: u8[32] = [0u8; 32];

View File

@ -0,0 +1 @@
[[pure]]

View File

@ -0,0 +1,62 @@
use crate::{assert_satisfied, parse_program_with_state, parse_state};
#[test]
fn test_basic() {
let bytes = include_bytes!("inputs/basic.state");
parse_state(bytes).unwrap();
}
#[test]
fn test_token_withdraw() {
let bytes = include_bytes!("inputs/token_withdraw.state");
parse_state(bytes).unwrap();
}
#[test]
fn test_access_state() {
let program_bytes = include_bytes!("access_state.leo");
let state_bytes = include_bytes!("inputs/token_withdraw.state");
let program = parse_program_with_state(program_bytes, state_bytes).unwrap();
assert_satisfied(program);
}
#[test]
fn test_access_all() {
let program_bytes = include_bytes!("access_all.leo");
let state_bytes = include_bytes!("inputs/token_withdraw.state");
let program = parse_program_with_state(program_bytes, state_bytes).unwrap();
assert_satisfied(program);
}
#[test]
fn test_visibility_fail() {
let state_bytes = include_bytes!("inputs/visibility_fail.state");
let is_err = parse_state(state_bytes).is_err();
assert!(is_err);
}
#[test]
fn test_section_undefined() {
let state_bytes = include_bytes!("inputs/section_undefined.state");
let is_err = parse_state(state_bytes).is_err();
assert!(is_err);
}
#[test]
fn test_section_invalid() {
let state_bytes = include_bytes!("inputs/section_invalid.state");
let is_err = parse_state(state_bytes).is_err();
assert!(is_err);
}

View File

@ -6,7 +6,7 @@ pub mod field;
pub mod function;
pub mod group;
pub mod import;
pub mod inputs;
pub mod input_files;
pub mod integers;
pub mod macros;
pub mod mutability;
@ -34,21 +34,6 @@ const EMPTY_FILE: &str = "";
pub type EdwardsTestCompiler = Compiler<Fq, EdwardsGroupType>;
pub type EdwardsConstrainedValue = ConstrainedValue<Fq, EdwardsGroupType>;
pub fn parse_program_with_inputs(
program_bytes: &[u8],
input_bytes: &[u8],
) -> Result<EdwardsTestCompiler, CompilerError> {
let mut compiler = new_compiler();
let program_string = String::from_utf8_lossy(program_bytes);
let inputs_string = String::from_utf8_lossy(input_bytes);
compiler.parse_inputs(&inputs_string, EMPTY_FILE)?;
compiler.parse_program(&program_string)?;
Ok(compiler)
}
fn new_compiler() -> EdwardsTestCompiler {
let program_name = "test".to_string();
let path = PathBuf::from("/test/src/main.leo");
@ -70,7 +55,76 @@ pub(crate) fn parse_inputs(bytes: &[u8]) -> Result<EdwardsTestCompiler, Compiler
let mut compiler = new_compiler();
let inputs_string = String::from_utf8_lossy(bytes);
compiler.parse_inputs(&inputs_string, "")?;
compiler.parse_inputs(&inputs_string, EMPTY_FILE)?;
Ok(compiler)
}
pub(crate) fn parse_state(bytes: &[u8]) -> Result<EdwardsTestCompiler, CompilerError> {
let mut compiler = new_compiler();
let state_string = String::from_utf8_lossy(bytes);
compiler.parse_inputs(EMPTY_FILE, &state_string)?;
Ok(compiler)
}
pub(crate) fn parse_inputs_and_state(
inputs_bytes: &[u8],
state_bytes: &[u8],
) -> Result<EdwardsTestCompiler, CompilerError> {
let mut compiler = new_compiler();
let inputs_string = String::from_utf8_lossy(inputs_bytes);
let state_string = String::from_utf8_lossy(state_bytes);
compiler.parse_inputs(&inputs_string, &state_string)?;
Ok(compiler)
}
pub fn parse_program_with_inputs(
program_bytes: &[u8],
input_bytes: &[u8],
) -> Result<EdwardsTestCompiler, CompilerError> {
let mut compiler = new_compiler();
let program_string = String::from_utf8_lossy(program_bytes);
let inputs_string = String::from_utf8_lossy(input_bytes);
compiler.parse_inputs(&inputs_string, EMPTY_FILE)?;
compiler.parse_program(&program_string)?;
Ok(compiler)
}
pub fn parse_program_with_state(
program_bytes: &[u8],
state_bytes: &[u8],
) -> Result<EdwardsTestCompiler, CompilerError> {
let mut compiler = new_compiler();
let program_string = String::from_utf8_lossy(program_bytes);
let state_string = String::from_utf8_lossy(state_bytes);
compiler.parse_inputs(EMPTY_FILE, &state_string)?;
compiler.parse_program(&program_string)?;
Ok(compiler)
}
pub fn parse_program_with_inputs_and_state(
program_bytes: &[u8],
inputs_bytes: &[u8],
state_bytes: &[u8],
) -> Result<EdwardsTestCompiler, CompilerError> {
let mut compiler = new_compiler();
let program_string = String::from_utf8_lossy(program_bytes);
let inputs_string = String::from_utf8_lossy(inputs_bytes);
let state_string = String::from_utf8_lossy(state_bytes);
compiler.parse_inputs(&inputs_string, &state_string)?;
compiler.parse_program(&program_string)?;
Ok(compiler)
}
@ -106,13 +160,6 @@ pub(crate) fn expect_synthesis_error(program: EdwardsTestCompiler) {
assert!(!cs.is_satisfied());
}
// pub(crate) fn fail_enforce(program: EdwardsTestCompiler) {
// match get_compiler_error(program) {
// CompilerError::FunctionError(FunctionError::StatementError(StatementError::Error(_))) => {}
// error => panic!("Expected evaluate error, got {}", error),
// }
// }
pub(crate) fn generate_main_inputs(inputs: Vec<(&str, Option<InputValue>)>) -> MainInputs {
let mut main_inputs = MainInputs::new();

View File

@ -76,7 +76,7 @@ type_boolean = { "bool" }
type_address = { "address" }
// Declared in types/data_type.rs
type_data = { type_field | type_group | type_boolean | type_integer }
type_data = { type_field | type_group | type_boolean | type_address | type_integer }
// Declared in types/array_type.rs
type_array = { type_data ~ ("[" ~ value_number ~ "]")+ }