How to Check K Value Correctly in Sagemath

11 replies 476 views
dan2015Hero Member
Posts: 41 · Reputation: 3344
#1Sep 28, 2025, 07:22 PM
How do I check if the K value I got is right for an R signature and public key? I'm really struggling with it. Can someone help me out?
6 Reply Quote Share
degen23Member
Posts: 18 · Reputation: 96
#2Sep 28, 2025, 11:19 PM
Hey, just a heads up, in "R = E.lift_x(r)" there are two options for R. You could get either "R" or "-R". If your public_key is a point object rather than a string, like G, then you should use "R = u2 * public_key + u1 * G". That’s the right way.
4 Reply Quote Share
dan2015Hero Member
Posts: 41 · Reputation: 3344
#3Sep 29, 2025, 05:01 AM
So you're suggesting I rewrite it that way?
0 Reply Quote Share
degen23Member
Posts: 18 · Reputation: 96
#4Sep 29, 2025, 06:29 AM
In Sagemath, it looks like this: sage:# prime [type:integer] but prime sage:P = 115792089237316195423570985008687907853269984665640564039457584007908834671663 sage:# Elliptic Curve y2=x3+7 for P [type:curve] sage:E = EllipticCurve(GF(P),[0,7]) sage:# Elliptic Curve Order [type:integer] sage:N = E.order() sage:# Base Point G [type:point] sage:G = E(55066263022277343669578718895168534326250603453777594175500187360389116729240,32670510020758816978083085130507043184471273380659243275938904335757337482424) sage:# "public_key = secret * G" or "public_key = E(pubkey_x, pubkey_y)" [type:point]
5 Reply Quote Share
dan2015Hero Member
Posts: 41 · Reputation: 3344
#5Sep 29, 2025, 09:35 AM
Got it, thanks a lot! But I still can't verify the K value I found. Your method only tells me if K+ or K- is right. I need code to brute-force the K value, what can I do?
3 Reply Quote Share
gmfrensMember
Posts: 213 · Reputation: 42
#6Sep 29, 2025, 10:03 AM
# To find the new signature point P = k * G # k is your test k value # Then check if the x value of the point matches r if P.x() == r: print(f"Found k candidate: {k:x}") private_key = (s * k z) * mod_inv(r, n) % n print("Private Key: %02x " % private_key)
2 Reply Quote Share
dan2015Hero Member
Posts: 41 · Reputation: 3344
#7Sep 29, 2025, 04:13 PM
Thanks a ton!!
2 Reply Quote Share
greg_moonMember
Posts: 16 · Reputation: 167
#8Sep 29, 2025, 06:32 PM
What’s your formula to figure out k? I’ve been searching for that too and came up empty.
3 Reply Quote Share
degen23Member
Posts: 18 · Reputation: 96
#9Sep 30, 2025, 12:24 AM
+K and -K are points and k is just an integer. So you can do this: K = (x/s) * PubKey + (z/s) * G or even K = k * G k=randint(1,N) # just pick a random number. Good luck finding it. You can technically derive "k" directly with r,s,z values. But E.lift_x(R) will give you either K+ or K-.
3 Reply Quote Share
greg_moonMember
Posts: 16 · Reputation: 167
#10Oct 1, 2025, 08:55 AM
How did you even get your G point? And P is your prime? I’m trying to figure this out.
4 Reply Quote Share
degen23Member
Posts: 18 · Reputation: 96
#11Oct 1, 2025, 11:40 AM
You gotta study up, can’t explain everything here. But yeah, AI can help you learn if you ask the right questions.
4 Reply Quote Share
0xLaserFull Member
Posts: 252 · Reputation: 641
#12Oct 1, 2025, 05:54 PM
No one really knows. Some folks started with 0x48ce563f89a0ed9414f5aa28ad0d96d6795f9c62 as a magic number, but why? Beats me. There are more magic numbers outside Bitcoin, some even offer rewards for solving them. Just a heads up, AI isn’t great with math. It mixes stuff up a lot, like Bitcoin and Ethereum formats.
5 Reply Quote Share

Related topics