-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from QED-it/v1.1.1-libsnark
v1.1.1 - libsnark
- Loading branch information
Showing
25 changed files
with
2,794 additions
and
703 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,8 @@ cpp/CMakeFiles/ | |
cpp/Makefile | ||
cpp/CMakeCache.txt | ||
cpp/cmake_install.cmake | ||
target/ | ||
libsnark | ||
*.a | ||
local/ | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,24 @@ | ||
# Version v1.1.1, 2020-06, libsnark and gadgetlib | ||
|
||
Interface definition: | ||
- Added fields for different data types in `KeyValue`. | ||
|
||
C++: | ||
- Added a CLI to setup/prove/verify using libsnark (see `cpp/backend.cpp`). | ||
- Added a library and CLI to call gadgets from gadgetlib TinyRAM (see `cpp/gadgetlib.cpp`). | ||
|
||
|
||
# Version 1.1.0, 2020-06, Simplifications | ||
|
||
Interface definition: | ||
- *(breaking)* Renamed R1CSContraints to ConstraintSystem. | ||
- *(breaking)* Moved r1cs_generation and witness_generation from Circuit to Command. | ||
- *(breaking)* The position of Circuit `field_maximum` and `configuration` changed (unused afaik.). | ||
- Renamed R1CSContraints to ConstraintSystem. | ||
- Added a dedicated Command message type to help with interoperable execution. | ||
- Added an example file in binary and JSON. | ||
|
||
Rust: | ||
- *(breaking)* Moved "writing" helpers to the "owned" modules. | ||
- Added "owned" versions of all message types. | ||
- Added a tool to convert to JSON. | ||
- Added a tool to convert to JSON. (`src/bin/zkif_json.rs`) | ||
- Added a tool to print basic statistics about a circuit. (`src/bin/stats.rs`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
BreakBeforeBraces: Attach |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,38 @@ | ||
cmake_minimum_required(VERSION 2.6) | ||
if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) | ||
set(CMAKE_INSTALL_PREFIX "${CMAKE_SOURCE_DIR}/target" CACHE PATH "default install path" FORCE) | ||
endif () | ||
|
||
# Optimizations above -O1 trigger this bug on MacOS: https://github.com/CodaProtocol/coda/pull/1419 | ||
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O1") | ||
|
||
project(zkinterface) | ||
|
||
set(CMAKE_CXX_STANDARD 14) | ||
set(CMAKE_CXX_STANDARD_REQUIRED ON) | ||
enable_testing() | ||
|
||
|
||
# Frontend gadgetlib. | ||
add_executable(gadgetlib gadgetlib.cpp zkinterface_utils.cpp libsnark_integration.cpp gadgetlib_example.cpp gadgetlib_alu.cpp) | ||
# Dependencies. | ||
target_link_libraries(gadgetlib ff gmp) | ||
target_include_directories(gadgetlib PRIVATE . libsnark libsnark/depends/libff libsnark/depends/libfqfft /usr/local/include) | ||
target_link_directories(gadgetlib PRIVATE ${CMAKE_SOURCE_DIR}/libsnark/libsnark/ ${CMAKE_SOURCE_DIR}/libsnark/depends/libff/libff/ /usr/local/lib) | ||
# Configuration from libsnark/libsnark/CMakeFiles/snark.dir/flags.make | ||
target_compile_definitions(gadgetlib PRIVATE -DBINARY_OUTPUT -DCURVE_ALT_BN128 -DMONTGOMERY_OUTPUT -DNO_PROCPS -DUSE_ASM) | ||
|
||
|
||
add_library(gadget_example gadget_example.cpp) | ||
include_directories(.) | ||
# Backend. | ||
add_executable(backend backend.cpp zkinterface_utils.cpp libsnark_integration.cpp) | ||
add_test(backend backend) | ||
# Dependencies. | ||
target_link_libraries(backend ff gmp) | ||
target_include_directories(backend PRIVATE . libsnark libsnark/depends/libff libsnark/depends/libfqfft /usr/local/include) | ||
target_link_directories(backend PRIVATE ${CMAKE_SOURCE_DIR}/libsnark/libsnark/ ${CMAKE_SOURCE_DIR}/libsnark/depends/libff/libff/ /usr/local/lib) | ||
# Configuration from libsnark/libsnark/CMakeFiles/snark.dir/flags.make | ||
target_compile_definitions(backend PRIVATE -DBINARY_OUTPUT -DCURVE_ALT_BN128 -DMONTGOMERY_OUTPUT -DNO_PROCPS -DUSE_ASM) | ||
|
||
install(TARGETS gadget_example DESTINATION .) | ||
install(FILES zkinterface.h zkinterface_generated.h libsnark_integration.hpp DESTINATION .) | ||
install(TARGETS gadgetlib DESTINATION .) | ||
install(FILES zkinterface.h zkinterface_generated.h libsnark_integration.hpp ../LICENSE DESTINATION .) | ||
install(DIRECTORY flatbuffers DESTINATION .) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
#include <fstream> | ||
#include <iterator> | ||
#include <chrono> | ||
#include <libff/common/default_types/ec_pp.hpp> | ||
#include <libsnark/zk_proof_systems/ppzksnark/r1cs_gg_ppzksnark/r1cs_gg_ppzksnark.hpp> | ||
#include "libsnark_zkif_import.cpp" | ||
|
||
using namespace std; | ||
using namespace zkinterface_libsnark; | ||
|
||
vector<char> read_file(string zkifPath) { | ||
ifstream zkifFile(zkifPath, ios::binary); | ||
vector<char> buf((istreambuf_iterator<char>(zkifFile)), | ||
istreambuf_iterator<char>()); | ||
|
||
if (zkifFile) { | ||
cerr << "Read messages from file " << zkifPath << endl; | ||
} else { | ||
throw "Error: could not open file"; | ||
} | ||
|
||
return buf; | ||
} | ||
|
||
protoboard<FieldT> load_protoboard(string zkifPath, bool with_constraints, bool with_witness) { | ||
CurveT::init_public_params(); | ||
libff::inhibit_profiling_info = true; | ||
|
||
protoboard<FieldT> pb; | ||
import_zkif iz(pb, "import_zkif"); | ||
|
||
auto buf = read_file(zkifPath); | ||
iz.load(buf); | ||
iz.allocate_variables(); | ||
if (with_constraints) iz.generate_constraints(); | ||
if (with_witness) iz.generate_witness(); | ||
return pb; | ||
} | ||
|
||
void print_protoboard(protoboard<FieldT> &pb) { | ||
cerr << pb.num_inputs() << " public inputs" << endl; | ||
cerr << pb.num_variables() << " variables" << endl; | ||
cerr << pb.num_constraints() << " constraints" << endl; | ||
} | ||
|
||
class Benchmark { | ||
chrono::steady_clock::time_point begin = chrono::steady_clock::now(); | ||
public: | ||
void print() { | ||
auto dur = chrono::steady_clock::now() - begin; | ||
cerr << "ZKPROOF_BENCHMARK: {" | ||
<< "\"iterations\":1, " | ||
<< "\"microseconds\":" | ||
<< chrono::duration_cast<chrono::microseconds>(dur).count() | ||
<< "}" << endl; | ||
} | ||
}; | ||
|
||
void run(string action, string zkifPath) { | ||
if (action == "validate") { | ||
auto pb = load_protoboard(zkifPath, true, true); | ||
print_protoboard(pb); | ||
cerr << "Satisfied: " << (pb.is_satisfied() ? "YES" : "NO") << endl; | ||
|
||
} else if (action == "setup") { | ||
auto pb = load_protoboard(zkifPath, true, false); | ||
|
||
auto keypair = r1cs_gg_ppzksnark_generator<CurveT>(pb.get_constraint_system()); | ||
|
||
ofstream(zkifPath + ".pk", ios::binary) << keypair.pk; | ||
ofstream(zkifPath + ".vk", ios::binary) << keypair.vk; | ||
|
||
} else if (action == "prove") { | ||
auto pb = load_protoboard(zkifPath, false, true); | ||
|
||
r1cs_gg_ppzksnark_proving_key<CurveT> pk; | ||
ifstream(zkifPath + ".pk", ios::binary) >> pk; | ||
Benchmark bench; | ||
|
||
auto proof = r1cs_gg_ppzksnark_prover<CurveT>(pk, pb.primary_input(), pb.auxiliary_input()); | ||
|
||
bench.print(); | ||
ofstream(zkifPath + ".proof", ios::binary) << proof; | ||
|
||
} else if (action == "verify") { | ||
auto pb = load_protoboard(zkifPath, false, false); | ||
|
||
r1cs_gg_ppzksnark_verification_key<CurveT> vk; | ||
ifstream(zkifPath + ".vk", ios::binary) >> vk; | ||
|
||
r1cs_gg_ppzksnark_proof<CurveT> proof; | ||
ifstream(zkifPath + ".proof", ios::binary) >> proof; | ||
Benchmark bench; | ||
|
||
auto ok = r1cs_gg_ppzksnark_verifier_strong_IC(vk, pb.primary_input(), proof); | ||
|
||
bench.print(); | ||
cout << endl << "Proof verified: " << (ok ? "YES" : "NO") << endl; | ||
} | ||
} | ||
|
||
static const char USAGE[] = | ||
R"(libsnark prover. | ||
Usage: | ||
snark validate <zkinterface_file> | ||
snark setup <zkinterface_file> | ||
snark prove <zkinterface_file> | ||
snark verify <zkinterface_file> | ||
)"; | ||
|
||
int main(int argc, const char **argv) { | ||
|
||
if (argc < 3) { | ||
cerr << USAGE << endl; | ||
return 1; | ||
} | ||
|
||
try { | ||
run(string(argv[1]), string(argv[2])); | ||
return 0; | ||
} catch (const char *msg) { | ||
cerr << msg << endl; | ||
return 2; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
# Latest. | ||
git clone https://github.com/scipr-lab/libsnark.git | ||
cd libsnark | ||
git checkout 477c9dfd07b280e42369f82f89c08416319e24ae | ||
git submodule init && git submodule update | ||
mkdir build && cd build && cmake .. | ||
make | ||
sudo make install | ||
|
||
# Deprecated. | ||
#sudo apt-get install build-essential git libgmp3-dev libprocps4-dev libgtest-dev python-markdown libboost-all-dev libssl-dev | ||
#git clone https://github.com/scipr-lab/libsnark.git | ||
#cd libsnark | ||
#git checkout deprecated-master | ||
#./prepare-depends.sh | ||
#make | ||
|
||
# MacOS | ||
#port install gmp openssl boost | ||
#cmake cmake -DWITH_PROCPS=OFF -DWITH_SUPERCOP=OFF -DCURVE=ALT_BN128 . | ||
#make |
Oops, something went wrong.