Skip to content
0.2.0-dev Download ZIP
158 lines ·5.3 KB Raw
1using System.Buffers.Binary;
2using System.Numerics;
3
4namespace EvilEngine.Adapters.Tests.Support;
5
6public static class Blake3
7{
8 private const int ChunkLength = 1024;
9 private const uint ChunkStart = 1, ChunkEnd = 2, Parent = 4, Root = 8;
10
11 private static readonly uint[] Iv =
12 [0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19];
13
14 private static readonly int[] Permutation = [2, 6, 3, 10, 7, 0, 4, 13, 1, 11, 12, 5, 9, 14, 15, 8];
15
16 public static byte[] Hash(ReadOnlySpan<byte> input, int length = 32)
17 {
18 var hasher = new Hasher();
19 hasher.Update(input);
20 return hasher.Finish(length);
21 }
22
23 private static uint[] Chunk(ReadOnlySpan<byte> chunk, ulong counter)
24 {
25 return ChunkOutput(chunk, counter).Value;
26 }
27
28 private static Output ChunkOutput(ReadOnlySpan<byte> chunk, ulong counter)
29 {
30 var cv = (uint[])Iv.Clone();
31 var blocks = Math.Max(1, (chunk.Length + 63) / 64);
32 for (var b = 0; b < blocks; b++)
33 {
34 var piece = chunk.Slice(b * 64, Math.Min(64, chunk.Length - b * 64));
35 var block = Words(piece);
36 var flags = (b == 0 ? ChunkStart : 0) | (b == blocks - 1 ? ChunkEnd : 0);
37 if (b == blocks - 1)
38 return new Output(cv, block, counter, (uint)piece.Length, flags);
39 cv = Compress(cv, block, counter, 64, flags)[..8];
40 }
41
42 throw new InvalidOperationException();
43 }
44
45 private static Output ParentOutput(uint[] left, uint[] right, uint flags)
46 {
47 return new Output(Iv, [.. left, .. right], 0, 64, Parent | flags);
48 }
49
50 private static uint[] Words(ReadOnlySpan<byte> block)
51 {
52 Span<byte> padded = stackalloc byte[64];
53 block.CopyTo(padded);
54 var words = new uint[16];
55 for (var i = 0; i < 16; i++)
56 words[i] = BinaryPrimitives.ReadUInt32LittleEndian(padded[(i * 4)..]);
57 return words;
58 }
59
60 private static uint[] Compress(uint[] cv, uint[] block, ulong counter, uint length, uint flags)
61 {
62 var s = new uint[16];
63 cv.AsSpan(0, 8).CopyTo(s);
64 Iv.AsSpan(0, 4).CopyTo(s.AsSpan(8));
65 s[12] = (uint)counter;
66 s[13] = (uint)(counter >> 32);
67 s[14] = length;
68 s[15] = flags;
69 var m = (uint[])block.Clone();
70 for (var round = 0; round < 7; round++)
71 {
72 G(s, 0, 4, 8, 12, m[0], m[1]);
73 G(s, 1, 5, 9, 13, m[2], m[3]);
74 G(s, 2, 6, 10, 14, m[4], m[5]);
75 G(s, 3, 7, 11, 15, m[6], m[7]);
76 G(s, 0, 5, 10, 15, m[8], m[9]);
77 G(s, 1, 6, 11, 12, m[10], m[11]);
78 G(s, 2, 7, 8, 13, m[12], m[13]);
79 G(s, 3, 4, 9, 14, m[14], m[15]);
80 var next = new uint[16];
81 for (var i = 0; i < 16; i++)
82 next[i] = m[Permutation[i]];
83 m = next;
84 }
85
86 for (var i = 0; i < 8; i++)
87 {
88 s[i] ^= s[i + 8];
89 s[i + 8] ^= cv[i];
90 }
91
92 return s;
93 }
94
95 private static void G(uint[] s, int a, int b, int c, int d, uint x, uint y)
96 {
97 s[a] = s[a] + s[b] + x;
98 s[d] = BitOperations.RotateRight(s[d] ^ s[a], 16);
99 s[c] = s[c] + s[d];
100 s[b] = BitOperations.RotateRight(s[b] ^ s[c], 12);
101 s[a] = s[a] + s[b] + y;
102 s[d] = BitOperations.RotateRight(s[d] ^ s[a], 8);
103 s[c] = s[c] + s[d];
104 s[b] = BitOperations.RotateRight(s[b] ^ s[c], 7);
105 }
106
107 private sealed record Output(uint[] Chaining, uint[] Block, ulong Counter, uint Length, uint Flags)
108 {
109 public uint[] Value => Compress(Chaining, Block, Counter, Length, Flags)[..8];
110 }
111
112 public sealed class Hasher
113 {
114 private readonly byte[] _chunk = new byte[ChunkLength];
115 private readonly List<uint[]> _stack = [];
116 private ulong _counter;
117 private int _filled;
118
119 public void Update(ReadOnlySpan<byte> input)
120 {
121 while (input.Length > 0)
122 {
123 if (_filled == ChunkLength)
124 {
125 var cv = Chunk(_chunk, _counter);
126 _counter++;
127 var total = _counter;
128 while ((total & 1) == 0)
129 {
130 cv = ParentOutput(_stack[^1], cv, 0).Value;
131 _stack.RemoveAt(_stack.Count - 1);
132 total >>= 1;
133 }
134
135 _stack.Add(cv);
136 _filled = 0;
137 }
138
139 var take = Math.Min(ChunkLength - _filled, input.Length);
140 input[..take].CopyTo(_chunk.AsSpan(_filled));
141 _filled += take;
142 input = input[take..];
143 }
144 }
145
146 public byte[] Finish(int length = 32)
147 {
148 var output = ChunkOutput(_chunk.AsSpan(0, _filled), _counter);
149 for (var i = _stack.Count - 1; i >= 0; i--)
150 output = ParentOutput(_stack[i], output.Value, 0);
151 var words = Compress(output.Chaining, output.Block, 0, output.Length, output.Flags | Root);
152 var bytes = new byte[64];
153 for (var i = 0; i < 16; i++)
154 BinaryPrimitives.WriteUInt32LittleEndian(bytes.AsSpan(i * 4), words[i]);
155 return bytes[..length];
156 }
157 }
158}