Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/add-derivation-path-test-dkls'
Browse files Browse the repository at this point in the history
  • Loading branch information
islamaminBitGo committed Feb 15, 2024
2 parents ebc1b16 + 7ab038f commit 0b3504e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
3 changes: 2 additions & 1 deletion modules/sdk-lib-mpc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"devDependencies": {
"@types/lodash": "^4.14.151",
"@types/node": "^20.11.19",
"nyc": "^15.0.0"
"nyc": "^15.0.0",
"secp256k1": "5.0.0"
}
}
43 changes: 40 additions & 3 deletions modules/sdk-lib-mpc/test/unit/tss/ecdsa/dklsDsg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,37 @@ import { DklsDsg } from '../../../../src/tss/ecdsa-dkls';
import * as fs from 'fs';
import * as crypto from 'crypto';
import should from 'should';
import { Keyshare } from '@silencelaboratories/dkls-wasm-ll-node';
import { decode } from 'cbor';
import { Secp256k1Bip32HdTree, bigIntFromBufferBE, bigIntToBufferBE } from '../../../../src';

import * as secp256k1 from 'secp256k1';

describe('DKLS Dsg 2x3', function () {
const vectors = [
{
party1: 0,
party2: 1,
msgToSign: 'ffff',
derivationPath: 'm',
},
{
party1: 0,
party2: 2,
msgToSign: 'ffff',
derivationPath: 'm/0',
},
{
party1: 1,
party2: 2,
msgToSign: 'ffff',
derivationPath: 'm/0/0/0',
},
{
party1: 1,
party2: 2,
msgToSign: 'ffff',
derivationPath: 'm/0/9/10',
},
];
// To generate the fixtures, run DKG as in the dklsDkg.ts tests and save the resulting party.getKeyShare in a file by doing fs.writeSync(party.getKeyShare()).
Expand All @@ -28,17 +42,17 @@ describe('DKLS Dsg 2x3', function () {
`${__dirname}/fixtures/bitgoShare`,
];
vectors.forEach(async function (vector) {
it(`should create signatures for parties ${vector.party1} and ${vector.party2}`, async function () {
it(`should create signatures for parties ${vector.party1} and ${vector.party2} with derivation`, async function () {
const party1 = new DklsDsg.Dsg(
fs.readFileSync(shareFiles[vector.party1]),
vector.party1,
'm',
vector.derivationPath,
crypto.createHash('sha256').update(Buffer.from(vector.msgToSign, 'hex')).digest()
);
const party2 = new DklsDsg.Dsg(
fs.readFileSync(shareFiles[vector.party2]),
vector.party2,
'm',
vector.derivationPath,
crypto.createHash('sha256').update(Buffer.from(vector.msgToSign, 'hex')).digest()
);
// Round 1 ////
Expand Down Expand Up @@ -83,6 +97,29 @@ describe('DKLS Dsg 2x3', function () {
broadcastMessages: party1Round4Messages.broadcastMessages,
});
party1.signature.should.deepEqual(party2.signature);
const keyShare: Keyshare = Keyshare.fromBytes(fs.readFileSync(shareFiles[vector.party1]));
const pk = bigIntFromBufferBE(Buffer.from(keyShare.publicKey));
const chaincode = bigIntFromBufferBE(Buffer.from(decode(keyShare.toBytes()).root_chain_code));
const hdTree = new Secp256k1Bip32HdTree();
const derivedKey = hdTree.publicDerive({ pk: pk, chaincode: chaincode }, vector.derivationPath);
const pub1 = secp256k1.ecdsaRecover(
Buffer.concat([party1.signature.R, party1.signature.S]),
0,
crypto.createHash('sha256').update(Buffer.from(vector.msgToSign, 'hex')).digest(),
true
);
const pub2 = secp256k1.ecdsaRecover(
Buffer.concat([party1.signature.R, party1.signature.S]),
1,
crypto.createHash('sha256').update(Buffer.from(vector.msgToSign, 'hex')).digest(),
true
);
const derivedPub =
vector.derivationPath === 'm' ? keyShare.publicKey : new Uint8Array(bigIntToBufferBE(derivedKey.pk));
(
(pub1.every((p) => derivedPub.includes(p)) && derivedPub.every((p) => pub1.includes(p))) ||
(pub2.every((p) => derivedPub.includes(p)) && derivedPub.every((p) => pub2.includes(p)))
).should.equal(true);
});
});

Expand Down

0 comments on commit 0b3504e

Please sign in to comment.