Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@solarity/zktype

dl-solarity4.8kMIT0.4.6TypeScript support: included

Unleash TypeScript bindings for Circom circuits

distributedlab, solarity, circom, zkp

readme

npm License: MIT

ZKType - TypeScript bindings for Circom circuits

ZKType simplifies and makes user-friendly the process of working with Circom circuits.

  • Generate a typed zkit wrapper for given circuits.
  • Support for groth16 and plonk proving systems.
  • Ensure that all inputs and proofs are correctly formatted.
  • Typization for witness signals substitution.

Installation

To install the package, run:

npm install --save-dev @solarity/zktype

Usage

[!IMPORTANT] ZKType is not meant to be used directly as its fitness relies heavily on the environment, e.g., on Circom compilation artifacts management. Consider using hardhat-zkit, which is a complete, developer-friendly package.

CircuitTypesGenerator

CircuitTypesGenerator is an entry point for generating TypeScript bindings for a given circuit.

To create a CircuitTypesGenerator object, it is necessary to pass a config:

ZKTypeConfig = {
  basePath: "circuits",
  projectRoot: process.cwd(),
  circuitsArtifacts: [
    {
      artifactPath: "circuits/auth/Matrix_artifacts.json",
      circuitProtocolType: ["groth16"],
    },
  ],
  outputTypesDir: "generated-types/circuits", 
  signalNamesTypeLimit: 50000,
}

This config contains all the information required to generate TypeScript bindings for given circuits.

  • basePath - Path to the root directory of the project where circuits are stored.
  • projectRoot - Absolute path to the root directory of the project.
  • 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.
  • signalNamesTypeLimit - Maximum number of signals allowed to generate a SignalNames union type for the circuit.
    • Optional. Default: 50000.

API reference


  • async generateTypes()

Generates TypeScript bindings for the given circuits, based on the provided config.

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.

  • async getCircuitObject(circuitName: string, protocolType?: string): Promise<any>

Returns the constructible object for the given circuit.

const generator = new CircuitTypesGenerator(config);

await generator.generateTypes();

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

The package supports generation of circuits by the groth16 and plonk protocols at the same time. In this case, you will have to specify the protocol type when retrieving the object.

After the circuitObject is retrieved, check out the zkit documentation to see how to work with it.

To ensure that the object can be imported, check the hardhat.d.ts file.

changelog

Changelog

[v0.4.4]

  • Changed the Calldata type in circuits from an array to a struct

[v0.4.3]

  • Renamed utils file to helpers because of a path bug on Windows

[v0.4.2]

  • Fixed redundant protocol type generation in the hardhat.config.ts

[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
  • Removed all code related to artifact generation and circuit compilation

[v0.2.7]

  • Improved error messages during the artifact extraction process

[v0.2.4-v0.2.6]

  • Added ability to recover after error during artifact extraction
  • Fixed bug with the linearization of public signals
  • Changed Public signals type to NumberLike
  • Added calculateWitness method to the wrapper class

[v0.2.2-v0.2.3]

  • Fixed bug during the package publishing process
  • Added generation of custom Calldata type to resolve type conflicts when interacting with verifier contracts

[v0.2.1]

  • Fixed incorrect export of CircuitTypesGenerator class
  • Added an ability to set project root path in ZKTypeConfig

[v0.2.0]

  • Resolved an issue where inputs could have the wrong number of dimensions, such as bigint[] when bigint[][] was expected.
  • Refactored architecture to work as a library
    • Deleted support for standalone run
    • Added ability to specify where the circuit's AST files are stored
    • Added ability to specify where to put generated artifacts and types
  • Implemented Zkit wrapper generation for given circuits
  • Used EJS instead of TS factory to render the wrapper class
  • Added generation of the Hardhat runtime extension file for circuits
  • Added a helper function to return the circuit object from a given name
  • Changed the ArtifactGeneratorConfig to accept an array of paths to circuit ASTs.
  • Deleted automatic artifacts clean up.

[v0.1.1]

Added

  • Initial release of the ZKType package
  • Support for generating TypeScript bindings for Circom circuits