Bls
Utility functions for BLS12-381 cryptography.
Examples
Below are some examples demonstrating common usages of the Bls
module:
Computing a Random Private Key
A random private key can be computed using Bls.randomPrivateKey
:
import { Bls } from 'ox'
const privateKey = Bls.randomPrivateKey()
'0x...'
Getting a Public Key
A public key can be derived from a private key using Bls.getPublicKey
:
import { Bls } from 'ox'
const privateKey = Bls.randomPrivateKey()
const publicKey = Bls.getPublicKey({ privateKey })
{ x: 3251...5152n, y: 1251...5152n, z: 1n }
Signing a Payload
A payload can be signed using Bls.sign
:
import { Bls } from 'ox'
const privateKey = Bls.randomPrivateKey()
const signature = Bls.sign({ payload: '0xdeadbeef', privateKey })
{ x: 1251...5152n, y: 1251...5152n, z: 1n }
Verifying a Signature
A signature can be verified using Secp256k1.verify
:
import { Bls } from 'ox'
const privateKey = Bls.randomPrivateKey()
const publicKey = Bls.getPublicKey({ privateKey })
const signature = Bls.sign({ payload: '0xdeadbeef', privateKey })
const isValid = Bls.verify({
payload: '0xdeadbeef',
publicKey,
signature,
})
true
Aggregating Public Keys & Signatures
Public keys and signatures can be aggregated using Bls.aggregate
:
import { Bls } from 'ox'
const publicKeys = [
Bls.getPublicKey({ privateKey: '0x...' }),
Bls.getPublicKey({ privateKey: '0x...' }),
]
const publicKey = Bls.aggregate(publicKeys)
const signatures = [
Bls.sign({ payload: '0x...', privateKey: '0x...' }),
Bls.sign({ payload: '0x...', privateKey: '0x...' }),
]
const signature = Bls.aggregate(signatures)
Verify Aggregated Signatures
We can also pass a public key and signature that was aggregated with Bls.aggregate
to Bls.verify
.
import { Bls, Hex } from 'ox'
const payload = Hex.random(32)
const privateKeys = Array.from({ length: 100 }, () => Bls.randomPrivateKey())
const publicKeys = privateKeys.map((privateKey) =>
Bls.getPublicKey({ privateKey }),
)
const signatures = privateKeys.map((privateKey) =>
Bls.sign({ payload, privateKey }),
)
const publicKey = Bls.aggregate(publicKeys)
const signature = Bls.aggregate(signatures)
const valid = Bls.verify({ payload, publicKey, signature })
Functions
Name | Description |
---|---|
Bls.aggregate | Aggregates a set of BLS points that are either on the G1 or G2 curves (ie. public keys or signatures). |
Bls.getPublicKey | Computes the BLS12-381 public key from a provided private key. |
Bls.randomPrivateKey | Generates a random BLS12-381 private key. |
Bls.sign | Signs the payload with the provided private key. |
Bls.verify | Verifies a payload was signed by the provided public key(s). |
Types
Name | Description |
---|---|
Bls.Size |