Skip to content

Commit

Permalink
feat(bls): add signing functions (#817)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhrubabasu authored Feb 20, 2024
1 parent 273b8ca commit 8cb2fbe
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/crypto/bls.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,26 @@ describe('bls', () => {
expect(bls.signatureToBytes(sig)).toEqual(hexToBuffer(sigStr));
});

it('generates signature correctly', async () => {
const sk = bls.secretKeyFromBytes(skStr);
expect(bls.sign(msg, sk)).toEqual(hexToBuffer(sigStr));
});

it('verifies signature correctly', async () => {
const pk = bls.publicKeyFromBytes(pkStr);
const sig = bls.signatureFromBytes(hexToBuffer(sigStr));

expect(bls.verify(pk, sig, msg)).toEqual(true);
});

it('generates proof of possession correctly', async () => {
const sk = bls.secretKeyFromBytes(skStr);
const pk = bls.publicKeyFromBytes(pkStr);
const pkBytes = bls.publicKeyToBytes(pk);

expect(bls.signProofOfPossession(pkBytes, sk)).toEqual(hexToBuffer(popStr));
});

it('verifies proof of possession correctly', async () => {
const pk = bls.publicKeyFromBytes(pkStr);
const pop = bls.signatureFromBytes(hexToBuffer(popStr));
Expand Down
19 changes: 19 additions & 0 deletions src/crypto/bls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,22 @@ export function verifyProofOfPossession(
DST: proofOfPossessionDST,
});
}

export function sign(msg: Uint8Array | string, sk: SecretKey): Uint8Array {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error Will error until a version of @noble/curves is released with https://github.com/paulmillr/noble-curves/pull/117
return bls12_381.sign(msg, sk, {
DST: signatureDST,
});
}

export function signProofOfPossession(
msg: Uint8Array | string,
sk: SecretKey,
): Uint8Array {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error Will error until a version of @noble/curves is released with https://github.com/paulmillr/noble-curves/pull/117
return bls12_381.sign(msg, sk, {
DST: proofOfPossessionDST,
});
}

0 comments on commit 8cb2fbe

Please sign in to comment.