Why Ethereum Addresses Begin with 0x and How Keccak-256 Makes it Possible?

Garvit DadheechGarvit Dadheech
4 min read

In this article I will tell you about the keccak-256 algorithm that is used to create Ethereum public addresses and then we will see why the ethereum addresses starts with 0x.

What is Keccak-256 ?

Keccak-256 is a cryptographic hash function and a member of the Keccak family, which was selected as the basis for the SHA-3 (Secure Hash Algorithm 3) standard by NIST in 2015. It produces a 256-bit (32-byte) hash value from any input size, which is why it’s known as Keccak-256.

Rather than diving into the inner workings of Keccak-256, let’s focus on its essential properties:

  • Collision Resistance: It’s computationally infeasible to find two different inputs with the same hash output.

  • Pre-image Resistance: Given a hash value, it's practically impossible to retrieve the original input.

  • Efficiency: Keccak-256 can operate efficiently on hardware and software, making it suitable for various applications.

Although Keccak-256 is a robust and dependable hashing algorithm, certain precautions are necessary:

  1. Mitigating Collision Risks: Even though collisions are highly unlikely, they are not entirely impossible. To further minimize the probability of collision, it's best to combine multiple unique parameters (such as block hash, timestamp, and contract nonce) when generating hashes.

  2. Defense Against Brute-force Attacks: While brute-force attacks are theoretically possible, using strong passwords or cryptographic keys significantly improves security by making such attacks computationally prohibitive.

To experiment with the Keccak-256 algorithm and see how it generates unique outputs for different inputs, you can visit this online tool.

How Ethereum addresses are generated?

  • When creating an Ethereum address, the process begins with the generation of a public key. This key is produced through elliptic curve cryptography (ECC), specifically using the secp256k1 curve, which is also used in Bitcoin. ECC ensures that public keys can be derived from private keys securely, providing strong cryptographic security for Ethereum addresses.

  • Once the public key is generated, it undergoes hashing with the Keccak-256 algorithm. This hashing step converts the public key into a fixed-size, 32-byte (256-bit) hash, which helps ensure security and simplifies handling by generating a shorter, consistent length output.

  • To derive the Ethereum address from this hash, only the last 20 bytes of the Keccak-256 hash result are taken. This truncation yields a 20-byte (160-bit) value, which is sufficient for uniquely identifying accounts within the Ethereum network. This 20-byte value is then transformed into a 40-character hexadecimal string and prefixed with '0x' to indicate its hexadecimal nature.

  • The resulting address, now in the format of a 42-character string (e.g., 0x32Be343B94f860124dC4fEe278FDCBD38C102D38), is the public-facing identifier used to send and receive Ether (ETH), as well as to interact with smart contracts on the Ethereum network. This address format is unique, compact, and compatible with Ethereum’s account-based model, where addresses serve as primary identifiers for account balances and contract interactions.

  • Here is a picture depicting the whole process.

The entire process of generating Ethereum addresses explains why they have the '0x' prefix. This prefix indicates that the address is represented in hexadecimal format, which is standard for Ethereum addresses.

To verify it further here is the code of ethers.js library where it is computing the public addresses and prefixing with 0x. Don’t forget to check it out for a deeper look into how Ethereum addresses are created!

Note: Solana public addresses are 32 bytes (256 bits) in length and do not require hashing.

This highlights a major difference between Solana and Ethereum address generation. In Ethereum, a public key is hashed with Keccak-256, and the last 20 bytes of the resulting hash are used to create a 20-byte (160-bit) address. In contrast, Solana uses the full 32-byte public key directly as the address, eliminating the need for a hashing step and resulting in a longer address. This difference in address length and creation process is a core distinction between how Ethereum and Solana handle public keys and addresses.

Conclusion

In conclusion, Keccak-256 plays a vital role in creating secure Ethereum addresses by hashing public keys. This process, along with the "0x" prefix, ensures a unique and compact address format. Understanding how different blockchains, like Solana, handle address generation highlights the diversity of security approaches across networks.

1
Subscribe to my newsletter

Read articles from Garvit Dadheech directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Garvit Dadheech
Garvit Dadheech

I am a Full Stack, DevOps, and Web3 Developer with expertise in building end-to-end applications. As a fast learner, I quickly adapt to different technology stacks, ensuring the delivery of robust software solutions. I specialize in creating scalable web applications tailored to meet client needs and drive innovation.