~/bend-docscommunity

hash.bend checks

raw source on the hub · import bend-kit-hash@0.3.0.2/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.

type V4 source · line 451 · raw

Data

xxHash32 (XXH32, xxHash spec 0.1.1).

type V64 source · line 506 · raw

Data

xxHash64 (XXH64, xxHash spec 0.1.1).

type S4 source · line 598 · raw

Data

SipHash-1-3 (Aumasson and Bernstein, 2012): one round per block, three to finish.

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.tab source · line 156 · raw

@+i:U32 -> U32

CRC-32 (ISO-HDLC, as in gzip and zlib): reflected, polynomial 0xEDB88320. The 256-entry table as a match, so no call rebuilds it.

def crc.byte source · line 415 · raw

@h:U32 -> @+c:U32 -> U32

def crc.word source · line 419 · raw

@h:U32 -> @+w:U32 -> U32

def crc.fin source · line 422 · raw

@r:Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, U32) -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, U32)

def crc32 source · line 426 · raw

@b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, U32)

def adler.of source · line 431 · 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 434 · raw

@h0:U32 -> @+c:U32 -> U32

def adler.word source · line 439 · raw

@h0:U32 -> @+w:U32 -> U32

def adler32 source · line 447 · raw

@b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, U32)

def x32.round source · line 454 · raw

@+acc:U32 -> @+x:U32 -> U32

def x32.lane source · line 458 · 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 463 · raw

@r:Pair(Array<U32>, V4) -> Pair(Array<U32>, U32)

def x32.word source · line 467 · raw

@h:U32 -> @+w:U32 -> U32

def x32.byte source · line 470 · raw

@h:U32 -> @+c:U32 -> U32

def x32.aval.c source · line 473 · raw

@+h:U32 -> U32

def x32.aval.b source · line 476 · raw

@+h:U32 -> U32

def x32.aval source · line 479 · raw

@+h:U32 -> U32

def x32.tail source · line 482 · raw

@+len:U32 -> @+h:U32 -> @r:Pair(Array<U32>, U32) -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, U32)

def x32.rest source · line 486 · raw

@+len:U32 -> @r:Pair(Array<U32>, U32) -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, U32)

def x32.mid source · line 490 · raw

@+len:U32 -> @r:Pair(Array<U32>, U32) -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, U32)

def x32.start source · line 494 · raw

@big:Bool -> @+len:U32 -> @+seed:U32 -> @a:Array<U32> -> Pair(Array<U32>, U32)

def xxh32 source · line 501 · raw

@b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> @+seed:U32 -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, U32)

def x64.P1 source · line 509 · raw

W64

def x64.P2 source · line 512 · raw

W64

def x64.P3 source · line 515 · raw

W64

def x64.P4 source · line 518 · raw

W64

def x64.P5 source · line 521 · raw

W64

def x64.round source · line 524 · raw

@acc:W64 -> @x:W64 -> W64

def x64.lane source · line 527 · raw

@v:V64 -> @+m:W64 -> V64

def x64.fold source · line 532 · raw

@h:W64 -> @v:W64 -> W64

def x64.merge source · line 535 · raw

@r:Pair(Array<U32>, V64) -> Pair(Array<U32>, W64)

def x64.word source · line 540 · raw

@h:W64 -> @+m:W64 -> W64

def x64.byte source · line 543 · raw

@h:W64 -> @+c:U32 -> W64

def x64.four source · line 546 · raw

@h:W64 -> @+w:U32 -> W64

def x64.aval.c source · line 549 · raw

@h:W64 -> W64

def x64.aval.b source · line 554 · raw

@+h:W64 -> W64

def x64.aval source · line 557 · raw

@h:W64 -> W64

def x64.bytes source · line 563 · 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 574 · raw

@+len:U32 -> @+h:W64 -> @r:Pair(Array<U32>, W64) -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, W64)

def x64.rest source · line 578 · raw

@+len:U32 -> @r:Pair(Array<U32>, W64) -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, W64)

def x64.mid source · line 582 · raw

@+len:U32 -> @r:Pair(Array<U32>, W64) -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, W64)

def x64.start source · line 586 · raw

@big:Bool -> @+len:U32 -> @+seed:W64 -> @a:Array<U32> -> Pair(Array<U32>, W64)

def xxh64 source · line 593 · raw

@b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> @+seed:W64 -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, W64)

def sip.round source · line 601 · raw

@s:S4 -> S4

def sip.post source · line 611 · raw

@+m:W64 -> @s:S4 -> S4

def sip.block source · line 615 · raw

@s:S4 -> @+m:W64 -> S4

def sip.fin source · line 619 · raw

@s:S4 -> W64

def sip.end source · line 623 · raw

@s:S4 -> W64

def sip.keep source · line 628 · 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 632 · 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 638 · raw

@+len:U32 -> @r:Pair(Array<U32>, S4) -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, W64)

def siphash13 source · line 642 · raw

@b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> @+k0:W64 -> @+k1:W64 -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, W64)

def v32 source · line 647 · 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 651 · raw

@r:Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, W64) -> W64

def fnv1a32.str source · line 655 · raw

@+s:String -> U32

def fnv1a64.str source · line 658 · raw

@+s:String -> W64

def xxh32.str source · line 661 · raw

@+s:String -> @+seed:U32 -> U32

def xxh64.str source · line 664 · raw

@+s:String -> @+seed:W64 -> W64

def siphash13.str source · line 667 · raw

@+s:String -> @+k0:W64 -> @+k1:W64 -> W64

def crc32.str source · line 670 · raw

@+s:String -> U32

def adler32.str source · line 673 · 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.