DEV Community

CoinMonks
CoinMonks

Posted on • Originally published at Medium on

Crypto Maths

Ethereum One Way Hashing functions explained

Image by author. Quote from here.

Can you represent 8,018,009 as a product of two prime numbers?

You can use whatever calculator or program you like. I’ll wait.

****

I couldn’t do it either. Nor can a computer.

This is the guiding principle behind Crypto Maths.

In this post we’ll go from a private key to an address using all the mathematical functions in between. Much of this comes from Chapter 4 of the ethereumbook.

Public Keys

You’ve heard many definitions of a Public Key. But here’s the real one:

“An Ethereum public key is a point on an elliptic curve, meaning it is a set of x and y coordinates that satisfy the elliptic curve equation.” — ethereumbook.

But you’re probably thinking: What’s an elliptical curve?

Elliptical Curve

Ethereum uses the same elliptical curve as Bitcoin (secp256k1).

Here’s the equation:

y² mod(p) = ( x³ + 7 ) mod (p)
Enter fullscreen mode Exit fullscreen mode

Image from ethereumbook

The mod p indicates that this curve is only valid over the field of prime order p.

So in reality it looks less smooth. For p=17the graph looks something like this:

Image from ethereumbook

The private key is “plugged in” to this curve to generate a public key.

Generating a Public Key

Your private key (k)uses elliptical curve arithmeticto generate the public key. The simplified form of the equation is follows:

K = k*G
Enter fullscreen mode Exit fullscreen mode

Where G is a predetermined point on the curve (called the generator point)

A more intuitive way of expressing this is as follows:

K = (k*G1, k*G2) where G1 and G2 are the x and y coordinates respectively.

Example:

Private Key: f8f8a2f43c8376ccb0871305060d7b27b0554d2cc72bccf41b2705608452f315

K = (f8f8a2f43c8376ccb0871305060d7b27b0554d2cc72bccf41b2705608452f315 * G1, f8f8a2f43c8376ccb0871305060d7b27b0554d2cc72bccf41b2705608452f315 * G2)

K = (6e145ccef1033dea239875dd00dfb4fee6e3348b84985c92f103444683bae07b, 83b5c38e5e2b0c8529d7fa3f64d46daa1ece2d9ac14cab9477d042c84c32ccd0)
Enter fullscreen mode Exit fullscreen mode

Serialization

Ethereum uses uncompressed keys. So here’s how we can serialize the keys:

serialized_key = 04 + x-coordinate (32 bytes/64 hex) + y-coordinate (32 bytes/64 hex)
Enter fullscreen mode Exit fullscreen mode

Example

serialized_key = 04 + 6e145ccef1033dea239875dd00dfb4fee6e3348b84985c92f103444683bae07b + 83b5c38e5e2b0c8529d7fa3f64d46daa1ece2d9ac14cab9477d042c84c32ccd0

serialized_key = 046e145ccef1033dea239875dd00dfb4fee6e3348b84985c92f103444683bae07b83b5c38e5e2b0c8529d7fa3f64d46daa1ece2d9ac14cab9477d042c84c32ccd0
Enter fullscreen mode Exit fullscreen mode

Once the public key is obtained we can derive the address. Now people can send you money!

Cryptographic Hash Function

“Much more than encryption algorithms, one-way hash functions are the workhorses of modern cryptography.”—Bruce Schneier

Remember the intro? We saw that representing 8018009 as a product of two primes is difficult.

But 2003 * 4003 is easy.

In other words you can go from x->y. But you can’t go from y->x feasibly. This is what one-way means.

Ethereum uses the Keccak-256 one-way hash function. Keccak-256 converts the public key into a unique address.

An address cannot be used to obtain the public key.

Keccak256(K) = 2a5bc342ed616b5ba5732269001d3f1ef827552ae1114027bd3ecf1f086ba0f9
Enter fullscreen mode Exit fullscreen mode

Only the last 20 bytes are used as an address. The prefix 0x is usually added:

address = 0x001d3f1ef827552ae1114027bd3ecf1f086ba0f9
Enter fullscreen mode Exit fullscreen mode

Done!

Hope this article was useful. If it was please let me know and I’ll make more.


Top comments (0)