From 297a44ce56542cdc91b3a25ad99b1872b1ce0e9d Mon Sep 17 00:00:00 2001 From: greged93 <82421016+greged93@users.noreply.github.com> Date: Mon, 8 Apr 2024 10:39:26 -0300 Subject: [PATCH] fix(katana-primitives): split_u256 (#1793) fix split_u256 --- crates/katana/primitives/src/utils/mod.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/crates/katana/primitives/src/utils/mod.rs b/crates/katana/primitives/src/utils/mod.rs index 3a1684749f..9c1591434b 100644 --- a/crates/katana/primitives/src/utils/mod.rs +++ b/crates/katana/primitives/src/utils/mod.rs @@ -9,7 +9,25 @@ pub mod transaction; /// The first element in the returned tuple is the low part, and the second element is the high /// part. pub fn split_u256(value: U256) -> (FieldElement, FieldElement) { - let low_u128: u128 = value.to::(); - let high_u128: u128 = U256::from(value >> 128).to::(); + let low_u128: u128 = (value & U256::from(u128::MAX)).to(); + let high_u128: u128 = U256::from(value >> 128).to(); (FieldElement::from(low_u128), FieldElement::from(high_u128)) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_split_u256() { + // Given + let value = U256::MAX; + + // When + let (low, high) = split_u256(value); + + // Then + assert_eq!(low, FieldElement::from(u128::MAX)); + assert_eq!(high, FieldElement::from(u128::MAX)); + } +}