Skip to content

Commit

Permalink
fix(katana-primitives): split_u256 (#1793)
Browse files Browse the repository at this point in the history
fix split_u256
  • Loading branch information
greged93 authored Apr 8, 2024
1 parent e974ee6 commit 297a44c
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions crates/katana/primitives/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<u128>();
let high_u128: u128 = U256::from(value >> 128).to::<u128>();
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));
}
}

0 comments on commit 297a44c

Please sign in to comment.