semaphore_rs_proof/
packing.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
use std::{
    fmt::Display,
    str::{from_utf8, FromStr},
};

use alloy_core::sol_types::{
    sol_data::{FixedArray, Uint},
    SolType, SolValue,
};
use serde::{Deserialize, Deserializer, Serialize, Serializer};

use crate::Proof;
use semaphore_rs_utils::{bytes_from_hex, bytes_to_hex, deserialize_bytes, serialize_bytes};

/// A packed proof is a representation of the ZKP in a single attribute (as
/// opposed to array of arrays) which is easier to transport
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct PackedProof(pub [u8; 256]);

impl From<Proof> for PackedProof {
    fn from(proof: Proof) -> Self {
        let flat_proof = [
            proof.0 .0,
            proof.0 .1,
            proof.1 .0[0],
            proof.1 .0[1],
            proof.1 .1[0],
            proof.1 .1[1],
            proof.2 .0,
            proof.2 .1,
        ];

        let bytes = flat_proof.abi_encode();
        let mut encoded = [0u8; 256];
        encoded.copy_from_slice(&bytes[..256]);
        Self(encoded)
    }
}

impl From<PackedProof> for Proof {
    fn from(proof: PackedProof) -> Self {
        let decoded = FixedArray::<Uint<256>, 8>::abi_decode(&proof.0, true).unwrap();

        let a = (decoded[0], decoded[1]);
        let b = ([decoded[2], decoded[3]], [decoded[4], decoded[5]]);
        let c = (decoded[6], decoded[7]);

        Self(a, b, c)
    }
}

impl Display for PackedProof {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let hex = bytes_to_hex::<256, 514>(&self.0);
        write!(
            f,
            "{}",
            from_utf8(&hex).expect("failed to convert to string")
        )
    }
}

impl FromStr for PackedProof {
    type Err = hex::FromHexError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        bytes_from_hex::<256>(s).map(Self)
    }
}

impl Serialize for PackedProof {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serialize_bytes::<256, 514, S>(serializer, &self.0)
    }
}

impl<'de> Deserialize<'de> for PackedProof {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let bytes = deserialize_bytes::<256, _>(deserializer)?;
        Ok(Self(bytes))
    }
}

#[cfg(test)]
pub mod test {
    use super::*;
    use ruint::aliases::U256;

    #[test]
    fn test_serializing_proof_into_packed_proof() {
        let proof = Proof(
            (U256::from(1), U256::from(2)),
            (
                [U256::from(3), U256::from(4)],
                [U256::from(5), U256::from(6)],
            ),
            (U256::from(7), U256::from(8)),
        );

        let packed_proof = PackedProof::from(proof);

        assert_eq!(packed_proof.to_string(), "0x00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000005000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000070000000000000000000000000000000000000000000000000000000000000008");

        let proof2 = Proof::from(packed_proof);

        assert_eq!(proof, proof2);
    }

    #[test]
    fn test_parse_from_string() {
        let packed_proof_str =  "0x00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000005000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000070000000000000000000000000000000000000000000000000000000000000008";

        let packed_proof = PackedProof::from_str(packed_proof_str).unwrap();

        let expected_proof = Proof(
            (U256::from(1), U256::from(2)),
            (
                [U256::from(3), U256::from(4)],
                [U256::from(5), U256::from(6)],
            ),
            (U256::from(7), U256::from(8)),
        );

        let proof: Proof = packed_proof.into();

        assert_eq!(proof, expected_proof);
    }

    #[test]
    fn test_parse_from_string_without_prefix() {
        // note the lack of 0x prefix
        let packed_proof_str =  "00000000000000000000000000000000000000000000000000000000000000050000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000005000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000070000000000000000000000000000000000000000000000000000000000000008";

        let packed_proof = PackedProof::from_str(packed_proof_str).unwrap();

        let expected_proof = Proof(
            (U256::from(5), U256::from(6)),
            (
                [U256::from(3), U256::from(4)],
                [U256::from(5), U256::from(6)],
            ),
            (U256::from(7), U256::from(8)),
        );

        let proof: Proof = packed_proof.into();

        assert_eq!(proof, expected_proof);
    }

    #[test]
    fn test_serialize_proof_to_json() {
        let packed_proof_str =  "0x00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000005000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000070000000000000000000000000000000000000000000000000000000000000008";

        let packed_proof = PackedProof::from_str(packed_proof_str).unwrap();
        let proof: Proof = packed_proof.into();

        let serialized = serde_json::to_value(proof).unwrap();

        assert_eq!(
            serialized,
            serde_json::json!([
                ["0x1", "0x2"],
                [["0x3", "0x4"], ["0x5", "0x6"]],
                ["0x7", "0x8"]
            ])
        );
    }

