Skip to content

Commit

Permalink
Change hash function from the MD5 to the SHA1 (#57)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
Hrom131 and 1KitCat1 authored Nov 15, 2024
1 parent c6aa722 commit 20e50c3
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 22 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/artifacts/CircuitArtifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ export class CircuitArtifacts implements ICircuitArtifacts {

circuitArtifact.compilerOutputFiles[CircuitArtifacts.getArtifactOutputFileKey(fileType, provingSystem)] = {
fileSourcePath,
fileHash: getFileHash(fileSourcePath),
fileHash: await getFileHash(fileSourcePath),
};
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/dependencies/CircomFilesResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);

Expand Down
27 changes: 21 additions & 6 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -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
*
Expand Down Expand Up @@ -104,8 +103,24 @@ export async function execCall(execFile: string, callArgs: string[]): Promise<Ex
});
}

export function getFileHash(filePath: string): string {
return createNonCryptographicHashBasedIdentifier(Buffer.from(fsExtra.readFileSync(filePath))).toString("hex");
export async function getFileHash(filePath: string): Promise<string> {
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[] {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/cache/circuits-compile-cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/unit/cache/circuits-setup-cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ describe("CircuitsSetupCache", () => {
"r1cs",
);

const contentHash = getFileHash(mul2R1CSFilePath);
const contentHash = await getFileHash(mul2R1CSFilePath);

expect(
CircuitsSetupCache!.hasFileChanged(mul2ArtifactFullPath, contentHash + "1", defaultContributionSettings),
Expand Down
22 changes: 14 additions & 8 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -43,12 +47,14 @@ export async function getSetupCacheEntry(
provingSystemsData?: ProvingSystemData[],
): Promise<SetupCacheEntry> {
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 {
Expand Down

0 comments on commit 20e50c3

Please sign in to comment.