Checking K Value in Sagemath

11 replies 149 views
benseedFull Member
Posts: 60 · Reputation: 461
#1Dec 6, 2022, 11:47 AM
What's the best way to check if the K value I got is correct for a given R signature and a public key? I'm really looking for someone to take a look at my code. How can I confirm the K value I found? My calculations seem off because it's verifying the wrong K value.
3 Reply Quote Share
Posts: 23 · Reputation: 192
#2Dec 6, 2022, 01:00 PM
Hello there "R = E.lift_x(r)" there are 2 possibilities in this line for point R "R" or "-R" If "public_key" is not a string but a "point" object(like G) with "E" element (Ellipric Curve) "R= u2*public_key + u1*G" it's the right thing to do. Thank you.
3 Reply Quote Share
benseedFull Member
Posts: 60 · Reputation: 461
#3Dec 6, 2022, 05:23 PM
So you are saying i should instead write it like this?
5 Reply Quote Share
Posts: 23 · Reputation: 192
#4Dec 6, 2022, 08:36 PM
on sagemath ------------------------------- sage:#prime [type:integer] but prime sage:P = 115792089237316195423570985008687907853269984665640564039457584007908834671663 sage:#Elliptic Curve y2=x3+7 for P [type:curve] sage:E = EllitpicCurve(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], we know "pubkey_x,pubkey_y" sage:public_key = E(pubkey_x,pubkey_y) sage:# "K=random_number*G"  [type:point] than "r = K[0]" [type:integer] , you known "r" sage:K = E.lift_x(r) sage:#K is_correct ? we don't know sage:r = your_value #[type:integer] sage:s = your_value #[type:integer] sage:z = your_value #[type:integer] sage:w = 1/s %N sage:u1 = z * w %N sage:u2 = r * w %N sage:#correct "K" point [type:point] sage:u2*public_key + u1*G #[type:point] sage:+K == u2*public_key + u1*G #(true or false) sage:-K == u2*public_key + u1*G #(true or false) sage r == Integer(+K[0]) #(true or false) [type:integer] sage r == Integer(-K[0]) #(true or false) [type:integer] sage:var("k x") sage:k*s == r*x+z #[type:variable] sage : K*s == r*public_key + z*G  #[type:point] (true or false) this line type: point R = E.lift_x(r)
4 Reply Quote Share
benseedFull Member
Posts: 60 · Reputation: 461
#5Dec 6, 2022, 10:28 PM
Ok. Got it. Thank you so much! But I still need to verify the K value found. How do I do it correctly? Your method only shows whether K+ or K- is the correct one. But I still need to verify the K value by brute forcing. So how do I write the code to verify the K found?
4 Reply Quote Share
bull_2019Senior Member
Posts: 296 · Reputation: 1992
#6Dec 7, 2022, 04:30 AM
# Compute the new signature point         P = k * G   (k = your new k on test)         # Check if the x-coordinate of the signature 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
benseedFull Member
Posts: 60 · Reputation: 461
#7Dec 8, 2022, 03:13 PM
Thank You!!
1 Reply Quote Share
gw3i1337Full Member
Posts: 148 · Reputation: 495
#8Dec 10, 2022, 06:07 AM
Hello, what is your formula to find k? I've been looking for this too and couldn't find a solution.
6 Reply Quote Share
Posts: 23 · Reputation: 192
#9Dec 10, 2022, 09:55 AM
+K and -K are [points], and k is [integer]. in this case K=(x/s)*PubKey + (z/s)*G or K = k*G k=randint(1,N) #i.e. a random number, you can only try to find this number. it is very difficult in this process. You can find "k" directly with the values r,s,z With E.lift_x(R) you get either "+K" or "-K" and this is a "point". However, you cannot find the value "k", which is an integer.
4 Reply Quote Share
gw3i1337Full Member
Posts: 148 · Reputation: 495
#10Dec 10, 2022, 11:21 AM
So how did you find your G spot? and p is your prime? I'm trying to learn, but I don't have enough knowledge.
4 Reply Quote Share
Posts: 23 · Reputation: 192
#11Dec 10, 2022, 04:00 PM
You have to research and learn, I can't explain this situation, you can now learn by communicating with an artificial intelligence.
0 Reply Quote Share
coin777Senior Member
Posts: 143 · Reputation: 970
#12Dec 10, 2022, 07:07 PM
Nobody knows that. It seems someone used 0x48ce563f89a0ed9414f5aa28ad0d96d6795f9c62 as a magic number to start with, but nobody knows why: https://www.youtube.com/watch?v=NGLR2N4EK58 And there are more magic numbers outside Bitcoin, even with reward for explaining them: https://bitcointalk.org/index.php?topic=5469657.0 AI is terrible at math, try asking about block hashes and transactions, in most cases it will be completely wrong, and it can give you for example an answer, where Bitcoin transaction format is mixed with Ethereum and other altcoins.
6 Reply Quote Share

Related topics