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

acir_field's FieldElement::is_negative invalid in some cases #5174

Closed
michaeljklein opened this issue Jun 4, 2024 · 1 comment · Fixed by #5214
Closed

acir_field's FieldElement::is_negative invalid in some cases #5174

michaeljklein opened this issue Jun 4, 2024 · 1 comment · Fixed by #5214
Labels
bug Something isn't working

Comments

@michaeljklein
Copy link
Contributor

Aim

FieldElement::is_negative should be correct in all cases

Expected Behavior

I expect FieldElement::is_negative to be correct in all cases

Bug

acvm-repo/acir_field/src/field_element.rs

    fn is_negative(&self) -> bool {
        self.neg().num_bits() < self.num_bits()

As far as I can tell, this will be incorrect when the field element is in the middle of its range.

Instead, I believe we should compare without num_bits, i.e. self.neg() < self

To Reproduce

No response

Project Impact

None

Impact Context

No response

Workaround

None

Workaround Description

No response

Additional Context

No response

Installation Method

None

Nargo Version

No response

NoirJS Version

No response

Would you like to submit a PR for this Issue?

None

Support Needs

No response

@michaeljklein michaeljklein added the bug Something isn't working label Jun 4, 2024
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Jun 4, 2024
@TomAFrench
Copy link
Member

I've had a little look into this and the logic of this function is correct for how we're using it.

The only place we're using it is when converting a field element into an i128 and so we're going to have values in the ranges 0..2^127-1 (representing positive integers) and p-2^127..p (representing negative integers). If we were to use field elements outside of this range we would get invalid results anyway.

That said, this method is devoid of any context as it is which opens it up to being misused in future. We should just inline it into to_i128 as a result.

github-merge-queue bot pushed a commit that referenced this issue Jun 10, 2024
# Description

## Problem\*

Resolves #5174

## Summary\*

This PR inlines `FieldElement.is_negative` into `FieldElement.to_i128`
so it can't be misused elsewhere while documenting the reasoning behind
it.

## Additional Context



## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
@github-project-automation github-project-automation bot moved this from 📋 Backlog to ✅ Done in Noir Jun 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants