diff --git a/README.md b/README.md index 26eec07..47a1068 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,8 @@ I will not show all twelve gates of the circuit, but here are the gates selectin | ``` +This binary will also output the verifying key and proof in json format to `data/vk.json` and `data/proof.json`. + ## Building and running ### Install dependencies diff --git a/src/prime_under_16.cpp b/src/prime_under_16.cpp index 48a0a05..4af32eb 100644 --- a/src/prime_under_16.cpp +++ b/src/prime_under_16.cpp @@ -2,6 +2,8 @@ #include "libsnark/zk_proof_systems/ppzksnark/tbcs_ppzksnark/tbcs_ppzksnark.hpp" #include "libsnark/common/default_types/tbcs_ppzksnark_pp.hpp" +#include "util.hpp" + using namespace libsnark; using namespace std; @@ -171,5 +173,10 @@ int main() printf("%s\n", verified ? "Proof accepted" : "Proof false"); + const tbcs_ppzksnark_verification_key vk = keypair.vk; + + vk_to_json(vk, "data/vk.json"); + proof_to_json(proof, "data/proof.json"); + return 0; } diff --git a/src/util.hpp b/src/util.hpp new file mode 100644 index 0000000..efc6284 --- /dev/null +++ b/src/util.hpp @@ -0,0 +1,70 @@ +#include +#include "libff/algebra/curves/public_params.hpp" + +using namespace libsnark; +using namespace libff; +using namespace std; + +template +void vk_to_json(tbcs_ppzksnark_verification_key vk, string pathToFile) +{ + ofstream vk_data; + vk_data.open(pathToFile); + + G2 tilde(vk.tilde_g2); + tilde.to_affine_coordinates(); + + G2 alpha_tilde(vk.alpha_tilde_g2); + alpha_tilde.to_affine_coordinates(); + + G2 Z(vk.Z_g2); + Z.to_affine_coordinates(); + + accumulation_vector> IC(vk.encoded_IC_query); + G1 IC_0(IC.first); + IC_0.to_affine_coordinates(); + + + vk_data << "[[\"" << tilde.X.c1 << "\", \"" << tilde.X.c0 << "\"],[\"" << tilde.Y.c1 << "\", \"" << tilde.Y.c0 << "\"]]," << endl; + vk_data << "[[\"" << alpha_tilde.X.c1 << "\", \"" << alpha_tilde.X.c0 << "\"],[\"" << alpha_tilde.Y.c1 << "\", \"" << alpha_tilde.Y.c0 << "\"]]," << endl; + vk_data << "[[\"" << Z.X.c1 << "\", \"" << Z.X.c0 << "\"],[\"" << Z.Y.c1 << "\", \"" << Z.Y.c0 << "\"]]," << endl; + vk_data << "[[\"" << IC_0.X << "\", \"" << IC_0.Y << "\"]"; + + + for(size_t i=0; i IC_N(IC.rest[i]); + IC_N.to_affine_coordinates(); + vk_data << "[\"" << IC_N.X << "\", \"" << IC_N.Y << "\"]"; + } + + vk_data << "]" << endl; + + vk_data.close(); +} + +template +void proof_to_json(tbcs_ppzksnark_proof proof, string pathToFile) +{ + ofstream proof_data; + proof_data.open(pathToFile); + + G1 V_g1(proof.V_g1); + V_g1.to_affine_coordinates(); + + G1 alpha_V_g1(proof.alpha_V_g1); + alpha_V_g1.to_affine_coordinates(); + + G1 H_g1(proof.H_g1); + H_g1.to_affine_coordinates(); + + G2 V_g2(proof.V_g2); + V_g2.to_affine_coordinates(); + + proof_data << "[\"" << V_g1.X << "\", \"" << V_g1.Y << "\"]," << endl; + proof_data << "[\"" << alpha_V_g1.X << "\", \"" << alpha_V_g1.Y << "\"]," << endl; + proof_data << "[\"" << H_g1.X << "\", \"" << H_g1.Y << "\"]," << endl; + proof_data << "[[\"" << V_g2.X.c1 << "\", \"" << V_g2.X.c0 << "\"],[\"" << V_g2.Y.c1 << "\", \"" << V_g2.Y.c0 << "\"]]" << endl; + + proof_data.close(); +}