Skip to content

Commit

Permalink
Publish (#317)
Browse files Browse the repository at this point in the history
* v0.1.0-alpha

* v0.1.0-alpha.2

* circom-prover v0.1.0

* v0.1.1-alpha.0

* fix: fix dependencies

* chore: import circom-prover from path

* v0.1.1
  • Loading branch information
vivianjeng authored Feb 14, 2025
1 parent f18249d commit 55a0790
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 74 deletions.
7 changes: 4 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 27 additions & 15 deletions circom-prover/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
name = "circom-prover"
version = "0.1.0"
edition = "2021"
description = "Circom prover is a Rust library for generating and verifying proofs for Circom circuits."
license = "MIT OR Apache-2.0"
repository = "https://github.com/zkmopro/mopro"
documentation = "https://zkmopro.org/"
homepage = "https://zkmopro.org/"
exclude = ["test-vectors/*"]

[lib]
name = "circom_prover"
Expand All @@ -10,15 +16,11 @@ name = "circom_prover"
default = ["rustwitness", "arkworks", "ethereum"]

# Witness Generation
rustwitness = [
"rust-witness",
]
witnesscalc = [
"witnesscalc-adapter",
]
rustwitness = ["rust-witness"]
witnesscalc = ["witnesscalc-adapter"]

# Proof Generation
arkworks = [
arkworks = [
"ark-serialize",
"ark-ec",
"ark-crypto-primitives",
Expand All @@ -42,22 +44,33 @@ num-bigint = { version = "0.4.3", default-features = false, features = [
] }
anyhow = "1.0.95"
rust-witness = { version = "0.1.2", optional = true }
witnesscalc-adapter = { git = "https://github.com/zkmopro/witnesscalc_adapter.git", package = "witnesscalc-adapter", optional = true }
witnesscalc-adapter = { version = "0.1.0", optional = true }
byteorder = { version = "1.0.0" }
uuid = { version = "1.9.1", features = ["v4"] }

# arkworks
ark-ec = { version = "=0.4.1", default-features = false, features = ["parallel"], optional = true }
ark-ff = { version = "=0.4.1", default-features = false, features = ["parallel", "asm"], optional = true }
ark-std = { version = "=0.4.0", default-features = false, features = ["parallel"], optional = true }
ark-ec = { version = "=0.4.1", default-features = false, features = [
"parallel",
], optional = true }
ark-ff = { version = "=0.4.1", default-features = false, features = [
"parallel",
"asm",
], optional = true }
ark-std = { version = "=0.4.0", default-features = false, features = [
"parallel",
], optional = true }
ark-crypto-primitives = { version = "=0.4.0", optional = true }
ark-relations = { version = "0.4", default-features = false, optional = true }
ark-bls12-381 = { version = "0.4.0", optional = true }
ark-bn254 = { version = "=0.4.0", optional = true }
ark-serialize = { version = "=0.4.1", features = ["derive"], optional = true }
ark-groth16 = { version = "=0.4.0", default-features = false, features = ["parallel"], optional = true }
ark-poly = { version = "=0.4.1", default-features = false, features = ["parallel"], optional = true}
rand = { version = "0.8", features = ["std"]}
ark-groth16 = { version = "=0.4.0", default-features = false, features = [
"parallel",
], optional = true }
ark-poly = { version = "=0.4.1", default-features = false, features = [
"parallel",
], optional = true }
rand = { version = "0.8", features = ["std"] }

# ethereum
rayon = { version = "1.10.0" }
Expand All @@ -66,4 +79,3 @@ hex-literal = "0.4.1"
[dev-dependencies]
serde_json = "1.0.94"
hex-literal = "0.4.1"

87 changes: 87 additions & 0 deletions circom-prover/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Circom Prover

Circom prover is a Rust library for generating and verifying proofs for [Circom](https://github.com/iden3/circom) circuits.
It is designed to be used in cross-platform applications, and is compatible with the [Mopro](https://github.com/zkmopro/mopro) library.

## Usage

Depends on the witness generation method, build the rust witness function first.
For example, if you use the [Rust Witness](https://github.com/chancehudson/rust-witness), please refer to the [Rust Witness](https://github.com/chancehudson/rust-witness) for more details.

### Proof Generation

```rust
use std::collections::HashMap;
rust_witness::witness!(multiplier2);
use circom_prover::{prover::ProofLib, witness::WitnessFn, CircomProver};

// Prepare inputs
let mut inputs = HashMap::new();
inputs.insert("a".to_string(), vec!["1".to_string()]);
inputs.insert("b".to_string(), vec!["2".to_string()]);

// 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),
inputs,
zkey_path,
).unwrap();
```

### Proof Verification

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

### Proof Deserialization

```rust
use ark_bn254::Bn254;
use circom_prover::{
prover::{
serialization::{deserialize_inputs, deserialize_proof},
},
};
let deserialized_proof = deserialize_proof::<Bn254>(result.proof);
let deserialized_pub_inputs = deserialize_inputs::<Bn254>(result.pub_inputs);
```

## Adapters

## Witness Generation

- [x] [Rust Witness](https://github.com/chancehudson/rust-witness)
- [ ] [Witnesscalc adapter](https://github.com/zkmopro/witnesscalc_adapter)
- [ ] [circom witnesscalc](https://github.com/iden3/circom-witnesscalc)

## Proof Generation

- [x] [Arkworks](https://github.com/arkworks-rs)
- [ ] [Rust rapidsnark](https://github.com/zkmopro/rust-rapidsnark)

## Performance

It speeds up circom proof by ~100x comparing to [arkworks-rs/circom-compat](https://github.com/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](https://zkmopro.org/docs/performance) for more benchmarks.

## Community

- X account: <a href="https://twitter.com/zkmopro"><img src="https://img.shields.io/twitter/follow/zkmopro?style=flat-square&logo=x&label=zkmopro"></a>
- Telegram group: <a href="https://t.me/zkmopro"><img src="https://img.shields.io/badge/telegram-@zkmopro-blue.svg?style=flat-square&logo=telegram"></a>
- Mopro Documentation: https://zkmopro.org

## Acknowledgements

This work is sponsored by [PSE](https://pse.dev/).
8 changes: 5 additions & 3 deletions mopro-ffi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mopro-ffi"
version = "0.1.0"
version = "0.1.1"
edition = "2021"
description = "Mopro is a toolkit for ZK app development on mobile. Mopro makes client-side proving on mobile simple."
license = "MIT OR Apache-2.0"
Expand Down Expand Up @@ -29,7 +29,9 @@ uniffi = { version = "=0.28.0", features = ["cli", "build"] }
serde = { version = "1.0", features = ["derive"] }
anyhow = "1.0.86"
bincode = "1.3.3"
num-bigint = { version = "0.4.3", default-features = false, features = ["rand",] }
num-bigint = { version = "0.4.3", default-features = false, features = [
"rand",
] }

# Error handling
thiserror = "=2.0.3"
Expand All @@ -38,7 +40,7 @@ color-eyre = "=0.6.2"
# circom deps
rust-witness = { version = "0.1.1", optional = true }
ark-ff = { version = "0.4.0", optional = true }
circom-prover = {path = "../circom-prover", optional = true}
circom-prover = { path = "../circom-prover", optional = true }

# ZKP generation
ark-bn254 = { version = "=0.4.0", optional = true }
Expand Down
64 changes: 32 additions & 32 deletions mopro-ffi/README.md
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
# mopro-ffi
# Mopro FFI

Mopro is a toolkit for ZK app development on mobile. Mopro makes client-side proving on mobile simple.
`mopro-ffi` is a tool designed to assist programmable cryptography application or rust application developers in efficiently creating bindings for client-side targets.

## Getting started
Key features include:

- Make sure you've installed the [prerequisites](https://zkmopro.org/docs/prerequisites).
- Getting started with this [tutorial](https://zkmopro.org/docs/getting-started).
- **Function Serialization and Export:** Enables serialization and export of functions within each proving system. To generate FFI bindings for different targets, inputs and outputs must conform to the specific types defined in [uniffi](https://mozilla.github.io/uniffi-rs/latest/udl/builtin_types.html).
- Supported proving systems: `circom`, `halo2`.
- **Executable Binaries:** Provides pre-built binaries, allowing developers to generate bindings for various targets effortlessly.
- Supported targets: `swift`, `kotlin`.
- **Customize Exported Functions:** Supports the ability to customize the exported functions. Users can define the functions in the `src/mopro.udl` file.

## Run tests
## Usage

- circom
```sh
cargo test --features circom
```
- halo2
```sh
cargo test --features halo2
```
- Please check the [Manual Setup for Android/iOS Bindings](https://zkmopro.org/docs/setup/rust-setup) for integrating `mopro-ffi` into your project.

## Bindings
## Usage for general Rust application

- `SwiftBindings`
- `KotlinBindings`
- Integrate the `mopro-ffi` like the above tutorial.
- Update the `src/mopro.udl` file to add the functions you want to export. Check out how to define the functions in UDL file: [UniFFI: The UDL file](https://mozilla.github.io/uniffi-rs/0.28/udl_file_spec.html)

The uniffi bindings are precompiled and committed here for a specifically named crate. This avoids the complexity of building/invoking the uniffi cli by dependent packages. Note that dependent crates _must_ have the library name `mopro_bindings`, or rebuild the binding themselves.
- E.g.
export Rust function like
```rust
pub fn hello_world() -> String {
"Hello World!".to_string()
}
```
and define the function in the UDL file like:
```udl
namespace mopro {
// ...
string hello_world();
}
```

## Modules

The root module exports functions for generating proofs. It also exports a macro that can be used to setup uniffi from our provided udl file. User modification to the UDL file is not supported at this time.

### `circom`

Includes all proving and serialization logic for circom proofs. Does _not_ include logic for witness generation.

### `halo2`

Includes all proving logic for halo2.
- Run `cargo run --bin ios` or `cargo run --bin android` again.

## Community

- X account: <a href="https://twitter.com/zkmopro"><img src="https://img.shields.io/twitter/follow/zkmopro?style=flat-square&logo=x&label=zkmopro"></a>
- Telegram group: <a href="https://t.me/zkmopro"><img src="https://img.shields.io/badge/telegram-@zkmopro-blue.svg?style=flat-square&logo=telegram"></a>
- X account: <a href="https://twitter.com/zkmopro"><img src="https://img.shields.io/twitter/follow/zkmopro?style=flat-square&logo=x&label=zkmopro"></a>
- Telegram group: <a href="https://t.me/zkmopro"><img src="https://img.shields.io/badge/telegram-@zkmopro-blue.svg?style=flat-square&logo=telegram"></a>
- Mopro Documentation: https://zkmopro.org

## Acknowledgements

This work was initially sponsored by a joint grant from [PSE](https://pse.dev/) and [0xPARC](https://0xparc.org/). It is currently incubated by PSE.
This work was initially sponsored by a joint grant from [PSE](https://pse.dev/) and [0xPARC](https://0xparc.org/). It is currently incubated by PSE.
11 changes: 6 additions & 5 deletions mopro-ffi/src/circom/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::collections::HashMap;
#[macro_export]
macro_rules! circom_app {
() => {
use mopro_ffi::witness::WitnessFn;
fn generate_circom_proof(
in0: String,
in1: std::collections::HashMap<String, Vec<String>>,
Expand All @@ -24,7 +25,7 @@ macro_rules! circom_app {
};
let witness_fn = get_circom_wtns_fn(name.to_str().unwrap())?;
mopro_ffi::generate_circom_proof_wtns(
circom_prover::prover::ProofLib::Arkworks,
mopro_ffi::prover::ProofLib::Arkworks,
in0,
in1,
witness_fn,
Expand All @@ -37,7 +38,7 @@ macro_rules! circom_app {
in1: Vec<u8>,
in2: Vec<u8>,
) -> Result<bool, mopro_ffi::MoproError> {
mopro_ffi::verify_circom_proof(circom_prover::prover::ProofLib::Arkworks, in0, in1, in2)
mopro_ffi::verify_circom_proof(mopro_ffi::prover::ProofLib::Arkworks, in0, in1, in2)
.map_err(|e| {
mopro_ffi::MoproError::CircomError(format!("Verification error: {}", e))
})
Expand Down Expand Up @@ -78,12 +79,12 @@ macro_rules! circom_app {
///
/// ## For Advanced Users:
/// This macro is abstracting away the implementation of
/// `get_circom_wtns_fn(circuit: &str) -> Result<circom_prover::witness::WitnessFn, mopro_ffi::MoproError>`.
/// `get_circom_wtns_fn(circuit: &str) -> Result<mopro_ffi::witness::WitnessFn, mopro_ffi::MoproError>`.
/// You can choose to implement it directly with your custom logic:
///
/// #### Example:
/// ```ignore
/// fn get_circom_wtns_fn(circuit: &str) -> Result<circom_prover::witness::WitnessFn, mopro_ffi::MoproError> {
/// fn get_circom_wtns_fn(circuit: &str) -> Result<mopro_ffi::witness::WitnessFn, mopro_ffi::MoproError> {
/// match circuit {
/// "circuit1.zkey" => Ok(circuit1_witness_fn),
/// _ => Err(mopro_ffi::MoproError::CircomError(format!("Unknown ZKEY: {}", circuit).to_string()))
Expand All @@ -93,7 +94,7 @@ macro_rules! circom_app {
#[macro_export]
macro_rules! set_circom_circuits {
($(($key:expr, $func:expr)),+ $(,)?) => {
fn get_circom_wtns_fn(circuit: &str) -> Result<circom_prover::witness::WitnessFn, mopro_ffi::MoproError> {
fn get_circom_wtns_fn(circuit: &str) -> Result<mopro_ffi::witness::WitnessFn, mopro_ffi::MoproError> {
match circuit {
$(
$key => Ok($func),
Expand Down
Loading

0 comments on commit 55a0790

Please sign in to comment.