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
Name | Description |
---|---|
P256.getPublicKey | Computes the P256 ECDSA public key from a provided private key. |
P256.randomPrivateKey | Generates a random P256 ECDSA private key. |
P256.recoverPublicKey | Recovers the signing public key from the signed payload and signature. |
P256.sign | Signs the payload with the provided private key and returns a P256 signature. |
P256.verify | Verifies a payload was signed by the provided public key. |