Threefish1024Cipher Class
Definition
- Namespace
- Bodu.Security.Cryptography
- Assembly
- Bodu.Security.Cryptography.dll
- Package
- Bodu.Security.Cryptography 1.2.0
AVX-512 vectorised implementation of Threefish1024Cipher. The sixteen 64-bit state words are split
across two Vector512<T> registers - loVec holds the eight even-position words
(x0, x2, x4, x6, x8, x10, x12, x14) and hiVec the eight odd-position words
(x1, x3, x5, x7, x9, x11, x13, x15). Each round performs a vector add, a per-lane variable rotate (
VPROLVQ), an XOR, and a pair of single-source VPERMQ shuffles that realign the registers for the next
round's MIX pairing.
public sealed class Threefish1024Cipher : ThreefishBlockCipher, IBlockCipher, IDisposable
- Inheritance
-
Threefish1024Cipher
- Implements
- Inherited Members
- Extension Methods
Examples
// Direct single-block use - most callers should prefer the Threefish1024 SymmetricAlgorithm.
byte[] key = new byte[128]; // 1024-bit key
byte[] tweak = new byte[16]; // 128-bit tweak
RandomNumberGenerator.Fill(key);
RandomNumberGenerator.Fill(tweak);
using var cipher = new Threefish1024Cipher(key, tweak);
byte[] plaintext = new byte[128]; // one 1024-bit block
byte[] ciphertext = new byte[128];
cipher.Encrypt(plaintext, ciphertext);
byte[] roundtrip = new byte[128];
cipher.Decrypt(ciphertext, roundtrip);
// roundtrip equals plaintext
Remarks
The per-round word permutation has a uniform 4-round cycle: the same two lane-shuffle vectors realign both registers for every inter-round transition within a group, and after four shuffles both registers return to canonical layout - exactly where the subkey injection lands.
Gated on IsSupported directly because the implementation operates on Vector512<T> rather than Vector256<T>, so the Vector Length extensions are not required.
Constructors
Threefish1024Cipher(ReadOnlySpan<byte>, ReadOnlySpan<byte>)
Initializes a new instance of the Threefish1024Cipher class using the specified key and tweak.
public Threefish1024Cipher(ReadOnlySpan<byte> key, ReadOnlySpan<byte> tweak)
Parameters
keyReadOnlySpan<byte>The 1024-bit (128-byte) key used for encryption and decryption.
tweakReadOnlySpan<byte>The 128-bit (16-byte) tweak value used to modify the block cipher behavior.
Fields
KeySize
Length of the Threefish-1024 key is 1024 bits (128 bytes).
public const int KeySize = 1024
Field Value
Properties
BlockSize
Gets the block size, in bits, of the cipher (for example, 128 bits / 16 bytes for AES).
public override int BlockSize { get; }
Property Value
- int
The block size, in bits.
Remarks
The block size is expressed in bits to align with the BCL convention used by
BlockSize. Byte-array operations (encrypt,
decrypt, slice) convert to bytes at the call site as BlockSize / 8.
BlockWords
Gets the number of 64-bit words in a single block.
protected override int BlockWords { get; }
Property Value
RotationSchedule
Gets the rotation constants used for MIX/UNMIX operations in this cipher variant.
protected override int[] RotationSchedule { get; }
Property Value
- int[]
Rounds
Gets the total number of cipher rounds.
protected override int Rounds { get; }
Property Value
Methods
Decrypt(ReadOnlySpan<byte>, Span<byte>)
Decrypts a single 128-byte ciphertext block using the Threefish-1024 cipher and writes the result to the
specified output buffer.
public override void Decrypt(ReadOnlySpan<byte> input, Span<byte> output)
Parameters
inputReadOnlySpan<byte>The 128-byte ciphertext block to decrypt.
outputSpan<byte>The 128-byte buffer to receive the decrypted plaintext block.
Remarks
Dispatches to an AVX-512F vectorised implementation when supported by the host, falling back to a scalar register-resident implementation otherwise. Dispatch is gated by Bodu.Security.Cryptography.SimdCapabilities.Avx512F, which combines the hardware intrinsic with the process-wide SIMD opt-out; on hosts without AVX-512 it still folds to a compile-time constant that removes the branch, otherwise it reduces to a single cached-boolean load.
Exceptions
- ObjectDisposedException
Thrown if the cipher has been disposed.
- ArgumentException
Thrown if
inputoroutputis not 128 bytes.
Encrypt(ReadOnlySpan<byte>, Span<byte>)
Encrypts a single 128-byte plaintext block using the Threefish-1024 cipher and writes the result to the
specified output buffer.
public override void Encrypt(ReadOnlySpan<byte> input, Span<byte> output)
Parameters
inputReadOnlySpan<byte>The 128-byte plaintext block to encrypt.
outputSpan<byte>The 128-byte buffer to receive the encrypted ciphertext block.
Remarks
Dispatches to an AVX-512F vectorised implementation when supported by the host, falling back to a scalar register-resident implementation otherwise. Dispatch is gated by Bodu.Security.Cryptography.SimdCapabilities.Avx512F, which combines the hardware intrinsic with the process-wide SIMD opt-out; on hosts without AVX-512 it still folds to a compile-time constant that removes the branch, otherwise it reduces to a single cached-boolean load.
Exceptions
- ObjectDisposedException
Thrown if the cipher has been disposed.
- ArgumentException
Thrown if
inputoroutputis not 128 bytes.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |