Git Product home page Git Product logo

Comments (4)

AlvaroLuken avatar AlvaroLuken commented on May 26, 2024 1

Hey @paulmillr , should have elaborated apologies! Thanks for the clear example on the verification working above. I think this corner case is seen whenever secp.recoverPublicKey comes into the mix...

const secp = require('@noble/secp256k1');
const priv = secp.utils.randomPrivateKey();
const SHA256 = require('crypto-js/sha256');

(async () => {
  const message = JSON.stringify({
    to: "0xbeef",
    amount: parseInt(40)
  });

  const message2 = JSON.stringify({
    to: "0xdead",
    amount: parseInt(50)
  });

  // different message hashes
  const msg1Hash = SHA256(message).toString();
  const msg2Hash = SHA256(message2).toString();

  // msg is SIGNED here!
  const signatureArray = await secp.sign(msg1Hash, priv, {
    recovered: true
  });
  
  // get signature + rBit
  const signature = signatureArray[0];
  const rBit = signatureArray[1];

  // recover public key using signed message hash + sig + rBit
  const recoveredPublicKey1 = secp.recoverPublicKey("beef", signature, rBit).toString('hex');

  // verify
  // yields false
  console.log('verify', msg1Hash);
  console.log(secp.verify(signature, msg1Hash, recoveredPublicKey1));

  // verify message passed in to secp.recoverPublicKey but never signed!
  // yields true when should yield false
  console.log('verify msg2Hash but corner case!', msg2Hash);
  console.log(secp.verify(signature, "beef", recoveredPublicKey1));
  
})()

from noble-secp256k1.

paulmillr avatar paulmillr commented on May 26, 2024
const secp = require('@noble/secp256k1');
const priv = secp.utils.randomPrivateKey();
const pub = secp.getPublicKey(priv);
const msg = 'deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef';
const msg2 = msg.replace(/d/g, '3');

(async () => {
  const signed = await secp.sign(msg, priv);
  console.log('verify', msg);
  console.log(secp.verify(signed, msg, pub));
  console.log('verify', msg2)
  console.log(secp.verify(signed, msg2, pub));
})()
verify deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef
true
verify 3ea3beef3ea3beef3ea3beef3ea3beef3ea3beef3ea3beef3ea3beef3ea3beef
false

from noble-secp256k1.

paulmillr avatar paulmillr commented on May 26, 2024

Still seeing correct behavior with your code, on my machine:

verify 305d43b0105f8bace8654dbc838e65e4ddfde377f82141719ecc60895a8d6124
false
verify msg2Hash but corner case! b592da96de07d6f1b2ec6ef53a5c48a9d36213e944a705d2caebebe0097f6009
false

BTW, this line is invalid:

const recoveredPublicKey1 = secp.recoverPublicKey("beef", signature, rBit).toString('hex');

It should have prob been:

const recoveredPublicKey1 = secp.recoverPublicKey(msg1Hash, signature, rBit).toString('hex');

BTW 2: I suggest to use noble-hashes instead of crypto-js

from noble-secp256k1.

paulmillr avatar paulmillr commented on May 26, 2024

@AlvaroLuken can you try using the most recent release?

from noble-secp256k1.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.