diff --git a/src/zcash/JoinSplit.cpp b/src/zcash/JoinSplit.cpp index d24f2174f..0a052209a 100644 --- a/src/zcash/JoinSplit.cpp +++ b/src/zcash/JoinSplit.cpp @@ -13,131 +13,22 @@ #include "tinyformat.h" #include "sync.h" #include "amount.h" -extern int64_t MAX_MONEY; #include "librustzcash.h" #include "streams.h" #include "version.h" -using namespace libsnark; - namespace libzcash { -#include "zcash/circuit/gadget.tcc" - static CCriticalSection cs_ParamsIO; -template -void saveToFile(const std::string path, T& obj) { - LOCK(cs_ParamsIO); - - std::stringstream ss; - ss << obj; - std::ofstream fh; - fh.open(path, std::ios::binary); - ss.rdbuf()->pubseekpos(0, std::ios_base::out); - fh << ss.rdbuf(); - fh.flush(); - fh.close(); -} - -template -void loadFromFile(const std::string path, T& objIn) { - LOCK(cs_ParamsIO); - - std::stringstream ss; - std::ifstream fh(path, std::ios::binary); - - if(!fh.is_open()) { - throw std::runtime_error(strprintf("could not load param file at %s", path)); - } - - ss << fh.rdbuf(); - fh.close(); - - ss.rdbuf()->pubseekpos(0, std::ios_base::in); - - T obj; - ss >> obj; - - objIn = std::move(obj); -} - template class JoinSplitCircuit : public JoinSplit { public: - typedef default_r1cs_ppzksnark_pp ppzksnark_ppT; - typedef Fr FieldT; - - r1cs_ppzksnark_verification_key vk; - r1cs_ppzksnark_processed_verification_key vk_precomp; - std::string pkPath; - - JoinSplitCircuit(const std::string vkPath, const std::string pkPath) : pkPath(pkPath) { - loadFromFile(vkPath, vk); - vk_precomp = r1cs_ppzksnark_verifier_process_vk(vk); - } + JoinSplitCircuit() {} ~JoinSplitCircuit() {} - static void generate(const std::string r1csPath, - const std::string vkPath, - const std::string pkPath) - { - protoboard pb; - - joinsplit_gadget g(pb); - g.generate_r1cs_constraints(); - - auto r1cs = pb.get_constraint_system(); - - saveToFile(r1csPath, r1cs); - - r1cs_ppzksnark_keypair keypair = r1cs_ppzksnark_generator(r1cs); - - saveToFile(vkPath, keypair.vk); - saveToFile(pkPath, keypair.pk); - } - - bool verify( - const PHGRProof& proof, - ProofVerifier& verifier, - const uint256& joinSplitPubKey, - const uint256& randomSeed, - const std::array& macs, - const std::array& nullifiers, - const std::array& commitments, - uint64_t vpub_old, - uint64_t vpub_new, - const uint256& rt - ) { - try { - auto r1cs_proof = proof.to_libsnark_proof>(); - - uint256 h_sig = this->h_sig(randomSeed, nullifiers, joinSplitPubKey); - - auto witness = joinsplit_gadget::witness_map( - rt, - h_sig, - macs, - nullifiers, - commitments, - vpub_old, - vpub_new - ); - - return verifier.check( - vk, - vk_precomp, - witness, - r1cs_proof - ); - } catch (...) { - return false; - } - } - SproutProof prove( - bool makeGrothProof, const std::array& inputs, const std::array& outputs, std::array& out_notes, @@ -268,74 +159,59 @@ public: out_macs[i] = PRF_pk(inputs[i].key, i, h_sig); } - if (makeGrothProof) { - if (!computeProof) { - return GrothProof(); - } - - GrothProof proof; + if (!computeProof) { + return GrothProof(); + } - CDataStream ss1(SER_NETWORK, PROTOCOL_VERSION); - ss1 << inputs[0].witness.path(); - std::vector auth1(ss1.begin(), ss1.end()); + GrothProof proof; - CDataStream ss2(SER_NETWORK, PROTOCOL_VERSION); - ss2 << inputs[1].witness.path(); - std::vector auth2(ss2.begin(), ss2.end()); + CDataStream ss1(SER_NETWORK, PROTOCOL_VERSION); + ss1 << inputs[0].witness.path(); + std::vector auth1(ss1.begin(), ss1.end()); - librustzcash_sprout_prove( - proof.begin(), + CDataStream ss2(SER_NETWORK, PROTOCOL_VERSION); + ss2 << inputs[1].witness.path(); + std::vector auth2(ss2.begin(), ss2.end()); - phi.begin(), - rt.begin(), - h_sig.begin(), + librustzcash_sprout_prove( + proof.begin(), - inputs[0].key.begin(), - inputs[0].note.value(), - inputs[0].note.rho.begin(), - inputs[0].note.r.begin(), - auth1.data(), + phi.begin(), + rt.begin(), + h_sig.begin(), - inputs[1].key.begin(), - inputs[1].note.value(), - inputs[1].note.rho.begin(), - inputs[1].note.r.begin(), - auth2.data(), + inputs[0].key.begin(), + inputs[0].note.value(), + inputs[0].note.rho.begin(), + inputs[0].note.r.begin(), + auth1.data(), - out_notes[0].a_pk.begin(), - out_notes[0].value(), - out_notes[0].r.begin(), + inputs[1].key.begin(), + inputs[1].note.value(), + inputs[1].note.rho.begin(), + inputs[1].note.r.begin(), + auth2.data(), - out_notes[1].a_pk.begin(), - out_notes[1].value(), - out_notes[1].r.begin(), + out_notes[0].a_pk.begin(), + out_notes[0].value(), + out_notes[0].r.begin(), - vpub_old, - vpub_new - ); + out_notes[1].a_pk.begin(), + out_notes[1].value(), + out_notes[1].r.begin(), - return proof; - } + vpub_old, + vpub_new + ); - throw std::invalid_argument("Cannot create non-Groth16 Sprout proofs"); + return proof; } }; template -void JoinSplit::Generate(const std::string r1csPath, - const std::string vkPath, - const std::string pkPath) -{ - initialize_curve_params(); - JoinSplitCircuit::generate(r1csPath, vkPath, pkPath); -} - -template -JoinSplit* JoinSplit::Prepared(const std::string vkPath, - const std::string pkPath) +JoinSplit* JoinSplit::Prepared() { - initialize_curve_params(); - return new JoinSplitCircuit(vkPath, pkPath); + return new JoinSplitCircuit(); } template diff --git a/src/zcash/JoinSplit.hpp b/src/zcash/JoinSplit.hpp index c37926ede..09e31570f 100644 --- a/src/zcash/JoinSplit.hpp +++ b/src/zcash/JoinSplit.hpp @@ -56,11 +56,7 @@ class JoinSplit { public: virtual ~JoinSplit() {} - static void Generate(const std::string r1csPath, - const std::string vkPath, - const std::string pkPath); - static JoinSplit* Prepared(const std::string vkPath, - const std::string pkPath); + static JoinSplit* Prepared(); static uint256 h_sig(const uint256& randomSeed, const std::array& nullifiers, @@ -69,7 +65,6 @@ public: // Compute nullifiers, macs, note commitments & encryptions, and SNARK proof virtual SproutProof prove( - bool makeGrothProof, const std::array& inputs, const std::array& outputs, std::array& out_notes, @@ -90,19 +85,6 @@ public: uint256 *out_esk = nullptr ) = 0; - virtual bool verify( - const PHGRProof& proof, - ProofVerifier& verifier, - const uint256& joinSplitPubKey, - const uint256& randomSeed, - const std::array& hmacs, - const std::array& nullifiers, - const std::array& commitments, - uint64_t vpub_old, - uint64_t vpub_new, - const uint256& rt - ) = 0; - protected: JoinSplit() {} };