Giter Club home page Giter Club logo

circom-witness-rs's Introduction

🏎️ circom-witness-rs

Description

This crate provides a fast witness generator for Circom circuits, serving as a drop-in replacement for Circom's witness generator. It was created in response to the slow performance of Circom's WASM generator for larger circuits, which also necessitates a WASM runtime, often a cumbersome requirement. The native C++ generator, though faster, depends on x86 assembly for field operations, rendering it impractical for use on other platforms (e.g., cross-compiling to ARM for mobile devices).

circom-witness-rs comes with two modes:

  1. Generate the static execution graph required for the witness generation at build time (--features=build-witness).
  2. Generate the witness elements at runtime from serialized graph.

In the first mode, it generates the c++ version of the witness generator through circom and links itself against it. The c++ code is made accessible to rust through cxx. It hooks all field functions (which are x86 assembly in the original generator), such that it can recreate the execution graph through symblic execution. The execution graph is further optimized through constant propagation and dead code elimination. The resulting graph is then serialized to a binary format. At runtime, the graph can be embedded in the binary and interpreted to generate the witness.

Usage

See this example project for Semaphore with more details on building.

See semaphore-rs for an example at runtime.

All of those example were used with circom compiler 2.1.6 (dcf7d68). Using a different version of circom might cause issues due to different c++ code being generated.

Benchmarks

TLDR: For semaphore circuit (depth 30) circom-witness-rs is ~25x faster than wasm and ~10x faster than native c++ version.

cargo bench --bench=criterion --features=bench,depth_30

With circom-witness-rs:q

witness_30              time:   [993.84 µs 996.62 µs 999.42 µs]

With wasm witness generator from circom-compat:

witness_30              time:   [24.630 ms 24.693 ms 24.759 ms]

With native c++ witness generator from circom: 9.640ms

As a nice side effect of the graph optimizations, the binary size is also reduced heavily. In the example of Semaphore the binary size is reduced from 1.3MB (semaphore.wasm) to 350KB (graph.bin).

Unimplemented features

There are still quite a few missing operations that need to be implemented. The list of supported and unsupported operations can be found here. Support for the missing operations is very straighfoward and will be added in the future.

// Field operations
unsafe fn Fr_mul(to: *mut FrElement, a: *const FrElement, b: *const FrElement);
unsafe fn Fr_add(to: *mut FrElement, a: *const FrElement, b: *const FrElement);
unsafe fn Fr_sub(to: *mut FrElement, a: *const FrElement, b: *const FrElement);
unsafe fn Fr_copy(to: *mut FrElement, a: *const FrElement);
unsafe fn Fr_copyn(to: *mut FrElement, a: *const FrElement, n: usize);
// fn Fr_neg(to: &mut FrElement, a: &FrElement);
// fn Fr_inv(to: &mut FrElement, a: &FrElement);
// fn Fr_div(to: &mut FrElement, a: &FrElement, b: &FrElement);
// fn Fr_square(to: &mut FrElement, a: &FrElement);
// fn Fr_shl(to: &mut FrElement, a: &FrElement, b: u32);
// fn Fr_shr(to: &mut FrElement, a: &FrElement, b: u32);
// fn Fr_band(to: &mut FrElement, a: &FrElement, b: &FrElement);
// fn Fr_bor(to: &mut FrElement, a: &FrElement, b: &FrElement);
// fn Fr_bxor(to: &mut FrElement, a: &FrElement, b: &FrElement);
// fn Fr_bnot(to: &mut FrElement, a: &FrElement);
unsafe fn Fr_eq(to: *mut FrElement, a: *const FrElement, b: *const FrElement);
unsafe fn Fr_neq(to: *mut FrElement, a: *const FrElement, b: *const FrElement);
unsafe fn Fr_lt(to: *mut FrElement, a: *const FrElement, b: *const FrElement);
unsafe fn Fr_gt(to: *mut FrElement, a: *const FrElement, b: *const FrElement);
unsafe fn Fr_leq(to: *mut FrElement, a: *const FrElement, b: *const FrElement);
unsafe fn Fr_geq(to: *mut FrElement, a: *const FrElement, b: *const FrElement);
unsafe fn Fr_isTrue(a: *mut FrElement) -> bool;
// fn Fr_fromBool(to: &mut FrElement, a: bool);
unsafe fn Fr_toInt(a: *mut FrElement) -> u64;
unsafe fn Fr_lor(to: *mut FrElement, a: *const FrElement, b: *const FrElement);
unsafe fn print(a: *mut FrElement);
// fn Fr_pow(to: &mut FrElement, a: &FrElement, b: &FrElement);
// fn Fr_idiv(to: &mut FrElement, a: &FrElement, b: &FrElement);

circom-witness-rs's People

Contributors

nikzak avatar philsippl avatar recmo avatar rymnc avatar sragss 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

circom-witness-rs's Issues

index out of bounds: the len is 4 but the index is 4

Hi,
When I build with the multiplier circuit

pragma circom 2.1.4;

