Skip to main content

Crypto Reference

All crypto modules in src/crypto/. Implementations use Zig core with Rust/C for complex curves.

Hash Functions

Keccak256

Primary Ethereum hash function. Used for addresses, transaction hashes, storage keys.
Files:
  • Keccak256/index.ts - TypeScript API
  • keccak256.wasm.ts - WASM variant
  • keccak256_accel.zig - Hardware-accelerated implementation
  • keccak_asm.zig - Assembly-optimized
  • keccak_wrapper.rs - Rust keccak-asm wrapper
Performance: ~500ns native, ~2μs WASM

SHA256

SHA-256 for EVM precompile and general use.
Files:
  • SHA256/index.ts - TypeScript API
  • sha256.wasm.ts - WASM variant
  • sha256_accel.zig - Hardware-accelerated (SHA-NI)

Blake2

Blake2b hashing (EVM precompile 0x09).
Files:
  • Blake2/index.ts - TypeScript API
  • blake2.zig - Zig implementation
  • blake2_c.zig - C bindings (optional)
  • blake2.fuzz.zig - Fuzz tests

Ripemd160

RIPEMD-160 for Bitcoin compatibility (EVM precompile 0x03).
Files:
  • Ripemd160/index.ts - TypeScript API
  • ripemd160.zig - Zig implementation
  • ripemd160_c.zig - C bindings

Digital Signatures

Secp256k1

Bitcoin/Ethereum ECDSA curve. Core signing primitive.
Methods:
  • sign(hash, privateKey) - ECDSA sign
  • verify(signature, hash, publicKey) - Verify signature
  • recover(signature, hash, v) - Recover public key
  • getPublicKey(privateKey) - Derive public key
  • compressPublicKey(pubkey) - Compress to 33 bytes
  • decompressPublicKey(pubkey) - Decompress to 65 bytes
  • isValidPrivateKey(key) - Validate private key
  • isValidPublicKey(key) - Validate public key
Files:
  • Secp256k1/index.ts - TypeScript API
  • secp256k1.zig - Zig implementation
  • secp256k1.wasm.ts - WASM variant
  • secp256k1.bench.zig - Benchmarks
  • secp256k1.fuzz.zig - Fuzz tests

Ed25519

EdDSA signatures. Used by some L2s and alt chains.
Files:
  • Ed25519/index.ts - TypeScript API
  • ed25519.zig - Zig implementation
  • ed25519.wasm.ts - WASM variant

P256

NIST P-256 (secp256r1). Used by hardware wallets and WebAuthn.
Files:
  • P256/index.ts - TypeScript API
  • p256.zig - Zig implementation
  • p256.wasm.ts - WASM variant

Elliptic Curves & Pairings

BN254

BN254 (alt_bn128) curve for zkSNARKs. Used by EVM precompiles 0x06-0x08.
Implementation: Dual implementation
  • bn254.zig - Pure Zig implementation
  • bn254_arkworks.zig - Rust arkworks wrapper (higher performance)
  • bn254_ark_c.zig - C interface to Rust
  • bn254_wrapper.rs - Rust arkworks bindings
Files:
  • bn254.ts - TypeScript API (auto-selects best impl)
  • bn254.wasm.ts - WASM variant
  • bn254.ark.ts - arkworks-specific exports

BLS12-381

BLS12-381 curve for beacon chain consensus. EVM precompiles 0x0B-0x13 (Prague). Uses blst C library for production performance.
Precompile Operations: Files (in src/evm/precompiles/):
  • bls12_g1_add.zig, bls12_g1_mul.zig, bls12_g1_msm.zig
  • bls12_g2_add.zig, bls12_g2_mul.zig, bls12_g2_msm.zig
  • bls12_pairing.zig
  • bls12_map_fp_to_g1.zig, bls12_map_fp2_to_g2.zig

KZG Commitments

KZG

Kate-Zaverucha-Goldberg polynomial commitments for EIP-4844 blob transactions.
Dependencies:
  • c-kzg-4844 - C library (lib/c-kzg-4844)
  • Trusted setup: kzg_trusted_setup.zig
Limitation: Not available in WASM (library too large, requires trusted setup file) Files:
  • KZG/index.ts - TypeScript API
  • c_kzg.zig - C bindings
  • kzg_setup.zig - Setup loading
  • kzg_trusted_setup.zig - Embedded trusted setup

Typed Data (EIP-712)

EIP712

Typed structured data hashing and signing.
Files:
  • EIP712/index.ts - TypeScript API (placeholder/WIP)
  • eip712.zig - Zig implementation
  • eip712.wasm.ts - WASM variant

Symmetric Encryption

AesGcm

AES-256-GCM authenticated encryption.
Files:
  • AesGcm/index.ts - TypeScript API
  • aes_gcm.zig - Zig implementation

X25519

Elliptic curve Diffie-Hellman key exchange.
Files:
  • X25519/index.ts - TypeScript API
  • x25519.zig - Zig implementation
  • x25519.wasm.ts - WASM variant

Wallet Derivation

Bip39

Mnemonic seed phrase generation and validation.
Files:
  • Bip39/index.ts - TypeScript API (uses @scure/bip39)
  • bip39.test.ts - Tests

HDWallet

Hierarchical Deterministic wallet derivation (BIP-32).
Files:
  • HDWallet/index.ts - TypeScript API (uses @scure/bip32)
  • hdwallet.test.ts - Tests
  • signers/ - Hardware wallet signers (Ledger, Trezor)

Hardware Acceleration

CPU Feature Detection

Detects available CPU features for optimal implementation selection.

Accelerated Implementations

Files:
  • cpu_features.zig - Feature detection
  • keccak256_accel.zig - Accelerated Keccak
  • sha256_accel.zig - Accelerated SHA256
  • keccak_asm.zig - Assembly Keccak

Modular Exponentiation

ModExp

Modular exponentiation for MODEXP precompile (0x05).
Files:
  • modexp.zig - Zig implementation

Module Summary

Native indicates the Bun native bindings. In Node.js, use the regular TypeScript API or the WASM variants.
WASM Limitations:
  • BLS12-381: Requires blst C library
  • KZG: Requires trusted setup file and c-kzg library