641: Bug/564 input array len not enforced r=collinc97 a=gluax

Resolves #564, depends on #563. The changes for this branch have pulled from PR #638, which is the PR that fixes #563.

Co-authored-by: gluaxspeed <jonathan.t.pavlik@gmail.com>
This commit is contained in:
bors[bot] 2021-02-11 21:41:53 +00:00 committed by GitHub
commit fa53e75f51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 35 additions and 3 deletions

View File

@ -25,7 +25,7 @@ use pest::Span;
use std::fmt;
#[derive(Clone, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum InputValue {
Address(String),
Boolean(bool),

View File

@ -93,6 +93,15 @@ impl FunctionError {
Self::new_from_span(message, span)
}
pub fn invalid_input_array_dimensions(expected: usize, actual: usize, span: Span) -> Self {
let message = format!(
"Input array dimensions mismatch expected {}, found array dimensions {}",
expected, actual
);
Self::new_from_span(message, span)
}
pub fn invalid_tuple(actual: String, span: Span) -> Self {
let message = format!("Expected function input tuple, found `{}`", actual);

View File

@ -41,6 +41,14 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
match input_value {
Some(InputValue::Array(arr)) => {
if array_len != arr.len() {
return Err(FunctionError::invalid_input_array_dimensions(
arr.len(),
array_len,
span.clone(),
));
}
// Allocate each value in the current row
for (i, value) in arr.into_iter().enumerate() {
let value_name = format!("{}_{}", &name, &i.to_string());

View File

@ -37,7 +37,6 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
let mut members = Vec::with_capacity(section.len());
// Allocate each section definition as a circuit member value
for (parameter, option) in section.into_iter() {
let section_members = expected_type.members.borrow();
let expected_type = match section_members.get(&parameter.variable.name) {

View File

@ -0,0 +1,2 @@
[main]
x: [i16; 1] = [0i16; 1];

View File

@ -1,3 +1,4 @@
function main (x: [i16; 2]) {
function main (x: [i16; 1]) {
console.log("{}", x);
console.assert(x[0] == 0);
}

View File

@ -0,0 +1,3 @@
function main(x: [i16; 2]){
console.log("x: {}", x);
}

View File

@ -83,3 +83,13 @@ fn test_input_multiple() {
assert_satisfied(program);
}
#[test]
fn test_input_array_dimensions_mismatch() {
let program_string = include_str!("main_array_fail.leo");
let input_string = include_str!("input/main_array_fail.in");
let program = parse_program_with_input(program_string, input_string).unwrap();
expect_fail(program);
}