|
| 1 | +# TrueEntropy 🎲 |
| 2 | + |
| 3 | +[](https://badge.fury.io/py/trueentropy) |
| 4 | +[](https://www.python.org/downloads/) |
| 5 | +[](https://opensource.org/licenses/MIT) |
| 6 | +[](https://github.com/psf/black) |
| 7 | + |
| 8 | +**True randomness from real-world entropy sources.** |
| 9 | + |
| 10 | +TrueEntropy harvests chaos from the physical world to generate truly random numbers. Unlike pseudo-random number generators (PRNGs) that use deterministic algorithms, TrueEntropy collects entropy from: |
| 11 | + |
| 12 | +- ⏱️ **CPU Timing Jitter** - Nanosecond variations in code execution |
| 13 | +- 🌐 **Network Latency** - The "weather" of internet infrastructure |
| 14 | +- 💻 **System State** - RAM, processes, and hardware fluctuations |
| 15 | +- 🌍 **External APIs** - Seismic activity (USGS), cryptocurrency prices |
| 16 | + |
| 17 | +All entropy sources are mixed using SHA-256 cryptographic hashing, ensuring uniform distribution and unpredictability. |
| 18 | + |
| 19 | +## Installation |
| 20 | + |
| 21 | +```bash |
| 22 | +pip install trueentropy |
| 23 | +``` |
| 24 | + |
| 25 | +## Quick Start |
| 26 | + |
| 27 | +```python |
| 28 | +import trueentropy |
| 29 | + |
| 30 | +# Generate a random float [0.0, 1.0) |
| 31 | +value = trueentropy.random() |
| 32 | +print(f"Random float: {value}") |
| 33 | + |
| 34 | +# Generate a random integer in range [1, 100] |
| 35 | +number = trueentropy.randint(1, 100) |
| 36 | +print(f"Random integer: {number}") |
| 37 | + |
| 38 | +# Random boolean (coin flip) |
| 39 | +coin = trueentropy.randbool() |
| 40 | +print(f"Coin flip: {'Heads' if coin else 'Tails'}") |
| 41 | + |
| 42 | +# Random choice from a sequence |
| 43 | +colors = ["red", "green", "blue", "yellow"] |
| 44 | +color = trueentropy.choice(colors) |
| 45 | +print(f"Random color: {color}") |
| 46 | + |
| 47 | +# Generate random bytes |
| 48 | +secret = trueentropy.randbytes(32) |
| 49 | +print(f"Random bytes: {secret.hex()}") |
| 50 | + |
| 51 | +# Check entropy health |
| 52 | +health = trueentropy.health() |
| 53 | +print(f"Entropy health: {health['score']}/100 ({health['status']})") |
| 54 | +``` |
| 55 | + |
| 56 | +## Background Collector |
| 57 | + |
| 58 | +For applications requiring continuous randomness, start the background collector: |
| 59 | + |
| 60 | +```python |
| 61 | +import trueentropy |
| 62 | + |
| 63 | +# Start collecting entropy every 2 seconds |
| 64 | +trueentropy.start_collector(interval=2.0) |
| 65 | + |
| 66 | +# ... your application code ... |
| 67 | + |
| 68 | +# Generate random numbers (pool is continuously filled) |
| 69 | +for _ in range(1000): |
| 70 | + value = trueentropy.random() |
| 71 | + |
| 72 | +# Stop when done |
| 73 | +trueentropy.stop_collector() |
| 74 | +``` |
| 75 | + |
| 76 | +## Entropy Sources |
| 77 | + |
| 78 | +### Timing Jitter |
| 79 | +Measures nanosecond variations in CPU execution time. The operating system's scheduler introduces unpredictable delays that are impossible to reproduce. |
| 80 | + |
| 81 | +### Network Latency |
| 82 | +Pings multiple servers (Cloudflare, Google) and measures response times. Network congestion, routing changes, and physical distance create natural randomness. |
| 83 | + |
| 84 | +### System State |
| 85 | +Samples volatile system metrics: |
| 86 | +- Available RAM (changes constantly) |
| 87 | +- Number of running processes |
| 88 | +- CPU usage percentages |
| 89 | +- System uptime with high precision |
| 90 | + |
| 91 | +### External APIs (Optional) |
| 92 | +Fetches real-world data: |
| 93 | +- **USGS Earthquake API** - Latest seismic magnitude readings |
| 94 | +- **Cryptocurrency prices** - Bitcoin price with 8 decimal precision |
| 95 | + |
| 96 | +## API Reference |
| 97 | + |
| 98 | +### Core Functions |
| 99 | + |
| 100 | +| Function | Description | |
| 101 | +|----------|-------------| |
| 102 | +| `random()` | Returns float in [0.0, 1.0) | |
| 103 | +| `randint(a, b)` | Returns integer in [a, b] | |
| 104 | +| `randbool()` | Returns True or False | |
| 105 | +| `choice(seq)` | Returns random element from sequence | |
| 106 | +| `randbytes(n)` | Returns n random bytes | |
| 107 | +| `shuffle(seq)` | Shuffles sequence in-place | |
| 108 | + |
| 109 | +### Management Functions |
| 110 | + |
| 111 | +| Function | Description | |
| 112 | +|----------|-------------| |
| 113 | +| `health()` | Returns entropy pool health status | |
| 114 | +| `start_collector(interval)` | Starts background entropy collection | |
| 115 | +| `stop_collector()` | Stops background collection | |
| 116 | +| `feed(data)` | Manually feed entropy into the pool | |
| 117 | + |
| 118 | +## How It Works |
| 119 | + |
| 120 | +``` |
| 121 | +┌─────────────────────────────────────────────────────────────┐ |
| 122 | +│ ENTROPY HARVESTERS │ |
| 123 | +│ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────────┐ │ |
| 124 | +│ │ Timing │ │ Network │ │ System │ │ External │ │ |
| 125 | +│ │ Jitter │ │ Latency │ │ State │ │ APIs │ │ |
| 126 | +│ └────┬─────┘ └────┬─────┘ └────┬─────┘ └──────┬───────┘ │ |
| 127 | +│ │ │ │ │ │ |
| 128 | +│ └────────────┴─────┬──────┴──────────────┘ │ |
| 129 | +│ ▼ │ |
| 130 | +│ ┌─────────────┐ │ |
| 131 | +│ │ MIXER │ SHA-256 Hashing │ |
| 132 | +│ │ (Whitening)│ Avalanche Effect │ |
| 133 | +│ └──────┬──────┘ │ |
| 134 | +│ ▼ │ |
| 135 | +│ ┌───────────────────────┐ │ |
| 136 | +│ │ ENTROPY POOL │ 4096 bits │ |
| 137 | +│ │ (Accumulated State) │ Thread-safe │ |
| 138 | +│ └───────────┬───────────┘ │ |
| 139 | +│ ▼ │ |
| 140 | +│ ┌─────────────┐ │ |
| 141 | +│ │ EXTRACTOR │ Secure extraction │ |
| 142 | +│ │ (Tap) │ Depletion protection │ |
| 143 | +│ └──────┬──────┘ │ |
| 144 | +│ ▼ │ |
| 145 | +│ ┌────────────────┴────────────────┐ │ |
| 146 | +│ ▼ ▼ ▼ │ |
| 147 | +│ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ |
| 148 | +│ │ Float │ │ Integer │ │ Bytes │ │ |
| 149 | +│ │ [0.0,1.0)│ │ [a, b] │ │ n │ │ |
| 150 | +│ └──────────┘ └──────────┘ └──────────┘ │ |
| 151 | +└─────────────────────────────────────────────────────────────┘ |
| 152 | +``` |
| 153 | + |
| 154 | +## Security Considerations |
| 155 | + |
| 156 | +- **Not a CSPRNG replacement**: While TrueEntropy uses cryptographic primitives, it's designed for applications needing real-world randomness, not as a replacement for `secrets` module in security contexts. |
| 157 | +- **Network dependency**: Some entropy sources require network access. The library gracefully degrades when sources are unavailable. |
| 158 | +- **Rate limiting**: External APIs may have rate limits. Use the background collector for sustained generation. |
| 159 | + |
| 160 | +## Contributing |
| 161 | + |
| 162 | +Contributions are welcome! Please read our [Contributing Guide](CONTRIBUTING.md) for details. |
| 163 | + |
| 164 | +```bash |
| 165 | +# Clone the repository |
| 166 | +git clone https://github.com/trueentropy/trueentropy.git |
| 167 | +cd trueentropy |
| 168 | + |
| 169 | +# Install development dependencies |
| 170 | +pip install -e ".[dev]" |
| 171 | + |
| 172 | +# Run tests |
| 173 | +pytest |
| 174 | + |
| 175 | +# Run linting |
| 176 | +ruff check src/ |
| 177 | +black --check src/ |
| 178 | +mypy src/ |
| 179 | +``` |
| 180 | + |
| 181 | +## License |
| 182 | + |
| 183 | +MIT License - see [LICENSE](LICENSE) for details. |
| 184 | + |
| 185 | +## Acknowledgments |
| 186 | + |
| 187 | +Inspired by: |
| 188 | +- Linux `/dev/random` and the entropy pool concept |
| 189 | +- [random.org](https://random.org) - Atmospheric noise randomness |
| 190 | +- Hardware random number generators (HRNGs) |
| 191 | + |
| 192 | +--- |
| 193 | + |
| 194 | +**TrueEntropy** - *Because the universe is the best random number generator.* 🌌 |
0 commit comments