Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[doc] privateKeyTweakAdd and publicKeyTweakAdd for HD Wallets #95

Closed
coolaj86 opened this issue Jan 30, 2023 · 3 comments
Closed

[doc] privateKeyTweakAdd and publicKeyTweakAdd for HD Wallets #95

coolaj86 opened this issue Jan 30, 2023 · 3 comments

Comments

@coolaj86
Copy link

coolaj86 commented Jan 30, 2023

I'm just documenting this for the sake of anyone else that's updating their HD Wallet libraries:
(see also #73)

The HD Wallet key functions are in the test files, no the main package.

  • tweakUtils.privateAdd is known in other libraries as privateKeyTweakAdd
  • tweakUtils.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.

@paulmillr
Copy link
Owner

should be updated for v2 now

@headfire94
Copy link

@paulmillr is this solved? I can't find these methods in lib

@paulmillr
Copy link
Owner

paulmillr commented Sep 20, 2023

@headfire94 there are no plans to implement these methods because they look terrible. Users should not even think about "tweaks": instead, there are points and scalars. I consider noble api much more concise. If you want to use these methods, you can implement them for v2 in your own fork.

Also there is no need in re-implementing HD wallet over and over, there's audited http://github.com/paulmillr/scure-bip32 and that's it. You don't need another one. You may need a different lib for different curve.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants