|
| 1 | +/** |
| 2 | + * Wire-format compatibility regression tests for EdDSA MPCv2 keyshares. |
| 3 | + * |
| 4 | + * These tests load *frozen* keyshare bytes captured at a specific @bitgo/sdk-lib-mpc |
| 5 | + * version. If @bitgo/wasm-mps changes its bincode layout, DSG round 0 will throw |
| 6 | + * while deserializing the fixture bytes — the earliest possible signal of a |
| 7 | + * breaking change before it reaches production. |
| 8 | + */ |
| 9 | +import assert from 'assert'; |
| 10 | +import crypto from 'crypto'; |
| 11 | +import { ed25519 } from '@noble/curves/ed25519'; |
| 12 | +import { EddsaMPSDsg, MPSTypes, MPSUtil } from '../../../../src/tss/eddsa-mps'; |
| 13 | +import { EDDSA_MPCV2_FIXTURES } from '../../../fixtures/eddsaMPCv2Keyshares'; |
| 14 | + |
| 15 | +const MESSAGE = Buffer.from('WCI-1476 EdDSA MPCv2 wire-format regression sentinel'); |
| 16 | + |
| 17 | +describe('EdDSA MPCv2 wire-format compatibility', function () { |
| 18 | + it('should deserialize frozen user keyshare without error (bincode format guard)', async function () { |
| 19 | + const dsg = new EddsaMPSDsg.DSG(0); |
| 20 | + await dsg.initDsg(EDDSA_MPCV2_FIXTURES.userKeyShare, MESSAGE, 'm', 2); |
| 21 | + const msg = dsg.getFirstMessage(); |
| 22 | + assert(msg.payload.length > 0, 'DSG round 0 must produce a non-empty message'); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should deserialize frozen backup keyshare without error', async function () { |
| 26 | + const dsg = new EddsaMPSDsg.DSG(1); |
| 27 | + await dsg.initDsg(EDDSA_MPCV2_FIXTURES.backupKeyShare, MESSAGE, 'm', 2); |
| 28 | + const msg = dsg.getFirstMessage(); |
| 29 | + assert(msg.payload.length > 0, 'Backup DSG round 0 must produce a non-empty message'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should deserialize frozen bitgo keyshare without error', async function () { |
| 33 | + const dsg = new EddsaMPSDsg.DSG(2); |
| 34 | + await dsg.initDsg(EDDSA_MPCV2_FIXTURES.bitgoKeyShare, MESSAGE, 'm', 0); |
| 35 | + const msg = dsg.getFirstMessage(); |
| 36 | + assert(msg.payload.length > 0, 'BitGo DSG round 0 must produce a non-empty message'); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should produce a valid signature from frozen user+bitgo keyshares', async function () { |
| 40 | + const sig = (await MPSUtil.executeTillRound( |
| 41 | + 3, |
| 42 | + new EddsaMPSDsg.DSG(0), |
| 43 | + new EddsaMPSDsg.DSG(2), |
| 44 | + EDDSA_MPCV2_FIXTURES.userKeyShare, |
| 45 | + EDDSA_MPCV2_FIXTURES.bitgoKeyShare, |
| 46 | + MESSAGE, |
| 47 | + 'm' |
| 48 | + )) as Buffer; |
| 49 | + |
| 50 | + assert.strictEqual(sig.length, 64, 'Signature must be 64 bytes'); |
| 51 | + |
| 52 | + const pubKeyHex = EDDSA_MPCV2_FIXTURES.commonKeychain.slice(0, 64); |
| 53 | + const pubKey = Buffer.from(pubKeyHex, 'hex'); |
| 54 | + assert(ed25519.verify(sig, MESSAGE, pubKey), 'Signature must verify under the frozen commonKeychain public key'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should produce a valid signature from frozen user+backup keyshares', async function () { |
| 58 | + const sig = (await MPSUtil.executeTillRound( |
| 59 | + 3, |
| 60 | + new EddsaMPSDsg.DSG(0), |
| 61 | + new EddsaMPSDsg.DSG(1), |
| 62 | + EDDSA_MPCV2_FIXTURES.userKeyShare, |
| 63 | + EDDSA_MPCV2_FIXTURES.backupKeyShare, |
| 64 | + MESSAGE, |
| 65 | + 'm' |
| 66 | + )) as Buffer; |
| 67 | + |
| 68 | + const pubKeyHex = EDDSA_MPCV2_FIXTURES.commonKeychain.slice(0, 64); |
| 69 | + const pubKey = Buffer.from(pubKeyHex, 'hex'); |
| 70 | + assert(ed25519.verify(sig, MESSAGE, pubKey), 'Signature must verify under the frozen commonKeychain public key'); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should decode frozen reducedKeyShare and match commonKeychain fields', function () { |
| 74 | + const decoded = MPSTypes.getDecodedReducedKeyShare(EDDSA_MPCV2_FIXTURES.userReducedKeyShare); |
| 75 | + |
| 76 | + const pubHex = EDDSA_MPCV2_FIXTURES.commonKeychain.slice(0, 64); |
| 77 | + const chaincodeHex = EDDSA_MPCV2_FIXTURES.commonKeychain.slice(64); |
| 78 | + |
| 79 | + assert.strictEqual( |
| 80 | + Buffer.from(decoded.pub).toString('hex'), |
| 81 | + pubHex, |
| 82 | + 'Decoded pub must match commonKeychain pubkey' |
| 83 | + ); |
| 84 | + assert.strictEqual( |
| 85 | + Buffer.from(decoded.rootChainCode).toString('hex'), |
| 86 | + chaincodeHex, |
| 87 | + 'Decoded rootChainCode must match commonKeychain chaincode' |
| 88 | + ); |
| 89 | + assert(decoded.keyShare.length > 0, 'keyShare in reducedKeyShare must be non-empty'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should reject randomised bytes as a keyshare (guard validation)', async function () { |
| 93 | + const randomBytes = Buffer.from(crypto.randomBytes(EDDSA_MPCV2_FIXTURES.userKeyShare.length)); |
| 94 | + const dsg = new EddsaMPSDsg.DSG(0); |
| 95 | + await dsg.initDsg(randomBytes, MESSAGE, 'm', 2); |
| 96 | + assert.throws( |
| 97 | + () => dsg.getFirstMessage(), |
| 98 | + /Error while creating/, |
| 99 | + 'DSG round 0 must reject random bytes that are not a valid bincode keyshare' |
| 100 | + ); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should produce a valid signature at a derived path from frozen keyshares', async function () { |
| 104 | + const sig = (await MPSUtil.executeTillRound( |
| 105 | + 3, |
| 106 | + new EddsaMPSDsg.DSG(0), |
| 107 | + new EddsaMPSDsg.DSG(2), |
| 108 | + EDDSA_MPCV2_FIXTURES.userKeyShare, |
| 109 | + EDDSA_MPCV2_FIXTURES.bitgoKeyShare, |
| 110 | + MESSAGE, |
| 111 | + 'm/0/1' |
| 112 | + )) as Buffer; |
| 113 | + |
| 114 | + assert.strictEqual(sig.length, 64, 'Derived-path signature must be 64 bytes'); |
| 115 | + }); |
| 116 | +}); |
0 commit comments