Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an ability to resolve arguments in component main #63

Merged
merged 7 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 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
Expand Up @@ -58,7 +58,7 @@
"node": ">=16"
},
"dependencies": {
"@distributedlab/circom-parser": "0.2.0",
"@distributedlab/circom-parser": "0.2.2",
"@solarity/zkit": "0.3.0",
"@solarity/zktype": "0.4.2",
"@wasmer/wasi": "0.12.0",
Expand Down
11 changes: 7 additions & 4 deletions src/core/dependencies/parser/CircomFilesParser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CircomValueType, getCircomParser, ParserError } from "@distributedlab/circom-parser";
import { buildVariableContext, CircomValueType, getCircomParser, ParserError } from "@distributedlab/circom-parser";

import { CircomFilesVisitor } from "./CircomFilesVisitor";
import { CircomTemplateInputsVisitor } from "./CircomTemplateInputsVisitor";
Expand Down Expand Up @@ -64,7 +64,7 @@ export class CircomFilesParser {
const visitorErrors = circomFilesVisitor.errors.filter(
(error) =>
error.type === ErrorType.InvalidPragmaVersion ||
error.type === ErrorType.TemplateAlreadyUsed ||
error.type === ErrorType.TemplateAlreadyVisited ||
error.type === ErrorType.FailedToResolveMainComponentParameter,
);

Expand Down Expand Up @@ -96,10 +96,13 @@ export class CircomFilesParser {
templateName: string,
parameterValues: Record<string, CircomValueType>,
): Record<string, InputData> {
const parsedFileData = circomResolvedFile.fileData.parsedFileData;

const values: CircomValueType[] = Object.keys(parameterValues).map((key) => parameterValues[key]);
const circomTemplateInputsVisitor = new CircomTemplateInputsVisitor(
circomResolvedFile.absolutePath,
circomResolvedFile.fileData.parsedFileData.templates[templateName].context,
parameterValues,
parsedFileData.templates[templateName].context,
buildVariableContext(parsedFileData.templates[templateName].parameters, values),
);

circomTemplateInputsVisitor.startParse();
Expand Down
2 changes: 1 addition & 1 deletion src/core/dependencies/parser/CircomFilesVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class CircomFilesVisitor extends CircomVisitor<void> {
visitTemplateDefinition = (ctx: TemplateDefinitionContext) => {
if (ctx.ID().getText() in this.fileData.templates) {
this.errors.push({
type: ErrorType.TemplateAlreadyUsed,
type: ErrorType.TemplateAlreadyVisited,
context: ctx,
fileIdentifier: this.fileIdentifier,
templateIdentifier: ctx.ID().getText(),
Expand Down
22 changes: 0 additions & 22 deletions src/core/dependencies/parser/CircomTemplateInputsVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
IfWithFollowUpIfContext,
ParserErrorItem,
ParserRuleContext,
parseSimpleIdentifierList,
PIdentifierStatementContext,
PUnderscoreContext,
SignalDeclarationContext,
Expand Down Expand Up @@ -58,8 +57,6 @@ export class CircomTemplateInputsVisitor extends CircomVisitor<void> {
this.templateInputs = {};

this._vars = parameterValues;

this._validateVariableContext();
}

startParse = () => {
Expand Down Expand Up @@ -561,25 +558,6 @@ export class CircomTemplateInputsVisitor extends CircomVisitor<void> {
});
};

private _validateVariableContext() {
const templateParameters = parseSimpleIdentifierList(this.templateContext.simpleIdentifierList());

for (const parameter of templateParameters) {
if (this._vars[parameter] === undefined || this._vars[parameter] === null) {
this.errors.push({
type: ErrorType.MissingTemplateParameterValue,
context: this.templateContext,
fileIdentifier: this.templateContext.ID().getText(),
message: `Missing value for parameter ${parameter} in template ${this.templateContext.ID().getText()}`,
});

continue;
}

this._declaredVariables[parameter] = true;
}
}

private _resolveIdentifier(ctx: IdentifierContext, variableContext: VariableContext = {}): IdentifierObject[] | null {
const baseName = ctx.ID().getText();

Expand Down
2 changes: 1 addition & 1 deletion src/types/core/dependencies/parser/circom-files-visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {

export enum ErrorType {
SignalDimensionResolution,
TemplateAlreadyUsed,
TemplateAlreadyVisited,
InvalidPragmaVersion,
FailedToResolveMainComponentParameter,
InternalExpressionHelperError,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/cache
/artifacts

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

include "templates/SomeCircuit.circom";

component main {public [in1]} = SomeCircuit([5, 3, 2, 5], 3);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pragma circom 2.1.6;

template SomeCircuit(p1, p2){
signal input in1;
signal input in2[p1[1] * p1[0]][p1[3] * p1[2] * p2];

signal output out <== in1 * in2[0][0];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { HardhatUserConfig } from "hardhat/config";

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

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

export default defaultConfig;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "hardhat-project-with-circuits-main-component",
"version": "1.0.0",
"scripts": {
"compile": "hardhat compile --force",
"clean": "hardhat clean && rm -rf artifacts && rm -rf cache"
}
}
29 changes: 28 additions & 1 deletion test/unit/core/dependencies/file-parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import { getNormalizedFullPath } from "@src/utils/path-utils";
import { CIRCUITS_COMPILE_CACHE_FILENAME } from "@src/constants";
import { createCircuitsCompileCache } from "@src/cache";
import { createReporter } from "@src/reporter";
import { ResolvedFileData } from "@src/types/core";
import { CircomResolvedFile, ResolvedFileData } from "@src/types/core";
import { getCircomParser, VariableContext } from "@distributedlab/circom-parser";
import { BaseCacheType } from "@src/types/cache/base-cache";

describe("CircomFilesParser", () => {
describe("parse", () => {
Expand Down Expand Up @@ -120,6 +121,32 @@ describe("CircomFilesParser", () => {
});
});

describe("parse with resolution of main component", () => {
useEnvironment("with-circuits-main-component");

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

it("should correctly resolve the arguments for the main component", async function () {
const circuitPath = getNormalizedFullPath(this.hre.config.paths.root, "circuits/base/SomeCircuit.circom");
// eslint-disable-next-line @typescript-eslint/no-var-requires
const cache: BaseCacheType<CircomResolvedFile> = require(
getNormalizedFullPath(this.hre.config.paths.root, "cache/circuits-compile-cache.json"),
);

const signals = cache.files[circuitPath].fileData.mainComponentData!.signals;

const signalIn1 = signals.find((signal) => signal.name === "in1")!;
const signalIn2 = signals.find((signal) => signal.name === "in2")!;
const signalOut = signals.find((signal) => signal.name === "out")!;

expect(signalIn1.dimension).to.deep.equal([]);
expect(signalIn2.dimension).to.deep.equal([15, 30]);
expect(signalOut.dimension).to.deep.equal([]);
});
});

describe("invalid parse", () => {
useEnvironment("with-complex-circuits");

Expand Down
Loading