rapidhash-js 1.0.0
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/LICENSE +21 -0
- package/README.md +38 -0
- package/build/src/index.d.ts +1 -0
- package/build/src/index.js +18 -0
- package/build/src/rapidhash.d.ts +33 -0
- package/build/src/rapidhash.js +170 -0
- package/package.json +32 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 KOMIYA Atsushi
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# rapidhash
|
|
2
|
+
|
|
3
|
+
A TypeScript implementation of the [Nicoshev/rapidhash](https://github.com/Nicoshev/rapidhash).
|
|
4
|
+
|
|
5
|
+
Rapidhash is a fast hashing algorithm that passes all tests in [SMHasher](https://github.com/rurban/smhasher) and [SMHasher3](https://gitlab.com/fwojcik/smhasher3).
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install rapidhash
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
```typescript
|
|
17
|
+
import {rapidhash} from 'rapidhash';
|
|
18
|
+
|
|
19
|
+
// rapidhash() returns a 64-bit hash value represented as bigint.
|
|
20
|
+
console.log(rapidhash('hello world')); // Output: 17498481775468162579n
|
|
21
|
+
|
|
22
|
+
// You can specify a 64-bit seed value with the 'seed' option.
|
|
23
|
+
console.log(rapidhash('hello world', {seed: 0x0123_4567_89AB_CDEFn})); // Output: 9400866032237060842n
|
|
24
|
+
|
|
25
|
+
// You can specify the behavior of the rapid_mum() function
|
|
26
|
+
// with the rapidMumBehaviour option (default is 'fast').
|
|
27
|
+
console.log(rapidhash('hello world', {rapidMumBehaviour: 'protected'})); // Output: 16019612682927597028n
|
|
28
|
+
|
|
29
|
+
// Instead of specifying the rapidMumBehaviour option,
|
|
30
|
+
// you can call rapidhash_fast() or rapidhash_protected().
|
|
31
|
+
console.log(rapidhash_protected('hello world'));
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## License
|
|
35
|
+
|
|
36
|
+
MIT License
|
|
37
|
+
|
|
38
|
+
Copyright (c) 2024 KOMIYA Atsushi.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './rapidhash';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./rapidhash"), exports);
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
type RapidMumBehaviour = 'fast' | 'protected';
|
|
2
|
+
interface RapidhashOptions {
|
|
3
|
+
seed: bigint;
|
|
4
|
+
rapidMumBehaviour: RapidMumBehaviour;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Calculate a 64-bit hash value of the given message.
|
|
8
|
+
*
|
|
9
|
+
* @param message {string|Uint8Array|DataView} The message to be hashed.
|
|
10
|
+
* @param options Options for modifying the hash calculation.
|
|
11
|
+
* @param options.seed {bigint} 64-bit unsigned seed value.
|
|
12
|
+
* @param options.rapidMumBehaviour {'fast'|'protected'} Alters behaviour of the rapid_mum function. Defaults to 'fast'.
|
|
13
|
+
*/
|
|
14
|
+
export declare function rapidhash(message: string | Uint8Array | DataView, options?: Partial<RapidhashOptions>): bigint;
|
|
15
|
+
/**
|
|
16
|
+
* Calculate a 64-bit hash value of the given message.
|
|
17
|
+
* This function is equivalent to rapidhash() with options.rapidMumBehaviour set to 'fast'.
|
|
18
|
+
*
|
|
19
|
+
* @param message {string|Uint8Array|DataView} The message to be hashed.
|
|
20
|
+
* @param options Options for modifying the hash calculation.
|
|
21
|
+
* @param options.seed {bigint} 64-bit unsigned seed value.
|
|
22
|
+
*/
|
|
23
|
+
export declare function rapidhash_fast(message: string | Uint8Array | DataView, options?: Partial<Omit<RapidhashOptions, 'rapidMumBehaviour'>>): bigint;
|
|
24
|
+
/**
|
|
25
|
+
* Calculate a 64-bit hash value of the given message.
|
|
26
|
+
* This function is equivalent to rapidhash() with options.rapidMumBehaviour set to 'protected'.
|
|
27
|
+
*
|
|
28
|
+
* @param message {string|Uint8Array|DataView} The message to be hashed.
|
|
29
|
+
* @param options Options for modifying the hash calculation.
|
|
30
|
+
* @param options.seed {bigint} 64-bit unsigned seed value.
|
|
31
|
+
*/
|
|
32
|
+
export declare function rapidhash_protected(message: string | Uint8Array | DataView, options?: Partial<Omit<RapidhashOptions, 'rapidMumBehaviour'>>): bigint;
|
|
33
|
+
export {};
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.rapidhash_protected = exports.rapidhash_fast = exports.rapidhash = void 0;
|
|
4
|
+
const RAPID_SEED = 0xbdd89aa982704029n;
|
|
5
|
+
const rapid_secret = [
|
|
6
|
+
0x2d358dccaa6c78a5n,
|
|
7
|
+
0x8bb84b93962eacc9n,
|
|
8
|
+
0x4b33a62ed433d4a3n,
|
|
9
|
+
];
|
|
10
|
+
function rapid_mum_fast(a, b) {
|
|
11
|
+
return a * b;
|
|
12
|
+
}
|
|
13
|
+
function rapid_mum_protected(a, b) {
|
|
14
|
+
const m = a * b;
|
|
15
|
+
return m ^ (a | (b << 64n));
|
|
16
|
+
}
|
|
17
|
+
function rapid_mix(a, b, rapid_mum) {
|
|
18
|
+
const m = rapid_mum(a, b);
|
|
19
|
+
return (m & 0xffffffffffffffffn) ^ (m >> 64n);
|
|
20
|
+
}
|
|
21
|
+
function rapid_read32(buf, offset) {
|
|
22
|
+
return BigInt(buf.getUint32(offset, true));
|
|
23
|
+
}
|
|
24
|
+
function rapid_read64(buf, offset) {
|
|
25
|
+
return buf.getBigUint64(offset, true);
|
|
26
|
+
}
|
|
27
|
+
function rapid_readSmall(buf, offset, k) {
|
|
28
|
+
const v1 = buf.getUint8(offset);
|
|
29
|
+
const v2 = buf.getUint8(offset + (k >> 1));
|
|
30
|
+
const v3 = buf.getUint8(offset + k - 1);
|
|
31
|
+
return (BigInt(v1) << 56n) | (BigInt(v2) << 32n) | BigInt(v3);
|
|
32
|
+
}
|
|
33
|
+
function rapidhash_internal(key, seed, secret, rapid_mum) {
|
|
34
|
+
const len = key.byteLength;
|
|
35
|
+
const lenBI = BigInt(key.byteLength);
|
|
36
|
+
seed ^= rapid_mix(seed ^ secret[0], secret[1], rapid_mum) ^ lenBI;
|
|
37
|
+
let a;
|
|
38
|
+
let b;
|
|
39
|
+
if (len <= 16) {
|
|
40
|
+
if (len >= 4) {
|
|
41
|
+
const last = len - 4;
|
|
42
|
+
a = (rapid_read32(key, 0) << 32n) | rapid_read32(key, last);
|
|
43
|
+
const delta = (len & 24) >> (len >> 3);
|
|
44
|
+
b = (rapid_read32(key, delta) << 32n) | rapid_read32(key, last - delta);
|
|
45
|
+
}
|
|
46
|
+
else if (len > 0) {
|
|
47
|
+
a = rapid_readSmall(key, 0, len);
|
|
48
|
+
b = 0n;
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
a = 0n;
|
|
52
|
+
b = 0n;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
let i = len;
|
|
57
|
+
let p = 0;
|
|
58
|
+
if (i > 48) {
|
|
59
|
+
let see1 = seed, see2 = seed;
|
|
60
|
+
while (i >= 96) {
|
|
61
|
+
seed = rapid_mix(rapid_read64(key, p) ^ secret[0], rapid_read64(key, p + 8) ^ seed, rapid_mum);
|
|
62
|
+
see1 = rapid_mix(rapid_read64(key, p + 16) ^ secret[1], rapid_read64(key, p + 24) ^ see1, rapid_mum);
|
|
63
|
+
see2 = rapid_mix(rapid_read64(key, p + 32) ^ secret[2], rapid_read64(key, p + 40) ^ see2, rapid_mum);
|
|
64
|
+
seed = rapid_mix(rapid_read64(key, p + 48) ^ secret[0], rapid_read64(key, p + 56) ^ seed, rapid_mum);
|
|
65
|
+
see1 = rapid_mix(rapid_read64(key, p + 64) ^ secret[1], rapid_read64(key, p + 72) ^ see1, rapid_mum);
|
|
66
|
+
see2 = rapid_mix(rapid_read64(key, p + 80) ^ secret[2], rapid_read64(key, p + 88) ^ see2, rapid_mum);
|
|
67
|
+
p += 96;
|
|
68
|
+
i -= 96;
|
|
69
|
+
}
|
|
70
|
+
if (i >= 48) {
|
|
71
|
+
seed = rapid_mix(rapid_read64(key, p) ^ secret[0], rapid_read64(key, p + 8) ^ seed, rapid_mum);
|
|
72
|
+
see1 = rapid_mix(rapid_read64(key, p + 16) ^ secret[1], rapid_read64(key, p + 24) ^ see1, rapid_mum);
|
|
73
|
+
see2 = rapid_mix(rapid_read64(key, p + 32) ^ secret[2], rapid_read64(key, p + 40) ^ see2, rapid_mum);
|
|
74
|
+
p += 48;
|
|
75
|
+
i -= 48;
|
|
76
|
+
}
|
|
77
|
+
seed ^= see1 ^ see2;
|
|
78
|
+
}
|
|
79
|
+
if (i > 16) {
|
|
80
|
+
seed = rapid_mix(rapid_read64(key, p) ^ secret[2], rapid_read64(key, p + 8) ^ seed ^ secret[1], rapid_mum);
|
|
81
|
+
if (i > 32) {
|
|
82
|
+
seed = rapid_mix(rapid_read64(key, p + 16) ^ secret[2], rapid_read64(key, p + 24) ^ seed, rapid_mum);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
a = rapid_read64(key, p + i - 16);
|
|
86
|
+
b = rapid_read64(key, p + i - 8);
|
|
87
|
+
}
|
|
88
|
+
a ^= secret[1];
|
|
89
|
+
b ^= seed;
|
|
90
|
+
const m = rapid_mum(a, b);
|
|
91
|
+
return rapid_mix((m & 0xffffffffffffffffn) ^ secret[0] ^ lenBI, (m >> 64n) ^ secret[1], rapid_mum);
|
|
92
|
+
}
|
|
93
|
+
const defaultRapidMumBehaviour = 'fast';
|
|
94
|
+
const rapidMumImplementations = {
|
|
95
|
+
fast: rapid_mum_fast,
|
|
96
|
+
protected: rapid_mum_protected,
|
|
97
|
+
};
|
|
98
|
+
function isBigUint64(value) {
|
|
99
|
+
return value >= 0n && value <= 0xffffffffffffffffn;
|
|
100
|
+
}
|
|
101
|
+
function validateOptions(options) {
|
|
102
|
+
const result = {
|
|
103
|
+
seed: RAPID_SEED,
|
|
104
|
+
rapidMumBehaviour: defaultRapidMumBehaviour,
|
|
105
|
+
};
|
|
106
|
+
if (options === undefined) {
|
|
107
|
+
return result;
|
|
108
|
+
}
|
|
109
|
+
if (options.seed !== undefined) {
|
|
110
|
+
if (!isBigUint64(options.seed)) {
|
|
111
|
+
throw new Error(`seed must be a 64-bit unsigned bigint value: ${options.seed}`);
|
|
112
|
+
}
|
|
113
|
+
result.seed = options.seed;
|
|
114
|
+
}
|
|
115
|
+
if (options.rapidMumBehaviour !== undefined) {
|
|
116
|
+
result.rapidMumBehaviour = options.rapidMumBehaviour;
|
|
117
|
+
}
|
|
118
|
+
return result;
|
|
119
|
+
}
|
|
120
|
+
const textEncoder = new TextEncoder();
|
|
121
|
+
function toDataView(message) {
|
|
122
|
+
if (message instanceof DataView) {
|
|
123
|
+
return message;
|
|
124
|
+
}
|
|
125
|
+
if (message instanceof Uint8Array) {
|
|
126
|
+
return new DataView(message.buffer, message.byteOffset, message.byteLength);
|
|
127
|
+
}
|
|
128
|
+
const utf8bytes = textEncoder.encode(message);
|
|
129
|
+
return new DataView(utf8bytes.buffer, utf8bytes.byteOffset, utf8bytes.byteLength);
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Calculate a 64-bit hash value of the given message.
|
|
133
|
+
*
|
|
134
|
+
* @param message {string|Uint8Array|DataView} The message to be hashed.
|
|
135
|
+
* @param options Options for modifying the hash calculation.
|
|
136
|
+
* @param options.seed {bigint} 64-bit unsigned seed value.
|
|
137
|
+
* @param options.rapidMumBehaviour {'fast'|'protected'} Alters behaviour of the rapid_mum function. Defaults to 'fast'.
|
|
138
|
+
*/
|
|
139
|
+
function rapidhash(message, options) {
|
|
140
|
+
const { seed, rapidMumBehaviour } = validateOptions(options);
|
|
141
|
+
return rapidhash_internal(toDataView(message), seed, rapid_secret, rapidMumImplementations[rapidMumBehaviour]);
|
|
142
|
+
}
|
|
143
|
+
exports.rapidhash = rapidhash;
|
|
144
|
+
/**
|
|
145
|
+
* Calculate a 64-bit hash value of the given message.
|
|
146
|
+
* This function is equivalent to rapidhash() with options.rapidMumBehaviour set to 'fast'.
|
|
147
|
+
*
|
|
148
|
+
* @param message {string|Uint8Array|DataView} The message to be hashed.
|
|
149
|
+
* @param options Options for modifying the hash calculation.
|
|
150
|
+
* @param options.seed {bigint} 64-bit unsigned seed value.
|
|
151
|
+
*/
|
|
152
|
+
function rapidhash_fast(message, options) {
|
|
153
|
+
const { seed } = validateOptions(options);
|
|
154
|
+
return rapidhash_internal(toDataView(message), seed, rapid_secret, rapid_mum_fast);
|
|
155
|
+
}
|
|
156
|
+
exports.rapidhash_fast = rapidhash_fast;
|
|
157
|
+
/**
|
|
158
|
+
* Calculate a 64-bit hash value of the given message.
|
|
159
|
+
* This function is equivalent to rapidhash() with options.rapidMumBehaviour set to 'protected'.
|
|
160
|
+
*
|
|
161
|
+
* @param message {string|Uint8Array|DataView} The message to be hashed.
|
|
162
|
+
* @param options Options for modifying the hash calculation.
|
|
163
|
+
* @param options.seed {bigint} 64-bit unsigned seed value.
|
|
164
|
+
*/
|
|
165
|
+
function rapidhash_protected(message, options) {
|
|
166
|
+
const { seed } = validateOptions(options);
|
|
167
|
+
return rapidhash_internal(toDataView(message), seed, rapid_secret, rapid_mum_protected);
|
|
168
|
+
}
|
|
169
|
+
exports.rapidhash_protected = rapidhash_protected;
|
|
170
|
+
//# sourceMappingURL=rapidhash.js.map
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "rapidhash-js",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A TypeScript implementation of Rapidhash",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"hash",
|
|
7
|
+
"rapidhash"
|
|
8
|
+
],
|
|
9
|
+
"author": {
|
|
10
|
+
"name": "KOMIYA Atsushi",
|
|
11
|
+
"url": "https://github.com/komiya-atsushi"
|
|
12
|
+
},
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/komiya-atsushi/rapidhash-js.git"
|
|
17
|
+
},
|
|
18
|
+
"bugs": {
|
|
19
|
+
"url": "https://github.com/komiya-atsushi/rapidhash-js/issues"
|
|
20
|
+
},
|
|
21
|
+
"engines": {
|
|
22
|
+
"node": ">=18"
|
|
23
|
+
},
|
|
24
|
+
"scripts": {
|
|
25
|
+
"compile": "tsc",
|
|
26
|
+
"pretest": "npm run compile"
|
|
27
|
+
},
|
|
28
|
+
"main": "build/src/index.js",
|
|
29
|
+
"files": [
|
|
30
|
+
"build/src/**/*.{js,d.ts}"
|
|
31
|
+
]
|
|
32
|
+
}
|