|
| 1 | +import assert from 'assert'; |
| 2 | +import { ed25519 } from '@noble/curves/ed25519'; |
| 3 | +import { deriveUnhardenedMps } from '../../../../src/tss/eddsa-mps/derive'; |
| 4 | +import { generateEdDsaDKGKeyShares, runEdDsaDSG } from './util'; |
| 5 | + |
| 6 | +const MESSAGE = Buffer.from('The Times 03/Jan/2009 Chancellor on brink of second bailout for banks'); |
| 7 | + |
| 8 | +describe('deriveUnhardenedMps', function () { |
| 9 | + this.timeout(60_000); |
| 10 | + |
| 11 | + // DKG is expensive; run once and reuse across tests. |
| 12 | + let commonKeychain: string; |
| 13 | + let rootPubKey: Buffer; |
| 14 | + let userKeyShare: Buffer; |
| 15 | + let bitgoKeyShare: Buffer; |
| 16 | + |
| 17 | + before(async function () { |
| 18 | + const [userDkg, , bitgoDkg] = await generateEdDsaDKGKeyShares(); |
| 19 | + commonKeychain = userDkg.getCommonKeychain(); |
| 20 | + rootPubKey = userDkg.getSharePublicKey(); |
| 21 | + userKeyShare = userDkg.getKeyShare(); |
| 22 | + bitgoKeyShare = bitgoDkg.getKeyShare(); |
| 23 | + }); |
| 24 | + |
| 25 | + describe('input validation', function () { |
| 26 | + it('throws when commonKeychainHex is shorter than 128 chars', function () { |
| 27 | + assert.throws(() => deriveUnhardenedMps('deadbeef', 'm'), /expected 128 hex chars/); |
| 28 | + }); |
| 29 | + |
| 30 | + it('throws when commonKeychainHex is longer than 128 chars', function () { |
| 31 | + assert.throws(() => deriveUnhardenedMps('a'.repeat(130), 'm'), /expected 128 hex chars/); |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + describe('derivation correctness using existing deriveUnhardened path parsing', function () { |
| 36 | + it('returns the root key unchanged for path "m"', function () { |
| 37 | + const result = deriveUnhardenedMps(commonKeychain, 'm'); |
| 38 | + assert.strictEqual(result, commonKeychain, 'Path "m" should return the keychain unchanged'); |
| 39 | + }); |
| 40 | + |
| 41 | + it('produces a different key at m/0 than at the root', function () { |
| 42 | + const derived = deriveUnhardenedMps(commonKeychain, 'm/0'); |
| 43 | + assert.notStrictEqual(derived.slice(0, 64), commonKeychain.slice(0, 64)); |
| 44 | + }); |
| 45 | + |
| 46 | + it('is deterministic — same inputs produce the same output', function () { |
| 47 | + const a = deriveUnhardenedMps(commonKeychain, 'm/0/1'); |
| 48 | + const b = deriveUnhardenedMps(commonKeychain, 'm/0/1'); |
| 49 | + assert.strictEqual(a, b); |
| 50 | + }); |
| 51 | + |
| 52 | + it('produces different keys for different paths', function () { |
| 53 | + const d0 = deriveUnhardenedMps(commonKeychain, 'm/0'); |
| 54 | + const d1 = deriveUnhardenedMps(commonKeychain, 'm/1'); |
| 55 | + assert.notStrictEqual(d0.slice(0, 64), d1.slice(0, 64)); |
| 56 | + }); |
| 57 | + |
| 58 | + it('output is always 128 hex chars', function () { |
| 59 | + assert.strictEqual(deriveUnhardenedMps(commonKeychain, 'm').length, 128); |
| 60 | + assert.strictEqual(deriveUnhardenedMps(commonKeychain, 'm/0').length, 128); |
| 61 | + assert.strictEqual(deriveUnhardenedMps(commonKeychain, 'm/0/1').length, 128); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + describe('DSG signature cross-check against the public key derived by deriveUnhardenedMps', function () { |
| 66 | + it('signature from DSG at "m" verifies against the root public key', function () { |
| 67 | + const { dsgA } = runEdDsaDSG(userKeyShare, bitgoKeyShare, 0, 2, MESSAGE, 'm'); |
| 68 | + const sig = dsgA.getSignature(); |
| 69 | + assert(ed25519.verify(sig, MESSAGE, rootPubKey), 'DSG at "m" should verify against the raw DKG public key'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('signature from DSG at "m/0" verifies against deriveUnhardenedMps(commonKeychain, "m/0")', function () { |
| 73 | + const derivedPk = Buffer.from(deriveUnhardenedMps(commonKeychain, 'm/0').slice(0, 64), 'hex'); |
| 74 | + const { dsgA } = runEdDsaDSG(userKeyShare, bitgoKeyShare, 0, 2, MESSAGE, 'm/0'); |
| 75 | + const sig = dsgA.getSignature(); |
| 76 | + assert( |
| 77 | + ed25519.verify(sig, MESSAGE, derivedPk), |
| 78 | + 'DSG at "m/0" should verify against deriveUnhardenedMps result at "m/0"' |
| 79 | + ); |
| 80 | + }); |
| 81 | + |
| 82 | + it('signature from DSG at "m/0/1" verifies against deriveUnhardenedMps(commonKeychain, "m/0/1")', function () { |
| 83 | + const derivedPk = Buffer.from(deriveUnhardenedMps(commonKeychain, 'm/0/1').slice(0, 64), 'hex'); |
| 84 | + const { dsgA } = runEdDsaDSG(userKeyShare, bitgoKeyShare, 0, 2, MESSAGE, 'm/0/1'); |
| 85 | + const sig = dsgA.getSignature(); |
| 86 | + assert( |
| 87 | + ed25519.verify(sig, MESSAGE, derivedPk), |
| 88 | + 'DSG at "m/0/1" should verify against deriveUnhardenedMps result at "m/0/1"' |
| 89 | + ); |
| 90 | + }); |
| 91 | + }); |
| 92 | +}); |
0 commit comments