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;
8mod copy;
9pub mod evm;
10pub mod jellyfish;
11pub mod light_client;
12pub mod sol_types;
13pub mod stake_table;
14
15/// convert a field element to U256, panic if field size is larger than 256 bit
16pub fn field_to_u256<F: PrimeField>(f: F) -> U256 {
17    if F::MODULUS_BIT_SIZE > 256 {
18        panic!("Shouldn't convert a >256-bit field to U256");
19    }
20    U256::from_le_slice(&f.into_bigint().to_bytes_le())
21}
22
23/// convert U256 to a field (mod order)
24pub fn u256_to_field<F: PrimeField>(x: U256) -> F {
25    let bytes: [u8; 32] = x.to_le_bytes();
26    F::from_le_bytes_mod_order(&bytes)
27}