Giter Site home page Giter Site logo

circom-101's Introduction

CIRCOM 101

Note: This repo has different branch, and each branch has a different program

Getting Started

To create a new package.json, run the following command:

npm init -y

Next, install circom2 and snarkjs using npm:

npm install circom2 snarkjs

Creating the Circuit

Let's create the circuit.circom file, which contains the circuit's logic.

Compiling the Circuit

Compile the circuit using the following command:

npx circom2 circuit.circom --r1cs --wasm

Here:

--r1cs specifies the circuit type as R1CS.
--wasm is the output type

It which generates a new folder containing a JavaScript file for this circuit(wasm) along with two utility files. Generating the Witness

To run the witness file, use the following command:

node circuit_js/generate_witness.js circuit_js/circuit.wasm input.json witness.wtns

Here:

  • circuit_js/generate_witness.js is the file that we want to run.
  • circuit_js/circuit.wasm is the circuit wasm file we are feeding to the JavaScript file.
  • input.json contains the input for the circuit.
  • witness.wtns is the output file that contains the witness.

Sample input.json file

{
    "x": 2,
    "y": 3
}

After running the witness generation command, you'll get a binary witness file. To convert it to JSON format, use:

npx snarkjs wtns export json witness.wtns witness.json

Witness file (witness.json)

[
 "1",
 "47",
 "2",
 "3",
 "4",
 "12",
 "9"
]

The witness file is structured as follows: The first number is always one, representing constant operations within the circuit. This number can be ignored. Next comes the output, which, in this case, is only one value: 47.

To verify our understanding, let's quickly calculate the output based on the given inputs, where x = 2 and y = 3. The result is 47, which matches the correct output.

After the output, the witness file contains the inputs (2 and 3) followed by all the intermediate witness values. The number of witnesses in the file will always be the same as the number of constraints during the compilation, minus one. The reason for this may be related to the degree of freedom in a constraint system, although it's not entirely certain.

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.