Browse Source

Key generation tested and linked in.

Thomas Kerber 1 year ago
parent
commit
b8cffbd27b
Signed by: Thomas Kerber <tk@drwx.org> GPG Key ID: 8489B911F9ED617B
5 changed files with 135 additions and 48 deletions
  1. 1
    0
      Cargo.toml
  2. 62
    11
      libsnark-sys/glue.cpp
  3. 0
    28
      libsnark-sys/glue.hpp
  4. 69
    9
      libsnark-sys/src/lib.rs
  5. 3
    0
      rustfmt.toml

+ 1
- 0
Cargo.toml View File

@@ -4,3 +4,4 @@ version = "0.1.0"
4 4
 authors = ["Thomas Kerber <tk@drwx.org>"]
5 5
 
6 6
 [dependencies]
7
+libsnark-sys = {path="libsnark-sys"}

+ 62
- 11
libsnark-sys/glue.cpp View File

@@ -1,8 +1,57 @@
1
-#include "glue.hpp"
1
+#include <libsnark/common/default_types/r1cs_gg_ppzksnark_pp.hpp>
2
+#include <libsnark/zk_proof_systems/ppzksnark/r1cs_gg_ppzksnark/r1cs_gg_ppzksnark.hpp>
2 3
 
3
-keypair_t *generate_keypair(cs_t *cs) {
4
-    return new keypair_t(
5
-        libsnark::r1cs_gg_ppzksnark_generator<default_curve>(*cs));
4
+extern "C" {
5
+
6
+typedef libsnark::default_r1cs_gg_ppzksnark_pp default_curve;
7
+typedef libff::Fr<libff::default_ec_pp> field_t;
8
+
9
+typedef libsnark::r1cs_constraint_system<field_t> cs_t;
10
+typedef libsnark::r1cs_primary_input<field_t> primary_input_t;
11
+typedef libsnark::r1cs_auxiliary_input<field_t> auxiliary_input_t;
12
+typedef libsnark::r1cs_gg_ppzksnark_keypair<default_curve> keypair_t;
13
+typedef libsnark::r1cs_gg_ppzksnark_processed_verification_key<default_curve>
14
+    pvk_t;
15
+typedef libsnark::r1cs_gg_ppzksnark_proving_key<default_curve> pk_t;
16
+typedef libsnark::r1cs_gg_ppzksnark_verification_key<default_curve> vk_t;
17
+typedef libsnark::r1cs_gg_ppzksnark_proof<default_curve> proof_t;
18
+typedef libsnark::r1cs_constraint<field_t> constraint_t;
19
+typedef libsnark::linear_term<field_t> linear_term_t;
20
+typedef libsnark::linear_combination<field_t> linear_combination_t;
21
+
22
+cs_t *init_cs_t(size_t inputs, size_t aux_inputs) {
23
+    auto ret = new cs_t();
24
+    ret->primary_input_size = inputs;
25
+    ret->auxiliary_input_size = aux_inputs;
26
+    return ret;
27
+}
28
+
29
+linear_combination_t *init_linear_combination() {
30
+    return new linear_combination_t();
31
+}
32
+
33
+constraint_t *init_constraint(
34
+        linear_combination_t *a,
35
+        linear_combination_t *b,
36
+        linear_combination_t *c) {
37
+    return new constraint_t(*a, *b, *c);
38
+}
39
+
40
+void add_linear_term(linear_combination_t *comb, size_t var_index, long coefficient) {
41
+    linear_term_t term;
42
+    term.index = var_index;
43
+    term.coeff = field_t(coefficient);
44
+    comb->terms.push_back(term);
45
+}
46
+
47
+void add_constraint(cs_t *cs, constraint_t *con) {
48
+    cs->add_constraint(*con);
49
+}
50
+
51
+void generate_keypair(cs_t *cs, pk_t **pk_out, vk_t **vk_out) {
52
+    auto kp = libsnark::r1cs_gg_ppzksnark_generator<default_curve>(*cs);
53
+    *pk_out = new pk_t(kp.pk);
54
+    *vk_out = new vk_t(kp.vk);
6 55
 }
7 56
 
8 57
 pvk_t *process_verification_key(vk_t *vk) {
@@ -30,14 +79,16 @@ void init() {
30 79
     default_curve::init_public_params();
31 80
 }
32 81
 
33
-void deinit_keypair(keypair_t *kp) {
34
-    delete kp;
82
+#define DEINIT(TYPE) void deinit_ ## TYPE (TYPE *ptr) {\
83
+    delete ptr;\
35 84
 }
36 85
 
37
-void deinit_pvk(pvk_t *pvk) {
38
-    delete pvk;
39
-}
86
+DEINIT(pk_t);
87
+DEINIT(vk_t);
88
+DEINIT(pvk_t);
89
+DEINIT(proof_t);
90
+DEINIT(cs_t);
91
+DEINIT(linear_combination_t);
92
+DEINIT(constraint_t);
40 93
 
41
-void deinit_proof(proof_t *proof) {
42
-    delete proof;
43 94
 }

+ 0
- 28
libsnark-sys/glue.hpp View File

@@ -1,28 +0,0 @@
1
-#include <libsnark/common/default_types/r1cs_gg_ppzksnark_pp.hpp>
2
-#include <libsnark/zk_proof_systems/ppzksnark/r1cs_gg_ppzksnark/r1cs_gg_ppzksnark.hpp>
3
-
4
-extern "C" {
5
-
6
-typedef libsnark::default_r1cs_gg_ppzksnark_pp default_curve;
7
-typedef libff::Fr<libff::default_ec_pp> field_t;
8
-
9
-typedef libsnark::r1cs_constraint_system<field_t> cs_t;
10
-typedef libsnark::r1cs_primary_input<field_t> primary_input_t;
11
-typedef libsnark::r1cs_auxiliary_input<field_t> auxiliary_input_t;
12
-typedef libsnark::r1cs_gg_ppzksnark_keypair<default_curve> keypair_t;
13
-typedef libsnark::r1cs_gg_ppzksnark_processed_verification_key<default_curve>
14
-    pvk_t;
15
-typedef libsnark::r1cs_gg_ppzksnark_proving_key<default_curve> pk_t;
16
-typedef libsnark::r1cs_gg_ppzksnark_verification_key<default_curve> vk_t;
17
-typedef libsnark::r1cs_gg_ppzksnark_proof<default_curve> proof_t;
18
-
19
-keypair_t *generate_keypair(cs_t *cs);
20
-pvk_t *process_verification_key(vk_t *vk);
21
-proof_t *prove(
22
-    pk_t *pk,
23
-    primary_input_t *primary_input,
24
-    auxiliary_input_t *auxiliary_input);
25
-int verify(pvk_t *pvk, primary_input_t *input, proof_t *proof);
26
-
27
-void init();
28
-}

+ 69
- 9
libsnark-sys/src/lib.rs View File

@@ -1,39 +1,99 @@
1
+#![allow(non_camel_case_types)]
1 2
 extern crate libc;
2 3
 
3
-use libc::{c_void, c_int};
4
+use libc::{c_int, c_long, c_void, size_t};
5
+
6
+// TODO:
7
+// - funcs for primary/auxiliary input
8
+// - funcs for (de)serialization of:
9
+//   - primary input
10
+//   - vk
11
+//   - pk
12
+//   - pvk
13
+//   - proof
14
+//   - (cs?)
4 15
 
5 16
 pub type cs_t = c_void;
6
-pub type keypair_t = c_void;
7 17
 pub type vk_t = c_void;
8 18
 pub type pvk_t = c_void;
9 19
 pub type primary_input_t = c_void;
10 20
 pub type auxiliary_input_t = c_void;
11 21
 pub type pk_t = c_void;
12 22
 pub type proof_t = c_void;
23
+pub type constraint_t = c_void;
24
+pub type linear_combination_t = c_void;
13 25
 
14 26
 extern "C" {
15
-    pub fn generate_keypair(cs: *const cs_t) -> *mut keypair_t;
27
+    pub fn init_cs_t(inputs: size_t, aux_inputs: size_t) -> *mut cs_t;
28
+    pub fn init_linear_combination() -> *mut linear_combination_t;
29
+    pub fn init_constraint(
30
+        a: *const linear_combination_t,
31
+        b: *const linear_combination_t,
32
+        c: *const linear_combination_t,
33
+    ) -> *mut constraint_t;
34
+    pub fn add_linear_term(
35
+        comb: *mut linear_combination_t,
36
+        var_index: size_t,
37
+        coefficient: c_long,
38
+    );
39
+    pub fn add_constraint(cs: *mut cs_t, con: *const constraint_t);
40
+    pub fn generate_keypair(
41
+        cs: *const cs_t,
42
+        pk_out: *mut *mut pk_t,
43
+        vk_out: *mut *mut vk_t,
44
+    );
16 45
     pub fn process_verification_key(vk: *const vk_t) -> *mut pvk_t;
17 46
     pub fn prove(
18 47
         pk: *const pk_t,
19 48
         primary_input: *const primary_input_t,
20 49
         auxiliary_input: *const auxiliary_input_t,
21 50
     ) -> *mut proof_t;
22
-    pub fn verify(pvk: *const pvk_t, input: *const primary_input_t, proof: *const proof_t)
23
-        -> c_int;
24
-    pub fn deinit_keypair(kp: *mut keypair_t);
25
-    pub fn deinit_pvk(pvk: *mut pvk_t);
26
-    pub fn deinit_proof(proof: *mut proof_t);
51
+    pub fn verify(
52
+        pvk: *const pvk_t,
53
+        input: *const primary_input_t,
54
+        proof: *const proof_t,
55
+    ) -> c_int;
56
+    pub fn deinit_pk_t(kp: *mut pk_t);
57
+    pub fn deinit_vk_t(kp: *mut vk_t);
58
+    pub fn deinit_pvk_t(pvk: *mut pvk_t);
59
+    pub fn deinit_proof_t(proof: *mut proof_t);
60
+    pub fn deinit_cs_t(cs: *mut cs_t);
61
+    pub fn deinit_linear_combination_t(lt: *mut linear_combination_t);
62
+    pub fn deinit_constraint_t(con: *mut constraint_t);
27 63
     pub fn init();
28 64
 }
29 65
 
30 66
 #[cfg(test)]
31 67
 mod tests {
32 68
     use super::*;
69
+    use std::ptr::null_mut;
33 70
     #[test]
34
-    fn it_works() {
71
+    fn test_keygen() {
35 72
         unsafe {
36 73
             init();
74
+            let lc1 = init_linear_combination();
75
+            let lc2 = init_linear_combination();
76
+            let lc3 = init_linear_combination();
77
+            add_linear_term(lc1, 0, 1);
78
+            add_linear_term(lc1, 1, 1);
79
+            add_linear_term(lc2, 2, 1);
80
+            add_linear_term(lc3, 3, 1);
81
+            // (1+a)*b = c
82
+            let con = init_constraint(lc1, lc2, lc3);
83
+            let cs = init_cs_t(2, 1);
84
+            add_constraint(cs, con);
85
+            let mut pk: *mut pk_t = null_mut();
86
+            let mut vk: *mut vk_t = null_mut();
87
+            generate_keypair(cs, &mut pk, &mut vk);
88
+            let pvk = process_verification_key(vk);
89
+            deinit_pvk_t(pvk);
90
+            deinit_pk_t(pk);
91
+            deinit_vk_t(vk);
92
+            deinit_cs_t(cs);
93
+            deinit_constraint_t(con);
94
+            deinit_linear_combination_t(lc1);
95
+            deinit_linear_combination_t(lc2);
96
+            deinit_linear_combination_t(lc3);
37 97
         }
38 98
     }
39 99
 }

+ 3
- 0
rustfmt.toml View File

@@ -0,0 +1,3 @@
1
+reorder_imported_names = true
2
+reorder_imports = true
3
+max_width = 79

Loading…
Cancel
Save