Comparing version 1.0.3 to 1.0.4
80
hash.js
@@ -6,2 +6,3 @@ export const BEBB4185 = discohash; | ||
const STATEM = STATE-1; | ||
const BSTATEM = (STATEM); | ||
const HSTATE64M = (STATE64 >> 1)-1; | ||
@@ -15,5 +16,78 @@ const STATE64M = STATE64-1; | ||
const ds = new BigUint64Array(disco_buf); | ||
const C = new BigUint64Array(1); | ||
const C8 = new Uint8Array(1); | ||
const T64 = new BigUint64Array(1); | ||
const T8 = new Uint8Array(1); | ||
const R = 23n; | ||
const S3 = 63n; | ||
const S4 = 64n; | ||
function rot( v, n = 0n ) { | ||
n = n & S3; | ||
if (n) { | ||
v = (v >> n) | (v << (S4-n)); | ||
} | ||
return v; | ||
} | ||
function rot8( v, n = 0 ) { | ||
n = n & 7; | ||
if (n) { | ||
v = (v >> n) | (v << (8-n)); | ||
} | ||
return v; | ||
} | ||
function mix(A = 0) { | ||
const B = A+1; | ||
ds[A] *= P; | ||
ds[A] = rot(ds[A], R); | ||
ds[A] *= Q; | ||
ds[B] ^= ds[A]; | ||
ds[B] *= P; | ||
ds[B] = rot(ds[B], R); | ||
ds[B] *= Q; | ||
} | ||
function round( m64, m8 ) { | ||
const len = m8.length; | ||
let index = 0; | ||
let sindex = 0; | ||
C[0] = 0xfaccadaccad09997n; | ||
C8[0] = 137; | ||
for( let Len = len >> 3; index < Len; index++) { | ||
T64[0] = m64[index] + BigInt(index) + C[0] + 1n; | ||
ds[sindex] += rot(T64[0], R); | ||
C[0] += ~m64[index] + 1n; | ||
if ( sindex == HSTATE64M ) { | ||
mix(0); | ||
} else if ( sindex == STATE64M ) { | ||
mix(2); | ||
sindex = -1; | ||
} | ||
sindex++; | ||
} | ||
mix(1); | ||
index <<= 3; | ||
sindex = index&(BSTATEM); | ||
for( ; index < len; index++) { | ||
T8[0] = m8[index] + index + C8[0] + 1; | ||
ds8[sindex] = rot8(T8[0], 23); | ||
C8[0] += ~m8[sindex] + 1; | ||
mix(index%STATE64M); | ||
if ( sindex >= STATEM ) { | ||
sindex = -1; | ||
} | ||
sindex++; | ||
} | ||
mix(0); | ||
mix(1); | ||
mix(2); | ||
} | ||
@@ -32,3 +106,7 @@ | ||
const len = key.length*4; | ||
let len = key.length*4; | ||
if( len % 8 != 0 ) { | ||
len += 4; | ||
} | ||
const key_buf = new ArrayBuffer(len); | ||
@@ -35,0 +113,0 @@ const key8Arr = new Uint8Array(key_buf); |
{ | ||
"name": "bebb4185", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "A super-fast SMHasher-passing non-cryptographic hash function.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -36,1 +36,3 @@ # :city_sunrise: [Discohash](https://github.com/cris691/discohash) | ||
``` | ||
**Note:** JS implementation is currently WIP. |
39
test.js
@@ -9,7 +9,44 @@ import {discohash} from './index.js'; | ||
test(); | ||
smhasher_verification_value(); | ||
function test() { | ||
tests.forEach(([k, s]) => { | ||
console.log(k, s, discohash(k, s)); | ||
console.log(`${k}, ${s}, 0x${discohash(k, s).toString(16).padStart(16,'0')}`); | ||
}); | ||
} | ||
function smhasher_verification_value() { | ||
// Copied from <smhasher_repo>/src/KeysetTests.cpp | ||
const hashbytes = 8; | ||
const key = new Uint8Array( 256 ); | ||
const hbuf = new ArrayBuffer(256*hashbytes); | ||
const hashes = new Uint8Array(hbuf); | ||
const out = new ArrayBuffer(hashbytes); | ||
const hash = new Uint8Array(out); | ||
const h32 = new Uint32Array(out); | ||
const hout = new BigUint64Array(out); | ||
const verif = new Uint32Array(1); | ||
// Hash keys of the form {0}, {0,1}, {0,1,2}... up to N=255,using 256-N as | ||
// the seed | ||
for ( let i = 0; i < 256; i++ ) { | ||
key[i] = i; | ||
const seed = 256-i; | ||
hout[0] = discohash(key.slice(0,i+1), seed ); | ||
hashes.set( hash, i*hashbytes ); | ||
} | ||
// Then hash the result array | ||
hout[0] = discohash(hashes, 0); | ||
// equivalentj | ||
// verif[0] = hash[0] | (hash[1] << 8) | (hash[2] << 16) | (hash[3] << 24); | ||
verif[0] = h32[0]; | ||
console.log(`JS verif val: 0x${verif[0].toString(16).padStart(8, '0')}`); | ||
return verif[0]; | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
420512
167
38