I’m diving into the technical side of Bitcoin and learning Python at the same time. I wrote a simple program to work with elliptic curves. I thought I could add points like Q+Q or Q+G, and even did things like G-Q, but when I tried to get half of 3G, I thought I'd mess up since there’s no 1.5G. What’s going wrong? How do I handle odd multiples of G like 3G or 5G?
You actually need to calculate the modinv of 2G and then multiply that by 3G. Remember, you can't have fractions of a curve point. It’s all about modular arithmetic, so you’re effectively calculating it like (3*(p-2))G because of Fermat’s theorem. It might sound complex, but it’s just about how you handle the math.
Yeah, exactly. When you’re halfing 3G, it’s really (3*(2**(N-2)) mod N), not quite what you first think. So, a shortcut is calculating that modular inverse which is pretty handy.
Oops, I should mention that those private key operations are mod N, not mod P. Also, not sure why you quoted part of my post, just saying. If you’re using the libsecp256k1 library, it’ll handle the modulus reduction for you automatically. Just use their secp256k1_fe types from the source code.
Right, to stress this point: elliptic curve points can get tricky because they lack fractions. It can confuse you when you try to halve something like 3G. So you do the modular inverse of 2 (mod N) and multiply by the point you're dealing with.
How does it even distinguish between regular halving like 16G to 8G and other operations like 7G to some other point? I mean, each point looks the same, right?
Honestly, it’s just a matter of how you see it. If you can halve G, you can halve any other point too. For instance: 3.5G doubles to become 7G. Just remember, to halve it, you multiply by 1/2, which in secp256k1 translates to some big number.
I wanted to write more, but then my phone freaked out and erased my draft. Anyway, it’s about big enough number types that the library handles overflow quite well. Just a heads up: this library does point operations and not mod P, which may be something to keep in mind.
Let’s not get too tangled up with elliptic curves right away. Think of it like basic fractions. If we divide 3 by 2 in decimal, we get 1.5. But on the secp256k1 side, we find that mod inverse of 2 and multiply it with 3 to avoid fractions. The final result is a really big number that represents that operation.