Skip to content

Commit

Permalink
clean up mopro-ffi dep.
Browse files Browse the repository at this point in the history
  • Loading branch information
KimiWu123 committed Jan 23, 2025
1 parent e2c16ce commit 82c6c52
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 269 deletions.
9 changes: 0 additions & 9 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion circom-prover/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ name = "circom_prover"
[features]
default = [
"rust-witness",
"witnesscalc-adapter",
"ark-circom",
"ark-serialize",
"ark-ec",
Expand All @@ -29,7 +30,7 @@ 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 = { git = "https://github.com/zkmopro/witnesscalc_adapter.git", package = "witnesscalc-adapter", optional = true }

# ZKP Generation
ark-ec = { version = "=0.4.1", default-features = false, features = ["parallel"], optional = true }
Expand Down
23 changes: 23 additions & 0 deletions circom-prover/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,25 @@
pub mod prover;
pub mod witness;

pub use rust_witness;
pub use rust_witness::transpile::transpile_wasm;
pub use witnesscalc_adapter;

#[derive(Debug, Clone, Default)]
pub struct G1 {
pub x: String,
pub y: String,
}

#[derive(Debug, Clone, Default)]
pub struct G2 {
pub x: Vec<String>,
pub y: Vec<String>,
}

#[derive(Debug, Clone, Default)]
pub struct ProofCalldata {
pub a: G1,
pub b: G2,
pub c: G1,
}
87 changes: 87 additions & 0 deletions circom-prover/src/prover/serialization.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use crate::{ProofCalldata, G1, G2};
use anyhow::Result;
use ark_bn254::Bn254;
use ark_circom::ethereum;
use ark_ec::pairing::Pairing;
use ark_groth16::{Proof, ProvingKey};
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
Expand Down Expand Up @@ -35,6 +39,34 @@ pub fn deserialize_inputs<T: Pairing>(data: Vec<u8>) -> SerializableInputs<T> {
SerializableInputs::deserialize_uncompressed(&mut &data[..]).expect("Deserialization failed")
}

// Convert proof to U256-tuples as expected by the Solidity Groth16 Verifier
// Only supports bn254 for now
pub fn to_ethereum_proof(proof: Vec<u8>) -> ProofCalldata {
let deserialized_proof = deserialize_proof::<Bn254>(proof);
let proof = ethereum::Proof::from(deserialized_proof.0);
let a = G1 {
x: proof.a.x.to_string(),
y: proof.a.y.to_string(),
};
let b = G2 {
x: proof.b.x.iter().map(|x| x.to_string()).collect(),
y: proof.b.y.iter().map(|x| x.to_string()).collect(),
};
let c = G1 {
x: proof.c.x.to_string(),
y: proof.c.y.to_string(),
};
ProofCalldata { a, b, c }
}

// Only supports bn254 for now
pub fn to_ethereum_inputs(inputs: Vec<u8>) -> Vec<String> {
let deserialized_inputs = deserialize_inputs::<Bn254>(inputs);
let inputs = ethereum::Inputs::from(&deserialized_inputs.0[..]);
let inputs = inputs.0.iter().map(|x| x.to_string()).collect();
inputs
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -101,4 +133,59 @@ mod tests {
"Original and deserialized proving keys do not match"
);
}

#[test]
fn test_to_ethereum_proof() {
let raw_proof = vec![
22, 2, 28, 144, 134, 93, 1, 6, 180, 134, 137, 11, 130, 217, 116, 35, 22, 58, 213, 215,
39, 9, 76, 99, 93, 46, 166, 183, 200, 20, 234, 26, 86, 182, 126, 104, 167, 218, 67,
106, 232, 177, 113, 76, 217, 110, 167, 101, 215, 168, 67, 23, 2, 2, 50, 131, 103, 159,
241, 197, 1, 75, 72, 154, 107, 226, 61, 6, 227, 5, 193, 103, 229, 40, 232, 183, 170,
218, 136, 73, 194, 166, 135, 22, 128, 83, 94, 84, 179, 66, 38, 17, 200, 0, 107, 4, 237,
57, 13, 157, 153, 39, 204, 59, 155, 91, 76, 89, 209, 195, 76, 165, 72, 165, 188, 119,
12, 210, 184, 168, 78, 56, 125, 146, 97, 253, 159, 42, 16, 203, 73, 47, 174, 29, 163,
124, 34, 156, 218, 243, 97, 226, 65, 123, 95, 132, 40, 158, 63, 255, 94, 39, 196, 45,
251, 145, 188, 37, 155, 16, 201, 208, 50, 33, 199, 98, 119, 172, 71, 240, 191, 110,
243, 225, 180, 215, 97, 98, 252, 124, 220, 169, 163, 130, 43, 114, 242, 40, 46, 60, 6,
5, 51, 186, 24, 73, 62, 221, 213, 61, 116, 62, 159, 150, 165, 183, 78, 86, 26, 236,
214, 9, 54, 152, 13, 135, 124, 137, 89, 119, 212, 15, 212, 24, 181, 54, 115, 197, 150,
31, 22, 150, 210, 187, 28, 94, 109, 138, 22, 234, 67, 58, 115, 199, 93, 121, 182, 221,
62, 212, 88, 84, 103, 215, 109, 154,
];

let proof = to_ethereum_proof(raw_proof);
assert!(!proof.a.x.is_empty());
assert!(!proof.a.y.is_empty());
assert!(!proof.b.x.is_empty());
assert!(!proof.b.y.is_empty());
assert!(!proof.c.x.is_empty());
assert!(!proof.c.y.is_empty());
}

#[test]
fn test_to_ethereum_inputs() {
let raw_inputs = vec![
2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 240, 147, 245, 225, 67, 145, 112, 185, 121, 72, 232,
51, 40, 93, 88, 129, 129, 182, 69, 80, 184, 41, 160, 49, 225, 114, 78, 100, 48, 0, 0,
0, 240, 147, 245, 225, 67, 145, 112, 185, 121, 72, 232, 51, 40, 93, 88, 129, 129, 182,
69, 80, 184, 41, 160, 49, 225, 114, 78, 100, 48,
];
let inputs = to_ethereum_inputs(raw_inputs);
let expected_inputs = vec![
"21888242871839275222246405745257275088548364400416034343698204186575808495616",
"21888242871839275222246405745257275088548364400416034343698204186575808495616",
];
assert_eq!(inputs, expected_inputs);
}

#[test]
fn test_to_ethereum_inputs_with_zero() {
let raw_inputs = vec![
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];
let inputs = to_ethereum_inputs(raw_inputs);
let expected_inputs = vec!["0".to_string()];
assert_eq!(inputs, expected_inputs);
}
}
4 changes: 2 additions & 2 deletions circom-prover/src/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use num::{BigInt, BigUint};
use std::{collections::HashMap, str::FromStr};

