Generate Ethereum Address from Private Key Python
Tags: solidity, ethereum, cryptography, python
Published: 2023-01-31 04:43:48

## Generate Ethereum Address from Public Key
An ethereum address is the last 20 bytes of the keccack256 of the public key. The public key algorithm is secp256k1, the same used in bitcoin.

Because it is an elliptic curve algorithm, the public key is an (x, y) pair corresponds to a point on the elliptic curve.

## Generate the Elliptic Curve Public Key.
The public key is the concatenation of x and y, and that is what we take the hash of.

The code is provided below.

```python
from ecpy.curves import Curve
from ecpy.keys import ECPublicKey, ECPrivateKey
from sha3 import keccak_256

private_key = 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80

cv = Curve.get_curve('secp256k1')
pv_key = ECPrivateKey(private_key, cv)
pu_key = pv_key.get_public_key()

# equivalent alternative for illustration:
# concat_x_y = bytes.fromhex(hex(pu_key.W.x)[2:] + hex(pu_key.W.y)[2:])

concat_x_y = pu_key.W.x.to_bytes(32, byteorder='big') + pu_key.W.y.to_bytes(32, byteorder='big')
eth_addr = '0x' + keccak_256(concat_x_y).digest()[-20:].hex()

print('private key: ', hex(private_key))
print('eth_address: ', eth_addr)
```

The ecpy library is here https://github.com/cslashm/ECPy. This library implements the elliptic curve math in python, so it won’t be as fast as a wrapper around the bitcoin C implementation, which is used by the [coincurve](https://github.com/ofek/coincurve/) library.

However, the python implementation allows you to see step by step the elliptic curve math used to derive the public key.

You can use this code to generate an ethereum vanity address with brute force, but be mindful that if your source of randomness is not secure or has too few bits of randomness, you may fall victim to a hack similar to [this](https://cointelegraph.com/news/almost-1m-in-crypto-stolen-from-vanity-address-exploit).

## About secp256k1: Public Key from Private Key
### The shape of the Curve
Secp256k1 defines the shape of the elliptic curve y² = x³ + y (mod p) where p is the prime number 115792089237316195423570985008687907853269984665640564039457584007908834671663

Or 2²⁵⁶ — 2³² — 977

The prime p should not be confused with the order of the curve. Not every value n in 0 < n < p satisfies the equation above. However, operations on the curve are guaranteed to be closed. That is, if two valid points are added or multiplied, the result will be a valid number on the curve.

### The starting point
The other important parameter in secp256k1 is the starting point G. Since G is a point on the elliptic curve, it is 2-dimensional and has the parameters

x = 55066263022277343669578718895168534326250603453777594175500187360389116729240
y = 32670510020758816978083085130507043184471273380659243275938904335757337482424

To see G is a valid point, you can plug the numbers into python

```python
x = 55066263022277343669578718895168534326250603453777594175500187360389116729240
y = 32670510020758816978083085130507043184471273380659243275938904335757337482424
p = 115792089237316195423570985008687907853269984665640564039457584007908834671663
assert pow(y, 2, p) == (pow(x, 3) + 7) % p
```

To create a public / private key pair, a random number s is created (this is the secret key). The point G is added to itself s times and the new point (x, y) is the public key. It is not feasible to derive s from G and (x, y) if s is sufficiently large.

Signing messages in this scheme boils down to demonstrating you know s without revealing it.

Adding G to itself s times is the same as multiplying s * G. In fact, we can see this operation at a lower level in by stripping away some of the abstractions the library is providing.

```python
from ecpy.curves import Curve
from sha3 import keccak_256

private_key = 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80

cv     = Curve.get_curve('secp256k1')
pu_key = private_key * cv.generator # just multiplying the private key by generator point (EC multiplication)

concat_x_y = pu_key.x.to_bytes(32, byteorder='big') + pu_key.y.to_bytes(32, byteorder='big')
eth_addr = '0x' + keccak_256(concat_x_y).digest()[-20:].hex()

print('private key: ', hex(private_key))
print('eth_address: ', eth_addr)
```

The public key is simply the private key multiplied by the point G on the secp256k1 elliptic curve. That’s it.

## Learn More
Check out our advanced [blockchain bootcamp](https://www.rareskills.io/web3-blockchain-bootcamps) today and become a blockchain developer who knows the hard stuff other coders don’t.