Skip to content

Bls.createKeyPair

Creates a new BLS12-381 key pair consisting of a private key and its corresponding public key.

  • G1 Point (Default): - short (48 bytes) - computes longer G2 Signatures (96 bytes) - G2 Point: - long (96 bytes) - computes short G1 Signatures (48 bytes)

Imports

Named
import { Bls } from 'ox'

Examples

Short G1 Public Keys (Default)

import { Bls } from 'ox'
 
const { 
const publicKey: { x: bigint; y: bigint; z: bigint; }
publicKey
} = Bls.createKeyPair()

Long G2 Public Keys

A G2 Public Key can be derived as a G2 point (96 bytes) using size: 'long-key:short-sig'.

This will allow you to compute G1 Signatures (48 bytes) with Bls.sign.

import { Bls } from 'ox'
 
const { publicKey } = Bls.createKeyPair({
  size: 'long-key:short-sig',
})
 
const publicKey: { x: { c0: bigint; c1: bigint; }; y: { c0: bigint; c1: bigint; }; z: { c0: bigint; c1: bigint; }; }
publicKey

Serializing

Public Keys can be serialized to hex or bytes using BlsPoint.toHex or BlsPoint.toBytes:

import { Bls, BlsPoint } from 'ox'
 
const { publicKey } = Bls.createKeyPair()
 
const 
const publicKeyHex: BlsPoint.G1Hex
publicKeyHex
= BlsPoint.toHex(publicKey)
const
const publicKeyBytes: BlsPoint.G1Bytes
publicKeyBytes
= BlsPoint.toBytes(publicKey)

They can also be deserialized from hex or bytes using BlsPoint.fromHex or BlsPoint.fromBytes:

import { Bls, BlsPoint } from 'ox'
 
const publicKeyHex = '0x...'
 
const 
const publicKey: { x: bigint; y: bigint; z: bigint; }
publicKey
= BlsPoint.fromHex(publicKeyHex, 'G1')

Definition

function createKeyPair<as, size>(
  options?: createKeyPair.Options<as, size>,
): createKeyPair.ReturnType<as, size>

Source: src/core/Bls.ts

Parameters

options

  • Type: createKeyPair.Options<as, size>
  • Optional

The options to generate the key pair.

options.as

  • Type: "Bytes" | "Hex" | as
  • Optional

Format of the returned private key.

options.size

  • Type: size | Size
  • Optional

Size of the public key to compute.

  • 'short-key:long-sig': 48 bytes; computes long signatures (96 bytes)
  • 'long-key:short-sig': 96 bytes; computes short signatures (48 bytes)

Return Type

The generated key pair containing both private and public keys.

createKeyPair.ReturnType<as, size>