Skip to main content
This guide covers adding new cryptographic functions. Crypto code requires extra care for security and cross-language implementation.

Prerequisites

Decision Tree

Before implementing, decide:
  1. Pure Zig? Simple operations (hashing, encoding)
  2. Rust FFI? Complex curves (arkworks ecosystem)
  3. C library? Performance-critical with existing impl (blst, c-kzg)

Example: Adding a Hash Function

We’ll add a hypothetical Whirlpool hash function.

Step 1: Create Directory

Step 2: Implement in Zig

Step 3: TypeScript Wrapper

Step 4: Register in Module

Step 5: Tests

Adding Curve Operations (Rust FFI)

For elliptic curve operations, use Rust with arkworks.

Step 1: Add Rust Dependency

Step 2: Create Rust Wrapper

Step 3: Zig FFI Bindings

Security Requirements

Constant-Time Operations

All crypto code must be constant-time to prevent timing attacks.

Memory Clearing

Clear sensitive data after use:

Input Validation

Always validate inputs before processing:

Test Vectors

Every crypto function needs test vectors from official sources:

Cross-Validation

Test against reference implementations:

Documentation

Checklist

Before submitting crypto code:
  • Zig implementation with inline tests
  • All operations constant-time where needed
  • Input validation on all public functions
  • Test vectors from official specification
  • Cross-validation against reference implementation
  • TypeScript wrapper with types
  • WASM variant working
  • Memory cleared after sensitive operations
  • Documentation with security notes
  • Registered in crypto/root.zig
  • All tests passing