Skip to content

Commit

Permalink
Fixed issue with caching parsedInputs
Browse files Browse the repository at this point in the history
  • Loading branch information
KyrylR committed Dec 23, 2024
1 parent 63822ae commit 2a9243c
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/core/dependencies/CircomFilesResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ 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 { deepClone } from "../../core/dependencies/utils";
import {
CircomResolvedFile as ICircomResolvedFile,
ResolvedFileData,
Expand Down Expand Up @@ -249,12 +249,21 @@ export class CircomFilesResolver {
},
);

if (!fileWithTemplate.fileData.parsedFileData.templates[templateName].parsedInputs) {
fileWithTemplate.fileData.parsedFileData.templates[templateName].parsedInputs =
this._parser.parseTemplateInputs(fileWithTemplate, templateName, mainComponentData.parameters);
if (!resolvedFile.fileData.parsedFileData.templates[templateName]) {
resolvedFile.fileData.parsedFileData.templates[templateName] = deepClone(
fileWithTemplate.fileData.parsedFileData.templates[templateName],
);
}

if (!resolvedFile.fileData.parsedFileData.templates[templateName].parsedInputs) {
resolvedFile.fileData.parsedFileData.templates[templateName].parsedInputs = this._parser.parseTemplateInputs(
fileWithTemplate,
templateName,
mainComponentData.parameters,
);
}

const parsedInputs = fileWithTemplate.fileData.parsedFileData.templates[templateName].parsedInputs!;
const parsedInputs = resolvedFile.fileData.parsedFileData.templates[templateName].parsedInputs!;

for (const key of Object.keys(parsedInputs)) {
const signalType: SignalType = this._getSignalType(parsedInputs[key].type);
Expand Down
16 changes: 16 additions & 0 deletions src/core/dependencies/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export function deepClone<T>(value: T): T {
const visited = new WeakSet();

const replacer = (_: any, obj: any) => {
if (typeof obj === "object" && obj !== null) {
if (visited.has(obj)) {
return "[Circular]";
}
visited.add(obj);
}

return obj;
};

return JSON.parse(JSON.stringify(value, replacer));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/cache
/artifacts

compilers
contracts/verifiers
zkit
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pragma circom 2.0.0;

include "templates/mul3Arr.circom";

component main = Multiplier3Arr(3);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pragma circom 2.0.0;

include "templates/mul3Arr.circom";

component main = Multiplier3Arr(6);
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
pragma circom 2.0.0;

template Multiplier3Arr(count){
signal input in[count];

signal output out;
signal output result[count];

signal tmp <-- in[0] * in[1];

out <== tmp * in[2];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { HardhatUserConfig } from "hardhat/config";

import config from "../hardhat.config";

const defaultConfig: HardhatUserConfig = {
...config,
zkit: {
circuitsDir: "circuits",
compilationSettings: {
artifactsDir: "zkit/artifacts",
skipFiles: ["vendor"],
},
setupSettings: {
ptauDir: "zkit/ptau",
ptauDownload: true,
},
quiet: true,
verifiersSettings: {
verifiersDir: "contracts/verifiers",
},
},
};

export default defaultConfig;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "hardhat-project-with-complex-circuits",
"version": "1.0.0",
"scripts": {
"compile": "hardhat compile --force",
"clean": "hardhat clean && rm -rf artifacts && rm -rf cache"
}
}
59 changes: 59 additions & 0 deletions test/unit/core/compile/types-generation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { useEnvironment } from "@test-helpers";
import { TASK_CIRCUITS_MAKE, ZKIT_SCOPE_NAME } from "../../../../src/task-names";
import fsExtra from "fs-extra";
import path from "path";
import { CircuitArtifact } from "../../../../src/types/artifacts/circuit-artifacts";
import { expect } from "chai";
import { HardhatRuntimeEnvironment } from "hardhat/types";

describe.only("Types Generation", () => {
describe("types generation:with duplicated main components", () => {
useEnvironment("with-duplicated-circuits");

beforeEach(async function () {
await this.hre.run({ scope: ZKIT_SCOPE_NAME, task: TASK_CIRCUITS_MAKE });
});

function getCircuitArtifact(hre: HardhatRuntimeEnvironment, pathToCircuit: string): CircuitArtifact {
return JSON.parse(fsExtra.readFileSync(path.join(hre.config.paths.root, pathToCircuit)) as any);
}

it("should correctly resolve dimensions for circuits with multiple main components", async function () {
const artifact1: CircuitArtifact = getCircuitArtifact(
this.hre,
"zkit/artifacts/circuits/mul3Arr.circom/Multiplier3Arr_artifacts.json",
);
const artifact2: CircuitArtifact = getCircuitArtifact(
this.hre,
"zkit/artifacts/circuits/mul3Arr2.circom/Multiplier3Arr_artifacts.json",
);

expect(artifact1.baseCircuitInfo.signals[0].dimension).to.be.deep.eq([3]);
expect(artifact2.baseCircuitInfo.signals[0].dimension).to.be.deep.eq([6]);
});

it.only("should correctly resolve dimensions for circuits with multiple main components from cache", async function () {
const initialFileContent = fsExtra
.readFileSync(path.join(this.hre.config.paths.root, "circuits/mul3Arr.circom"))
.toString();

fsExtra.appendFileSync(path.join(this.hre.config.paths.root, "circuits/mul3Arr.circom"), "// comment");

await this.hre.run({ scope: ZKIT_SCOPE_NAME, task: TASK_CIRCUITS_MAKE });

const artifact2: CircuitArtifact = getCircuitArtifact(
this.hre,
"zkit/artifacts/circuits/mul3Arr2.circom/Multiplier3Arr_artifacts.json",
);
const artifact1: CircuitArtifact = getCircuitArtifact(
this.hre,
"zkit/artifacts/circuits/mul3Arr.circom/Multiplier3Arr_artifacts.json",
);

expect(artifact1.baseCircuitInfo.signals[0].dimension).to.be.deep.eq([3]);
expect(artifact2.baseCircuitInfo.signals[0].dimension).to.be.deep.eq([6]);

fsExtra.writeFileSync(path.join(this.hre.config.paths.root, "circuits/mul3Arr.circom"), initialFileContent);
});
});
});

0 comments on commit 2a9243c

Please sign in to comment.