Skip to main content

Adding Crypto Functions

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

Security

  • 512-bit output
  • Designed by Vincent Rijmen and Paulo Barreto
  • Standardized in ISO/IEC 10118-3:2004

Test Vectors