Post
Topic
Board Development & Technical Discussion
Merits 21 from 6 users
Re: Fail at coding my own Secp256k1 function (Pyhon)
by
NotATether
on 09/08/2022, 08:24:14 UTC
⭐ Merited by mprep (5) ,BlackHatCoiner (4) ,ETFbitcoin (4) ,o_e_l_e_o (4) ,SamYezi (3) ,DdmrDdmr (1)
Code:
    while coef:
        if coef & 1:
            resultX, resultY = addition(currentX, currentY, gx, gy, a, b, prime)
        currentX, currentY = addition(currentX, currentY, gx, gy, a, b, prime)
        coef >>= 1

Let's unroll this loop:

- Current (x,y) is set to G
- Start at the least-significant bit
- If the bit is odd:
-- Then set Result = Current(x,y) + G [for the first iteration this means G+G]
- Set CurrentX += G [again, for the first iteration, it is G+G].

Do you see the problem here?

As you go through all of the bits, you are *adding* G to itself, this will make G, 2G, 3G, and so forth. You have to multiply the CurrentX by 2 each time, to get G, 2G, 4G, 8G, 16G,... 128G.

And each time the bit is odd, you are adding another G to the result which is already full of G's you're adding in succession, when you should set result = 0 in the initialization, and then you add it to Current (x,y). That is to say, Result += Current(x,y).

Binary expansion on private keys doesn't work without multiplication.