master
1//
2// SipHash is a moderately fast pseudorandom function, returning a 64-bit or 128-bit tag for an arbitrary long input.
3//
4// Typical use cases include:
5// - protection against DoS attacks for hash tables and bloom filters
6// - authentication of short-lived messages in online protocols
7//
8// https://www.aumasson.jp/siphash/siphash.pdf
9const std = @import("../std.zig");
10const assert = std.debug.assert;
11const testing = std.testing;
12const math = std.math;
13const mem = std.mem;
14
15/// SipHash function with 64-bit output.
16///
17/// Recommended parameters are:
18/// - (c_rounds=4, d_rounds=8) for conservative security; regular hash functions such as BLAKE2 or BLAKE3 are usually a better alternative.
19/// - (c_rounds=2, d_rounds=4) standard parameters.
20/// - (c_rounds=1, d_rounds=3) reduced-round function. Faster, no known implications on its practical security level.
21/// - (c_rounds=1, d_rounds=2) fastest option, but the output may be distinguishable from random data with related keys or non-uniform input - not suitable as a PRF.
22///
23/// SipHash is not a traditional hash function. If the input includes untrusted content, a secret key is absolutely necessary.
24/// And due to its small output size, collisions in SipHash64 can be found with an exhaustive search.
25pub fn SipHash64(comptime c_rounds: usize, comptime d_rounds: usize) type {
26 return SipHash(u64, c_rounds, d_rounds);
27}
28
29/// SipHash function with 128-bit output.
30///
31/// Recommended parameters are:
32/// - (c_rounds=4, d_rounds=8) for conservative security; regular hash functions such as BLAKE2 or BLAKE3 are usually a better alternative.
33/// - (c_rounds=2, d_rounds=4) standard parameters.
34/// - (c_rounds=1, d_rounds=4) reduced-round function. Recommended to hash very short, similar strings, when a 128-bit PRF output is still required.
35/// - (c_rounds=1, d_rounds=3) reduced-round function. Faster, no known implications on its practical security level.
36/// - (c_rounds=1, d_rounds=2) fastest option, but the output may be distinguishable from random data with related keys or non-uniform input - not suitable as a PRF.
37///
38/// SipHash is not a traditional hash function. If the input includes untrusted content, a secret key is absolutely necessary.
39pub fn SipHash128(comptime c_rounds: usize, comptime d_rounds: usize) type {
40 return SipHash(u128, c_rounds, d_rounds);
41}
42
43fn SipHashStateless(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize) type {
44 assert(T == u64 or T == u128);
45 assert(c_rounds > 0 and d_rounds > 0);
46
47 return struct {
48 const Self = @This();
49 const block_length = 64;
50 const key_length = 16;
51
52 v0: u64,
53 v1: u64,
54 v2: u64,
55 v3: u64,
56 msg_len: u8,
57
58 fn init(key: *const [key_length]u8) Self {
59 const k0 = mem.readInt(u64, key[0..8], .little);
60 const k1 = mem.readInt(u64, key[8..16], .little);
61
62 var d = Self{
63 .v0 = k0 ^ 0x736f6d6570736575,
64 .v1 = k1 ^ 0x646f72616e646f6d,
65 .v2 = k0 ^ 0x6c7967656e657261,
66 .v3 = k1 ^ 0x7465646279746573,
67 .msg_len = 0,
68 };
69
70 if (T == u128) {
71 d.v1 ^= 0xee;
72 }
73
74 return d;
75 }
76
77 fn update(self: *Self, b: []const u8) void {
78 std.debug.assert(b.len % 8 == 0);
79
80 var off: usize = 0;
81 while (off < b.len) : (off += 8) {
82 const blob = b[off..][0..8].*;
83 @call(.always_inline, round, .{ self, blob });
84 }
85
86 self.msg_len +%= @as(u8, @truncate(b.len));
87 }
88
89 fn final(self: *Self, b: []const u8) T {
90 std.debug.assert(b.len < 8);
91
92 self.msg_len +%= @as(u8, @truncate(b.len));
93
94 var buf = [_]u8{0} ** 8;
95 @memcpy(buf[0..b.len], b);
96 buf[7] = self.msg_len;
97 self.round(buf);
98
99 if (T == u128) {
100 self.v2 ^= 0xee;
101 } else {
102 self.v2 ^= 0xff;
103 }
104
105 comptime var i: usize = 0;
106 inline while (i < d_rounds) : (i += 1) {
107 @call(.always_inline, sipRound, .{self});
108 }
109
110 const b1 = self.v0 ^ self.v1 ^ self.v2 ^ self.v3;
111 if (T == u64) {
112 return b1;
113 }
114
115 self.v1 ^= 0xdd;
116
117 comptime var j: usize = 0;
118 inline while (j < d_rounds) : (j += 1) {
119 @call(.always_inline, sipRound, .{self});
120 }
121
122 const b2 = self.v0 ^ self.v1 ^ self.v2 ^ self.v3;
123 return (@as(u128, b2) << 64) | b1;
124 }
125
126 fn round(self: *Self, b: [8]u8) void {
127 const m = mem.readInt(u64, &b, .little);
128 self.v3 ^= m;
129
130 comptime var i: usize = 0;
131 inline while (i < c_rounds) : (i += 1) {
132 @call(.always_inline, sipRound, .{self});
133 }
134
135 self.v0 ^= m;
136 }
137
138 fn sipRound(d: *Self) void {
139 d.v0 +%= d.v1;
140 d.v1 = math.rotl(u64, d.v1, @as(u64, 13));
141 d.v1 ^= d.v0;
142 d.v0 = math.rotl(u64, d.v0, @as(u64, 32));
143 d.v2 +%= d.v3;
144 d.v3 = math.rotl(u64, d.v3, @as(u64, 16));
145 d.v3 ^= d.v2;
146 d.v0 +%= d.v3;
147 d.v3 = math.rotl(u64, d.v3, @as(u64, 21));
148 d.v3 ^= d.v0;
149 d.v2 +%= d.v1;
150 d.v1 = math.rotl(u64, d.v1, @as(u64, 17));
151 d.v1 ^= d.v2;
152 d.v2 = math.rotl(u64, d.v2, @as(u64, 32));
153 }
154
155 fn hash(msg: []const u8, key: *const [key_length]u8) T {
156 const aligned_len = msg.len - (msg.len % 8);
157 var c = Self.init(key);
158 @call(.always_inline, update, .{ &c, msg[0..aligned_len] });
159 return @call(.always_inline, final, .{ &c, msg[aligned_len..] });
160 }
161 };
162}
163
164fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize) type {
165 assert(T == u64 or T == u128);
166 assert(c_rounds > 0 and d_rounds > 0);
167
168 return struct {
169 const State = SipHashStateless(T, c_rounds, d_rounds);
170 const Self = @This();
171 pub const key_length = 16;
172 pub const mac_length = @sizeOf(T);
173 pub const block_length = 8;
174
175 state: State,
176 buf: [8]u8,
177 buf_len: usize,
178
179 /// Initialize a state for a SipHash function
180 pub fn init(key: *const [key_length]u8) Self {
181 return Self{
182 .state = State.init(key),
183 .buf = undefined,
184 .buf_len = 0,
185 };
186 }
187
188 /// Add data to the state
189 pub fn update(self: *Self, b: []const u8) void {
190 var off: usize = 0;
191
192 if (self.buf_len != 0 and self.buf_len + b.len >= 8) {
193 off += 8 - self.buf_len;
194 @memcpy(self.buf[self.buf_len..][0..off], b[0..off]);
195 self.state.update(self.buf[0..]);
196 self.buf_len = 0;
197 }
198
199 const remain_len = b.len - off;
200 const aligned_len = remain_len - (remain_len % 8);
201 self.state.update(b[off .. off + aligned_len]);
202
203 const b_slice = b[off + aligned_len ..];
204 @memcpy(self.buf[self.buf_len..][0..b_slice.len], b_slice);
205 self.buf_len += @as(u8, @intCast(b_slice.len));
206 }
207
208 pub fn peek(self: Self) [mac_length]u8 {
209 var copy = self;
210 return copy.finalResult();
211 }
212
213 /// Return an authentication tag for the current state
214 /// Assumes `out` is less than or equal to `mac_length`.
215 pub fn final(self: *Self, out: *[mac_length]u8) void {
216 mem.writeInt(T, out, self.state.final(self.buf[0..self.buf_len]), .little);
217 }
218
219 pub fn finalResult(self: *Self) [mac_length]u8 {
220 var result: [mac_length]u8 = undefined;
221 self.final(&result);
222 return result;
223 }
224
225 /// Return an authentication tag for a message and a key
226 pub fn create(out: *[mac_length]u8, msg: []const u8, key: *const [key_length]u8) void {
227 var ctx = Self.init(key);
228 ctx.update(msg);
229 ctx.final(out);
230 }
231
232 /// Return an authentication tag for the current state, as an integer
233 pub fn finalInt(self: *Self) T {
234 return self.state.final(self.buf[0..self.buf_len]);
235 }
236
237 /// Return an authentication tag for a message and a key, as an integer
238 pub fn toInt(msg: []const u8, key: *const [key_length]u8) T {
239 return State.hash(msg, key);
240 }
241 };
242}
243
244// Test vectors from reference implementation.
245// https://github.com/veorq/SipHash/blob/master/vectors.h
246const test_key = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f";
247
248test "siphash64-2-4 sanity" {
249 const vectors = [_][8]u8{
250 "\x31\x0e\x0e\xdd\x47\xdb\x6f\x72".*, // ""
251 "\xfd\x67\xdc\x93\xc5\x39\xf8\x74".*, // "\x00"
252 "\x5a\x4f\xa9\xd9\x09\x80\x6c\x0d".*, // "\x00\x01" ... etc
253 "\x2d\x7e\xfb\xd7\x96\x66\x67\x85".*,
254 "\xb7\x87\x71\x27\xe0\x94\x27\xcf".*,
255 "\x8d\xa6\x99\xcd\x64\x55\x76\x18".*,
256 "\xce\xe3\xfe\x58\x6e\x46\xc9\xcb".*,
257 "\x37\xd1\x01\x8b\xf5\x00\x02\xab".*,
258 "\x62\x24\x93\x9a\x79\xf5\xf5\x93".*,
259 "\xb0\xe4\xa9\x0b\xdf\x82\x00\x9e".*,
260 "\xf3\xb9\xdd\x94\xc5\xbb\x5d\x7a".*,
261 "\xa7\xad\x6b\x22\x46\x2f\xb3\xf4".*,
262 "\xfb\xe5\x0e\x86\xbc\x8f\x1e\x75".*,
263 "\x90\x3d\x84\xc0\x27\x56\xea\x14".*,
264 "\xee\xf2\x7a\x8e\x90\xca\x23\xf7".*,
265 "\xe5\x45\xbe\x49\x61\xca\x29\xa1".*,
266 "\xdb\x9b\xc2\x57\x7f\xcc\x2a\x3f".*,
267 "\x94\x47\xbe\x2c\xf5\xe9\x9a\x69".*,
268 "\x9c\xd3\x8d\x96\xf0\xb3\xc1\x4b".*,
269 "\xbd\x61\x79\xa7\x1d\xc9\x6d\xbb".*,
270 "\x98\xee\xa2\x1a\xf2\x5c\xd6\xbe".*,
271 "\xc7\x67\x3b\x2e\xb0\xcb\xf2\xd0".*,
272 "\x88\x3e\xa3\xe3\x95\x67\x53\x93".*,
273 "\xc8\xce\x5c\xcd\x8c\x03\x0c\xa8".*,
274 "\x94\xaf\x49\xf6\xc6\x50\xad\xb8".*,
275 "\xea\xb8\x85\x8a\xde\x92\xe1\xbc".*,
276 "\xf3\x15\xbb\x5b\xb8\x35\xd8\x17".*,
277 "\xad\xcf\x6b\x07\x63\x61\x2e\x2f".*,
278 "\xa5\xc9\x1d\xa7\xac\xaa\x4d\xde".*,
279 "\x71\x65\x95\x87\x66\x50\xa2\xa6".*,
280 "\x28\xef\x49\x5c\x53\xa3\x87\xad".*,
281 "\x42\xc3\x41\xd8\xfa\x92\xd8\x32".*,
282 "\xce\x7c\xf2\x72\x2f\x51\x27\x71".*,
283 "\xe3\x78\x59\xf9\x46\x23\xf3\xa7".*,
284 "\x38\x12\x05\xbb\x1a\xb0\xe0\x12".*,
285 "\xae\x97\xa1\x0f\xd4\x34\xe0\x15".*,
286 "\xb4\xa3\x15\x08\xbe\xff\x4d\x31".*,
287 "\x81\x39\x62\x29\xf0\x90\x79\x02".*,
288 "\x4d\x0c\xf4\x9e\xe5\xd4\xdc\xca".*,
289 "\x5c\x73\x33\x6a\x76\xd8\xbf\x9a".*,
290 "\xd0\xa7\x04\x53\x6b\xa9\x3e\x0e".*,
291 "\x92\x59\x58\xfc\xd6\x42\x0c\xad".*,
292 "\xa9\x15\xc2\x9b\xc8\x06\x73\x18".*,
293 "\x95\x2b\x79\xf3\xbc\x0a\xa6\xd4".*,
294 "\xf2\x1d\xf2\xe4\x1d\x45\x35\xf9".*,
295 "\x87\x57\x75\x19\x04\x8f\x53\xa9".*,
296 "\x10\xa5\x6c\xf5\xdf\xcd\x9a\xdb".*,
297 "\xeb\x75\x09\x5c\xcd\x98\x6c\xd0".*,
298 "\x51\xa9\xcb\x9e\xcb\xa3\x12\xe6".*,
299 "\x96\xaf\xad\xfc\x2c\xe6\x66\xc7".*,
300 "\x72\xfe\x52\x97\x5a\x43\x64\xee".*,
301 "\x5a\x16\x45\xb2\x76\xd5\x92\xa1".*,
302 "\xb2\x74\xcb\x8e\xbf\x87\x87\x0a".*,
303 "\x6f\x9b\xb4\x20\x3d\xe7\xb3\x81".*,
304 "\xea\xec\xb2\xa3\x0b\x22\xa8\x7f".*,
305 "\x99\x24\xa4\x3c\xc1\x31\x57\x24".*,
306 "\xbd\x83\x8d\x3a\xaf\xbf\x8d\xb7".*,
307 "\x0b\x1a\x2a\x32\x65\xd5\x1a\xea".*,
308 "\x13\x50\x79\xa3\x23\x1c\xe6\x60".*,
309 "\x93\x2b\x28\x46\xe4\xd7\x06\x66".*,
310 "\xe1\x91\x5f\x5c\xb1\xec\xa4\x6c".*,
311 "\xf3\x25\x96\x5c\xa1\x6d\x62\x9f".*,
312 "\x57\x5f\xf2\x8e\x60\x38\x1b\xe5".*,
313 "\x72\x45\x06\xeb\x4c\x32\x8a\x95".*,
314 };
315
316 const siphash = SipHash64(2, 4);
317
318 var buffer: [64]u8 = undefined;
319 for (vectors, 0..) |vector, i| {
320 buffer[i] = @as(u8, @intCast(i));
321
322 var out: [siphash.mac_length]u8 = undefined;
323 siphash.create(&out, buffer[0..i], test_key);
324 try testing.expectEqual(out, vector);
325 }
326}
327
328test "siphash128-2-4 sanity" {
329 const vectors = [_][16]u8{
330 "\xa3\x81\x7f\x04\xba\x25\xa8\xe6\x6d\xf6\x72\x14\xc7\x55\x02\x93".*,
331 "\xda\x87\xc1\xd8\x6b\x99\xaf\x44\x34\x76\x59\x11\x9b\x22\xfc\x45".*,
332 "\x81\x77\x22\x8d\xa4\xa4\x5d\xc7\xfc\xa3\x8b\xde\xf6\x0a\xff\xe4".*,
333 "\x9c\x70\xb6\x0c\x52\x67\xa9\x4e\x5f\x33\xb6\xb0\x29\x85\xed\x51".*,
334 "\xf8\x81\x64\xc1\x2d\x9c\x8f\xaf\x7d\x0f\x6e\x7c\x7b\xcd\x55\x79".*,
335 "\x13\x68\x87\x59\x80\x77\x6f\x88\x54\x52\x7a\x07\x69\x0e\x96\x27".*,
336 "\x14\xee\xca\x33\x8b\x20\x86\x13\x48\x5e\xa0\x30\x8f\xd7\xa1\x5e".*,
337 "\xa1\xf1\xeb\xbe\xd8\xdb\xc1\x53\xc0\xb8\x4a\xa6\x1f\xf0\x82\x39".*,
338 "\x3b\x62\xa9\xba\x62\x58\xf5\x61\x0f\x83\xe2\x64\xf3\x14\x97\xb4".*,
339 "\x26\x44\x99\x06\x0a\xd9\xba\xab\xc4\x7f\x8b\x02\xbb\x6d\x71\xed".*,
340 "\x00\x11\x0d\xc3\x78\x14\x69\x56\xc9\x54\x47\xd3\xf3\xd0\xfb\xba".*,
341 "\x01\x51\xc5\x68\x38\x6b\x66\x77\xa2\xb4\xdc\x6f\x81\xe5\xdc\x18".*,
342 "\xd6\x26\xb2\x66\x90\x5e\xf3\x58\x82\x63\x4d\xf6\x85\x32\xc1\x25".*,
343 "\x98\x69\xe2\x47\xe9\xc0\x8b\x10\xd0\x29\x93\x4f\xc4\xb9\x52\xf7".*,
344 "\x31\xfc\xef\xac\x66\xd7\xde\x9c\x7e\xc7\x48\x5f\xe4\x49\x49\x02".*,
345 "\x54\x93\xe9\x99\x33\xb0\xa8\x11\x7e\x08\xec\x0f\x97\xcf\xc3\xd9".*,
346 "\x6e\xe2\xa4\xca\x67\xb0\x54\xbb\xfd\x33\x15\xbf\x85\x23\x05\x77".*,
347 "\x47\x3d\x06\xe8\x73\x8d\xb8\x98\x54\xc0\x66\xc4\x7a\xe4\x77\x40".*,
348 "\xa4\x26\xe5\xe4\x23\xbf\x48\x85\x29\x4d\xa4\x81\xfe\xae\xf7\x23".*,
349 "\x78\x01\x77\x31\xcf\x65\xfa\xb0\x74\xd5\x20\x89\x52\x51\x2e\xb1".*,
350 "\x9e\x25\xfc\x83\x3f\x22\x90\x73\x3e\x93\x44\xa5\xe8\x38\x39\xeb".*,
351 "\x56\x8e\x49\x5a\xbe\x52\x5a\x21\x8a\x22\x14\xcd\x3e\x07\x1d\x12".*,
352 "\x4a\x29\xb5\x45\x52\xd1\x6b\x9a\x46\x9c\x10\x52\x8e\xff\x0a\xae".*,
353 "\xc9\xd1\x84\xdd\xd5\xa9\xf5\xe0\xcf\x8c\xe2\x9a\x9a\xbf\x69\x1c".*,
354 "\x2d\xb4\x79\xae\x78\xbd\x50\xd8\x88\x2a\x8a\x17\x8a\x61\x32\xad".*,
355 "\x8e\xce\x5f\x04\x2d\x5e\x44\x7b\x50\x51\xb9\xea\xcb\x8d\x8f\x6f".*,
356 "\x9c\x0b\x53\xb4\xb3\xc3\x07\xe8\x7e\xae\xe0\x86\x78\x14\x1f\x66".*,
357 "\xab\xf2\x48\xaf\x69\xa6\xea\xe4\xbf\xd3\xeb\x2f\x12\x9e\xeb\x94".*,
358 "\x06\x64\xda\x16\x68\x57\x4b\x88\xb9\x35\xf3\x02\x73\x58\xae\xf4".*,
359 "\xaa\x4b\x9d\xc4\xbf\x33\x7d\xe9\x0c\xd4\xfd\x3c\x46\x7c\x6a\xb7".*,
360 "\xea\x5c\x7f\x47\x1f\xaf\x6b\xde\x2b\x1a\xd7\xd4\x68\x6d\x22\x87".*,
361 "\x29\x39\xb0\x18\x32\x23\xfa\xfc\x17\x23\xde\x4f\x52\xc4\x3d\x35".*,
362 "\x7c\x39\x56\xca\x5e\xea\xfc\x3e\x36\x3e\x9d\x55\x65\x46\xeb\x68".*,
363 "\x77\xc6\x07\x71\x46\xf0\x1c\x32\xb6\xb6\x9d\x5f\x4e\xa9\xff\xcf".*,
364 "\x37\xa6\x98\x6c\xb8\x84\x7e\xdf\x09\x25\xf0\xf1\x30\x9b\x54\xde".*,
365 "\xa7\x05\xf0\xe6\x9d\xa9\xa8\xf9\x07\x24\x1a\x2e\x92\x3c\x8c\xc8".*,
366 "\x3d\xc4\x7d\x1f\x29\xc4\x48\x46\x1e\x9e\x76\xed\x90\x4f\x67\x11".*,
367 "\x0d\x62\xbf\x01\xe6\xfc\x0e\x1a\x0d\x3c\x47\x51\xc5\xd3\x69\x2b".*,
368 "\x8c\x03\x46\x8b\xca\x7c\x66\x9e\xe4\xfd\x5e\x08\x4b\xbe\xe7\xb5".*,
369 "\x52\x8a\x5b\xb9\x3b\xaf\x2c\x9c\x44\x73\xcc\xe5\xd0\xd2\x2b\xd9".*,
370 "\xdf\x6a\x30\x1e\x95\xc9\x5d\xad\x97\xae\x0c\xc8\xc6\x91\x3b\xd8".*,
371 "\x80\x11\x89\x90\x2c\x85\x7f\x39\xe7\x35\x91\x28\x5e\x70\xb6\xdb".*,
372 "\xe6\x17\x34\x6a\xc9\xc2\x31\xbb\x36\x50\xae\x34\xcc\xca\x0c\x5b".*,
373 "\x27\xd9\x34\x37\xef\xb7\x21\xaa\x40\x18\x21\xdc\xec\x5a\xdf\x89".*,
374 "\x89\x23\x7d\x9d\xed\x9c\x5e\x78\xd8\xb1\xc9\xb1\x66\xcc\x73\x42".*,
375 "\x4a\x6d\x80\x91\xbf\x5e\x7d\x65\x11\x89\xfa\x94\xa2\x50\xb1\x4c".*,
376 "\x0e\x33\xf9\x60\x55\xe7\xae\x89\x3f\xfc\x0e\x3d\xcf\x49\x29\x02".*,
377 "\xe6\x1c\x43\x2b\x72\x0b\x19\xd1\x8e\xc8\xd8\x4b\xdc\x63\x15\x1b".*,
378 "\xf7\xe5\xae\xf5\x49\xf7\x82\xcf\x37\x90\x55\xa6\x08\x26\x9b\x16".*,
379 "\x43\x8d\x03\x0f\xd0\xb7\xa5\x4f\xa8\x37\xf2\xad\x20\x1a\x64\x03".*,
380 "\xa5\x90\xd3\xee\x4f\xbf\x04\xe3\x24\x7e\x0d\x27\xf2\x86\x42\x3f".*,
381 "\x5f\xe2\xc1\xa1\x72\xfe\x93\xc4\xb1\x5c\xd3\x7c\xae\xf9\xf5\x38".*,
382 "\x2c\x97\x32\x5c\xbd\x06\xb3\x6e\xb2\x13\x3d\xd0\x8b\x3a\x01\x7c".*,
383 "\x92\xc8\x14\x22\x7a\x6b\xca\x94\x9f\xf0\x65\x9f\x00\x2a\xd3\x9e".*,
384 "\xdc\xe8\x50\x11\x0b\xd8\x32\x8c\xfb\xd5\x08\x41\xd6\x91\x1d\x87".*,
385 "\x67\xf1\x49\x84\xc7\xda\x79\x12\x48\xe3\x2b\xb5\x92\x25\x83\xda".*,
386 "\x19\x38\xf2\xcf\x72\xd5\x4e\xe9\x7e\x94\x16\x6f\xa9\x1d\x2a\x36".*,
387 "\x74\x48\x1e\x96\x46\xed\x49\xfe\x0f\x62\x24\x30\x16\x04\x69\x8e".*,
388 "\x57\xfc\xa5\xde\x98\xa9\xd6\xd8\x00\x64\x38\xd0\x58\x3d\x8a\x1d".*,
389 "\x9f\xec\xde\x1c\xef\xdc\x1c\xbe\xd4\x76\x36\x74\xd9\x57\x53\x59".*,
390 "\xe3\x04\x0c\x00\xeb\x28\xf1\x53\x66\xca\x73\xcb\xd8\x72\xe7\x40".*,
391 "\x76\x97\x00\x9a\x6a\x83\x1d\xfe\xcc\xa9\x1c\x59\x93\x67\x0f\x7a".*,
392 "\x58\x53\x54\x23\x21\xf5\x67\xa0\x05\xd5\x47\xa4\xf0\x47\x59\xbd".*,
393 "\x51\x50\xd1\x77\x2f\x50\x83\x4a\x50\x3e\x06\x9a\x97\x3f\xbd\x7c".*,
394 };
395
396 const siphash = SipHash128(2, 4);
397
398 var buffer: [64]u8 = undefined;
399 for (vectors, 0..) |vector, i| {
400 buffer[i] = @as(u8, @intCast(i));
401
402 var out: [siphash.mac_length]u8 = undefined;
403 siphash.create(&out, buffer[0..i], test_key[0..]);
404 try testing.expectEqual(out, vector);
405 }
406}
407
408test "iterative non-divisible update" {
409 var buf: [1024]u8 = undefined;
410 for (&buf, 0..) |*e, i| {
411 e.* = @as(u8, @truncate(i));
412 }
413
414 const key = "0x128dad08f12307";
415 const Siphash = SipHash64(2, 4);
416
417 var end: usize = 9;
418 while (end < buf.len) : (end += 9) {
419 const non_iterative_hash = Siphash.toInt(buf[0..end], key[0..]);
420
421 var siphash = Siphash.init(key);
422 var i: usize = 0;
423 while (i < end) : (i += 7) {
424 siphash.update(buf[i..@min(i + 7, end)]);
425 }
426 const iterative_hash = siphash.finalInt();
427
428 try std.testing.expectEqual(iterative_hash, non_iterative_hash);
429 }
430}