Skip to content
This repository has been archived by the owner on Oct 28, 2024. It is now read-only.

Commit

Permalink
Merge pull request #34 from HarryR/reduce-build-times
Browse files Browse the repository at this point in the history
Reduce build times + C++ style improvements
  • Loading branch information
HarryR authored Aug 29, 2018
2 parents eac23e2 + 6bdadc4 commit 749dbfd
Show file tree
Hide file tree
Showing 25 changed files with 316 additions and 223 deletions.
44 changes: 25 additions & 19 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,73 +4,79 @@ include_directories(
${DEPENDS_DIR}/libsnark/depends/libff
${DEPENDS_DIR}/libsnark/depends/libfqfft)

add_library(ethsnarks_common STATIC export.cpp import.cpp stubs.cpp utils.cpp)
target_link_libraries(ethsnarks_common snark)

add_library(miximus SHARED mod/miximus.cpp)
target_link_libraries(miximus snark)
set_property(TARGET miximus PROPERTY POSITION_INDEPENDENT_CODE ON)
add_library(miximus_objs OBJECT mod/miximus.cpp)

add_library(miximus_static STATIC $<TARGET_OBJECTS:miximus_objs>)
target_link_libraries(miximus_static ethsnarks_common)

add_library(miximus SHARED $<TARGET_OBJECTS:miximus_objs>)
target_link_libraries(miximus miximus_static)
set_property(TARGET miximus PROPERTY POSITION_INDEPENDENT_CODE ON)

add_library(hashpreimage SHARED mod/hashpreimage.cpp)
target_link_libraries(hashpreimage snark)
target_link_libraries(hashpreimage ethsnarks_common)
set_property(TARGET hashpreimage PROPERTY POSITION_INDEPENDENT_CODE ON)


add_executable(miximus_cli miximus_cli.cpp)
target_link_libraries(miximus_cli snark)
target_link_libraries(miximus_cli miximus_static)


add_executable(hashpreimage_cli hashpreimage_cli.cpp)
target_link_libraries(hashpreimage_cli snark)
target_link_libraries(hashpreimage_cli ethsnarks_common)


add_executable(verify verify.cpp)
target_link_libraries(verify snark)
target_link_libraries(verify ethsnarks_common)


add_executable(test_vk_raw2json test/test_vk_raw2json.cpp)
target_link_libraries(test_vk_raw2json snark)
target_link_libraries(test_vk_raw2json ethsnarks_common)


add_executable(test_load_proofkey test/test_load_proofkey.cpp)
target_link_libraries(test_load_proofkey snark)
target_link_libraries(test_load_proofkey ethsnarks_common)


add_executable(test_shamir_poly test/test_shamir_poly.cpp)
target_link_libraries(test_shamir_poly snark)
target_link_libraries(test_shamir_poly ethsnarks_common)


add_executable(test_sha256_full_gadget test/test_sha256_full_gadget.cpp)
target_link_libraries(test_sha256_full_gadget snark)
target_link_libraries(test_sha256_full_gadget ethsnarks_common)


add_executable(test_proof_raw2json test/test_proof_raw2json.cpp)
target_link_libraries(test_proof_raw2json snark)
target_link_libraries(test_proof_raw2json ethsnarks_common)


add_executable(test_field_packing test/test_field_packing.cpp)
target_link_libraries(test_field_packing snark)
target_link_libraries(test_field_packing ethsnarks_common)


add_executable(test_hashpreimage test/test_hashpreimage.cpp)
target_link_libraries(test_hashpreimage snark)
target_link_libraries(test_hashpreimage ethsnarks_common)


add_executable(test_one_of_n test/test_one_of_n.cpp)
target_link_libraries(test_one_of_n snark)
target_link_libraries(test_one_of_n ethsnarks_common)


add_executable(test_longsightf test/test_longsightf.cpp)
target_link_libraries(test_longsightf snark)
target_link_libraries(test_longsightf ethsnarks_common)


