Skip to content

Add conversion function for EVM address type (20 bytes) to AccountId #2229

@partylikeits1983

Description

@partylikeits1983

An Ethereum address is 20 bytes (160 bits).

The Miden AccountId is represented by two field elements: prefix & suffix:

prefix: [hash (56 bits) | storage mode (2 bits) | type (2 bits) | version (4 bits)]
suffix: [zero bit | hash (55 bits) | 8 zero bits]

So while technically the AccountId can be represented by 120 bits (the last 8 bits of the suffix are set to 0), for the purpose of this document it's easier to think of a 128-bit representation.

A Miden AccountId can be naturally embedded into an Ethereum address by setting the top 32 bits (4 bytes) to zero:
0x00000000 || prefix(8) || suffix(8).

A concrete example:

Representation Value Notes
Ethereum address 0x00000000b0E79c68cafC54802726C6F102Cca300 Last two hex nibbles are 00
Miden address (hex) 0xb0E79c68cafC54802726C6F102Cca3 Trailing 00 nibbles truncated by AccountId::to_hex()

Conversion to AccountId

EthAddressFormat::to_account_id() succeeds only when bytes[0..4] are all zero (the "Miden-embedded"
prefix). The remaining 16 bytes encode the AccountId:

prefix = bytes[4..12]  (big-endian u64)
suffix = bytes[12..20] (big-endian u64)
AccountId::try_from([prefix_felt, suffix_felt])

The reverse conversion (from_account_id) embeds the AccountId's prefix and suffix as
BE-bytes 4–11 and 12-19, respectively, with bytes 0–3 set to zero.

Required Functions

For the agglayer contracts we need Rust implementations of:

  1. AccountId → EVM address (bytes20): EthAddressFormat::from_account_id(account_id: AccountId)
  2. EVM address → AccountId: EthAddressFormat::to_account_id() -> AccountId
  3. EVM address → Vec: EthAddressFormat::to_elements() -> Vec<Felt> Convert EVM address to field elements (used in constructing NoteStorage)

Metadata

Metadata

Labels

agglayerPRs or issues related to AggLayer bridging integration

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions