Skip to content

Commit

Permalink
Rename utility methods for U256 conversion
Browse files Browse the repository at this point in the history
Co-authored-by: Nicholas Ward <[email protected]>
  • Loading branch information
Nashtare and npwardberkeley committed Sep 18, 2023
1 parent f3ea95c commit b5c28bd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
24 changes: 12 additions & 12 deletions evm/src/get_challenges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::permutation::{
get_n_grand_product_challenge_sets_target,
};
use crate::proof::*;
use crate::util::{h256_limbs, u256_limbs, u256_lowest_limb, u256_lowest_word};
use crate::util::{h256_limbs, u256_limbs, u256_to_u32, u256_to_u64};
use crate::witness::errors::ProgramError;

fn observe_root<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>, const D: usize>(
Expand Down Expand Up @@ -61,15 +61,15 @@ fn observe_block_metadata<
challenger.observe_elements(
&u256_limbs::<F>(U256::from_big_endian(&block_metadata.block_beneficiary.0))[..5],
);
challenger.observe_element(u256_lowest_limb(block_metadata.block_timestamp)?);
challenger.observe_element(u256_lowest_limb(block_metadata.block_number)?);
challenger.observe_element(u256_lowest_limb(block_metadata.block_difficulty)?);
challenger.observe_element(u256_lowest_limb(block_metadata.block_gaslimit)?);
challenger.observe_element(u256_lowest_limb(block_metadata.block_chain_id)?);
let basefee = u256_lowest_word(block_metadata.block_base_fee)?;
challenger.observe_element(u256_to_u32(block_metadata.block_timestamp)?);
challenger.observe_element(u256_to_u32(block_metadata.block_number)?);
challenger.observe_element(u256_to_u32(block_metadata.block_difficulty)?);
challenger.observe_element(u256_to_u32(block_metadata.block_gaslimit)?);
challenger.observe_element(u256_to_u32(block_metadata.block_chain_id)?);
let basefee = u256_to_u64(block_metadata.block_base_fee)?;
challenger.observe_element(basefee.0);
challenger.observe_element(basefee.1);
challenger.observe_element(u256_lowest_limb(block_metadata.block_gas_used)?);
challenger.observe_element(u256_to_u32(block_metadata.block_gas_used)?);
for i in 0..8 {
challenger.observe_elements(&u256_limbs(block_metadata.block_bloom[i]));
}
Expand Down Expand Up @@ -106,10 +106,10 @@ fn observe_extra_block_data<
challenger: &mut Challenger<F, C::Hasher>,
extra_data: &ExtraBlockData,
) -> Result<(), ProgramError> {
challenger.observe_element(u256_lowest_limb(extra_data.txn_number_before)?);
challenger.observe_element(u256_lowest_limb(extra_data.txn_number_after)?);
challenger.observe_element(u256_lowest_limb(extra_data.gas_used_before)?);
challenger.observe_element(u256_lowest_limb(extra_data.gas_used_after)?);
challenger.observe_element(u256_to_u32(extra_data.txn_number_before)?);
challenger.observe_element(u256_to_u32(extra_data.txn_number_after)?);
challenger.observe_element(u256_to_u32(extra_data.gas_used_before)?);
challenger.observe_element(u256_to_u32(extra_data.gas_used_after)?);
for i in 0..8 {
challenger.observe_elements(&u256_limbs(extra_data.block_bloom_before[i]));
}
Expand Down
16 changes: 8 additions & 8 deletions evm/src/recursive_verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use crate::proof::{
TrieRootsTarget,
};
use crate::stark::Stark;
use crate::util::{h256_limbs, u256_limbs, u256_lowest_limb, u256_lowest_word};
use crate::util::{h256_limbs, u256_limbs, u256_to_u32, u256_to_u64};
use crate::vanishing_poly::eval_vanishing_poly_circuit;
use crate::vars::StarkEvaluationTargets;
use crate::witness::errors::ProgramError;
Expand Down Expand Up @@ -1009,31 +1009,31 @@ where
witness.set_target_arr(&block_metadata_target.block_beneficiary, &beneficiary_limbs);
witness.set_target(
block_metadata_target.block_timestamp,
u256_lowest_limb(block_metadata.block_timestamp)?,
u256_to_u32(block_metadata.block_timestamp)?,
);
witness.set_target(
block_metadata_target.block_number,
u256_lowest_limb(block_metadata.block_number)?,
u256_to_u32(block_metadata.block_number)?,
);
witness.set_target(
block_metadata_target.block_difficulty,
u256_lowest_limb(block_metadata.block_difficulty)?,
u256_to_u32(block_metadata.block_difficulty)?,
);
witness.set_target(
block_metadata_target.block_gaslimit,
u256_lowest_limb(block_metadata.block_gaslimit)?,
u256_to_u32(block_metadata.block_gaslimit)?,
);
witness.set_target(
block_metadata_target.block_chain_id,
u256_lowest_limb(block_metadata.block_chain_id)?,
u256_to_u32(block_metadata.block_chain_id)?,
);
// Basefee fits in 2 limbs
let basefee = u256_lowest_word(block_metadata.block_base_fee)?;
let basefee = u256_to_u64(block_metadata.block_base_fee)?;
witness.set_target(block_metadata_target.block_base_fee[0], basefee.0);
witness.set_target(block_metadata_target.block_base_fee[1], basefee.1);
witness.set_target(
block_metadata_target.block_gas_used,
u256_lowest_limb(block_metadata.block_gas_used)?,
u256_to_u32(block_metadata.block_gas_used)?,
);
let mut block_bloom_limbs = [F::ZERO; 64];
for (i, limbs) in block_bloom_limbs.chunks_exact_mut(8).enumerate() {
Expand Down
4 changes: 2 additions & 2 deletions evm/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub fn trace_rows_to_poly_values<F: Field, const COLUMNS: usize>(

/// Returns the lowest LE 32-bit limb of a `U256` as a field element,
/// and errors if the integer is actually greater.
pub(crate) fn u256_lowest_limb<F: Field>(u256: U256) -> Result<F, ProgramError> {
pub(crate) fn u256_to_u32<F: Field>(u256: U256) -> Result<F, ProgramError> {
if TryInto::<u32>::try_into(u256).is_err() {
return Err(ProgramError::IntegerTooLarge);
}
Expand All @@ -59,7 +59,7 @@ pub(crate) fn u256_lowest_limb<F: Field>(u256: U256) -> Result<F, ProgramError>

/// Returns the lowest LE 64-bit word of a `U256` as two field elements
/// each storing a 32-bit limb, and errors if the integer is actually greater.
pub(crate) fn u256_lowest_word<F: Field>(u256: U256) -> Result<(F, F), ProgramError> {
pub(crate) fn u256_to_u64<F: Field>(u256: U256) -> Result<(F, F), ProgramError> {
if TryInto::<u64>::try_into(u256).is_err() {
return Err(ProgramError::IntegerTooLarge);
}
Expand Down

0 comments on commit b5c28bd

Please sign in to comment.