diff --git a/lib/libsnarc/src/r1cs_libiop.cpp b/lib/libsnarc/src/r1cs_libiop.cpp index dc6a436..640f29a 100644 --- a/lib/libsnarc/src/r1cs_libiop.cpp +++ b/lib/libsnarc/src/r1cs_libiop.cpp @@ -98,6 +98,27 @@ linear_combination R1CSLibiop::parseLinearCombJson(json &jlc) return lc; } +template +linear_combination R1CSLibiop::parseLinearCombJson(json &jlc, int input_nb, int input_padding) +{ + linear_combination lc; + for (auto const& term : jlc) + { + int idx = term[0]; + if (idx > input_nb) + idx = idx + input_padding-input_nb; + variable var(idx); + std::string str_coeff = term[1]; + F cc(str_coeff.c_str()); + + lc.add_term(var, cc);//TODO handle negative idx; something like this: if var<0; idx = primary.len - var + + } + return lc; +} + + + //Convert R1CS assignment into a '.j1cs.in' json input file template json R1CSLibiop::Inputs2Json(const r1cs_primary_input &primary_input,const r1cs_auxiliary_input &auxiliary_input) @@ -164,8 +185,9 @@ bool R1CSLibiop::ToJsonl(r1cs_constraint_system &in_cs, const std::string } template -bool R1CSLibiop::FromJsonl(const std::string jsonFile, r1cs_constraint_system &out_cs) +bool R1CSLibiop::FromJsonl(const std::string jsonFile, r1cs_constraint_system &out_cs, bool pad_inputs) { + printf("debuggggn"); //read from file std::ifstream r1cs_file(jsonFile); if (!r1cs_file.good()) @@ -173,6 +195,9 @@ bool R1CSLibiop::FromJsonl(const std::string jsonFile, r1cs_constraint_system std::string line; json header; + int input_nb =0; + int input_padding = 0; + //todo: clear out_cs while (std::getline(r1cs_file, line)) { @@ -182,18 +207,24 @@ bool R1CSLibiop::FromJsonl(const std::string jsonFile, r1cs_constraint_system { // header header = jc["r1cs"]; + input_nb = header["instance_nb"]; + input_padding = input_nb; + if (pad_inputs) + input_padding = libiop::round_to_next_power_of_2(input_padding+1)-1; + printf("input nb:%d, padding:%d\n",input_nb,input_padding); } else { //constraint - linear_combination A = parseLinearCombJson(jc["A"]); - linear_combination B = parseLinearCombJson(jc["B"]); - linear_combination C = parseLinearCombJson(jc["C"]); + linear_combination A = parseLinearCombJson(jc["A"], input_nb, input_padding); + linear_combination B = parseLinearCombJson(jc["B"], input_nb, input_padding); + linear_combination C = parseLinearCombJson(jc["C"], input_nb, input_padding); r1cs_constraint constraint(A,B,C); out_cs.add_constraint(constraint); + //printf("adding cs\n"); } } - out_cs.primary_input_size_ = header["instance_nb"]; + out_cs.primary_input_size_ = input_padding;// header["instance_nb"]; out_cs.auxiliary_input_size_ = header["witness_nb"]; return true; @@ -205,9 +236,13 @@ void R1CSLibiop::Pad(r1cs_constraint_system &out_cs) size_t cur_cs_nb = out_cs.num_constraints(); size_t cs_nb = libiop::round_to_next_power_of_2(cur_cs_nb); + while (cur_cs_nb < cs_nb) { linear_combination dummy; + variable var(0); + F cc(0); + dummy.add_term(var, cc); r1cs_constraint constraint(dummy, dummy, dummy); out_cs.add_constraint(constraint); ++cur_cs_nb; @@ -217,11 +252,10 @@ void R1CSLibiop::Pad(r1cs_constraint_system &out_cs) template void R1CSLibiop::PadInputs(r1cs_primary_input &primary_inputs) { - size_t cur_size = primary_inputs.size(); - size_t target_size = libiop::round_to_next_power_of_2(cur_size)-1; - if (target_size < cur_size) - target_size = libiop::round_to_next_power_of_2(cur_size+1)-1; + size_t target_size = libiop::round_to_next_power_of_2(cur_size+1)-1; + assert (cur_size <= target_size); + while (cur_size < target_size) { F cc(0); diff --git a/lib/libsnarc/src/r1cs_libiop.hpp b/lib/libsnarc/src/r1cs_libiop.hpp index 0c3fa7b..d9ccdc7 100644 --- a/lib/libsnarc/src/r1cs_libiop.hpp +++ b/lib/libsnarc/src/r1cs_libiop.hpp @@ -25,13 +25,14 @@ class R1CSLibiop public: void InitR1CS(); nlohmann::json LinearCombination2Json(libiop::linear_combination vec); + libiop::linear_combination parseLinearCombJson(nlohmann::json &jlc, int input_nb, int input_padding); libiop::linear_combination parseLinearCombJson(nlohmann::json &jlc); nlohmann::json Inputs2Json(const libiop::r1cs_primary_input &primary_input,const libiop::r1cs_auxiliary_input &auxiliary_input); bool SaveInputs(const std::string jsonFile, const libiop::r1cs_primary_input &primary_input,const libiop::r1cs_auxiliary_input &auxiliary_input); bool ToJsonl(libiop::r1cs_constraint_system &in_cs, const std::string &out_fname); - bool FromJsonl(const std::string jsonFile, libiop::r1cs_constraint_system &out_cs); + bool FromJsonl(const std::string jsonFile, libiop::r1cs_constraint_system &out_cs, bool pad_inputs = false); bool LoadInputs(const std::string jsonFile, libiop::r1cs_primary_input &primary_input, libiop::r1cs_auxiliary_input &auxiliary_input); void Pad(libiop::r1cs_constraint_system &out_cs); void PadInputs(libiop::r1cs_primary_input &primary_inputs); diff --git a/lib/libsnarc/src/skAurora.cpp b/lib/libsnarc/src/skAurora.cpp index 0ded7bf..c1f1eb9 100644 --- a/lib/libsnarc/src/skAurora.cpp +++ b/lib/libsnarc/src/skAurora.cpp @@ -39,9 +39,12 @@ nlohmann::json skAurora::Proof(const std::string &r1cs_filename, const std::str r1cs_constraint_system cs; - printf("loading constraints...\n"); - r1cs.FromJsonl(r1cs_filename, cs); - r1cs.Pad(cs); + printf("loading constraints....\n"); + + r1cs.FromJsonl(r1cs_filename, cs, true); + printf("padding...\n"); + r1cs.Pad(cs); + //load the inputs r1cs_primary_input primary_input; r1cs_auxiliary_input auxiliary_input; @@ -50,9 +53,9 @@ nlohmann::json skAurora::Proof(const std::string &r1cs_filename, const std::str else printf("error with inputs file\n"); r1cs.PadInputs(primary_input); + if(!cs.is_satisfied(primary_input, auxiliary_input)) printf("NOT SATISFIED!!!\n"); - /* Actual SNARK test */ aurora_snark_parameters params(security_parameter,