Module: Omnizip::Algorithms::Zstandard::XXHash64
- Defined in:
- lib/omnizip/algorithms/zstandard/xxhash.rb
Overview
XXHash64 (https://github.com/Cyan4973/xxHash), single-shot.
Zstandard frame checksums are the low 32 bits of XXHash64 of the decoded content with seed 0 (the zstd C reference truncates XXH64_digest to a U32; RFC 8878's "xxh64" wording agrees).
Constant Summary collapse
- MASK64 =
0xFFFFFFFFFFFFFFFF- MASK32 =
0xFFFFFFFF- PRIME64_1 =
0x9E3779B185EBCA87- PRIME64_2 =
0xC2B2AE3D27D4EB4F- PRIME64_3 =
0x165667B19E3779F9- PRIME64_4 =
0x85EBCA77C2B2AE63- PRIME64_5 =
0x27D4EB2F165667C5
Class Method Summary collapse
-
.digest(data) ⇒ Integer
Digest
datawith seed 0. - .digest_seeded(data, seed) ⇒ Object
-
.frame_checksum(data) ⇒ Integer
Zstandard frame checksum: low 32 bits of XXHash64.
- .merge_round64(acc, value) ⇒ Object
- .read_u32(data, offset) ⇒ Object
- .read_u64(data, offset) ⇒ Object
- .rotl64(value, count) ⇒ Object
- .round64(acc, input) ⇒ Object
Class Method Details
.digest(data) ⇒ Integer
Digest data with seed 0.
47 48 49 |
# File 'lib/omnizip/algorithms/zstandard/xxhash.rb', line 47 def digest(data) digest_seeded(data, 0) end |
.digest_seeded(data, seed) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/omnizip/algorithms/zstandard/xxhash.rb', line 51 def digest_seeded(data, seed) len = data.bytesize hash = 0 tail_start = 0 if len >= 32 v1 = (seed + PRIME64_1 + PRIME64_2) & MASK64 v2 = (seed + PRIME64_2) & MASK64 v3 = seed & MASK64 v4 = (seed - PRIME64_1) & MASK64 i = 0 while i + 32 <= len v1 = round64(v1, read_u64(data, i)) v2 = round64(v2, read_u64(data, i + 8)) v3 = round64(v3, read_u64(data, i + 16)) v4 = round64(v4, read_u64(data, i + 24)) i += 32 end hash = rotl64(v1, 1) hash = (hash + rotl64(v2, 7)) & MASK64 hash = (hash + rotl64(v3, 12)) & MASK64 hash = (hash + rotl64(v4, 18)) & MASK64 hash = merge_round64(hash, v1) hash = merge_round64(hash, v2) hash = merge_round64(hash, v3) hash = merge_round64(hash, v4) tail_start = len - (len % 32) else hash = (seed + PRIME64_5) & MASK64 end hash = (hash + len) & MASK64 i = tail_start while i + 8 <= len k1 = round64(0, read_u64(data, i)) hash ^= k1 hash = ((rotl64(hash, 27) * PRIME64_1) & MASK64) + PRIME64_4 hash &= MASK64 i += 8 end if i + 4 <= len hash ^= (read_u32(data, i) * PRIME64_1) & MASK64 hash = ((rotl64(hash, 23) * PRIME64_2) & MASK64) + PRIME64_3 hash &= MASK64 i += 4 end while i < len hash ^= (data.getbyte(i) * PRIME64_5) & MASK64 hash = (rotl64(hash, 11) * PRIME64_1) & MASK64 i += 1 end hash ^= hash >> 33 hash = (hash * PRIME64_2) & MASK64 hash ^= hash >> 29 hash = (hash * PRIME64_3) & MASK64 hash ^= hash >> 32 hash end |
.frame_checksum(data) ⇒ Integer
Zstandard frame checksum: low 32 bits of XXHash64.
119 120 121 |
# File 'lib/omnizip/algorithms/zstandard/xxhash.rb', line 119 def frame_checksum(data) digest(data) & MASK32 end |
.merge_round64(acc, value) ⇒ Object
151 152 153 154 155 |
# File 'lib/omnizip/algorithms/zstandard/xxhash.rb', line 151 def merge_round64(acc, value) value = round64(0, value) acc ^= value (((acc * PRIME64_1) & MASK64) + PRIME64_4) & MASK64 end |
.read_u32(data, offset) ⇒ Object
134 135 136 137 138 139 |
# File 'lib/omnizip/algorithms/zstandard/xxhash.rb', line 134 def read_u32(data, offset) data.getbyte(offset) | (data.getbyte(offset + 1) << 8) | (data.getbyte(offset + 2) << 16) | (data.getbyte(offset + 3) << 24) end |
.read_u64(data, offset) ⇒ Object
123 124 125 126 127 128 129 130 131 132 |
# File 'lib/omnizip/algorithms/zstandard/xxhash.rb', line 123 def read_u64(data, offset) data.getbyte(offset) | (data.getbyte(offset + 1) << 8) | (data.getbyte(offset + 2) << 16) | (data.getbyte(offset + 3) << 24) | (data.getbyte(offset + 4) << 32) | (data.getbyte(offset + 5) << 40) | (data.getbyte(offset + 6) << 48) | (data.getbyte(offset + 7) << 56) end |
.rotl64(value, count) ⇒ Object
141 142 143 |
# File 'lib/omnizip/algorithms/zstandard/xxhash.rb', line 141 def rotl64(value, count) ((value << count) | (value >> (64 - count))) & MASK64 end |