Dividing Points on Secp256k1 Curve

8 replies 494 views
Posts: 11 · Reputation: 209
#1Aug 27, 2019, 06:08 PM
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?
4 Reply Quote Share
0xNodeMember
Posts: 307 · Reputation: 80
#2Aug 27, 2019, 11:36 PM
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.
3 Reply Quote Share
falcon2019Full Member
Posts: 50 · Reputation: 425
#3Aug 28, 2019, 12:42 AM
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.
1 Reply Quote Share
0xNodeMember
Posts: 307 · Reputation: 80
#4Aug 28, 2019, 02:50 AM
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.
2 Reply Quote Share
roguewizardFull Member
Posts: 3 · Reputation: 646
#5Aug 28, 2019, 08:27 AM
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.
5 Reply Quote Share
Posts: 11 · Reputation: 209
#6Aug 28, 2019, 10:37 AM
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?
4 Reply Quote Share
diamondhandsHero Member
Posts: 98 · Reputation: 2112
#7Aug 28, 2019, 03:45 PM
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.
0 Reply Quote Share
0xNodeMember
Posts: 307 · Reputation: 80
#8Aug 29, 2019, 06:48 AM
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.
3 Reply Quote Share
m1kes4tFull Member
Posts: 60 · Reputation: 252
#9Aug 29, 2019, 11:37 AM
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.
2 Reply Quote Share

Related topics