Coder Social home page Coder Social logo

getzefi / circomjs Goto Github PK

View Code? Open in Web Editor NEW
46.0 2.0 3.0 21.44 MB

CircomJS is a javascript framework that allows you to automate your Circom workflow in pure javascript.

Home Page: https://zefi.gitbook.io/circomjs/

TypeScript 97.56% JavaScript 0.99% Circom 1.45%
circom snarkjs zk-snarks circom-framework circomjs

circomjs's Introduction

CIRCOMJS

circomjs-logo

CircomJS is a javascript framework for automating and easing workflow around developing in the Circom DSL.

We would suggest getting started with this gitbook, and you should also checkout our blog post on why we made CircomJS.

The framework operates on a simple circuit.config.js file, which is a configuration file for circomJS.

below is an example of what a circuit.config.json file looks like.

{
  "projectName": "arithmetic",
  "outputDir": "./out",
  "build": {
    "inputDir": "./circuits",
    "circuits": [
      {
        "cID": "mul",
        "fileName": "multiply.circom",
        "compilationMode": "wasm"
      },
      {
        "cID": "add",
        "fileName": "addition.circom",
        "proofType": "plonk",
        "compilationMode": "wasm"
      }
    ]
  }
}

(example circuit.config.json)

You can also take a look at CircomJS starter repository for reference.

CircomJS gives you the following capabilities in Javascript!

Compile your circuits

const {CircomJS} = require("@zefi/circomjs")

const main = async() => {
    const circomjs = new CircomJS()
    const circuit =  circomjs.getCircuit("mul")

    // it will build the circuit with cID mul
    await circuit.compile()
}

main()

Generate Proofs

const {CircomJS} = require("@zefi/circomjs")

const main = async() => {
    const circomjs = new CircomJS()
    const circuit =  circomjs.getCircuit("mul")

    // important to await compilation, before running circuit.genProof()
    await circuit.compile()

    const input = {
        x: 3,
        y: 5
    }

    const proof = await circuit.genProof(input);
    console.log("proof verification result ----->",await circuit.verifyProof(proof))
}

main()

Verify proofs

const {CircomJS} = require("@zefi/circomjs")

const main = async() => {
    const circomjs = new CircomJS()
    const circuit =  circomjs.getCircuit("mul")

    // important to await compilation, before running circuit.genProof()
    await circuit.compile()

    const input = {
        x: 3,
        y: 5
    }

    const proof = await circuit.genProof(input);
    console.log("proof verification result ----->",await circuit.verifyProof(proof))
}

main()

and much more!

You can do a lot more than this:

  • calculate witness
  • calculate total constraints
  • check constraints on a generated witness
  • automatic download of ideal power of tau file

Upcoming features

We are working towards adding new features to the framework, some of which include:

  • support for C compilation of circuits { witness generation programs }
  • parallel build of ZK Circuits
  • integration with Ethersjs to deploy smart contract verifiers
  • smart builds: only building circuits when either source code or a dependency of the circuit has changed.
  • export solidity smart contract verifier
  • export solidity call-data

We at zefi are open to ideas around the project and would love to interact with the community and see contributions comings its way, feel free to join our telegram group and say hi, also feel free to write to us at [email protected].

circomjs's People

Contributors

bajpai244 avatar ivorynoise avatar shivamsaxena12 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

circomjs's Issues

Tests failing on running `npm test`

@bajpai244 Can you see this as well?

✔ ~/Documents/devel/zefi/circomjs [feat/publish-to-npm L|✚ 2…35] 
15:04 $ npm test

> [email protected] test /Users/deepakaggarwal/Documents/devel/zefi/circomjs
> jest

 PASS  tests/configParser.test.ts
 PASS  tests/circomJS.test.ts
