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

Remove truncation from Instruction::Cast to allow unsafe casts #3749

Closed
TomAFrench opened this issue Dec 9, 2023 · 0 comments · Fixed by #3946
Closed

Remove truncation from Instruction::Cast to allow unsafe casts #3749

TomAFrench opened this issue Dec 9, 2023 · 0 comments · Fixed by #3946

Comments

@TomAFrench
Copy link
Member

TomAFrench commented Dec 9, 2023

I've been doing some thinking about how we can perform "unsafe maths" to avoid all those nasty overflow checks and similar.

In general how this is being done is by converting to Field type, doing your operations and then converting back to the desired type. This runs into the trouble that converting back to the original type will automatically include an overflow check despite the developer knowing that it's unnecessary.

As an example where this is useful, see: https://github.com/shuklaayush/noir-bigint/blob/9a876290c9f75058dc53e74d25da5b3d79ada0d8/crates/biguint/src/utils.nr#L6-L21

In the line (ret as u112 >> BITS as u112) as u56 we're performing 2 casts (BITS as u112 gets handled at compile time so doesn't count) which automatically includes 2 truncations despite the fact that we know that these casts are valid and will not cause an overflow in the target type. If we had an unsafe cast then we could simplify this down to a single division rather than it having 3.

In order to do this we need to remove the truncation logic from a cast so that doing x as u8 would be represented in SSA as two instructions (a truncation and then a cast). Cast instructions would then not be represented in the ACIR at all but only exist to change the types of values in SSA.

Once this is done we can add some form of unsafe_cast function to the stdlib which would emit just a cast instruction without the truncation.

@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Dec 9, 2023
github-merge-queue bot pushed a commit that referenced this issue Jan 8, 2024
…struction (#3946)

# Description

## Problem\*

Resolves #3749

## Summary\*

This PR removes the implicit `Instruction::Truncate` which lives inside
`Instruction::Cast` so that it's purely responsible for casting the type
of the value in SSA rather than massaging the input to fit in the
desired type.

## Additional Context



## Documentation\*

Check one:
- [ ] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[Exceptional Case]** Documentation to be submitted in a separate
PR.

# PR Checklist\*

- [ ] I have tested the changes locally.
- [ ] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.

---------

Co-authored-by: jfecher <[email protected]>
@github-project-automation github-project-automation bot moved this from 📋 Backlog to ✅ Done in Noir Jan 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

1 participant