/*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 { public [ a ] } = Multiplier2();

It shows the error

error: failed to run custom build command for `wtest v0.1.0 (/Users/zhengyawen/Documents/GitHub/semaphore-witness-example)`
note: To improve backtraces for build dependencies, set the CARGO_PROFILE_DEV_BUILD_OVERRIDE_DEBUG=true environment variable to enable debug information generation.

Caused by:
  process didn't exit successfully: `/Users/zhengyawen/Documents/GitHub/semaphore-witness-example/target/debug/build/wtest-d40a5b9cdeee7289/build-script-build` (exit status: 101)
  --- stderr
  thread 'main' panicked at /Users/zhengyawen/.cargo/git/checkouts/circom-witness-rs-68138715c9db01bf/1de8315/src/generate.rs:218:22:
  index out of bounds: the len is 4 but the index is 4
  stack backtrace:
     0: rust_begin_unwind
               at /rustc/a28077b28a02b92985b3a3faecf92813155f1ea1/library/std/src/panicking.rs:597:5
     1: core::panicking::panic_fmt
               at /rustc/a28077b28a02b92985b3a3faecf92813155f1ea1/library/core/src/panicking.rs:72:14
     2: core::panicking::panic_bounds_check
               at /rustc/a28077b28a02b92985b3a3faecf92813155f1ea1/library/core/src/panicking.rs:180:5
     3: <usize as core::slice::index::SliceIndex<[T]>>::index_mut
     4: <alloc::vec::Vec<T,A> as core::ops::index::IndexMut<I>>::index_mut
     5: witness::generate::build_witness
     6: build_script_build::main
     7: core::ops::function::FnOnce::call_once
  note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

Do you have any idea about how this happened?

supported prime numbers

I use circom with a specific prime number (secq256k1). Are all primes supported for a drop-in replacement of witness generation or need to expand this library to support different primes?

`Fr_toInt` overflows

Circuit:

Anon-adhaar verifier: https://github.com/oskarth/mopro/blob/b84d4423bd5c0b3d11292b5587d20124229fd630/mopro-core/examples/circom/anonAadhaar/aadhaar-verifier.circom#L111

Error message

Total input length: 1607
Total signal length: 1602
Main input signal no: 5
thread 'main' panicked at witness/src/field.rs:247:26:
called `Result::unwrap()` on an `Err` value: Overflow(256, 4891460686036598784, 18446744073709551615)
stack backtrace:
   0: rust_begin_unwind
             at /rustc/a28077b28a02b92985b3a3faecf92813155f1ea1/library/std/src/panicking.rs:597:5
   1: core::panicking::panic_fmt
             at /rustc/a28077b28a02b92985b3a3faecf92813155f1ea1/library/core/src/panicking.rs:72:14
   2: core::result::unwrap_failed
             at /rustc/a28077b28a02b92985b3a3faecf92813155f1ea1/library/core/src/result.rs:1652:5
   3: witness::field::Fr_toInt
   4: _cxxbridge1$Fr_toInt
   5: _Z19BigLessThan_119_runjP14Circom_CalcWit
             at /Users/zhengyawen/Documents/GitHub/mopro/witness/src/circuit.cc:202103:28
   6: _Z22RSAVerify65537_124_runjP14Circom_CalcWit
             at /Users/zhengyawen/Documents/GitHub/mopro/witness/src/circuit.cc:203339:1
   7: _Z23AadhaarVerifier_366_runjP14Circom_CalcWit
             at /Users/zhengyawen/Documents/GitHub/mopro/witness/src/circuit.cc:1509527:1
   8: _Z3runP14Circom_CalcWit
             at /Users/zhengyawen/Documents/GitHub/mopro/witness/src/circuit.cc:1509835:1
   9: cxxbridge1$run
             at /Users/zhengyawen/Documents/GitHub/mopro/target/release/build/witness-bc43ce4e137812d3/out/cxxbridge/sources/witness/src/generate.rs.cc:878:3
  10: witness::generate::build_witness
  11: generate_witness::main
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
libc++abi: terminating due to uncaught foreign exception
./scripts/prepare.sh: line 29: 51257 Abort trap: 6           RUST_BACKTRACE=1 WITNESS_CPP="$CIRCOM_DIR/$circuit_dir/$circuit_file" cargo run --package witness --bin generate-witness --release --features=build-witness

values[a].try_into().unwrap()

ref: circuit.cc

Missing ops

My circuit requires:
Fr_element2str
Fr_neg
Fr_div
Fr_band
Fr_shr

I am happy to help implementing those if you could provide a bit of guidance. Thank you!

Unclosed "{" in circuit.cc

My generated circuit.cc has unclosed "{" so that at some point of compilation I get

a function-definition is not allowed here before '{' token

Maybe it is due to replace.sh file? How can I debug that?

NotFound, message: "No such file or directory"

Launching it this way
WITNESS_CPP=[absolute path to my circom circuit] cargo build --release

build.rs has this

fn main() {
    witness::generate::build_witness()
}

However, I get

called `Result::unwrap()` on an `Err` value: Os { code: 2, kind: NotFound, message: "No such file or directory" }

Any hint why this may be happenning? Am I using the correct system variable?

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.