Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: split_u256 #1793

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}
Loading