Advent of Hardcaml
Several Advent of Code 2024 puzzles were solved entirely on a small Lattice ECP5 FPGA (84K LUTs, 4 Mbits RAM) using Hardcaml, Jane Street's OCaml-embedded HDL, with the open-source Yosys + NextPNR toolchain. Day 4's word search becomes a convolution over a shift register holding the last four grid rows, with a higher-order function generating sliding windows of any dimensions. Day 7's operator search becomes a deep pipeline that evaluates one operator combination per clock cycle, testing hundreds of millions of combinations in seconds. OCaml's type system, functors and metaprogramming make unrolled, parametrized hardware concise and reusable, and the post closes by challenging other HDL communities to solve Advent of Code puzzles too.
Related posts
- Fun with Algebraic Effects - from Toy Examples to Hardcaml Simulations
Jane Street Tech Blog hardMasterAlgebraic effects in OCaml 5/OxCaml can replace monads for most uses, and Jane Street ported Hardcaml_step_testbench from a monadic API to effects. Monads spread through every caller, require special combinators and returns, and block OxCaml features such as locally allocated values and unboxed layouts, because bind needs globally allocated closures returning value-layout types. Using the type-safe Handled_effect library, a computation performs operations through a local handler, and the handler code receives a continuation it can resume. The approach is then applied to interleaving multiple testbench threads that synchronize on clock cycles in FPGA circuit simulations.
- Accelerating zk-SNARKs - MSM and NTT algorithms on FPGAs with Hardcaml
Jane Street Tech Blog hardMasterA Hardcaml-built FPGA implementation of Pippenger's algorithm for multi-scalar multiplication on the BLS12-377 curve won first place in the ZPrize MSM track, computing a 2^26-point MSM in about 5.08s on an AWS f1.2xlarge at 52W. Its core is a 200+ stage pipelined elliptic curve point adder whose controller stalls on bucket data hazards instead of flushing the pipeline, with the host combining partial bucket sums in parallel with the next round. A companion NTT design uses the 4-step algorithm to split a 2^24 NTT into 2^12 Cooley-Tukey transforms that fit in on-chip SRAM, running on clusters of 8 cores matched to the 512-bit HBM bus. It placed second, scaling from 0.23s at 8 cores to 0.045s at 64 cores, with power and resource figures reported for each configuration.
- What the interns have wrought, 2022 edition
Jane Street Tech Blog mediumAdeptThree Jane Street intern projects from 2022 show substantial tooling work in OCaml. magic-trace gained a sampling mode that can cover 20 seconds to 20 minutes on Intel or AMD hardware, plus multi-process tracing, CPU-frequency transition reporting and a perf dlfilter backend that speeds trace decoding by 10–50%. Datafetcher jobs can now be compiled into Incremental computations, using a Response_or_waiting type to represent async state and mandatory memoization of fetch nodes. Quickcheck gained a [@@contract] syntax for deriving property tests from interface signatures, along with early support for bisimulation testing of two implementations of the same API.
- What the interns have wrought, 2023 edition
Jane Street Tech Blog mediumAdeptThree Jane Street intern projects, all in OCaml. The first is an SQL-like temporal query language, parsed with Angstrom and compiled onto a Linear Temporal Logic engine, for finding event sequences in Concord's transaction stream. The second is a pure-OCaml tiktoken-compatible tokenizer with a handcrafted regex automaton and an imperative rewrite; it matched or beat the Rust reference implementations, though GC pauses gave it higher variance. The third adds async support to ppx_tracing by storing correlation IDs in the Async scheduler's execution context, which helped the Iron code review team cut server startup time by about 50%.
- Writing a DSL for high performance parallel programming in OxCaml
Jane Street Tech Blog hardMasterA Jane Street intern built a JIT compiler for an SPMD-style DSL in OxCaml so SIMD kernels for options pricing and risk checks can be written once as ordinary-looking OxCaml functions instead of architecture-specific C intrinsics. Kernels describe the computation for a single output index, and the compiler generates the loop, which keeps each iteration writing exactly one output and makes vectorization safe. The front end builds a GADT-typed DAG that tracks uniform vs. varying rates and provides three control-flow forms (select, if_, if_var) with different evaluation semantics. The backend uses OxCaml's runtime metaprogramming (quotes and splices) to emit OxCaml code, partially evaluating runtime-known values such as the chosen distance metric away and relying on the OxCaml compiler for further optimization.