add_executable(test_longsightf_bits test/test_longsightf_bits.cpp)
target_link_libraries(test_longsightf_bits snark)
target_link_libraries(test_longsightf_bits ethsnarks_common)


add_executable(test_longsightf_merkletree test/test_longsightf_merkletree.cpp)
target_link_libraries(test_longsightf_merkletree snark)
target_link_libraries(test_longsightf_merkletree ethsnarks_common)


add_executable(test_r1cs_gg_ppzksnark_zok r1cs_gg_ppzksnark_zok/tests/test_r1cs_gg_zok_ppzksnark.cpp)
target_link_libraries(test_r1cs_gg_ppzksnark_zok snark)
target_link_libraries(test_r1cs_gg_ppzksnark_zok ethsnarks_common)

30 changes: 30 additions & 0 deletions src/ethsnarks.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef ETHSNARKS_HPP_
#define ETHSNARKS_HPP_

#include <libff/algebra/curves/alt_bn128/alt_bn128_pp.hpp>
#include <libsnark/gadgetlib1/protoboard.hpp>
#include "r1cs_gg_ppzksnark_zok/r1cs_gg_ppzksnark_zok.hpp"


namespace ethsnarks {

typedef libff::bigint<libff::alt_bn128_r_limbs> LimbT;
typedef libff::alt_bn128_G1 G1T;
typedef libff::alt_bn128_G2 G2T;
typedef libff::alt_bn128_pp ppT;
typedef libff::Fr<ppT> FieldT;
typedef libsnark::protoboard<FieldT> ProtoboardT;

typedef libsnark::r1cs_gg_ppzksnark_zok_proof<ppT> ProofT;
typedef libsnark::r1cs_gg_ppzksnark_zok_proving_key<ppT> ProvingKeyT;
typedef libsnark::r1cs_gg_ppzksnark_zok_verification_key<ppT> VerificationKeyT;
typedef libsnark::r1cs_gg_ppzksnark_zok_primary_input<ppT> PrimaryInputT;
typedef libsnark::r1cs_gg_ppzksnark_zok_auxiliary_input<ppT> AuxiliaryInputT;

//using ProverF = libsnark::r1cs_gg_ppzksnark_zok_prover<ppT>;
//typedef libsnark::r1cs_gg_ppzksnark_zok_verifier_strong_IC<ppT> VerifierF;
//typedef libsnark::r1cs_gg_ppzksnark_zok_generator<ppT> GeneratorF;

}

#endif
28 changes: 11 additions & 17 deletions src/export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,17 @@
along with Semaphore. If not, see <https://www.gnu.org/licenses/>.
*/

#pragma once

#include <fstream>
#include <iostream>
#include <cassert>
#include <iomanip>



#include "r1cs_gg_ppzksnark_zok/r1cs_gg_ppzksnark_zok.hpp"

#include <libff/algebra/curves/alt_bn128/alt_bn128_pp.hpp>

#include <libsnark/gadgetlib1/gadget.hpp>

#include "ethsnarks.hpp"