(node:3873) Warning: Closing file descriptor 175 on garbage collection
(Use `node --trace-warnings ...` to show where the warning was created)
(node:3873) [DEP0137] DeprecationWarning: Closing a FileHandle object on garbage collection is deprecated. Please close FileHandle objects explicitly using FileHandle.prototype.close(). In the future, an error will be thrown if a file descriptor is closed during garbage collection.
(node:3873) Warning: Closing file descriptor 23 on garbage collection
(node:3873) Warning: Closing file descriptor 23 on garbage collection
(node:3873) Warning: Closing file descriptor 23 on garbage collection
(node:3873) Warning: Closing file descriptor 23 on garbage collection
 FAIL  tests/circuit.test.ts (34.729 s)
  ● Console

    console.log
      JsonRpcProvider failed to startup; retry in 1s

      at node_modules/ethers/src.ts/providers/provider-jsonrpc.ts:599:29

  ● Circuit test › should calculate witness

    assert.strictEqual(received, expected)

    Expected value to strictly be equal to:
      undefined
    Received:
      null
    
    Message:
      circom compiler error 
    Error: Command failed: circom --wasm --sym --r1cs --output tests/data/out/circuit2 --O0 tests/data/circuits/testtemp/circuit2.circom
    thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Os { code: 2, kind: NotFound, message: "No such file or directory" }', circom/src/compilation_user.rs:105:49
    note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
    

    Difference:

      Comparing two different types of values. Expected undefined but received null.

      89 |      }
      90 |     } catch (e) {
    > 91 |      assert(false,
         |      ^
      92 |             "circom compiler error \n" + e);
      93 |     }
      94 | }

      at assert (src/vendors/circom_tester/wasm/tester.js:91:2)
      at wasm_tester (src/vendors/circom_tester/wasm/tester.js:47:2)
      at Circuit.compile (src/circuit.ts:33:28)
      at Object.<anonymous> (tests/circuit.test.ts:77:5)

  ● Circuit test › should generate proof

    tests/data/out/circuit2/circuit_0000.zkey: Missing section 10

      21 |
      22 |     await zKey.newZKey(r1cs, powerofTauPath, zKeyPath_0000)
    > 23 |     await zKey.contribute(zKeyPath_0000, zKeyPath_0001, "1st Contributor Name", "askljdaklsdjkls")
         |     ^
      24 |     await zKey.contribute(zKeyPath_0001, zKeyPath_0002, "2nd Contributor Name", "askljdaklsdjkls")
      25 |
      26 |     await zKey.exportBellman(zKeyPath_0002, challenge_phase2_0003)

      at Object.startReadUniqueSection (node_modules/@iden3/binfileutils/build/main.cjs:96:38)
      at readMPCParams (node_modules/snarkjs/build/main.cjs:638:35)
      at Object.phase2contribute [as contribute] (node_modules/snarkjs/build/main.cjs:5510:29)
      at genGrothZKey (src/utils/zKey.ts:23:5)
      at tests/circuit.test.ts:63:7

  ● Circuit test › should deploy smart contract verifier

    TypeError: invalid BytesLike value (argument="value", value="undefined", code=INVALID_ARGUMENT, version=6.0.3)

      68 |
      69 |     const provider = new JsonRpcProvider(rpcURL)
    > 70 |     const signer =  new Wallet(privKey, provider)
         |                     ^
      71 |     const cFactory = new ContractFactory(abi, bytecode, signer)
      72 |
      73 |     return cFactory

      at makeError (node_modules/ethers/src.ts/utils/errors.ts:656:21)
      at assert (node_modules/ethers/src.ts/utils/errors.ts:680:25)
      at assertArgument (node_modules/ethers/src.ts/utils/errors.ts:692:5)
      at _getBytes (node_modules/ethers/src.ts/utils/data.ts:44:19)
      at getBytes (node_modules/ethers/src.ts/utils/data.ts:55:12)
      at dataLength (node_modules/ethers/src.ts/utils/data.ts:125:12)
      at new SigningKey (node_modules/ethers/src.ts/crypto/signing-key.ts:40:34)
      at new Wallet (node_modules/ethers/src.ts/wallet/wallet.ts:42:55)
      at getVerifierContractFactory (src/utils/verifier.ts:70:21)
      at getVerifierContractFactory (src/utils/verifier.ts:57:22)
          at runMicrotasks (<anonymous>)
      at Circuit.deploySmartContractVerifier (src/circuit.ts:117:33)
      at Object.<anonymous> (tests/circuit.test.ts:150:30)

  ● Circuit test › should check verification on chain

    unsupported protocol undefined (info={ "protocol": "undefined" }, operation="request", code=UNSUPPORTED_OPERATION, version=6.0.3)

      at makeError (node_modules/ethers/src.ts/utils/errors.ts:663:21)
      at assert (node_modules/ethers/src.ts/utils/errors.ts:680:25)
      at getUrl (node_modules/ethers/src.ts/utils/geturl.ts:17:11)
      at FetchRequest.#send (node_modules/ethers/src.ts/utils/fetch.ts:492:28)
      at FetchRequest.send (node_modules/ethers/src.ts/utils/fetch.ts:547:26)
      at JsonRpcProvider._send (node_modules/ethers/src.ts/providers/provider-jsonrpc.ts:1016:40)
      at node_modules/ethers/src.ts/providers/provider-jsonrpc.ts:406:51
      at Timeout._onTimeout (node_modules/ethers/src.ts/providers/provider-jsonrpc.ts:438:19)

