Last active
October 22, 2024 18:46
-
-
Save Goatghosts/e1b71b7d5d1e9798fe724d373345fb9d to your computer and use it in GitHub Desktop.
Many people know how to get a private key from a nonce. But I did not find information on how to get a nonce from a private key. If someone also does not know how to search, then here is a ready-made solution. And here is what I took as a basis: https://asecuritysite.com/ecdsa/ecd3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ecdsa | |
import random | |
import libnum | |
import hashlib | |
G = ecdsa.SECP256k1.generator | |
ORDER = G.order() | |
def get_k_from_pkey(valid_pkey, sign, z): | |
s_inv = libnum.invmod(sign.s, ORDER) | |
return (s_inv * ((valid_pkey * sign.r) + z)) % ORDER | |
def get_pkey_from_k(valid_k, sign, z): | |
r_inv = libnum.invmod(sign.r, ORDER) | |
return (r_inv * ((valid_k * sign.s) - z)) % ORDER | |
def main(): | |
valid_private_key = random.randrange(1, ORDER) | |
Public_key = ecdsa.ecdsa.Public_key(G, G * valid_private_key) | |
Private_key = ecdsa.ecdsa.Private_key(Public_key, valid_private_key) | |
valid_k = random.randrange(1, 2**127) | |
msg = "Hello" | |
z = int(hashlib.sha256(msg.encode()).hexdigest(), base=16) | |
sign = Private_key.sign(z, valid_k) | |
print("Sign") | |
print(f"R: {sign.r}") | |
print(f"S: {sign.s}") | |
print(f"Z: {z}") | |
print() | |
found_private_key = get_pkey_from_k(valid_k, sign, z) | |
assert found_private_key == valid_private_key | |
print(f"Private key: {valid_private_key} == {found_private_key}") | |
found_k = get_k_from_pkey(valid_private_key, sign, z) | |
assert found_k == valid_k | |
print(f"K (Nonce): {valid_k} == {found_k}") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment