Skip to content

Commit

Permalink
Add an ability to provide protocol type in the ZKType config structure (
Browse files Browse the repository at this point in the history
#17)

* Added an ability to provide protocol type in the ZKType config structure

* Updated versions

* Updated name in ZKTypeConfig

* Updated README

* Updated CHANGELOG.md

* readme style

---------

Co-authored-by: Artem Chystiakov <artem.ch31@gmail.com>
  • Loading branch information
KyrylR and Arvolear authored Nov 5, 2024
1 parent 144dee8 commit a68981e
Show file tree
Hide file tree
Showing 17 changed files with 121 additions and 42 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
21 changes: 16 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
}
Expand All @@ -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<any>
- **`async getCircuitObject(circuitName: string, protocolType?: string): Promise<any>`**

Returns the constructible object for the given circuit.

```typescript
const generator = new CircuitTypesGenerator(config);

await generator.generateTypes();

const circuitObject = await generator.getCircuitObject("MyCircuit", "groth16");
```

Expand Down
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

0 comments on commit a68981e

Please sign in to comment.