Giter Club home page Giter Club logo

zkcircuit's Introduction

zardkat ๐Ÿฑ

A hardhat-circom template to generate zero-knowledge circuits, proofs, and solidity verifiers

Quick Start

Compile the Multiplier2() circuit and verify it against a smart contract verifier

pragma circom 2.0.0;

/*This circuit template checks that c is the multiplication of a and b.*/  

template Multiplier2 () {  

   // Declaration of signals.  
   signal input a;  
   signal input b;  
   signal output c;  

   // Constraints.  
   c <== a * b;  
}
component main = Multiplier2();

Install

npm i

Compile

npx hardhat compile This will generate the out file with circuit intermediaries and geneate the MultiplierVerifier.sol contract

Prove and Deploy

npx hardhat run scripts/deploy.ts This script does 4 things

  1. Deploys the MultiplierVerifier.sol contract
  2. Generates a proof from circuit intermediaries with generateProof()
  3. Generates calldata with generateCallData()
  4. Calls verifyProof() on the verifier contract with calldata

With two commands you can compile a ZKP, generate a proof, deploy a verifier, and verify the proof ๐ŸŽ‰

Configuration

Directory Structure

circuits

โ”œโ”€โ”€ multiplier
โ”‚   โ”œโ”€โ”€ circuit.circom
โ”‚   โ”œโ”€โ”€ input.json
โ”‚   โ””โ”€โ”€ out
โ”‚       โ”œโ”€โ”€ circuit.wasm
โ”‚       โ”œโ”€โ”€ multiplier.r1cs
โ”‚       โ”œโ”€โ”€ multiplier.vkey
โ”‚       โ””โ”€โ”€ multiplier.zkey
โ”œโ”€โ”€ new-circuit
โ””โ”€โ”€ powersOfTau28_hez_final_12.ptau

Each new circuit lives in it's own directory. At the top level of each circuit directory lives the circom circuit and input to the circuit. The out directory will be autogenerated and store the compiled outputs, keys, and proofs. The Powers of Tau file comes from the Polygon Hermez ceremony, which saves time by not needing a new ceremony.

contracts

contracts
โ””โ”€โ”€ MultiplierVerifier.sol

Verifier contracts are autogenerated and prefixed by the circuit name, in this example Multiplier

hardhat.config.ts

  circom: {
    // (optional) Base path for input files, defaults to `./circuits/`
    inputBasePath: "./circuits",
    // (required) The final ptau file, relative to inputBasePath, from a Phase 1 ceremony
    ptau: "powersOfTau28_hez_final_12.ptau",
    // (required) Each object in this array refers to a separate circuit
    circuits: JSON.parse(JSON.stringify(circuits))
  },

circuits.config.json

circuits configuation is separated from hardhat.config.ts for autogenerated purposes (see next section)

[
  {
    "name": "multiplier",
    "protocol": "groth16",
    "circuit": "multiplier/circuit.circom",
    "input": "multiplier/input.json",
    "wasm": "multiplier/out/circuit.wasm",
    "zkey": "multiplier/out/multiplier.zkey",
    "vkey": "multiplier/out/multiplier.vkey",
    "r1cs": "multiplier/out/multiplier.r1cs",
    "beacon": "0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"
  }
]

adding circuits
To add a new circuit, you can run the newcircuit hardhat task to autogenerate configuration and directories i.e

npx hardhat newcircuit --name newcircuit

determinism

When you recompile the same circuit using the groth16 protocol, even with no changes, this plugin will apply a new final beacon, changing all the zkey output files. This also causes your Verifier contracts to be updated. For development builds of groth16 circuits, we provide the --deterministic flag in order to use a NON-RANDOM and UNSECURE hardcoded entropy (0x000000 by default) which will allow you to more easily inspect and catch changes in your circuits. You can adjust this default beacon by setting the beacon property on a circuit's config in your hardhat.config.js file.

zkcircuit's People

Contributors

omkark97 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.