From 74be1ac188859436a74cad9831d4e073a91aa36f Mon Sep 17 00:00:00 2001 From: Marcus Liotta Date: Thu, 9 Nov 2023 16:29:55 +0100 Subject: [PATCH] adds alternative implementation for rotate_left on u256 --- src/math/src/lib.cairo | 8 ++------ src/math/src/tests/math_test.cairo | 10 ++++------ 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/src/math/src/lib.cairo b/src/math/src/lib.cairo index cbceb32b..b29d91e1 100644 --- a/src/math/src/lib.cairo +++ b/src/math/src/lib.cairo @@ -221,12 +221,8 @@ impl U128BitRotate of BitRotate { impl U256BitRotate of BitRotate { fn rotate_left(x: u256, n: u256) -> u256 { - // TODO(sveamarcus): missing non-zero implementation for u512 - // let word = u256_wide_mul(x, pow(2, n)); - // let (quotient, remainder) = DivRem::div_rem(word, - // u512_as_non_zero(u512{limb0: 0, limb1: 0, limb2: 1, limb3: 0 })); - // (quotient + remainder).try_into().unwrap() - panic_with_felt252('missing impl') + // Alternative solution since we cannot divide u512 yet + BitShift::shl(x, n) + BitShift::shr(x, 256 - n) } fn rotate_right(x: u256, n: u256) -> u256 { diff --git a/src/math/src/tests/math_test.cairo b/src/math/src/tests/math_test.cairo index 95aca929..d90bef4d 100644 --- a/src/math/src/tests/math_test.cairo +++ b/src/math/src/tests/math_test.cairo @@ -170,27 +170,25 @@ fn shl_should_not_overflow() { } #[test] -#[available_gas(2000000)] +#[available_gas(3000000)] fn test_rotl_min() { assert(BitRotate::rotate_left(pow::(2, 7) + 1, 1) == 3, 'invalid result'); assert(BitRotate::rotate_left(pow::(2, 15) + 1, 1) == 3, 'invalid result'); assert(BitRotate::rotate_left(pow::(2, 31) + 1, 1) == 3, 'invalid result'); assert(BitRotate::rotate_left(pow::(2, 63) + 1, 1) == 3, 'invalid result'); assert(BitRotate::rotate_left(pow::(2, 127) + 1, 1) == 3, 'invalid result'); -// TODO(sveamarcus): missing implementation -// assert(BitRotate::rotate_left(pow::(2, 255) + 1, 1) == 3, 'invalid result'); + assert(BitRotate::rotate_left(pow::(2, 255) + 1, 1) == 3, 'invalid result'); } #[test] -#[available_gas(2000000)] +#[available_gas(3000000)] fn test_rotl_max() { assert(BitRotate::rotate_left(0b101, 7) == pow::(2, 7) + 0b10, 'invalid result'); assert(BitRotate::rotate_left(0b101, 15) == pow::(2, 15) + 0b10, 'invalid result'); assert(BitRotate::rotate_left(0b101, 31) == pow::(2, 31) + 0b10, 'invalid result'); assert(BitRotate::rotate_left(0b101, 63) == pow::(2, 63) + 0b10, 'invalid result'); assert(BitRotate::rotate_left(0b101, 127) == pow::(2, 127) + 0b10, 'invalid result'); -// TODO(sveamarcus): missing implementation -// assert(BitRotate::rotate_left(0b101, 255) == pow::(2, 255) + 0b10, 'invalid result'); + assert(BitRotate::rotate_left(0b101, 255) == pow::(2, 255) + 0b10, 'invalid result'); } #[test]