Browse Source

Very basic start.

Thomas Kerber 1 year ago
commit
3e212f163c
Signed by: Thomas Kerber <tk@drwx.org> GPG Key ID: 8489B911F9ED617B
10 changed files with 179 additions and 0 deletions
  1. 3
    0
      .gitignore
  2. 3
    0
      .gitmodules
  3. 6
    0
      Cargo.toml
  4. 13
    0
      libsnark-sys/Cargo.toml
  5. 37
    0
      libsnark-sys/build.rs
  6. 43
    0
      libsnark-sys/glue.cpp
  7. 28
    0
      libsnark-sys/glue.hpp
  8. 1
    0
      libsnark-sys/libsnark
  9. 39
    0
      libsnark-sys/src/lib.rs
  10. 6
    0
      src/lib.rs

+ 3
- 0
.gitignore View File

@@ -0,0 +1,3 @@
1
+**/target/
2
+**/*.rs.bk
3
+Cargo.lock

+ 3
- 0
.gitmodules View File

@@ -0,0 +1,3 @@
1
+[submodule "libsnark-sys/--recursive"]
2
+	path = libsnark-sys/libsnark
3
+	url = https://git.drwx.org/phd/libsnark.git

+ 6
- 0
Cargo.toml View File

@@ -0,0 +1,6 @@
1
+[package]
2
+name = "usnark"
3
+version = "0.1.0"
4
+authors = ["Thomas Kerber <tk@drwx.org>"]
5
+
6
+[dependencies]

+ 13
- 0
libsnark-sys/Cargo.toml View File

@@ -0,0 +1,13 @@
1
+[package]
2
+name = "libsnark-sys"
3
+version = "0.1.0"
4
+authors = ["Thomas Kerber <tk@drwx.org>"]
5
+build = "build.rs"
6
+
7
+[dependencies]
8
+libc = "0.2"
9
+
10
+[build-dependencies]
11
+cmake = "0.1"
12
+cc = "1.0"
13
+pkg-config = "0.3"

+ 37
- 0
libsnark-sys/build.rs View File

@@ -0,0 +1,37 @@
1
+extern crate cmake;
2
+extern crate cc;
3
+extern crate pkg_config;
4
+
5
+fn main() {
6
+    let dst = cmake::Config::new("libsnark")
7
+        .define("BINARY_OUTPUT", "")
8
+        .define("BN_SUPPORT_SNARK", "1")
9
+        .define("CURVE_BN128", "")
10
+        .define("MONTGOMERY_OUTPUT", "")
11
+        .define("USE_ASM", "")
12
+        .build();
13
+    cc::Build::new()
14
+        .cpp(true)
15
+        .include("libsnark")
16
+        .include("libsnark/depends/libff")
17
+        .include("libsnark/depends/libfqfft")
18
+        .define("BINARY_OUTPUT", "")
19
+        .define("BN_SUPPORT_SNARK", "1")
20
+        .define("CURVE_BN128", "")
21
+        .define("MONTGOMERY_OUTPUT", "")
22
+        .define("USE_ASM", "")
23
+        .file("glue.cpp")
24
+        .compile("glue");
25
+    println!(
26
+        "cargo:rustc-link-search=native={}/build/depends",
27
+        dst.display()
28
+    );
29
+    pkg_config::probe_library("libcrypto").unwrap();
30
+    pkg_config::probe_library("libprocps").unwrap();
31
+    println!("cargo:rustc-link-search=native={}/lib", dst.display());
32
+    println!("cargo:rustc-link-lib=static=snark");
33
+    println!("cargo:rustc-link-lib=static=ff");
34
+    println!("cargo:rustc-link-lib=static=zm");
35
+    println!("cargo:rustc-link-lib=gmp");
36
+    println!("cargo:rustc-link-lib=gmpxx");
37
+}

+ 43
- 0
libsnark-sys/glue.cpp View File

@@ -0,0 +1,43 @@
1
+#include "glue.hpp"
2
+
3
+keypair_t *generate_keypair(cs_t *cs) {
4
+    return new keypair_t(
5
+        libsnark::r1cs_gg_ppzksnark_generator<default_curve>(*cs));
6
+}
7
+
8
+pvk_t *process_verification_key(vk_t *vk) {
9
+    return new pvk_t(
10
+        libsnark::r1cs_gg_ppzksnark_verifier_process_vk<default_curve>(*vk));
11
+}
12
+
13
+proof_t *prove(
14
+        pk_t *pk,
15
+        primary_input_t *primary_input,
16
+        auxiliary_input_t *auxiliary_input) {
17
+    return new proof_t(libsnark::r1cs_gg_ppzksnark_prover<default_curve>(
18
+        *pk, *primary_input, *auxiliary_input));
19
+}
20
+
21
+int verify(pvk_t *pvk, primary_input_t *input, proof_t *proof) {
22
+    return (int)
23
+        libsnark::r1cs_gg_ppzksnark_online_verifier_strong_IC<default_curve>(
24
+            *pvk, *input, *proof);
25
+}
26
+
27
+void init() {
28
+    libff::inhibit_profiling_info = true;
29
+    libff::inhibit_profiling_counters = true;
30
+    default_curve::init_public_params();
31
+}
32
+
33
+void deinit_keypair(keypair_t *kp) {
34
+    delete kp;
35
+}
36
+
37
+void deinit_pvk(pvk_t *pvk) {
38
+    delete pvk;
39
+}
40
+
41
+void deinit_proof(proof_t *proof) {
42
+    delete proof;
43
+}

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

@@ -0,0 +1,28 @@
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
+}

+ 1
- 0
libsnark-sys/libsnark

@@ -0,0 +1 @@
1
+Subproject commit 2cf466656368ef4c7c1e9f871a613d24ff97c429

+ 39
- 0
libsnark-sys/src/lib.rs View File

@@ -0,0 +1,39 @@
1
+extern crate libc;
2
+
3
+use libc::{c_void, c_int};
4
+
5
+pub type cs_t = c_void;
6
+pub type keypair_t = c_void;
7
+pub type vk_t = c_void;
8
+pub type pvk_t = c_void;
9
+pub type primary_input_t = c_void;
10
+pub type auxiliary_input_t = c_void;
11
+pub type pk_t = c_void;
12
+pub type proof_t = c_void;
13
+
14
+extern "C" {
15
+    pub fn generate_keypair(cs: *const cs_t) -> *mut keypair_t;
16
+    pub fn process_verification_key(vk: *const vk_t) -> *mut pvk_t;
17
+    pub fn prove(
18
+        pk: *const pk_t,
19
+        primary_input: *const primary_input_t,
20
+        auxiliary_input: *const auxiliary_input_t,
21
+    ) -> *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);
27
+    pub fn init();
28
+}
29
+
30
+#[cfg(test)]
31
+mod tests {
32
+    use super::*;
33
+    #[test]
34
+    fn it_works() {
35
+        unsafe {
36
+            init();
37
+        }
38
+    }
39
+}

+ 6
- 0
src/lib.rs View File

@@ -0,0 +1,6 @@
1
+#[cfg(test)]
2
+mod tests {
3
+    #[test]
4
+    fn it_works() {
5
+    }
6
+}

Loading…
Cancel
Save