rapidhash-js 3.0.0 → 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -0
- package/lib/common.d.mts +7 -0
- package/lib/common.d.ts +7 -0
- package/lib/common.js +167 -0
- package/lib/common.js.map +1 -0
- package/lib/common.mjs +96 -0
- package/lib/common.mjs.map +1 -0
- package/lib/common_v2.js +26 -0
- package/lib/common_v2.js.map +1 -0
- package/lib/common_v2.mjs +15 -0
- package/lib/common_v2.mjs.map +1 -0
- package/lib/index.d.mts +2 -31
- package/lib/index.d.ts +2 -31
- package/lib/index.js +11 -248
- package/lib/index.mjs +1 -219
- package/lib/v1.0/index.d.mts +2 -3
- package/lib/v1.0/index.d.ts +2 -3
- package/lib/v1.0/index.js +96 -199
- package/lib/v1.0/index.js.map +1 -1
- package/lib/v1.0/index.mjs +93 -170
- package/lib/v1.0/index.mjs.map +1 -1
- package/lib/v2.0/index.d.mts +2 -3
- package/lib/v2.0/index.d.ts +2 -3
- package/lib/v2.0/index.js +118 -240
- package/lib/v2.0/index.js.map +1 -1
- package/lib/v2.0/index.mjs +115 -211
- package/lib/v2.0/index.mjs.map +1 -1
- package/lib/v2.2/index.d.mts +2 -3
- package/lib/v2.2/index.d.ts +2 -3
- package/lib/v2.2/index.js +118 -239
- package/lib/v2.2/index.js.map +1 -1
- package/lib/v2.2/index.mjs +115 -210
- package/lib/v2.2/index.mjs.map +1 -1
- package/lib/v3.0/index.d.mts +30 -6
- package/lib/v3.0/index.d.ts +30 -6
- package/lib/v3.0/index.js +11 -440
- package/lib/v3.0/index.mjs +1 -405
- package/lib/v3.0.js +407 -0
- package/lib/v3.0.js.map +1 -0
- package/lib/v3.0.mjs +354 -0
- package/lib/v3.0.mjs.map +1 -0
- package/package.json +5 -6
- package/lib/common-FK5Aq1u9.d.mts +0 -7
- package/lib/common-FK5Aq1u9.d.ts +0 -7
- package/lib/index.js.map +0 -1
- package/lib/index.mjs.map +0 -1
- package/lib/v3.0/index.js.map +0 -1
- package/lib/v3.0/index.mjs.map +0 -1
package/lib/v1.0/index.js
CHANGED
|
@@ -1,209 +1,106 @@
|
|
|
1
|
-
"
|
|
2
|
-
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/v1.0/index.ts
|
|
21
|
-
var v1_exports = {};
|
|
22
|
-
__export(v1_exports, {
|
|
23
|
-
rapidhash: () => rapidhash_v1,
|
|
24
|
-
rapidhash_fast: () => rapidhash_v1_fast,
|
|
25
|
-
rapidhash_protected: () => rapidhash_v1_protected
|
|
26
|
-
});
|
|
27
|
-
module.exports = __toCommonJS(v1_exports);
|
|
28
|
-
|
|
29
|
-
// src/common.ts
|
|
30
|
-
function rapid_mix_fast(a, b) {
|
|
31
|
-
const m = a * b;
|
|
32
|
-
return BigInt.asUintN(64, m) ^ m >> 64n;
|
|
33
|
-
}
|
|
34
|
-
function rapidhash_epilogue_fast(a, b, secretA, secretB, len) {
|
|
35
|
-
const m0 = a * b;
|
|
36
|
-
const m1 = (BigInt.asUintN(64, m0) ^ secretA ^ len) * (m0 >> 64n ^ secretB);
|
|
37
|
-
return BigInt.asUintN(64, m1) ^ m1 >> 64n;
|
|
38
|
-
}
|
|
39
|
-
function rapid_mix_protected(a, b) {
|
|
40
|
-
const m = a * b;
|
|
41
|
-
return BigInt.asUintN(64, m) ^ m >> 64n ^ a ^ b;
|
|
42
|
-
}
|
|
43
|
-
function rapidhash_epilogue_protected(a0, b0, secretA, secretB, len) {
|
|
44
|
-
const m0 = a0 * b0;
|
|
45
|
-
const a1 = BigInt.asUintN(64, m0) ^ secretA ^ len ^ a0;
|
|
46
|
-
const b1 = m0 >> 64n ^ secretB ^ b0;
|
|
47
|
-
const m1 = a1 * b1;
|
|
48
|
-
return BigInt.asUintN(64, m1) ^ m1 >> 64n ^ a1 ^ b1;
|
|
49
|
-
}
|
|
50
|
-
var read32x2Buffer = new Uint8Array(8);
|
|
51
|
-
var read32x2View = new DataView(read32x2Buffer.buffer);
|
|
52
|
-
function rapid_read32x2(buf, offset1, offset2) {
|
|
53
|
-
read32x2View.setUint32(4, buf.getUint32(offset1, true), true);
|
|
54
|
-
read32x2View.setUint32(0, buf.getUint32(offset2, true), true);
|
|
55
|
-
return read32x2View.getBigUint64(0, true);
|
|
56
|
-
}
|
|
57
|
-
var read32Buffer = new Uint8Array(8);
|
|
58
|
-
var read32View = new DataView(read32Buffer.buffer);
|
|
59
|
-
function rapid_read64(buf, offset) {
|
|
60
|
-
return buf.getBigUint64(offset, true);
|
|
61
|
-
}
|
|
62
|
-
var readSmallBuffer = new Uint8Array(8);
|
|
63
|
-
var readSmallView = new DataView(readSmallBuffer.buffer);
|
|
64
|
-
function rapid_readSmall(buf, offset, k) {
|
|
65
|
-
readSmallBuffer[7] = buf.getUint8(offset);
|
|
66
|
-
readSmallBuffer[4] = buf.getUint8(offset + (k >> 1));
|
|
67
|
-
readSmallBuffer[0] = buf.getUint8(offset + k - 1);
|
|
68
|
-
return readSmallView.getBigUint64(0, true);
|
|
69
|
-
}
|
|
70
|
-
var readBytesBuffer = new Uint8Array(8);
|
|
71
|
-
var readBytesView = new DataView(readBytesBuffer.buffer);
|
|
72
|
-
var textEncoder = new TextEncoder();
|
|
73
|
-
var utf8BufferSize = 2048;
|
|
74
|
-
var stringLengthThresholdToUseBuffer = utf8BufferSize / 3;
|
|
75
|
-
var utf8Buffer = new Uint8Array(utf8BufferSize);
|
|
76
|
-
function toDataView(message) {
|
|
77
|
-
if (typeof message === "string") {
|
|
78
|
-
if (message.length <= stringLengthThresholdToUseBuffer) {
|
|
79
|
-
const { written } = textEncoder.encodeInto(message, utf8Buffer);
|
|
80
|
-
return new DataView(utf8Buffer.buffer, 0, written);
|
|
81
|
-
}
|
|
82
|
-
const utf8bytes = textEncoder.encode(message);
|
|
83
|
-
return new DataView(utf8bytes.buffer, utf8bytes.byteOffset, utf8bytes.byteLength);
|
|
84
|
-
}
|
|
85
|
-
if (message instanceof Uint8Array) {
|
|
86
|
-
return new DataView(message.buffer, message.byteOffset, message.byteLength);
|
|
87
|
-
}
|
|
88
|
-
return message;
|
|
89
|
-
}
|
|
90
|
-
var defaultRapidMumBehaviour = "fast";
|
|
91
|
-
var rapidMumImplementations = {
|
|
92
|
-
fast: {
|
|
93
|
-
rapid_mix: rapid_mix_fast,
|
|
94
|
-
rapidhash_epilogue: rapidhash_epilogue_fast
|
|
95
|
-
},
|
|
96
|
-
protected: {
|
|
97
|
-
rapid_mix: rapid_mix_protected,
|
|
98
|
-
rapidhash_epilogue: rapidhash_epilogue_protected
|
|
99
|
-
}
|
|
100
|
-
};
|
|
101
|
-
function isBigUint64(value) {
|
|
102
|
-
return value >= 0n && value <= 0xffffffffffffffffn;
|
|
103
|
-
}
|
|
104
|
-
function validateOptions(options, defaultSeed) {
|
|
105
|
-
const result = {
|
|
106
|
-
seed: defaultSeed,
|
|
107
|
-
rapidMumBehaviour: defaultRapidMumBehaviour
|
|
108
|
-
};
|
|
109
|
-
if (options === void 0) {
|
|
110
|
-
return result;
|
|
111
|
-
}
|
|
112
|
-
if (options.seed !== void 0) {
|
|
113
|
-
if (!isBigUint64(options.seed)) {
|
|
114
|
-
throw new Error(`seed must be a 64-bit unsigned bigint value: ${options.seed}`);
|
|
115
|
-
}
|
|
116
|
-
result.seed = options.seed;
|
|
117
|
-
}
|
|
118
|
-
if (options.rapidMumBehaviour !== void 0) {
|
|
119
|
-
result.rapidMumBehaviour = options.rapidMumBehaviour;
|
|
120
|
-
}
|
|
121
|
-
return result;
|
|
122
|
-
}
|
|
123
|
-
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_common = require("../common.js");
|
|
124
3
|
// src/v1.0/rapidhash_v1.0.ts
|
|
125
|
-
|
|
126
|
-
|
|
4
|
+
const RAPID_SEED = 13679853920966426665n;
|
|
5
|
+
const rapid_secret = [
|
|
6
|
+
3257665815644502181n,
|
|
7
|
+
10067880064238660809n,
|
|
8
|
+
5418857496715711651n
|
|
9
|
+
];
|
|
127
10
|
function rapidhash_v1_internal(key, seed, secret, rapid_mix, rapidhash_epilogue) {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
b ^= seed;
|
|
182
|
-
return rapidhash_epilogue(a, b, secret[0], secret[1], lenBI);
|
|
11
|
+
const len = key.byteLength;
|
|
12
|
+
const lenBI = BigInt(key.byteLength);
|
|
13
|
+
seed ^= rapid_mix(seed ^ secret[0], secret[1]) ^ lenBI;
|
|
14
|
+
let a;
|
|
15
|
+
let b;
|
|
16
|
+
if (len <= 16) {
|
|
17
|
+
if (len >= 4) {
|
|
18
|
+
const last = len - 4;
|
|
19
|
+
a = require_common.rapid_read32x2(key, 0, last);
|
|
20
|
+
const delta = (len & 24) >> (len >> 3);
|
|
21
|
+
b = require_common.rapid_read32x2(key, delta, last - delta);
|
|
22
|
+
} else if (len > 0) {
|
|
23
|
+
a = require_common.rapid_readSmall(key, 0, len);
|
|
24
|
+
b = 0n;
|
|
25
|
+
} else {
|
|
26
|
+
a = 0n;
|
|
27
|
+
b = 0n;
|
|
28
|
+
}
|
|
29
|
+
} else {
|
|
30
|
+
let i = len;
|
|
31
|
+
let p = 0;
|
|
32
|
+
if (i > 48) {
|
|
33
|
+
let see1 = seed;
|
|
34
|
+
let see2 = seed;
|
|
35
|
+
while (i >= 96) {
|
|
36
|
+
seed = rapid_mix(require_common.rapid_read64(key, p) ^ secret[0], require_common.rapid_read64(key, p + 8) ^ seed);
|
|
37
|
+
see1 = rapid_mix(require_common.rapid_read64(key, p + 16) ^ secret[1], require_common.rapid_read64(key, p + 24) ^ see1);
|
|
38
|
+
see2 = rapid_mix(require_common.rapid_read64(key, p + 32) ^ secret[2], require_common.rapid_read64(key, p + 40) ^ see2);
|
|
39
|
+
seed = rapid_mix(require_common.rapid_read64(key, p + 48) ^ secret[0], require_common.rapid_read64(key, p + 56) ^ seed);
|
|
40
|
+
see1 = rapid_mix(require_common.rapid_read64(key, p + 64) ^ secret[1], require_common.rapid_read64(key, p + 72) ^ see1);
|
|
41
|
+
see2 = rapid_mix(require_common.rapid_read64(key, p + 80) ^ secret[2], require_common.rapid_read64(key, p + 88) ^ see2);
|
|
42
|
+
p += 96;
|
|
43
|
+
i -= 96;
|
|
44
|
+
}
|
|
45
|
+
if (i >= 48) {
|
|
46
|
+
seed = rapid_mix(require_common.rapid_read64(key, p) ^ secret[0], require_common.rapid_read64(key, p + 8) ^ seed);
|
|
47
|
+
see1 = rapid_mix(require_common.rapid_read64(key, p + 16) ^ secret[1], require_common.rapid_read64(key, p + 24) ^ see1);
|
|
48
|
+
see2 = rapid_mix(require_common.rapid_read64(key, p + 32) ^ secret[2], require_common.rapid_read64(key, p + 40) ^ see2);
|
|
49
|
+
p += 48;
|
|
50
|
+
i -= 48;
|
|
51
|
+
}
|
|
52
|
+
seed ^= see1 ^ see2;
|
|
53
|
+
}
|
|
54
|
+
if (i > 16) {
|
|
55
|
+
seed = rapid_mix(require_common.rapid_read64(key, p) ^ secret[2], require_common.rapid_read64(key, p + 8) ^ seed ^ secret[1]);
|
|
56
|
+
if (i > 32) seed = rapid_mix(require_common.rapid_read64(key, p + 16) ^ secret[2], require_common.rapid_read64(key, p + 24) ^ seed);
|
|
57
|
+
}
|
|
58
|
+
a = require_common.rapid_read64(key, p + i - 16);
|
|
59
|
+
b = require_common.rapid_read64(key, p + i - 8);
|
|
60
|
+
}
|
|
61
|
+
a ^= secret[1];
|
|
62
|
+
b ^= seed;
|
|
63
|
+
return rapidhash_epilogue(a, b, secret[0], secret[1], lenBI);
|
|
183
64
|
}
|
|
65
|
+
/**
|
|
66
|
+
* Calculate a 64-bit hash value of the given message.
|
|
67
|
+
*
|
|
68
|
+
* @param message {string|Uint8Array|DataView} The message to be hashed.
|
|
69
|
+
* @param options Options for modifying the hash calculation.
|
|
70
|
+
* @param options.seed {bigint} 64-bit unsigned seed value.
|
|
71
|
+
* @param options.rapidMumBehaviour {'fast'|'protected'} Alters behaviour of the rapid_mum function. Defaults to 'fast'.
|
|
72
|
+
*/
|
|
184
73
|
function rapidhash_v1(message, options) {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
74
|
+
const { seed, rapidMumBehaviour } = require_common.validateOptions(options, RAPID_SEED);
|
|
75
|
+
const { rapid_mix, rapidhash_epilogue } = require_common.rapidMumImplementations[rapidMumBehaviour];
|
|
76
|
+
return rapidhash_v1_internal(require_common.toDataView(message), seed, rapid_secret, rapid_mix, rapidhash_epilogue);
|
|
188
77
|
}
|
|
78
|
+
/**
|
|
79
|
+
* Calculate a 64-bit hash value of the given message.
|
|
80
|
+
* This function is equivalent to rapidhash() with options.rapidMumBehaviour set to 'fast'.
|
|
81
|
+
*
|
|
82
|
+
* @param message {string|Uint8Array|DataView} The message to be hashed.
|
|
83
|
+
* @param options Options for modifying the hash calculation.
|
|
84
|
+
* @param options.seed {bigint} 64-bit unsigned seed value.
|
|
85
|
+
*/
|
|
189
86
|
function rapidhash_v1_fast(message, options) {
|
|
190
|
-
|
|
191
|
-
|
|
87
|
+
const { seed } = require_common.validateOptions(options, RAPID_SEED);
|
|
88
|
+
return rapidhash_v1_internal(require_common.toDataView(message), seed, rapid_secret, require_common.rapid_mix_fast, require_common.rapidhash_epilogue_fast);
|
|
192
89
|
}
|
|
90
|
+
/**
|
|
91
|
+
* Calculate a 64-bit hash value of the given message.
|
|
92
|
+
* This function is equivalent to rapidhash() with options.rapidMumBehaviour set to 'protected'.
|
|
93
|
+
*
|
|
94
|
+
* @param message {string|Uint8Array|DataView} The message to be hashed.
|
|
95
|
+
* @param options Options for modifying the hash calculation.
|
|
96
|
+
* @param options.seed {bigint} 64-bit unsigned seed value.
|
|
97
|
+
*/
|
|
193
98
|
function rapidhash_v1_protected(message, options) {
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
toDataView(message),
|
|
197
|
-
seed,
|
|
198
|
-
rapid_secret,
|
|
199
|
-
rapid_mix_protected,
|
|
200
|
-
rapidhash_epilogue_protected
|
|
201
|
-
);
|
|
99
|
+
const { seed } = require_common.validateOptions(options, RAPID_SEED);
|
|
100
|
+
return rapidhash_v1_internal(require_common.toDataView(message), seed, rapid_secret, require_common.rapid_mix_protected, require_common.rapidhash_epilogue_protected);
|
|
202
101
|
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
rapidhash_protected
|
|
208
|
-
});
|
|
102
|
+
exports.rapidhash = rapidhash_v1;
|
|
103
|
+
exports.rapidhash_fast = rapidhash_v1_fast;
|
|
104
|
+
exports.rapidhash_protected = rapidhash_v1_protected;
|
|
105
|
+
|
|
209
106
|
//# sourceMappingURL=index.js.map
|
package/lib/v1.0/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/v1.0/index.ts","../../src/common.ts","../../src/v1.0/rapidhash_v1.0.ts"],"sourcesContent":["import {\n rapidhash_v1 as rapidhash,\n rapidhash_v1_fast as rapidhash_fast,\n rapidhash_v1_protected as rapidhash_protected,\n} from './rapidhash_v1.0';\n\nexport {rapidhash, rapidhash_fast, rapidhash_protected};\n","export type RapidMix = (a: bigint, b: bigint) => bigint;\nexport type RapidhashEpilogue = (a: bigint, b: bigint, secretA: bigint, secretB: bigint, len: bigint) => bigint;\n\nexport function rapid_mix_fast(a: bigint, b: bigint): bigint {\n const m = a * b;\n return BigInt.asUintN(64, m) ^ (m >> 64n);\n}\n\nexport function rapidhash_epilogue_fast(a: bigint, b: bigint, secretA: bigint, secretB: bigint, len: bigint): bigint {\n const m0 = a * b;\n const m1 = (BigInt.asUintN(64, m0) ^ secretA ^ len) * ((m0 >> 64n) ^ secretB);\n return BigInt.asUintN(64, m1) ^ (m1 >> 64n);\n}\n\nexport function rapid_mix_protected(a: bigint, b: bigint): bigint {\n const m = a * b;\n return BigInt.asUintN(64, m) ^ (m >> 64n) ^ a ^ b;\n}\n\nexport function rapidhash_epilogue_protected(\n a0: bigint,\n b0: bigint,\n secretA: bigint,\n secretB: bigint,\n len: bigint,\n): bigint {\n const m0 = a0 * b0;\n const a1 = BigInt.asUintN(64, m0) ^ secretA ^ len ^ a0;\n const b1 = (m0 >> 64n) ^ secretB ^ b0;\n const m1 = a1 * b1;\n return BigInt.asUintN(64, m1) ^ (m1 >> 64n) ^ a1 ^ b1;\n}\n\n// ---\n\nconst read32x2Buffer = new Uint8Array(8);\nconst read32x2View = new DataView(read32x2Buffer.buffer);\n\nexport function rapid_read32x2(buf: DataView, offset1: number, offset2: number): bigint {\n read32x2View.setUint32(4, buf.getUint32(offset1, true), true);\n read32x2View.setUint32(0, buf.getUint32(offset2, true), true);\n return read32x2View.getBigUint64(0, true);\n}\n\nconst read32Buffer = new Uint8Array(8);\nconst read32View = new DataView(read32Buffer.buffer);\n\nexport function rapid_read32(buf: DataView, offset: number): bigint {\n read32View.setUint32(0, buf.getUint32(offset, true), true);\n return read32View.getBigUint64(0, true);\n}\n\nexport function rapid_read64(buf: DataView, offset: number): bigint {\n return buf.getBigUint64(offset, true);\n}\n\nconst readSmallBuffer = new Uint8Array(8);\nconst readSmallView = new DataView(readSmallBuffer.buffer);\n\nexport function rapid_readSmall(buf: DataView, offset: number, k: number): bigint {\n readSmallBuffer[7] = buf.getUint8(offset);\n readSmallBuffer[4] = buf.getUint8(offset + (k >> 1));\n readSmallBuffer[0] = buf.getUint8(offset + k - 1);\n return readSmallView.getBigUint64(0, true);\n}\n\nconst readBytesBuffer = new Uint8Array(8);\nconst readBytesView = new DataView(readBytesBuffer.buffer);\n\nexport function rapid_readBytes(buf: DataView, offset1: number, offset2: number): bigint {\n readBytesBuffer[0] = buf.getUint8(offset2);\n readBytesBuffer[7] = buf.getUint8(offset1);\n return readBytesView.getBigUint64(0, true);\n}\n\n// ---\n\nconst textEncoder = new TextEncoder();\nconst utf8BufferSize = 2048;\nconst stringLengthThresholdToUseBuffer = utf8BufferSize / 3;\nconst utf8Buffer = new Uint8Array(utf8BufferSize);\n\nexport function toDataView(message: string | Uint8Array | DataView): DataView {\n if (typeof message === 'string') {\n if (message.length <= stringLengthThresholdToUseBuffer) {\n const {written} = textEncoder.encodeInto(message, utf8Buffer);\n return new DataView(utf8Buffer.buffer, 0, written);\n }\n\n const utf8bytes = textEncoder.encode(message);\n return new DataView(utf8bytes.buffer, utf8bytes.byteOffset, utf8bytes.byteLength);\n }\n\n if (message instanceof Uint8Array) {\n return new DataView(message.buffer, message.byteOffset, message.byteLength);\n }\n\n return message;\n}\n\n// ---\n\nexport type RapidMumBehaviour = 'fast' | 'protected';\nconst defaultRapidMumBehaviour: RapidMumBehaviour = 'fast';\n\nexport interface RapidhashOptions {\n seed: bigint;\n rapidMumBehaviour: RapidMumBehaviour;\n}\n\nexport const rapidMumImplementations: {\n [behaviour in RapidMumBehaviour]: {\n rapid_mix: RapidMix;\n rapidhash_epilogue: RapidhashEpilogue;\n };\n} = {\n fast: {\n rapid_mix: rapid_mix_fast,\n rapidhash_epilogue: rapidhash_epilogue_fast,\n },\n protected: {\n rapid_mix: rapid_mix_protected,\n rapidhash_epilogue: rapidhash_epilogue_protected,\n },\n};\n\nfunction isBigUint64(value: bigint): boolean {\n return value >= 0n && value <= 0xffff_ffff_ffff_ffffn;\n}\n\nexport function validateOptions(options: Partial<RapidhashOptions> | undefined, defaultSeed: bigint): RapidhashOptions {\n const result: RapidhashOptions = {\n seed: defaultSeed,\n rapidMumBehaviour: defaultRapidMumBehaviour,\n };\n\n if (options === undefined) {\n return result;\n }\n\n if (options.seed !== undefined) {\n if (!isBigUint64(options.seed)) {\n throw new Error(`seed must be a 64-bit unsigned bigint value: ${options.seed}`);\n }\n result.seed = options.seed;\n }\n\n if (options.rapidMumBehaviour !== undefined) {\n result.rapidMumBehaviour = options.rapidMumBehaviour;\n }\n\n return result;\n}\n","import {\n type RapidhashEpilogue,\n type RapidhashOptions,\n type RapidMix,\n rapid_mix_fast,\n rapid_mix_protected,\n rapid_read32x2,\n rapid_read64,\n rapid_readSmall,\n rapidhash_epilogue_fast,\n rapidhash_epilogue_protected,\n rapidMumImplementations,\n toDataView,\n validateOptions,\n} from '../common';\n\ntype RapidSecret = [bigint, bigint, bigint];\n\nconst RAPID_SEED = 0xbdd89aa982704029n;\nconst rapid_secret: RapidSecret = [0x2d358dccaa6c78a5n, 0x8bb84b93962eacc9n, 0x4b33a62ed433d4a3n];\n\n// ---\n\nfunction rapidhash_v1_internal(\n key: DataView,\n seed: bigint,\n secret: RapidSecret,\n rapid_mix: RapidMix,\n rapidhash_epilogue: RapidhashEpilogue,\n): bigint {\n const len = key.byteLength;\n const lenBI = BigInt(key.byteLength);\n\n seed ^= rapid_mix(seed ^ secret[0], secret[1]) ^ lenBI;\n\n let a: bigint;\n let b: bigint;\n\n if (len <= 16) {\n if (len >= 4) {\n const last = len - 4;\n a = rapid_read32x2(key, 0, last);\n const delta = (len & 24) >> (len >> 3);\n b = rapid_read32x2(key, delta, last - delta);\n } else if (len > 0) {\n a = rapid_readSmall(key, 0, len);\n b = 0n;\n } else {\n a = 0n;\n b = 0n;\n }\n } else {\n let i = len;\n let p = 0;\n\n if (i > 48) {\n let see1 = seed;\n let see2 = seed;\n\n while (i >= 96) {\n seed = rapid_mix(rapid_read64(key, p) ^ secret[0], rapid_read64(key, p + 8) ^ seed);\n see1 = rapid_mix(rapid_read64(key, p + 16) ^ secret[1], rapid_read64(key, p + 24) ^ see1);\n see2 = rapid_mix(rapid_read64(key, p + 32) ^ secret[2], rapid_read64(key, p + 40) ^ see2);\n seed = rapid_mix(rapid_read64(key, p + 48) ^ secret[0], rapid_read64(key, p + 56) ^ seed);\n see1 = rapid_mix(rapid_read64(key, p + 64) ^ secret[1], rapid_read64(key, p + 72) ^ see1);\n see2 = rapid_mix(rapid_read64(key, p + 80) ^ secret[2], rapid_read64(key, p + 88) ^ see2);\n p += 96;\n i -= 96;\n }\n if (i >= 48) {\n seed = rapid_mix(rapid_read64(key, p) ^ secret[0], rapid_read64(key, p + 8) ^ seed);\n see1 = rapid_mix(rapid_read64(key, p + 16) ^ secret[1], rapid_read64(key, p + 24) ^ see1);\n see2 = rapid_mix(rapid_read64(key, p + 32) ^ secret[2], rapid_read64(key, p + 40) ^ see2);\n p += 48;\n i -= 48;\n }\n\n seed ^= see1 ^ see2;\n }\n\n if (i > 16) {\n seed = rapid_mix(rapid_read64(key, p) ^ secret[2], rapid_read64(key, p + 8) ^ seed ^ secret[1]);\n if (i > 32) {\n seed = rapid_mix(rapid_read64(key, p + 16) ^ secret[2], rapid_read64(key, p + 24) ^ seed);\n }\n }\n a = rapid_read64(key, p + i - 16);\n b = rapid_read64(key, p + i - 8);\n }\n a ^= secret[1];\n b ^= seed;\n\n return rapidhash_epilogue(a, b, secret[0], secret[1], lenBI);\n}\n\n// ---\n\n/**\n * Calculate a 64-bit hash value of the given message.\n *\n * @param message {string|Uint8Array|DataView} The message to be hashed.\n * @param options Options for modifying the hash calculation.\n * @param options.seed {bigint} 64-bit unsigned seed value.\n * @param options.rapidMumBehaviour {'fast'|'protected'} Alters behaviour of the rapid_mum function. Defaults to 'fast'.\n */\nexport function rapidhash_v1(message: string | Uint8Array | DataView, options?: Partial<RapidhashOptions>): bigint {\n const {seed, rapidMumBehaviour} = validateOptions(options, RAPID_SEED);\n const {rapid_mix, rapidhash_epilogue} = rapidMumImplementations[rapidMumBehaviour];\n\n return rapidhash_v1_internal(toDataView(message), seed, rapid_secret, rapid_mix, rapidhash_epilogue);\n}\n\n/**\n * Calculate a 64-bit hash value of the given message.\n * This function is equivalent to rapidhash() with options.rapidMumBehaviour set to 'fast'.\n *\n * @param message {string|Uint8Array|DataView} The message to be hashed.\n * @param options Options for modifying the hash calculation.\n * @param options.seed {bigint} 64-bit unsigned seed value.\n */\nexport function rapidhash_v1_fast(\n message: string | Uint8Array | DataView,\n options?: Partial<Omit<RapidhashOptions, 'rapidMumBehaviour'>>,\n): bigint {\n const {seed} = validateOptions(options, RAPID_SEED);\n\n return rapidhash_v1_internal(toDataView(message), seed, rapid_secret, rapid_mix_fast, rapidhash_epilogue_fast);\n}\n\n/**\n * Calculate a 64-bit hash value of the given message.\n * This function is equivalent to rapidhash() with options.rapidMumBehaviour set to 'protected'.\n *\n * @param message {string|Uint8Array|DataView} The message to be hashed.\n * @param options Options for modifying the hash calculation.\n * @param options.seed {bigint} 64-bit unsigned seed value.\n */\nexport function rapidhash_v1_protected(\n message: string | Uint8Array | DataView,\n options?: Partial<Omit<RapidhashOptions, 'rapidMumBehaviour'>>,\n): bigint {\n const {seed} = validateOptions(options, RAPID_SEED);\n\n return rapidhash_v1_internal(\n toDataView(message),\n seed,\n rapid_secret,\n rapid_mix_protected,\n rapidhash_epilogue_protected,\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACGO,SAAS,eAAe,GAAW,GAAmB;AAC3D,QAAM,IAAI,IAAI;AACd,SAAO,OAAO,QAAQ,IAAI,CAAC,IAAK,KAAK;AACvC;AAEO,SAAS,wBAAwB,GAAW,GAAW,SAAiB,SAAiB,KAAqB;AACnH,QAAM,KAAK,IAAI;AACf,QAAM,MAAM,OAAO,QAAQ,IAAI,EAAE,IAAI,UAAU,QAAS,MAAM,MAAO;AACrE,SAAO,OAAO,QAAQ,IAAI,EAAE,IAAK,MAAM;AACzC;AAEO,SAAS,oBAAoB,GAAW,GAAmB;AAChE,QAAM,IAAI,IAAI;AACd,SAAO,OAAO,QAAQ,IAAI,CAAC,IAAK,KAAK,MAAO,IAAI;AAClD;AAEO,SAAS,6BACd,IACA,IACA,SACA,SACA,KACQ;AACR,QAAM,KAAK,KAAK;AAChB,QAAM,KAAK,OAAO,QAAQ,IAAI,EAAE,IAAI,UAAU,MAAM;AACpD,QAAM,KAAM,MAAM,MAAO,UAAU;AACnC,QAAM,KAAK,KAAK;AAChB,SAAO,OAAO,QAAQ,IAAI,EAAE,IAAK,MAAM,MAAO,KAAK;AACrD;AAIA,IAAM,iBAAiB,IAAI,WAAW,CAAC;AACvC,IAAM,eAAe,IAAI,SAAS,eAAe,MAAM;AAEhD,SAAS,eAAe,KAAe,SAAiB,SAAyB;AACtF,eAAa,UAAU,GAAG,IAAI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AI;AACV,aAAO,UAAU,aAAa,KAAK,CAAC,IAAI,OAAO,CAAC,GAAG,aAAa,KAAK,IAAI,CAAC,IAAI,OAAO,OAAO,CAAC,CAAC;AAC9F,UAAI,IAAI,IAAI;AACV,eAAO,UAAU,aAAa,KAAK,IAAI,EAAE,IAAI,OAAO,CAAC,GAAG,aAAa,KAAK,IAAI,EAAE,IAAI,IAAI;AAAA,MAC1F;AAAA,IACF;AACA,QAAI,aAAa,KAAK,IAAI,IAAI,EAAE;AAChC,QAAI,aAAa,KAAK,IAAI,IAAI,CAAC;AAAA,EACjC;AACA,OAAK,OAAO,CAAC;AACb,OAAK;AAEL,SAAO,mBAAmB,GAAG,GAAG,OAAO,CAAC,GAAG,OAAO,CAAC,GAAG,KAAK;AAC7D;AAYO,SAAS,aAAa,SAAyC,SAA6C;AACjH,QAAM,EAAC,MAAM,kBAAiB,IAAI,gBAAgB,SAAS,UAAU;AACrE,QAAM,EAAC,WAAW,mBAAkB,IAAI,wBAAwB,iBAAiB;AAEjF,SAAO,sBAAsB,WAAW,OAAO,GAAG,MAAM,cAAc,WAAW,kBAAkB;AACrG;AAUO,SAAS,kBACd,SACA,SACQ;AACR,QAAM,EAAC,KAAI,IAAI,gBAAgB,SAAS,UAAU;AAElD,SAAO,sBAAsB,WAAW,OAAO,GAAG,MAAM,cAAc,gBAAgB,uBAAuB;AAC/G;AAUO,SAAS,uBACd,SACA,SACQ;AACR,QAAM,EAAC,KAAI,IAAI,gBAAgB,SAAS,UAAU;AAElD,SAAO;AAAA,IACL,WAAW,OAAO;AAAA,IAClB;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;","names":[]}
|
|
1
|
+
{"version":3,"file":"index.js","names":["rapid_read32x2","rapid_readSmall","rapid_read64","validateOptions","rapidMumImplementations","toDataView","rapid_mix_fast","rapidhash_epilogue_fast","rapid_mix_protected","rapidhash_epilogue_protected"],"sources":["../../src/v1.0/rapidhash_v1.0.ts"],"sourcesContent":["import {\n type RapidhashEpilogue,\n type RapidhashOptions,\n type RapidMix,\n rapid_mix_fast,\n rapid_mix_protected,\n rapid_read32x2,\n rapid_read64,\n rapid_readSmall,\n rapidhash_epilogue_fast,\n rapidhash_epilogue_protected,\n rapidMumImplementations,\n toDataView,\n validateOptions,\n} from '../common';\n\ntype RapidSecret = [bigint, bigint, bigint];\n\nconst RAPID_SEED = 0xbdd89aa982704029n;\nconst rapid_secret: RapidSecret = [0x2d358dccaa6c78a5n, 0x8bb84b93962eacc9n, 0x4b33a62ed433d4a3n];\n\n// ---\n\nfunction rapidhash_v1_internal(\n key: DataView,\n seed: bigint,\n secret: RapidSecret,\n rapid_mix: RapidMix,\n rapidhash_epilogue: RapidhashEpilogue,\n): bigint {\n const len = key.byteLength;\n const lenBI = BigInt(key.byteLength);\n\n seed ^= rapid_mix(seed ^ secret[0], secret[1]) ^ lenBI;\n\n let a: bigint;\n let b: bigint;\n\n if (len <= 16) {\n if (len >= 4) {\n const last = len - 4;\n a = rapid_read32x2(key, 0, last);\n const delta = (len & 24) >> (len >> 3);\n b = rapid_read32x2(key, delta, last - delta);\n } else if (len > 0) {\n a = rapid_readSmall(key, 0, len);\n b = 0n;\n } else {\n a = 0n;\n b = 0n;\n }\n } else {\n let i = len;\n let p = 0;\n\n if (i > 48) {\n let see1 = seed;\n let see2 = seed;\n\n while (i >= 96) {\n seed = rapid_mix(rapid_read64(key, p) ^ secret[0], rapid_read64(key, p + 8) ^ seed);\n see1 = rapid_mix(rapid_read64(key, p + 16) ^ secret[1], rapid_read64(key, p + 24) ^ see1);\n see2 = rapid_mix(rapid_read64(key, p + 32) ^ secret[2], rapid_read64(key, p + 40) ^ see2);\n seed = rapid_mix(rapid_read64(key, p + 48) ^ secret[0], rapid_read64(key, p + 56) ^ seed);\n see1 = rapid_mix(rapid_read64(key, p + 64) ^ secret[1], rapid_read64(key, p + 72) ^ see1);\n see2 = rapid_mix(rapid_read64(key, p + 80) ^ secret[2], rapid_read64(key, p + 88) ^ see2);\n p += 96;\n i -= 96;\n }\n if (i >= 48) {\n seed = rapid_mix(rapid_read64(key, p) ^ secret[0], rapid_read64(key, p + 8) ^ seed);\n see1 = rapid_mix(rapid_read64(key, p + 16) ^ secret[1], rapid_read64(key, p + 24) ^ see1);\n see2 = rapid_mix(rapid_read64(key, p + 32) ^ secret[2], rapid_read64(key, p + 40) ^ see2);\n p += 48;\n i -= 48;\n }\n\n seed ^= see1 ^ see2;\n }\n\n if (i > 16) {\n seed = rapid_mix(rapid_read64(key, p) ^ secret[2], rapid_read64(key, p + 8) ^ seed ^ secret[1]);\n if (i > 32) {\n seed = rapid_mix(rapid_read64(key, p + 16) ^ secret[2], rapid_read64(key, p + 24) ^ seed);\n }\n }\n a = rapid_read64(key, p + i - 16);\n b = rapid_read64(key, p + i - 8);\n }\n a ^= secret[1];\n b ^= seed;\n\n return rapidhash_epilogue(a, b, secret[0], secret[1], lenBI);\n}\n\n// ---\n\n/**\n * Calculate a 64-bit hash value of the given message.\n *\n * @param message {string|Uint8Array|DataView} The message to be hashed.\n * @param options Options for modifying the hash calculation.\n * @param options.seed {bigint} 64-bit unsigned seed value.\n * @param options.rapidMumBehaviour {'fast'|'protected'} Alters behaviour of the rapid_mum function. Defaults to 'fast'.\n */\nexport function rapidhash_v1(message: string | Uint8Array | DataView, options?: Partial<RapidhashOptions>): bigint {\n const {seed, rapidMumBehaviour} = validateOptions(options, RAPID_SEED);\n const {rapid_mix, rapidhash_epilogue} = rapidMumImplementations[rapidMumBehaviour];\n\n return rapidhash_v1_internal(toDataView(message), seed, rapid_secret, rapid_mix, rapidhash_epilogue);\n}\n\n/**\n * Calculate a 64-bit hash value of the given message.\n * This function is equivalent to rapidhash() with options.rapidMumBehaviour set to 'fast'.\n *\n * @param message {string|Uint8Array|DataView} The message to be hashed.\n * @param options Options for modifying the hash calculation.\n * @param options.seed {bigint} 64-bit unsigned seed value.\n */\nexport function rapidhash_v1_fast(\n message: string | Uint8Array | DataView,\n options?: Partial<Omit<RapidhashOptions, 'rapidMumBehaviour'>>,\n): bigint {\n const {seed} = validateOptions(options, RAPID_SEED);\n\n return rapidhash_v1_internal(toDataView(message), seed, rapid_secret, rapid_mix_fast, rapidhash_epilogue_fast);\n}\n\n/**\n * Calculate a 64-bit hash value of the given message.\n * This function is equivalent to rapidhash() with options.rapidMumBehaviour set to 'protected'.\n *\n * @param message {string|Uint8Array|DataView} The message to be hashed.\n * @param options Options for modifying the hash calculation.\n * @param options.seed {bigint} 64-bit unsigned seed value.\n */\nexport function rapidhash_v1_protected(\n message: string | Uint8Array | DataView,\n options?: Partial<Omit<RapidhashOptions, 'rapidMumBehaviour'>>,\n): bigint {\n const {seed} = validateOptions(options, RAPID_SEED);\n\n return rapidhash_v1_internal(\n toDataView(message),\n seed,\n rapid_secret,\n rapid_mix_protected,\n rapidhash_epilogue_protected,\n );\n}\n"],"mappings":";;;AAkBA,MAAM,aAAa;AACnB,MAAM,eAA4B;CAAC;CAAqB;CAAqB;AAAmB;AAIhG,SAAS,sBACP,KACA,MACA,QACA,WACA,oBACQ;CACR,MAAM,MAAM,IAAI;CAChB,MAAM,QAAQ,OAAO,IAAI,UAAU;CAEnC,QAAQ,UAAU,OAAO,OAAO,IAAI,OAAO,EAAE,IAAI;CAEjD,IAAI;CACJ,IAAI;CAEJ,IAAI,OAAO,IAAI;EACb,IAAI,OAAO,GAAG;GACZ,MAAM,OAAO,MAAM;GACnB,IAAIA,eAAAA,eAAe,KAAK,GAAG,IAAI;GAC/B,MAAM,SAAS,MAAM,QAAQ,OAAO;GACpC,IAAIA,eAAAA,eAAe,KAAK,OAAO,OAAO,KAAK;EAC7C,OAAO,IAAI,MAAM,GAAG;GAClB,IAAIC,eAAAA,gBAAgB,KAAK,GAAG,GAAG;GAC/B,IAAI;EACN,OAAO;GACL,IAAI;GACJ,IAAI;EACN;CACF,OAAO;EACL,IAAI,IAAI;EACR,IAAI,IAAI;EAER,IAAI,IAAI,IAAI;GACV,IAAI,OAAO;GACX,IAAI,OAAO;GAEX,OA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I,CAAC,IAAI,OAAO,OAAO,EAAE;GAC9F,IAAI,IAAI,IACN,OAAO,UAAUA,eAAAA,aAAa,KAAK,IAAI,EAAE,IAAI,OAAO,IAAIA,eAAAA,aAAa,KAAK,IAAI,EAAE,IAAI,IAAI;EAE5F;EACA,IAAIA,eAAAA,aAAa,KAAK,IAAI,IAAI,EAAE;EAChC,IAAIA,eAAAA,aAAa,KAAK,IAAI,IAAI,CAAC;CACjC;CACA,KAAK,OAAO;CACZ,KAAK;CAEL,OAAO,mBAAmB,GAAG,GAAG,OAAO,IAAI,OAAO,IAAI,KAAK;AAC7D;;;;;;;;;AAYA,SAAgB,aAAa,SAAyC,SAA6C;CACjH,MAAM,EAAC,MAAM,sBAAqBC,eAAAA,gBAAgB,SAAS,UAAU;CACrE,MAAM,EAAC,WAAW,uBAAsBC,eAAAA,wBAAwB;CAEhE,OAAO,sBAAsBC,eAAAA,WAAW,OAAO,GAAG,MAAM,cAAc,WAAW,kBAAkB;AACrG;;;;;;;;;AAUA,SAAgB,kBACd,SACA,SACQ;CACR,MAAM,EAAC,SAAQF,eAAAA,gBAAgB,SAAS,UAAU;CAElD,OAAO,sBAAsBE,eAAAA,WAAW,OAAO,GAAG,MAAM,cAAcC,eAAAA,gBAAgBC,eAAAA,uBAAuB;AAC/G;;;;;;;;;AAUA,SAAgB,uBACd,SACA,SACQ;CACR,MAAM,EAAC,SAAQJ,eAAAA,gBAAgB,SAAS,UAAU;CAElD,OAAO,sBACLE,eAAAA,WAAW,OAAO,GAClB,MACA,cACAG,eAAAA,qBACAC,eAAAA,4BACF;AACF"}
|
package/lib/v1.0/index.mjs
CHANGED
|
@@ -1,180 +1,103 @@
|
|
|
1
|
-
|
|
2
|
-
function rapid_mix_fast(a, b) {
|
|
3
|
-
const m = a * b;
|
|
4
|
-
return BigInt.asUintN(64, m) ^ m >> 64n;
|
|
5
|
-
}
|
|
6
|
-
function rapidhash_epilogue_fast(a, b, secretA, secretB, len) {
|
|
7
|
-
const m0 = a * b;
|
|
8
|
-
const m1 = (BigInt.asUintN(64, m0) ^ secretA ^ len) * (m0 >> 64n ^ secretB);
|
|
9
|
-
return BigInt.asUintN(64, m1) ^ m1 >> 64n;
|
|
10
|
-
}
|
|
11
|
-
function rapid_mix_protected(a, b) {
|
|
12
|
-
const m = a * b;
|
|
13
|
-
return BigInt.asUintN(64, m) ^ m >> 64n ^ a ^ b;
|
|
14
|
-
}
|
|
15
|
-
function rapidhash_epilogue_protected(a0, b0, secretA, secretB, len) {
|
|
16
|
-
const m0 = a0 * b0;
|
|
17
|
-
const a1 = BigInt.asUintN(64, m0) ^ secretA ^ len ^ a0;
|
|
18
|
-
const b1 = m0 >> 64n ^ secretB ^ b0;
|
|
19
|
-
const m1 = a1 * b1;
|
|
20
|
-
return BigInt.asUintN(64, m1) ^ m1 >> 64n ^ a1 ^ b1;
|
|
21
|
-
}
|
|
22
|
-
var read32x2Buffer = new Uint8Array(8);
|
|
23
|
-
var read32x2View = new DataView(read32x2Buffer.buffer);
|
|
24
|
-
function rapid_read32x2(buf, offset1, offset2) {
|
|
25
|
-
read32x2View.setUint32(4, buf.getUint32(offset1, true), true);
|
|
26
|
-
read32x2View.setUint32(0, buf.getUint32(offset2, true), true);
|
|
27
|
-
return read32x2View.getBigUint64(0, true);
|
|
28
|
-
}
|
|
29
|
-
var read32Buffer = new Uint8Array(8);
|
|
30
|
-
var read32View = new DataView(read32Buffer.buffer);
|
|
31
|
-
function rapid_read64(buf, offset) {
|
|
32
|
-
return buf.getBigUint64(offset, true);
|
|
33
|
-
}
|
|
34
|
-
var readSmallBuffer = new Uint8Array(8);
|
|
35
|
-
var readSmallView = new DataView(readSmallBuffer.buffer);
|
|
36
|
-
function rapid_readSmall(buf, offset, k) {
|
|
37
|
-
readSmallBuffer[7] = buf.getUint8(offset);
|
|
38
|
-
readSmallBuffer[4] = buf.getUint8(offset + (k >> 1));
|
|
39
|
-
readSmallBuffer[0] = buf.getUint8(offset + k - 1);
|
|
40
|
-
return readSmallView.getBigUint64(0, true);
|
|
41
|
-
}
|
|
42
|
-
var readBytesBuffer = new Uint8Array(8);
|
|
43
|
-
var readBytesView = new DataView(readBytesBuffer.buffer);
|
|
44
|
-
var textEncoder = new TextEncoder();
|
|
45
|
-
var utf8BufferSize = 2048;
|
|
46
|
-
var stringLengthThresholdToUseBuffer = utf8BufferSize / 3;
|
|
47
|
-
var utf8Buffer = new Uint8Array(utf8BufferSize);
|
|
48
|
-
function toDataView(message) {
|
|
49
|
-
if (typeof message === "string") {
|
|
50
|
-
if (message.length <= stringLengthThresholdToUseBuffer) {
|
|
51
|
-
const { written } = textEncoder.encodeInto(message, utf8Buffer);
|
|
52
|
-
return new DataView(utf8Buffer.buffer, 0, written);
|
|
53
|
-
}
|
|
54
|
-
const utf8bytes = textEncoder.encode(message);
|
|
55
|
-
return new DataView(utf8bytes.buffer, utf8bytes.byteOffset, utf8bytes.byteLength);
|
|
56
|
-
}
|
|
57
|
-
if (message instanceof Uint8Array) {
|
|
58
|
-
return new DataView(message.buffer, message.byteOffset, message.byteLength);
|
|
59
|
-
}
|
|
60
|
-
return message;
|
|
61
|
-
}
|
|
62
|
-
var defaultRapidMumBehaviour = "fast";
|
|
63
|
-
var rapidMumImplementations = {
|
|
64
|
-
fast: {
|
|
65
|
-
rapid_mix: rapid_mix_fast,
|
|
66
|
-
rapidhash_epilogue: rapidhash_epilogue_fast
|
|
67
|
-
},
|
|
68
|
-
protected: {
|
|
69
|
-
rapid_mix: rapid_mix_protected,
|
|
70
|
-
rapidhash_epilogue: rapidhash_epilogue_protected
|
|
71
|
-
}
|
|
72
|
-
};
|
|
73
|
-
function isBigUint64(value) {
|
|
74
|
-
return value >= 0n && value <= 0xffffffffffffffffn;
|
|
75
|
-
}
|
|
76
|
-
function validateOptions(options, defaultSeed) {
|
|
77
|
-
const result = {
|
|
78
|
-
seed: defaultSeed,
|
|
79
|
-
rapidMumBehaviour: defaultRapidMumBehaviour
|
|
80
|
-
};
|
|
81
|
-
if (options === void 0) {
|
|
82
|
-
return result;
|
|
83
|
-
}
|
|
84
|
-
if (options.seed !== void 0) {
|
|
85
|
-
if (!isBigUint64(options.seed)) {
|
|
86
|
-
throw new Error(`seed must be a 64-bit unsigned bigint value: ${options.seed}`);
|
|
87
|
-
}
|
|
88
|
-
result.seed = options.seed;
|
|
89
|
-
}
|
|
90
|
-
if (options.rapidMumBehaviour !== void 0) {
|
|
91
|
-
result.rapidMumBehaviour = options.rapidMumBehaviour;
|
|
92
|
-
}
|
|
93
|
-
return result;
|
|
94
|
-
}
|
|
95
|
-
|
|
1
|
+
import { a as rapid_read32x2, c as rapid_readSmall, d as toDataView, f as validateOptions, l as rapidhash_epilogue_fast, n as rapid_mix_fast, o as rapid_read64, r as rapid_mix_protected, t as rapidMumImplementations, u as rapidhash_epilogue_protected } from "../common.mjs";
|
|
96
2
|
// src/v1.0/rapidhash_v1.0.ts
|
|
97
|
-
|
|
98
|
-
|
|
3
|
+
const RAPID_SEED = 13679853920966426665n;
|
|
4
|
+
const rapid_secret = [
|
|
5
|
+
3257665815644502181n,
|
|
6
|
+
10067880064238660809n,
|
|
7
|
+
5418857496715711651n
|
|
8
|
+
];
|
|
99
9
|
function rapidhash_v1_internal(key, seed, secret, rapid_mix, rapidhash_epilogue) {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
b ^= seed;
|
|
154
|
-
return rapidhash_epilogue(a, b, secret[0], secret[1], lenBI);
|
|
10
|
+
const len = key.byteLength;
|
|
11
|
+
const lenBI = BigInt(key.byteLength);
|
|
12
|
+
seed ^= rapid_mix(seed ^ secret[0], secret[1]) ^ lenBI;
|
|
13
|
+
let a;
|
|
14
|
+
let b;
|
|
15
|
+
if (len <= 16) {
|
|
16
|
+
if (len >= 4) {
|
|
17
|
+
const last = len - 4;
|
|
18
|
+
a = rapid_read32x2(key, 0, last);
|
|
19
|
+
const delta = (len & 24) >> (len >> 3);
|
|
20
|
+
b = rapid_read32x2(key, delta, last - delta);
|
|
21
|
+
} else if (len > 0) {
|
|
22
|
+
a = rapid_readSmall(key, 0, len);
|
|
23
|
+
b = 0n;
|
|
24
|
+
} else {
|
|
25
|
+
a = 0n;
|
|
26
|
+
b = 0n;
|
|
27
|
+
}
|
|
28
|
+
} else {
|
|
29
|
+
let i = len;
|
|
30
|
+
let p = 0;
|
|
31
|
+
if (i > 48) {
|
|
32
|
+
let see1 = seed;
|
|
33
|
+
let see2 = seed;
|
|
34
|
+
while (i >= 96) {
|
|
35
|
+
seed = rapid_mix(rapid_read64(key, p) ^ secret[0], rapid_read64(key, p + 8) ^ seed);
|
|
36
|
+
see1 = rapid_mix(rapid_read64(key, p + 16) ^ secret[1], rapid_read64(key, p + 24) ^ see1);
|
|
37
|
+
see2 = rapid_mix(rapid_read64(key, p + 32) ^ secret[2], rapid_read64(key, p + 40) ^ see2);
|
|
38
|
+
seed = rapid_mix(rapid_read64(key, p + 48) ^ secret[0], rapid_read64(key, p + 56) ^ seed);
|
|
39
|
+
see1 = rapid_mix(rapid_read64(key, p + 64) ^ secret[1], rapid_read64(key, p + 72) ^ see1);
|
|
40
|
+
see2 = rapid_mix(rapid_read64(key, p + 80) ^ secret[2], rapid_read64(key, p + 88) ^ see2);
|
|
41
|
+
p += 96;
|
|
42
|
+
i -= 96;
|
|
43
|
+
}
|
|
44
|
+
if (i >= 48) {
|
|
45
|
+
seed = rapid_mix(rapid_read64(key, p) ^ secret[0], rapid_read64(key, p + 8) ^ seed);
|
|
46
|
+
see1 = rapid_mix(rapid_read64(key, p + 16) ^ secret[1], rapid_read64(key, p + 24) ^ see1);
|
|
47
|
+
see2 = rapid_mix(rapid_read64(key, p + 32) ^ secret[2], rapid_read64(key, p + 40) ^ see2);
|
|
48
|
+
p += 48;
|
|
49
|
+
i -= 48;
|
|
50
|
+
}
|
|
51
|
+
seed ^= see1 ^ see2;
|
|
52
|
+
}
|
|
53
|
+
if (i > 16) {
|
|
54
|
+
seed = rapid_mix(rapid_read64(key, p) ^ secret[2], rapid_read64(key, p + 8) ^ seed ^ secret[1]);
|
|
55
|
+
if (i > 32) seed = rapid_mix(rapid_read64(key, p + 16) ^ secret[2], rapid_read64(key, p + 24) ^ seed);
|
|
56
|
+
}
|
|
57
|
+
a = rapid_read64(key, p + i - 16);
|
|
58
|
+
b = rapid_read64(key, p + i - 8);
|
|
59
|
+
}
|
|
60
|
+
a ^= secret[1];
|
|
61
|
+
b ^= seed;
|
|
62
|
+
return rapidhash_epilogue(a, b, secret[0], secret[1], lenBI);
|
|
155
63
|
}
|
|
64
|
+
/**
|
|
65
|
+
* Calculate a 64-bit hash value of the given message.
|
|
66
|
+
*
|
|
67
|
+
* @param message {string|Uint8Array|DataView} The message to be hashed.
|
|
68
|
+
* @param options Options for modifying the hash calculation.
|
|
69
|
+
* @param options.seed {bigint} 64-bit unsigned seed value.
|
|
70
|
+
* @param options.rapidMumBehaviour {'fast'|'protected'} Alters behaviour of the rapid_mum function. Defaults to 'fast'.
|
|
71
|
+
*/
|
|
156
72
|
function rapidhash_v1(message, options) {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
73
|
+
const { seed, rapidMumBehaviour } = validateOptions(options, RAPID_SEED);
|
|
74
|
+
const { rapid_mix, rapidhash_epilogue } = rapidMumImplementations[rapidMumBehaviour];
|
|
75
|
+
return rapidhash_v1_internal(toDataView(message), seed, rapid_secret, rapid_mix, rapidhash_epilogue);
|
|
160
76
|
}
|
|
77
|
+
/**
|
|
78
|
+
* Calculate a 64-bit hash value of the given message.
|
|
79
|
+
* This function is equivalent to rapidhash() with options.rapidMumBehaviour set to 'fast'.
|
|
80
|
+
*
|
|
81
|
+
* @param message {string|Uint8Array|DataView} The message to be hashed.
|
|
82
|
+
* @param options Options for modifying the hash calculation.
|
|
83
|
+
* @param options.seed {bigint} 64-bit unsigned seed value.
|
|
84
|
+
*/
|
|
161
85
|
function rapidhash_v1_fast(message, options) {
|
|
162
|
-
|
|
163
|
-
|
|
86
|
+
const { seed } = validateOptions(options, RAPID_SEED);
|
|
87
|
+
return rapidhash_v1_internal(toDataView(message), seed, rapid_secret, rapid_mix_fast, rapidhash_epilogue_fast);
|
|
164
88
|
}
|
|
89
|
+
/**
|
|
90
|
+
* Calculate a 64-bit hash value of the given message.
|
|
91
|
+
* This function is equivalent to rapidhash() with options.rapidMumBehaviour set to 'protected'.
|
|
92
|
+
*
|
|
93
|
+
* @param message {string|Uint8Array|DataView} The message to be hashed.
|
|
94
|
+
* @param options Options for modifying the hash calculation.
|
|
95
|
+
* @param options.seed {bigint} 64-bit unsigned seed value.
|
|
96
|
+
*/
|
|
165
97
|
function rapidhash_v1_protected(message, options) {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
toDataView(message),
|
|
169
|
-
seed,
|
|
170
|
-
rapid_secret,
|
|
171
|
-
rapid_mix_protected,
|
|
172
|
-
rapidhash_epilogue_protected
|
|
173
|
-
);
|
|
98
|
+
const { seed } = validateOptions(options, RAPID_SEED);
|
|
99
|
+
return rapidhash_v1_internal(toDataView(message), seed, rapid_secret, rapid_mix_protected, rapidhash_epilogue_protected);
|
|
174
100
|
}
|
|
175
|
-
export {
|
|
176
|
-
|
|
177
|
-
rapidhash_v1_fast as rapidhash_fast,
|
|
178
|
-
rapidhash_v1_protected as rapidhash_protected
|
|
179
|
-
};
|
|
101
|
+
export { rapidhash_v1 as rapidhash, rapidhash_v1_fast as rapidhash_fast, rapidhash_v1_protected as rapidhash_protected };
|
|
102
|
+
|
|
180
103
|
//# sourceMappingURL=index.mjs.map
|