using namespace libsnark;
using namespace libff;
namespace ethsnarks {


std::string HexStringFromBigint(libff::bigint<libff::alt_bn128_r_limbs> _x){
Expand All @@ -55,7 +48,7 @@ std::string HexStringFromBigint(libff::bigint<libff::alt_bn128_r_limbs> _x){

std::string outputPointG1AffineAsHex(libff::alt_bn128_G1 _p)
{
libff::alt_bn128_G1 aff = _p;
G1T aff = _p;
aff.to_affine_coordinates();
//std::stringstream ss;
//ss << "0x" << aff.X.as_bigint() << "," << aff.Y.as_bigint() << "," << aff.Z.as_bigint();
Expand All @@ -66,7 +59,7 @@ std::string outputPointG1AffineAsHex(libff::alt_bn128_G1 _p)

std::string outputPointG2AffineAsHex(libff::alt_bn128_G2 _p)
{
libff::alt_bn128_G2 aff = _p;
G2T aff = _p;

if (aff.Z.c0.as_bigint() != "0" && aff.Z.c1.as_bigint() != "0" ) {
aff.to_affine_coordinates();
Expand All @@ -79,8 +72,7 @@ std::string outputPointG2AffineAsHex(libff::alt_bn128_G2 _p)
}


template<typename ppT>
std::string proof_to_json(r1cs_gg_ppzksnark_zok_proof<ppT> &proof, r1cs_primary_input<libff::Fr<ppT>> &input) {
std::string proof_to_json(ProofT &proof, PrimaryInputT &input) {
std::stringstream ss;

ss << "{\n";
Expand All @@ -104,8 +96,8 @@ std::string proof_to_json(r1cs_gg_ppzksnark_zok_proof<ppT> &proof, r1cs_primary_
return(ss.str());
}

template<typename ppT>
std::string vk2json(r1cs_gg_ppzksnark_zok_verification_key<ppT> &vk )

std::string vk2json(VerificationKeyT &vk )
{
std::stringstream ss;
unsigned icLength = vk.gamma_ABC_g1.rest.indices.size() + 1;
Expand All @@ -129,12 +121,14 @@ std::string vk2json(r1cs_gg_ppzksnark_zok_verification_key<ppT> &vk )
}


template<typename ppT>
void vk2json_file(r1cs_gg_ppzksnark_zok_verification_key<ppT> &vk, std::string path )
void vk2json_file(VerificationKeyT &vk, const std::string &path )
{
std::ofstream fh;
fh.open(path, std::ios::binary);
fh << vk2json(vk);
fh.flush();
fh.close();
}

}
// namespace ethsnarks
22 changes: 22 additions & 0 deletions src/export.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef ETHSNARKS_EXPORT_HPP_
#define ETHSNARKS_EXPORT_HPP_

#include "ethsnarks.hpp"

namespace ethsnarks {

std::string HexStringFromBigint( LimbT _x);

std::string outputPointG1AffineAsHex( G1T _p );

std::string outputPointG2AffineAsHex( G2T _p );

std::string proof_to_json( ProofT &proof, PrimaryInputT &input );

std::string vk2json( VerificationKeyT &vk );

void vk2json_file( VerificationKeyT &vk, const std::string &path );

}

#endif
4 changes: 3 additions & 1 deletion src/gadgets/one_of_n.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
#include <libsnark/gadgetlib1/gadget.hpp>
#include <libsnark/gadgetlib1/gadgets/basic_gadgets.hpp>

#include "ethsnarks.hpp"

using libsnark::gadget;
using libsnark::pb_variable;
using libsnark::pb_variable_array;
using libsnark::protoboard;
using libsnark::r1cs_constraint;
using libsnark::generate_boolean_r1cs_constraint;
using ethsnarks::FieldT;


/**
Expand Down Expand Up @@ -42,7 +45,6 @@ using libsnark::generate_boolean_r1cs_constraint;
*
* This ensures that only 1 item is toggled, and whichever one it is is ours.
*/
template<typename FieldT>
class one_of_n : public gadget<FieldT>
{
public:
Expand Down
2 changes: 1 addition & 1 deletion src/gadgets/sha256_full.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

#include <openssl/sha.h> // SHA256_CTX

#include "utils.cpp"
#include "utils.hpp"

using namespace libsnark;

Expand Down
6 changes: 5 additions & 1 deletion src/hashpreimage_cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
#include <fstream> // ofstream

#include "mod/hashpreimage.cpp"
#include "utils.cpp" // hex_to_bytes
#include "utils.hpp"
#include "stubs.hpp"


using std::cerr;
Expand All @@ -15,6 +16,9 @@ using std::ofstream;
using std::ifstream;
using std::stringstream;

using ethsnarks::stub_main_genkeys;
using ethsnarks::stub_main_verify;


static int main_prove( int argc, char **argv )
{
Expand Down
7 changes: 5 additions & 2 deletions src/miximus_cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@
#include <fstream> // ofstream

#include "mod/miximus.cpp"
#include "stubs.cpp"
#include "utils.cpp" // hex_to_bytes
#include "stubs.hpp"
#include "utils.hpp" // hex_to_bytes


using std::cerr;
using std::cout;
using std::endl;
using std::ofstream;

using ethsnarks::stub_main_genkeys;
using ethsnarks::stub_main_verify;


static int main_prove( int argc, char **argv )
{
Expand Down
27 changes: 14 additions & 13 deletions src/mod/hashpreimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@

#include "hashpreimage.hpp"

#include "ethsnarks.hpp"

#include "gadgets/sha256_full.cpp"
#include "utils.cpp"
#include "export.cpp"
#include "utils.hpp"
#include "export.hpp"
#include "import.cpp"
#include "stubs.cpp"
#include "stubs.hpp"

#include <libff/algebra/curves/alt_bn128/alt_bn128_pp.hpp>
#include <libff/algebra/fields/field_utils.hpp>

#include <openssl/sha.h>

using ethsnarks::FieldT;
using ethsnarks::ProvingKeyT;
using ethsnarks::ppT;
using ethsnarks::proof_to_json;

template<typename FieldT>
pb_variable_array<FieldT> pb_variable_array_allocate( protoboard<FieldT> &in_pb, size_t n, const std::string &annotation_prefix )
{
pb_variable_array<FieldT> res;
Expand All @@ -27,7 +31,6 @@ pb_variable_array<FieldT> pb_variable_array_allocate( protoboard<FieldT> &in_pb,
/**
* Verify that SHA256(private<512bit_block>) == public<output>
*/
template<typename FieldT>
class mod_hashpreimage : public gadget<FieldT>
{
public:
Expand Down Expand Up @@ -64,7 +67,7 @@ class mod_hashpreimage : public gadget<FieldT>
input_size_in_fields( libff::div_ceil(input_size_in_bits, FieldT::capacity()) ),

// packed input, given to prover/verifier
input_as_field_elements( pb_variable_array_allocate<FieldT>(in_pb, input_size_in_fields, FMT(annotation_prefix, " input_as_field_elements")) ),
input_as_field_elements( pb_variable_array_allocate(in_pb, input_size_in_fields, FMT(annotation_prefix, " input_as_field_elements")) ),

// public input digest, must match output
expected_digest(in_pb, SHA256_digest_size, FMT(annotation_prefix, " expected_digest")),
Expand Down Expand Up @@ -149,12 +152,10 @@ class mod_hashpreimage : public gadget<FieldT>

char *hashpreimage_prove( const char *pk_file, const uint8_t *preimage_bytes64 )
{
typedef libff::alt_bn128_pp ppT;
typedef libff::Fr<ppT> FieldT;
ppT::init_public_params();

protoboard<FieldT> pb;
mod_hashpreimage<FieldT> mod(pb, "module");
mod_hashpreimage mod(pb, "module");
mod.generate_r1cs_constraints();
mod.generate_r1cs_witness(preimage_bytes64);

Expand All @@ -163,7 +164,7 @@ char *hashpreimage_prove( const char *pk_file, const uint8_t *preimage_bytes64 )
return nullptr;
}

auto proving_key = loadFromFile<r1cs_gg_ppzksnark_zok_proving_key<ppT>>(pk_file);
auto proving_key = loadFromFile<ProvingKeyT>(pk_file);
// TODO: verify if proving key was loaded correctly, if not return NULL

auto primary_input = pb.primary_input();
Expand All @@ -176,11 +177,11 @@ char *hashpreimage_prove( const char *pk_file, const uint8_t *preimage_bytes64 )

int hashpreimage_genkeys( const char *pk_file, const char *vk_file )
{
return stub_genkeys<mod_hashpreimage>(pk_file, vk_file);
return ethsnarks::stub_genkeys<mod_hashpreimage>(pk_file, vk_file);
}


bool hashpreimage_verify( const char *vk_json, const char *proof_json )
{
return stub_verify( vk_json, proof_json );
return ethsnarks::stub_verify( vk_json, proof_json );
}
Loading

0 comments on commit 749dbfd

Please sign in to comment.