    #[test]
    fn test_serialize_proof_to_json_real_numbers() {
        let packed_proof_str =  "0x15c1fc6907219676890dfe147ee6f10b580c7881dddacb1567b3bcbfc513a54d233afda3efff43a7631990d2e79470abcbae3ccad4b920476e64745bfe97bb0a0c8c7d7434c382d590d601d951c29c8463d555867db70f9e84f7741c81c2e1e6241d2ddf1c9e6670a24109a0e9c915cd6e07d0248a384dd38d3c91e9b0419f5f0b23c5467a06eff56cc2c246ada1e7d5705afc4dc8b43fd5a6972c679a2019c5091ed6522f7924d3674d08966a008f947f9aa016a4100bb12f911326f3e1befd0acdf5a5996e00933206cbec48f3bbdcee2a4ca75f8db911c00001e5a05474872446d6f1c1506837392a30fdc73d66fd89f4e1b1a5d14b93e2ad0c5f7b777520";

        let packed_proof = PackedProof::from_str(packed_proof_str).unwrap();
        let proof: Proof = packed_proof.into();

        let serialized = serde_json::to_value(proof).unwrap();

        assert_eq!(
            serialized,
            serde_json::json!([
                [
                    "0x15c1fc6907219676890dfe147ee6f10b580c7881dddacb1567b3bcbfc513a54d",
                    "0x233afda3efff43a7631990d2e79470abcbae3ccad4b920476e64745bfe97bb0a"
                ],
                [
                    [
                        "0xc8c7d7434c382d590d601d951c29c8463d555867db70f9e84f7741c81c2e1e6",
                        "0x241d2ddf1c9e6670a24109a0e9c915cd6e07d0248a384dd38d3c91e9b0419f5f"
                    ],
                    [
                        "0xb23c5467a06eff56cc2c246ada1e7d5705afc4dc8b43fd5a6972c679a2019c5",
                        "0x91ed6522f7924d3674d08966a008f947f9aa016a4100bb12f911326f3e1befd"
                    ]
                ],
                [
                    "0xacdf5a5996e00933206cbec48f3bbdcee2a4ca75f8db911c00001e5a0547487",
                    "0x2446d6f1c1506837392a30fdc73d66fd89f4e1b1a5d14b93e2ad0c5f7b777520"
                ]
            ])
        );
    }

    #[test]
    fn test_deserialize_proof_from_json() {
        let proof_str = "[
            [
                \"0x15c1fc6907219676890dfe147ee6f10b580c7881dddacb1567b3bcbfc513a54d\",
                \"0x233afda3efff43a7631990d2e79470abcbae3ccad4b920476e64745bfe97bb0a\"
            ],
            [
                [
                    \"0xc8c7d7434c382d590d601d951c29c8463d555867db70f9e84f7741c81c2e1e6\",
                    \"0x241d2ddf1c9e6670a24109a0e9c915cd6e07d0248a384dd38d3c91e9b0419f5f\"
                ],
                [
                    \"0xb23c5467a06eff56cc2c246ada1e7d5705afc4dc8b43fd5a6972c679a2019c5\",
                    \"0x91ed6522f7924d3674d08966a008f947f9aa016a4100bb12f911326f3e1befd\"
                ]
            ],
            [
                \"0xacdf5a5996e00933206cbec48f3bbdcee2a4ca75f8db911c00001e5a0547487\",
                \"0x2446d6f1c1506837392a30fdc73d66fd89f4e1b1a5d14b93e2ad0c5f7b777520\"
            ]
        ]";

        let proof = serde_json::from_str::<Proof>(proof_str).unwrap();

        let packed_proof = PackedProof::from(proof);

        let expected_proof =  "0x15c1fc6907219676890dfe147ee6f10b580c7881dddacb1567b3bcbfc513a54d233afda3efff43a7631990d2e79470abcbae3ccad4b920476e64745bfe97bb0a0c8c7d7434c382d590d601d951c29c8463d555867db70f9e84f7741c81c2e1e6241d2ddf1c9e6670a24109a0e9c915cd6e07d0248a384dd38d3c91e9b0419f5f0b23c5467a06eff56cc2c246ada1e7d5705afc4dc8b43fd5a6972c679a2019c5091ed6522f7924d3674d08966a008f947f9aa016a4100bb12f911326f3e1befd0acdf5a5996e00933206cbec48f3bbdcee2a4ca75f8db911c00001e5a05474872446d6f1c1506837392a30fdc73d66fd89f4e1b1a5d14b93e2ad0c5f7b777520";

        assert_eq!(packed_proof.to_string(), expected_proof);
    }

    #[test]
    fn test_invalid_parsing() {
        // note this is only 7 numbers
        let packed_proof_str =  "0x0000000000000000000000000000000000000000000000000000000000000005000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000007";
        PackedProof::from_str(packed_proof_str).expect_err("parsing should fail");

        // not a valid number
        let packed_proof_str =  "0000000000000000p000000000000000000000000000000000000000000000050000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000005000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000070000000000000000000000000000000000000000000000000000000000000008";
        PackedProof::from_str(packed_proof_str).expect_err("parsing should fail");

        // completely invalid
        let packed_proof_str = "0x0";
        PackedProof::from_str(packed_proof_str).expect_err("parsing should fail");
    }
}