scipr-lab / libsnark

C++ library for zkSNARKs
Other
1.8k stars 570 forks source link

Cannot load key from file to keypair.vk #169

Closed edoardopuggioni closed 4 years ago

edoardopuggioni commented 4 years ago

Hi, I am trying to compile the following simple code but I have a problem with this single instruction at the end: verifierKeyFromFile >> keypair.vk; which gives a compile error. I know the problem is only that because if I comment it the program compiles and runs perfectly. I will include the compile error at the end.

Any suggestions on how to load the key from a file? Thanks!

#include <libsnark/common/default_types/r1cs_gg_ppzksnark_pp.hpp>
#include <libsnark/zk_proof_systems/ppzksnark/r1cs_gg_ppzksnark/r1cs_gg_ppzksnark.hpp>
#include <libsnark/gadgetlib1/pb_variable.hpp>
#include <libsnark/gadgetlib1/gadgets/basic_gadgets.hpp>

#include <sstream>
#include <fstream>

using namespace libsnark;
using namespace std;

int main () {

    typedef libff::Fr<default_r1cs_gg_ppzksnark_pp> FieldT;

    // Initialize the curve parameters
    default_r1cs_gg_ppzksnark_pp::init_public_params();

    // Create protoboard
    protoboard<FieldT> pb;

    pb_variable<FieldT> x, max;
    pb_variable<FieldT> less, less_or_eq;

    x.allocate(pb, "x");
    max.allocate(pb, "max");

    pb.val(max)= 60;

    // Create comparison gadget object providing parameters for constructor
    comparison_gadget<FieldT> cmp (pb, 10, x, max, less, less_or_eq, "cmp");

    cmp.generate_r1cs_constraints();

    // Add constraint to enforce that less==TRUE, enforcing value below threshold
    pb.add_r1cs_constraint(r1cs_constraint<FieldT>(less, 1, FieldT::one()));

    const r1cs_constraint_system<FieldT> constraint_system = pb.get_constraint_system();

    // Generate keypair
    const r1cs_gg_ppzksnark_keypair<default_r1cs_gg_ppzksnark_pp> keypair = r1cs_gg_ppzksnark_generator<default_r1cs_gg_ppzksnark_pp>(constraint_system);

    stringstream verifierKey;
    verifierKey << keypair.vk;

    ofstream fileOut;
    fileOut.open("./src/verifierKey");

    fileOut << verifierKey.rdbuf();
    fileOut.close();

    cout << "verifierKey before" << endl;
    cout << keypair.vk << endl;

    // Now I want to read the key from file and load it in keypair.vk

    ifstream fileIn("./src/verifierKey");
    stringstream verifierKeyFromFile;
    if (fileIn) {
        verifierKeyFromFile << fileIn.rdbuf();
        fileIn.close();
    }

    verifierKeyFromFile >> keypair.vk;

    cout << "verifierKey after" << endl;
    cout << keypair.vk << endl;

    return 0;
}

Compile error is the following:

main.cpp:81:25: error: no match for ‘operator>>’ (operand types are ‘std::stringstream {aka std::__cxx11::basic_stringstream<char>}’ and ‘const libsnark::r1cs_gg_ppzksnark_verification_key<libff::bn128_pp>’)
     verifierKeyFromFile >> keypair.vk;
                         ^
compilation terminated due to -Wfatal-errors.