Browse Source

Alright. Serialization works.

Thomas Kerber 1 year ago
parent
commit
e8e76de4c0
Signed by: Thomas Kerber <tk@drwx.org> GPG Key ID: 8489B911F9ED617B
5 changed files with 126 additions and 20 deletions
  1. 1
    0
      Cargo.toml
  2. 4
    8
      libsnark-sys/build.rs
  3. 52
    5
      libsnark-sys/glue.cpp
  4. 27
    6
      libsnark-sys/src/lib.rs
  5. 42
    1
      src/lib.rs

+ 1
- 0
Cargo.toml View File

@@ -6,3 +6,4 @@ authors = ["Thomas Kerber <tk@drwx.org>"]
6 6
 [dependencies]
7 7
 libsnark-sys = {path="libsnark-sys"}
8 8
 rand = "*"
9
+hex = "*"

+ 4
- 8
libsnark-sys/build.rs View File

@@ -4,11 +4,7 @@ extern crate pkg_config;
4 4
 
5 5
 fn main() {
6 6
     let dst = cmake::Config::new("libsnark")
7
-        .define("BINARY_OUTPUT", "")
8
-        .define("BN_SUPPORT_SNARK", "1")
9 7
         .define("CURVE_BN128", "")
10
-        .define("MONTGOMERY_OUTPUT", "")
11
-        .define("USE_ASM", "")
12 8
         .build();
13 9
     cc::Build::new()
14 10
         .cpp(true)
@@ -16,10 +12,10 @@ fn main() {
16 12
         .include("libsnark/depends/libff")
17 13
         .include("libsnark/depends/libfqfft")
18 14
         .define("BINARY_OUTPUT", "")
19
-        .define("BN_SUPPORT_SNARK", "1")
20
-        .define("CURVE_BN128", "")
21
-        .define("MONTGOMERY_OUTPUT", "")
22
-        .define("USE_ASM", "")
15
+        .define("BN_SUPPORT_SNARK", "")
16
+        .define("CURVE_BN128", "a")
17
+        .define("MONTGOMERY_OUTPUT", "a")
18
+        .define("USE_ASM", "a")
23 19
         .file("glue.cpp")
24 20
         .compile("glue");
25 21
     println!(

+ 52
- 5
libsnark-sys/glue.cpp View File

@@ -116,7 +116,7 @@ DEINIT(string_t);
116 116
 #define SERDE(TYPE) \
117 117
     std::string *libsnark_ser_ ## TYPE (TYPE *ptr) {\
118 118
         std::stringstream ss;\
119
-        ss << *ptr;\
119
+        static_cast<std::ostream&>(ss) << *ptr;\
120 120
         return new std::string(ss.str());\
121 121
     }\
122 122
     TYPE *libsnark_de ## TYPE(std::string *str) {\
@@ -127,11 +127,58 @@ DEINIT(string_t);
127 127
         return ret;\
128 128
     }
129 129
 
130
-
131
-SERDE(vk_t);
132
-SERDE(pk_t);
133
-SERDE(pvk_t);
134 130
 SERDE(proof_t);
135 131
 SERDE(input_t);
136 132
 
133
+std::string *libsnark_ser_vk_t(vk_t *ptr, size_t inputs) {
134
+    std::stringstream ss;
135
+    static_cast<std::ostream&>(ss) << inputs;
136
+    static_cast<std::ostream&>(ss) << *ptr;
137
+    return new std::string(ss.str());
138
+}
139
+
140
+void libsnark_de_vk_t(std::string *str, vk_t **vk_out, size_t *inputs_out) {
141
+    *vk_out = new vk_t;
142
+    std::stringstream ss;
143
+    ss.str(*str);
144
+    static_cast<std::istream&>(ss) >> *inputs_out;
145
+    static_cast<std::istream&>(ss) >> **vk_out;
146
+}
147
+
148
+std::string *libsnark_ser_pk_t(pk_t *ptr, size_t inputs, size_t aux_inputs) {
149
+    std::stringstream ss;
150
+    static_cast<std::ostream&>(ss) << inputs;
151
+    static_cast<std::ostream&>(ss) << aux_inputs;
152
+    static_cast<std::ostream&>(ss) << *ptr;
153
+    return new std::string(ss.str());
154
+}
155
+
156
+void libsnark_de_pk_t(
157
+        std::string *str,
158
+        pk_t **pk_out,
159
+        size_t *inputs_out,
160
+        size_t *aux_inputs_out) {
161
+    *pk_out = new pk_t;
162
+    std::stringstream ss;
163
+    ss.str(*str);
164
+    static_cast<std::istream&>(ss) >> *inputs_out;
165
+    static_cast<std::istream&>(ss) >> *aux_inputs_out;
166
+    static_cast<std::istream&>(ss) >> **pk_out;
167
+}
168
+
169
+std::string *libsnark_ser_pvk_t(pvk_t *ptr, size_t inputs) {
170
+    std::stringstream ss;
171
+    static_cast<std::ostream&>(ss) << inputs;
172
+    static_cast<std::ostream&>(ss) << *ptr;
173
+    return new std::string(ss.str());
174
+}
175
+
176
+void libsnark_de_pvk_t(std::string *str, pvk_t **pvk_out, size_t *inputs_out) {
177
+    *pvk_out = new pvk_t;
178
+    std::stringstream ss;
179
+    ss.str(*str);
180
+    static_cast<std::istream&>(ss) >> *inputs_out;
181
+    static_cast<std::istream&>(ss) >> **pvk_out;
182
+}
183
+
137 184
 }

