Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

murmur.ts

139 lines4.0 KB

1/*
2 * Implementation of murmur hash based on the Composites polyfill implementation:
3 * https://github.com/tc39/proposal-composites
4 */
5​
6const RANDOM_SEED = randomHash()
7const STRING_MARKER = randomHash()
8const BIG_INT_MARKER = randomHash()
9const NEG_BIG_INT_MARKER = randomHash()
10const SYMBOL_MARKER = randomHash()
11​
12export type Hash = number
13​
14export function randomHash() {
15 return (Math.random() * (2 ** 31 - 1)) >>> 0
17​
18export interface Hasher {
19 update: (val: symbol | string | number | bigint) => void
20 digest: () => number
22​
23// Allocate these once, outside the hot path
24const buf = new ArrayBuffer(8)
25// dv and u8 are 2 different views on the same buffer `buf`
26const dv = new DataView(buf)
27const u8 = new Uint8Array(buf)
28​
29/**
30 * This implementation of Murmur hash uses a random initial seed and random markers.
31 * This means that hashes aren't deterministic across app restarts.
32 * This is intentional in the composites polyfill to be resistent to hash-flooding attacks
33 * where malicious users would precompute lots of different objects whose hashes collide with each other.
34 *
35 * Currently, for ts/db-ivm this is fine because we don't persist client state.
36 * However, when we will introduce persistence we will either need to store the seeds or remove the randomness
37 * to ensure deterministic hashes across app restarts.
38 */
39export class MurmurHashStream implements Hasher {
40 private hash: number = RANDOM_SEED
41 private length = 0
42 private carry = 0
43 private carryBytes = 0
44​
45 private _mix(k1: number): void {
46 k1 = Math.imul(k1, 0xcc9e2d51)
47 k1 = (k1 << 15) | (k1 >>> 17)
48 k1 = Math.imul(k1, 0x1b873593)
49 this.hash ^= k1
50 this.hash = (this.hash << 13) | (this.hash >>> 19)
51 this.hash = Math.imul(this.hash, 5) + 0xe6546b64
52 }
53​
54 writeByte(byte: number): void {
55 this.carry |= (byte & 0xff) << (8 * this.carryBytes)
56 this.carryBytes++
57 this.length++
58​
59 if (this.carryBytes === 4) {
60 this._mix(this.carry >>> 0)
61 this.carry = 0
62 this.carryBytes = 0
63 }
64 }
65​
66 update(chunk: symbol | string | number | bigint): void {
67 switch (typeof chunk) {
68 case `symbol`: {
69 this.update(SYMBOL_MARKER)
70 const description = chunk.description
71 if (!description) {
72 return
73 }
74​
75 for (let i = 0; i < description.length; i++) {
76 const code = description.charCodeAt(i)
77 this.writeByte(code & 0xff)
78 this.writeByte((code >>> 8) & 0xff)
79 }
80 return
81 }
82 case `string`:
83 this.update(STRING_MARKER)
84 for (let i = 0; i < chunk.length; i++) {
85 const code = chunk.charCodeAt(i)
86 this.writeByte(code & 0xff)
87 this.writeByte((code >>> 8) & 0xff)
88 }
89 return
90 case `number`:
91 dv.setFloat64(0, chunk, true) // fixed little-endian
92 this.writeByte(u8[0]!)
93 this.writeByte(u8[1]!)
94 this.writeByte(u8[2]!)
95 this.writeByte(u8[3]!)
96 this.writeByte(u8[4]!)
97 this.writeByte(u8[5]!)
98 this.writeByte(u8[6]!)
99 this.writeByte(u8[7]!)
100 return
101 case `bigint`: {
102 let value = chunk
103 if (value < 0n) {
104 value = -value
105 this.update(NEG_BIG_INT_MARKER)
106 } else {
107 this.update(BIG_INT_MARKER)
108 }
109 while (value > 0n) {
110 this.writeByte(Number(value & 0xffn))
111 value >>= 8n
112 }
113 if (chunk === 0n) this.writeByte(0)
114 return
115 }
116 default:
117 throw new TypeError(`Unsupported input type: ${typeof chunk}`)
118 }
119 }
120​
121 digest(): number {
122 if (this.carryBytes > 0) {
123 let k1 = this.carry >>> 0
124 k1 = Math.imul(k1, 0xcc9e2d51)
125 k1 = (k1 << 15) | (k1 >>> 17)
126 k1 = Math.imul(k1, 0x1b873593)
127 this.hash ^= k1
128 }
129​
130 this.hash ^= this.length
131 this.hash ^= this.hash >>> 16
132 this.hash = Math.imul(this.hash, 0x85ebca6b)
133 this.hash ^= this.hash >>> 13
134 this.hash = Math.imul(this.hash, 0xc2b2ae35)
135 this.hash ^= this.hash >>> 16
136​
137 return this.hash >>> 0
138 }