Cha Cha Engine Structure
Represents a ChaCha counter-based random number generator engine with 32-bit output.
Definition
Namespace: Numerics.NET.Random.Engines
Assembly: Numerics.NET (in Numerics.NET.dll) Version: 10.3.0
C#
Assembly: Numerics.NET (in Numerics.NET.dll) Version: 10.3.0
public struct ChaChaEngine : IBlockEngine<uint>,
IRandomEngine, IStateful, ICounterBased<uint>, IJumpable, IAdvanceable- Implements
- IBlockEngine<UInt32>, ICounterBased<UInt32>, IRandomEngine, IAdvanceable, IJumpable, IStateful
Remarks
The ChaCha engine is a counter-based (CBRNG) random number generator based on the ChaCha stream cipher designed by Daniel J. Bernstein. It transforms a 128-bit counter and 256-bit key into 512-bit random output (16 × 32-bit words).
Algorithm Details:
- Key size: 256 bits (8 × 32-bit words)
- Counter size: 128 bits (4 × 32-bit words: 64-bit counter + 64-bit nonce)
- Block size: 512 bits (16 × 32-bit words)
- Rounds: Configurable (8, 12, or 20)
- Statistical quality: Cryptographic-grade security
Counter-Based Design:
As a counter-based generator, ChaCha allows:
- Direct position access: Jump to any position in O(1) by setting the counter
- Parallel generation: Multiple threads can use different counter ranges
- Reproducibility: Same key/counter always produces the same output block
- Cryptographic quality: Suitable for security-critical applications
Variant Support:
- ChaCha20: 20 rounds (default, standard security)
- ChaCha12: 12 rounds (reduced security, higher performance)
- ChaCha8: 8 rounds (minimal security, highest performance)
Use Cases:
- Cryptographic key generation and secure token creation
- Parallel computing with independent, non-overlapping sequences
- Security-critical applications requiring cryptographic-quality randomness
Reference: Daniel J. Bernstein. "ChaCha, a variant of Salsa20." 2008. RFC 8439: "ChaCha20 and Poly1305 for IETF Protocols."
Example
var engine = new ChaChaEngine(20); // ChaCha20
engine.Seed(SeedSequences.SplitMix64(54321));
uint value = engine.Next();
// Set counter for direct position access
uint[] counter = new uint[4] { 0, 0, 0, 0 };
engine.SetCounter(counter);
// Jump ahead by 1000 blocks
engine.AdvanceBlock(1000);Constructors
| Cha | Initializes a new ChaCha engine with the specified number of rounds. |
Properties
| Bits | Gets the native word size in bits. |
| Block | Gets the number of native words addressable in each block. |
| Counter | Gets the number of words in the engine counter. |
| Jump | Gets the size category of the primary jump operation. |
| Key | Gets the number of words in the engine key. |
| Long | Gets the size category of the long jump operation. |
| Name | Gets a human-readable name for this engine. |
| Rounds | Gets the number of rounds used in the ChaCha permutation. |
| State | Gets the number of bytes required to save/load this engine's state. |
Methods
| Advance | Advances the RNG state by the specified number of outputs. |
| Advance | Advances the engine state to the next block (next entropy window). |
| Advance | Advances the counter by the specified number of blocks. |
| Equals | Indicates whether this instance and a specified object are equal. (Inherited from ValueType) |
| Fill | Fills the destination span with values from a contiguous range of indices within the current block. |
| Generate | Computes the output block for the current key and counter. |
| Generate | Generates the ChaCha stream block for a 16-word (512-bit) state. |
| Get | Copies the current counter into the provided destination span. |
| Get | Returns the hash code for this instance. (Inherited from ValueType) |
| Get | Copies the current key into the provided destination span. |
| Get | Gets the Type of the current instance. (Inherited from Object) |
| Jump | Jumps the RNG forward by the specified number of primary jump strides. |
| Load | Loads the engine's state from the source span in a stable little-endian format. |
| Long | Jumps the RNG forward by the specified number of long jump strides. |
| Output | Returns the native word at the specified index within the current block. |
| Permute | Applies the ChaCha permutation to a 16-word (512-bit) state. |
| Reset | Resets the counter to all zeros. |
| Save | Saves the engine's state to the destination span in a stable little-endian format. |
| Seed( | Directly sets the ChaCha key with counter/nonce initialized to zero. |
| Seed( | Initializes the engine state from a seed sequence. |
| Seed( | Directly sets the ChaCha key and nonce. |
| Set | Sets the counter to the specified value. |
| Set | Sets the key to the specified value. |
| ToString | Returns the fully qualified type name of this instance. (Inherited from ValueType) |