1

If I hash the message "HELLO" - keccak256("Hello") then run it to an ECDSA, what's the probability that I will get the same r, s and v value everytime?

Do I need to add some unique identifier other than the actual message then do keccak256(abi.encode("Hello", uniqueIdentifier)) to make sure that it's unique?

1 Answer 1

1

The ECDSA implementation creates and uses a random number internally. So, the probability of getting the same signature given the same message is really low, it's not feasible.

You can try a couple of ECDSA implementations and play around with the results. For example, using nodejs, you can install starkbank-ecdsa and try it out:

npm install starkbank-ecdsa npm install keccak256

const keccak256 = require("keccak256");
const ellipticcurve = require("starkbank-ecdsa");
const Ecdsa = ellipticcurve.Ecdsa;
const PrivateKey = ellipticcurve.PrivateKey;

// Generate privateKey from PEM string
const privateKey = PrivateKey.fromPem(
  "-----BEGIN EC PARAMETERS-----\nBgUrgQQACg==\n-----END EC PARAMETERS-----\n-----BEGIN EC PRIVATE KEY-----\nMHQCAQEEIODvZuS34wFbt0X53+P5EnSj6tMjfVK01dD1dgDH02RzoAcGBSuBBAAK\noUQDQgAE/nvHu/SQQaos9TUljQsUuKI15Zr5SabPrbwtbfT/408rkVVzq8vAisbB\nRmpeRREXj5aog/Mq8RrdYy75W9q/Ig==\n-----END EC PRIVATE KEY-----\n"
);

const messageHash = keccak256("Hello").toString("hex");

console.log("Message keccak256 hash: ", messageHash);

signature = Ecdsa.sign(messageHash, privateKey);
signature2 = Ecdsa.sign(messageHash, privateKey);
signature3 = Ecdsa.sign(messageHash, privateKey);
signature4 = Ecdsa.sign(messageHash, privateKey);

// Even though these signatures where created for the same message, they are different.
console.log("signature: ", signature.toBase64());
console.log("signature2: ", signature2.toBase64());
console.log("signature3: ", signature3.toBase64());
console.log("signature4: ", signature4.toBase64());

// To double check if message matches the signature
let publicKey = privateKey.publicKey();

console.log(Ecdsa.verify(messageHash, signature, publicKey));
console.log(Ecdsa.verify(messageHash, signature2, publicKey));
console.log(Ecdsa.verify(messageHash, signature3, publicKey));
console.log(Ecdsa.verify(messageHash, signature4, publicKey));

I modified their example from npm: https://www.npmjs.com/package/starkbank-ecdsa

It would yield something like this:

Message keccak256 hash:  06b3dfaec148fb1bb2b066f10ec285e7c9bf402ab32aa78a5d38e34566810cd2
signature:  MEYCIQD7odNPByFXKAoaOzni4BI1hRQ8qcolcHUQcPMITf7j+QIhALoIW5vOSCGGdsfeD1zWwc/VrXN+fukKE213HAiRIlgq
signature2:  MEUCIQC7X4HZpSu1kjNB2Dz7IEtnhtut1nKJ/zJrzcL04SxJxAIgDtccNTtn0tu7j+7oMPovez8NgiULALS+k2B1veRAp5Y=
signature3:  MEUCIBLk94Uyx8EaVSZqEbi85zP9B5qtlLgUezjb0ctXz8MWAiEA3ubrR5CgaDiOD/NZ1r9moGa0F8xLOtjWEHfTKupzjcQ=
signature4:  MEQCIEAiBKQXk2M+DmEjxONKs0N6qmUuhrjFhVuj+ZzBtpmdAiAHVtdy/e3s4yfL77wzYiR399h0JKFPdH8tVPtMLiiyzg==
true
true
true
true

You will notice that given the same message all the signatures are different, and all of them yield true when verifying the message.

2
  • 1
    Thank you for this. really helpful!
    – Lonewarp
    Aug 11 at 4:45
  • You're welcome. Aug 11 at 5:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.