Skip to content

Commit

Permalink
review: markups
Browse files Browse the repository at this point in the history
  • Loading branch information
0xaatif committed Oct 11, 2024
1 parent adeaa4e commit 512b693
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 44 deletions.
32 changes: 19 additions & 13 deletions trace_decoder/src/tries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ fn mpt_key_into_hash() {
/// Bounded sequence of bits,
/// used as a key for [`StateSmt`].
///
/// Semantically equivalent to
/// Semantically equivalent to [`smt_trie::bits::Bits`].
#[derive(Clone, Copy)]
pub struct SmtKey {
bits: bitvec::array::BitArray<[u8; 32]>,
Expand Down Expand Up @@ -255,7 +255,7 @@ impl SmtKey {
impl From<Address> for SmtKey {
fn from(addr: Address) -> Self {
let H256(bytes) = keccak_hash::keccak(addr);
Self::new(BitArray::<_>::new(bytes)).unwrap()
Self::new(BitArray::<_>::new(bytes)).expect("SmtKey has room for 256 bits")
}
}

Expand Down Expand Up @@ -503,7 +503,6 @@ impl StateTrie for StateSmt {
}

impl StateSmt {
#[deprecated = "this should only be called from the frontend parsing"]
pub(crate) fn new_unchecked(
address2state: BTreeMap<Address, AccountRlp>,
hashed_out: BTreeMap<SmtKey, H256>,
Expand Down Expand Up @@ -552,18 +551,25 @@ mod conv_hash {

use ethereum_types::H256;
use itertools::Itertools as _;
use plonky2::{field::goldilocks_field::GoldilocksField, hash::hash_types::HashOut};
use plonky2::{
field::{
goldilocks_field::GoldilocksField,
types::{Field as _, PrimeField64},
},
hash::hash_types::HashOut,
};

/// # Panics
/// - On certain inputs if `debug_assertions` are enabled. See
/// [`GoldilocksField::from_canonical_u64`] for more.
pub fn eth2smt(H256(bytes): H256) -> smt_trie::smt::HashOut {
let mut bytes = bytes.into_iter();
// (no unsafe, no unstable)
let ret = HashOut {
elements: array::from_fn(|_ix| {
let (a, b, c, d, e, f, g, h) = bytes.next_tuple().unwrap();
// REVIEW(0xaatif): what endianness?
// do we want the `canonical_u64` methods like
// the frontend uses?
GoldilocksField(u64::from_be_bytes([a, b, c, d, e, f, g, h]))
GoldilocksField::from_canonical_u64(u64::from_be_bytes([a, b, c, d, e, f, g, h]))
}),
};
assert_eq!(bytes.len(), 0);
Expand All @@ -573,11 +579,9 @@ mod conv_hash {
H256(
build_array::ArrayBuilder::from_iter(
elements
.into_iter()
// REVIEW(0xaatif): what endianness?
// do we want the `canonical_u64` methods
// like the frontend uses?
.flat_map(|GoldilocksField(u)| u.to_be_bytes()),
.iter()
.map(GoldilocksField::to_canonical_u64)
.flat_map(u64::to_be_bytes),
)
.build_exact()
.unwrap(),
Expand All @@ -586,10 +590,12 @@ mod conv_hash {

#[test]
fn test() {
use plonky2::field::types::Field64 as _;
let mut max = std::iter::repeat(GoldilocksField::ORDER - 1).flat_map(u64::to_be_bytes);
for h in [
H256::zero(),
H256(array::from_fn(|ix| ix as u8)),
H256([u8::MAX; 32]),
H256(array::from_fn(|_| max.next().unwrap())),
] {
assert_eq!(smt2eth(eth2smt(h)), h);
}
Expand Down
59 changes: 28 additions & 31 deletions trace_decoder/src/type2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,39 +117,36 @@ fn node2trie(node: Node) -> anyhow::Result<StateSmt> {
let mut hashes = BTreeMap::new();
let mut leaves = BTreeMap::new();
visit(&mut hashes, &mut leaves, Stack::new(), node)?;
Ok(
#[expect(deprecated, reason = "this is the frontend")]
StateSmt::new_unchecked(
leaves
.into_iter()
.map(
|(
Ok(StateSmt::new_unchecked(
leaves
.into_iter()
.map(
|(
addr,
CollatedLeaf {
balance,
nonce,
// TODO(0xaatif): https://github.com/0xPolygonZero/zk_evm/issues/707
// we shouldn't ignore these fields
code: _,
code_length: _,
storage: _,
},
)| {
(
addr,
CollatedLeaf {
balance,
nonce,
// TODO(0xaatif): https://github.com/0xPolygonZero/zk_evm/issues/707
// we shouldn't ignore these fields
code: _,
code_length: _,
storage: _,
AccountRlp {
nonce: nonce.unwrap_or_default(),
balance: balance.unwrap_or_default(),
storage_root: H256::zero(),
code_hash: H256::zero(),
},
)| {
(
addr,
AccountRlp {
nonce: nonce.unwrap_or_default(),
balance: balance.unwrap_or_default(),
storage_root: H256::zero(),
code_hash: H256::zero(),
},
)
},
)
.collect(),
hashes,
),
)
)
},
)
.collect(),
hashes,
))
}

fn visit(
Expand Down

0 comments on commit 512b693

Please sign in to comment.