Skip to content

feat: add tweakHash and extraEntropy optional params to signSchnorr() #6

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"prettier": "^2.4.1",
"proxyquire": "^2.0.1",
"rimraf": "^2.6.3",
"tiny-secp256k1": "^2.1.2",
"tiny-secp256k1": "^2.2.0",
"ts-node": "^8.3.0",
"tslint": "^6.1.3",
"typescript": "^4.4.4"
Expand Down
9 changes: 6 additions & 3 deletions src/ecpair.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ export interface ECPairInterface extends Signer {
privateKey?: Buffer;
toWIF(): string;
verify(hash: Buffer, signature: Buffer): boolean;
verifySchnorr(hash: Buffer, signature: Buffer): boolean;
signSchnorr(hash: Buffer): Buffer;
verifySchnorr(hash: Buffer, signature: Buffer, tweak?: Buffer): boolean;
signSchnorr(hash: Buffer, extraEntropy?: Buffer, tweak?: Buffer): Buffer;
}
export interface ECPairAPI {
isPoint(maybePoint: any): boolean;
Expand All @@ -39,8 +39,11 @@ export interface ECPairAPI {
export interface TinySecp256k1Interface {
isPoint(p: Uint8Array): boolean;
pointCompress(p: Uint8Array, compressed?: boolean): Uint8Array;
isPrivate(d: Uint8Array): boolean;
pointFromScalar(d: Uint8Array, compressed?: boolean): Uint8Array | null;
pointAddScalar(p: Uint8Array, tweak: Uint8Array, compressed?: boolean): Uint8Array | null;
isPrivate(d: Uint8Array): boolean;
privateAdd(d: Uint8Array, tweak: Uint8Array): Uint8Array | null;
privateNegate(d: Uint8Array): Uint8Array;
sign(h: Uint8Array, d: Uint8Array, e?: Uint8Array): Uint8Array;
signSchnorr?(h: Uint8Array, d: Uint8Array, e?: Uint8Array): Uint8Array;
verify(h: Uint8Array, Q: Uint8Array, signature: Uint8Array, strict?: boolean): boolean;
Expand Down
31 changes: 27 additions & 4 deletions src/ecpair.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,42 @@ function ECPairFactory(ecc) {
return Buffer.from(sig);
}
}
signSchnorr(hash) {
signSchnorr(hash, e, tweak) {
if (!this.privateKey) throw new Error('Missing private key');
if (!ecc.signSchnorr)
throw new Error('signSchnorr not supported by ecc library');
return Buffer.from(ecc.signSchnorr(hash, this.privateKey));
if (!tweak) return Buffer.from(ecc.signSchnorr(hash, this.privateKey, e));
const privateKey =
this.publicKey[0] === 2
? this.privateKey
: ecc.privateNegate(this.privateKey);
const tweakedPrivateKey = ecc.privateAdd(privateKey, tweak);
if (!tweakedPrivateKey) {
throw new Error('Invalid tweaked private key!');
}
return Buffer.from(ecc.signSchnorr(hash, tweakedPrivateKey, e));
}
verify(hash, signature) {
return ecc.verify(hash, this.publicKey, signature);
}
verifySchnorr(hash, signature) {
verifySchnorr(hash, signature, tweak) {
if (!ecc.verifySchnorr)
throw new Error('verifySchnorr not supported by ecc library');
return ecc.verifySchnorr(hash, this.publicKey.subarray(1, 33), signature);
if (!tweak)
return ecc.verifySchnorr(
hash,
this.publicKey.subarray(1, 33),
signature,
);
const tweakedPublicKey = ecc.pointAddScalar(this.publicKey, tweak);
if (!tweakedPublicKey) {
throw new Error('Invalid tweaked publc key!');
}
return ecc.verifySchnorr(
hash,
tweakedPublicKey.subarray(1, 33),
signature,
);
}
}
return {
Expand Down
75 changes: 75 additions & 0 deletions src/testecc.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,61 @@ function testEcc(ecc) {
h('fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364142'),
),
);
// 1 + 0 == 1
assert(
Buffer.from(
ecc.privateAdd(
h('0000000000000000000000000000000000000000000000000000000000000001'),
h('0000000000000000000000000000000000000000000000000000000000000000'),
),
).equals(
h('0000000000000000000000000000000000000000000000000000000000000001'),
),
);
// -3 + 3 == 0
assert(
ecc.privateAdd(
h('fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd036413e'),
h('0000000000000000000000000000000000000000000000000000000000000003'),
) === null,
);
assert(
Buffer.from(
ecc.privateAdd(
h('e211078564db65c3ce7704f08262b1f38f1ef412ad15b5ac2d76657a63b2c500'),
h('b51fbb69051255d1becbd683de5848242a89c229348dd72896a87ada94ae8665'),
),
).equals(
h('9730c2ee69edbb958d42db7460bafa18fef9d955325aec99044c81c8282b0a24'),
),
);
assert(
Buffer.from(
ecc.privateNegate(
h('0000000000000000000000000000000000000000000000000000000000000001'),
),
).equals(
h('fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140'),
),
);
assert(
Buffer.from(
ecc.privateNegate(
h('fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd036413e'),
),
).equals(
h('0000000000000000000000000000000000000000000000000000000000000003'),
),
);
assert(
Buffer.from(
ecc.privateNegate(
h('b1121e4088a66a28f5b6b0f5844943ecd9f610196d7bb83b25214b60452c09af'),
),
).equals(
h('4eede1bf775995d70a494f0a7bb6bc11e0b8cccd41cce8009ab1132c8b0a3792'),
),
);
assert(
Buffer.from(
ecc.pointCompress(
Expand Down Expand Up @@ -90,6 +145,26 @@ function testEcc(ecc) {
),
),
);
assert(
Buffer.from(
ecc.pointAddScalar(
h('0379be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798'),
h('0000000000000000000000000000000000000000000000000000000000000002'),
),
).equals(
h('0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798'),
),
);
assert(
Buffer.from(
ecc.pointAddScalar(
h('0379be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798'),
h('fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140'),
),
).equals(
h('03c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5'),
),
);
assert(
Buffer.from(
ecc.pointFromScalar(
Expand Down
37 changes: 37 additions & 0 deletions test/ecpair.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,26 @@ describe('ECPair', () => {
);
});

it('creates signature for tweaked private key', () => {
const kP = ECPair.fromPrivateKey(ONE, {
compressed: true,
});
const h = Buffer.alloc(32, 2);
const tweak = Buffer.from(
'3cf5216d476a5e637bf0da674e50ddf55c403270dd36494dfcca438132fa30e7',
'hex',
);
const schnorrTweakedSig = Buffer.from(
'c2cc6ba5ac926ae7a99b2dc4d410532c05787c562e5ae02fb4f204e8a4e86384ef4b436597bbc1808dcfa9f3e4223e0c2a9ab6fd4b43ab2c6d18405c43b8f0a6',
'hex',
);

assert.deepStrictEqual(
kP.signSchnorr(h, undefined, tweak).toString('hex'),
schnorrTweakedSig.toString('hex'),
);
});

it(
'wraps tinysecp.signSchnorr',
hoodwink(function (this: any): void {
Expand Down Expand Up @@ -386,6 +406,23 @@ describe('ECPair', () => {
assert.strictEqual(kP.verifySchnorr(h, schnorrsig), true);
});

it('checks signature for tweaked pubic key', () => {
const kP = ECPair.fromPrivateKey(ONE, {
compressed: false,
});
const h = Buffer.alloc(32, 2);
const tweak = Buffer.from(
'3cf5216d476a5e637bf0da674e50ddf55c403270dd36494dfcca438132fa30e7',
'hex',
);
const schnorrsig = Buffer.from(
'c2cc6ba5ac926ae7a99b2dc4d410532c05787c562e5ae02fb4f204e8a4e86384ef4b436597bbc1808dcfa9f3e4223e0c2a9ab6fd4b43ab2c6d18405c43b8f0a6',
'hex',
);

assert.strictEqual(kP.verifySchnorr(h, schnorrsig, tweak), true);
});

it(
'wraps tinysecp.verifySchnorr',
hoodwink(function (this: any): void {
Expand Down
47 changes: 40 additions & 7 deletions ts_src/ecpair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ export interface ECPairInterface extends Signer {
privateKey?: Buffer;
toWIF(): string;
verify(hash: Buffer, signature: Buffer): boolean;
verifySchnorr(hash: Buffer, signature: Buffer): boolean;
signSchnorr(hash: Buffer): Buffer;
verifySchnorr(hash: Buffer, signature: Buffer, tweak?: Buffer): boolean;
signSchnorr(hash: Buffer, extraEntropy?: Buffer, tweak?: Buffer): Buffer;
}

export interface ECPairAPI {
Expand All @@ -55,8 +55,16 @@ export interface ECPairAPI {
export interface TinySecp256k1Interface {
isPoint(p: Uint8Array): boolean;
pointCompress(p: Uint8Array, compressed?: boolean): Uint8Array;
isPrivate(d: Uint8Array): boolean;
pointFromScalar(d: Uint8Array, compressed?: boolean): Uint8Array | null;
pointAddScalar(
p: Uint8Array,
tweak: Uint8Array,
compressed?: boolean,
): Uint8Array | null;

isPrivate(d: Uint8Array): boolean;
privateAdd(d: Uint8Array, tweak: Uint8Array): Uint8Array | null;
privateNegate(d: Uint8Array): Uint8Array;

sign(h: Uint8Array, d: Uint8Array, e?: Uint8Array): Uint8Array;
signSchnorr?(h: Uint8Array, d: Uint8Array, e?: Uint8Array): Uint8Array;
Expand Down Expand Up @@ -204,21 +212,46 @@ export function ECPairFactory(ecc: TinySecp256k1Interface): ECPairAPI {
}
}

signSchnorr(hash: Buffer): Buffer {
signSchnorr(hash: Buffer, e?: Buffer, tweak?: Buffer): Buffer {
if (!this.privateKey) throw new Error('Missing private key');
if (!ecc.signSchnorr)
throw new Error('signSchnorr not supported by ecc library');
return Buffer.from(ecc.signSchnorr(hash, this.privateKey));
if (!tweak) return Buffer.from(ecc.signSchnorr(hash, this.privateKey, e));

const privateKey =
this.publicKey[0] === 2
? this.privateKey
: ecc.privateNegate(this.privateKey!);

const tweakedPrivateKey = ecc.privateAdd(privateKey, tweak);
if (!tweakedPrivateKey) {
throw new Error('Invalid tweaked private key!');
}
return Buffer.from(ecc.signSchnorr(hash, tweakedPrivateKey, e));
}

verify(hash: Buffer, signature: Buffer): boolean {
return ecc.verify(hash, this.publicKey, signature);
}

verifySchnorr(hash: Buffer, signature: Buffer): boolean {
verifySchnorr(hash: Buffer, signature: Buffer, tweak?: Buffer): boolean {
if (!ecc.verifySchnorr)
throw new Error('verifySchnorr not supported by ecc library');
return ecc.verifySchnorr(hash, this.publicKey.subarray(1, 33), signature);
if (!tweak)
return ecc.verifySchnorr(
hash,
this.publicKey.subarray(1, 33),
signature,
);
const tweakedPublicKey = ecc.pointAddScalar(this.publicKey, tweak);
if (!tweakedPublicKey) {
throw new Error('Invalid tweaked publc key!');
}
return ecc.verifySchnorr(
hash,
tweakedPublicKey.subarray(1, 33),
signature,
);
}
}

Expand Down
Loading