/// Witness function signature for rust_witness (inputs) -> witness
pub type RustWitnessWtnsFn = fn(HashMap<String, Vec<BigInt>>) -> Vec<BigInt>;
type RustWitnessWtnsFn = fn(HashMap<String, Vec<BigInt>>) -> Vec<BigInt>;
/// Witness function signature for witnesscalc_adapter (inputs, .dat file path) -> witness
pub type WitnesscalcWtnsFn = fn(HashMap<String, Vec<BigInt>>, &str) -> Vec<BigInt>;
type WitnesscalcWtnsFn = fn(HashMap<String, Vec<BigInt>>, &str) -> Vec<BigInt>;

pub enum WitnessFn {
WitnessCalc(WitnesscalcWtnsFn),
Expand Down
40 changes: 8 additions & 32 deletions mopro-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,19 @@ default = []
ashlang = ["dep:ashlang"]
halo2 = []
circom = [
"circom-prover",
"rust-witness",
"ark-circom",
"ark-serialize",
"ark-ec",
"ark-crypto-primitives",
"ark-std",
"ark-bn254",
"ark-groth16",
"ark-relations",
"ark-ff",
"ark-bls12-381",
"num-traits",
"byteorder",
"ark-ff",
]

[dependencies]
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",] }

# Error handling
thiserror = "=2.0.3"
Expand All @@ -45,33 +40,14 @@ color-eyre = "=0.6.2"
ashlang = { git = "https://github.com/chancehudson/ashlang.git", rev = "696960a0c15db47170fdd1ff058682023d904b1b", optional = true, default-features = false, features = ["spartan-prover", "serde"] }

# circom deps
ark-circom = { git = "https://github.com/zkmopro/circom-compat.git", version = "0.1.0", branch = "wasm-delete", optional = true }
ark-serialize = { version = "=0.4.1", features = ["derive"], optional = true }
num-bigint = { version = "0.4.3", default-features = false, features = [
"rand",
] }
circom-prover = {path = "../circom-prover"}
rust-witness = { version = "0.1.1", optional = true }
ark-ff = { version = "0.4.0", optional = true }
circom-prover = {path = "../circom-prover", optional = true}

# ZKP generation
ark-ec = { version = "=0.4.1", default-features = false, features = [
"parallel",
], optional = true }
ark-crypto-primitives = { version = "=0.4.0", optional = true }
ark-std = { version = "=0.4.0", default-features = false, features = [
"parallel",
], optional = true }
ark-bn254 = { version = "=0.4.0", optional = true }
ark-groth16 = { version = "=0.4.0", default-features = false, features = [
"parallel",
], optional = true }
ark-relations = { version = "0.4", default-features = false, optional = true }
uuid = { version = "1.9.1", features = ["v4"] }
byteorder = { version = "1.0.0", optional = true }
ark-ff = { version = "0.4.0", optional = true }
ark-bls12-381 = { version = "0.4.0", optional = true }
num-traits = { version = "0.2.0", optional = true }
anyhow = "1.0.86"
bincode = "1.3.3"

[build-dependencies]
rust-witness = { version = "0.1.1", optional = true }
Expand Down
16 changes: 8 additions & 8 deletions mopro-ffi/src/circom/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
pub mod serialization;

use crate::GenerateProofResult;
use anyhow::Ok;
use anyhow::Result;
Expand Down Expand Up @@ -44,12 +42,12 @@ macro_rules! circom_app {
})
}

