using System.Buffers.Binary; using System.Numerics; namespace EvilEngine.Adapters.Tests.Support; public static class Blake3 { private const int ChunkLength = 1024; private const uint ChunkStart = 1, ChunkEnd = 2, Parent = 4, Root = 8; private static readonly uint[] Iv = [0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19]; private static readonly int[] Permutation = [2, 6, 3, 10, 7, 0, 4, 13, 1, 11, 12, 5, 9, 14, 15, 8]; public static byte[] Hash(ReadOnlySpan input, int length = 32) { var hasher = new Hasher(); hasher.Update(input); return hasher.Finish(length); } private static uint[] Chunk(ReadOnlySpan chunk, ulong counter) { return ChunkOutput(chunk, counter).Value; } private static Output ChunkOutput(ReadOnlySpan chunk, ulong counter) { var cv = (uint[])Iv.Clone(); var blocks = Math.Max(1, (chunk.Length + 63) / 64); for (var b = 0; b < blocks; b++) { var piece = chunk.Slice(b * 64, Math.Min(64, chunk.Length - b * 64)); var block = Words(piece); var flags = (b == 0 ? ChunkStart : 0) | (b == blocks - 1 ? ChunkEnd : 0); if (b == blocks - 1) return new Output(cv, block, counter, (uint)piece.Length, flags); cv = Compress(cv, block, counter, 64, flags)[..8]; } throw new InvalidOperationException(); } private static Output ParentOutput(uint[] left, uint[] right, uint flags) { return new Output(Iv, [.. left, .. right], 0, 64, Parent | flags); } private static uint[] Words(ReadOnlySpan block) { Span padded = stackalloc byte[64]; block.CopyTo(padded); var words = new uint[16]; for (var i = 0; i < 16; i++) words[i] = BinaryPrimitives.ReadUInt32LittleEndian(padded[(i * 4)..]); return words; } private static uint[] Compress(uint[] cv, uint[] block, ulong counter, uint length, uint flags) { var s = new uint[16]; cv.AsSpan(0, 8).CopyTo(s); Iv.AsSpan(0, 4).CopyTo(s.AsSpan(8)); s[12] = (uint)counter; s[13] = (uint)(counter >> 32); s[14] = length; s[15] = flags; var m = (uint[])block.Clone(); for (var round = 0; round < 7; round++) { G(s, 0, 4, 8, 12, m[0], m[1]); G(s, 1, 5, 9, 13, m[2], m[3]); G(s, 2, 6, 10, 14, m[4], m[5]); G(s, 3, 7, 11, 15, m[6], m[7]); G(s, 0, 5, 10, 15, m[8], m[9]); G(s, 1, 6, 11, 12, m[10], m[11]); G(s, 2, 7, 8, 13, m[12], m[13]); G(s, 3, 4, 9, 14, m[14], m[15]); var next = new uint[16]; for (var i = 0; i < 16; i++) next[i] = m[Permutation[i]]; m = next; } for (var i = 0; i < 8; i++) { s[i] ^= s[i + 8]; s[i + 8] ^= cv[i]; } return s; } private static void G(uint[] s, int a, int b, int c, int d, uint x, uint y) { s[a] = s[a] + s[b] + x; s[d] = BitOperations.RotateRight(s[d] ^ s[a], 16); s[c] = s[c] + s[d]; s[b] = BitOperations.RotateRight(s[b] ^ s[c], 12); s[a] = s[a] + s[b] + y; s[d] = BitOperations.RotateRight(s[d] ^ s[a], 8); s[c] = s[c] + s[d]; s[b] = BitOperations.RotateRight(s[b] ^ s[c], 7); } private sealed record Output(uint[] Chaining, uint[] Block, ulong Counter, uint Length, uint Flags) { public uint[] Value => Compress(Chaining, Block, Counter, Length, Flags)[..8]; } public sealed class Hasher { private readonly byte[] _chunk = new byte[ChunkLength]; private readonly List _stack = []; private ulong _counter; private int _filled; public void Update(ReadOnlySpan input) { while (input.Length > 0) { if (_filled == ChunkLength) { var cv = Chunk(_chunk, _counter); _counter++; var total = _counter; while ((total & 1) == 0) { cv = ParentOutput(_stack[^1], cv, 0).Value; _stack.RemoveAt(_stack.Count - 1); total >>= 1; } _stack.Add(cv); _filled = 0; } var take = Math.Min(ChunkLength - _filled, input.Length); input[..take].CopyTo(_chunk.AsSpan(_filled)); _filled += take; input = input[take..]; } } public byte[] Finish(int length = 32) { var output = ChunkOutput(_chunk.AsSpan(0, _filled), _counter); for (var i = _stack.Count - 1; i >= 0; i--) output = ParentOutput(_stack[i], output.Value, 0); var words = Compress(output.Chaining, output.Block, 0, output.Length, output.Flags | Root); var bytes = new byte[64]; for (var i = 0; i < 16; i++) BinaryPrimitives.WriteUInt32LittleEndian(bytes.AsSpan(i * 4), words[i]); return bytes[..length]; } } }