A worker process has failed to exit gracefully and has been force exited. This is likely caused by tests leaking due to improper teardown. Try running with --detectOpenHandles to find leaks. Active timers can also cause this, ensure that .unref() was called on them.
Test Suites: 1 failed, 2 passed, 3 total
Tests:       4 failed, 19 passed, 23 total
Snapshots:   0 total
Time:        35.785 s
Ran all test suites.
npm ERR! Test failed.  See above for more details.
✘-1 ~/Documents/devel/zefi/circomjs [feat/publish-to-npm L|✚ 9…35] 
15:04 $ 


review-01

This issue is a review of this codebase and should be resolved inside a separate PR.

Using formatter & linter

Hi, do you have a specific formatter & linter that you use? It would be a bit better to use one so that the code looks the same for everyone.

I can suggest Google TypeScript Style guide, which is installed simply via npx gts init. You can override some settings there too.

Default `filepath` for config should be calculated relative to the current working directory.

image

Currently, the default configFilePath is assumed to be ./circuit.config.json, hence we have the assumption that the file that consumes this library is at the root of the project { since we want default config path to be at project root.}

The correct way to resolve this should be by looking into path.resolve(process.cwd(),"./circuit.config.json"), this way it will look for the config at the project root rather than relative to the path of file consuming the library.

Warning on running tests

@bajpai244 we right now get the following warning when we run tests.

Can you see to it and fix it?

(node:3873) Warning: Closing file descriptor 23 on garbage collection

Failed to compile circom file under PLONK proofType

Hi, I am compiling my circom using the circomjs. The compiling of the circom file works fine when I use zkREPL, or using circomjs in the Groth16 proof type. But with PLONK proof type, there is the error of the following:

❯ node main.js
Error compiling the circuit: out/running/circuit_final.zkey: Missing section 1
Detailed stack trace: Error: out/running/circuit_final.zkey: Missing section 1
    at Object.startReadUniqueSection (/Users/jidali/projects/push/server/node_modules/@iden3/binfileutils/build/main.cjs:96:38)
    at readHeader$1 (/Users/jidali/projects/push/server/node_modules/snarkjs/build/main.cjs:392:35)
    at Object.zkeyExportVerificationKey [as exportVerificationKey] (/Users/jidali/projects/push/server/node_modules/snarkjs/build/main.cjs:6094:24)

I wonder how I can solve this issue. Thanks!

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.