Create our own Bitcoin(Mathematics and Coding)-1
The understanding of mathematics behind Bitcoin requires a understanding of Fields in Set theory. Bitcoin is built upon elliptic curve cryptography which is an example of asymmetric cryptography. A brief overview of asymmetric cryptography and one of its examples, RSA, is given in my previous blog
https://rahuram-thiagarajan.medium.com/the-secret-rsa-cryptography-4f4aad307460
Fields:
A field is nothing but a set in which addition, subtraction ,multiplication and division are defined and behave in a corresponding way.
So if we take a Field as
F₂₃={0,1,2,3,4,5…23}
19+ᶠ17=13 (where 36%23=13)
+ᶠ means addition happens over the field F₂₃
I have defined the addition, subtraction, multiplication and exponentiation operations for Fields . The variable “prime” in the code just represents the upper limit of the field, which is usually a prime number(23 in the previous example)for the reasons which we would explore further.
Elliptic curve:
An elliptic curve is just a simple curve made up of the points satisfying the equation
y²=x³+Ax+B
where A and B are constants.
This elliptic curve has an interesting property which makes it useful for cryptographic functions, and the property is :
Take any two points on this elliptic curve and draw a line connecting those two points, it will intersect the curve in a new third point.
Example
Let us take the curve y²=x³-4x+1 and two points (0,1) and (4,7) on it.
1² = 0³–4 × 0 + 1 ====>(0,1) exists on the curve.
7² =4³-4×4+1====>(4,7) also exists on the curve.
The property of the elliptic curve states that the line joining these two points which we chose randomly should intersect the curve at a third point ,which happens as follows
Let the points be A(0,1) B(4,7) and C (-1.75,-1.625).We can define C as a result of binary operation(+,Point addition) of A and B ,also B as a result of binary operation of A and C and A as a binary operation of B and C
ie
C(-1.75,1.625)=A(0,1) +B(4,7)
The line joining P₁(x₁,y₁) and P₂(x₂,y₂) intersects the curve again at P₃(x₃,y₃),which can be calculated using the following formulae.
P₃(x₃,y₃)=P₁(x₁,y₁) +P₂(x₂,y₂), (‘+’ refers to Point addition)
1.When P₁≠P₂
s = (y — y₁ )/(x₂ — x₁ )
x₃ = [s²— x₁ — x₂]
y₃ = [s(x₁ — x₃) — y₁]
Since elliptic curves are horizontally symmetrical if U(x , y) exist in the curve then V(x,-y) also exists on the curve.
2.When P₁=P₂,
s = (3x₁²+ a)/(2y₁ )x₃= [s²–2x₁]y₃ = [s(x₁ — x₃ ) — y₁]
In this case ,since P₁=P₂ ,the line is the tangent to the curve at the point P₁ or P₂ which intersects the elliptic curve at P₃
Let P=(0,1), According to the formula
P₃(x₃,y₃)=P(0,1)+P(0,1)=(4,7), which means
P₁+P₂= P+P = 2P = (4,7)
(! Remainder: ‘+’ here between 2 points means Point addition andnot normal addition)
2P=(4,7)
3P=2P+P=(4,7) + (0,1) which is calculated as (-1.75,1.625)
3P=(-1.75,1.625)
Similarly P+P+P+….n times=nP
From the above example, we can see that the calculation of the value of 3P(‘P+P+P’) from P(0,1) can be easily done using the formulas. But the division
(-1.75,1.625)/(0,1) = 3,requires intense computation,
This satisfies the basic requirement of asymmetric cryptography where the construction of public key from private key is needed to be easy but the decoding the private key from the public key should be very difficult.
A more relatable example is RSA which works on the fact that the multiplication of two prime numbers is easy but factorizing a large number to its prime factors takes intensive calculations.
In a similar way calculating nP from P is easy but finding the value of n from nP and P require immense computational power .
Code to plot graph:
Code to define operations in Elliptic curve
Main
In the subsequent blogs, the transactions will be discussed .Please follow if you like it.