# ECDSA Weakness Where Nonces Are Reused

It is a well-known secret that ECDSA needs to be set up properly, else the private key could be revealed. In the worse case, Eve could reveal Alice’s Bitcoin private key from the ECDSA signatures. One of the weaknesses is where the same nonce value is used for different messages. So let’s crack.

With an ECDSA signature, we sign a message with a private key (priv) and prove the signature with the public key (pub). A random value (a nonce) is then used to randomize the signature. Each time we sign, we create a random nonce value and it will produce a different (but verifiable) signature. The private key, though, can be discovered if Alice signs two different messages with the same nonce. In this case, we will use SECP256k1 (and which is used in Bitcoin).

In ECDSA, Bob creates a random private key (priv), and then a public key from:

Next, in order to create a signature for a message of M, he creates a random number (k) and generates the signature of:

The signature is then (r,s) and where r is the x-coordinate of the point kG. H(M) is the SHA-256 hash of the message (M), and converted into an integer value. Now let’s say we have two messages (m1 and m2) and have hashes of:

Now let’s say that Alice signs the messages with the same private key (priv) and the same nonce (k), we can then recover the private key with :

We can also recover the nonce with:

Here is some code which does a discovery of the private key, and the nonce (k) if we use the same nonce value [here]:

`import ecdsaimport randomimport libnumimport hashlibimport sysG = ecdsa.SECP256k1.generatororder = G.order()priv1 = random.randrange(1,order) Public_key = ecdsa.ecdsa.Public_key(G, G * priv1)x1 = ecdsa.ecdsa.Private_key(Public_key, priv1)k = random.randrange(1, 2**127)msg1="Hello"msg2="Hello1"if (len(sys.argv)>1):	msg1=(sys.argv)if (len(sys.argv)>2):	msg2=(sys.argv) h1 = int(hashlib.sha256(msg1.encode()).hexdigest(),base=16)h2 = int(hashlib.sha256(msg2.encode()).hexdigest(),base=16) sig1 = x1.sign(h1, k)sig2 = x1.sign(h2, k)`