Generate Ethereum Address from Private Key Python
Updated: Feb 15
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.
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 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.
About secp256k1: Public Key from Private Key
The shape of the Curve
Secp256k1 defines the shape of the elliptic curve y^2 = x^3 + y (mod p) where p is the prime number 115792089237316195423570985008687907853269984665640564039457584007908834671663
Or 2^256 – 2^32 – 977
The prime p should not be confused with the order of the curve. Not every value 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, we can plug the numbers into 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.
The public key is simply the private key multiplied by the point G on the secp256k1 elliptic curve. That's it.
Check out our advanced blockchain bootcamp today and become a blockchain developer who knows the hard stuff other coders don't.