Crypto Maths
Tags: cryptocurrency, crypto, ethereum
Published: 2021-06-23 14:21:20

---
title: Crypto Maths
published: true
date: 2021-06-20 09:58:13 UTC
tags: cryptocurrency,crypto,ethereum
canonical_url: https://medium.com/coinmonks/crypto-maths-b04770116642
---

#### Ethereum One Way Hashing functions explained

![](https://cdn-images-1.medium.com/max/955/1*kVGnGNYnRWOK3nooAWDAlw.jpeg)<figcaption>Image by author. Quote from <a href="https://blog.cloudflare.com/a-relatively-easy-to-understand-primer-on-elliptic-curve-cryptography/">here</a>.</figcaption>

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](https://github.com/ethereumbook/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)
```

![](https://cdn-images-1.medium.com/max/409/0*9gxPiUHRWNNjrkWx.png)<figcaption>Image from <a href="https://github.com/ethereumbook/ethereumbook">ethereumbook</a></figcaption>

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:

![](https://cdn-images-1.medium.com/max/532/0*FJ7xErQGz_geeXdn.png)<figcaption>Image from <a href="https://github.com/ethereumbook/ethereumbook">ethereumbook</a></figcaption>

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 arithmetic](https://github.com/ethereumbook/ethereumbook/blob/develop/04keys-addresses.asciidoc#elliptic-curve-arithmetic-operations)to generate the public key. The simplified form of the equation is follows:

```
K = k*G
```

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)
```

### 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)
```

**Example**

```
serialized_key = 04 + 6e145ccef1033dea239875dd00dfb4fee6e3348b84985c92f103444683bae07b + 83b5c38e5e2b0c8529d7fa3f64d46daa1ece2d9ac14cab9477d042c84c32ccd0

serialized_key = 046e145ccef1033dea239875dd00dfb4fee6e3348b84985c92f103444683bae07b83b5c38e5e2b0c8529d7fa3f64d46daa1ece2d9ac14cab9477d042c84c32ccd0
```

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](https://www.schneier.com/essays/archives/2004/08/cryptanalysis_of_md5.html)

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
```

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

```
address = 0x001d3f1ef827552ae1114027bd3ecf1f086ba0f9
```

Done!

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

* * *