Skip to content

Commit

Permalink
chore: add signing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dhrubabasu committed Feb 18, 2024
1 parent b1783b6 commit faa828f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
19 changes: 12 additions & 7 deletions src/crypto/bls.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { stringToBytes } from '@scure/base';
import { bytesToHex } from '@noble/hashes/utils';
import { hexToBuffer } from '../utils/buffer';
import * as bls from './bls';

const msgStr = 'test';
const msg = stringToBytes('utf8', 'test');
const skStr =
'233428aaadf8a5d11ebba263d97b85a286750540f4abd04f109321e07b746277';
const pkStr =
Expand Down Expand Up @@ -31,26 +32,30 @@ describe('bls', () => {
});

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

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

expect(bls.verify(pk, sig, stringToBytes('utf8', msgStr))).toEqual(true);
expect(bls.verify(pk, sig, msg)).toEqual(true);
});

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

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

it('verifies proof of possession correctly', async () => {
const pk = bls.publicKeyFromBytes(pkStr);
const pop = bls.signatureFromBytes(hexToBuffer(popStr));
const pkBytes = bls.publicKeyToBytes(pk);

expect(
bls.verifyProofOfPossession(pk, pop, bls.publicKeyToBytes(pk)),
).toEqual(true);
expect(bls.verifyProofOfPossession(pk, pop, pkBytes)).toEqual(true);
});
});
24 changes: 13 additions & 11 deletions src/crypto/bls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,17 @@ export function verifyProofOfPossession(
});
}

// TODO: Uncomment once https://github.com/paulmillr/noble-curves/pull/117 is merged.
// export function sign(msg: Uint8Array | string, sk: SecretKey): Uint8Array {
// return bls12_381.sign(msg, sk, {
// DST: signatureDST
// })
// }
export function sign(msg: Uint8Array | string, sk: SecretKey): Uint8Array {
return bls12_381.sign(msg, sk, {

Check failure on line 61 in src/crypto/bls.ts

View workflow job for this annotation

GitHub Actions / Lint, build and test

Expected 2 arguments, but got 3.
DST: signatureDST,
});
}

// export function signProofOfPossession(msg: Uint8Array | string, sk: SecretKey): Uint8Array {
// return bls12_381.sign(msg, sk, {
// DST: proofOfPossessionDST
// })
// }
export function signProofOfPossession(
msg: Uint8Array | string,
sk: SecretKey,
): Uint8Array {
return bls12_381.sign(msg, sk, {

Check failure on line 70 in src/crypto/bls.ts

View workflow job for this annotation

GitHub Actions / Lint, build and test

Expected 2 arguments, but got 3.
DST: proofOfPossessionDST,
});
}

0 comments on commit faa828f

Please sign in to comment.