Post
Topic
Board Development & Technical Discussion
Re: Fail at coding my own Secp256k1 function (Pyhon)
by
SamYezi
on 11/08/2022, 12:09:21 UTC
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,... (2^256-1)*G.

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.


Ok, It is helpful. I've made a few changes. But it still doesn't work for small numbers (Don't know why),
https://github.com/MaltoonYezi/Python-DSA/blob/main/Cryptography/SECP256k1Procedural.py
Sorry, for a delayed response