#prover #source #witnesscalc

circom-prover

Circom prover is a Rust library for generating and verifying proofs for Circom circuits

6 releases

new 0.1.1 Mar 27, 2025
0.1.0 Feb 6, 2025

#493 in Filesystem

33 downloads per month
Used in mopro-ffi

MIT/Apache

105KB
2K SLoC

Circom Prover

Circom prover is a Rust library for generating and verifying proofs for Circom circuits. It is designed to be used in cross-platform applications, and is compatible with the Mopro library.

Usage

Install circom-prover

cargo add circom-prover

Depends on the witness generation method, build the rust witness function first. Here is how to use the rust-witness generator.

Include the crate in your Cargo.toml:

[dependencies]
rust-witness = "0.1"
num-bigint = "0.4"

[build-dependencies]
rust-witness = "0.1"

In build.rs, add the following code to compile the witness generator wasm sources (.wasm) into a native library and link to it:

rust_witness::transpile::transpile_wasm("../path to directory containing your wasm sources");
// e.g. rust_witness::transpile::transpile_wasm("./test-vectors".to_string());
// The directory should contain the following files:
// - <circuit name>.wasm

Proof Generation

use std::collections::HashMap;
use circom_prover::{prover::ProofLib, witness::WitnessFn, CircomProver};

// Prepare witness generator
rust_witness::witness!(multiplier2);

// Prepare inputs
let inputs = HashMap::from([
    ("a".to_string(), vec!["1".to_string()]),
    ("b".to_string(), vec!["2".to_string()]),
]);
let input_str = serde_json::to_string(&inputs).unwrap();

// Prepare zkey path
let zkey_path = "./test-vectors/multiplier2_final.zkey".to_string();

// Generate proof
let result = CircomProver::prove(
    ProofLib::Arkworks,
    WitnessFn::RustWitness(multiplier2_witness),
    input_str,
    zkey_path.clone(),
).unwrap();

Proof Verification

// Verify proof
let valid = CircomProver::verify(
    ProofLib::Arkworks,
    result,
    zkey_path,
).unwrap();

Advanced usage

circom-prover also supports witnesscalc and rapidsnark for advanced users. These tools offer better performance but may not be entirely stable.

Below is a tutorial on how to enable witnesscalc and rapidsnark.

Set feature flags

[dependencies]
circom-prover = { version = "0.1", default-features = false, features = ["witnesscalc", "rapidsnark"] }
witnesscalc-adapter = "0.1"
anyhow = "1.0"

[build-dependencies]
witnesscalc-adapter = "0.1"
circom-prover = { version = "0.1", default-features = false, features = ["rapidsnark"] }

witnesscalc

Install dependencies:

  • Linux:
sudo apt install build-essential cmake m4 nasm libstdc++6
  • MacOS
brew install nasm

Rust toolchain: cargo 1.81.0 (2dbb1af80 2024-08-20)

In build.rs, add the following code to compile the witness generator wasm sources (.wasm) into a native library and link to it:

witnesscalc_adapter::build_and_link("../path to directory containing your C++ sources");
// e.g. witnesscalc_adapter::build_and_link("../testdata");
// The directory should contain the following files:
// - <circuit name>.cpp
// - <circuit name>.dat

Proof Generation

use circom_prover::{prover::ProofLib, witness::WitnessFn, CircomProver};
use std::collections::HashMap;
use anyhow::Result;

// Prepare witness generator
witnesscalc_adapter::witness!(multiplier2);

// Prepare inputs
let inputs = HashMap::from([
    ("a".to_string(), vec!["1".to_string()]),
    ("b".to_string(), vec!["2".to_string()]),
]);
let input_str = serde_json::to_string(&inputs).unwrap();

// Prepare zkey path
let zkey_path = "./test-vectors/multiplier2_final.zkey".to_string();

// Generate proof
let result = CircomProver::prove(
    ProofLib::Rapidsnark,
    WitnessFn::WitnessCalc(multiplier2_witness),
    input_str,
    zkey_path.clone(),
)
.unwrap();

Proof Verification

// Verify proof
let valid = CircomProver::verify(
    ProofLib::Rapidsnark,
    result,
    zkey_path,
).unwrap();

Adapters

Witness Generation

Proof Generation

Performance

It speeds up circom proof by ~100x comparing to arkworks-rs/circom-compat in keccak256 circuits. We will provide more benchmarks with different adapters in the future. And you can also check the Mopro documentation for more benchmarks.

Community

Acknowledgements

This work is sponsored by PSE.

Dependencies

~3–11MB
~126K SLoC