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 provide protocol type in the ZKType config structure #17

Merged
merged 6 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 2 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
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import { ZKTypeConfig } from "./types";
export const defaultCircuitArtifactGeneratorConfig: ZKTypeConfig = {
basePath: "circuits",
projectRoot: process.cwd(),
circuitsArtifactsPaths: [],
circuitsArtifacts: [],
outputTypesDir: "generated-types/circuits",
};
11 changes: 8 additions & 3 deletions src/core/CircuitTypesGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/core/ZkitTSGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ export default class ZkitTSGenerator extends BaseTSGenerator {

private _getUnifiedProtocolType(circuitArtifact: CircuitArtifact): Set<ProtocolType> {
if (!circuitArtifact.baseCircuitInfo.protocol) {
return new Set(["groth16"]);
throw new Error(`INTERNAL ERROR. Open a bug report please!`);
}

return new Set(circuitArtifact.baseCircuitInfo.protocol);
Expand Down
3 changes: 2 additions & 1 deletion src/types/circuitArtifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
};
Expand Down
11 changes: 9 additions & 2 deletions src/types/config.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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.
Expand Down
32 changes: 25 additions & 7 deletions test/CircuitProofGeneration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
},
],
});

Expand Down
32 changes: 25 additions & 7 deletions test/CircuitTypesGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
},
],
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"circuitSourceName": "circuits/fixture/credentialAtomicQueryMTPV2OnChainVoting.circom",
"baseCircuitInfo": {
"constraintsNumber": 86791,
"protocol": ["groth16", "plonk"],
"signals": [
{
"name": "merklized",
Expand Down
1 change: 0 additions & 1 deletion test/fixture-cache/Multiplier2_artifacts.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"circuitSourceName": "circuits/fixture/Basic.circom",
"baseCircuitInfo": {
"constraintsNumber": 1,
"protocol": ["groth16", "plonk"],
"signals": [
{
"name": "in1",
Expand Down
1 change: 0 additions & 1 deletion test/fixture-cache/auth/EnhancedMultiplier_artifacts.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"circuitSourceName": "circuits/fixture/auth/EMultiplier.circom",
"baseCircuitInfo": {
"constraintsNumber": 1,
"protocol": ["groth16"],
"signals": [
{
"name": "in1",
Expand Down
1 change: 0 additions & 1 deletion test/fixture-cache/auth/Multiplier2_artifacts.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"circuitSourceName": "circuits/fixture/auth/BasicInAuth.circom",
"baseCircuitInfo": {
"constraintsNumber": 1,
"protocol": ["plonk"],
"signals": [
{
"name": "in1",
Expand Down
1 change: 0 additions & 1 deletion test/fixture-cache/lib/Multiplier2_artifacts.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"circuitSourceName": "circuits/fixture/lib/BasicInLib.circom",
"baseCircuitInfo": {
"constraintsNumber": 1,
"protocol": ["groth16", "groth16", "plonk"],
"signals": [
{
"name": "in1",
Expand Down
32 changes: 25 additions & 7 deletions test/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
},
],
});

Expand Down
Loading