# Understanding Bitcoin Internal Math for Private and Public Keys

in STEMGeekslast year

# Frustration

How is it that all of the JS APIs we have around do not have an easy way to validate a signature?

# Building My Own System For Validating Signatures

First, if you look at the integer set that Bitcoin uses, it consists of 2^256^. This is good for users, but if you want to just try to understand things in a concrete manner. It would be nice to work in something smaller:

Let's take this set: {-2,-1,0,1,2}.

How do we add? We add normally and then we add or subtract 5 until we get a number back in the original set. Same rule with subtraction or multiplication. Division is the same idea as multiplication but backwards. If we want to do a / b, we need to figure out an x, such that x * b = 1 in our current system.

+2-2
2-10
-20-1

Addition for 1, 0, and -1 pairs work like you would otherwise expect.

*2-2
2-11
-21-1

Multiplication by -1, 0, and 1 do what you would otherwise expect.
So, now we come to the elliptic curve:
y^2^ = x^3^ - x - 1

The set only has five numbers so we can plugin x for each value

xyy^2^
-2-2
-12 or -2-1
02 or -2-1
12 or -2-1
200

For getting y from y^2^, you must find the square root (but for this system), so now you are trying to find a y so that y^2^ is -2, -1, and 0. It turns out that y^2^, is never positive on this curve. Now in the case of y^2^=-2, there is no solution. Check the multiplication table above. There is no square root for -2.

So now we have a set of points that satisfy our equation. {(-1,-2), (-1,2), (0,-2), (0,2), (1,-2), (1,2), (2,0)}.

(2,0) is our O point. The reason will be clear later.
Now one of these points will have to be our G. When you draw the dots on graph paper, you get three dots on y = 2, and three dots on y = -2 and another one on the x axis at 2.

# Yet Another System of Adding and Multiplying

Notice that every point except (2,0) has a reflecting point in the x-axis. Any point can be added to any other provided they don't land on the same vertical. Addition is done by taking the slope between the two of them finding the other point that is also on that line and then the reflection of that point in the x-axis is the sum of the two original points.

Sometimes we can multiply a point by 2, but not always.

Take (-1,2)+(1,-2) Well the slope here is 1/2. So, now these points define a sloped line: (-1+2a,2+a): {.., (-1,2), (1,-2), (-2, -1), (0, 2), ...}. The point (0,2) is on the curve. We now must reflect it along the x-axis. It's reflection is (0,-2), so `(-1,2)+(1,-2)=(0,-2)`. And so that's how that works.

If you add a point to itself, you must calculate the slope by pretending it's real again. Use calculus and derive the derivative:

`y^2^ = x^3^ - x - 1`
`2ydy = 3x^2^dx - dx`
`dy/dx = (3x^2 - 1 )/(2y)`

Example:

For `(0,2)` :
`dy/dx = (3x^2 - 1 )/(2y)`
`dy/dx = (3*0^2 - 1 )/(2*2)`
`dy/dx = -1/(4)`

Remember 4 = -1 in this system.
`dy/dx = -1/(-1)`

Now divide by -1, is find a number `a` so that `-1a = 1`. Trying different numbers, gives you `a = -1`.

`dy/dx = -1*-1 = 1`
The slope is 1.
Now, { (a, 2 + a) | a integer }
{(-2, 0), (-1, 1), (0, 2), (1, -2), (2, -1)}

So the other number is (1,-2). Now we take the reflection of it. Thus `2(0,2)` is `(1,2)`.

Qslope2Q
(-1,2)3(-1,2)
(-1,-2)2(1,-2)
(0,2)1(1,2)
(0,-2)-1(1,-2)
(1,2)3(2,0)
(1,-2)2(2,0)
(2,0)undefined - can't divide by zeroundefined

`2(1,-2) = (2,0)` and so does `2(1,2)`!
They consider the point (2,0) to be a Zero, so now you can add with it as if it is a real and we have an identity so when adding nothing happens.

This is a weird world where points that are considered non-zero can add up to zero.

Later, I'll encrypt something with this system using a private key. It wont be terribly secure.

Enough mental exercise, time to get on the bike.

Sort:

@leprechaun ...you lost me at JS APIs 😉 😂 Clearly this is above my head. This looks highly technical although I'm sure to your Maths brain it isn't. For a layperson like me, whats is the takeaway from this? You can give it to me in one sentence lol or is it basically that the encryption is not secure? As an aside... did you see that @snook 's weekly prompt for this week is the word leprechaun lol? worth an entry? 😉🌈

Thanks for reading so far into it! 😄 I get confused sometimes myself. I wrote about how Bitcoin public keys are related to private keys. The important change is the that instead of using 75 digit numbers for keys like in real Bitcoin, here I used 1 digit keys. Of course when keys are this small it is easy to generate all the keys and be able to look up the private keys with public keys.

I wanted to see if using mixed negative and positive numbers for the field rather than strictly positive numbers gave a somewhat more elegant result. I wanted to be able to understand this twenty years down the road when perhaps I won't have looked at this kind of math in the last fifteen years previous. It's annoying not understanding one's own notes.

ok gotcha !LOLZ that makes a bit more sense now when you put it that way. I was like...am I back in Advanced Mathematics here? hehe Thank you for taking the time to explain your thought process 😉 I did actually read it all and tried to understand but realised that I just couldn't 😆. Hope you're keeping well.

She said because it won't sit still.

Credit: minopoly
@leprechaun, I sent you an \$LOLZ on behalf of @samsmith1971
Use the !LOL or !LOLZ command to share a joke and an \$LOLZ. (4/4)

Congratulations @leprechaun! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s):

If you no longer want to receive notifications, reply to this comment with the word `STOP`