From 8b07547fbf38302d2398d5e9a95e13bb9843e8d5 Mon Sep 17 00:00:00 2001 From: Kyryl R Date: Tue, 5 Nov 2024 13:40:49 +0200 Subject: [PATCH 1/6] Added an ability to provide protocol type in the ZKType config structure --- src/core/CircuitTypesGenerator.ts | 9 ++++-- src/core/ZkitTSGenerator.ts | 2 +- src/types/circuitArtifact.ts | 3 +- src/types/config.ts | 11 +++++-- test/CircuitProofGeneration.test.ts | 30 +++++++++++++---- test/CircuitTypesGenerator.test.ts | 30 +++++++++++++---- ...AtomicQueryMTPOnChainVoting_artifacts.json | 1 - test/fixture-cache/Multiplier2_artifacts.json | 1 - .../auth/EnhancedMultiplier_artifacts.json | 1 - .../auth/Multiplier2_artifacts.json | 1 - .../lib/Multiplier2_artifacts.json | 1 - test/helpers/index.ts | 32 +++++++++++++++---- 12 files changed, 92 insertions(+), 30 deletions(-) diff --git a/src/core/CircuitTypesGenerator.ts b/src/core/CircuitTypesGenerator.ts index bceb80e..f237767 100644 --- a/src/core/CircuitTypesGenerator.ts +++ b/src/core/CircuitTypesGenerator.ts @@ -303,13 +303,18 @@ export class CircuitTypesGenerator extends ZkitTSGenerator { const artifacts: CircuitArtifact[] = []; for (const file of this._zktypeConfig.circuitsArtifactsPaths) { - const filePath = file.toString(); + const filePath = file.artifactPath.toString(); if (!path.extname(filePath) || !path.extname(filePath).includes(".json")) { continue; } - artifacts.push(JSON.parse(fs.readFileSync(path.join(this._projectRoot, filePath), "utf-8"))); + const artifactStructure: CircuitArtifact = JSON.parse( + fs.readFileSync(path.join(this._projectRoot, filePath), "utf-8"), + ); + artifactStructure.baseCircuitInfo.protocol = file.circuitProtocolType; + + artifacts.push(artifactStructure); } return artifacts; diff --git a/src/core/ZkitTSGenerator.ts b/src/core/ZkitTSGenerator.ts index 3ebfd7f..3a548d9 100644 --- a/src/core/ZkitTSGenerator.ts +++ b/src/core/ZkitTSGenerator.ts @@ -239,7 +239,7 @@ export default class ZkitTSGenerator extends BaseTSGenerator { private _getUnifiedProtocolType(circuitArtifact: CircuitArtifact): Set { if (!circuitArtifact.baseCircuitInfo.protocol) { - return new Set(["groth16"]); + throw new Error(`INTERNAL ERROR. Open a bug report please!`); } return new Set(circuitArtifact.baseCircuitInfo.protocol); diff --git a/src/types/circuitArtifact.ts b/src/types/circuitArtifact.ts index 9fa042d..0c46360 100644 --- a/src/types/circuitArtifact.ts +++ b/src/types/circuitArtifact.ts @@ -28,11 +28,12 @@ export type CircuitArtifact = { * Represents the base circuit information. * * @param {string} protocol - The proving system protocol used in the circuit. + * Set by the package itself from provided constructor arguments. * @param {number} constraintsNumber - The number of constraints in the circuit. * @param {SignalInfo[]} signals - The array of `input` and `output` signals used in the circuit. */ export type BaseCircuitInfo = { - protocol: ["groth16" | "plonk"]; + protocol?: ProtocolType[]; constraintsNumber: number; signals: SignalInfo[]; }; diff --git a/src/types/config.ts b/src/types/config.ts index 2155761..5452c28 100644 --- a/src/types/config.ts +++ b/src/types/config.ts @@ -1,3 +1,10 @@ +import { ProtocolType } from "./circuitArtifact"; + +export interface CircuitArtifactData { + artifactPath: string; + circuitProtocolType: ProtocolType[]; +} + export interface ZKTypeConfig { /** * The path to the directory where the generated types will be stored. @@ -10,9 +17,9 @@ export interface ZKTypeConfig { basePath: string; /** - * An array of paths to all circuit artifacts. + * An array of object containing the path to the circuit artifact and the protocol type of the circuit. */ - circuitsArtifactsPaths: string[]; + circuitsArtifactsPaths: CircuitArtifactData[]; /** * The absolute path to the root directory of the project. diff --git a/test/CircuitProofGeneration.test.ts b/test/CircuitProofGeneration.test.ts index 63a9083..1aa3e69 100644 --- a/test/CircuitProofGeneration.test.ts +++ b/test/CircuitProofGeneration.test.ts @@ -10,12 +10,30 @@ describe("Circuit Proof Generation", function () { basePath: "test/fixture", projectRoot: findProjectRoot(process.cwd()), circuitsArtifactsPaths: [ - "test/fixture-cache/auth/EnhancedMultiplier_artifacts.json", - "test/fixture-cache/auth/Matrix_artifacts.json", - "test/fixture-cache/auth/Multiplier2_artifacts.json", - "test/fixture-cache/lib/Multiplier2_artifacts.json", - "test/fixture-cache/CredentialAtomicQueryMTPOnChainVoting_artifacts.json", - "test/fixture-cache/Multiplier2_artifacts.json", + { + artifactPath: "test/fixture-cache/auth/EnhancedMultiplier_artifacts.json", + circuitProtocolType: ["groth16"], + }, + { + artifactPath: "test/fixture-cache/auth/Matrix_artifacts.json", + circuitProtocolType: ["groth16"], + }, + { + artifactPath: "test/fixture-cache/auth/Multiplier2_artifacts.json", + circuitProtocolType: ["plonk"], + }, + { + artifactPath: "test/fixture-cache/lib/Multiplier2_artifacts.json", + circuitProtocolType: ["groth16", "groth16", "plonk"], + }, + { + artifactPath: "test/fixture-cache/CredentialAtomicQueryMTPOnChainVoting_artifacts.json", + circuitProtocolType: ["groth16", "plonk"], + }, + { + artifactPath: "test/fixture-cache/Multiplier2_artifacts.json", + circuitProtocolType: ["groth16", "plonk"], + }, ], }); diff --git a/test/CircuitTypesGenerator.test.ts b/test/CircuitTypesGenerator.test.ts index 2abc484..19984d4 100644 --- a/test/CircuitTypesGenerator.test.ts +++ b/test/CircuitTypesGenerator.test.ts @@ -26,12 +26,30 @@ describe("Circuit Types Generation", function () { basePath: "circuits/fixture", projectRoot: findProjectRoot(process.cwd()), circuitsArtifactsPaths: [ - "test/fixture-cache/auth/EnhancedMultiplier_artifacts.json", - "test/fixture-cache/auth/Matrix_artifacts.json", - "test/fixture-cache/auth/Multiplier2_artifacts.json", - "test/fixture-cache/lib/Multiplier2_artifacts.json", - "test/fixture-cache/CredentialAtomicQueryMTPOnChainVoting_artifacts.json", - "test/fixture-cache/Multiplier2_artifacts.json", + { + artifactPath: "test/fixture-cache/auth/EnhancedMultiplier_artifacts.json", + circuitProtocolType: ["groth16"], + }, + { + artifactPath: "test/fixture-cache/auth/Matrix_artifacts.json", + circuitProtocolType: ["groth16"], + }, + { + artifactPath: "test/fixture-cache/auth/Multiplier2_artifacts.json", + circuitProtocolType: ["plonk"], + }, + { + artifactPath: "test/fixture-cache/lib/Multiplier2_artifacts.json", + circuitProtocolType: ["groth16", "groth16", "plonk"], + }, + { + artifactPath: "test/fixture-cache/CredentialAtomicQueryMTPOnChainVoting_artifacts.json", + circuitProtocolType: ["groth16", "plonk"], + }, + { + artifactPath: "test/fixture-cache/Multiplier2_artifacts.json", + circuitProtocolType: ["groth16", "plonk"], + }, ], }); diff --git a/test/fixture-cache/CredentialAtomicQueryMTPOnChainVoting_artifacts.json b/test/fixture-cache/CredentialAtomicQueryMTPOnChainVoting_artifacts.json index 5afb57d..497f991 100644 --- a/test/fixture-cache/CredentialAtomicQueryMTPOnChainVoting_artifacts.json +++ b/test/fixture-cache/CredentialAtomicQueryMTPOnChainVoting_artifacts.json @@ -5,7 +5,6 @@ "circuitSourceName": "circuits/fixture/credentialAtomicQueryMTPV2OnChainVoting.circom", "baseCircuitInfo": { "constraintsNumber": 86791, - "protocol": ["groth16", "plonk"], "signals": [ { "name": "merklized", diff --git a/test/fixture-cache/Multiplier2_artifacts.json b/test/fixture-cache/Multiplier2_artifacts.json index 5913629..57d1e55 100644 --- a/test/fixture-cache/Multiplier2_artifacts.json +++ b/test/fixture-cache/Multiplier2_artifacts.json @@ -5,7 +5,6 @@ "circuitSourceName": "circuits/fixture/Basic.circom", "baseCircuitInfo": { "constraintsNumber": 1, - "protocol": ["groth16", "plonk"], "signals": [ { "name": "in1", diff --git a/test/fixture-cache/auth/EnhancedMultiplier_artifacts.json b/test/fixture-cache/auth/EnhancedMultiplier_artifacts.json index 739c12d..48bcef1 100644 --- a/test/fixture-cache/auth/EnhancedMultiplier_artifacts.json +++ b/test/fixture-cache/auth/EnhancedMultiplier_artifacts.json @@ -5,7 +5,6 @@ "circuitSourceName": "circuits/fixture/auth/EMultiplier.circom", "baseCircuitInfo": { "constraintsNumber": 1, - "protocol": ["groth16"], "signals": [ { "name": "in1", diff --git a/test/fixture-cache/auth/Multiplier2_artifacts.json b/test/fixture-cache/auth/Multiplier2_artifacts.json index 79f8a6a..1c40176 100644 --- a/test/fixture-cache/auth/Multiplier2_artifacts.json +++ b/test/fixture-cache/auth/Multiplier2_artifacts.json @@ -5,7 +5,6 @@ "circuitSourceName": "circuits/fixture/auth/BasicInAuth.circom", "baseCircuitInfo": { "constraintsNumber": 1, - "protocol": ["plonk"], "signals": [ { "name": "in1", diff --git a/test/fixture-cache/lib/Multiplier2_artifacts.json b/test/fixture-cache/lib/Multiplier2_artifacts.json index 32c1888..1e143af 100644 --- a/test/fixture-cache/lib/Multiplier2_artifacts.json +++ b/test/fixture-cache/lib/Multiplier2_artifacts.json @@ -5,7 +5,6 @@ "circuitSourceName": "circuits/fixture/lib/BasicInLib.circom", "baseCircuitInfo": { "constraintsNumber": 1, - "protocol": ["groth16", "groth16", "plonk"], "signals": [ { "name": "in1", diff --git a/test/helpers/index.ts b/test/helpers/index.ts index 79bb636..6162aa5 100644 --- a/test/helpers/index.ts +++ b/test/helpers/index.ts @@ -5,13 +5,31 @@ const circuitTypesGenerator = new CircuitTypesGenerator({ basePath: "circuits/fixture", projectRoot: findProjectRoot(process.cwd()), circuitsArtifactsPaths: [ - "test/fixture-cache/auth/EnhancedMultiplier_artifacts.json", - "test/fixture-cache/auth/Matrix_artifacts.json", - "test/fixture-cache/auth/Multiplier2_artifacts.json", - "test/fixture-cache/lib/Multiplier2_artifacts.json", - "test/fixture-cache/CredentialAtomicQueryMTPOnChainVoting_artifacts.json", - "test/fixture-cache/Multiplier2_artifacts.json", + { + artifactPath: "test/fixture-cache/auth/EnhancedMultiplier_artifacts.json", + circuitProtocolType: ["groth16"], + }, + { + artifactPath: "test/fixture-cache/auth/Matrix_artifacts.json", + circuitProtocolType: ["groth16"], + }, + { + artifactPath: "test/fixture-cache/auth/Multiplier2_artifacts.json", + circuitProtocolType: ["plonk"], + }, + { + artifactPath: "test/fixture-cache/lib/Multiplier2_artifacts.json", + circuitProtocolType: ["groth16", "groth16", "plonk"], + }, + { + artifactPath: "test/fixture-cache/CredentialAtomicQueryMTPOnChainVoting_artifacts.json", + circuitProtocolType: ["groth16", "plonk"], + }, + { + artifactPath: "test/fixture-cache/Multiplier2_artifacts.json", + circuitProtocolType: ["groth16", "plonk"], + }, ], }); -// circuitTypesGenerator.generateTypes().then(console.log).catch(console.error); +circuitTypesGenerator.generateTypes().then(console.log).catch(console.error); From 8d90f38d6acd3d88b17715ed1edb14f1db18987b Mon Sep 17 00:00:00 2001 From: Kyryl R Date: Tue, 5 Nov 2024 14:03:12 +0200 Subject: [PATCH 2/6] Updated versions --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 55ad498..992ca90 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@solarity/zktype", - "version": "0.4.0", + "version": "0.4.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@solarity/zktype", - "version": "0.4.0", + "version": "0.4.1", "license": "MIT", "dependencies": { "ejs": "3.1.10", diff --git a/package.json b/package.json index 3142bc7..3c57158 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@solarity/zktype", - "version": "0.4.0", + "version": "0.4.1", "description": "Unleash TypeScript bindings for Circom circuits", "main": "dist/index.js", "types": "dist/index.d.ts", From f9bb576251c62edac897e424effb75c0d5d39ca9 Mon Sep 17 00:00:00 2001 From: Kyryl R Date: Tue, 5 Nov 2024 14:04:03 +0200 Subject: [PATCH 3/6] Updated name in ZKTypeConfig --- src/config.ts | 2 +- src/core/CircuitTypesGenerator.ts | 2 +- src/types/config.ts | 2 +- test/CircuitProofGeneration.test.ts | 2 +- test/CircuitTypesGenerator.test.ts | 2 +- test/helpers/index.ts | 4 ++-- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/config.ts b/src/config.ts index 9f0410d..1a81726 100644 --- a/src/config.ts +++ b/src/config.ts @@ -3,6 +3,6 @@ import { ZKTypeConfig } from "./types"; export const defaultCircuitArtifactGeneratorConfig: ZKTypeConfig = { basePath: "circuits", projectRoot: process.cwd(), - circuitsArtifactsPaths: [], + circuitsArtifacts: [], outputTypesDir: "generated-types/circuits", }; diff --git a/src/core/CircuitTypesGenerator.ts b/src/core/CircuitTypesGenerator.ts index f237767..8b96823 100644 --- a/src/core/CircuitTypesGenerator.ts +++ b/src/core/CircuitTypesGenerator.ts @@ -302,7 +302,7 @@ export class CircuitTypesGenerator extends ZkitTSGenerator { private _fetchCircuitArtifacts(): CircuitArtifact[] { const artifacts: CircuitArtifact[] = []; - for (const file of this._zktypeConfig.circuitsArtifactsPaths) { + for (const file of this._zktypeConfig.circuitsArtifacts) { const filePath = file.artifactPath.toString(); if (!path.extname(filePath) || !path.extname(filePath).includes(".json")) { diff --git a/src/types/config.ts b/src/types/config.ts index 5452c28..d6182e7 100644 --- a/src/types/config.ts +++ b/src/types/config.ts @@ -19,7 +19,7 @@ export interface ZKTypeConfig { /** * An array of object containing the path to the circuit artifact and the protocol type of the circuit. */ - circuitsArtifactsPaths: CircuitArtifactData[]; + circuitsArtifacts: CircuitArtifactData[]; /** * The absolute path to the root directory of the project. diff --git a/test/CircuitProofGeneration.test.ts b/test/CircuitProofGeneration.test.ts index 1aa3e69..f40b01c 100644 --- a/test/CircuitProofGeneration.test.ts +++ b/test/CircuitProofGeneration.test.ts @@ -9,7 +9,7 @@ describe("Circuit Proof Generation", function () { const circuitTypesGenerator = new CircuitTypesGenerator({ basePath: "test/fixture", projectRoot: findProjectRoot(process.cwd()), - circuitsArtifactsPaths: [ + circuitsArtifacts: [ { artifactPath: "test/fixture-cache/auth/EnhancedMultiplier_artifacts.json", circuitProtocolType: ["groth16"], diff --git a/test/CircuitTypesGenerator.test.ts b/test/CircuitTypesGenerator.test.ts index 19984d4..439f514 100644 --- a/test/CircuitTypesGenerator.test.ts +++ b/test/CircuitTypesGenerator.test.ts @@ -25,7 +25,7 @@ describe("Circuit Types Generation", function () { const circuitTypesGenerator = new CircuitTypesGenerator({ basePath: "circuits/fixture", projectRoot: findProjectRoot(process.cwd()), - circuitsArtifactsPaths: [ + circuitsArtifacts: [ { artifactPath: "test/fixture-cache/auth/EnhancedMultiplier_artifacts.json", circuitProtocolType: ["groth16"], diff --git a/test/helpers/index.ts b/test/helpers/index.ts index 6162aa5..11bccbe 100644 --- a/test/helpers/index.ts +++ b/test/helpers/index.ts @@ -4,7 +4,7 @@ import { findProjectRoot } from "../../src/utils"; const circuitTypesGenerator = new CircuitTypesGenerator({ basePath: "circuits/fixture", projectRoot: findProjectRoot(process.cwd()), - circuitsArtifactsPaths: [ + circuitsArtifacts: [ { artifactPath: "test/fixture-cache/auth/EnhancedMultiplier_artifacts.json", circuitProtocolType: ["groth16"], @@ -32,4 +32,4 @@ const circuitTypesGenerator = new CircuitTypesGenerator({ ], }); -circuitTypesGenerator.generateTypes().then(console.log).catch(console.error); +// circuitTypesGenerator.generateTypes().then(console.log).catch(console.error); From fad759b86a40f3bc3c11a1c93208dd3e22ffaada Mon Sep 17 00:00:00 2001 From: Kyryl R Date: Tue, 5 Nov 2024 14:32:41 +0200 Subject: [PATCH 4/6] Updated README --- README.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 936d7b7..227e24c 100644 --- a/README.md +++ b/README.md @@ -31,8 +31,11 @@ To create a `CircuitTypesGenerator` object, it is necessary to pass a config: ZKTypeConfig = { basePath: "circuits", projectRoot: process.cwd(), - circuitsArtifactsPaths: [ - "circuits/auth/Matrix_artifacts.json", + circuitsArtifacts: [ + { + artifactPath: "circuits/auth/Matrix_artifacts.json", + circuitProtocolType: ["groth16"], + }, ], outputTypesDir: "generated-types/circuits", } @@ -42,7 +45,7 @@ This config contains all the information required to generate TypeScript binding - `basePath` - Path to the root directory of the project where circuits are stored. - `projectRoot` - Absolute path to the root directory of the project. -- `circuitsArtifactsPaths` - Array of paths to the circuits' artifact files. +- `circuitsArtifacts` - Array of object containing the path to the circuit artifact and the protocol type of the circuit. - `outputTypesDir` - Path to the directory where the generated types will be stored. - Optional. Default: `generated-types/circuits`. From 708e7c81d23b591f2b9d6f026f384e42060b555d Mon Sep 17 00:00:00 2001 From: Kyryl R Date: Tue, 5 Nov 2024 14:36:01 +0200 Subject: [PATCH 5/6] Updated CHANGELOG.md --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b82348..3192271 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## [v0.3.1-v0.4.1] + +* Added an ability to provide protocol type in the ZKType config structure (#17) +* Added support of circuits generation for the `plonk` protocol (#14) (#15) +* Fixed types resolution in utils.ts (#13) + ## [v0.3.0] * Switched to the use of custom artifacts generated by the hardhat-zkit package From 6000b0021e8927b7e2cd7737c4fc3a012a88c90e Mon Sep 17 00:00:00 2001 From: Artem Chystiakov Date: Tue, 5 Nov 2024 14:39:37 +0200 Subject: [PATCH 6/6] readme style --- README.md | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 227e24c..6716fbe 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ **ZKType simplifies and makes user-friendly the process of working with Circom circuits.** - Generate a [zkit](https://github.com/dl-solarity/zkit) wrapper for given circuits. +- Support for `groth16` and `plonk` proving systems. - Ensure that all inputs and proofs are correctly formatted. ## Installation @@ -49,24 +50,31 @@ This config contains all the information required to generate TypeScript binding - `outputTypesDir` - Path to the directory where the generated types will be stored. - Optional. Default: `generated-types/circuits`. -#### generateTypes() +#### API reference + +--- + +- **`async generateTypes()`** Generates TypeScript bindings for the given circuits, based on the provided config. ```typescript const generator = new CircuitTypesGenerator(config); + await generator.generateTypes(); ``` Also, this function generates the `hardhat.d.ts` file, where you can find all the possible objects that can be retrieved by the function below. -#### getCircuitObject(circuitName: string, protocolType?: string): Promise +- **`async getCircuitObject(circuitName: string, protocolType?: string): Promise`** Returns the constructible object for the given circuit. ```typescript const generator = new CircuitTypesGenerator(config); + await generator.generateTypes(); + const circuitObject = await generator.getCircuitObject("MyCircuit", "groth16"); ```