From 45111d657627a70a3ab83ab1b98c6fbb5263d310 Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Thu, 24 Dec 2015 02:58:38 -0700 Subject: [PATCH] Initial commit --- .gitignore | 2 ++ .gitmodules | 3 +++ Cargo.toml | 13 +++++++++++++ LICENSE-MIT | 21 +++++++++++++++++++++ README.md | 1 + src/main.rs | 5 +++++ tinysnark/.gitignore | 2 ++ tinysnark/Cargo.toml | 13 +++++++++++++ tinysnark/build.rs | 40 +++++++++++++++++++++++++++++++++++++++ tinysnark/libsnark | 1 + tinysnark/src/lib.rs | 9 +++++++++ tinysnark/tinysnark.cpp | 42 +++++++++++++++++++++++++++++++++++++++++ 12 files changed, 152 insertions(+) create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 Cargo.toml create mode 100644 LICENSE-MIT create mode 100644 README.md create mode 100644 src/main.rs create mode 100644 tinysnark/.gitignore create mode 100644 tinysnark/Cargo.toml create mode 100644 tinysnark/build.rs create mode 160000 tinysnark/libsnark create mode 100644 tinysnark/src/lib.rs create mode 100644 tinysnark/tinysnark.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a9d37c5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +target +Cargo.lock diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..24d4152 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "tinysnark/libsnark"] + path = tinysnark/libsnark + url = git://github.com/scipr-lab/libsnark.git diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..8bc53bb --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "bellman" +version = "0.0.1" +authors = ["Sean Bowe "] +homepage = "https://github.com/ebfull/bellman" +repository = "https://github.com/ebfull/bellman" +documentation = "https://github.com/ebfull/bellman" +license = "MIT" +description = "zk-SNARK library" + +[dependencies.tinysnark] +path = "tinysnark" +version = "0.0.1" \ No newline at end of file diff --git a/LICENSE-MIT b/LICENSE-MIT new file mode 100644 index 0000000..6415849 --- /dev/null +++ b/LICENSE-MIT @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2015 Sean Bowe + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..758af65 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +Bellman is a Rust language zk-SNARK crate. \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..6b95114 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,5 @@ +extern crate tinysnark; + +fn main() { + tinysnark::test(); +} \ No newline at end of file diff --git a/tinysnark/.gitignore b/tinysnark/.gitignore new file mode 100644 index 0000000..a9d37c5 --- /dev/null +++ b/tinysnark/.gitignore @@ -0,0 +1,2 @@ +target +Cargo.lock diff --git a/tinysnark/Cargo.toml b/tinysnark/Cargo.toml new file mode 100644 index 0000000..40af785 --- /dev/null +++ b/tinysnark/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "tinysnark" +homepage = "https://github.com/ebfull/bellman" +repository = "https://github.com/ebfull/bellman" +documentation = "https://github.com/ebfull/bellman" +license = "MIT" +description = "Tiny libsnark bindings" +version = "0.0.1" +authors = ["Sean Bowe "] +build = "build.rs" + +[build-dependencies] +gcc = "0.3" diff --git a/tinysnark/build.rs b/tinysnark/build.rs new file mode 100644 index 0000000..5601419 --- /dev/null +++ b/tinysnark/build.rs @@ -0,0 +1,40 @@ +extern crate gcc; + +fn main() { + // we don't need ate-pairing for ALT_BN128, but + // i'll keep this in case i need it for some reason... + /* + let mut cfg = gcc::Config::new(); + + cfg.cpp(true) + .define("BN_SUPPORT_SNARK", None) + .include("ate-pairing/include") + .include("xbyak") + .file("ate-pairing/src/zm.cpp") + .file("ate-pairing/src/zm2.cpp") + .compile("libzm.a"); + */ + + println!("cargo:rustc-link-lib=gmp"); + println!("cargo:rustc-link-lib=gmpxx"); + + let mut cfg = gcc::Config::new(); + + cfg.cpp(true) + .define("NO_PROCPS", None) + .define("STATIC", None) + .define("CURVE_ALT_BN128", None) + .flag("-std=c++11") + .include("libsnark/src") + .file("tinysnark.cpp") + .file("libsnark/src/algebra/curves/alt_bn128/alt_bn128_g1.cpp") + .file("libsnark/src/algebra/curves/alt_bn128/alt_bn128_g2.cpp") + .file("libsnark/src/algebra/curves/alt_bn128/alt_bn128_init.cpp") + .file("libsnark/src/algebra/curves/alt_bn128/alt_bn128_pairing.cpp") + .file("libsnark/src/algebra/curves/alt_bn128/alt_bn128_pp.cpp") + .file("libsnark/src/common/utils.cpp") + .file("libsnark/src/common/profiling.cpp") + ; + + cfg.compile("libtinysnark.a"); +} \ No newline at end of file diff --git a/tinysnark/libsnark b/tinysnark/libsnark new file mode 160000 index 0000000..0b928a7 --- /dev/null +++ b/tinysnark/libsnark @@ -0,0 +1 @@ +Subproject commit 0b928a7b36717db6f67ff7e1e34dfa3bfaee1c97 diff --git a/tinysnark/src/lib.rs b/tinysnark/src/lib.rs new file mode 100644 index 0000000..c2e8bda --- /dev/null +++ b/tinysnark/src/lib.rs @@ -0,0 +1,9 @@ +extern "C" { + fn tinysnark_init_public_params(); + fn tinysnark_test(); +} + +pub fn test() { + unsafe { tinysnark_init_public_params(); } + unsafe { tinysnark_test(); } +} \ No newline at end of file diff --git a/tinysnark/tinysnark.cpp b/tinysnark/tinysnark.cpp new file mode 100644 index 0000000..9976826 --- /dev/null +++ b/tinysnark/tinysnark.cpp @@ -0,0 +1,42 @@ +/* +This is a wrapper around libsnark which provides basic R1CS +zk-SNARK support using the ALT_BN128 curve. +*/ + +#include "gadgetlib1/gadgets/basic_gadgets.hpp" +#include "zk_proof_systems/ppzksnark/r1cs_ppzksnark/r1cs_ppzksnark.hpp" +#include "common/default_types/r1cs_ppzksnark_pp.hpp" +#include "common/utils.hpp" + +using namespace libsnark; +using namespace std; + +extern "C" void tinysnark_init_public_params() { + default_r1cs_ppzksnark_pp::init_public_params(); +} + +extern "C" void tinysnark_test() { + typedef Fr FieldT; + + protoboard pb; + + linear_combination sum; + + sum = sum + 1; + + pb.add_r1cs_constraint(r1cs_constraint(1, sum, 1), "testing"); + + assert(pb.is_satisfied()); + + const r1cs_constraint_system constraint_system = pb.get_constraint_system(); + + cout << "Number of R1CS constraints: " << constraint_system.num_constraints() << endl; + + auto keypair = r1cs_ppzksnark_generator(constraint_system); + + auto proof = r1cs_ppzksnark_prover(keypair.pk, pb.primary_input(), pb.auxiliary_input()); + + r1cs_primary_input input; + + assert(r1cs_ppzksnark_verifier_strong_IC(keypair.vk, input, proof)); +} \ No newline at end of file