Skip to content

Commit

Permalink
Merge pull request privacy-scaling-explorations#56 from appliedzkp/ev…
Browse files Browse the repository at this point in the history
…m_word_conversion

Implement byte-conversion for `EvmWord` & `MemoryAddress`
  • Loading branch information
CPerezz authored Sep 1, 2021
2 parents 2d24653 + e9e54f6 commit 18339c8
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion bus-mapping/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ impl MemoryAddress {
pub fn zero() -> MemoryAddress {
MEM_ADDR_ZERO.clone()
}

/// Return the little-endian byte representation of the word as a 32-byte
/// array.
pub fn to_bytes(&self) -> [u8; 32] {
let mut array = [0u8; 32];
array.copy_from_slice(&self.0.to_bytes_le());

array
}
}

impl From<MemoryAddress> for BigUint {
Expand Down Expand Up @@ -152,4 +161,20 @@ macro_rules! impl_from_basic_types {
};
}

impl_from_basic_types!(u8, u16, u32, u64, u128);
impl_from_basic_types!(u8, u16, u32, u64, u128, usize);

impl EvmWord {
/// Return the little-endian byte representation of the word as a 32-byte
/// array.
pub fn to_bytes(&self) -> [u8; 32] {
let mut array = [0u8; 32];
array.copy_from_slice(&self.0.to_bytes_le());

array
}

/// Returns the underlying representation of the `EvmWord` as a [`BigUint`].
pub fn as_big_uint(&self) -> &BigUint {
&self.0
}
}

0 comments on commit 18339c8

Please sign in to comment.