npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@teleportdao/bls12-381

v0.1.1

Published

an implementation of bls12-381 in typescript

Downloads

5

Readme

BLS12-381

npm version

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.

Features

  • 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

Installation

To install the package, run the following command:

yarn add @teleportdao/bls12-381

// or

npm install @teleportdao/bls12-381

Usage

Here's a basic example of how to use the package:


import {derivePublicKey, g1PointCompress, uncompressG2Point, g2PointCompress, sign, verify, blsSigner} from "@teleportdao/bls12-381"

API

function derivePublicKey(privateKey: bigint): point

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

...

function sign(privateKey: bigint, hashedMessage: point): point

Sign a hashed message using the private key.

Arguments

  • privateKey (bigint): The private key used for signing
  • hashedMessage (point): The hashed message to be signed

Returns

  • (point): The signature in point format

...

function verify(publicKey: point, signature: point, hashedMessage: point): Boolean

Verify a signature using the public key, hashed message, and signature.

Arguments

  • publicKey (point): The public key used for verification
  • signature (point): The signature to be verified
  • hashedMessage (point): The hashed message to be signed

Returns

  • (Boolean): True if the signature is valid, otherwise false

...

function aggregatePublicKeys(pubKeys: point[]): point

Aggregate public keys.

Arguments

  • pubKeys (point[]): An array of public keys to be aggregated

Returns

  • (point): The aggregated public key

...

function aggregateSignatures(signatures: point[]): point

Aggregate signatures.

Arguments

  • signatures (point[]): An array of signatures to be aggregated

Returns

  • (point): The aggregated signature

...

function verifyBatch(publicKeys: point[], signatures: point[], hashedMessage: point): Boolean

Aggregate signatures.

Arguments

  • publicKeys (point[]): An array of public keys for verification
  • signatures (point[]): An array of signatures for verification
  • hashedMessage (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.

blsSigner methods:

signHashedMsg(cHashedMsg: string): string

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(cPubKey: string, cSig: string, cHashedMsg: string): Boolean

Verify if the compressed public key, compressed signature, and compressed hashed message are a valid combination.

Arguments

  • cPubKey (string): Compressed public key
  • cSig (string): Compressed signature
  • cHashedMsg (string): Compressed hashed message

Returns

  • (Boolean): True if the compressed signature is valid, otherwise false

...

aggregatePublicKeys(cPubKeys: string[]): string

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

...

aggregateSignatures(cSigs: string[]): string

Aggregate an array of compressed signatures.

Arguments

  • cSigs (string[]): An array of compressed signatures

Returns

  • (string): The compressed aggregate of the signatures

...

verifyBatch(cPubKeys: string[], cSigs: string[], cHashedMsg: string): Boolean

Verify an array of compressed public keys and compressed signatures against a compressed hashed message.

Arguments

  • cPubKeys (string[]): An array of compressed public keys
  • cSigs (string[]): An array of compressed signatures
  • cHashedMsg (string): Compressed hashed message

Returns

  • (Boolean): True if the arrays of compressed signatures is valid, otherwise false

...

Examples

Below are a few examples demonstrating how to use the package in various scenarios:

Example 1: deriving public key

// 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)

Example 2: signing a hashed message

        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)

Example 3: verify the signature

        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

Example 4: bls signer class

    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)
    }

...

License

BLS12-381 is MIT licensed.