hash.bend checks
raw source on the hub · import bend-kit-hash@0.3.2.0/hash.bend as Hash
Non-cryptographic hashes over Bytes: FNV-1a, xxHash, SipHash-1-3 and SipHash-2-4, CRC-32, and Adler-32. Source: https://github.com/paymog/bend-kit/tree/main/hash
2 imports
import Base import bend-kit-bytes@0.3.3.0/bytes.bend as Bytes
Types
type W64 source · line 10 · raw
Data
A 64-bit value as two halves. Base has no native U64, and int's U64 is a bit list.
W64@hi:U32 -> @lo:U32 -> W64
type V4 source · line 2287 · raw
Data
xxHash32 (XXH32, xxHash spec 0.1.1).
V4@a:U32 -> @b:U32 -> @c:U32 -> @d:U32 -> V4
type V64 source · line 2342 · raw
Data
xxHash64 (XXH64, xxHash spec 0.1.1).
V64@a:W64 -> @b:W64 -> @c:W64 -> @d:W64 -> V64
type S4 source · line 2434 · raw
Data
SipHash-c-d (Aumasson and Bernstein, 2012).
S4@v0:W64 -> @v1:W64 -> @v2:W64 -> @v3:W64 -> S4
Definitions
def W64.of source · line 13 · raw
@+hi:U32 -> @+lo:U32 -> W64
def W64.xor source · line 16 · raw
@a:W64 -> @b:W64 -> W64
def W64.add.of source · line 21 · raw
@+ah:U32 -> @+bh:U32 -> @+al:U32 -> @+lo:U32 -> W64
def W64.add source · line 24 · raw
@a:W64 -> @b:W64 -> W64
def mul32.of source · line 30 · raw
@+p00:U32 -> @+p01:U32 -> @+p10:U32 -> @+p11:U32 -> W64
The full 64-bit product of two U32, from four 16-bit partial products.
def mul32 source · line 34 · raw
@+x:U32 -> @+y:U32 -> W64
def W64.mul.of source · line 41 · raw
@+cross:U32 -> @p:W64 -> W64
def W64.mul source · line 45 · raw
@a:W64 -> @b:W64 -> W64
def W64.rotl source · line 51 · raw
@+k:Nat -> @+j:Nat -> @a:W64 -> W64
Rotate left by k, for 0 < k < 32; j is 32 - k.
def W64.swap source · line 56 · raw
@a:W64 -> W64
def W64.shr source · line 62 · raw
@+k:Nat -> @+j:Nat -> @a:W64 -> W64
Shift right by k, for 0 < k < 32; j is 32 - k.
def W64.hi source · line 67 · raw
@a:W64 -> U32
def W64.lo source · line 72 · raw
@a:W64 -> U32
def rotl32 source · line 77 · raw
@+x:U32 -> @+k:Nat -> @+j:Nat -> U32
def get2.of source · line 95 · raw
@+lo:U32 -> @r:Pair(Array<U32>, U32) -> Pair(Array<U32>, W64)
Words i and i + 1 as one little-endian 64-bit lane.
def get2.lo source · line 99 · raw
@+i:U32 -> @r:Pair(Array<U32>, U32) -> Pair(Array<U32>, W64)
def get2 source · line 103 · raw
@a:Array<U32> -> @+i:U32 -> Pair(Array<U32>, W64)
def fnv32.byte source · line 142 · raw
@h:U32 -> @+c:U32 -> U32
FNV-1a (draft-eastlake-fnv), 32 and 64 bits.
def fnv32.word source · line 145 · raw
@h:U32 -> @+w:U32 -> U32
def fnv1a32 source · line 148 · raw
@b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, U32)
def fnv64.byte source · line 151 · raw
@h:W64 -> @+c:U32 -> W64
def fnv64.word source · line 156 · raw
@h:W64 -> @+w:U32 -> W64
def fnv1a64 source · line 159 · raw
@b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, W64)
def crc.tab source · line 164 · raw
@+i:U32 -> U32
CRC-32 (ISO-HDLC, as in gzip and zlib): reflected, polynomial 0xEDB88320. The 256-entry tables as matches, so no call rebuilds them. crc.tabK steps a byte K places further.
def crc.tab1 source · line 423 · raw
@+i:U32 -> U32
def crc.tab2 source · line 682 · raw
@+i:U32 -> U32
def crc.tab3 source · line 941 · raw
@+i:U32 -> U32
def crc.tab4 source · line 1200 · raw
@+i:U32 -> U32
def crc.tab5 source · line 1459 · raw
@+i:U32 -> U32
def crc.tab6 source · line 1718 · raw
@+i:U32 -> U32
def crc.tab7 source · line 1977 · raw
@+i:U32 -> U32
def crc.byte source · line 2236 · raw
@h:U32 -> @+c:U32 -> U32
def crc.word source · line 2240 · raw
@h:U32 -> @+w:U32 -> U32
def crc.lane source · line 2244 · raw
@h:U32 -> @+m:W64 -> U32
def crc.rest source · line 2253 · raw
@+len:U32 -> @r:Pair(Array<U32>, U32) -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, U32)
After the lanes: one word when len % 8 >= 4, then the bytes.
def crc.fin source · line 2257 · raw
@r:Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, U32) -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, U32)
def crc32 source · line 2261 · raw
@b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, U32)
def adler.of source · line 2267 · raw
@+s1:U32 -> @+s2:U32 -> U32
Adler-32 (RFC 1950 §9). The state is the checksum itself: s2 << 16 | s1. A word takes mod 65521 once, after four bytes; no sum reaches 2^32 first.
def adler.byte source · line 2270 · raw
@h0:U32 -> @+c:U32 -> U32
def adler.word source · line 2275 · raw
@h0:U32 -> @+w:U32 -> U32
def adler32 source · line 2283 · raw
@b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, U32)
def x32.round source · line 2290 · raw
@+acc:U32 -> @+x:U32 -> U32
def x32.lane source · line 2294 · raw
@v:V4 -> @+w:U32 -> V4
The four accumulators take lanes in turn: v1 eats this word and moves to the back.
def x32.merge source · line 2299 · raw
@r:Pair(Array<U32>, V4) -> Pair(Array<U32>, U32)
def x32.word source · line 2303 · raw
@h:U32 -> @+w:U32 -> U32
def x32.byte source · line 2306 · raw
@h:U32 -> @+c:U32 -> U32
def x32.aval.c source · line 2309 · raw
@+h:U32 -> U32
def x32.aval.b source · line 2312 · raw
@+h:U32 -> U32
def x32.aval source · line 2315 · raw
@+h:U32 -> U32
def x32.tail source · line 2318 · raw
@+len:U32 -> @+h:U32 -> @r:Pair(Array<U32>, U32) -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, U32)
def x32.rest source · line 2322 · raw
@+len:U32 -> @r:Pair(Array<U32>, U32) -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, U32)
def x32.mid source · line 2326 · raw
@+len:U32 -> @r:Pair(Array<U32>, U32) -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, U32)
def x32.start source · line 2330 · raw
@big:Bool -> @+len:U32 -> @+seed:U32 -> @a:Array<U32> -> Pair(Array<U32>, U32)
def xxh32 source · line 2337 · raw
@b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> @+seed:U32 -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, U32)
def x64.P1 source · line 2345 · raw
W64
def x64.P2 source · line 2348 · raw
W64
def x64.P3 source · line 2351 · raw
W64
def x64.P4 source · line 2354 · raw
W64
def x64.P5 source · line 2357 · raw
W64
def x64.round source · line 2360 · raw
@acc:W64 -> @x:W64 -> W64
def x64.lane source · line 2363 · raw
@v:V64 -> @+m:W64 -> V64
def x64.fold source · line 2368 · raw
@h:W64 -> @v:W64 -> W64
def x64.merge source · line 2371 · raw
@r:Pair(Array<U32>, V64) -> Pair(Array<U32>, W64)
def x64.word source · line 2376 · raw
@h:W64 -> @+m:W64 -> W64
def x64.byte source · line 2379 · raw
@h:W64 -> @+c:U32 -> W64
def x64.four source · line 2382 · raw
@h:W64 -> @+w:U32 -> W64
def x64.aval.c source · line 2385 · raw
@h:W64 -> W64
def x64.aval.b source · line 2390 · raw
@+h:W64 -> W64
def x64.aval source · line 2393 · raw
@h:W64 -> W64
def x64.bytes source · line 2399 · raw
@four:Bool -> @+k:Nat -> @h:W64 -> @m:W64 -> W64
The last len % 8 bytes: a 4-byte lane when there is one, then single bytes.
def x64.tail source · line 2410 · raw
@+len:U32 -> @+h:W64 -> @r:Pair(Array<U32>, W64) -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, W64)
def x64.rest source · line 2414 · raw
@+len:U32 -> @r:Pair(Array<U32>, W64) -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, W64)
def x64.mid source · line 2418 · raw
@+len:U32 -> @r:Pair(Array<U32>, W64) -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, W64)
def x64.start source · line 2422 · raw
@big:Bool -> @+len:U32 -> @+seed:W64 -> @a:Array<U32> -> Pair(Array<U32>, W64)
def xxh64 source · line 2429 · raw
@b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> @+seed:W64 -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, W64)
def sip.round source · line 2437 · raw
@s:S4 -> S4
def sip.rounds source · line 2447 · raw
@n:Nat -> @s:S4 -> S4
def sip.post source · line 2454 · raw
@+m:W64 -> @s:S4 -> S4
def sip.fin source · line 2462 · raw
@s:S4 -> W64
def sip.keep source · line 2471 · raw
@+w:U32 -> @+n:U32 -> U32
The low n bytes of w, for n <= 4, so bytes past len never reach the hash.
def siphash13 source · line 2489 · raw
@b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> @+k0:W64 -> @+k1:W64 -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, W64)
def siphash24 source · line 2492 · raw
@b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> @+k0:W64 -> @+k1:W64 -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, W64)
def v32 source · line 2496 · raw
@r:Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, U32) -> U32
The same hashes over a byte string. ponytail: copies to Bytes; hash short strings in place if the copy shows up in profiles.
def v64 source · line 2500 · raw
@r:Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, W64) -> W64
def fnv1a32.str source · line 2504 · raw
@+s:String -> U32
def fnv1a64.str source · line 2507 · raw
@+s:String -> W64
def xxh32.str source · line 2510 · raw
@+s:String -> @+seed:U32 -> U32
def xxh64.str source · line 2513 · raw
@+s:String -> @+seed:W64 -> W64
def siphash13.str source · line 2516 · raw
@+s:String -> @+k0:W64 -> @+k1:W64 -> W64
def siphash24.str source · line 2519 · raw
@+s:String -> @+k0:W64 -> @+k1:W64 -> W64
def crc32.str source · line 2522 · raw
@+s:String -> U32
def adler32.str source · line 2525 · raw
@+s:String -> U32
Templates
template wfold source · line 81 · raw
@-T:Type -> @-f:(@_:T -> @+c:U32 -> T) -> @n:Nat -> @r:Pair(Array<U32>, U32) -> @+i:U32 -> @acc:T -> Pair(Array<U32>, T)
Folds f over n words from word i on. r holds the array and word i.
template wfrom source · line 91 · raw
@-T:Type -> @-f:(@_:T -> @+c:U32 -> T) -> @n:Nat -> @a:Array<U32> -> @+i:U32 -> @acc:T -> Pair(Array<U32>, T)
template dfold source · line 107 · raw
@-T:Type -> @-f:(@_:T -> @+m:W64 -> T) -> @n:Nat -> @r:Pair(Array<U32>, W64) -> @+i:U32 -> @acc:T -> Pair(Array<U32>, T)
Folds f over n 64-bit lanes from word i on. r holds the array and the lane at i.
template dfrom source · line 117 · raw
@-T:Type -> @-f:(@_:T -> @+m:W64 -> T) -> @n:Nat -> @a:Array<U32> -> @+i:U32 -> @acc:T -> Pair(Array<U32>, T)
template tail source · line 121 · raw
@-T:Type -> @-f:(@_:T -> @+c:U32 -> T) -> @k:Nat -> @+w:U32 -> @acc:T -> T
Folds f over the low k bytes of w, lowest first.
template bytewise.last source · line 128 · raw
@-T:Type -> @-byte:(@_:T -> @+c:U32 -> T) -> @+len:U32 -> @acc:T -> @r:Pair(Array<U32>, U32) -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, T)
template bytewise.rest source · line 132 · raw
@-T:Type -> @-byte:(@_:T -> @+c:U32 -> T) -> @+len:U32 -> @r:Pair(Array<U32>, T) -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, T)
template bytewise source · line 137 · raw
@-T:Type -> @-byte:(@_:T -> @+c:U32 -> T) -> @-word:(@_:T -> @+c:U32 -> T) -> @b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> @acc:T -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, T)
A hash that eats one byte at a time: word takes the four bytes of a word, byte one.
template sip.block source · line 2458 · raw
@-c:Nat -> @s:S4 -> @+m:W64 -> S4
template sip.end source · line 2466 · raw
@-d:Nat -> @s:S4 -> W64
template sip.last source · line 2475 · raw
@-c:Nat -> @-d:Nat -> @+len:U32 -> @r:Pair(Array<U32>, W64) -> @s:S4 -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, W64)
The last block: the len % 8 bytes left, and len mod 256 in the top byte.
template sip.rest source · line 2481 · raw
@-c:Nat -> @-d:Nat -> @+len:U32 -> @r:Pair(Array<U32>, S4) -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, W64)
template sip.hash source · line 2485 · raw
@-c:Nat -> @-d:Nat -> @b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> @+k0:W64 -> @+k1:W64 -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, W64)