hash.bend

bend-kit-hash@0.3.0.2 · 674 lines · 94 definitions

1# Non-cryptographic hashes over Bytes: FNV-1a, xxHash, SipHash-1-3, CRC-32, and Adler-32. Source: https://github.com/paymog/bend-kit/tree/main/hash
2import Base
3import bend-kit-bytes@0.3.3.0/bytes.bend as Bytes
4
5# Each hash hands the buffer back beside its value. A buffer's bytes at or past len are 0.
6# Each hash also has a .str form over a byte string (one Char per octet), copied to Bytes first.
7#   import bend-kit-hash@0.3.0.2/hash.bend as Hash
8
9# A 64-bit value as two halves. Base has no native U64, and int's U64 is a bit list.
10type W64 is Data:
11  W64{hi: U32, lo: U32}
12
13def W64.xor(a: W64, b: W64) -> W64:
14  match a b:
15    case W64{ah, al} W64{bh, bl}:
16      W64{U32.xor(ah, bh), U32.xor(al, bl)}
17
18def W64.add.of(+ah: U32, +bh: U32, +al: U32, +lo: U32) -> W64:
19  W64{(ah + bh + Bool.pick(U32, U32.is_lt(lo, al), 1, 0) : U32), lo}
20
21def W64.add(a: W64, b: W64) -> W64:
22  match a b:
23    case W64{+ah, +al} W64{+bh, +bl}:
24      W64.add.of(ah, bh, al, (al + bl : U32))
25
26# The full 64-bit product of two U32, from four 16-bit partial products.
27def mul32.of(+p00: U32, +p01: U32, +p10: U32, +p11: U32) -> W64:
28  +mid = ((p00 >> 16n) + (p01 .&. 65535) + (p10 .&. 65535) : U32)
29  W64{(p11 + (p01 >> 16n) + (p10 >> 16n) + (mid >> 16n) : U32), ((p00 .&. 65535) .|. (mid << 16n) : U32)}
30
31def mul32(+x: U32, +y: U32) -> W64:
32  +x0 = (x .&. 65535 : U32)
33  +x1 = (x >> 16n : U32)
34  +y0 = (y .&. 65535 : U32)
35  +y1 = (y >> 16n : U32)
36  mul32.of((x0 * y0 : U32), (x0 * y1 : U32), (x1 * y0 : U32), (x1 * y1 : U32))
37
38def W64.mul.of(+cross: U32, p: W64) -> W64:
39  W64{+h, +l} = p
40  W64{(h + cross : U32), l}
41
42def W64.mul(a: W64, b: W64) -> W64:
43  match a b:
44    case W64{+ah, +al} W64{+bh, +bl}:
45      W64.mul.of(((al * bh) + (ah * bl) : U32), mul32(al, bl))
46
47# Rotate left by k, for 0 < k < 32; j is 32 - k.
48def W64.rotl(+k: Nat, +j: Nat, a: W64) -> W64:
49  match a:
50    case W64{+h, +l}:
51      W64{((h << k) .|. (l >> j) : U32), ((l << k) .|. (h >> j) : U32)}
52
53def W64.swap(a: W64) -> W64:
54  match a:
55    case W64{h, l}:
56      W64{l, h}
57
58# Shift right by k, for 0 < k < 32; j is 32 - k.
59def W64.shr(+k: Nat, +j: Nat, a: W64) -> W64:
60  match a:
61    case W64{+h, +l}:
62      W64{(h >> k : U32), ((l >> k) .|. (h << j) : U32)}
63
64def W64.lo(a: W64) -> U32:
65  match a:
66    case W64{h, l}:
67      l
68
69def rotl32(+x: U32, +k: Nat, +j: Nat) -> U32:
70  ((x << k) .|. (x >> j) : U32)
71
72# Folds f over n words from word i on. r holds the array and word i.
73def wfold(~T: Type, ~f: T -> @+c: U32 -> T, n: Nat, r: Array<U32> & U32, +i: U32, acc: T) -> Array<U32> & T:
74  match n:
75    case 0n:
76      (a, w) = r
77      (a, acc)
78    case 1n+p:
79      (a, +w) = r
80      +j = (i + 1 : U32)
81      wfold(~T, ~f, p, Array.get(U32, a, j), j, f(acc, w))
82
83def wfrom(~T: Type, ~f: T -> @+c: U32 -> T, n: Nat, a: Array<U32>, +i: U32, acc: T) -> Array<U32> & T:
84  wfold(~T, ~f, n, Array.get(U32, a, i), i, acc)
85
86# Words i and i + 1 as one little-endian 64-bit lane.
87def get2.of(+lo: U32, r: Array<U32> & U32) -> Array<U32> & W64:
88  (a, +hi) = r
89  (a, W64{hi, lo})
90
91def get2.lo(+i: U32, r: Array<U32> & U32) -> Array<U32> & W64:
92  (a, +lo) = r
93  get2.of(lo, Array.get(U32, a, (i + 1 : U32)))
94
95def get2(a: Array<U32>, +i: U32) -> Array<U32> & W64:
96  get2.lo(i, Array.get(U32, a, i))
97
98# Folds f over n 64-bit lanes from word i on. r holds the array and the lane at i.
99def dfold(~T: Type, ~f: T -> @+m: W64 -> T, n: Nat, r: Array<U32> & W64, +i: U32, acc: T) -> Array<U32> & T:
100  match n:
101    case 0n:
102      (a, m) = r
103      (a, acc)
104    case 1n+p:
105      (a, +m) = r
106      +j = (i + 2 : U32)
107      dfold(~T, ~f, p, get2(a, j), j, f(acc, m))
108
109def dfrom(~T: Type, ~f: T -> @+m: W64 -> T, n: Nat, a: Array<U32>, +i: U32, acc: T) -> Array<U32> & T:
110  dfold(~T, ~f, n, get2(a, i), i, acc)
111
112# Folds f over the low k bytes of w, lowest first.
113def tail(~T: Type, ~f: T -> @+c: U32 -> T, k: Nat, +w: U32, acc: T) -> T:
114  match k:
115    case 0n:
116      acc
117    case 1n+p:
118      tail(~T, ~f, p, (w >> 8n : U32), f(acc, (w .&. 255 : U32)))
119
120def bytewise.last(~T: Type, ~byte: T -> @+c: U32 -> T, +len: U32, acc: T, r: Array<U32> & U32) -> Bytes.Bytes & T:
121  (a, +w) = r
122  (Bytes.Bytes{len, a}, tail(~T, ~byte, U32.to_nat((len .&. 3 : U32)), w, acc))
123
124def bytewise.rest(~T: Type, ~byte: T -> @+c: U32 -> T, +len: U32, r: Array<U32> & T) -> Bytes.Bytes & T:
125  (a, acc) = r
126  bytewise.last(~T, ~byte, len, acc, Array.get(U32, a, (len >> 2n : U32)))
127
128# A hash that eats one byte at a time: word takes the four bytes of a word, byte one.
129def bytewise(~T: Type, ~byte: T -> @+c: U32 -> T, ~word: T -> @+c: U32 -> T, b: Bytes.Bytes, acc: T) -> Bytes.Bytes & T:
130  Bytes.Bytes{+len, buf} = b
131  bytewise.rest(~T, ~byte, len, wfrom(~T, ~word, U32.to_nat((len >> 2n : U32)), buf, 0, acc))
132
133# FNV-1a (draft-eastlake-fnv), 32 and 64 bits.
134def fnv32.byte(h: U32, +c: U32) -> U32:
135  ((h .^. c) * 16777619 : U32)
136
137def fnv32.word(h: U32, +w: U32) -> U32:
138  fnv32.byte(fnv32.byte(fnv32.byte(fnv32.byte(h, (w .&. 255 : U32)), ((w >> 8n) .&. 255 : U32)), ((w >> 16n) .&. 255 : U32)), (w >> 24n : U32))
139
140def fnv1a32(b: Bytes.Bytes) -> Bytes.Bytes & U32:
141  bytewise(~U32, ~fnv32.byte, ~fnv32.word, b, 2166136261)
142
143def fnv64.byte(h: W64, +c: U32) -> W64:
144  match h:
145    case W64{hi, lo}:
146      W64.mul(W64{hi, (lo .^. c : U32)}, W64{256, 435})
147
148def fnv64.word(h: W64, +w: U32) -> W64:
149  fnv64.byte(fnv64.byte(fnv64.byte(fnv64.byte(h, (w .&. 255 : U32)), ((w >> 8n) .&. 255 : U32)), ((w >> 16n) .&. 255 : U32)), (w >> 24n : U32))
150
151def fnv1a64(b: Bytes.Bytes) -> Bytes.Bytes & W64:
152  bytewise(~W64, ~fnv64.byte, ~fnv64.word, b, W64{3421674724, 2216829733})
153
154# CRC-32 (ISO-HDLC, as in gzip and zlib): reflected, polynomial 0xEDB88320.
155# The 256-entry table as a match, so no call rebuilds it.
156def crc.tab(+i: U32) -> U32:
157  match i:
158    case 0: 0
159    case 1: 1996959894
160    case 2: 3993919788
161    case 3: 2567524794
162    case 4: 124634137
163    case 5: 1886057615
164    case 6: 3915621685
165    case 7: 2657392035
166    case 8: 249268274
167    case 9: 2044508324
168    case 10: 3772115230
169    case 11: 2547177864
170    case 12: 162941995
171    case 13: 2125561021
172    case 14: 3887607047
173    case 15: 2428444049
174    case 16: 498536548
175    case 17: 1789927666
176    case 18: 4089016648
177    case 19: 2227061214
178    case 20: 450548861
179    case 21: 1843258603
180    case 22: 4107580753
181    case 23: 2211677639
182    case 24: 325883990
183    case 25: 1684777152
184    case 26: 4251122042
185    case 27: 2321926636
186    case 28: 335633487
187    case 29: 1661365465
188    case 30: 4195302755
189    case 31: 2366115317
190    case 32: 997073096
191    case 33: 1281953886
192    case 34: 3579855332
193    case 35: 2724688242
194    case 36: 1006888145
195    case 37: 1258607687
196    case 38: 3524101629
197    case 39: 2768942443
198    case 40: 901097722
199    case 41: 1119000684
200    case 42: 3686517206
201    case 43: 2898065728
202    case 44: 853044451
203    case 45: 1172266101
204    case 46: 3705015759
205    case 47: 2882616665
206    case 48: 651767980
207    case 49: 1373503546
208    case 50: 3369554304
209    case 51: 3218104598
210    case 52: 565507253
211    case 53: 1454621731
212    case 54: 3485111705
213    case 55: 3099436303
214    case 56: 671266974
215    case 57: 1594198024
216    case 58: 3322730930
217    case 59: 2970347812
218    case 60: 795835527
219    case 61: 1483230225
220    case 62: 3244367275
221    case 63: 3060149565
222    case 64: 1994146192
223    case 65: 31158534
224    case 66: 2563907772
225    case 67: 4023717930
226    case 68: 1907459465
227    case 69: 112637215
228    case 70: 2680153253
229    case 71: 3904427059
230    case 72: 2013776290
231    case 73: 251722036
232    case 74: 2517215374
233    case 75: 3775830040
234    case 76: 2137656763
235    case 77: 141376813
236    case 78: 2439277719
237    case 79: 3865271297
238    case 80: 1802195444
239    case 81: 476864866
240    case 82: 2238001368
241    case 83: 4066508878
242    case 84: 1812370925
243    case 85: 453092731
244    case 86: 2181625025
245    case 87: 4111451223
246    case 88: 1706088902
247    case 89: 314042704
248    case 90: 2344532202
249    case 91: 4240017532
250    case 92: 1658658271
251    case 93: 366619977
252    case 94: 2362670323
253    case 95: 4224994405
254    case 96: 1303535960
255    case 97: 984961486
256    case 98: 2747007092
257    case 99: 3569037538
258    case 100: 1256170817
259    case 101: 1037604311
260    case 102: 2765210733
261    case 103: 3554079995
262    case 104: 1131014506
263    case 105: 879679996
264    case 106: 2909243462
265    case 107: 3663771856
266    case 108: 1141124467
267    case 109: 855842277
268    case 110: 2852801631
269    case 111: 3708648649
270    case 112: 1342533948
271    case 113: 654459306
272    case 114: 3188396048
273    case 115: 3373015174
274    case 116: 1466479909
275    case 117: 544179635
276    case 118: 3110523913
277    case 119: 3462522015
278    case 120: 1591671054
279    case 121: 702138776
280    case 122: 2966460450
281    case 123: 3352799412
282    case 124: 1504918807
283    case 125: 783551873
284    case 126: 3082640443
285    case 127: 3233442989
286    case 128: 3988292384
287    case 129: 2596254646
288    case 130: 62317068
289    case 131: 1957810842
290    case 132: 3939845945
291    case 133: 2647816111
292    case 134: 81470997
293    case 135: 1943803523
294    case 136: 3814918930
295    case 137: 2489596804
296    case 138: 225274430
297    case 139: 2053790376
298    case 140: 3826175755
299    case 141: 2466906013
300    case 142: 167816743
301    case 143: 2097651377
302    case 144: 4027552580
303    case 145: 2265490386
304    case 146: 503444072
305    case 147: 1762050814
306    case 148: 4150417245
307    case 149: 2154129355
308    case 150: 426522225
309    case 151: 1852507879
310    case 152: 4275313526
311    case 153: 2312317920
312    case 154: 282753626
313    case 155: 1742555852
314    case 156: 4189708143
315    case 157: 2394877945
316    case 158: 397917763
317    case 159: 1622183637
318    case 160: 3604390888
319    case 161: 2714866558
320    case 162: 953729732
321    case 163: 1340076626
322    case 164: 3518719985
323    case 165: 2797360999
324    case 166: 1068828381
325    case 167: 1219638859
326    case 168: 3624741850
327    case 169: 2936675148
328    case 170: 906185462
329    case 171: 1090812512
330    case 172: 3747672003
331    case 173: 2825379669
332    case 174: 829329135
333    case 175: 1181335161
334    case 176: 3412177804
335    case 177: 3160834842
336    case 178: 628085408
337    case 179: 1382605366
338    case 180: 3423369109
339    case 181: 3138078467
340    case 182: 570562233
341    case 183: 1426400815
342    case 184: 3317316542
343    case 185: 2998733608
344    case 186: 733239954
345    case 187: 1555261956
346    case 188: 3268935591
347    case 189: 3050360625
348    case 190: 752459403
349    case 191: 1541320221
350    case 192: 2607071920
351    case 193: 3965973030
352    case 194: 1969922972
353    case 195: 40735498
354    case 196: 2617837225
355    case 197: 3943577151
356    case 198: 1913087877
357    case 199: 83908371
358    case 200: 2512341634
359    case 201: 3803740692
360    case 202: 2075208622
361    case 203: 213261112
362    case 204: 2463272603
363    case 205: 3855990285
364    case 206: 2094854071
365    case 207: 198958881
366    case 208: 2262029012
367    case 209: 4057260610
368    case 210: 1759359992
369    case 211: 534414190
370    case 212: 2176718541
371    case 213: 4139329115
372    case 214: 1873836001
373    case 215: 414664567
374    case 216: 2282248934
375    case 217: 4279200368
376    case 218: 1711684554
377    case 219: 285281116
378    case 220: 2405801727
379    case 221: 4167216745
380    case 222: 1634467795
381    case 223: 376229701
382    case 224: 2685067896
383    case 225: 3608007406
384    case 226: 1308918612
385    case 227: 956543938
386    case 228: 2808555105
387    case 229: 3495958263
388    case 230: 1231636301
389    case 231: 1047427035
390    case 232: 2932959818
391    case 233: 3654703836
392    case 234: 1088359270
393    case 235: 936918000
394    case 236: 2847714899
395    case 237: 3736837829
396    case 238: 1202900863
397    case 239: 817233897
398    case 240: 3183342108
399    case 241: 3401237130
400    case 242: 1404277552
401    case 243: 615818150
402    case 244: 3134207493
403    case 245: 3453421203
404    case 246: 1423857449
405    case 247: 601450431
406    case 248: 3009837614
407    case 249: 3294710456
408    case 250: 1567103746
409    case 251: 711928724
410    case 252: 3020668471
411    case 253: 3272380065
412    case 254: 1510334235
413    case _: 755167117
414
415def crc.byte(h: U32, +c: U32) -> U32:
416  +x = h
417  (crc.tab(((x .^. c) .&. 255 : U32)) .^. (x >> 8n) : U32)
418
419def crc.word(h: U32, +w: U32) -> U32:
420  crc.byte(crc.byte(crc.byte(crc.byte(h, (w .&. 255 : U32)), ((w >> 8n) .&. 255 : U32)), ((w >> 16n) .&. 255 : U32)), (w >> 24n : U32))
421
422def crc.fin(r: Bytes.Bytes & U32) -> Bytes.Bytes & U32:
423  (b, +h) = r
424  (b, (h .^. 4294967295 : U32))
425
426def crc32(b: Bytes.Bytes) -> Bytes.Bytes & U32:
427  crc.fin(bytewise(~U32, ~crc.byte, ~crc.word, b, 4294967295))
428
429# Adler-32 (RFC 1950 §9). The state is the checksum itself: s2 << 16 | s1.
430# A word takes mod 65521 once, after four bytes; no sum reaches 2^32 first.
431def adler.of(+s1: U32, +s2: U32) -> U32:
432  (((s2 % 65521) << 16n) .|. (s1 % 65521) : U32)
433
434def adler.byte(h0: U32, +c: U32) -> U32:
435  +h = h0
436  +s1 = ((h .&. 65535) + c : U32)
437  adler.of(s1, ((h >> 16n) + s1 : U32))
438
439def adler.word(h0: U32, +w: U32) -> U32:
440  +h = h0
441  +a0 = ((h .&. 65535) + (w .&. 255) : U32)
442  +a1 = (a0 + ((w >> 8n) .&. 255) : U32)
443  +a2 = (a1 + ((w >> 16n) .&. 255) : U32)
444  +a3 = (a2 + (w >> 24n) : U32)
445  adler.of(a3, ((h >> 16n) + a0 + a1 + a2 + a3 : U32))
446
447def adler32(b: Bytes.Bytes) -> Bytes.Bytes & U32:
448  bytewise(~U32, ~adler.byte, ~adler.word, b, 1)
449
450# xxHash32 (XXH32, xxHash spec 0.1.1).
451type V4 is Data:
452  V4{a: U32, b: U32, c: U32, d: U32}
453
454def x32.round(+acc: U32, +x: U32) -> U32:
455  (rotl32((acc + x * 2246822519 : U32), 13n, 19n) * 2654435761 : U32)
456
457# The four accumulators take lanes in turn: v1 eats this word and moves to the back.
458def x32.lane(v: V4, +w: U32) -> V4:
459  match v:
460    case V4{a, b, c, d}:
461      V4{b, c, d, x32.round(a, w)}
462
463def x32.merge(r: Array<U32> & V4) -> Array<U32> & U32:
464  (buf, V4{+a, +b, +c, +d}) = r
465  (buf, (rotl32(a, 1n, 31n) + rotl32(b, 7n, 25n) + rotl32(c, 12n, 20n) + rotl32(d, 18n, 14n) : U32))
466
467def x32.word(h: U32, +w: U32) -> U32:
468  (rotl32((h + w * 3266489917 : U32), 17n, 15n) * 668265263 : U32)
469
470def x32.byte(h: U32, +c: U32) -> U32:
471  (rotl32((h + c * 374761393 : U32), 11n, 21n) * 2654435761 : U32)
472
473def x32.aval.c(+h: U32) -> U32:
474  (h .^. (h >> 16n) : U32)
475
476def x32.aval.b(+h: U32) -> U32:
477  x32.aval.c(((h .^. (h >> 13n)) * 3266489917 : U32))
478
479def x32.aval(+h: U32) -> U32:
480  x32.aval.b(((h .^. (h >> 15n)) * 2246822519 : U32))
481
482def x32.tail(+len: U32, +h: U32, r: Array<U32> & U32) -> Bytes.Bytes & U32:
483  (a, +w) = r
484  (Bytes.Bytes{len, a}, x32.aval(tail(~U32, ~x32.byte, U32.to_nat((len .&. 3 : U32)), w, h)))
485
486def x32.rest(+len: U32, r: Array<U32> & U32) -> Bytes.Bytes & U32:
487  (a, +h) = r
488  x32.tail(len, h, Array.get(U32, a, (len >> 2n : U32)))
489
490def x32.mid(+len: U32, r: Array<U32> & U32) -> Bytes.Bytes & U32:
491  (a, +h) = r
492  x32.rest(len, wfrom(~U32, ~x32.word, U32.to_nat(((len .&. 15) >> 2n : U32)), a, ((len >> 4n) << 2n : U32), (h + len : U32)))
493
494def x32.start(big: Bool, +len: U32, +seed: U32, a: Array<U32>) -> Array<U32> & U32:
495  match big:
496    case True{}:
497      x32.merge(wfrom(~V4, ~x32.lane, U32.to_nat(((len >> 4n) << 2n : U32)), a, 0, V4{(seed + 606290984 : U32), (seed + 2246822519 : U32), seed, (seed + 1640531535 : U32)}))
498    case False{}:
499      (a, (seed + 374761393 : U32))
500
501def xxh32(b: Bytes.Bytes, +seed: U32) -> Bytes.Bytes & U32:
502  Bytes.Bytes{+len, buf} = b
503  x32.mid(len, x32.start(U32.is_le(16, len), len, seed, buf))
504
505# xxHash64 (XXH64, xxHash spec 0.1.1).
506type V64 is Data:
507  V64{a: W64, b: W64, c: W64, d: W64}
508
509def x64.P1() -> W64:
510  W64{2654435761, 2246822535}
511
512def x64.P2() -> W64:
513  W64{3266489917, 668265295}
514
515def x64.P3() -> W64:
516  W64{374761393, 2654435833}
517
518def x64.P4() -> W64:
519  W64{2246822519, 3266489955}
520
521def x64.P5() -> W64:
522  W64{668265263, 374761413}
523
524def x64.round(acc: W64, x: W64) -> W64:
525  W64.mul(W64.rotl(31n, 1n, W64.add(acc, W64.mul(x, x64.P2()))), x64.P1())
526
527def x64.lane(v: V64, +m: W64) -> V64:
528  match v:
529    case V64{a, b, c, d}:
530      V64{b, c, d, x64.round(a, m)}
531
532def x64.fold(h: W64, v: W64) -> W64:
533  W64.add(W64.mul(W64.xor(h, x64.round(W64{0, 0}, v)), x64.P1()), x64.P4())
534
535def x64.merge(r: Array<U32> & V64) -> Array<U32> & W64:
536  (buf, V64{+a, +b, +c, +d}) = r
537  +h = W64.add(W64.add(W64.rotl(1n, 31n, a), W64.rotl(7n, 25n, b)), W64.add(W64.rotl(12n, 20n, c), W64.rotl(18n, 14n, d)))
538  (buf, x64.fold(x64.fold(x64.fold(x64.fold(h, a), b), c), d))
539
540def x64.word(h: W64, +m: W64) -> W64:
541  W64.add(W64.mul(W64.rotl(27n, 5n, W64.xor(h, x64.round(W64{0, 0}, m))), x64.P1()), x64.P4())
542
543def x64.byte(h: W64, +c: U32) -> W64:
544  W64.mul(W64.rotl(11n, 21n, W64.xor(h, W64.mul(W64{0, c}, x64.P5()))), x64.P1())
545
546def x64.four(h: W64, +w: U32) -> W64:
547  W64.add(W64.mul(W64.rotl(23n, 9n, W64.xor(h, W64.mul(W64{0, w}, x64.P1()))), x64.P2()), x64.P3())
548
549def x64.aval.c(h: W64) -> W64:
550  match h:
551    case W64{+hi, lo}:
552      W64{hi, (lo .^. hi : U32)}
553
554def x64.aval.b(+h: W64) -> W64:
555  x64.aval.c(W64.mul(W64.xor(h, W64.shr(29n, 3n, h)), x64.P3()))
556
557def x64.aval(h: W64) -> W64:
558  match h:
559    case W64{+hi, lo}:
560      x64.aval.b(W64.mul(W64{hi, (lo .^. (hi >> 1n) : U32)}, x64.P2()))
561
562# The last len % 8 bytes: a 4-byte lane when there is one, then single bytes.
563def x64.bytes(four: Bool, +k: Nat, h: W64, m: W64) -> W64:
564  match four:
565    case True{}:
566      match m:
567        case W64{+hi, +lo}:
568          tail(~W64, ~x64.byte, k, hi, x64.four(h, lo))
569    case False{}:
570      match m:
571        case W64{hi, +lo}:
572          tail(~W64, ~x64.byte, k, lo, h)
573
574def x64.tail(+len: U32, +h: W64, r: Array<U32> & W64) -> Bytes.Bytes & W64:
575  (a, m) = r
576  (Bytes.Bytes{len, a}, x64.aval(x64.bytes(U32.is_ne((len .&. 4 : U32), 0), U32.to_nat((len .&. 3 : U32)), h, m)))
577
578def x64.rest(+len: U32, r: Array<U32> & W64) -> Bytes.Bytes & W64:
579  (a, h) = r
580  x64.tail(len, h, get2(a, ((len >> 3n) << 1n : U32)))
581
582def x64.mid(+len: U32, r: Array<U32> & W64) -> Bytes.Bytes & W64:
583  (a, h) = r
584  x64.rest(len, dfrom(~W64, ~x64.word, U32.to_nat(((len .&. 31) >> 3n : U32)), a, ((len >> 5n) << 3n : U32), W64.add(h, W64{0, len})))
585
586def x64.start(big: Bool, +len: U32, +seed: W64, a: Array<U32>) -> Array<U32> & W64:
587  match big:
588    case True{}:
589      x64.merge(dfrom(~V64, ~x64.lane, U32.to_nat(((len >> 5n) << 2n : U32)), a, 0, V64{W64.add(seed, W64{1625958382, 2915087830}), W64.add(seed, x64.P2()), seed, W64.add(seed, W64{1640531534, 2048144761})}))
590    case False{}:
591      (a, W64.add(seed, x64.P5()))
592
593def xxh64(b: Bytes.Bytes, +seed: W64) -> Bytes.Bytes & W64:
594  Bytes.Bytes{+len, buf} = b
595  x64.mid(len, x64.start(U32.is_le(32, len), len, seed, buf))
596
597# SipHash-1-3 (Aumasson and Bernstein, 2012): one round per block, three to finish.
598type S4 is Data:
599  S4{v0: W64, v1: W64, v2: W64, v3: W64}
600
601def sip.round(s: S4) -> S4:
602  S4{v0, +v1, v2, +v3} = s
603  +a0 = W64.add(v0, v1)
604  +b1 = W64.xor(W64.rotl(13n, 19n, v1), a0)
605  +a2 = W64.add(v2, v3)
606  +b3 = W64.xor(W64.rotl(16n, 16n, v3), a2)
607  +c0 = W64.add(W64.swap(a0), b3)
608  +c2 = W64.add(a2, b1)
609  S4{c0, W64.xor(W64.rotl(17n, 15n, b1), c2), W64.swap(c2), W64.xor(W64.rotl(21n, 11n, b3), c0)}
610
611def sip.post(+m: W64, s: S4) -> S4:
612  S4{v0, v1, v2, v3} = s
613  S4{W64.xor(v0, m), v1, v2, v3}
614
615def sip.block(s: S4, +m: W64) -> S4:
616  S4{v0, v1, v2, v3} = s
617  sip.post(m, sip.round(S4{v0, v1, v2, W64.xor(v3, m)}))
618
619def sip.fin(s: S4) -> W64:
620  S4{v0, v1, v2, v3} = s
621  W64.xor(W64.xor(v0, v1), W64.xor(v2, v3))
622
623def sip.end(s: S4) -> W64:
624  S4{v0, v1, v2, v3} = s
625  sip.fin(sip.round(sip.round(sip.round(S4{v0, v1, W64.xor(v2, W64{0, 255}), v3}))))
626
627# The low n bytes of w, for n <= 4, so bytes past len never reach the hash.
628def sip.keep(+w: U32, +n: U32) -> U32:
629  Bool.pick(U32, U32.is_lt(n, 4), (w .&. (U32.shln(1, U32.to_nat((n * 8 : U32))) - 1) : U32), w)
630
631# The last block: the len % 8 bytes left, and len mod 256 in the top byte.
632def sip.last(+len: U32, r: Array<U32> & W64, s: S4) -> Bytes.Bytes & W64:
633  (a, W64{+hi, +lo}) = r
634  +k = (len .&. 7 : U32)
635  +top = ((len .&. 255) << 24n : U32)
636  (Bytes.Bytes{len, a}, sip.end(sip.block(s, W64{(sip.keep(hi, Bool.pick(U32, U32.is_lt(4, k), (k - 4 : U32), 0)) .|. top : U32), sip.keep(lo, U32.min(k, 4))})))
637
638def sip.rest(+len: U32, r: Array<U32> & S4) -> Bytes.Bytes & W64:
639  (a, s) = r
640  sip.last(len, get2(a, ((len >> 3n) << 1n : U32)), s)
641
642def siphash13(b: Bytes.Bytes, +k0: W64, +k1: W64) -> Bytes.Bytes & W64:
643  Bytes.Bytes{+len, buf} = b
644  sip.rest(len, dfrom(~S4, ~sip.block, U32.to_nat((len >> 3n : U32)), buf, 0, S4{W64.xor(k0, W64{1936682341, 1886610805}), W64.xor(k1, W64{1685025377, 1852075885}), W64.xor(k0, W64{1819895653, 1852142177}), W64.xor(k1, W64{1952801890, 2037671283})}))
645
646# The same hashes over a byte string. ponytail: copies to Bytes; hash short strings in place if the copy shows up in profiles.
647def v32(r: Bytes.Bytes & U32) -> U32:
648  (b, h) = r
649  h
650
651def v64(r: Bytes.Bytes & W64) -> W64:
652  (b, h) = r
653  h
654
655def fnv1a32.str(+s: String) -> U32:
656  v32(fnv1a32(Bytes.from_string(s)))
657
658def fnv1a64.str(+s: String) -> W64:
659  v64(fnv1a64(Bytes.from_string(s)))
660
661def xxh32.str(+s: String, +seed: U32) -> U32:
662  v32(xxh32(Bytes.from_string(s), seed))
663
664def xxh64.str(+s: String, +seed: W64) -> W64:
665  v64(xxh64(Bytes.from_string(s), seed))
666
667def siphash13.str(+s: String, +k0: W64, +k1: W64) -> W64:
668  v64(siphash13(Bytes.from_string(s), k0, k1))
669
670def crc32.str(+s: String) -> U32:
671  v32(crc32(Bytes.from_string(s)))
672
673def adler32.str(+s: String) -> U32:
674  v32(adler32(Bytes.from_string(s)))