Skip to content

Commit ce79269

Browse files
committed
fix(sdk-coin-eth): fixes to the sign and verify functions for eth tss
Ticket: bg-58807
1 parent 9aed51e commit ce79269

File tree

3 files changed

+32
-11
lines changed
  • modules

3 files changed

+32
-11
lines changed

modules/account-lib/test/unit/mpc/tss/ecdsa/ecdsa.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ describe('TSS ECDSA TESTS', function () {
121121
});
122122

123123
describe('ECDSA Signing', async function () {
124-
let config: { signerOne: ECDSA.KeyCombined; signerTwo: ECDSA.KeyCombined; hash?: Hash }[];
124+
let config: { signerOne: ECDSA.KeyCombined; signerTwo: ECDSA.KeyCombined; hash?: string; shouldHash?: boolean }[];
125125

126126
before(async () => {
127127
const [A, B, C, D, E, F] = keyShares;
@@ -137,11 +137,14 @@ describe('TSS ECDSA TESTS', function () {
137137
{ signerOne: F, signerTwo: D },
138138

139139
// Checks with specific hashing algorithm
140-
{ signerOne: A, signerTwo: B, hash: createKeccakHash('keccak256') },
140+
{ signerOne: A, signerTwo: B, hash: 'keccak256' },
141+
142+
// checks with no hashing
143+
{ signerOne: A, signerTwo: B, shouldHash: false },
141144
];
142145
});
143146

144-
for (let index = 0; index < 3; index++) {
147+
for (let index = 0; index < 8; index++) {
145148
it(`should properly sign the message case ${index}`, async function () {
146149
// Step One
147150
// signerOne, signerTwo have decided to sign the message
@@ -213,9 +216,24 @@ describe('TSS ECDSA TESTS', function () {
213216
// and finally signs the message using their private OShare
214217
// and delta share received from the other signer
215218

219+
const hashGenerator = (hashType?: string): Hash | undefined => {
220+
return hashType === 'keccak256' ? createKeccakHash('keccak256') : undefined;
221+
};
216222
const [signA, signB] = [
217-
MPC.sign(MESSAGE, signCombineOne.oShare, signCombineTwo.dShare, config[index].hash),
218-
MPC.sign(MESSAGE, signCombineTwo.oShare, signCombineOne.dShare, config[index].hash),
223+
MPC.sign(
224+
MESSAGE,
225+
signCombineOne.oShare,
226+
signCombineTwo.dShare,
227+
hashGenerator(config[index].hash),
228+
config[index].shouldHash,
229+
),
230+
MPC.sign(
231+
MESSAGE,
232+
signCombineTwo.oShare,
233+
signCombineOne.dShare,
234+
hashGenerator(config[index].hash),
235+
config[index].shouldHash,
236+
),
219237
];
220238

221239
// Step Eight
@@ -226,7 +244,7 @@ describe('TSS ECDSA TESTS', function () {
226244
// Step Nine
227245
// Verify signature
228246

229-
const isValid = MPC.verify(MESSAGE, signature, config[index].hash);
247+
const isValid = MPC.verify(MESSAGE, signature, hashGenerator(config[index].hash), config[index].shouldHash);
230248
isValid.should.equal(true);
231249
});
232250
}

modules/sdk-core/src/account-lib/mpc/tss/ecdsa/ecdsa.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,10 +313,11 @@ export default class Ecdsa {
313313
* @param {OShare} oShare private omicron share of current participant
314314
* @param {DShare} dShare delta share received from the other participant
315315
* @param {Hash} hash hashing algorithm implementing Node`s standard crypto hash interface
316+
* @param {boolean} shouldHash if true, we hash the provided buffer before signing
316317
* @returns {SShare}
317318
*/
318-
sign(M: Buffer, oShare: OShare, dShare: DShare, hash?: Hash): SShare {
319-
const m = (hash || createHash('sha256')).update(M).digest();
319+
sign(M: Buffer, oShare: OShare, dShare: DShare, hash?: Hash, shouldHash = true): SShare {
320+
const m = shouldHash ? (hash || createHash('sha256')).update(M).digest() : M;
320321

321322
const delta = Ecdsa.curve.scalarAdd(hexToBigInt(oShare.delta), hexToBigInt(dShare.delta));
322323

@@ -370,11 +371,13 @@ export default class Ecdsa {
370371
* @param {Buffer} message
371372
* @param {Signature } signature
372373
* @param {Hash} hash hashing algorithm implementing Node`s standard crypto hash interface
374+
* @param {boolean} shouldHash if true, we hash the provided buffer before verifying
373375
* @returns {boolean} True if signature is valid; False otherwise
374376
*/
375-
verify(message: Buffer, signature: Signature, hash?: Hash): boolean {
377+
verify(message: Buffer, signature: Signature, hash?: Hash, shouldHash = true): boolean {
378+
const messageToVerify = shouldHash ? (hash || createHash('sha256')).update(message).digest() : message;
376379
return Ecdsa.curve.verify(
377-
(hash || createHash('sha256')).update(message).digest(),
380+
messageToVerify,
378381
Buffer.concat([
379382
Buffer.from([signature['recid']]),
380383
bigIntToBufferBE(hexToBigInt(signature['r']), 32),

modules/sdk-core/src/bitgo/utils/tss/ecdsa/ecdsa.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ export class EcdsaUtils extends baseTSSUtils<KeyShare> {
309309
let signablePayload;
310310

311311
if (requestType === RequestType.tx) {
312-
signablePayload = Buffer.from(txRequestResolved.transactions[0].unsignedTx.serializedTxHex, 'hex');
312+
signablePayload = Buffer.from(txRequestResolved.transactions[0].unsignedTx.signableHex, 'hex');
313313
} else if (requestType === RequestType.message) {
314314
assert(txRequestResolved.unsignedMessages?.[0]);
315315
signablePayload = Buffer.from(txRequestResolved.unsignedMessages[0].message, 'hex');

0 commit comments

Comments
 (0)