pedersen.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include <stdlib.h>
  2. #include <iostream>
  3. #include <fstream>
  4. #include "ecgadget.hpp"
  5. #include "pedersen.hpp"
  6. using namespace libsnark;
  7. using namespace std;
  8. int main()
  9. {
  10. // Initialize the curve parameters
  11. default_r1cs_gg_ppzksnark_pp::init_public_params();
  12. typedef libff::Fr<default_r1cs_gg_ppzksnark_pp> FieldT;
  13. // Create protoboard
  14. libff::start_profiling();
  15. cout << "Keypair" << endl;
  16. protoboard<FieldT> pb;
  17. pb_variable<FieldT> outx, outy;
  18. pb_variable<FieldT> a, b;
  19. // Allocate variables
  20. outx.allocate(pb, "outx");
  21. outy.allocate(pb, "outy");
  22. a.allocate(pb, "a");
  23. b.allocate(pb, "b");
  24. // This sets up the protoboard variables so that the first n of them
  25. // represent the public input and the rest is private input
  26. pb.set_input_sizes(2);
  27. // Initialize gadget
  28. ec_pedersen_gadget<FieldT> ped(pb, outx, outy, a, b);
  29. ped.generate_r1cs_constraints();
  30. const r1cs_constraint_system<FieldT> constraint_system = pb.get_constraint_system();
  31. const r1cs_gg_ppzksnark_keypair<default_r1cs_gg_ppzksnark_pp> keypair = r1cs_gg_ppzksnark_generator<default_r1cs_gg_ppzksnark_pp>(constraint_system);
  32. // Add witness values
  33. cout << "Prover" << endl;
  34. pb.val(a) = FieldT::random_element();
  35. pb.val(b) = FieldT::random_element();
  36. cout << "Computing " << pb.val(a) << "*G + " << pb.val(b) << "*H" << endl;
  37. ped.generate_r1cs_witness();
  38. const r1cs_gg_ppzksnark_proof<default_r1cs_gg_ppzksnark_pp> proof = r1cs_gg_ppzksnark_prover<default_r1cs_gg_ppzksnark_pp>(keypair.pk, pb.primary_input(), pb.auxiliary_input());
  39. cout << "Verifier" << endl;
  40. bool verified = r1cs_gg_ppzksnark_verifier_strong_IC<default_r1cs_gg_ppzksnark_pp>(keypair.vk, pb.primary_input(), proof);
  41. cout << "Number of R1CS constraints: " << constraint_system.num_constraints() << endl;
  42. cout << "Primary (public) input: " << pb.primary_input() << endl;
  43. cout << "Auxiliary (private) input length: " << pb.auxiliary_input().size() << endl;
  44. // cout << "Auxiliary (private) input: " << pb.auxiliary_input() << endl;
  45. cout << "Verification status: " << verified << endl;
  46. ofstream pkfile("pk_pedersen");
  47. pkfile << keypair.pk;
  48. pkfile.close();
  49. ofstream vkfile("vk_pedersen");
  50. vkfile << keypair.vk;
  51. vkfile.close();
  52. ofstream pffile("proof_pedersen");
  53. pffile << proof;
  54. pffile.close();
  55. cout << pb.val(a) << "*G" << " + " << pb.val(b) << "*H = (" << pb.val(outx) << ", " << pb.val(outy) << ")" << endl;
  56. return 0;
  57. }