3 unstable releases

new 0.5.0 Oct 28, 2024
0.5.0-alpha Sep 11, 2024
0.1.0 Jul 26, 2023

#875 in Magic Beans

Download history 30/week @ 2024-07-14 21/week @ 2024-07-21 38/week @ 2024-07-28 34/week @ 2024-08-04 38/week @ 2024-08-11 44/week @ 2024-08-18 30/week @ 2024-08-25 82/week @ 2024-09-01 173/week @ 2024-09-08 43/week @ 2024-09-15 51/week @ 2024-09-22 30/week @ 2024-09-29 18/week @ 2024-10-06 45/week @ 2024-10-13 11/week @ 2024-10-20 160/week @ 2024-10-27

235 downloads per month
Used in 3 crates

MIT/Apache

4.5MB
3K SLoC

Rust 2K SLoC // 0.0% comments JavaScript 761 SLoC // 0.1% comments Shell 78 SLoC // 0.0% comments Circom 56 SLoC

ark-circom

Arkworks bindings to Circom's R1CS, for Groth16 Proof and Witness generation in Rust.

Github Actions

Documentation

Clone the repository and run cd ark-circom/ && cargo doc --open

Add ark-circom to your repository

[dependencies]

ark-circom = "0.5.0"

Example

// Load the WASM and R1CS for witness and proof generation
let cfg = CircomConfig::<Bn254>::new(
    "./test-vectors/mycircuit.wasm",
    "./test-vectors/mycircuit.r1cs",
)?;

// Insert our public inputs as key value pairs
let mut builder = CircomBuilder::new(cfg);
builder.push_input("a", 3);
builder.push_input("b", 11);

// Create an empty instance for setting it up
let circom = builder.setup();

// Run a trusted setup
let mut rng = thread_rng();
let params = generate_random_parameters_with_reduction(circom, &mut rng)?;

// Get the populated instance of the circuit with the witness
let circom = builder.build()?;

let inputs = circom.get_public_inputs().unwrap();

// Generate the proof
let proof = prove(&params, circom, &mut rng)?;

// Check that the proof is valid
let pvk = process_vk(&params.vk)?;
let verified = verify_with_processed_vk(&pvk, &inputs, &proof)?;
assert!(verified);

Running the tests

Tests require the following installed:

  1. solc. We also recommend using solc-select for more flexibility.
  2. ganache-cli

Features

  • Witness generation using Circom's WASM witness code
  • ZKey parsing into Arkworks Proving Key over BN254
  • Compatibility layer for Ethereum types, so that proofs can be used in Solidity verifiers
  • Proof generations and verification using Arkworks
  • CLI for common operations

Notes

The prover key generated by circom differs from the one generated by arkworks' groth16 library. While the format is the same, it represents different values. Circom 'prepares' the powers of tau by converting them to Lagrange base, i.e. from s^i.G -> L_i(s).G. This affects the witness generation process, and the caller needs to ensure the correct R1CSToQAP implementer is used:

Acknowledgements

This library would not have been possibly without the great work done in:

Special shoutout to Kobi Gurkan for all the help in parsing SnarkJS' ZKey file format.

Dependencies

~41–58MB
~1M SLoC