From f58a6159ea4c9146dc76adcc2c205e579387c7fe Mon Sep 17 00:00:00 2001 From: Anson Date: Thu, 10 Oct 2024 18:10:05 +0100 Subject: [PATCH 1/2] feat(bls): remove case 218 as we don't use it --- packages/crypto/src/lib/crypto.ts | 34 +++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/packages/crypto/src/lib/crypto.ts b/packages/crypto/src/lib/crypto.ts index e2dd3782d1..935062f41f 100644 --- a/packages/crypto/src/lib/crypto.ts +++ b/packages/crypto/src/lib/crypto.ts @@ -45,6 +45,7 @@ export interface BlsSignatureShare { /** * Encrypt data with a BLS public key. + * We are using G1 for encryption and G2 for signatures * * @param publicKeyHex hex-encoded string of the BLS public key to encrypt with * @param message Uint8Array of the data to encrypt @@ -58,18 +59,29 @@ export const encrypt = async ( ): Promise => { const publicKey = Buffer.from(publicKeyHex, 'hex'); - switch (publicKeyHex.replace('0x', '').length) { - case 218: - return Buffer.from( - await blsEncrypt('Bls12381G2', publicKey, message, identity) - ).toString('hex'); - case 96: - return Buffer.from( - await blsEncrypt('Bls12381G2', publicKey, message, identity) - ).toString('base64'); - default: - return ''; + /** + * Our system uses BLS12-381 on the G1 curve for encryption. + * However, on the SDK side (this function), we expect the public key + * to use the G2 curve for signature purposes, hence the switch on public key length. + * + * The G2 curve, `Bls12381G2`, is typically associated with signature generation/verification, + * while G1 is associated with encryption. Here, the length of the public key determines how + * we handle the encryption and the format of the returned encrypted message. + */ + if (publicKeyHex.replace('0x', '').length !== 96) { + throw new InvalidParamType( + { + info: { + publicKeyHex, + }, + }, + `Invalid public key length. Expecting 96 characters, got ${publicKeyHex.replace('0x', '').length} instead.` + ); } + return Buffer.from( + await blsEncrypt('Bls12381G2', publicKey, message, identity) + ).toString('base64'); + }; /** From 5453dfad6c5bc71b8543de5cf32104e7fa79cbcb Mon Sep 17 00:00:00 2001 From: Anson Date: Thu, 10 Oct 2024 20:04:08 +0100 Subject: [PATCH 2/2] fmt --- packages/crypto/src/lib/crypto.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/crypto/src/lib/crypto.ts b/packages/crypto/src/lib/crypto.ts index 935062f41f..7e0846a4fd 100644 --- a/packages/crypto/src/lib/crypto.ts +++ b/packages/crypto/src/lib/crypto.ts @@ -65,7 +65,7 @@ export const encrypt = async ( * to use the G2 curve for signature purposes, hence the switch on public key length. * * The G2 curve, `Bls12381G2`, is typically associated with signature generation/verification, - * while G1 is associated with encryption. Here, the length of the public key determines how + * while G1 is associated with encryption. Here, the length of the public key determines how * we handle the encryption and the format of the returned encrypted message. */ if (publicKeyHex.replace('0x', '').length !== 96) { @@ -75,13 +75,14 @@ export const encrypt = async ( publicKeyHex, }, }, - `Invalid public key length. Expecting 96 characters, got ${publicKeyHex.replace('0x', '').length} instead.` + `Invalid public key length. Expecting 96 characters, got ${ + publicKeyHex.replace('0x', '').length + } instead.` ); } return Buffer.from( await blsEncrypt('Bls12381G2', publicKey, message, identity) ).toString('base64'); - }; /**