I wrote some code that outlines how to create a transaction from scratch. First I want to explain the concepts I found difficult to grasp, and the information that took me a while to find. Then I will walk through my code. I tried to keep it concise so I don’t bore you with the obvious or bombard you with lengthy explanations. This is the stuff I found interesting while learning about transactions. It is intended to be used in conjunction with other resources.

Elliptic Curve Cryptography

Bitcoin uses Secp256k1 for its elliptic curve digital signature algorithm. Secp256k1 has a prime order N = FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE BAAEDCE6 AF48A03B BFD25E8C D0364141, which is equal to the total number of points on the curve minus the infinity point. The infinity point is where X = 0. These points(not including the infinity point) make a cyclic group. Every cyclic group is an abelian group, meaning it has 5 rules that it follows. They are closure, associativity, identity element, inverse element, commutativity. We can use modulus to restrict the elliptic curve to manageable numbers while keeping all the important properties.

Addition

These properties allow us to define addition as 3 points a line intersects equals the infinity point. P + Q + R = 0. This also means two points equals another one, by moving R to the other side of the equation. The inverse properties tells us that -R equals R flipped over the X-axis. We can now find every point in the group, it would just take N additions. Starting from the base/generator point we can add it with the previous point to find the next one.

generator point(P) + 0 = P

P + P = - R

P + -R = new point(Q)

P + Q = another new point(public key)

None of the points will be the same, they will all be whole number coordinates and after N additions we will be finding the same points in the same order(hence cyclic). Each of these points is a public key. The private key is equal to the number of additions it took to reach the public key.

Doubling

The first line in the code above adds the generator point to itself, also called doubling. If a line is tangent to a point on the curve it will only intersect at two points. Doing N additions would make finding a private and public key pair a brute force task. We won’t know every key pair “Until computers are build from something other than matter and occupy something other than space.” We know how many points there are because of Schoof’s algorithm. Doubling allows us to easily calculate key pairs of large numbers. When the public key point is doubled so is the private key.

Modular Inverse

We need to use modular Inverse to calculate slope because division does not work with modulus. The Euclidean algorithm find the greatest common divisor(GCD) of two integers. The GCD of P( FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE FFFFFC2F) and any number less than it will be one because P is prime. Reversing the steps of the Euclidean algorithm(Extended Euclidean algorithm) gives us Bezout’s identity. This also gives us the modular inverse we need to calculate slope. The slope is used in addition and doubling. The slope at each point is still calculable over a finite field.

Discrete Log