Nostringer Ring Signatures (Rust)
A blazing fast Rust implementation of the unlinkable ring signature scheme in 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)
- Table of Contents
- Disclaimer
- Problem Statement
- Key Features
- Installation
- Usage
- Examples
- Benchmarks
- API Reference
sign(message: &[u8], private_key_hex: &str, ring_pubkeys_hex: &[String]) -> Result<RingSignature, Error>verify(signature: &RingSignature, message: &[u8], ring_pubkeys_hex: &[String]) -> Result<bool, Error>generate_keypair_hex(format: &str) -> KeyPairHexRingSignatureStructKeyPairHexStructErrorEnum
- Signature Size
- Security Considerations
- License
- References
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.
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.
Key Features
- Unlinkable: Signatures hide the signer's identity. Two signatures from the same signer cannot be linked cryptographically.
- Fast: Implemented in Rust, leveraging efficient and audited cryptographic primitives from the RustCrypto ecosystem (
k256,sha2). - 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.
- Easy to Use: Simple
sign,verify, andgenerate_keypair_hexfunctions. - Minimal Dependencies: Relies on well-maintained RustCrypto crates.
- No Trusted Setup: The scheme does not require any special setup ceremony.
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 ;
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. -
Error Handling (
examples/error_handling.rs): Demonstrates proper error handling for common error scenarios.
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:
- Signing with different ring sizes (3, 5, 10, 20 members)
- Verification with different ring sizes
- Combined signing and verification
- Performance with different key formats (x-only, compressed, uncompressed)
To run the benchmarks:
This will generate detailed reports showing the performance of each operation across different parameters.
API Reference
sign(message: &[u8], private_key_hex: &str, ring_pubkeys_hex: &[String]) -> Result<RingSignature, Error>
Signs a message using the SAG-like ring signature scheme.
message: The message bytes (&[u8]) to sign.private_key_hex: The signer's private key as a 64-character hex string.ring_pubkeys_hex: A slice of public key hex strings representing the ring members. The signer's corresponding public key (or the key corresponding to the negated private key) must be present in this ring. The order of keys matters for verification.- Returns: A
Resultcontaining theRingSignatureon success, or anErroron failure (e.g., signer not in ring, invalid keys, ring too small).
verify(signature: &RingSignature, message: &[u8], ring_pubkeys_hex: &[String]) -> Result<bool, Error>
Verifies a ring signature against a message and the ring of public keys.
signature: A reference to theRingSignatureobject ({ c0, s }).message: The original message bytes (&[u8]) that were allegedly signed.ring_pubkeys_hex: A slice of public key hex strings representing the ring. Must be identical (including order) to the ring used during signing.- Returns: A
Resultcontainingtrueif the signature is valid for the message and ring, orfalseif it's invalid. Returns anErrorif inputs are malformed (e.g., wrong signature length, invalid hex).
generate_keypair_hex(format: &str) -> KeyPairHex
Generates a new random secp256k1 key pair.
format: A string slice specifying the desired public key format:"xonly": 64-hex (32 bytes), guaranteed even-Y point."compressed": 66-hex (33 bytes), starts with02or03."uncompressed": 130-hex (65 bytes), starts with04.- Defaults to
"compressed"if an unrecognized format is provided.
- Returns: A
KeyPairHexstruct containingprivate_key_hex(String) andpublic_key_hex(String). Note: The returnedprivate_key_hexis the original randomly generated scalar, even if internal negation was required to produce an even-Y public key for the"xonly"format.
RingSignature Struct
KeyPairHex Struct
Error Enum
An enum representing possible errors during signing or verification, such as invalid key formats, signer not found in the ring, ring too small, hex decoding errors, or internal cryptographic errors.
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 containskeys that could realistically be the signer in the given context. - No Trusted Setup: This scheme does not require any trusted setup procedure.
- Unlinkability: Signatures produced by the same signer for different messages (using the same or different rings) should be cryptographically unlinkable.
- No Traceability: This specific SAG implementation does not produce linkability tags (like key images used in Monero's MLSAG/CLSAG) which would allow detecting if the same key was used to sign twice within different rings for the same message. This enhances privacy but means double-spending prevention requires other mechanisms if used for voting/claiming.
- 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.
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:
