Understanding Ethereum Cryptography (2024)

Understanding the Basics of Cryptography: How Private and Public Keys are Generated

Understanding Ethereum Cryptography (3)

Ethereum has two different kinds of accounts: externally owned accounts (EOAs) and contracts. Ownership of ether is established through private keys, Ethereum addresses, and digital signatures. The private keys are at the centre of all user interactions with Ethereum. In fact, account addresses are derived directly from private keys: which uniquely determine a single Ethereum address, also known as an account.

A private key is a randomly-generated number that allows a user to control all funds associated with a corresponding Ethereum address, including access to contracts that authorize the address. The private key is used to create digital signatures that confirm the ownership of funds in a transaction.

It is crucial to keep the private key secure at all times, as revealing it to anyone would give them control over the ether and contracts. It is important to create a backup of the key and protect it from accidental loss, as it cannot be recovered if lost, resulting in the permanent loss of funds secured by it.

Generating a private key from a random number

The first and most crucial step in generating keys is to find a secure source of entropy, or randomness. Generating an Ethereum private key involves selecting a number between 1 and 2²⁵⁶. Ethereum software uses the operating system’s random number generator to generate 256 random bits.

A private key can be any non-zero number up to a large number slightly less than 2²⁵⁶ — a massive 78-digit number, approximately 1.158 * 10⁷⁷.

To generate a private key, it is important to do it offline and not communicate with the Ethereum network. It is not advisable to use a “simple” random number generator offered by a programming language or to write your own code to create a random number. This is because such methods may not provide true randomness and could potentially be exploited by attackers. It is best to use a secure method, such as the operating system’s random number generator, to ensure the randomness and security of the private key.

An Ethereum public key is a set of x and y coordinates on an elliptic curve that satisfy the elliptic curve equation. It is derived from two numbers that are generated from the private key using elliptic curve multiplication. This process is irreversible, meaning that the private key cannot be derived from the public key.

To generate the public key, the private key is used in an equation involving elliptic curve multiplication, which is irreversible. The equation is: K = k * G, where K is the public key, k is the private key, and G is the constant point (generator point).

Elliptic curve multiplication is different from normal multiplication. It is a one-way function that is easy to perform in one direction (multiplication) but impossible to do in the reverse direction (division). This makes it a useful tool for generating a public key from a private key, as it ensures that the private key cannot be derived from the public key.Elliptic Curve Cryptography

Asymmetric or public-key cryptography is based on the concept of the discrete algorithm problem, which involves performing addition and multiplication on the points of an elliptic curve. This type of cryptography, known as elliptic curve cryptography, is a promising potential form of cryptography that may be used in the future.

Ethereum uses the same elliptic curve, secp256k1, as Bitcoin.

Understanding Ethereum Cryptography (4)

Ethereum uses a specific elliptic curve and set of mathematical constants defined in a standard called secp256k1. The secp256k1 curve is defined by the following function, which generates an elliptic curve:

y ² = ( x³ + 7 ) over ( 𝔽 p )or:y ² |p| = ( x³ + 7 ) |p|

The mod p indicates that this curve is over a finite field of prime order p, where p = 2²⁵⁶–2³²–2⁹–2⁸–2⁷–2⁶–2⁴–1. This is a very large prime number, which makes this curve very reliable.

Ethereum address formats

Ethereum addresses are created by taking the Keccak-256 hash of the public key and representing it as a hexadecimal number. The last 20 bytes of the Keccak-256 hash are used to generate the address.

You can install the helpeth command-line tool using npm to create ICAP addresses.

npm install -g helpeth

If you don’t have npm, you may have to install it by following the instructions at https://nodejs.org.

helpeth keyGenerate

Output:

Address: 0xe16c1623c1aa7d919cd2241d8b36d9e79c1be2a2Address (checksum): 0xe16C1623c1AA7D919cd2241d8b36d9E79C1Be2A2ICAP: XE56 QBY1 TCEL SB4U 7BTR QQRC IOUY 8UNS DK2Public key: 0xaa931f5ee58735270821b3722866d8882d1948909532cf8ac2b3ef144ae8043363d1d3728b49f10c7cd78c38289c8012477473879f3b53169f2a677b7fbed0c7Private key: 0x227dbb8586117d55284e26620bc76534dfbd2394be34cf4a09cb775d593b6f2b

We will now use the given private key to generate a public key, as shown below:

helpeth keyDetails \
-p 0x227dbb8586117d55284e26620bc76534dfbd2394be34cf4a09cb775d593b6f2b

Output:

Address: 0xe16c1623c1aa7d919cd2241d8b36d9e79c1be2a2Address (checksum): 0xe16C1623c1AA7D919cd2241d8b36d9E79C1Be2A2ICAP: XE56 QBY1 TCEL SB4U 7BTR QQRC IOUY 8UNS DK2Public key: 0xaa931f5ee58735270821b3722866d8882d1948909532cf8ac2b3ef144ae8043363d1d3728b49f10c7cd78c38289c8012477473879f3b53169f2a677b7fbed0c7

“The Interchange Client Address Protocol (ICAP) provides a flexible, checksummed, and interoperable encoding system for Ethereum addresses. ICAP addresses can encode Ethereum addresses or common names registered with an Ethereum name registry. The compatibility of ICAP with the International Bank Account Number (IBAN) encoding makes it an useful tool for encoding Ethereum addresses.”

Manual method

Manually calculating a public key from a private key using the Elliptic Curve Digital Signature Algorithm (ECDSA) and the secp256k1 curve is a complex process that is best left to specialized libraries or tools. However, for those who are interested in the mathematical details, the formula for deriving the public key is as follows:

Public Key = Private Key * G

Where G is a fixed generator point on the secp256k1 curve, and Private Key is the 256-bit private key in hexadecimal format.

The coordinates of this point are specified in the standard and widely used in most of the implementations. The coordinates of Gin secp256k1 are:

x = 55066263022277343669578718895168534326250603453777594175500187360389116729240
y = 32670510020758816978083085130507043184471273380659243275938904335757337482424

This point is used as the base point for generating public keys. It is also used as a reference point to perform mathematical operations on other points on the curve.

This operation is performed using the mathematical operations defined by the secp256k1 curve, such as point addition and scalar multiplication. Please note that the above formula is a high-level representation of the process, the actual computation is much more complex and usually done by specialized libraries or tools.

[1]: Mastering Ethereum: Building Smart Contracts and DApps by Andreas M. Antonopoulos, Gavin Wood.

Understanding Ethereum Cryptography (2024)
Top Articles
Latest Posts
Article information

Author: Prof. An Powlowski

Last Updated:

Views: 6272

Rating: 4.3 / 5 (64 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Prof. An Powlowski

Birthday: 1992-09-29

Address: Apt. 994 8891 Orval Hill, Brittnyburgh, AZ 41023-0398

Phone: +26417467956738

Job: District Marketing Strategist

Hobby: Embroidery, Bodybuilding, Motor sports, Amateur radio, Wood carving, Whittling, Air sports

Introduction: My name is Prof. An Powlowski, I am a charming, helpful, attractive, good, graceful, thoughtful, vast person who loves writing and wants to share my knowledge and understanding with you.