From e3fc396bec308867b5078f6794f8b04e30c571c9 Mon Sep 17 00:00:00 2001 From: Nalin Bhardwaj Date: Thu, 13 Jan 2022 16:14:49 -0800 Subject: [PATCH] Drop fnv-plus, handwrite fnv-hash Closes https://github.com/iden3/circom_runtime/issues/9 --- build/main.cjs | 17 +++++++++++------ js/utils.js | 14 +++++++++++--- package-lock.json | 13 +------------ package.json | 3 +-- 4 files changed, 24 insertions(+), 23 deletions(-) diff --git a/build/main.cjs b/build/main.cjs index d492201..6602499 100644 --- a/build/main.cjs +++ b/build/main.cjs @@ -2,13 +2,8 @@ Object.defineProperty(exports, '__esModule', { value: true }); -var fnv = require('fnv-plus'); var ffjavascript = require('ffjavascript'); -function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } - -var fnv__default = /*#__PURE__*/_interopDefaultLegacy(fnv); - /* Copyright 2020 0KIMS association. @@ -44,7 +39,17 @@ function flatArray(a) { } function fnvHash(str) { - return fnv__default['default'].hash(str, 64).hex(); + const uint64_max = BigInt(2) ** BigInt(64); + let hash = BigInt("0xCBF29CE484222325"); + for (var i = 0; i < str.length; i++) { + hash ^= BigInt(str[i].charCodeAt()); + hash *= BigInt(0x100000001B3); + hash %= uint64_max; + } + let shash = hash.toString(16); + let n = 16 - shash.length; + shash = '0'.repeat(n).concat(shash); + return shash; } /* globals WebAssembly */ diff --git a/js/utils.js b/js/utils.js index 103ac2d..3e69d07 100644 --- a/js/utils.js +++ b/js/utils.js @@ -16,8 +16,6 @@ limitations under the License. */ -import fnv from "fnv-plus"; - export function flatArray(a) { var res = []; fillArray(res, a); @@ -35,5 +33,15 @@ export function flatArray(a) { } export function fnvHash(str) { - return fnv.hash(str, 64).hex(); + const uint64_max = BigInt(2) ** BigInt(64); + let hash = BigInt("0xCBF29CE484222325"); + for (var i = 0; i < str.length; i++) { + hash ^= BigInt(str[i].charCodeAt()); + hash *= BigInt(0x100000001B3); + hash %= uint64_max; + } + let shash = hash.toString(16); + let n = 16 - shash.length; + shash = '0'.repeat(n).concat(shash); + return shash; } diff --git a/package-lock.json b/package-lock.json index 9922026..9855f50 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,8 +9,7 @@ "version": "0.1.14", "license": "Apache-2.0", "dependencies": { - "ffjavascript": "0.2.39", - "fnv-plus": "^1.3.1" + "ffjavascript": "0.2.39" }, "bin": { "calcwit": "calcwit.js" @@ -720,11 +719,6 @@ "integrity": "sha512-r5wGx7YeOwNWNlCA0wQ86zKyDLMQr+/RB8xy74M4hTphfmjlijTSSXGuH8rnvKZnfT9i+75zmd8jcKdMR4O6jA==", "dev": true }, - "node_modules/fnv-plus": { - "version": "1.3.1", - "resolved": "https://registry.npmjs.org/fnv-plus/-/fnv-plus-1.3.1.tgz", - "integrity": "sha512-Gz1EvfOneuFfk4yG458dJ3TLJ7gV19q3OM/vVvvHf7eT02Hm1DleB4edsia6ahbKgAYxO9gvyQ1ioWZR+a00Yw==" - }, "node_modules/fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -2097,11 +2091,6 @@ "integrity": "sha512-r5wGx7YeOwNWNlCA0wQ86zKyDLMQr+/RB8xy74M4hTphfmjlijTSSXGuH8rnvKZnfT9i+75zmd8jcKdMR4O6jA==", "dev": true }, - "fnv-plus": { - "version": "1.3.1", - "resolved": "https://registry.npmjs.org/fnv-plus/-/fnv-plus-1.3.1.tgz", - "integrity": "sha512-Gz1EvfOneuFfk4yG458dJ3TLJ7gV19q3OM/vVvvHf7eT02Hm1DleB4edsia6ahbKgAYxO9gvyQ1ioWZR+a00Yw==" - }, "fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", diff --git a/package.json b/package.json index 2b6d4d4..876a62b 100644 --- a/package.json +++ b/package.json @@ -34,8 +34,7 @@ }, "homepage": "https://github.com/iden3/circom_runtime#readme", "dependencies": { - "ffjavascript": "0.2.39", - "fnv-plus": "^1.3.1" + "ffjavascript": "0.2.39" }, "devDependencies": { "eslint": "^6.8.0",