-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support multiple hashing algorthms per ledger version. Bump ledger ve…
…rsion. Remove dependency ohash.
- Loading branch information
Showing
6 changed files
with
239 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,10 +14,10 @@ jobs: | |
uses: cross-org/workflows/.github/workflows/bun-ci.yml@main | ||
with: | ||
jsr_dependencies: "@cross/test @cross/fs @cross/runtime @std/[email protected] @std/[email protected]" | ||
npm_dependencies: "cbor-x ohash" | ||
npm_dependencies: "cbor-x" | ||
node_ci: | ||
uses: cross-org/workflows/.github/workflows/node-ci.yml@main | ||
with: | ||
jsr_dependencies: "@cross/test @cross/fs @cross/runtime @std/[email protected] @std/[email protected]" | ||
npm_dependencies: "cbor-x ohash" | ||
npm_dependencies: "cbor-x" | ||
test_target: "test/*.test.ts" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ node_modules/ | |
.npmrc | ||
package-lock.json | ||
package.json | ||
scripts | ||
|
||
# Deno / JSR | ||
deno.lock | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
/** | ||
* JS Implementation of MurmurHash3 (r136) (as of May 20, 2011) | ||
* | ||
* Based on main branch of https://github.com/unjs/ohash 2024-09-19, slightly modified | ||
* | ||
* @param {Uint8Array | string} key | ||
* @param {number} seed Positive integer only | ||
* @return {number} 32-bit positive integer hash | ||
*/ | ||
export function murmurHash(key: Uint8Array | string, seed = 0) { | ||
if (typeof key === "string") { | ||
key = createBuffer(key); | ||
} | ||
|
||
let i = 0; | ||
let h1 = seed; | ||
let k1; | ||
let h1b; | ||
|
||
const remainder = key.length & 3; // key.length % 4 | ||
const bytes = key.length - remainder; | ||
const c1 = 0xcc_9e_2d_51; | ||
const c2 = 0x1b_87_35_93; | ||
|
||
while (i < bytes) { | ||
k1 = (key[i] & 0xff) | | ||
((key[++i] & 0xff) << 8) | | ||
((key[++i] & 0xff) << 16) | | ||
((key[++i] & 0xff) << 24); | ||
++i; | ||
|
||
k1 = ((k1 & 0xff_ff) * c1 + ((((k1 >>> 16) * c1) & 0xff_ff) << 16)) & | ||
0xff_ff_ff_ff; | ||
k1 = (k1 << 15) | (k1 >>> 17); | ||
k1 = ((k1 & 0xff_ff) * c2 + ((((k1 >>> 16) * c2) & 0xff_ff) << 16)) & | ||
0xff_ff_ff_ff; | ||
|
||
h1 ^= k1; | ||
h1 = (h1 << 13) | (h1 >>> 19); | ||
h1b = ((h1 & 0xff_ff) * 5 + ((((h1 >>> 16) * 5) & 0xff_ff) << 16)) & | ||
0xff_ff_ff_ff; | ||
h1 = (h1b & 0xff_ff) + 0x6b_64 + | ||
((((h1b >>> 16) + 0xe6_54) & 0xff_ff) << 16); | ||
} | ||
|
||
k1 = 0; | ||
|
||
switch (remainder) { | ||
case 3: { | ||
k1 ^= (key[i + 2] & 0xff) << 16; | ||
/* falls through */ | ||
} | ||
// deno-lint-ignore no-fallthrough | ||
case 2: { | ||
k1 ^= (key[i + 1] & 0xff) << 8; | ||
/* falls through */ | ||
} | ||
case 1: { | ||
k1 ^= key[i] & 0xff; | ||
k1 = ((k1 & 0xff_ff) * c1 + ((((k1 >>> 16) * c1) & 0xff_ff) << 16)) & | ||
0xff_ff_ff_ff; | ||
k1 = (k1 << 15) | (k1 >>> 17); | ||
k1 = ((k1 & 0xff_ff) * c2 + ((((k1 >>> 16) * c2) & 0xff_ff) << 16)) & | ||
0xff_ff_ff_ff; | ||
h1 ^= k1; | ||
} | ||
} | ||
|
||
h1 ^= key.length; | ||
|
||
h1 ^= h1 >>> 16; | ||
h1 = ((h1 & 0xff_ff) * 0x85_eb_ca_6b + | ||
((((h1 >>> 16) * 0x85_eb_ca_6b) & 0xff_ff) << 16)) & | ||
0xff_ff_ff_ff; | ||
h1 ^= h1 >>> 13; | ||
h1 = ((h1 & 0xff_ff) * 0xc2_b2_ae_35 + | ||
((((h1 >>> 16) * 0xc2_b2_ae_35) & 0xff_ff) << 16)) & | ||
0xff_ff_ff_ff; | ||
h1 ^= h1 >>> 16; | ||
|
||
return h1 >>> 0; | ||
} | ||
|
||
/** | ||
* JS Implementation of MurmurHash3 (r136) (as of May 20, 2011) | ||
* | ||
* Based on version 1.1.3 of https://github.com/unjs/ohash | ||
* | ||
* @param {Uint8Array | string} key | ||
* @param {number} seed Positive integer only | ||
* @return {number} 32-bit positive integer hash | ||
*/ | ||
export function faultyMurmurHash(key: Uint8Array | string, seed = 0) { | ||
if (typeof key === "string") { | ||
key = createBuffer(key); | ||
} | ||
|
||
let i = 0; | ||
let h1 = seed; | ||
let k1; | ||
let h1b; | ||
|
||
const remainder = key.length & 3; // key.length % 4 | ||
const bytes = key.length - remainder; | ||
const c1 = 0xcc_9e_2d_51; | ||
const c2 = 0x1b_87_35_93; | ||
|
||
while (i < bytes) { | ||
k1 = (key[i] & 0xff) | | ||
((key[++i] & 0xff) << 8) | | ||
((key[++i] & 0xff) << 16) | | ||
((key[++i] & 0xff) << 24); | ||
++i; | ||
|
||
k1 = ((k1 & 0xff_ff) * c1 + ((((k1 >>> 16) * c1) & 0xff_ff) << 16)) & | ||
0xff_ff_ff_ff; | ||
k1 = (k1 << 15) | (k1 >>> 17); | ||
k1 = ((k1 & 0xff_ff) * c2 + ((((k1 >>> 16) * c2) & 0xff_ff) << 16)) & | ||
0xff_ff_ff_ff; | ||
|
||
h1 ^= k1; | ||
h1 = (h1 << 13) | (h1 >>> 19); | ||
h1b = ((h1 & 0xff_ff) * 5 + ((((h1 >>> 16) * 5) & 0xff_ff) << 16)) & | ||
0xff_ff_ff_ff; | ||
h1 = (h1b & 0xff_ff) + 0x6b_64 + | ||
((((h1b >>> 16) + 0xe6_54) & 0xff_ff) << 16); | ||
} | ||
|
||
k1 = 0; | ||
|
||
switch (remainder) { | ||
case 3: { | ||
k1 ^= (key[i + 2] & 0xff) << 16; | ||
break; | ||
} | ||
case 2: { | ||
k1 ^= (key[i + 1] & 0xff) << 8; | ||
break; | ||
} | ||
case 1: { | ||
k1 ^= key[i] & 0xff; | ||
k1 = ((k1 & 0xff_ff) * c1 + ((((k1 >>> 16) * c1) & 0xff_ff) << 16)) & | ||
0xff_ff_ff_ff; | ||
k1 = (k1 << 15) | (k1 >>> 17); | ||
k1 = ((k1 & 0xff_ff) * c2 + ((((k1 >>> 16) * c2) & 0xff_ff) << 16)) & | ||
0xff_ff_ff_ff; | ||
h1 ^= k1; | ||
} | ||
} | ||
|
||
h1 ^= key.length; | ||
|
||
h1 ^= h1 >>> 16; | ||
h1 = ((h1 & 0xff_ff) * 0x85_eb_ca_6b + | ||
((((h1 >>> 16) * 0x85_eb_ca_6b) & 0xff_ff) << 16)) & | ||
0xff_ff_ff_ff; | ||
h1 ^= h1 >>> 13; | ||
h1 = ((h1 & 0xff_ff) * 0xc2_b2_ae_35 + | ||
((((h1 >>> 16) * 0xc2_b2_ae_35) & 0xff_ff) << 16)) & | ||
0xff_ff_ff_ff; | ||
h1 ^= h1 >>> 16; | ||
|
||
return h1 >>> 0; | ||
} | ||
|
||
function createBuffer(val: string) { | ||
return new TextEncoder().encode(val); | ||
} |