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