[Back] In Ethereum, we use ECC (Elliptic Curve Cryptography) to create a public key and a private key. The private key is kept secure, and then the public key is used to derive the Ethereum address. When we sign for a transaction, we use our private key, and to create a signature, and which is automatically checked against our Ethereum address. In creating the keys (for our wallet) we generate a 256-bit private key, and then the public key is a point on the secp256k1 ECDSA curve (x,y point). This key is then hashed using Keccak-256 (aka SHA-3), and the lower 160 bits becomes the public Ethereum address:

## Ethereum Address Generation |

## Theory

In Ethereum, we use ECC (Elliptic Curve Cryptography) to create a public key and a private key. The private key is kept secure, and then the public key is used to derive the Ethereum address. When we sign for a transaction, we use our private key, and to create a signature, and which is automatically checked against our Ethereum address.

In creating the keys (for our wallet) we generate a 256-bit private key, and then the public key is a point on the secp256k1 ECDSA curve (x,y point). This key is then hashed using Keccak-256 (aka SHA-3), and the lower 160 bits becomes the public Ethereum address :

The private key should be completely random, and there are 2²⁵⁶ different keys that can be generated, so the chance of us matching a private key to a public key is:

1 in 1,550 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 … 000 (75 zeros — 1.55 quattuorvigintillion)An outline of the code is:

import codecs import ecdsa from Crypto.Hash import keccak import os private_key_bytes = os.urandom(32) key = ecdsa.SigningKey.from_string(private_key_bytes, curve=ecdsa.SECP256k1).verifying_key key_bytes = key.to_string() private_key = codecs.encode(private_key_bytes, 'hex') public_key = codecs.encode(key_bytes, 'hex') print "Private key: ",private_key print "Public key: ",public_key public_key_bytes = codecs.decode(public_key, 'hex') hash = keccak.new(digest_bits=256) hash.update(public_key_bytes) keccak_digest = hash.hexdigest() address = '0x' + keccak_digest[-40:] print "Address:",address

A sample run is:

Private key: 18dd1dcd752466afa3d1fac1424333c6461c3a0f1d6702e9c45bc9254ec74e5f Public key: bdfb71e2d953406c45279ac434667a6a1ea9fae608af91e7f6bfb0792011df760895a528e8b83622886039b4803b6182d708fb40a16919bddaef84493ef1d4cf Address: 0xaae47eae4ddd4877e0ae0bc780cfaee3cc3b52cb