Skip to content

Commit

Permalink
fix exp as u32
Browse files Browse the repository at this point in the history
  • Loading branch information
xunilrj committed Oct 19, 2023
1 parent d554f71 commit 2d8f20f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
14 changes: 6 additions & 8 deletions sway-lib-std/src/math.sw
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,19 @@ impl Power for u256 {
/// # Panics
///
/// Panics if the result overflows the type.
fn pow(self, exp: Self) -> Self {
fn pow(self, exp: u32) -> Self {
let one = 0x0000000000000000000000000000000000000000000000000000000000000001u256;

if exp == 0x0000000000000000000000000000000000000000000000000000000000000000u256 {
if exp == 0 {
return one;
}

let mut exp = exp;
let mut base = self;
let mut acc = one;

while exp > one {
if (exp & one) == one {
while exp > 1 {
if (exp & 1) == 1 {
acc = acc * base;
}
exp = exp >> 1;
Expand Down Expand Up @@ -197,14 +197,12 @@ impl BinaryLogarithm for u8 {
#[test]
fn u256_pow_tests() {
let five = 0x0000000000000000000000000000000000000000000000000000000000000005u256;
let two = 0x0000000000000000000000000000000000000000000000000000000000000002u256;
let twenty_eight = 0x000000000000000000000000000000000000000000000000000000000000001Cu256; // 28

use ::assert::*;

// 5^2 = 25 = 0x19
assert_eq(five.pow(two), 0x0000000000000000000000000000000000000000000000000000000000000019u256);
assert_eq(five.pow(2), 0x0000000000000000000000000000000000000000000000000000000000000019u256);

// 5^28 = 0x204FCE5E3E2502611 (see https://www.wolframalpha.com/input?i=convert+5%5E28+in+hex)
assert_eq(five.pow(twenty_eight), 0x0000000000000000204FCE5E3E2502611u256);
assert_eq(five.pow(28), 0x0000000000000000204FCE5E3E2502611u256);
}
17 changes: 7 additions & 10 deletions sway-lib-std/src/u256.sw
Original file line number Diff line number Diff line change
Expand Up @@ -685,20 +685,20 @@ impl Power for U256 {
/// # Panics
///
/// Panics if the result overflows the type.
fn pow(self, expon: Self) -> Self {
fn pow(self, expon: u32) -> Self {
let one = U256::from((0, 0, 0, 1));
let two = U256::from((0, 0, 0, 2));

if expon.is_zero() {
if expon == 0 {
return one;
}

let mut exp = expon;
let mut base = self;
let mut acc = one;

while exp > one {
if (exp & one) == one {
while exp > 1 {
if (exp & 1) == 1 {
acc = acc * base;
}
exp = exp >> 1;
Expand All @@ -717,9 +717,8 @@ fn is_even(x: U256) -> bool {
#[test]
fn test_five_pow_two_u256() {
let five = U256::from((0, 0, 0, 5));
let two = U256::from((0, 0, 0, 2));

let five_pow_two = five.pow(two);
let five_pow_two = five.pow(2);
assert(five_pow_two.a == 0);
assert(five_pow_two.b == 0);
assert(five_pow_two.c == 0);
Expand All @@ -729,9 +728,8 @@ fn test_five_pow_two_u256() {
#[test]
fn test_five_pow_three_u256() {
let five = U256::from((0, 0, 0, 5));
let three = U256::from((0, 0, 0, 3));

let five_pow_three = five.pow(three);
let five_pow_three = five.pow(3);
assert_eq(five_pow_three.a, 0);
assert_eq(five_pow_three.b, 0);
assert_eq(five_pow_three.c, 0);
Expand All @@ -741,9 +739,8 @@ fn test_five_pow_three_u256() {
#[test]
fn test_five_pow_28_u256() {
let five = U256::from((0, 0, 0, 5));
let twenty_eight = U256::from((0, 0, 0, 28));

let five_pow_28 = five.pow(twenty_eight);
let five_pow_28 = five.pow(28);
assert_eq(five_pow_28.a, 0);
assert_eq(five_pow_28.b, 0);
assert_eq(five_pow_28.c, 2);
Expand Down

0 comments on commit 2d8f20f

Please sign in to comment.