BLS12-381 is a simple and easy-to-use NPM package that implements bls12-381 signing schema base on this thesis and provides derive public key, signing, verification and batch verification. The codes of the package is easy to understand for those who want to know details of implementation of pairing crypto-system and bls12-381.
- Derive public key: having secret key of type bigint, you can derive public key in point format or compressed format
- Sign: having a secret key of type bigint, you can sign any hashed message
- Verify: having public key, signature and the hashed message, you can verify if the combination matchs or not
- Aggregate public key: by having a list of public keys, you can aggregate them into one public key
- Aggregate signature: by having a list of signatures, you can aggregate them into one signature
- verify batch: having a list of public keys, a list of signatures and the hashed message, you can verify if the combination matchs or not
To install the package, run the following command:
yarn add @teleportdao/bls12-381
// or
npm install @teleportdao/bls12-381
Here's a basic example of how to use the package:
import {derivePublicKey, g1PointCompress, uncompressG2Point, g2PointCompress, sign, verify, blsSigner} from "@teleportdao/bls12-381"
Derive the public key from a private key by multiplying the base point (g1) with the private key.
Arguments
privateKey
(bigint): The private key used for deriving the public key
Returns
- (point): The derived public key in point format
Sign a hashed message using the private key.
Arguments
privateKey
(bigint): The private key used for signinghashedMessage
(point): The hashed message to be signed
Returns
- (point): The signature in point format
Verify a signature using the public key, hashed message, and signature.
Arguments
publicKey
(point): The public key used for verificationsignature
(point): The signature to be verifiedhashedMessage
(point): The hashed message to be signed
Returns
- (Boolean): True if the signature is valid, otherwise false
Aggregate public keys.
Arguments
pubKeys
(point[]): An array of public keys to be aggregated
Returns
- (point): The aggregated public key
Aggregate signatures.
Arguments
signatures
(point[]): An array of signatures to be aggregated
Returns
- (point): The aggregated signature
Aggregate signatures.
Arguments
publicKeys
(point[]): An array of public keys for verificationsignatures
(point[]): An array of signatures for verificationhashedMessage
(point): The hashed message used for batch verification
Returns
- (Boolean): True if the aggregated signatures are valid, otherwise false
For ease of use, above functions aggregated in blsSigner class with some additional consideration like working with compressed version of public key and signatures, instead of point format.
Sign a compressed hashed message and return the compressed signature.
Arguments
cHashedMsg
(string): The hashed message to be signed
Returns
- (string): Compressed signature of the hashed message
Verify if the compressed public key, compressed signature, and compressed hashed message are a valid combination.
Arguments
cPubKey
(string): Compressed public keycSig
(string): Compressed signaturecHashedMsg
(string): Compressed hashed message
Returns
- (Boolean): True if the compressed signature is valid, otherwise false
Aggregate an array of compressed public keys.
Arguments
cPubKeys
(string[]): An array of compressed public keys
Returns
- (string): The compressed aggregate of the public keys
Aggregate an array of compressed signatures.
Arguments
cSigs
(string[]): An array of compressed signatures
Returns
- (string): The compressed aggregate of the signatures
Verify an array of compressed public keys and compressed signatures against a compressed hashed message.
Arguments
cPubKeys
(string[]): An array of compressed public keyscSigs
(string[]): An array of compressed signaturescHashedMsg
(string): Compressed hashed message
Returns
- (Boolean): True if the arrays of compressed signatures is valid, otherwise false
Below are a few examples demonstrating how to use the package in various scenarios:
// functional usage
import {derivePublicKey, g1PointCompress, uncompressG2Point, g2PointCompress, sign, verify} from "@teleportdao/bls12-381"
function signAndVerify() {
let privateKey = BigInt("0x12345678901234567890")
let pubKey = derivePublicKey(privateKey)
let compressedPubKey = g1PointCompress(pubKey)
console.log("the compressed public key in hex format: ", compressedPubKey)
let hashedMessage = "8273ecc619a16d8d34d71afdf701254a3a30f282ad505af908ecccf2c6e6b32d95fedfda1d331df12edd3a5d9485ade006d40654fa9de7336e478b207afe75575e663cc8c18df7ac6c659cc01249a726e4a67fe19dffcf06f6af5cf4e2523a1a"
let unCompressedHashedMsg = uncompressG2Point(hashedMessage)
let signature = sign(privateKey, unCompressedHashedMsg)
let compressedSignature = g2PointCompress(signature)
console.log("the compressed signature in hex format: ", compressedSignature)
let isVerified = verify(pubKey, signature, unCompressedHashedMsg)
console.log("the verification result: ", isVerified)
}
// it's possible to do implement the above functionality using blsSigner class and its methods
import {blsSigner} from "@teleportdao/bls12-381"
function signAndVerifyByBlsSigner() {
let privateKey = BigInt("0x12345678901234567890")
let theSigner = new blsSigner(privateKey)
console.log("the compressed public key in hex format: ")
theSigner.displayInfo()
let hashedMessage = "8273ecc619a16d8d34d71afdf701254a3a30f282ad505af908ecccf2c6e6b32d95fedfda1d331df12edd3a5d9485ade006d40654fa9de7336e478b207afe75575e663cc8c18df7ac6c659cc01249a726e4a67fe19dffcf06f6af5cf4e2523a1a"
let signature = theSigner.signHashedMsg(hashedMessage)
console.log("the compressed signature in hex format: ", signature)
let isVerified = theSigner.verify(theSigner.cpk, signature, hashedMessage)
console.log("the verification result: ", isVerified)
}
BLS12-381 is MIT licensed.