fn to_ethereum_proof(in0: Vec<u8>) -> mopro_ffi::ProofCalldata {
mopro_ffi::to_ethereum_proof(in0)
fn to_ethereum_proof(in0: Vec<u8>) -> circom_prover::ProofCalldata {
circom_prover::prover::serialization::to_ethereum_proof(in0)
}

fn to_ethereum_inputs(in0: Vec<u8>) -> Vec<String> {
mopro_ffi::to_ethereum_inputs(in0)
circom_prover::prover::serialization::to_ethereum_inputs(in0)
}
};
}
Expand Down Expand Up @@ -140,18 +138,19 @@ mod tests {
use std::ops::{Add, Mul};
use std::str::FromStr;

use crate::circom::{generate_circom_proof_wtns, serialization, verify_circom_proof};
use crate::circom::{generate_circom_proof_wtns, verify_circom_proof};
use crate::GenerateProofResult;
use anyhow::bail;
use anyhow::Result;
use ark_bls12_381::Bls12_381;
use ark_bn254::Bn254;
use ark_ff::PrimeField;
use circom_prover::create_witness_fn;
use circom_prover::prover::ProofLib;
use circom_prover::prover::serialization::{to_ethereum_inputs, to_ethereum_proof};
use circom_prover::prover::{serialization, ProofLib};
use circom_prover::rust_witness;
use circom_prover::witness::WitnessFn;
use num_bigint::{BigInt, BigUint, ToBigInt};
use serialization::{to_ethereum_inputs, to_ethereum_proof};

// Only build the witness functions for tests, don't bundle them into
// the final library
Expand All @@ -163,6 +162,7 @@ mod tests {
use crate as mopro_ffi;

#[test]
#[allow(dead_code)]
fn test_circom_macros() {
circom_app!();

Expand Down
Loading

0 comments on commit 82c6c52

Please sign in to comment.