circom_prover/
prover.rs

1use anyhow::Result;
2use circom::Proof;
3use num::BigUint;
4use std::{str::FromStr, thread::JoinHandle};
5
6pub mod ark_circom;
7pub mod circom;
8
9#[cfg(feature = "arkworks")]
10pub mod arkworks;
11#[cfg(feature = "rapidsnark")]
12pub mod rapidsnark;
13
14#[derive(Debug, Clone)]
15pub struct PublicInputs(pub Vec<BigUint>);
16
17#[derive(Debug, Clone)]
18pub struct CircomProof {
19    pub proof: Proof,
20    pub pub_inputs: PublicInputs,
21}
22
23#[derive(Debug, Clone, Copy)]
24pub enum ProofLib {
25    Arkworks,
26    Rapidsnark,
27}
28
29pub fn prove(
30    lib: ProofLib,
31    zkey_path: String,
32    witnesses: JoinHandle<Vec<BigUint>>,
33) -> Result<CircomProof> {
34    match lib {
35        #[cfg(feature = "arkworks")]
36        ProofLib::Arkworks => arkworks::generate_circom_proof(zkey_path, witnesses),
37        #[cfg(feature = "rapidsnark")]
38        ProofLib::Rapidsnark => rapidsnark::generate_circom_proof(zkey_path, witnesses),
39        #[allow(unreachable_patterns)]
40        _ => panic!("Unsupported proof library"),
41    }
42}
43
44pub fn verify(lib: ProofLib, zkey_path: String, proof: CircomProof) -> Result<bool> {
45    match lib {
46        #[cfg(feature = "arkworks")]
47        ProofLib::Arkworks => arkworks::verify_circom_proof(zkey_path, proof),
48        #[cfg(feature = "rapidsnark")]
49        ProofLib::Rapidsnark => rapidsnark::verify_circom_proof(zkey_path, proof),
50        #[allow(unreachable_patterns)]
51        _ => panic!("Unsupported proof library"),
52    }
53}
54
55//
56// Helper functions to convert PublicInputs to other types we need
57//
58impl From<Vec<String>> for PublicInputs {
59    fn from(src: Vec<String>) -> Self {
60        let pi = src
61            .iter()
62            .map(|str| BigUint::from_str(str).unwrap())
63            .collect();
64        PublicInputs(pi)
65    }
66}
67
68impl From<PublicInputs> for Vec<String> {
69    fn from(src: PublicInputs) -> Self {
70        src.0.iter().map(|p| p.to_string()).collect()
71    }
72}