veil-crypto
Pure Rust implementation of secp256k1 cryptographic primitives for Veil blockchain.
Overview
veil-crypto is a pure Rust cryptography library that implements all the cryptographic primitives needed for Veil's privacy features. Unlike traditional implementations that rely on C libraries (libsecp256k1-zkp), this library is 100% Rust, making it fully compatible with WebAssembly and safe from memory-related vulnerabilities.
Features
- Pure Rust - No C dependencies, fully WASM-compatible
- Pedersen Commitments - Hide transaction amounts
- Range Proofs - Prove values are valid without revealing them
- MLSAG Ring Signatures - Anonymous sender via ring signatures
- Borromean Ring Signatures - Efficient ring signatures
- Key Images - Prevent double-spending with hash-to-curve
- ECDH - Shared secret generation for stealth addresses
- Stealth Keys - Derive one-time keys for recipients
Installation
Add to your Cargo.toml:
[]
= "0.1"
Usage Examples
Pedersen Commitments
use pedersen_commit;
let value = 1000u64;
let blind = ; // Blinding factor
let commitment = pedersen_commit?;
println!;
Range Proofs
use ;
let value = 1000u64;
let blind = ;
let commitment = pedersen_commit?;
// Generate range proof
let proof = rangeproof_sign?;
// Verify range proof
let valid = rangeproof_verify?;
assert!;
MLSAG Ring Signatures
use ;
// Prepare MLSAG context
let context = prepare_mlsag?;
// Generate signature
let signature = generate_mlsag?;
// Verify signature
let valid = verify_mlsag?;
assert!;
Key Image Generation
use generate_keyimage;
let secret_key = ;
let key_image = generate_keyimage?;
ECDH Shared Secrets
use ecdh_veil;
let secret_key = ;
let public_key = ; // Compressed pubkey
let shared_secret = ecdh_veil?;
Architecture
Core Modules
utils.rs- EC operations (derive_pubkey, point_add_scalar, etc.)ecdh.rs- ECDH_VEIL implementation for stealth addresseskeyimage.rs- Key image generation using hash-to-curvepedersen.rs- Pedersen commitments with QR format handlingrangeproof.rs- Bulletproof-style range proofsmlsag.rs- MLSAG signatures (870 lines, fully tested)borromean.rs- Borromean ring signatures
Dependencies
All dependencies are pure Rust and WASM-compatible:
- k256 - secp256k1 elliptic curve arithmetic
- sha2 - SHA-256 hashing
- sha3 - Keccak hashing
- hmac - HMAC-DRBG for deterministic randomness
Testing
# Output:
# running 62 tests
# test result: ok. 62 passed; 0 failed
Test Coverage
- ✅ ECDH (1 test)
- ✅ Pedersen commitments (7 tests)
- ✅ Key images (3 tests)
- ✅ Range proofs (12 tests)
- ✅ Borromean signatures (9 tests)
- ✅ MLSAG (1 comprehensive test)
- ✅ Utilities (29 tests)
All tests use vectors generated from the original C implementation to ensure compatibility.
Critical Discovery: Pedersen QR Format
During development, we discovered a critical bug in how Pedersen format (0x08/0x09) commitments are converted to standard format (0x02/0x03).
The Problem: Simple bit mapping fails for ~50% of points because Pedersen format encodes based on quadratic residue (QR), not y-coordinate parity.
The Solution: Compute canonical y-coordinate via sqrt(x³ + 7), check parity, then select the correct point based on the format prefix bit.
See veil-crypto/src/pedersen.rs for implementation details. This fix is critical for MLSAG signature validity.
WASM Support
This library is designed to compile to WebAssembly. For JavaScript/TypeScript bindings, see the companion package:
@veil/secp256k1-wasm- WASM bindings for browser and Node.js
Security Considerations
Status: Ready for integration testing
This implementation is:
- ✅ Feature complete
- ✅ Fully tested (62 tests passing)
- ✅ Byte-perfect compatible with Veil Core
- ✅ Pure Rust (memory-safe)
For production use:
- ⚠️ Professional security audit recommended
- ⚠️ Constant-time operations should be verified
- ⚠️ Side-channel analysis suggested
- ⚠️ Use on testnet first
Performance
| Operation | Time | Notes |
|---|---|---|
| Pedersen commitment | ~0.5ms | Pure Rust k256 |
| Range proof generation | ~50-100ms | CPU-intensive |
| MLSAG generation | ~50-100ms | Per input |
| Key image generation | ~1-2ms | Hash-to-curve |
Documentation
Generate and view the full API documentation:
Contributing
Contributions welcome! Please ensure:
- All tests pass:
cargo test - Code is formatted:
cargo fmt - No warnings:
cargo clippy - Add tests for new features
License
MIT License - See LICENSE for details
Related Projects
- veil-wasm - WASM bindings for JavaScript/TypeScript
- Veil Core - Official Veil blockchain
- Monero - Original RingCT research
Support
- Issues: GitHub Issues
- Discussions: GitHub Discussions
Built with 🦀 Rust for the Veil community