@@ -2,6 +2,8 @@ import assert from 'assert';
22import { ed25519 } from '@noble/curves/ed25519' ;
33import { EddsaMPSDsg , MPSUtil } from '../../../../src/tss/eddsa-mps' ;
44import { deriveUnhardenedMps } from '../../../../src/tss/eddsa-mps/derive' ;
5+ import { Ed25519Bip32HdTree } from '../../../../src/curves/ed25519Bip32HdTree' ;
6+ import { bigIntFromBufferBE , bigIntFromBufferLE , bigIntToBufferLE } from '../../../../src/util' ;
57import { generateEdDsaDKGKeyShares } from './util' ;
68
79const MESSAGE = Buffer . from ( 'The Times 03/Jan/2009 Chancellor on brink of second bailout for banks' ) ;
@@ -14,15 +16,28 @@ describe('deriveUnhardenedMps', function () {
1416 let rootPubKey : Buffer ;
1517 let userKeyShare : Buffer ;
1618 let bitgoKeyShare : Buffer ;
19+ let hdTree : Ed25519Bip32HdTree ;
1720
1821 before ( async function ( ) {
1922 const [ userDkg , , bitgoDkg ] = await generateEdDsaDKGKeyShares ( ) ;
2023 commonKeychain = userDkg . getCommonKeychain ( ) ;
2124 rootPubKey = userDkg . getSharePublicKey ( ) ;
2225 userKeyShare = userDkg . getKeyShare ( ) ;
2326 bitgoKeyShare = bitgoDkg . getKeyShare ( ) ;
27+ hdTree = await Ed25519Bip32HdTree . initialize ( ) ;
2428 } ) ;
2529
30+ // Derives a public key from commonKeychain using the standard Ed25519 BIP32
31+ // formula (same as Eddsa.deriveUnhardened). Returns the 32-byte public key.
32+ function derivePublicKeyBip32 ( keychain : string , path : string ) : Buffer {
33+ const buf = Buffer . from ( keychain , 'hex' ) ;
34+ const derived = hdTree . publicDerive (
35+ { pk : bigIntFromBufferLE ( buf . slice ( 0 , 32 ) ) , chaincode : bigIntFromBufferBE ( buf . slice ( 32 , 64 ) ) } ,
36+ path
37+ ) ;
38+ return bigIntToBufferLE ( derived . pk , 32 ) ;
39+ }
40+
2641 describe ( 'input validation' , function ( ) {
2742 it ( 'throws when commonKeychainHex is shorter than 128 chars' , function ( ) {
2843 assert . throws ( ( ) => deriveUnhardenedMps ( 'deadbeef' , 'm' ) , / e x p e c t e d 1 2 8 h e x c h a r s / ) ;
@@ -63,7 +78,7 @@ describe('deriveUnhardenedMps', function () {
6378 } ) ;
6479 } ) ;
6580
66- describe ( 'DSG signature cross-check against the public key derived by deriveUnhardenedMps ' , function ( ) {
81+ describe ( 'DSG signature cross-check against Ed25519Bip32HdTree (deriveUnhardened) ' , function ( ) {
6782 let sigAtRoot : Buffer ;
6883 let sigAtM0 : Buffer ;
6984 let sigAtM01 : Buffer ;
@@ -86,19 +101,19 @@ describe('deriveUnhardenedMps', function () {
86101 assert ( ed25519 . verify ( sigAtRoot , MESSAGE , rootPubKey ) , 'DSG at "m" should verify against the raw DKG public key' ) ;
87102 } ) ;
88103
89- it ( 'signature from DSG at "m/0" verifies against deriveUnhardenedMps(commonKeychain, "m/0") ' , function ( ) {
90- const derivedPk = Buffer . from ( deriveUnhardenedMps ( commonKeychain , 'm/0' ) . slice ( 0 , 64 ) , 'hex ') ;
104+ it ( 'signature from DSG at "m/0" verifies against Eddsa.deriveUnhardened key at "m/0"' , function ( ) {
105+ const derivedPk = derivePublicKeyBip32 ( commonKeychain , 'm/0' ) ;
91106 assert (
92107 ed25519 . verify ( sigAtM0 , MESSAGE , derivedPk ) ,
93- 'DSG at "m/0" should verify against deriveUnhardenedMps result at "m/0"'
108+ 'DSG at "m/0" should verify against Eddsa.deriveUnhardened result at "m/0"'
94109 ) ;
95110 } ) ;
96111
97- it ( 'signature from DSG at "m/0/1" verifies against deriveUnhardenedMps(commonKeychain, "m/0/1") ' , function ( ) {
98- const derivedPk = Buffer . from ( deriveUnhardenedMps ( commonKeychain , 'm/0/1' ) . slice ( 0 , 64 ) , 'hex ') ;
112+ it ( 'signature from DSG at "m/0/1" verifies against Eddsa.deriveUnhardened key at "m/0/1"' , function ( ) {
113+ const derivedPk = derivePublicKeyBip32 ( commonKeychain , 'm/0/1' ) ;
99114 assert (
100115 ed25519 . verify ( sigAtM01 , MESSAGE , derivedPk ) ,
101- 'DSG at "m/0/1" should verify against deriveUnhardenedMps result at "m/0/1"'
116+ 'DSG at "m/0/1" should verify against Eddsa.deriveUnhardened result at "m/0/1"'
102117 ) ;
103118 } ) ;
104119 } ) ;
0 commit comments