Skip to content

Commit

Permalink
fix: respect struct ordering when abi encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Apr 24, 2023
1 parent dba5054 commit 1126a3c
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions crates/noirc_abi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ impl Abi {
return Err(AbiError::TypeMismatch { param, value });
}

Self::encode_value(value).map(|v| (param_name, v))
Self::encode_value(value, &expected_type).map(|v| (param_name, v))
})
.collect::<Result<_, _>>()?;

Expand All @@ -251,7 +251,7 @@ impl Abi {
value: return_value,
});
}
let encoded_return_fields = Self::encode_value(return_value)?;
let encoded_return_fields = Self::encode_value(return_value, return_type)?;

// We need to be more careful when writing the return value's witness values.
// This is as it may share witness indices with other public inputs so we must check that when
Expand All @@ -276,7 +276,7 @@ impl Abi {
Ok(witness_map)
}

fn encode_value(value: InputValue) -> Result<Vec<FieldElement>, AbiError> {
fn encode_value(value: InputValue, abi_type: &AbiType) -> Result<Vec<FieldElement>, AbiError> {
let mut encoded_value = Vec::new();
match value {
InputValue::Field(elem) => encoded_value.push(elem),
Expand All @@ -286,11 +286,14 @@ impl Abi {
string.bytes().map(|byte| FieldElement::from_be_bytes_reduce(&[byte]));
encoded_value.extend(str_as_fields);
}
InputValue::Struct(object) => {
for value in object.into_values() {
encoded_value.extend(Self::encode_value(value)?);
InputValue::Struct(object) => match abi_type {
AbiType::Struct { fields } => {
for (field, typ) in fields {
encoded_value.extend(Self::encode_value(object[field].clone(), typ)?);
}
}
}
_ => unreachable!("value does not match type"),
},
}
Ok(encoded_value)
}
Expand Down

0 comments on commit 1126a3c

Please sign in to comment.