Skip to content

Commit

Permalink
fix: from field with constant values (#4226)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves a small issue when trying to cast a constant value from a field
element.

## Summary\*

We were trying to do a modulo 2^{254} which does not fit in u128

## Additional Context



## Documentation\*

Check one:
- [X] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[Exceptional Case]** 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.
  • Loading branch information
guipublic authored Feb 1, 2024
1 parent 8e042f2 commit 593916b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
3 changes: 3 additions & 0 deletions compiler/noirc_evaluator/src/ssa/ir/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,9 @@ impl Instruction {
None
}
Instruction::Truncate { value, bit_size, max_bit_size } => {
if bit_size == max_bit_size {
return SimplifiedTo(*value);
}
if let Some((numeric_constant, typ)) = dfg.get_numeric_constant_with_type(*value) {
let integer_modulus = 2_u128.pow(*bit_size);
let truncated = numeric_constant.to_u128() % integer_modulus;
Expand Down
2 changes: 1 addition & 1 deletion test_programs/execution_success/u128/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ fn main(mut x: u32, y: u32, z: u32, big_int: U128, hexa: str<7>) {
assert(shift >> small_int == small_int);
assert(shift >> U128::from_integer(127) == U128::from_integer(0));
assert(shift << U128::from_integer(127) == U128::from_integer(0));

assert(U128::from_integer(3).to_integer() == 3);
}

0 comments on commit 593916b

Please sign in to comment.