Nostringer Ring Signatures (Rust)
A blazing fast Rust implementation of the Nostringer unlinkable ring signature scheme for Nostr, compatible with the nostringer TypeScript library.
Built using pure Rust crypto crates, this library allows a signer to prove membership in a group of Nostr accounts (defined by their public keys) without revealing which specific account produced the signature. It uses a Spontaneous Anonymous Group (SAG)-like algorithm compatible with secp256k1 keys used in Nostr.
Nostringer is largely inspired by Monero's Ring Signatures using Spontaneous Anonymous Group signatures (SAG), and beritani/ring-signatures implementation of ring signatures using the elliptic curve Ed25519 and Keccak for hashing.
Table of Contents
- Nostringer Ring Signatures (Rust)
Problem Statement
In many scenarios, you want to prove that "someone among these N credentials produced this signature," but you do not want to reveal which credential or identity. For instance, you might have a set of recognized Nostr pubkeys (e.g., moderators, DAO members, authorized reviewers) who are allowed to perform certain actions, but you want them to remain anonymous within that set when doing so.
A ring signature solves this by letting an individual sign a message on behalf of the group (the ring). A verifier can confirm the message originated from one of the public keys in the ring, without learning the specific signer's identity.
Roadmap
Check ROADMAP.md for the detailed project roadmap, including completed and upcoming milestones.
Key Features
- Simplified API: Top-level
signandverifyfunctions with compact format signatures for easier use. - Variant Selection: Choose between SAG (unlinkable) and BLSAG (linkable) signature variants with a simple enum.
- Compact Signatures: All signatures use the space-efficient "ringA..." format, which includes version and variant information.
- Unlinkable: SAG signatures hide the signer's identity. Two signatures from the same signer cannot be linked cryptographically.
- Linkable Option: The BLSAG variant provides linkability through key images to detect when the same key is used multiple times, while still preserving anonymity within the ring.
- Fast: Implemented in Rust, leveraging efficient and audited cryptographic primitives from the RustCrypto ecosystem (
k256,sha2). - Optimized API: Provides both high-level API and a more efficient low-level binary API that avoids serialization overhead.
- WebAssembly Support: Use the library directly in web browsers and other WASM environments.
- Nostr Key Compatibility: Directly supports standard Nostr key formats (hex strings):
- 32-byte (64-hex) x-only public keys.
- 33-byte (66-hex) compressed public keys.
- 65-byte (130-hex) uncompressed public keys.
- 32-byte (64-hex) private keys.
- Minimal Dependencies: Relies on well-maintained RustCrypto crates.
- No Trusted Setup: The scheme does not require any special setup ceremony.
Signature Variants
The library offers two main variants of ring signatures:
SAG (Spontaneous Anonymous Group)
The default variant that provides:
- Complete unlinkability (no way to tell if two signatures came from the same signer)
- Maximum privacy within the ring
- Suitable for anonymous voting, whistleblowing, or any scenario requiring maximum privacy
BLSAG (Back's Linkable Spontaneous Anonymous Group)
A linkable variant that:
- Produces a key image along with the signature to enable linkability
- Can detect when the same key signs multiple times (via the key image)
- Still doesn't reveal which specific ring member signed (preserves anonymity within the ring)
- Suitable for preventing double-spending, duplicate voting, or tracking usage of a credential
- Similar to the linkable ring signature scheme used in Monero
Choose the variant that best suits your privacy and security requirements.
SAG vs. bLSAG Trade-offs
This library implements both a basic SAG-like ring signature and the bLSAG (Back's Linkable Spontaneous Anonymous Group) variant. They offer different properties with corresponding performance characteristics:
Functionality:
- SAG (e.g.,
sign,verify,sign_binary,verify_binary):- Provides Anonymity: Hides which ring member produced the signature. The verifier only knows the signature came from someone in the specified ring.
- Provides Unlinkability: Signatures produced by the same signer (for different messages or using different rings) cannot be cryptographically linked back to that signer or to each other.
- bLSAG (e.g.,
sign_blsag_binary,verify_blsag_binary):- Provides Anonymity: Same as SAG.
- Provides Linkability: Introduces a Key Image (
I) which is unique and deterministic for each private key (I = sk * H_p(PK)). If the same private key is used to create multiple bLSAG signatures (even with different rings or messages), they will all produce the same key image. This allows detection of multiple signatures from the same (anonymous) source, useful for preventing double-voting or double-spending in anonymous contexts. Signatures from different private keys will produce different key images.
Signature Size:
- SAG Signature (
c0,s): Containsn + 1scalars (wherenis the ring size).- Binary Size:
32 * (n + 1)bytes.
- Binary Size:
- bLSAG Signature (
c0,s) + Key Image (I): Containsn + 1scalars plus one key image (a curve point).- Binary Size:
[32 * (n + 1)]bytes (signature) +33bytes (compressed key image) =32n + 65bytes.
- Binary Size:
- Comparison: bLSAG signatures require transmitting the additional key image alongside the
c0andsvalues, making them slightly larger (a constant overhead of 33 bytes compared to SAG when using compressed points).
Performance (Signing & Verification Speed):
The computational cost is dominated by elliptic curve scalar multiplications and hashing operations.
- Elliptic Curve Operations:
- SAG: Roughly
2npoint multiplications per sign/verify operation in the main loop (s*G + c*P). - bLSAG: Roughly
4npoint multiplications per sign/verify operation in the main loop (s*G + c*Pands*Hp(P) + c*I). It also includes the key image calculation (sk * Hp(PK)) during signing and a key image validity check (subgroup check viais_torsion_free) during verification.
- SAG: Roughly
- Hashing: -SAG: Uses one type of hash function (
hash_to_scalar) involving the message, ring keys (hex strings in current implementation), and one point. This hash is computedntimes per operation.- bLSAG: Requires an additional
hash_to_pointoperation (hashing a public key to a point) for each ring member (ntimes per operation). It uses a different challenge hash function (hash_for_blsag_challenge) involving the message and two points, also computedntimes per operation.
- bLSAG: Requires an additional
- Comparison: bLSAG signing and verification involve approximately twice the number of core point multiplications and additional hashing steps (
hash_to_point). Therefore, bLSAG operations are expected to be noticeably slower than their SAG counterparts. We will provide detailed benchmarks to quantify this difference.
Summary Table:
| Feature | SAG | bLSAG | Trade-off Summary |
|---|---|---|---|
| Linkability | No (Unlinkable) | Yes (Via Key Image) | bLSAG adds same-signer detection. |
| Size | 32(n+1) bytes |
32n + 65 bytes |
bLSAG is slightly larger (+33 bytes). |
| Speed | Faster (~2n mults) |
Slower (~4n mults + extras) |
bLSAG is computationally heavier. |
When to Choose:
- Choose SAG if simple anonymity and unlinkability are sufficient, and maximum performance or minimum signature size are priorities.
- Choose bLSAG if you need the ability to detect if the same anonymous signer has signed multiple times (e.g., voting, unique claims), and can accept the slightly larger signature size and increased computation time.
Installation
Add this crate to your Cargo.toml dependencies:
[]
= "0.1.0" # Replace with the latest version from crates.io
(Note: You might need other crates like hex or rand in your own project depending on how you handle keys and messages.)
Usage
use ;
Using Different Signature Variants
The library provides two signature variants that you can select using the SignatureVariant enum:
use ;
Low-Level Binary API
For applications requiring maximum performance, we also provide lower-level binary APIs that work directly with the native types, avoiding hex conversion overhead:
use ;
use ;
WebAssembly Usage
Nostringer can be compiled to WebAssembly, allowing you to use it directly in web browsers and other WASM environments:
// Import the WASM module
import init from "./nostringer.js";
// Initialize the WASM module
;
Building for WASM
To compile Nostringer for WebAssembly:
# Install wasm-pack if you don't have it
# Build the WASM module
# For bundlers like webpack
# For Node.js
See the WebAssembly example for a complete demonstration of using Nostringer in a web browser.
Examples
The repository includes several examples that demonstrate different aspects of the library:
-
Basic Signing (
examples/basic_signing.rs): Demonstrates the core signing and verification functionality. -
Key Formats (
examples/key_formats.rs): Shows how to work with different key formats (x-only, compressed, uncompressed) and create larger rings. -
BLSAG Linkability (
examples/blsag_linkability.rs): Demonstrates the linkable BLSAG variant and how to detect when the same key is used for multiple signatures. -
Error Handling (
examples/error_handling.rs): Demonstrates proper error handling for common error scenarios. -
WebAssembly (
examples/web/basic_wasm): A web-based example showing how to use the library in a browser via WebAssembly.# Build the WASM module # Serve the example (using Python's built-in server)
These examples provide practical demonstrations of how to use the library in real-world scenarios and handle various edge cases.
Benchmarks
The library includes comprehensive benchmarks using the Criterion framework for different ring sizes and operations. You can run these benchmarks yourself with:
For detailed information on running and interpreting benchmarks, see BENCHMARKS.md.
The repository also includes a GitHub Actions workflow that automatically runs benchmarks on each push and pull request, with the HTML report available as an artifact in the workflow run.
Performance Results
Below is a summary of the benchmark results, showing median execution times for each operation with different ring sizes:
| Operation | Ring Size | Execution Time |
|---|---|---|
| Sign | 2 members | 204.75 µs |
| Sign | 10 members | 897.76 µs |
| Sign | 100 members | 13.31 ms |
| Verify | 2 members | 166.83 µs |
| Verify | 10 members | 847.23 µs |
| Verify | 100 members | 12.71 ms |
| Sign+Verify | 2 members | 370.41 µs |
| Sign+Verify | 10 members | 1.76 ms |
| Sign+Verify | 100 members | 25.02 ms |
Benchmarking Environment:
- Model: MacBook Pro (Identifier:
MacBookPro18,2) - CPU: Apple M1 Max
- Cores: 10
- RAM: 64 GB
- Architecture:
arm64 - Operating System: macOS 14.7 (Build
23H124)
API Reference
Check the Rust API Docs for detailed API reference and usage examples.
Signature Size
The size of the generated ring signature depends directly on the number of members (n) in the ring. It consists of:
- One initial challenge (
c0) scalar (32 bytes binary / 64 hex chars). nresponse scalars (sarray) (each 32 bytes binary / 64 hex chars).
The total binary size follows the formula:
Size (bytes) = 32 * (n + 1)
This means the signature size grows linearly with the ring size. A larger ring provides more anonymity but results in a larger signature.
Security Considerations
- Anonymity Set: The level of anonymity depends on the size (
n) and plausibility of the chosen ring members. Ensure the ring contains keys that could realistically be the signer in the given context. - No Trusted Setup: This scheme does not require any trusted setup procedure.
- Unlinkability vs. Linkability:
- SAG: The default SAG implementation provides complete unlinkability. Signatures produced by the same signer for different messages (using the same or different rings) are cryptographically unlinkable.
- BLSAG: The BLSAG variant intentionally provides linkability through key images. These key images allow detecting when the same key signed multiple messages, while still preserving anonymity (not revealing which specific ring member is the signer).
- Implementation Security: This library relies on the correctness of the underlying
k256crate. Whilek256is well-regarded, this specific ring signature implementation has not been independently audited.
Disclaimer
This code is highly experimental. The original author is not a cryptographer, and this Rust port, while aiming for compatibility and correctness using standard libraries, has not been audited or formally verified. Use for educational exploration at your own risk. Production usage is strongly discouraged until thorough security reviews and testing are performed by qualified individuals.
License
This project is licensed under the MIT License.
References
- Linkable Spontaneous Anonymous Group Signature for Ad Hoc Groups - (Joseph Liu et al., 2004) – basis of LSAG.
- Beritani, ring-signatures JS library – Ed25519 ring signature implementation (SAG, bLSAG, MLSAG, CLSAG).
- Blockstream Elements rust-secp256k1-zkp library – Whitelist Ring Signature in libsecp256k1-zkp (C code exposed via Rust).
- Zero to Monero 2.0 – Chapter 3, ring signature algorithms.
- Cronokirby Blog – On Monero's Ring Signatures, explains Schnorr ring signatures in detail.
Built with love by AbdelStark 🧡
Feel free to follow me on Nostr if you'd like, using my public key:
npub1hr6v96g0phtxwys4x0tm3khawuuykz6s28uzwtj5j0zc7lunu99snw2e29
Or just scan this QR code to find me:
