Learn how to code elliptic curve cryptography

This article gives an introduction to understanding elliptic curve cryptography and coding it.

Cornelius Schätz
Coinmonks
Published in
5 min readFeb 8, 2019

--

https://amyxinternetofthings.com/2017/09/14/quantum-computing-series-cryptography/

In my last article on how to Build a simple Blockchain I wrote about the ultimate basics of blockchain technology and gave you a step by step guide on how to build a simple one on your one. Now I’d like to show you the cryptography part of cryptocurrencies and blockchain. As you are probably used from the last time, we start with a little bit of theory and then go straight to programming. Let’s go!

Introduction

As most of you probably have some crypto assets on your own, you might have heard of the words ‘public key’ and ‘private key’. In a blockchain network, the public key kind of works as your address. If anyone else in the network wants to send you some funds, he or she is going to send it to your public key. Your private key can be seen as your signature — whenever you like to make a transaction of funds, you prove that you are the one owning the private key belonging to your public key without showing it to anyone in the network. Participants of the network, who check your transaction, can prove that you, with your public key, are the owner of the private key, without even knowing what it is. Pretty cool! But how does this really work on a more fundamental level? Let’s dig a little bit into the theory.

Elliptic Curve Cryptography

In this part, I will give you a pretty short introduction to the magic behind the used cryptography system. Since the maths behind it is pretty complicated and it is not necessary to understand every aspect of it. The cryptography system, which is mainly used in blockchain networks, is based on the mathematics of elliptic curves. But what is such an elliptic curve actually? Written in mathematical terms, it is the set of all points (x,y) that fulfill the equation

y² = x³ +ax + b

Such a curve may then look like this for example:

How an elliptic curve might look like. Source: https://www.google.de/search?q=elliptic+curve&source=lnms&tbm=isch&sa=X&ved=0ahUKEwjwzrey_NDdAhXJIsAKHRR6CiEQ_AUICigB&biw=1363&bih=601#imgrc=jFTvyQoRaPIAfM:

If you take an arbitrary point P = (x,y) on this curve and add it to another point Q on the curve, you will again get a point located on this very elliptic curve. A visualization of this point addition can be seen in the figure below.

Visualization of points on an elliptic curve. Source: https://www.google.de/search?q=elliptic+curve+cryptography&source=lnms&tbm=isch&sa=X&ved=0ahUKEwi2lbW9iNHdAhUM2BoKHX3bAngQ_AUICigB&biw=1363&bih=239#imgrc=NSETzS5aoM6peM:

You can also choose some point P on the curve and add it x-times to itself — you will still get a point located on the elliptic curve.

P+P+…+P = xP = R

In this case, x is just an arbitrary natural number. In elliptic curve cryptography one uses the fact, that it is computationally infeasible to calculate the number x only by knowing the points P and R. This is often described as the problem of solving the discrete logarithm. For cryptography, one chooses an appropriate point P on the elliptic curve generates a high enough random natural number x. This number is called the private key. With the chosen point P and the private key one calculates the point R on the curve, which is then defined as the public key. So public and private key are strongly connected to each other! The public key cannot exist without the private key and only by knowing the public key it is due to the discrete logarithm problem nearly impossible to determine the private key.

Using this method, it is possible to ‘sign’ any desired message. Let M be any message, pub be the public key and priv the private key of the sender. A signature is then calculated dependent on the two parameters M and priv:

Signature = Sign(M,priv)

In the above equation the function Sign() generates the signature. Anyone, who receives the message M can verify the signature — proving that the sender public key indeed also holds the private key:

Verify = Ver(Signature, M, pub)

To verify, the recipient only needs the signature itself, the message and the public key of the sender. This is, without diving too deep into the theory, how elliptic curve cryptography basically works. Now let’s do some coding!

Coding cryptography

In python, the above described method can be implemented using the library fastecdsa. However there are many more libraries written to use for elliptic curve cryptography. First you need to install it on your computer using the
pip install’ command in your terminal.

pip install fastecdsa

After installation, we can open our python IDE and start coding.

from fastecdsa import keys, curve,ecdsa
priv_key, pub_key = keys.gen_keypair(curve.P256)
print(priv_key)
print(pub_key)

First we import some classes from the fastecdsa library. The class keys contains a function to generate a keypair using the maths of elliptic curves. The class curve contains many different elliptic curves, from which you can choose one to generate your keys on. The class ecdsa is used later to generate and verify signatures. In the second line, a pair of a public and a private key is generated using the curve P256. After that we print both the private and the public key. The result should look something like this for the private key:

20053020608649230331723442089943129241597707800309205888496491961204729412316

You see, the private key is a pretty huge number with a lot of digits! Let’s take a look, at what the public key looks like:

X: 0xf8781fc1967637b0fe3e43cbd750051672fad09d0fd8f18d2d49ed1f84ebb5c9
Y: 0x26a617f3fc7b1c34bf00b21445201299f9730bc7838994751ead5ddff511c622
(On curve <P256>)

As you can see, the public key is a combination of the x-coordinate and the y-coordinate of the point on the curve P256. Perfect! Let’s move on to generating and verifying signatures for some messages. Just add the following lines to the written code above:

message = ‘I am a message’
(r,s) = ecdsa.sign(message,priv_key)
print((r,s))

We define some string as a message and then use the imported class ecdsa to generate a signature (r,s). After that, we print it and should get something similar to the following:

(50517963046740693636358980032162772006533914957769092229968951281723335924730, 24429916358942402334243425477548564776488638570074379964228780478761347218109)

Now we can go on to verifying this signature. Again, just add the following lines of code:

valid = ecdsa.verify((r,s),message,pub_key)
print(valid)

We are again using the ecdsa class and calling the function verify() from it, which is dependent on the three parameters signature, message and the public key of the one generating the message. After that, we print the variable valid, which, if everything worked out, should give you the following output:

True

Congratulations! You just understood the basics of public key cryptography used in blockchain networks. One step further to creating your own cryptocurrency.

At this point I would like to thank you for reading. Leave some claps, if you liked this article. If you like to dig deeper into the blockchain rabbit hole, feel free to have a look at How cryptocurrencies actually work.

Get Best Software Deals Directly In Your Inbox

--

--