DEV Community

Discussion on: February 17th, 2022: What did you learn this week?

Collapse
 
kayis profile image
K • Edited

I'm currently creating a Web3 course and so I read about elliptic curve cryptography. I wanted to understand what the keys in Ethereum actually are.

ECC is based on elliptic curves; basically, math functions on a 2D space (x/y).

ECC on Ethereum uses a particular standard curve, called secp256k1.

This curve is defined by the equation y²=x³+7 mod p or y=sqrt(x³+7) mod p

The curve doesn't use the real numbers, but a fixed set of numbers called a finite field.

A fixed set of numbers, in this case, means there are 115792089237316195423570985008687907853269984665640564039457584007908834671663 numbers in the field that secp256k1 is defined for. This number is the p from the equation above, a large prime number. Crazy big, but still smaller than all real numbers, lol.

The idea here is if you calculate something and it gets greater than p, you get an overflow and go on from 0.

The private key is just a random integer between 0 and p.

This integer defines how often a base point g on that curve has to be added to itself to get the public key. The base point g is also predefined by secp256k1, and since it's a point on that curve, it's an x and y coordinate on the curve, and basically two big integers.

So, if the private key was 5, the public key would be the result of g+g+g+g+g.

The + isn't a common addition but a more complex operation that uses two points (in this case, both g) to calculate a new point on the curve.

So, the public key would be a point, or an x and y coordinate on that y²=x³+7 mod p curve. Both x and y will be integers smaller than p because of overflow etc.

So to sum all up.

The private key is a random 256bit integer smaller than that big prime I mentioned.

The public key is two integers, essentially x and y coordinates, defining a point on the secp256k1 curve. You get this point by executing a special kind of addition on a predefined base point "private key times".

Collapse
 
nickytonline profile image
Nick Taylor

That's awesome! 🔥

Yes, that's awesome!