| Crates.io | clock-rand |
| lib.rs | clock-rand |
| version | 1.0.3 |
| created_at | 2026-01-13 05:47:41.232661+00 |
| updated_at | 2026-01-19 03:19:24.476984+00 |
| description | Next-generation random number generation with blockchain-aware RNGs, fork detection, and cryptographic security |
| homepage | |
| repository | https://github.com/clockinchain/clock-rand |
| max_upload_size | |
| id | 2039395 |
| size | 228,940 |
Fast, cryptographically secure RNGs with blockchain-aware features
Optimized for performance, security, and modern application needs
๐ฆ Quick Install โข ๐ Documentation โข โก Benchmarks โข ๐งช Examples โข ๐ค Contributing
A comprehensive RNG library designed for modern Rust applications requiring both performance and security.
rand crate ecosystem| Feature | clock-rand | rand crate | fastrand |
|---|---|---|---|
| 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 |
Blockchain & DeFi Applications:
Security-Critical Systems:
High-Performance Computing:
WebAssembly Applications:
Add this to your Cargo.toml:
[dependencies]
clock-rand = "1.0.2"
Or for specific features:
[dependencies]
clock-rand = { version = "1.0", features = ["crypto_rng", "custom_rng", "thread_safe"] }
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!");
}
| RNG Type | Algorithm | Security | Performance | Use Case |
|---|---|---|---|---|
| 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 |
# 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 is our top priority. clock-rand provides multiple RNG types with clear security boundaries:
| Level | RNG Types | Use Cases | Security Features |
|---|---|---|---|
| โ ๏ธ 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 |
cargo-auditsecurity feature enabled)// โ 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
Industry-leading performance with security guarantees:
| RNG Type | Throughput | Memory | Use Case |
|---|---|---|---|
| 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
// 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
randDrop-in replacement for the rand crate ecosystem:
// 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!
rand |
clock-rand |
Notes |
|---|---|---|
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 |
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);
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();
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!");
}
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);
std::thread::spawn(move || {
let value = rng_clone.lock().gen::<u64>();
println!("Thread got: {}", value);
});
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/
basic_usage.rs - Getting startedblockchain_seeding.rs - Block hash seedingfork_detection.rs - Fork handlingthread_safe.rs - Multi-threadingserialization.rs - State persistencewasm_example/ - WebAssembly usage// Core traits
use clock_rand::{Rng, CryptoRng, RngCore, SeedableRng};
// RNG implementations
use clock_rand::{Xoshiro256Plus, ChaCha20Rng, ChainSeedX};
// Utilities
use clock_rand::{Seed, RngExt, utils::*};
We โค๏ธ contributions! Help make clock-rand even better.
git checkout -b feature/amazing-featuregit commit -m "feat: add amazing feature"โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ
โ feature โ -> โ pull request โ -> โ review โ
โ branch โ โ (develop) โ โ & merge โ
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ
โ โ โ
implement CI checks approval
Found a bug? Have a feature request?
๐จ Never report security vulnerabilities publicly!
Email: security@clockinchain.com
We take security seriously and will respond promptly.
ClockInChain 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.
Dual-licensed for maximum compatibility:
Licensed under either of:
If clock-rand helps your project, consider giving us a โญ on GitHub! Your support helps us:
Made with โค๏ธ by ClockInChain
๐ฆ Install โข ๐ Docs โข ๐ Report Bug โข ๐ก Request Feature โข โญ Star on GitHub