Writing

SIMD in Rust: Making leanSig Signing More Efficient

Reading time: 2 minutes

I worked on leanSig issue #49, which involved using SIMD to speed up cryptographic signing in Rust.

I knew what SIMD was in theory, but I hadn’t implemented it before. I studied the signing code, looked at a similar approach in Plonky3, and built a solution for leanSig.

Here’s what I learned and how the change works.

First, what is SIMD?

SIMD stands for Single Instruction, Multiple Data. It lets a CPU perform the same operation on several values at once, rather than processing each value separately.

Imagine washing dishes. Instead of cleaning one plate at a time, you use a rack that lets you wash several together. SIMD follows a similar idea, but with numbers.

For example, instead of adding four pairs of numbers one after another, a SIMD instruction can add all four pairs together, provided they fit in the CPU’s available lanes.

It’s especially useful when a program repeats the same calculation across many independent inputs.

Why does signing need it?

leanSig is a Rust research implementation of a cryptographic signature scheme. Its signing process uses target-sum encoding, which involves finding a value that meets a mathematical condition.

In simple terms, the signer:

  1. Generates a candidate value called rho.
  2. Hashes the message and candidate using Poseidon.
  3. Checks whether the resulting chunks add up to a target sum.
  4. Tries another candidate if the check fails.

This repeated search is called grinding. Checking candidates one at a time can require many sequential hash computations.

What I changed

I used the same general batching idea found in Plonky3’s grinding code.

Instead of hashing one rho at a time, leanSig can pack multiple candidates into SIMD lanes and evaluate them together using Poseidon. It then checks the results in the original candidate order and returns the first valid candidate.

Here’s the idea in simplified pseudocode—not the exact library API.

Before: One candidate at a time

loop {
    let rho = prf.next();
    let chunks = hash(message, rho);

    if chunks.sum() == TARGET_SUM {
        return rho;
    }
}

After: A batch of candidates at once

loop {
    let rhos = prf.next_simd_batch();
    let chunks = poseidon_packed(message, rhos);

    if let Some(rho) = first_valid_in_order(chunks) {
        return rho;
    }
}

I also moved the grinding logic behind the message-hash and encoding interfaces. This keeps the signing code simpler and allows hash functions without SIMD support to use the original approach.

Keeping the result the same

Speed isn’t useful if it changes a cryptographic signature’s behavior. The SIMD path must preserve three things:

  • Determinism: For the same inputs, it selects the same first valid PRF counter as the scalar path.
  • Error handling: It keeps the existing EncodingAttemptsExceeded behavior.
  • Compatibility: Non-Poseidon hash functions continue using the scalar implementation.

The regression test test_grind_matches_first_successful_attempt compares both paths to check that they produce the same randomness and chunks.

What I learned

This contribution helped me understand SIMD beyond the textbook definition. The key wasn’t simply running calculations in parallel; it was finding independent work that could be batched without changing the result.

The change is designed to reduce sequential work in the Poseidon signing path. Actual speedups should be established with benchmarks on the target hardware.

Code and references: My leanSig PR #51 · leanSig · Plonky3 · My GitHub

Originally published on HackMD . Comments and discussion live there.