Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

noirc_abi cannot parse negative integer strings from JSON #6637

Closed
TomAFrench opened this issue Nov 27, 2024 · 6 comments
Closed

noirc_abi cannot parse negative integer strings from JSON #6637

TomAFrench opened this issue Nov 27, 2024 · 6 comments
Assignees

Comments

@TomAFrench
Copy link
Member

I noticed that we're handling signed integers differently for JSON files compared to TOML.

(
TomlTypes::String(string),
AbiType::Field
| AbiType::Integer { sign: crate::Sign::Unsigned, .. }
| AbiType::Boolean,
) => InputValue::Field(parse_str_to_field(&string)?),
(TomlTypes::String(string), AbiType::Integer { sign: crate::Sign::Signed, width }) => {
InputValue::Field(parse_str_to_signed(&string, *width)?)
}
(
TomlTypes::Integer(integer),
AbiType::Field | AbiType::Integer { .. } | AbiType::Boolean,
) => {
let new_value = FieldElement::from(i128::from(integer));
InputValue::Field(new_value)
}

(
JsonTypes::String(string),
AbiType::Field | AbiType::Integer { .. } | AbiType::Boolean,
) => InputValue::Field(parse_str_to_field(&string)?),
(
JsonTypes::Integer(integer),
AbiType::Field | AbiType::Integer { .. } | AbiType::Boolean,
) => {
let new_value = FieldElement::from(i128::from(integer));
InputValue::Field(new_value)
}

That is we don't have handling for negative integer strings in JSON. This means that the WASM ABI encoder can't handle these inputs which is bad.

We currently have proptests for ABI encoding but we should probably extend these to cover the input parsing and fix any issues which arise.

@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Nov 27, 2024
@TomAFrench TomAFrench changed the title noirc_abi cannot parse negative integer strings noirc_abi cannot parse negative integer strings from JSON Nov 27, 2024
@TomAFrench
Copy link
Member Author

I've scoped out the tests necessary in #6638 but I'd appreciate others taking this up.

@asterite
Copy link
Collaborator

asterite commented Nov 28, 2024

Oh, I started working on this before it was assigned to @guipublic .

So far what I found is that parse_str_to_signed has a bug in it. In this line:

BigInt::from(2).pow(width) + bigint

we are turning, say, -1, to fit in i8, so we end up with 2*8 - 1 = 255. However the maximum value of i8 is 127, so the math should be BigInt::from(2).pow(width -1) + bigint.

Then the tests signed_integer_serialization_roundtrip will always fail for negative numbers because output_number will always be positive. We need to do a different check when it's negative.

I'll try to send a PR if I manage to fix this.

@TomAFrench
Copy link
Member Author

You've written the same expression as the code you're saying is incorrect but I would say that 255 is correct here as it's the two's complement of -1 which is how we represent signed integers.

@asterite
Copy link
Collaborator

Oh, I see what you mean. I'll rollback that commit, or fix it and the fix the test.

@asterite
Copy link
Collaborator

I think I got it right this time 🤞

@asterite
Copy link
Collaborator

asterite commented Dec 2, 2024

Closed by #6638

@asterite asterite closed this as completed Dec 2, 2024
@github-project-automation github-project-automation bot moved this from 📋 Backlog to ✅ Done in Noir Dec 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: ✅ Done
Development

No branches or pull requests

3 participants