3.0.2 • Published 6 months ago

@keyringnetwork/circuits v3.0.2

Weekly downloads
-
License
-
Repository
-
Last release
6 months ago

Keyring Circuits

Description

Contained in this repository are the zero knowledge circuit descriptions, and Typescript integration code for the Keyring protocol.

Layout

Source Controlled

  • circuits - The Circom zero knowledge circuit descriptions
  • src - Typescript integration code
  • types - Typescript type definitions
  • test - Test suite
  • scripts - Helper scripts for building the circuits, test data, and testing regime keys

Generated

  • dist/keyring, dist/index.* - The distributable Javascript implementation
  • dist/circuits - The distributable zero knowledge circuits artifacts
  • dist/proof - Test proofs
  • dist/test_wallets - Test wallet keys
  • dist/testdata.py - Test data for the Keyring SDK

Installation

Build the distributable Javascript implementation, which is also needed for building the circuits and test data

npm install
npm run build
npm run rollup

Run Test Suite

Each circuit-oriented test builds both the unoptimized and optimized versions of the circuit and displays the constraint counts of each.

npm test

Usage

This package exposes a command-line interface which automates the process of building, setting up, and exporting the circuit artifacts. The primary entrypoint to this is the npm run circuit command. Take care to specify the -- argument separator when passing flag arguments to the command.

Circuit Configuration

The circuits are configured in config.json, primarily through the top-level constants dictionary. The constants are made available to the circuits through a Circom import named constants.circom, allowing global configuration to be applied throughout the whole circuits suite, instead of passed as arguments.

Individual circuits are defined in the top-level circuits dictionary, with the key being the name of the circuit, and the value being a dictionary of configuration options for that circuit. Take special care that the public outputs and inputs are defined in the same order as the circuit defines them, as this will affect the generation of the verifier and witness integration.

Building Circuits

Circuits are built using the circuit build command. This command executes the circom compiler, applies the phase 1 powers of tau to the resulting r1cs, and writes all of the needed circuit artifacts to the dist/circuits directory.

npm run circuit build <circuit name> -- --write

Setting up Circuits

snarkjs zkey contribute dist/circuits/Authorisation.zKey dist/circuits/Authorisation.001.zKey --name="<Your Name> <Your Email>" -v

# Obtain additional contributions from other parties

# A random beacon is optional, but serves as a useful indication of the finalization of the phase 2 setup
snarkjs zkey beacon dist/circuits/Authorisation.<number>.zKey dist/circuits/Authorisation.zKey 0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f 10 -n="Final Beacon phase2"

Build Verifiers

The verifier for a circuit is either output as a templated solidity contract, or as a binary blob to be deployed on-chain, to be read by the next-gen "heap" verifier. The verifier command outputs the JSON verifying key used by snarkjs, and then outputs either the solidity contract or binary blob depending on the USE_HEAP configuration for the circuit.

npm run circuit verifier Authorisation

Generating Test Data

These three commands generate a set of test wallet keys, and corresponding proofs to be used in testing the aggregator and smart contracts. The build-proofs script is particularly important for generating all of the proof fixtures for the test suites of other application components. If you're looking for the canonical steps for front-end proof generation, look there.

npm run build-testwallets
npm run build-proofs
npm run build-sdktestdata

Generating a Backdoor Regime Key

There are helper scripts for generating a backdoor regime key and testing the encryption of a point to that key, and decryption of that encrypted point.

npm run generate-regime-key
npm run encrypt-to-regime <compressed public key> <compressed message point>
npm run decrypt-backdoor <private key> <compressed c1> <compressed c2>