Skip to content

Commit

Permalink
Revert when overflow using pow for u8, u16, u32 (#6340)
Browse files Browse the repository at this point in the history
## Description
The current implementation for pow does not check if the value has
overflowed above the max value that can be held by the type

## Checklist

- [ ] I have linked to any relevant issues.
- [ ] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [ ] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [ ] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: K1-R1 <77465250+K1-R1@users.noreply.github.com>
  • Loading branch information
SwayStar123 and K1-R1 authored Aug 9, 2024
1 parent 3ade41c commit 360fcf5
Show file tree
Hide file tree
Showing 3 changed files with 328 additions and 226 deletions.
40 changes: 33 additions & 7 deletions sway-lib-std/src/math.sw
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
library;

use ::assert::*;
use ::flags::{disable_panic_on_overflow, F_UNSAFEMATH_DISABLE_MASK, set_flags};
use ::flags::{
disable_panic_on_overflow,
F_UNSAFEMATH_DISABLE_MASK,
F_WRAPPING_DISABLE_MASK,
set_flags,
};
use ::registers::{flags, overflow};

/// Calculates the square root.
Expand Down Expand Up @@ -113,27 +118,48 @@ impl Power for u64 {

impl Power for u32 {
fn pow(self, exponent: u32) -> Self {
asm(r1: self, r2: exponent, r3) {
let res = asm(r1: self, r2: exponent, r3) {
exp r3 r1 r2;
r3: Self
r3: u64
};
// If panic on wrapping math is enabled, only then revert
if flags() & F_WRAPPING_DISABLE_MASK == 0 {
assert(res <= Self::max().as_u64());
}
asm(r1: res) {
r1: Self
}
}
}

impl Power for u16 {
fn pow(self, exponent: u32) -> Self {
asm(r1: self, r2: exponent, r3) {
let res = asm(r1: self, r2: exponent, r3) {
exp r3 r1 r2;
r3: Self
r3: u64
};
// If panic on wrapping math is enabled, only then revert
if flags() & F_WRAPPING_DISABLE_MASK == 0 {
assert(res <= Self::max().as_u64());
}
asm(r1: res) {
r1: Self
}
}
}

impl Power for u8 {
fn pow(self, exponent: u32) -> Self {
asm(r1: self, r2: exponent, r3) {
let res = asm(r1: self, r2: exponent, r3) {
exp r3 r1 r2;
r3: Self
r3: u64
};
// If panic on wrapping math is enabled, only then revert
if flags() & F_WRAPPING_DISABLE_MASK == 0 {
assert(res <= Self::max().as_u64());
}
asm(r1: res) {
r1: Self
}
}
}
Expand Down
Loading

0 comments on commit 360fcf5

Please sign in to comment.