+ 27
- 6
libsnark-sys/src/lib.rs View File

@@ -23,14 +23,35 @@ pub enum linear_combination_t {}
23 23
 pub enum string_t {}
24 24
 
25 25
 extern "C" {
26
-    pub fn libsnark_ser_vk_t(vk: *const vk_t) -> *mut string_t;
27
-    pub fn libsnark_ser_pk_t(pk: *const pk_t) -> *mut string_t;
28
-    pub fn libsnark_ser_pvk_t(pk: *const pk_t) -> *mut string_t;
26
+    pub fn libsnark_ser_vk_t(vk: *const vk_t, inputs: size_t)
27
+        -> *mut string_t;
28
+    pub fn libsnark_ser_pk_t(
29
+        pk: *const pk_t,
30
+        inputs: size_t,
31
+        aux_inputs: size_t,
32
+    ) -> *mut string_t;
33
+    pub fn libsnark_ser_pvk_t(
34
+        pvk: *const pvk_t,
35
+        inputs: size_t,
36
+    ) -> *mut string_t;
29 37
     pub fn libsnark_ser_proof_t(proof: *const proof_t) -> *mut string_t;
30 38
     pub fn libsnark_ser_input_t(input: *const input_t) -> *mut string_t;
31
-    pub fn libsnark_de_vk_t(from: *const string_t) -> *mut vk_t;
32
-    pub fn libsnark_de_pk_t(from: *const string_t) -> *mut pk_t;
33
-    pub fn libsnark_de_pvk_t(from: *const string_t) -> *mut pvk_t;
39
+    pub fn libsnark_de_vk_t(
40
+        from: *const string_t,
41
+        vk_out: *mut *mut vk_t,
42
+        inputs_out: *mut size_t,
43
+    );
44
+    pub fn libsnark_de_pk_t(
45
+        from: *const string_t,
46
+        pk_out: *mut *mut pk_t,
47
+        inputs_out: *mut size_t,
48
+        aux_inputs_out: *mut size_t,
49
+    ) -> *mut pk_t;
50
+    pub fn libsnark_de_pvk_t(
51
+        from: *const string_t,
52
+        pvk_out: *mut *mut pvk_t,
53
+        inputs_out: *mut size_t,
54
+    );
34 55
     pub fn libsnark_de_proof_t(from: *const string_t) -> *mut proof_t;
35 56
     pub fn libsnark_de_input_t(from: *const string_t) -> *mut input_t;
36 57
     pub fn libsnark_init_string_t(

+ 42
- 1
src/lib.rs View File

@@ -26,6 +26,32 @@ impl CppString {
26 26
     }
27 27
 }
28 28
 
29
+impl<'a> From<&'a ProvingKey> for CppString {
30
+    fn from(pk: &ProvingKey) -> CppString {
31
+        CppString(unsafe {
32
+            libsnark_ser_pk_t(pk.inner, pk.inputs, pk.aux_inputs)
33
+        })
34
+    }
35
+}
36
+
37
+impl<'a> From<&'a VerificationKey> for CppString {
38
+    fn from(vk: &VerificationKey) -> CppString {
39
+        CppString(unsafe { libsnark_ser_vk_t(vk.inner, vk.inputs) })
40
+    }
41
+}
42
+
43
+impl<'a> From<&'a ProcessedVerificationKey> for CppString {
44
+    fn from(pvk: &ProcessedVerificationKey) -> CppString {
45
+        CppString(unsafe { libsnark_ser_pvk_t(pvk.inner, pvk.inputs) })
46
+    }
47
+}
48
+
49
+impl<'a> From<&'a Proof> for CppString {
50
+    fn from(proof: &Proof) -> CppString {
51
+        CppString(unsafe { libsnark_ser_proof_t(proof.0) })
52
+    }
53
+}
54
+
29 55
 impl AsRef<[u8]> for CppString {
30 56
     fn as_ref(&self) -> &[u8] {
31 57
         unsafe {
@@ -242,7 +268,8 @@ impl ProvingKey {
242 268
     ///
243 269
     /// # Panics
244 270
     ///
245
-    /// Fails external assertions if the inputs given do not satisfy the constraints.
271
+    /// Fails external assertions if the inputs given do not satisfy the
272
+    /// constraints.
246 273
     pub fn prove(
247 274
         &self,
248 275
         primary_inputs: &[i64],
@@ -268,6 +295,14 @@ impl ProvingKey {
268 295
             res
269 296
         }))
270 297
     }
298
+
299
+    pub fn primary_inputs(&self) -> usize {
300
+        self.inputs
301
+    }
302
+
303
+    pub fn auxiliary_inputs(&self) -> usize {
304
+        self.aux_inputs
305
+    }
271 306
 }
272 307
 
273 308
 impl Drop for ProvingKey {
@@ -300,6 +335,10 @@ impl ProcessedVerificationKey {
300 335
             res != 0
301 336
         })
302 337
     }
338
+
339
+    pub fn inputs(&self) -> usize {
340
+        self.inputs
341
+    }
303 342
 }
304 343
 
305 344
 impl Drop for ProcessedVerificationKey {
@@ -310,6 +349,8 @@ impl Drop for ProcessedVerificationKey {
310 349
 
311 350
 #[cfg(test)]
312 351
 mod tests {
352
+    extern crate hex;
353
+    use self::hex::ToHex;
313 354
     use super::*;
314 355
 
315 356
     #[test]

Loading…
Cancel
Save