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 support for updated parser #51

Merged
merged 6 commits into from
Nov 1, 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: 7 additions & 6 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@solarity/hardhat-zkit",
"version": "0.4.11",
"version": "0.4.12",
"description": "The ultimate TypeScript environment for Circom development",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
Expand Down Expand Up @@ -58,7 +58,7 @@
"node": ">=16"
},
"dependencies": {
"@distributedlab/circom-parser": "0.1.5",
"@distributedlab/circom-parser": "0.2.0",
"@solarity/zkit": "0.2.6",
"@solarity/zktype": "0.3.1",
"@wasmer/wasi": "0.12.0",
Expand Down
6 changes: 5 additions & 1 deletion src/cache/BaseCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ export class BaseCache<T> {
public async writeToFile(cacheFilePath: string) {
const jsonContent = JSON.stringify(
this._cache,
(_key, value) => {
(key, value) => {
if (key === "context") {
return;
}

if (typeof value === "bigint") {
return { __bigintval__: value.toString() };
}
Expand Down
12 changes: 6 additions & 6 deletions src/cache/schemas/compile-schemas.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { BigIntOrNestedArray } from "@distributedlab/circom-parser";
import { CircomValueType } from "@distributedlab/circom-parser";
import { z } from "zod";

/**
* {@link https://github.com/colinhacks/zod | Zod} schema for defining a recursive type {@link BigIntOrNestedArray}.
* {@link https://github.com/colinhacks/zod | Zod} schema for defining a recursive type {@link CircomValueType}.
*
* This schema allows for either a `BigInt` value or an array that contains
* other `BigInt` values or nested arrays of `BigInt` values, recursively.
*/
export const BigIntOrNestedArraySchema: z.ZodType<BigIntOrNestedArray> = z.lazy(() =>
z.union([z.bigint(), BigIntOrNestedArraySchema.array()]),
export const CircomValueTypeSchema: z.ZodType<CircomValueType> = z.lazy(() =>
z.union([z.bigint(), CircomValueTypeSchema.array()]),
);

export const SignalTypeSchema = z.literal("Input").or(z.literal("Output")).or(z.literal("Intermediate"));
Expand All @@ -35,7 +35,7 @@ export const TemplatesSchema = z.record(z.string(), TemplateSchema);
export const MainComponentSchema = z.object({
templateName: z.union([z.string(), z.null()]),
publicInputs: z.string().array(),
parameters: BigIntOrNestedArraySchema.array(),
parameters: CircomValueTypeSchema.array(),
});

export const ParsedCircomFileDataSchema = z.object({
Expand All @@ -53,7 +53,7 @@ export const SignalInfoSchema = z.object({
});

export const ResolvedMainComponentDataSchema = z.object({
parameters: z.record(z.string(), BigIntOrNestedArraySchema),
parameters: z.record(z.string(), CircomValueTypeSchema),
signals: SignalInfoSchema.array(),
});

Expand Down
2 changes: 1 addition & 1 deletion src/core/dependencies/CircomFilesResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ export class CircomFilesResolver {
);

const parsedInputs = this._parser.parseTemplateInputs(
fileWithTemplate.absolutePath,
fileWithTemplate,
templateName,
mainComponentData.parameters,
);
Expand Down
42 changes: 29 additions & 13 deletions src/core/dependencies/parser/CircomFilesParser.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { getCircomParser, ParserError, BigIntOrNestedArray } from "@distributedlab/circom-parser";
import { CircomValueType, getCircomParser, ParserError } from "@distributedlab/circom-parser";

import { CircomFilesVisitor } from "./CircomFilesVisitor";
import { CircomTemplateInputsVisitor } from "./CircomTemplateInputsVisitor";
import { CircuitsCompileCache } from "../../../cache";
import { Reporter } from "../../../reporter";

import { InputData, ResolvedFileData } from "../../../types/core";
import { VisitorError } from "../parser/VisitorError";
import { CircomResolvedFile, ErrorType, InputData, ResolvedFileData } from "../../../types/core";

/**
* A parser class for handling Circom files and extracting relevant data.
Expand Down Expand Up @@ -48,7 +49,7 @@ export class CircomFilesParser {

const parser = getCircomParser(fileContent);

const circomFilesVisitor = new CircomFilesVisitor();
const circomFilesVisitor = new CircomFilesVisitor(absolutePath);

Reporter!.verboseLog("circom-files-parser", "Parsing '%s' file", [absolutePath]);

Expand All @@ -60,6 +61,17 @@ export class CircomFilesParser {

circomFilesVisitor.visit(context);

const visitorErrors = circomFilesVisitor.errors.filter(
(error) =>
error.type === ErrorType.InvalidPragmaVersion ||
error.type === ErrorType.TemplateAlreadyUsed ||
error.type === ErrorType.FailedToResolveMainComponentParameter,
);

if (visitorErrors.length > 0) {
throw new VisitorError(visitorErrors);
}

this._cache.set(contentHash, { parsedFileData: circomFilesVisitor.fileData });

return { parsedFileData: circomFilesVisitor.fileData };
Expand All @@ -73,29 +85,33 @@ export class CircomFilesParser {
* parsing errors and throws a `ParserError` if any issues are encountered.
* The structured input data associated with the specified template is then returned.
*
* @param absolutePath The absolute path to the Circom file containing the template
* @param circomResolvedFile The resolved Circom file data containing the template to parse
* @param templateName The name of the template whose inputs are being parsed
* @param parameterValues A record of parameter values used for template input resolution
* @returns A structured record of input data for the specified template
* @throws ParserError If any parsing issues occur while processing the template inputs
*/
public parseTemplateInputs(
absolutePath: string,
circomResolvedFile: CircomResolvedFile,
templateName: string,
parameterValues: Record<string, BigIntOrNestedArray>,
parameterValues: Record<string, CircomValueType>,
): Record<string, InputData> {
const parser = getCircomParser(absolutePath);
const circomTemplateInputsVisitor = new CircomTemplateInputsVisitor(
circomResolvedFile.absolutePath,
circomResolvedFile.fileData.parsedFileData.templates[templateName].context,
parameterValues,
);

const circomTemplateInputsVisitor = new CircomTemplateInputsVisitor(templateName, parameterValues);
circomTemplateInputsVisitor.startParse();

const context = parser.circuit();
const visitorErrors = circomTemplateInputsVisitor.errors.filter(
(error) => error.type === ErrorType.SignalDimensionResolution,
);

if (parser.hasAnyErrors()) {
throw new ParserError(parser.getAllErrors());
if (visitorErrors.length > 0) {
throw new VisitorError(visitorErrors);
}

circomTemplateInputsVisitor.visit(context);

return circomTemplateInputsVisitor.templateInputs;
}

Expand Down
Loading
Loading