Skip to content

P256

Utility functions for NIST P256 ECDSA cryptography.

Examples

Below are some examples demonstrating common usages of the P256 module:

Computing a Random Private Key

A random private key can be computed using P256.randomPrivateKey:

import { P256 } from 'ox'
 
const privateKey = P256.randomPrivateKey()
'0x...'

Getting a Public Key

A public key can be derived from a private key using P256.getPublicKey:

import { P256 } from 'ox'
 
const privateKey = P256.randomPrivateKey()
 
const publicKey = P256.getPublicKey({ privateKey })
{ x: 3251...5152n, y: 1251...5152n }

Signing a Payload

A payload can be signed using P256.sign:

import { P256 } from 'ox'
 
const privateKey = P256.randomPrivateKey()
 
const signature = P256.sign({ payload: '0xdeadbeef', privateKey })
{ r: 1251...5152n, s: 1251...5152n, yParity: 1 }

Verifying a Signature

A signature can be verified using P256.verify:

import { P256 } from 'ox'
 
const privateKey = P256.randomPrivateKey()
const publicKey = P256.getPublicKey({ privateKey })
const signature = P256.sign({ payload: '0xdeadbeef', privateKey })
 
const isValid = P256.verify({ 
  payload: '0xdeadbeef', 
  publicKey, 
  signature, 
})
true

Functions

NameDescription
P256.getPublicKeyComputes the P256 ECDSA public key from a provided private key.
P256.randomPrivateKeyGenerates a random P256 ECDSA private key.
P256.recoverPublicKeyRecovers the signing public key from the signed payload and signature.
P256.signSigns the payload with the provided private key and returns a P256 signature.
P256.verifyVerifies a payload was signed by the provided public key.