Skip to content

Commit 902925f

Browse files
committed
Added support for Plonk Protocol per circuit
1 parent 08c4b88 commit 902925f

18 files changed

+88
-14
lines changed

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@solarity/zktype",
3-
"version": "0.3.1",
3+
"version": "0.4.0",
44
"description": "Unleash TypeScript bindings for Circom circuits",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
@@ -49,7 +49,7 @@
4949
"typescript": "5.5.4"
5050
},
5151
"peerDependencies": {
52-
"@solarity/zkit": "^0.2.4"
52+
"@solarity/zkit": "^0.3.0-rc.0"
5353
},
5454
"devDependencies": {
5555
"@types/chai": "^4.3.12",

src/constants/protocol.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const Groth16CalldataPointsType =
2+
"[NumericString, NumericString], [[NumericString, NumericString], [NumericString, NumericString]], [NumericString, NumericString]";
3+
4+
export const PlonkCalldataPointsType = "NumericString[]";

src/core/ZkitTSGenerator.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919

2020
import { normalizeName } from "../utils";
2121
import { SignalTypeNames, SignalVisibilityNames } from "../constants";
22+
import { Groth16CalldataPointsType, PlonkCalldataPointsType } from "../constants/protocol";
2223

2324
export default class ZkitTSGenerator extends BaseTSGenerator {
2425
protected async _genHardhatZkitTypeExtension(circuits: {
@@ -70,6 +71,8 @@ export default class ZkitTSGenerator extends BaseTSGenerator {
7071
circuitArtifact: CircuitArtifact,
7172
pathToGeneratedFile: string,
7273
): Promise<string> {
74+
this._validateCircuitArtifact(circuitArtifact);
75+
7376
const template = fs.readFileSync(path.join(__dirname, "templates", "circuit-wrapper.ts.ejs"), "utf8");
7477

7578
let outputCounter: number = 0;
@@ -114,11 +117,15 @@ export default class ZkitTSGenerator extends BaseTSGenerator {
114117

115118
const pathToUtils = path.join(this.getOutputTypesDir(), "utils");
116119
const templateParams: WrapperTemplateParams = {
120+
protocolTypeName: circuitArtifact.baseCircuitInfo.protocol,
121+
protocolImplementerName: this._getProtocolImplementerName(circuitArtifact),
122+
proofTypeInternalName: this._getProofTypeInternalName(circuitArtifact),
117123
circuitClassName: this._getCircuitName(circuitArtifact),
118124
publicInputsTypeName: this._getTypeName(circuitArtifact, "Public"),
119125
calldataPubSignalsType: this._getCalldataPubSignalsType(calldataPubSignalsCount),
120126
publicInputs,
121127
privateInputs,
128+
calldataPointsType: this._getCalldataPointsType(circuitArtifact),
122129
proofTypeName: this._getTypeName(circuitArtifact, "Proof"),
123130
privateInputsTypeName: this._getTypeName(circuitArtifact, "Private"),
124131
pathToUtils: path.relative(path.dirname(pathToGeneratedFile), pathToUtils),
@@ -150,4 +157,43 @@ export default class ZkitTSGenerator extends BaseTSGenerator {
150157

151158
return signal.dimension.reduce((acc: number, dim: string) => acc * Number(dim), 1);
152159
}
160+
161+
private _getProtocolImplementerName(circuitArtifact: CircuitArtifact): any {
162+
switch (circuitArtifact.baseCircuitInfo.protocol) {
163+
case "groth16":
164+
return "Groth16Implementer";
165+
case "plonk":
166+
return "PlonkImplementer";
167+
default:
168+
throw new Error(`Unknown protocol: ${circuitArtifact.baseCircuitInfo.protocol}`);
169+
}
170+
}
171+
172+
private _getProofTypeInternalName(circuitArtifact: CircuitArtifact): any {
173+
switch (circuitArtifact.baseCircuitInfo.protocol) {
174+
case "groth16":
175+
return "Groth16Proof";
176+
case "plonk":
177+
return "PlonkProof";
178+
default:
179+
throw new Error(`Unknown protocol: ${circuitArtifact.baseCircuitInfo.protocol}`);
180+
}
181+
}
182+
183+
private _getCalldataPointsType(circuitArtifact: CircuitArtifact): any {
184+
switch (circuitArtifact.baseCircuitInfo.protocol) {
185+
case "groth16":
186+
return Groth16CalldataPointsType;
187+
case "plonk":
188+
return PlonkCalldataPointsType;
189+
default:
190+
throw new Error(`Unknown protocol: ${circuitArtifact.baseCircuitInfo.protocol}`);
191+
}
192+
}
193+
194+
private _validateCircuitArtifact(circuitArtifact: CircuitArtifact): void {
195+
if (!circuitArtifact.baseCircuitInfo.protocol) {
196+
throw new Error(`ZKType: Protocol is missing in the circuit artifact: ${circuitArtifact.circuitTemplateName}`);
197+
}
198+
}
153199
}

src/core/templates/circuit-wrapper.ts.ejs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ import {
22
CircuitZKit,
33
CircuitZKitConfig,
44
Groth16Proof,
5+
PlonkProof,
56
NumberLike,
67
NumericString,
78
PublicSignals,
9+
Groth16Implementer,
10+
PlonkImplementer,
811
} from "@solarity/zkit";
912

1013
import { normalizePublicSignals, denormalizePublicSignals } from "<%= pathToUtils %>";
@@ -22,20 +25,18 @@ export type <%= publicInputsTypeName %> = {
2225
}
2326

2427
export type <%= proofTypeName %> = {
25-
proof: Groth16Proof;
28+
proof: <%= proofTypeInternalName %>;
2629
publicSignals: <%= publicInputsTypeName %>;
2730
}
2831

2932
export type Calldata = [
30-
[NumericString, NumericString],
31-
[[NumericString, NumericString], [NumericString, NumericString]],
32-
[NumericString, NumericString],
33+
<%= calldataPointsType %>,
3334
<%= calldataPubSignalsType %>,
3435
];
3536

36-
export class <%= circuitClassName %> extends CircuitZKit {
37+
export class <%= circuitClassName %> extends CircuitZKit<"<%= protocolTypeName %>"> {
3738
constructor(config: CircuitZKitConfig) {
38-
super(config);
39+
super(config, new <%= protocolImplementerName %>());
3940
}
4041

4142
public async generateProof(inputs: <%= privateInputsTypeName %>): Promise<<%= proofTypeName %>> {

src/types/circuitArtifact.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ export type CircuitArtifact = {
2525
/**
2626
* Represents the base circuit information.
2727
*
28+
* @param {string} protocol - The proving system protocol used in the circuit.
2829
* @param {number} constraintsNumber - The number of constraints in the circuit.
2930
* @param {SignalInfo[]} signals - The array of `input` and `output` signals used in the circuit.
3031
*/
3132
export type BaseCircuitInfo = {
33+
protocol: "groth16" | "plonk";
3234
constraintsNumber: number;
3335
signals: SignalInfo[];
3436
};

src/types/typesGenerator.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { CircuitArtifact } from "./circuitArtifact";
2+
import { Groth16CalldataPointsType, PlonkCalldataPointsType } from "../constants/protocol";
23

34
export interface ArtifactWithPath {
45
circuitArtifact: CircuitArtifact;
@@ -16,9 +17,13 @@ export interface DefaultWrapperTemplateParams {
1617
}
1718

1819
export interface WrapperTemplateParams {
20+
protocolTypeName: "groth16" | "plonk";
21+
protocolImplementerName: "Groth16Implementer" | "PlonkImplementer";
22+
proofTypeInternalName: "Groth16Proof" | "PlonkProof";
1923
publicInputsTypeName: string;
2024
privateInputs: Inputs[];
2125
publicInputs: Inputs[];
26+
calldataPointsType: typeof Groth16CalldataPointsType | typeof PlonkCalldataPointsType;
2227
calldataPubSignalsType: string;
2328
proofTypeName: string;
2429
privateInputsTypeName: string;

test/fixture-cache/CredentialAtomicQueryMTPOnChainVoting_artifacts.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"circuitSourceName": "circuits/fixture/credentialAtomicQueryMTPV2OnChainVoting.circom",
66
"baseCircuitInfo": {
77
"constraintsNumber": 86791,
8+
"protocol": "groth16",
89
"signals": [
910
{
1011
"name": "merklized",

test/fixture-cache/Multiplier2_artifacts.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"circuitSourceName": "circuits/fixture/Basic.circom",
66
"baseCircuitInfo": {
77
"constraintsNumber": 1,
8+
"protocol": "groth16",
89
"signals": [
910
{
1011
"name": "in1",

test/fixture-cache/auth/EnhancedMultiplier_artifacts.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"circuitSourceName": "circuits/fixture/auth/EMultiplier.circom",
66
"baseCircuitInfo": {
77
"constraintsNumber": 1,
8+
"protocol": "groth16",
89
"signals": [
910
{
1011
"name": "in1",

test/fixture-cache/auth/Matrix_artifacts.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"circuitSourceName": "circuits/fixture/auth/Matrix.circom",
66
"baseCircuitInfo": {
77
"constraintsNumber": 8,
8+
"protocol": "groth16",
89
"signals": [
910
{
1011
"name": "a",

test/fixture-cache/auth/Multiplier2_artifacts.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"circuitSourceName": "circuits/fixture/auth/BasicInAuth.circom",
66
"baseCircuitInfo": {
77
"constraintsNumber": 1,
8+
"protocol": "groth16",
89
"signals": [
910
{
1011
"name": "in1",

test/fixture-cache/lib/Multiplier2_artifacts.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"circuitSourceName": "circuits/fixture/lib/BasicInLib.circom",
66
"baseCircuitInfo": {
77
"constraintsNumber": 1,
8+
"protocol": "groth16",
89
"signals": [
910
{
1011
"name": "in1",

test/helpers/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { CircuitTypesGenerator } from "../../src";
2+
import { findProjectRoot } from "../../src/utils";
3+
4+
const circuitTypesGenerator = new CircuitTypesGenerator({
5+
basePath: "test/fixture",
6+
projectRoot: findProjectRoot(process.cwd()),
7+
circuitsArtifactsPaths: ["test/fixture-cache/Multiplier2_artifacts.json"],
8+
});
9+
10+
// circuitTypesGenerator.generateTypes().then(console.log).catch(console.error);

0 commit comments

Comments
 (0)