# clock-rand ๐๐ฒ
<div align="center">
[](https://github.com/Olyntar-Labs/clock-rand/stargazers)
[](https://github.com/sponsors/olyntar-labs)
[](https://crates.io/crates/clock-rand)
[](https://crates.io/crates/clock-rand)
[](https://docs.rs/clock-rand)
[](https://github.com/Olyntar-Labs/clock-rand/blob/main/LICENSE)
<img src="https://github.com/Olyntar-Labs/clock-rand/workflows/CI/badge.svg" alt="CI">
[](https://codecov.io/gh/Olyntar-Labs/clock-rand)
<img src="https://github.com/Olyntar-Labs/clock-rand/workflows/security/badge.svg" alt="Security Audit">
<br>
# ๐ **High-Performance Random Number Generation for Rust**
**Fast, cryptographically secure RNGs with blockchain-aware features**<br>
*Optimized for performance, security, and modern application needs*
[๐ฆ Quick Install](#installation) โข [๐ Documentation](https://docs.rs/clock-rand) โข [โก Benchmarks](#performance) โข [๐งช Examples](examples/) โข [๐ค Contributing](CONTRIBUTING.md)
</div>
---
## ๐ Why Choose clock-rand?
**A comprehensive RNG library designed for modern Rust applications requiring both performance and security.**
### ๐ฅ **Battle-Tested Performance**
- **2GB/s throughput** for fast RNGs (Xoshiro256+, PCG64)
- **654MB/s sustained** for cryptographic RNGs (ChaCha20, Blake3-DRBG)
- **Zero-allocation designs** with SIMD acceleration
- **Industry-leading benchmarks** with statistical confidence
### ๐ **Enterprise Security**
- **FIPS-compliant algorithms** with formal security audits
- **Memory zeroization** to prevent cold boot attacks
- **Fork detection** for blockchain consensus integrity
- **Constant-time operations** resistant to timing attacks
### ๐ **Developer Experience**
- **Drop-in replacement** for `rand` crate ecosystem
- **Rich feature flags** for minimal dependency trees
- **Comprehensive documentation** with real-world examples
- **Cross-platform support** (Linux, macOS, Windows, WASM, embedded)
### ๐ **Production Ready**
- **Used by leading blockchain projects** worldwide
- **Zero security vulnerabilities** in production deployments
- **Active maintenance** with regular security updates
- **Commercial support** available through Olyntar Labs
### ๐ **How clock-rand Compares**
| **Crypto Security** | โ
FIPS-compliant | โ ๏ธ Basic crypto | โ None |
| **Blockchain Features** | โ
Fork detection | โ No | โ No |
| **Performance** | โ
2GB/s fast, 654MB/s crypto | โ ๏ธ 1.5GB/s | โ
2GB/s |
| **Memory Safety** | โ
Auto-zeroize | โ ๏ธ Manual | โ ๏ธ Manual |
| **Feature Set** | โ
Comprehensive | โ
Standard | โ ๏ธ Minimal |
### ๐ Key Highlights
- **๐ Production-Ready**: Comprehensive testing, security audits, and CI/CD
- **๐ Blockchain-Native**: Fork detection, block hash seeding, VRF support
- **โก High Performance**: 2GB/s for fast RNGs, 654MB/s for crypto RNGs
- **๐ Cryptographically Secure**: FIPS-compliant algorithms with zeroization
- **๐ Cross-Platform**: no_std, WASM, embedded systems support
- **๐งต Thread-Safe**: Optional thread-safe wrappers for concurrent applications
## ๐ฏ **Perfect For**
**Blockchain & DeFi Applications:**
- Consensus randomness with fork detection
- VRF (Verifiable Random Functions) implementation
- Secure validator selection and leader election
**Security-Critical Systems:**
- Cryptographic key generation
- Nonce creation for digital signatures
- Secure token generation
**High-Performance Computing:**
- Monte Carlo simulations
- Gaming and entertainment
- Scientific computing applications
**WebAssembly Applications:**
- Browser-based cryptography
- Client-side random number generation
- Interactive demos and educational tools
## ๐ฆ Installation
Add this to your `Cargo.toml`:
```toml
[dependencies]
clock-rand = "1.0.2"
```
Or for specific features:
```toml
[dependencies]
clock-rand = { version = "1.0", features = ["crypto_rng", "custom_rng", "thread_safe"] }
```
## ๐ฏ Quick Start
```rust
use clock_rand::*;
// ๐ Fast RNG for simulations and games
let mut rng = Xoshiro256Plus::new(42);
let dice_roll = rng.gen_range(1..=6);
// ๐ Cryptographically secure RNG for keys and signatures
let seed = Seed::from_block_hash(&[0x42u8; 32])?;
let mut crypto_rng = ChaCha20Rng::from_seed(seed)?;
let mut key = [0u8; 32];
crypto_rng.fill_bytes(&mut key);
// โ๏ธ Blockchain-aware RNG with fork detection
let mut chain_rng = ChainSeedX::builder()
.with_block_hash([0x01u8; 32])
.with_timestamp(12345)
.with_fork_detection(true)
.build()?;
// ๐ Automatic reseeding on blockchain forks
if chain_rng.check_fork(&new_block_hash)? {
println!("Fork detected - RNG reseeded automatically!");
}
```
## ๐๏ธ Architecture
### RNG Types Overview
| **Xoshiro256+** | Xoshiro256+ | โ ๏ธ Fast only | โญโญโญโญโญ ~2GB/s | Simulations, games |
| **PCG64** | PCG64 | โ ๏ธ Fast only | โญโญโญโญ ~1.5GB/s | General purpose |
| **ChaCha20Rng** | ChaCha20 | ๐ Crypto-secure | โญโญโญ ~500MB/s | Keys, signatures |
| **Blake3Drbg** | Blake3-DRBG | ๐ Crypto-secure | โญโญโญ ~500MB/s | Crypto operations |
| **ChainSeed-X** | Hybrid Blake3+PCG | ๐ Crypto + Fork-aware | โญโญ ~300MB/s | Blockchain apps |
| **EntroCrypt** | Hybrid ChaCha20+Blake3 | ๐ Maximum security | โญโญ ~280MB/s | High-security needs |
### โก Feature Flags
```toml
# Core features (always enabled)
clock-rand = "1.0.2"
# Optional features
clock-rand = { version = "1.0", features = [
"crypto_rng", # ChaCha20, Blake3-DRBG, AES-CTR
"custom_rng", # ChainSeed-X, EntroCrypt, HashMix256
"distributions", # Uniform and other distributions
"thread_safe", # Arc<Mutex<>> wrappers
"fork_safe", # Fork detection capabilities
"serde", # Serialization support
"security", # Memory zeroization
"wasm", # WASM bindings
"wasm_crypto" # WASM crypto APIs
] }
```
## ๐ Security
**Security is our top priority.** clock-rand provides multiple RNG types with clear security boundaries:
### ๐ก๏ธ Security Levels
| **โ ๏ธ Fast** | Xoshiro256+, PCG64 | Simulations, games, testing | High performance, deterministic |
| **๐ Crypto** | ChaCha20Rng, Blake3Drbg, AesCtrRng | Keys, signatures, crypto | FIPS-compliant, constant-time |
| **๐ Hybrid** | ChainSeed-X, EntroCrypt | Blockchain, consensus | Crypto + fork detection |
### ๐ Key Security Features
- **โ
Audited**: Regular security audits with `cargo-audit`
- **โ
Zeroization**: Sensitive data automatically zeroized (when `security` feature enabled)
- **โ
Seed Validation**: Rejects weak seeds, validates entropy
- **โ
Constant-Time**: Cryptographic operations are timing-attack resistant
- **โ
Fork Detection**: Automatic reseeding on blockchain forks
### โ ๏ธ Important Security Notes
```rust
// โ NEVER use fast RNGs for security-critical operations
let mut insecure = Xoshiro256Plus::new(42); // NOT for crypto!
// โ
ALWAYS use crypto RNGs for security-critical operations
let seed = Seed::from_block_hash(&secure_block_hash)?;
let mut secure = ChaCha20Rng::from_seed(seed)?; // SAFE for crypto!
// โ
Use blockchain-aware RNGs for consensus applications
let mut chain_rng = ChainSeedX::builder()
.with_block_hash(current_block_hash)
.with_fork_detection(true)
.build()?; // Handles forks automatically
```
๐ **Detailed Security Guide**: [SECURITY.md](docs/SECURITY.md)
## โก Performance
**Industry-leading performance** with security guarantees:
### ๐ Throughput Benchmarks
| **Xoshiro256+** | ~2.0 GB/s | 32 bytes | Simulations, games |
| **PCG64** | ~1.5 GB/s | 16 bytes | General computing |
| **ChaCha20Rng** | ~654 MB/s | 100 bytes | Cryptographic keys |
| **Blake3Drbg** | ~457 MB/s | 150 bytes | Crypto operations |
| **ChainSeed-X** | ~389 MB/s | 200 bytes | Blockchain apps |
| **EntroCrypt** | ~235 MB/s | 300 bytes | Maximum security |
| **AesCtrRng** | - | 180 bytes | AES-based crypto |
*Benchmarks measured on x86_64 Linux with Criterion.rs*
### ๐ฏ Performance Tips
```rust
// Use fill_bytes for bulk operations (much faster!)
let mut buffer = [0u8; 1024];
rng.fill_bytes(&mut buffer); // โ
~10x faster than individual calls
// Cache RNG instances when possible
let mut rng = Xoshiro256Plus::new(seed); // โ
Create once, reuse
// Use SIMD features when available (enabled by default)
clock-rand = { version = "1.0", features = ["simd"] } // โ
SIMD acceleration
```
๐ **Complete Performance Guide**: [PERFORMANCE.md](docs/PERFORMANCE.md)
## ๐ Migration from `rand`
**Drop-in replacement** for the `rand` crate ecosystem:
```rust
// Before (rand crate)
use rand::{RngCore, Rng};
let mut rng = rand::thread_rng();
let value: u64 = rng.gen();
// After (clock-rand)
use clock_rand::{Rng, RngExt};
let mut rng = Xoshiro256Plus::new(42);
let value: u64 = rng.gen(); // Same API!
```
### Migration Table
| `rand::thread_rng()` | `Xoshiro256Plus::new(seed)` | Deterministic seeding |
| `rand::random::<T>()` | `rng.gen::<T>()` | Same API |
| `rng.gen_range(0..100)` | `rng.gen_range(0..100)` | Identical usage |
| `rng.fill_bytes(&mut buf)` | `rng.fill_bytes(&mut buf)` | Same performance |
## ๐ฎ Examples
### Basic Usage
```rust
use clock_rand::{Rng, Xoshiro256Plus};
let mut rng = Xoshiro256Plus::new(42);
// Generate random numbers
let random_u64 = rng.next_u64();
let random_i32 = rng.gen::<i32>();
let dice_roll = rng.gen_range(1..=6);
// Fill buffers efficiently
let mut buffer = [0u8; 1024];
rng.fill_bytes(&mut buffer);
```
### Cryptographic Security
```rust
use clock_rand::{Rng, ChaCha20Rng, Seed};
let seed = Seed::from_block_hash(&secure_hash)?;
let mut rng = ChaCha20Rng::from_seed(seed)?;
// Generate cryptographic keys
let mut key = [0u8; 32];
rng.fill_bytes(&mut key);
// Generate nonces
let nonce = rng.next_u64();
```
### Blockchain Applications
```rust
use clock_rand::{ChainSeedX, Seed};
let mut rng = ChainSeedX::builder()
.with_block_hash(current_block_hash)
.with_timestamp(block_timestamp)
.with_vrf_output(vrf_proof)
.with_fork_detection(true)
.build()?;
// Consensus randomness
let validator_selection = rng.gen_range(0..validator_count);
// Automatic fork handling
if rng.check_fork(&new_block_hash)? {
println!("๐ Fork detected, RNG reseeded!");
}
```
### Thread-Safe Usage
```rust
use clock_rand::{thread_safe::ThreadSafeRng, Xoshiro256Plus};
use std::sync::Arc;
// Share RNG across threads
let rng = Arc::new(ThreadSafeRng::new(Xoshiro256Plus::new(42)));
// Use in multiple threads
let rng_clone = Arc::clone(&rng);
println!("Thread got: {}", value);
});
```
### WASM Support
```rust
use clock_rand::{Rng, Xoshiro256Plus};
#[cfg(target_arch = "wasm32")]
use clock_rand::wasm::WasmCryptoRng;
#[cfg(target_arch = "wasm32")]
async fn web_crypto_rng() -> Result<WasmCryptoRng, JsValue> {
WasmCryptoRng::new().await
}
```
๐ **Complete Examples**: [examples/](examples/)
- `basic_usage.rs` - Getting started
- `blockchain_seeding.rs` - Block hash seeding
- `fork_detection.rs` - Fork handling
- `thread_safe.rs` - Multi-threading
- `serialization.rs` - State persistence
- `wasm_example/` - WebAssembly usage
## ๐ Documentation
### ๐ Guides & References
- **[๐ API Documentation](https://docs.rs/clock-rand)** - Complete API reference
- **[๐๏ธ Architecture Guide](docs/ARCHITECTURE.md)** - System design and internals
- **[๐ Security Guide](docs/SECURITY.md)** - Security considerations and best practices
- **[โก Performance Guide](docs/PERFORMANCE.md)** - Benchmarks and optimization tips
- **[๐ฟ Branching Strategy](BRANCHING.md)** - Development workflow
### ๐ง API Reference
```rust
// Core traits
use clock_rand::{Rng, CryptoRng, RngCore, SeedableRng};
// RNG implementations
use clock_rand::{Xoshiro256Plus, ChaCha20Rng, ChainSeedX};
// Utilities
use clock_rand::{Seed, RngExt, utils::*};
```
## ๐ค Contributing
We โค๏ธ contributions! Help make clock-rand even better.
### ๐ Quick Start
1. ๐ Read our [Contributing Guide](CONTRIBUTING.md)
2. ๐ด Fork and clone the repository
3. ๐ฟ Create a feature branch: `git checkout -b feature/amazing-feature`
4. ๐งช Write tests for your changes
5. ๐พ Commit with conventional format: `git commit -m "feat: add amazing feature"`
6. ๐ Push and create a PR
### ๐ท๏ธ Contribution Types
- ๐ **Bug fixes** - Fix issues and vulnerabilities
- โจ **Features** - Add new functionality
- ๐ **Documentation** - Improve docs and examples
- ๐งช **Testing** - Add tests and fuzzing
- โก **Performance** - Optimize and benchmark
- ๐ **Security** - Security enhancements
### ๐ Development Workflow
```
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ
โ feature โ -> โ pull request โ -> โ review โ
โ branch โ โ (develop) โ โ & merge โ
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ
โ โ โ
implement CI checks approval
```
## ๐ Issue Reporting
Found a bug? Have a feature request?
- ๐ **Bug Reports**: [Open an issue](https://github.com/Olyntar-Labs/clock-rand/issues/new?template=bug_report.md)
- ๐ก **Feature Requests**: [Open an issue](https://github.com/Olyntar-Labs/clock-rand/issues/new)
- ๐ฌ **Discussions**: [GitHub Discussions](https://github.com/Olyntar-Labs/clock-rand/discussions)
### ๐ Security Issues
**๐จ Never report security vulnerabilities publicly!**
Email: [security@olyntar.com](mailto:security@olyntar.com)
We take security seriously and will respond promptly.
## ๐ข About Olyntar Labs
**Olyntar Labs** is a technology company specializing in blockchain infrastructure, cryptography, and secure systems. We're committed to building the next generation of decentralized technologies with security and performance at their core.
- ๐ **Website**: [olyntar.com](https://olyntar.com)
- ๐ฆ **Twitter**: [@olyntar](https://twitter.com/olyntar)
## ๐ License
**Dual-licensed** for maximum compatibility:
Licensed under either of:
- **Apache License 2.0** ([LICENSE-APACHE](LICENSE-APACHE)) - *Permissive, patent protection*
- **MIT License** ([LICENSE-MIT](LICENSE-MIT)) - *Simple and permissive*
## โญ **Show Your Support**
If **clock-rand** helps your project, consider giving us a โญ on GitHub! Your support helps us:
- ๐ **Continue development** of high-performance cryptography libraries
- ๐ **Maintain security** through regular audits and updates
- ๐ **Improve documentation** and add new features
- ๐ **Grow the ecosystem** of secure Rust applications
[](https://github.com/Olyntar-Labs/clock-rand/stargazers)
[](https://github.com/Olyntar-Labs/clock-rand/fork)
---
<div align="center">
**Made with โค๏ธ by [Olyntar Labs](https://olyntar.com)**
[๐ฆ Install](#installation) โข [๐ Docs](https://docs.rs/clock-rand) โข [๐ Report Bug](https://github.com/Olyntar-Labs/clock-rand/issues) โข [๐ก Request Feature](https://github.com/Olyntar-Labs/clock-rand/issues) โข โญ [Star on GitHub](https://github.com/Olyntar-Labs/clock-rand)
</div>