Zerokit RLN Module
The Zerokit RLN Module provides a Rust implementation for working with Rate-Limiting Nullifier RLN zkSNARK proofs and primitives. This module allows you to:
- Generate and verify RLN proofs
- Work with Merkle trees for commitment storage
- Implement rate-limiting mechanisms for distributed systems
Quick Start
[!IMPORTANT] Version 0.7.0 is the only version that does not support WASM and x32 architecture. WASM support is available in version 0.8.0 and above.
Add RLN as dependency
We start by adding zerokit RLN to our Cargo.toml
[]
= { = "https://github.com/vacp2p/zerokit" }
Basic Usage Example
The RLN object constructor requires the following files:
graph.bin: The graph file built for the input tree sizerln_final.zkeyorrln_final_uncompr.arkzkey: The proving keyverification_key.arkvkey: The verification key (optional)
Additionally, rln.wasm is used for testing in the rln-wasm module.
In the following we will use cursors as readers/writers for interfacing with RLN public APIs.
use Cursor;
use ;
use json;
Comments for the code above for point 4
The external nullifier includes two parameters.
The first one is epoch and it's used to identify messages received in a certain time frame.
It usually corresponds to the current UNIX time but can also be set to a random value or generated by a seed,
provided that it corresponds to a field element.
The second one is rln_identifier and it's used to prevent a RLN ZK proof generated
for one application to be re-used in another one.
Features
- Multiple Backend Support: Choose between different zkey formats with feature flags
arkzkey: Use the optimized Arkworks-compatible zkey format (faster loading)stateless: For stateless proof verification
- Pre-compiled Circuits: Ready-to-use circuits with Merkle tree depth of 20
- Wasm Support: WebAssembly bindings via rln-wasm crate with features like:
- Browser and Node.js compatibility
- Optional multi-threading support using wasm-bindgen-rayon
- Headless browser testing capabilities
Building and Testing
Prerequisites
Build Commands
# Build with default features
# Test with default features
# Test with specific features
Advanced: Custom Circuit Compilation
The rln (https://github.com/rate-limiting-nullifier/circom-rln) repository,
which contains the RLN circuit implementation is using for pre-compiled RLN circuit for zerokit RLN.
If you want to compile your own RLN circuit, you can follow the instructions below.
1. Compile ZK Circuits for getting the zkey and verification key files
This script actually generates not only the zkey and verification key files for the RLN circuit,
but also the execution wasm file used for witness calculation.
However, the wasm file is not needed for the rln module,
because current implementation uses the iden3 graph file for witness calculation.
This graph file is generated by the circom-witnesscalc tool in step 2.
To customize the circuit parameters, modify circom-rln/circuits/rln.circom:
pragma circom 2.1.0;
include "./rln.circom";
component main { public [x, externalNullifier] } = RLN(N, M);
Where:
-
N: Merkle tree depth, determining the maximum membership capacity (2^N members). -
M: Bit size for range checks, setting an upper bound for the number of messages per epoch (2^M messages).
[!NOTE] However, if
Nis too big, this might require a larger Powers of Tau ceremony than the one hardcoded in./scripts/build-circuits.sh, which is2^14. In such case, we refer to the official Circom documentation for instructions on how to run an appropriate Powers of Tau ceremony and Phase 2 in order to compile the desired circuit.
Additionally, whileMsets an upper bound on the number of messages per epoch (2^M), you can configure lower message limit for your use case, as long as it satisfiesuser_message_limit ≤ 2^M.
Currently, therlnmodule comes with a pre-compiled RLN circuit with a Merkle tree of depth20and a bit size of16, allowing up to2^20registered members and a2^16message limit per epoch.
Install circom compiler
You can follow the instructions below or refer to the
installing Circom guide for more details,
but make sure to use the specific version v2.1.0.
# Clone the circom repository
# Checkout the specific version
&&
# Build the circom compiler
# Install the circom binary globally
# Check the circom version to ensure it's v2.1.0
Generate the zkey and verification key files example
# Clone the circom-rln repository
# Install dependencies
&&
# Build circuits
# Use the generated zkey file in subsequent steps
2. Generate Witness Calculation Graph
The execution graph file used for witness calculation can be compiled following instructions
in the circom-witnesscalc repository.
As mentioned in step 1, we should use rln.circom file from circom-rln repository.
# Clone the circom-witnesscalc repository
# Load the submodules
&&
# Build the circom-witnesscalc tool
# Generate the witness calculation graph
The rln module comes with pre-compiled
execution graph files for the RLN circuit.
3. Generate Arkzkey Representation for zkey and verification key files
For faster loading, compile the zkey file into the arkzkey format using ark-zkey. This is fork of the original repository with the uncompressed zkey support.
# Clone the ark-zkey repository
# Build the ark-zkey tool
&&
# Generate the arkzkey representation for the zkey file
Currently, the rln module comes with
pre-compiled arkzkey keys for the RLN circuit.
Get involved
Zerokit RLN public and FFI APIs allow interaction with many more features than what briefly showcased above.
We invite you to check our API documentation by running
cargo doc --no-deps
and look at unit tests to have an hint on how to interface and use them.
Detailed Protocol Flow
- Identity Creation: Generate a secret key and commitment
- Rate Commitment: Add commitment to a Merkle tree
- External Nullifier Setup: Combine epoch and application identifier
- Proof Generation: Create a zkSNARK proof that:
- Proves membership in the Merkle tree
- Ensures rate-limiting constraints are satisfied
- Generates a nullifier to prevent double-usage
- Proof Verification: Verify the proof without revealing the prover's identity
Getting Involved
- Check the unit tests for more usage examples
- RFC specification for the Rate-Limiting Nullifier protocol
- GitHub repository for the latest updates