semaphore_rs_proof/
lib.rs

1use ruint::aliases::U256;
2use serde::{Deserialize, Serialize};
3
4#[cfg(feature = "ark")]
5mod ark;
6
7pub mod compression;
8pub mod packing;
9
10// Matches the private G1Tup type in ark-circom.
11pub type G1 = (U256, U256);
12
13// Matches the private G2Tup type in ark-circom.
14pub type G2 = ([U256; 2], [U256; 2]);
15
16/// Wrap a proof object so we have serde support
17#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
18pub struct Proof(pub G1, pub G2, pub G1);
19
20impl Proof {
21    pub const fn from_flat(flat: [U256; 8]) -> Self {
22        let [x0, x1, x2, x3, x4, x5, x6, x7] = flat;
23        Self((x0, x1), ([x2, x3], [x4, x5]), (x6, x7))
24    }
25
26    pub const fn flatten(self) -> [U256; 8] {
27        let Self((a0, a1), ([bx0, bx1], [by0, by1]), (c0, c1)) = self;
28        [a0, a1, bx0, bx1, by0, by1, c0, c1]
29    }
30}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35
36    #[test]
37    fn deser() {
38        let s = r#"[["0x1","0x2"],[["0x3","0x4"],["0x5","0x6"]],["0x7","0x8"]]"#;
39
40        let deserialized: Proof = serde_json::from_str(s).unwrap();
41        let reserialized = serde_json::to_string(&deserialized).unwrap();
42
43        assert_eq!(s, reserialized);
44    }
45}