Git Product home page Git Product logo

Comments (9)

landabaso avatar landabaso commented on May 23, 2024 1

Yeah, you can get these functions from there.
BTW, there's a full implementation in the link I copied above as an npm package ready to use and heavily tested:

https://github.com/bitcoinerlab/secp256k1

Let me know if I can help you somehow.

from noble-secp256k1.

paulmillr avatar paulmillr commented on May 23, 2024

Thanks. This should be adjusted then

from noble-secp256k1.

paulmillr avatar paulmillr commented on May 23, 2024

On the other hand, the current behavior was made on purpose. Why would you add an all-zero private key? It's certainly invalid in ECDH context, and in some others.

from noble-secp256k1.

landabaso avatar landabaso commented on May 23, 2024

On the other hand, the current behavior was made on purpose. Why would you add an all-zero private key? It's certainly invalid in ECDH context, and in some others.

I understand your point. It's more of an interoperability issue with ecpair. Not really a bug.
This issue concerns tweakUtils which aren't exposed anyway.

I copied and adjusted tweakUtils in my project with this little change. I just wanted to share this with you so you are aware.
BTW, in order to make noble-secp256k1 and ecpair work together, I also had to adjust tweakUtils functions (in fact, in the wrapper around them) so that they throw when the computed private key is zero.

Feel free to close the issue as this is more of a comment than a bug.

from noble-secp256k1.

coolaj86 avatar coolaj86 commented on May 23, 2024

@landabaso, I'm also trying to use this with HD wallet paths (to make a fully browser-compatible implementation).

What is "tweakUtils"? Whether I do a search for "tweakutils" on npm or "tweakutils secp256k1" on the web, I don't seem to get back any relevant results.

What privateAdd() function are you referring to? I don't see it in the docs or code here or the ecpair repo.

Could you post a link to your implementation?

from noble-secp256k1.

paulmillr avatar paulmillr commented on May 23, 2024

For tweak utils, look into secp tests: they have the utils. It was part of the main pkg, then got removed because I think it has bad UX and it's just legacy of other libraries while here we have better primitives (Point).

For hdkey, take a look at scure/bip32.

from noble-secp256k1.

landabaso avatar landabaso commented on May 23, 2024

Could you post a link to your implementation?

I hope it helps @coolaj86: https://github.com/bitcoinerlab/secp256k1

from noble-secp256k1.

coolaj86 avatar coolaj86 commented on May 23, 2024

@landabaso I was able to get this to work with our APIs.

For anyone looking on, this is the code that's missing (i.e. for HD Wallets):

  • privateAdd is known in other libraries as privateKeyTweakAdd
  • pointAddScalar is known in other libraries as publicKeyTweakAdd
  let tweakUtils = {
    /**
     * @param {Uint8Array} privateKey
     * @param {Uint8Array} tweak
     * @returns {Uint8Array} - a new (derivative) privateKey
     */
    privateAdd: function (privateKey, tweak) {
      const p = Secp256k1.utils._normalizePrivateKey(privateKey);
      const t = Secp256k1.utils._normalizePrivateKey(tweak);
      return Secp256k1.utils._bigintTo32Bytes(
        Secp256k1.utils.mod(p + t, Secp256k1.CURVE.n),
      );
    },

    /**
     * @param {Uint8Array} p
     * @param {Uint8Array} tweak
     * @param {Boolean} [isCompressed]
     * @returns {Uint8Array} - a new (derivative) publicKey
     */
    pointAddScalar: function (p, tweak, isCompressed) {
      const P = Secp256k1.Point.fromHex(p);
      const t = Secp256k1.utils._normalizePrivateKey(tweak);
      const Q = Secp256k1.Point.BASE.multiplyAndAddUnsafe(P, t, 1n);
      if (!Q) {
        throw new Error("Tweaked point at infinity");
      }
      return Q.toRawBytes(isCompressed);
    },
  };

Adapted from ./test/index.ts:

const tweakUtils = {
privateAdd: (privateKey: PrivKey, tweak: Hex): Uint8Array => {
const p = normal(privateKey);
const t = normal(tweak);
return secp.utils._bigintTo32Bytes(secp.utils.mod(p + t, secp.CURVE.n));
},
privateNegate: (privateKey: PrivKey): Uint8Array => {
const p = normal(privateKey);
return secp.utils._bigintTo32Bytes(secp.CURVE.n - p);
},
pointAddScalar: (p: Hex, tweak: Hex, isCompressed?: boolean): Uint8Array => {
const P = secp.Point.fromHex(p);
const t = normal(tweak);
const Q = secp.Point.BASE.multiplyAndAddUnsafe(P, t, 1n);
if (!Q) throw new Error('Tweaked point at infinity');
return Q.toRawBytes(isCompressed);
},
pointMultiply: (p: Hex, tweak: Hex, isCompressed?: boolean): Uint8Array => {
const P = secp.Point.fromHex(p);
const h = typeof tweak === 'string' ? tweak : secp.utils.bytesToHex(tweak);
const t = BigInt(`0x${h}`);
return P.multiply(t).toRawBytes(isCompressed);
},
};

This may need to be updated again for the upcoming major refactor.

from noble-secp256k1.

coolaj86 avatar coolaj86 commented on May 23, 2024

Thanks.

We're optimizing for the fewest dependencies and using only WebCrypto for both Web and node v18+.
(dropping node-crypto and <= node v16 entirely)

We have our own fork for a number of repos, as well as from-scratch rewrites, and the goal is that they should all work with CommonJS, ESM, Browsers, Node, and Bundlers without transpiling (adds about 5 lines of wrapper boilerplate per module).

We're preparing our "launch" of these tools for not this Monday, but the following Monday:

https://github.com/dashhive/

It's mostly just for our own community and our own tools - moving towards a fully vertically integrated model for a more optimized client experience and better developer experience - rather than so many half-baked, decade-old modules across so many ecosystems (hence @noble for secp256k1).

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.