or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

curves.mdecdsa.mdeddsa.mdindex.mdutils.md
tile.json

ecdsa.mddocs/

ECDSA Operations

Complete ECDSA (Elliptic Curve Digital Signature Algorithm) implementation supporting key generation, digital signatures, signature verification, public key recovery, and ECDH key agreement across multiple elliptic curves.

Capabilities

EC Constructor

Creates an EC context for ECDSA operations on a specific elliptic curve.

/**
 * Create EC context for ECDSA operations
 * @param options - Curve name string or curve configuration object
 * @returns EC instance configured for the specified curve
 */
function EC(options);

Usage Examples:

const EC = require('elliptic').ec;

// Using curve name
const ec = new EC('secp256k1');

// Using curve object
const ec = new EC(elliptic.curves.p256);

Key Pair Generation

Generate cryptographically secure key pairs for ECDSA operations.

/**
 * Generate a new random key pair
 * @param options - Key generation options
 * @returns KeyPair instance with private and public keys
 */
ec.genKeyPair(options);

/**
 * Create KeyPair from existing private key
 * @param priv - Private key data
 * @param enc - Encoding format ('hex', 'base64', etc.)
 * @returns KeyPair instance
 */
ec.keyFromPrivate(priv, enc);

/**
 * Create KeyPair from existing public key  
 * @param pub - Public key data
 * @param enc - Encoding format ('hex', 'base64', etc.)
 * @returns KeyPair instance (verification only)
 */
ec.keyFromPublic(pub, enc);

/**
 * Create empty KeyPair with options
 * @param options - KeyPair configuration
 * @returns KeyPair instance
 */
ec.keyPair(options);

Usage Examples:

const EC = require('elliptic').ec;
const ec = new EC('secp256k1');

// Generate random key pair
const key = ec.genKeyPair();

// Create from private key hex string
const key2 = ec.keyFromPrivate('97ddae0f3a25b92268175400149d65d6887b9cefaf28ea2c078e05cdc15a3c0a');

// Create from public key coordinates
const key3 = ec.keyFromPublic({
  x: '79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798',
  y: '483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8'
}, 'hex');

Digital Signatures

Create and verify ECDSA digital signatures using deterministic k-value generation per RFC6979.

/**
 * Sign message with private key
 * @param msg - Message to sign (array, hex string, or BN)
 * @param key - Private key (KeyPair instance or raw key data)
 * @param enc - Key encoding format
 * @param options - Signature options
 * @returns Signature instance
 */
ec.sign(msg, key, enc, options);

/**
 * Verify signature with public key
 * @param msg - Original message
 * @param signature - Signature to verify
 * @param key - Public key (KeyPair instance or raw key data)
 * @param enc - Key encoding format
 * @param options - Verification options
 * @returns Boolean indicating signature validity
 */
ec.verify(msg, signature, key, enc, options);

Usage Examples:

const EC = require('elliptic').ec;
const ec = new EC('secp256k1');

const key = ec.genKeyPair();
const msgHash = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

// Sign message
const signature = ec.sign(msgHash, key);

// Verify with same key
const isValid = ec.verify(msgHash, signature, key);

// Verify with public key only
const pubKey = key.getPublic();
const isValid2 = ec.verify(msgHash, signature, pubKey, 'hex');

Public Key Recovery

Recover the public key from a signature and message, useful for blockchain applications.

/**
 * Recover public key from signature
 * @param msg - Original message
 * @param signature - Signature object
 * @param j - Recovery parameter (0-3)
 * @param enc - Encoding format
 * @returns Recovered public key point
 */
ec.recoverPubKey(msg, signature, j, enc);

/**
 * Get recovery parameter for signature
 * @param e - Message hash
 * @param signature - Signature object
 * @param Q - Known public key point
 * @param enc - Encoding format
 * @returns Recovery parameter
 */
ec.getKeyRecoveryParam(e, signature, Q, enc);

KeyPair Class Methods

Methods available on KeyPair instances for key operations.

/**
 * Get public key with optional encoding
 * @param compact - Use compact point encoding
 * @param enc - Output encoding format
 * @returns Public key data
 */
keyPair.getPublic(compact, enc);

/**
 * Get private key with optional encoding
 * @param enc - Output encoding format ('hex' for hex string)
 * @returns Private key data
 */
keyPair.getPrivate(enc);

/**
 * Validate the key pair
 * @returns Validation result object
 */
keyPair.validate();

/**
 * Sign message with this key pair
 * @param msg - Message to sign
 * @param enc - Message encoding
 * @param options - Signature options
 * @returns Signature instance
 */
keyPair.sign(msg, enc, options);

/**
 * Verify signature with this key pair
 * @param msg - Original message
 * @param signature - Signature to verify
 * @param options - Verification options
 * @returns Boolean indicating validity
 */
keyPair.verify(msg, signature, options);

/**
 * Perform ECDH key derivation
 * @param pub - Other party's public key
 * @returns Shared secret as BN instance
 */
keyPair.derive(pub);

Usage Examples:

const EC = require('elliptic').ec;
const ec = new EC('secp256k1');

const key1 = ec.genKeyPair();
const key2 = ec.genKeyPair();

// Get keys in different formats
const privHex = key1.getPrivate('hex');
const pubHex = key1.getPublic('hex');
const pubCompressed = key1.getPublic(true, 'hex');

// ECDH key derivation
const shared1 = key1.derive(key2.getPublic());
const shared2 = key2.derive(key1.getPublic());
// shared1 and shared2 are equal

// Validation
const result = key1.validate();
console.log('Key valid:', result.result);

Signature Class Methods

Methods available on Signature instances.

/**
 * Export signature to DER format
 * @param enc - Output encoding ('hex', 'base64', etc.)
 * @returns DER-encoded signature
 */
signature.toDER(enc);

Key Formats

Private Key Formats

Private keys can be provided in multiple formats:

  • Hex string: '97ddae0f3a25b92268175400149d65d6887b9cefaf28ea2c078e05cdc15a3c0a'
  • Buffer/Array: [0x97, 0xdd, 0xae, ...]
  • BN instance: Big number representation

Public Key Formats

Public keys support several representations:

  1. Uncompressed hex string: '04' + x_hex + y_hex
  2. Object with coordinates: { x: 'hex_string', y: 'hex_string' }
  3. Object with buffers: { x: Buffer, y: Buffer }
  4. Compressed point encoding for supported curves

Signature Formats

Signatures can be provided as:

  1. DER-encoded hex string
  2. DER-encoded buffer
  3. Object with components: { r: 'hex_string', s: 'hex_string' }
  4. Object with buffers: { r: Buffer, s: Buffer }

Options

Key Generation Options

interface KeyGenOptions {
  pers?: any;           // Personalization string
  persEnc?: string;     // Personalization encoding
  entropy?: any;        // Additional entropy
  entropyEnc?: string;  // Entropy encoding
}

Signature Options

interface SignOptions {
  pers?: any;           // Personalization string
  persEnc?: string;     // Personalization encoding
  entropy?: any;        // Additional entropy
  entropyEnc?: string;  // Entropy encoding
  k?: function;         // Custom k-value function
  canonical?: boolean;  // Use canonical signatures
  msgBitLength?: number; // Message bit length
}

KeyPair Options

interface KeyPairOptions {
  priv?: any;           // Private key data
  pub?: any;            // Public key data
  privEnc?: string;     // Private key encoding
  pubEnc?: string;      // Public key encoding
}