Skip to content

Commit 602b8d6

Browse files
committed
fix: pkpSign with P384 testing and a problematic ecdsa signature concatenation with the wrong recovery param (using eth value)
1 parent 57e2d4f commit 602b8d6

File tree

4 files changed

+42
-36
lines changed

4 files changed

+42
-36
lines changed

local-tests/tests/testUseEoaSessionSigsToPkpSignK256.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ export const testUseEoaSessionSigsToPkpSignK256 = async (
1616
) => {
1717
const alice = await devEnv.createRandomPerson();
1818
const messageToSign = [1, 2, 3, 4, 5];
19-
const messageHash = createHash('sha256').update(Buffer.from(messageToSign)).digest();
19+
const messageHash = createHash('sha256')
20+
.update(Buffer.from(messageToSign))
21+
.digest();
2022

2123
const eoaSessionSigs = await getEoaSessionSigs(devEnv, alice);
2224
const runWithSessionSigs = await devEnv.litNodeClient.pkpSign({
@@ -66,7 +68,10 @@ export const testUseEoaSessionSigsToPkpSignK256 = async (
6668
.keyFromPublic(runWithSessionSigs.publicKey, 'hex')
6769
.getPublic(false, 'hex');
6870

69-
if (runWithSessionSigsUncompressedPublicKey !== recoveredPubKey.encode('hex', false)) {
71+
if (
72+
runWithSessionSigsUncompressedPublicKey !==
73+
recoveredPubKey.encode('hex', false)
74+
) {
7075
throw new Error(
7176
`Expected recovered public key to match runWithSessionSigsUncompressedPublicKey and recoveredPubKey.encode('hex', false)`
7277
);

local-tests/tests/testUseEoaSessionSigsToPkpSignP256.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ export const testUseEoaSessionSigsToPkpSignP256 = async (
1616
) => {
1717
const alice = await devEnv.createRandomPerson();
1818
const messageToSign = [1, 2, 3, 4, 5];
19-
const messageHash = createHash('sha256').update(Buffer.from(messageToSign)).digest();
19+
const messageHash = createHash('sha256')
20+
.update(Buffer.from(messageToSign))
21+
.digest();
2022

2123
const eoaSessionSigs = await getEoaSessionSigs(devEnv, alice);
2224
const runWithSessionSigs = await devEnv.litNodeClient.pkpSign({
@@ -66,7 +68,10 @@ export const testUseEoaSessionSigsToPkpSignP256 = async (
6668
.keyFromPublic(runWithSessionSigs.publicKey, 'hex')
6769
.getPublic(false, 'hex');
6870

69-
if (runWithSessionSigsUncompressedPublicKey !== recoveredPubKey.encode('hex', false)) {
71+
if (
72+
runWithSessionSigsUncompressedPublicKey !==
73+
recoveredPubKey.encode('hex', false)
74+
) {
7075
throw new Error(
7176
`Expected recovered public key to match runWithSessionSigsUncompressedPublicKey and recoveredPubKey.encode('hex', false)`
7277
);
Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ethers } from 'ethers';
1+
import EC from 'elliptic';
22
import { createHash } from 'crypto';
33

44
import { log } from '@lit-protocol/misc';
@@ -15,8 +15,10 @@ export const testUseEoaSessionSigsToPkpSignP384 = async (
1515
devEnv: TinnyEnvironment
1616
) => {
1717
const alice = await devEnv.createRandomPerson();
18-
const messageToSign = new Uint8Array([1, 2, 3, 4, 5]);
19-
const messageHash = createHash('sha384').update(messageToSign).digest();
18+
const messageToSign = [1, 2, 3, 4, 5];
19+
const messageHash = createHash('sha384')
20+
.update(Buffer.from(messageToSign))
21+
.digest();
2022

2123
const eoaSessionSigs = await getEoaSessionSigs(devEnv, alice);
2224
const runWithSessionSigs = await devEnv.litNodeClient.pkpSign({
@@ -28,16 +30,6 @@ export const testUseEoaSessionSigsToPkpSignP384 = async (
2830

2931
devEnv.releasePrivateKeyFromUser(alice);
3032

31-
// Expected output:
32-
// {
33-
// r: "25fc0d2fecde8ed801e9fee5ad26f2cf61d82e6f45c8ad1ad1e4798d3b747fd9",
34-
// s: "549fe745b4a09536e6e7108d814cf7e44b93f1d73c41931b8d57d1b101833214",
35-
// recid: 1,
36-
// signature: "0x25fc0d2fecde8ed801e9fee5ad26f2cf61d82e6f45c8ad1ad1e4798d3b747fd9549fe745b4a09536e6e7108d814cf7e44b93f1d73c41931b8d57d1b1018332141c",
37-
// publicKey: "04A3CD53CCF63597D3FFCD1DF1E8236F642C7DF8196F532C8104625635DC55A1EE59ABD2959077432FF635DF2CED36CC153050902B71291C4D4867E7DAAF964049",
38-
// dataSigned: "7D87C5EA75F7378BB701E404C50639161AF3EFF66293E9F375B5F17EB50476F4",
39-
// }
40-
4133
// -- assertions
4234
// r, s, dataSigned, and public key should be present
4335
if (!runWithSessionSigs.r) {
@@ -63,29 +55,33 @@ export const testUseEoaSessionSigsToPkpSignP384 = async (
6355
throw new Error(`Expected "recid" to be parseable as a number`);
6456
}
6557

66-
// TODO fix after fixing P256
67-
const signature = ethers.utils.joinSignature({
68-
r: '0x' + runWithSessionSigs.r,
69-
s: '0x' + runWithSessionSigs.s,
70-
recoveryParam: runWithSessionSigs.recid,
71-
});
72-
const recoveredPubKey = ethers.utils.recoverPublicKey(
73-
alice.loveLetter,
74-
signature
75-
);
58+
const ec = new EC.ec('p384');
7659

77-
console.log('recoveredPubKey:', recoveredPubKey);
60+
// Public key derived from message and signature
61+
const recoveredPubKey = ec.recoverPubKey(
62+
messageHash,
63+
runWithSessionSigs,
64+
runWithSessionSigs.recid
65+
); // Error: The recovery param is more than two bits
66+
// Public key returned from nodes
67+
const runWithSessionSigsUncompressedPublicKey = ec
68+
.keyFromPublic(runWithSessionSigs.publicKey, 'hex')
69+
.getPublic(false, 'hex');
7870

79-
if (recoveredPubKey !== `0x${runWithSessionSigs.publicKey.toLowerCase()}`) {
71+
if (
72+
runWithSessionSigsUncompressedPublicKey !==
73+
recoveredPubKey.encode('hex', false)
74+
) {
8075
throw new Error(
81-
`Expected recovered public key to match runWithSessionSigs.publicKey`
82-
);
83-
}
84-
if (recoveredPubKey !== `0x${alice.pkp.publicKey.toLowerCase()}`) {
85-
throw new Error(
86-
`Expected recovered public key to match alice.pkp.publicKey`
76+
`Expected recovered public key to match runWithSessionSigsUncompressedPublicKey and recoveredPubKey.encode('hex', false)`
8777
);
8878
}
79+
// PKP public key lives in k256, it cannot be directly compared
80+
// if (recoveredPubKey.encode('hex', false) !== alice.pkp.publicKey) {
81+
// throw new Error(
82+
// `Expected recovered public key to match alice.pkp.publicKey`
83+
// );
84+
// }
8985

9086
log('✅ testUseEoaSessionSigsToPkpSignP384');
9187
};

packages/crypto/src/lib/crypto.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ export const combineEcdsaShares = async (
259259
await ecdsaVerify(variant!, messageHash, publicKey, [r, s, recId]);
260260

261261
const signature = splitEcdsaSignature(
262-
Buffer.concat([r, s, Buffer.from([recId + 27])])
262+
Buffer.concat([r, s, Buffer.from([recId])])
263263
);
264264

265265
return {

0 commit comments

Comments
 (0)