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 diff --git a/README.md b/README.md index 936d7b7..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 @@ -31,8 +32,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,28 +46,35 @@ 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`. -#### 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"); ``` 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", 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 bceb80e..8b96823 100644 --- a/src/core/CircuitTypesGenerator.ts +++ b/src/core/CircuitTypesGenerator.ts @@ -302,14 +302,19 @@ export class CircuitTypesGenerator extends ZkitTSGenerator { private _fetchCircuitArtifacts(): CircuitArtifact[] { const artifacts: CircuitArtifact[] = []; - for (const file of this._zktypeConfig.circuitsArtifactsPaths) { - const filePath = file.toString(); + for (const file of this._zktypeConfig.circuitsArtifacts) { + 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..d6182e7 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[]; + 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 63a9083..f40b01c 100644 --- a/test/CircuitProofGeneration.test.ts +++ b/test/CircuitProofGeneration.test.ts @@ -9,13 +9,31 @@ describe("Circuit Proof Generation", function () { const circuitTypesGenerator = new CircuitTypesGenerator({ 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", + circuitsArtifacts: [ + { + 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..439f514 100644 --- a/test/CircuitTypesGenerator.test.ts +++ b/test/CircuitTypesGenerator.test.ts @@ -25,13 +25,31 @@ describe("Circuit Types Generation", function () { 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", + circuitsArtifacts: [ + { + 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..11bccbe 100644 --- a/test/helpers/index.ts +++ b/test/helpers/index.ts @@ -4,13 +4,31 @@ import { findProjectRoot } from "../../src/utils"; 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", + circuitsArtifacts: [ + { + 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"], + }, ], });