use std::collections::HashMap;
use alloy::primitives::{bytes::Bytes, Uint, U256};
use num_bigint::BigUint;
use tycho_common::simulation::errors::SimulationError;
pub fn u256_to_f64(x: U256) -> Result<f64, SimulationError> {
if x == U256::from(0u64) {
return Ok(0.0);
}
let n_shifts = i32::try_from(x.bit_len())
.map(|x_bits| 53i32 - x_bits)
.map_err(|e| SimulationError::FatalError(format!("Bit length of {x} out of range: {e}")))?;
let mut exponent = u64::try_from(1023 + 52 - n_shifts)
.map_err(|e| SimulationError::FatalError(format!("Exponent for {x} out of range: {e}")))?;
let mut significant = if n_shifts >= 0 {
let shift = n_shifts.unsigned_abs();
let shifted = x << shift;
u64::try_from(shifted).map_err(|_out_of_range| {
SimulationError::FatalError(format!(
"Unable to convert shifted U256 value `{shifted}` into u64 when converting {x}"
))
})?
} else {
let shift = n_shifts.unsigned_abs();
let lsb = (x >> shift) & U256::from(1u64);
let round_bit = (x >> (shift - 1)) & U256::from(1u64);
let sticky_bit = if shift < 2 {
U256::from(0u64)
} else {
x & ((U256::from(1u64) << (shift - 2)) - U256::from(1u64))
};
let rounded_towards_zero_u256 = x >> shift;
let rounded_towards_zero: u64 = rounded_towards_zero_u256.try_into().map_err(|_out_of_range| {
SimulationError::FatalError(format!(
"Unable to convert rounded U256 value `{rounded_towards_zero_u256}` into u64 when converting {x}"
))
})?;
if round_bit == U256::from(1u64) {
if sticky_bit == U256::from(0u64) {
if lsb == U256::from(0u64) {
rounded_towards_zero
} else {
rounded_towards_zero + 1
}
} else {
rounded_towards_zero + 1
}
} else {
rounded_towards_zero
}
};
if significant & (1 << 53) > 0 {
significant >>= 1;
exponent += 1;
}
let merged = (exponent << 52) | (significant & 0xFFFFFFFFFFFFFu64);
Ok(f64::from_bits(merged))
}
#[inline]
pub fn u256_to_biguint(value: U256) -> BigUint {
BigUint::from(value)
}
pub fn biguint_to_uint<const BITS: usize, const LIMBS: usize>(
value: &BigUint,
) -> Result<Uint<BITS, LIMBS>, SimulationError> {
Uint::try_from(value).map_err(|_too_wide| {
SimulationError::InvalidInput(format!("{value} does not fit uint{BITS}"), None)
})
}
#[expect(
clippy::needless_pass_by_value,
reason = "public API; taking the bytes by reference is a breaking change"
)]
pub fn bytes_to_u256(bytes: Bytes) -> U256 {
let mut padded_bytes = [0u8; 32];
let start = 32 - bytes.len().min(32);
padded_bytes[start..].copy_from_slice(&bytes[bytes.len().saturating_sub(32)..]);
U256::from_be_slice(&padded_bytes)
}
pub fn map_slots_to_u256(
slots: HashMap<tycho_common::hex_bytes::Bytes, tycho_common::hex_bytes::Bytes>,
) -> HashMap<U256, U256> {
slots
.into_iter()
.map(|(k, v)| (bytes_to_u256(k.into()), bytes_to_u256(v.into())))
.collect()
}
#[cfg(test)]
mod test {
use rstest::rstest;
use super::*;
#[rstest]
#[case::one(U256::from(1u64), 1.0f64)]
#[case::two(U256::from(2), 2.0f64)]
#[case::zero(U256::from(0u64), 0.0f64)]
#[case::two_pow1024(U256::from(2).pow(U256::from(190)), 2.0f64.powi(190))]
#[case::max32(U256::from_limbs([u32::MAX as u64, 0, 0, 0]), u32::MAX as f64)]
#[case::max64(U256::from_limbs([u64::MAX, 0, 0, 0]), u64::MAX as f64)]
#[case::edge_54bits_trailing_zeros(U256::from(2u64.pow(53)), 2u64.pow(53) as f64)]
#[case::edge_54bits_trailing_ones(U256::from(2u64.pow(54) - 1), (2u64.pow(54) - 1) as f64)]
#[case::edge_53bits_trailing_zeros(U256::from(2u64.pow(52)), 2u64.pow(52) as f64)]
#[case::edge_53bits_trailing_ones(U256::from(2u64.pow(53) - 1), (2u64.pow(53) - 1) as f64)]
fn test_convert(#[case] inp: U256, #[case] out: f64) {
let res = u256_to_f64(inp).expect("convert U256 to f64");
assert_eq!(res, out);
}
#[test]
fn test_u256_biguint_round_trip() {
let values = [
U256::ZERO,
U256::from(1u64),
U256::from(u32::MAX),
U256::from(u64::MAX),
U256::from(u128::MAX),
U256::from_limbs([0, 0, 0, 1]),
U256::from_limbs([0x0123_4567_89ab_cdef, 0, 0xfedc_ba98_7654_3210, 0]),
U256::MAX,
];
for value in values {
let converted = u256_to_biguint(value);
assert_eq!(converted, BigUint::from_bytes_le(&value.to_le_bytes::<32>()));
assert_eq!(biguint_to_uint(&converted).unwrap(), value);
}
}
#[test]
fn test_biguint_to_uint_refuses_values_wider_than_256_bits() {
let max = BigUint::parse_bytes(
b"115792089237316195423570985008687907853269984665640564039457584007913129639935",
10,
)
.unwrap();
assert_eq!(biguint_to_uint(&max).unwrap(), U256::MAX);
let two_pow_256 = BigUint::parse_bytes(
b"115792089237316195423570985008687907853269984665640564039457584007913129639936",
10,
)
.unwrap();
assert!(matches!(
biguint_to_uint::<{ U256::BITS }, { U256::LIMBS }>(&two_pow_256),
Err(SimulationError::InvalidInput(..))
));
}
}