Cryptography
Last updated
Last updated
Ownership of ether by EOAs is established with digital private keys, an Ethereum address and digital signatures.
The private key is one of the most important concepts of cryptography in ethereum. The private key can also be used to derive an Ethereum address -> an account.
Private keys are not directly used in the Ethereum system in any way. They are never transmitted or stored. They shouldn't appear in messages nor should they be passed around in the network.
Access and control of funds is done with digital signatures. The digital signatures are created using the private key.
Public key cryptography relies on special mathematical functions that have a specific property: they are easy to calculate, but hard to reverse-engineer
One of the advanced category of mathematical functions that Ethereum uses is based on artithmetic operations on an elliptic curve
The elliptic curve used by Ethereum is called secp256k1 curve
The equation for the curve is y^2 = x^3 +7
In elliptic curves, adding two points results in a third new point (that intersects the curve). After getting that third point, it is then reflected across the x axis.
In order for it to be even more secure, a starting point P is added to itself to receive a new point, which is then reflected:
Doing the above step 10 times can be calculated in four addition operations.
P+P = 2โขP
2โขP+2โขP = 4โขP
4โขP+4โขP = 8โขP
2โขP+8โขP=10โขP
How many steps would it take to compute xโขP, where x is a random 256-bit integer? In this case, x can range anywhere from 0 to 1.1579209e+77
Computing P would never require more than 510 point addition operations
There is no known algorithm or computer that could calculate this. Even if the calculations are started in the middle of the operations, on average it would still take about 2^128 point addition operations.
The problem with the above shown elliptic curve is that some of the coordinates might end up being too large to be stored in a standard 512-bit public key.
yยฒ = xยณ+ax+b
is transformed to
yยฒ mod p = (xยณ + ax + b) mod p.
X=xโขP, where x is a random 256-bit integer, how can you prove to someone that you know the x that corresponds to X without revealing any useful information about x?
We can use the point addition property for the modified equation:
hash(m, rโขP)โขnโขP+rโขP = (hash(m, rโขP)*n+r)โขP
After simplifying to hash(m, R)โขX+R = sโขP, we are left with the fact that if you can provide an m, R and s that satisfy the above equation, then this proves that you know the x corresponding to the X in x.P = X equation.
Digital signatures
A specific message can be made so that it is required for the verification to be succesful. We can use the m, R and s to form a digital signature for that message. Usually, the message is the unsigned part of a transaction. Generally, the digital signature for a transaction is the x-coordinate of R concatenated with s.
Private keysPrivate keys are basically very long unsigned numbers that are picked at random. The private keys must be kept secret at all costs, because their exposal means giving access to all of the ether on that particular account, as well as access to all of the smart contracts.
How are private keys in Ethereum generated?The Ethereum key is basically just a randomly generated number. It is basically between 1 and 2 ^256. Ethereum's software uses the underlying OS to generate 256 random bits.โ This is usually achieved by getting a long set of characters and feeding it to a hashing function, usually keccak256 or sha256, both of which produce a 256 bit output. We then check if the number is within the suitable range and if not - repeat the process once again.โ The process of generating a random number is offline. The random number generator is not done by using a pseudo-random function such as rand in most languages.
Public keysA public key in ethereum is a point on a so called elliptic curve. The one used by ethereum is a standard followed by the USITS.The public key is generated by using the following equationK = k * g, where k is the private key and G is some constant point that is called the generator point.This results in some point that is impossible to trace back to.The generator point is specified as part of the secp256k1 standard (which is the above mentioned elliptic curve that is used by Ethereum)โ Thisga generator point is the same one for all of the users in Ethereum, meaning that a private key multiplied by the generator point will always result in the same public key K.โ The relationship of k (private key) to K one directional and can only be calculated one way. Thats why the public keys can be freely shared between users without the worry of exposing a private key.