-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bls): validate proof of possession (#815)
- Loading branch information
1 parent
0e81005
commit 273b8ca
Showing
8 changed files
with
134 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { stringToBytes } from '@scure/base'; | ||
import { hexToBuffer } from '../utils/buffer'; | ||
import * as bls from './bls'; | ||
|
||
const msg = stringToBytes('utf8', 'test'); | ||
const skStr = | ||
'233428aaadf8a5d11ebba263d97b85a286750540f4abd04f109321e07b746277'; | ||
const pkStr = | ||
'adf6062df01fc18456140f7126567a84834d85b2af70454a7aacad932b92d0d7d0dab897d2f9bf46021511969f5b62f8'; | ||
const popStr = | ||
'98e8d8e33a51ecdcbcca2166370d99fdc02134e8e84ca34327cd2ec4412eb3b39619050a0146cba5d5948cb43c32a7f00f5df841700e3937d58b64e6f74493891b2a70402111841f69e9fc73236beb79f2e63e9a7caa55b724c61a139969ff57'; | ||
const sigStr = | ||
'9254acb2bfe4638daef4424b07f7a03987245c8945e634a7fca3302a2bb45e0aa9d2f8f5198e37d41aa65f8ab81efa4608d23ab55ccf06122f9718b37d42e0274297966191e3de2852f3a328727fe0dcced453c943405205b0f23038b7409e66'; | ||
|
||
describe('bls', () => { | ||
it('serializes correctly', async () => { | ||
const sk = bls.secretKeyFromBytes(skStr); | ||
expect(bls.secretKeyToBytes(sk)).toEqual(hexToBuffer(skStr)); | ||
|
||
const pk = bls.publicKeyFromBytes(pkStr); | ||
expect(bls.publicKeyToBytes(pk)).toEqual(hexToBuffer(pkStr)); | ||
|
||
const pk2 = bls.publicKeyFromBytes(hexToBuffer(pkStr)); | ||
expect(bls.publicKeyToBytes(pk2)).toEqual(hexToBuffer(pkStr)); | ||
|
||
const pop = bls.signatureFromBytes(hexToBuffer(popStr)); | ||
expect(bls.signatureToBytes(pop)).toEqual(hexToBuffer(popStr)); | ||
|
||
const sig = bls.signatureFromBytes(hexToBuffer(sigStr)); | ||
expect(bls.signatureToBytes(sig)).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('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, pkBytes)).toEqual(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { bls12_381 } from '@noble/curves/bls12-381'; | ||
import type { ProjPointType } from '@noble/curves/abstract/weierstrass'; | ||
import { hexToBuffer } from '../utils/buffer'; | ||
|
||
export type PublicKey = ProjPointType<bigint>; | ||
export type SecretKey = bigint; | ||
export type Signature = ProjPointType<typeof bls12_381.fields.Fp2.ZERO>; | ||
export type Message = ProjPointType<typeof bls12_381.fields.Fp2.ZERO>; | ||
|
||
export const PUBLIC_KEY_LENGTH = 48; | ||
export const SIGNATURE_LENGTH = 96; | ||
|
||
const signatureDST = 'BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_'; | ||
const proofOfPossessionDST = 'BLS_POP_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_'; | ||
|
||
export function secretKeyFromBytes(skBytes: Uint8Array | string): SecretKey { | ||
return bls12_381.G1.normPrivateKeyToScalar(skBytes); | ||
} | ||
|
||
export function secretKeyToBytes(sk: SecretKey): Uint8Array { | ||
return hexToBuffer(sk.toString(16)); | ||
} | ||
|
||
export function publicKeyFromBytes(pkBytes: Uint8Array | string): PublicKey { | ||
return bls12_381.G1.ProjectivePoint.fromHex(pkBytes); | ||
} | ||
|
||
export function publicKeyToBytes(pk: PublicKey): Uint8Array { | ||
return pk.toRawBytes(); | ||
} | ||
|
||
export function signatureFromBytes(sigBytes: Uint8Array): Signature { | ||
return bls12_381.Signature.fromHex(sigBytes); | ||
} | ||
|
||
export function signatureToBytes(sig: Signature): Uint8Array { | ||
return sig.toRawBytes(); | ||
} | ||
|
||
export function verify( | ||
pk: PublicKey, | ||
sig: Signature, | ||
msg: Uint8Array | string | Message, | ||
): boolean { | ||
return bls12_381.verify(sig, msg, pk, { | ||
DST: signatureDST, | ||
}); | ||
} | ||
|
||
export function verifyProofOfPossession( | ||
pk: PublicKey, | ||
sig: Signature, | ||
msg: Uint8Array | string | Message, | ||
): boolean { | ||
return bls12_381.verify(sig, msg, pk, { | ||
DST: proofOfPossessionDST, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * as secp256k1 from './secp256k1'; | ||
export * as bls from './bls'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters