hash.bend checks
raw source on the hub · import bend-kit-hash@0.3.0.1/hash.bend as Hash
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
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 Crc source · line 173 · raw
Type
The table and the running register.
Crc@t:Array<U32> -> @h:U32 -> Crc
type V4 source · line 216 · raw
Data
xxHash32 (XXH32, xxHash spec 0.1.1).
V4@a:U32 -> @b:U32 -> @c:U32 -> @d:U32 -> V4
type V64 source · line 271 · raw
Data
xxHash64 (XXH64, xxHash spec 0.1.1).
V64@a:W64 -> @b:W64 -> @c:W64 -> @d:W64 -> V64
type S4 source · line 363 · raw
Data
SipHash-1-3 (Aumasson and Bernstein, 2012): one round per block, three to finish.
S4@v0:W64 -> @v1:W64 -> @v2:W64 -> @v3:W64 -> S4
Definitions
def W64.xor source · line 13 · raw
@a:W64 -> @b:W64 -> W64
def W64.add.of source · line 18 · raw
@+ah:U32 -> @+bh:U32 -> @+al:U32 -> @+lo:U32 -> W64
def W64.add source · line 21 · raw
@a:W64 -> @b:W64 -> W64
def mul32.of source · line 27 · 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 31 · raw
@+x:U32 -> @+y:U32 -> W64
def W64.mul.of source · line 38 · raw
@+cross:U32 -> @p:W64 -> W64
def W64.mul source · line 42 · raw
@a:W64 -> @b:W64 -> W64
def W64.rotl source · line 48 · 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 53 · raw
@a:W64 -> W64
def W64.shr source · line 59 · raw
@+k:Nat -> @+j:Nat -> @a:W64 -> W64
Shift right by k, for 0 < k < 32; j is 32 - k.
def W64.lo source · line 64 · raw
@a:W64 -> U32
def rotl32 source · line 69 · raw
@+x:U32 -> @+k:Nat -> @+j:Nat -> U32
def get2.of source · line 87 · 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 91 · raw
@+i:U32 -> @r:Pair(Array<U32>, U32) -> Pair(Array<U32>, W64)
def get2 source · line 95 · raw
@a:Array<U32> -> @+i:U32 -> Pair(Array<U32>, W64)
def fnv32.byte source · line 134 · raw
@h:U32 -> @+c:U32 -> U32
FNV-1a (draft-eastlake-fnv), 32 and 64 bits.
def fnv32.word source · line 137 · raw
@h:U32 -> @+w:U32 -> U32
def fnv1a32 source · line 140 · raw
@b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, U32)
def fnv64.byte source · line 143 · raw
@h:W64 -> @+c:U32 -> W64
def fnv64.word source · line 148 · raw
@h:W64 -> @+w:U32 -> W64
def fnv1a64 source · line 151 · raw
@b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, W64)
def crc.bit source · line 155 · raw
@+c:U32 -> U32
CRC-32 (ISO-HDLC, as in gzip and zlib): reflected, polynomial 0xEDB88320.
def crc.bits source · line 158 · raw
@k:Nat -> @+c:U32 -> U32
def crc.fill source · line 165 · raw
@k:Nat -> @+i:U32 -> @a:Array<U32> -> Array<U32>
def crc.mix source · line 176 · raw
@+c:U32 -> @r:Pair(Array<U32>, U32) -> Crc
def crc.byte source · line 180 · raw
@st:Crc -> @+c:U32 -> Crc
def crc.word source · line 184 · raw
@st:Crc -> @+w:U32 -> Crc
def crc.fin source · line 187 · raw
@r:Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, Crc) -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, U32)
def crc32 source · line 191 · raw
@b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, U32)
def adler.of source · line 196 · 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 199 · raw
@h0:U32 -> @+c:U32 -> U32
def adler.word source · line 204 · raw
@h0:U32 -> @+w:U32 -> U32
def adler32 source · line 212 · raw
@b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, U32)
def x32.round source · line 219 · raw
@+acc:U32 -> @+x:U32 -> U32
def x32.lane source · line 223 · 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 228 · raw
@r:Pair(Array<U32>, V4) -> Pair(Array<U32>, U32)
def x32.word source · line 232 · raw
@h:U32 -> @+w:U32 -> U32
def x32.byte source · line 235 · raw
@h:U32 -> @+c:U32 -> U32
def x32.aval.c source · line 238 · raw
@+h:U32 -> U32
def x32.aval.b source · line 241 · raw
@+h:U32 -> U32
def x32.aval source · line 244 · raw
@+h:U32 -> U32
def x32.tail source · line 247 · raw
@+len:U32 -> @+h:U32 -> @r:Pair(Array<U32>, U32) -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, U32)
def x32.rest source · line 251 · raw
@+len:U32 -> @r:Pair(Array<U32>, U32) -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, U32)
def x32.mid source · line 255 · raw
@+len:U32 -> @r:Pair(Array<U32>, U32) -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, U32)
def x32.start source · line 259 · raw
@big:Bool -> @+len:U32 -> @+seed:U32 -> @a:Array<U32> -> Pair(Array<U32>, U32)
def xxh32 source · line 266 · raw
@b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> @+seed:U32 -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, U32)
def x64.P1 source · line 274 · raw
W64
def x64.P2 source · line 277 · raw
W64
def x64.P3 source · line 280 · raw
W64
def x64.P4 source · line 283 · raw
W64
def x64.P5 source · line 286 · raw
W64
def x64.round source · line 289 · raw
@acc:W64 -> @x:W64 -> W64
def x64.lane source · line 292 · raw
@v:V64 -> @+m:W64 -> V64
def x64.fold source · line 297 · raw
@h:W64 -> @v:W64 -> W64
def x64.merge source · line 300 · raw
@r:Pair(Array<U32>, V64) -> Pair(Array<U32>, W64)
def x64.word source · line 305 · raw
@h:W64 -> @+m:W64 -> W64
def x64.byte source · line 308 · raw
@h:W64 -> @+c:U32 -> W64
def x64.four source · line 311 · raw
@h:W64 -> @+w:U32 -> W64
def x64.aval.c source · line 314 · raw
@h:W64 -> W64
def x64.aval.b source · line 319 · raw
@+h:W64 -> W64
def x64.aval source · line 322 · raw
@h:W64 -> W64
def x64.bytes source · line 328 · 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 339 · raw
@+len:U32 -> @+h:W64 -> @r:Pair(Array<U32>, W64) -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, W64)
def x64.rest source · line 343 · raw
@+len:U32 -> @r:Pair(Array<U32>, W64) -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, W64)
def x64.mid source · line 347 · raw
@+len:U32 -> @r:Pair(Array<U32>, W64) -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, W64)
def x64.start source · line 351 · raw
@big:Bool -> @+len:U32 -> @+seed:W64 -> @a:Array<U32> -> Pair(Array<U32>, W64)
def xxh64 source · line 358 · raw
@b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> @+seed:W64 -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, W64)
def sip.round source · line 366 · raw
@s:S4 -> S4
def sip.post source · line 376 · raw
@+m:W64 -> @s:S4 -> S4
def sip.block source · line 380 · raw
@s:S4 -> @+m:W64 -> S4
def sip.fin source · line 384 · raw
@s:S4 -> W64
def sip.end source · line 388 · raw
@s:S4 -> W64
def sip.keep source · line 393 · raw
@+w:U32 -> @+n:U32 -> U32
The low n bytes of w, for n <= 4, so bytes past len never reach the hash.
def sip.last source · line 397 · raw
@+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.
def sip.rest source · line 403 · raw
@+len:U32 -> @r:Pair(Array<U32>, S4) -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, W64)
def siphash13 source · line 407 · raw
@b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> @+k0:W64 -> @+k1:W64 -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, W64)
def v32 source · line 412 · 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 416 · raw
@r:Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, W64) -> W64
def fnv1a32.str source · line 420 · raw
@+s:String -> U32
def fnv1a64.str source · line 423 · raw
@+s:String -> W64
def xxh32.str source · line 426 · raw
@+s:String -> @+seed:U32 -> U32
def xxh64.str source · line 429 · raw
@+s:String -> @+seed:W64 -> W64
def siphash13.str source · line 432 · raw
@+s:String -> @+k0:W64 -> @+k1:W64 -> W64
def crc32.str source · line 435 · raw
@+s:String -> U32
def adler32.str source · line 438 · raw
@+s:String -> U32
Templates
template wfold source · line 73 · 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 83 · 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 99 · 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 109 · 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 113 · 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 120 · 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 124 · raw
@-T:Type -> @-byte:(@_:T -> @+c:U32 -> T) -> @+len:U32 -> @r:Pair(Array<U32>, T) -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, T)
template bytewise source · line 129 · 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.