Skip to content

Commit 2a94a3f

Browse files
authored
Serialize Circom proof (#598)
1 parent 19a94ab commit 2a94a3f

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

circom-prover/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,14 @@ num = { version = "0.4.0" }
5656
num-traits = { version = "0.2.15", default-features = false }
5757
num-bigint = { version = "0.4.3", default-features = false, features = [
5858
"rand",
59+
"serde",
5960
] }
6061
anyhow = "1.0.95"
6162
rust-witness = { workspace = true, optional = true }
6263
byteorder = { version = "1.0.0" }
6364
uuid = { version = "1.9.1", features = ["v4"] }
6465
serde_json = "1.0.94"
66+
serde = { version = "1.0", features = ["derive"] }
6567

6668
# arkworks
6769
ark-ec = { version = "=0.5.0", default-features = false, features = [

circom-prover/src/prover.rs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use anyhow::Result;
22
use circom::Proof;
33
use num::BigUint;
4+
use serde::{Deserialize, Serialize};
45
use std::{str::FromStr, thread::JoinHandle};
56

67
pub mod ark_circom;
@@ -11,15 +12,54 @@ pub mod arkworks;
1112
#[cfg(feature = "rapidsnark")]
1213
pub mod rapidsnark;
1314

14-
#[derive(Debug, Clone)]
15+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
1516
pub struct PublicInputs(pub Vec<BigUint>);
1617

17-
#[derive(Debug, Clone)]
18+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
1819
pub struct CircomProof {
1920
pub proof: Proof,
2021
pub pub_inputs: PublicInputs,
2122
}
2223

24+
#[cfg(test)]
25+
mod tests {
26+
use super::*;
27+
use crate::prover::circom::{Proof, G1, G2};
28+
use num::BigUint;
29+
30+
#[test]
31+
fn serde_roundtrip_circom_proof() {
32+
let a = G1 {
33+
x: BigUint::from(1u32),
34+
y: BigUint::from(2u32),
35+
z: BigUint::from(1u32),
36+
};
37+
let b = G2 {
38+
x: [BigUint::from(3u32), BigUint::from(4u32)],
39+
y: [BigUint::from(5u32), BigUint::from(6u32)],
40+
z: [BigUint::from(1u32), BigUint::from(0u32)],
41+
};
42+
let c = G1 {
43+
x: BigUint::from(7u32),
44+
y: BigUint::from(8u32),
45+
z: BigUint::from(1u32),
46+
};
47+
let proof = Proof {
48+
a,
49+
b,
50+
c,
51+
protocol: "groth16".to_string(),
52+
curve: "bn128".to_string(),
53+
};
54+
let pub_inputs = PublicInputs(vec![BigUint::from(9u32), BigUint::from(10u32)]);
55+
let cp = CircomProof { proof, pub_inputs };
56+
57+
let serialized = serde_json::to_string(&cp).unwrap();
58+
let deserialized: CircomProof = serde_json::from_str(&serialized).unwrap();
59+
assert_eq!(cp, deserialized);
60+
}
61+
}
62+
2363
#[derive(Debug, Clone, Copy)]
2464
pub enum ProofLib {
2565
Arkworks,

circom-prover/src/prover/circom.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use ark_ff::{BigInteger, PrimeField};
1515
use ark_serialize::CanonicalDeserialize;
1616
use num::BigUint;
1717
use num_traits::Zero;
18+
use serde::{Deserialize, Serialize};
1819

1920
pub const PROTOCOL_GROTH16: &str = "groth16";
2021
pub const CURVE_BN254: &str = "bn128";
@@ -57,7 +58,7 @@ impl From<Inputs> for Vec<bls12_381_Fr> {
5758
}
5859

5960
// Follow the interface: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/snarkjs/index.d.cts
60-
#[derive(Default, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
61+
#[derive(Default, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
6162
pub struct G1 {
6263
pub x: BigUint,
6364
pub y: BigUint,
@@ -112,7 +113,7 @@ impl G1 {
112113
}
113114
}
114115

115-
#[derive(Default, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
116+
#[derive(Default, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
116117
pub struct G2 {
117118
pub x: [BigUint; 2],
118119
pub y: [BigUint; 2],
@@ -184,7 +185,7 @@ impl G2 {
184185
}
185186
}
186187

187-
#[derive(Default, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
188+
#[derive(Default, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
188189
pub struct Proof {
189190
pub a: G1,
190191
pub b: G2,

0 commit comments

Comments
 (0)