Skip to content

Commit

Permalink
feat: assert maximum bit size when creating a U128 from an integer (#…
Browse files Browse the repository at this point in the history
…4024)

# Description

## Problem\*

Resolves <!-- Link to GitHub Issue -->

## Summary\*

This PR adds a range check to `U128::from_integer` as this method
accepts an arbitrary field and so is unsafe.

## 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
TomAFrench authored Jan 12, 2024
1 parent c9ec0d8 commit 8f9c7e4
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion noir_stdlib/src/uint128.nr
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ impl U128 {

pub fn from_integer<T>(i: T) -> U128 {
let f = crate::as_field(i);
// Reject values which would overflow a u128
f.assert_max_bit_size(128);
let lo = f as u64 as Field;
let hi = (f-lo) / pow64;
U128 {
Expand Down Expand Up @@ -289,4 +291,4 @@ impl Shr for U128 {
}
self / U128::from_integer(y)
}
}
}

0 comments on commit 8f9c7e4

Please sign in to comment.