hotshot_contract_adapter/
lib.rs

1//! Cross-domain (between Solidity and Rust) utilities for type conversion and testing
2
3use alloy::primitives::U256;
4use ark_ff::{BigInteger, PrimeField};
5
6#[allow(dead_code)]
7pub(crate) mod bindings;
8pub mod evm;
9pub mod jellyfish;
10pub mod light_client;
11pub mod sol_types;
12pub mod stake_table;
13
14/// convert a field element to U256, panic if field size is larger than 256 bit
15pub fn field_to_u256<F: PrimeField>(f: F) -> U256 {
16    if F::MODULUS_BIT_SIZE > 256 {
17        panic!("Shouldn't convert a >256-bit field to U256");
18    }
19    U256::from_le_slice(&f.into_bigint().to_bytes_le())
20}
21
22/// convert U256 to a field (mod order)
23pub fn u256_to_field<F: PrimeField>(x: U256) -> F {
24    let bytes: [u8; 32] = x.to_le_bytes();
25    F::from_le_bytes_mod_order(&bytes)
26}