From 20e50c3f9d4f621beea7894543a56f4f63946b37 Mon Sep 17 00:00:00 2001 From: Oleg Komendant <44612825+Hrom131@users.noreply.github.com> Date: Fri, 15 Nov 2024 20:21:53 +0200 Subject: [PATCH] Change hash function from the MD5 to the SHA1 (#57) * Change hash function from the MD5 to the SHA256 * Change hash to the sha1 * Made file hashing asynchronous (#58) * Fix code style --------- Co-authored-by: Denys Riabtsev <68712122+1KitCat1@users.noreply.github.com> --- package-lock.json | 4 +-- package.json | 2 +- src/artifacts/CircuitArtifacts.ts | 2 +- src/core/dependencies/CircomFilesResolver.ts | 4 +-- src/utils/utils.ts | 27 ++++++++++++++----- .../unit/cache/circuits-compile-cache.test.ts | 2 +- test/unit/cache/circuits-setup-cache.test.ts | 2 +- test/utils.ts | 22 +++++++++------ 8 files changed, 43 insertions(+), 22 deletions(-) diff --git a/package-lock.json b/package-lock.json index 1843f6c..1f7cbca 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@solarity/hardhat-zkit", - "version": "0.5.1", + "version": "0.5.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@solarity/hardhat-zkit", - "version": "0.5.1", + "version": "0.5.2", "license": "MIT", "workspaces": [ "test/fixture-projects/*" diff --git a/package.json b/package.json index 6df0dc9..737eeea 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@solarity/hardhat-zkit", - "version": "0.5.1", + "version": "0.5.2", "description": "The ultimate TypeScript environment for Circom development", "main": "dist/src/index.js", "types": "dist/src/index.d.ts", diff --git a/src/artifacts/CircuitArtifacts.ts b/src/artifacts/CircuitArtifacts.ts index 2d5b79a..5d9d6e2 100644 --- a/src/artifacts/CircuitArtifacts.ts +++ b/src/artifacts/CircuitArtifacts.ts @@ -225,7 +225,7 @@ export class CircuitArtifacts implements ICircuitArtifacts { circuitArtifact.compilerOutputFiles[CircuitArtifacts.getArtifactOutputFileKey(fileType, provingSystem)] = { fileSourcePath, - fileHash: getFileHash(fileSourcePath), + fileHash: await getFileHash(fileSourcePath), }; } } diff --git a/src/core/dependencies/CircomFilesResolver.ts b/src/core/dependencies/CircomFilesResolver.ts index d021ebc..8ebe015 100644 --- a/src/core/dependencies/CircomFilesResolver.ts +++ b/src/core/dependencies/CircomFilesResolver.ts @@ -16,10 +16,10 @@ import { import { assertHardhatInvariant, HardhatError } from "hardhat/internal/core/errors"; import { ERRORS } from "hardhat/internal/core/errors-list"; import { getRealPath } from "hardhat/internal/util/fs-utils"; -import { createNonCryptographicHashBasedIdentifier } from "hardhat/internal/util/hash"; import { CircomFilesParser } from "./parser/CircomFilesParser"; import { CIRCOM_FILE_REG_EXP, NODE_MODULES, NODE_MODULES_REG_EXP, URI_SCHEME_REG_EXP } from "../../constants"; +import { getFileHash } from "../../utils"; import { HardhatZKitError } from "../../errors"; import { @@ -385,7 +385,7 @@ export class CircomFilesResolver { const stats = await fsExtra.stat(absolutePath); const lastModificationDate = new Date(stats.ctime); - const contentHash = createNonCryptographicHashBasedIdentifier(Buffer.from(rawContent)).toString("hex"); + const contentHash = await getFileHash(absolutePath); const fileData = this._parser.parse(rawContent, absolutePath, contentHash); diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 7700617..ab3b2c4 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -1,19 +1,18 @@ import fsExtra from "fs-extra"; import https from "https"; +import { createHash } from "crypto"; import { exec } from "child_process"; import * as snarkjs from "snarkjs"; -import { createNonCryptographicHashBasedIdentifier } from "hardhat/internal/util/hash"; - import { ProvingSystemType } from "@solarity/zkit"; import { Reporter } from "../reporter"; +import { HardhatZKitError } from "../errors"; +import { BN128_CURVE_NAME } from "../constants"; import { ExecCallResult } from "../types/utils"; -import { BN128_CURVE_NAME } from "../constants"; - /** * Downloads a file from the specified URL * @@ -104,8 +103,24 @@ export async function execCall(execFile: string, callArgs: string[]): Promise { + return new Promise((resolve, reject) => { + const hash = createHash("sha1"); + const stream = fsExtra.createReadStream(filePath); + + stream.on("data", (data: Buffer) => { + // Add data chunk to the hash object + hash.update(data); + }); + + stream.on("end", () => { + resolve(hash.digest("hex")); + }); + + stream.on("error", () => { + reject(new HardhatZKitError(`Failed to read ${filePath} file`)); + }); + }); } export function getUniqueProvingSystems(provingSystems: ProvingSystemType | ProvingSystemType[]): ProvingSystemType[] { diff --git a/test/unit/cache/circuits-compile-cache.test.ts b/test/unit/cache/circuits-compile-cache.test.ts index cd6d8f5..8d04b8b 100644 --- a/test/unit/cache/circuits-compile-cache.test.ts +++ b/test/unit/cache/circuits-compile-cache.test.ts @@ -94,7 +94,7 @@ describe("CircuitsCompileCache", () => { expect(CircuitsCompileCache!.hasFileChanged("invalid-path", "", defaultCompileFlags)).to.be.true; const circuitPath = getNormalizedFullPath(this.hre.config.paths.root, "circuits/main/mul2.circom"); - const contentHash = getFileHash(circuitPath); + const contentHash = await getFileHash(circuitPath); expect(CircuitsCompileCache!.hasFileChanged(circuitPath, contentHash + "1", defaultCompileFlags)).to.be.true; expect(CircuitsCompileCache!.hasFileChanged(circuitPath, contentHash, { ...defaultCompileFlags, c: true })).to.be diff --git a/test/unit/cache/circuits-setup-cache.test.ts b/test/unit/cache/circuits-setup-cache.test.ts index 8177e41..6ad0250 100644 --- a/test/unit/cache/circuits-setup-cache.test.ts +++ b/test/unit/cache/circuits-setup-cache.test.ts @@ -105,7 +105,7 @@ describe("CircuitsSetupCache", () => { "r1cs", ); - const contentHash = getFileHash(mul2R1CSFilePath); + const contentHash = await getFileHash(mul2R1CSFilePath); expect( CircuitsSetupCache!.hasFileChanged(mul2ArtifactFullPath, contentHash + "1", defaultContributionSettings), diff --git a/test/utils.ts b/test/utils.ts index 94f3fa0..12052c8 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -19,11 +19,15 @@ export async function getCompileCacheEntry( const circuitPath = getNormalizedFullPath(projectRoot, sourceName); if (!contentHash) { - contentHash = getFileHash(circuitPath); + contentHash = await getFileHash(circuitPath); } const parser: CircomFilesParser = new CircomFilesParser(); - const fileData: ResolvedFileData = parser.parse(fsExtra.readFileSync(circuitPath, "utf-8"), circuitPath, contentHash); + const fileData: ResolvedFileData = parser.parse( + fsExtra.readFileSync(circuitPath, "utf-8"), + circuitPath, + await contentHash, + ); const stats = await fsExtra.stat(circuitPath); const lastModificationDate: Date = new Date(stats.ctime); @@ -43,12 +47,14 @@ export async function getSetupCacheEntry( provingSystemsData?: ProvingSystemData[], ): Promise { if (!provingSystemsData) { - provingSystemsData = defaultContributionSettings.provingSystems.map((provingSystem) => { - return { - provingSystem: provingSystem, - lastR1CSFileHash: getFileHash(r1csSourcePath), - }; - }); + provingSystemsData = await Promise.all( + defaultContributionSettings.provingSystems.map(async (provingSystem) => { + return { + provingSystem: provingSystem, + lastR1CSFileHash: await getFileHash(r1csSourcePath), + }; + }), + ); } return {