varscalarmul.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include <stdlib.h>
  2. #include <iostream>
  3. #include <fstream>
  4. #include "ecgadget.hpp"
  5. #include "scalarmul.hpp"
  6. using namespace libsnark;
  7. using namespace std;
  8. int main(int argc, char **argv)
  9. {
  10. // Should the Ptable be private or public (arg of "1" = public, "0" = private)
  11. bool Ptable_is_private = true;
  12. if (argc > 1 && atoi(argv[1]) > 0) {
  13. Ptable_is_private = false;
  14. }
  15. // Initialize the curve parameters
  16. default_r1cs_gg_ppzksnark_pp::init_public_params();
  17. typedef libff::Fr<default_r1cs_gg_ppzksnark_pp> FieldT;
  18. // Create protoboard
  19. libff::start_profiling();
  20. cout << "Keypair" << endl;
  21. protoboard<FieldT> pb;
  22. pb_variable<FieldT> outx, outy;
  23. pb_variable<FieldT> Px, Py;
  24. pb_variable<FieldT> s;
  25. pb_variable_array<FieldT> Ptable;
  26. // Allocate variables
  27. size_t numbits = FieldT::num_bits;
  28. outx.allocate(pb, "outx");
  29. outy.allocate(pb, "outy");
  30. Px.allocate(pb, "Px");
  31. Py.allocate(pb, "Py");
  32. Ptable.allocate(pb, 2*numbits, "Ptable");
  33. s.allocate(pb, "s");
  34. // This sets up the protoboard variables so that the first n of them
  35. // represent the public input and the rest is private input
  36. if (Ptable_is_private) {
  37. pb.set_input_sizes(4);
  38. } else {
  39. pb.set_input_sizes(4+2*numbits);
  40. }
  41. // Initialize the gadget
  42. ec_scalarmul_gadget<FieldT> sm(pb, outx, outy, s, Px, Py, Ptable, Ptable_is_private, true);
  43. sm.generate_r1cs_constraints();
  44. const r1cs_constraint_system<FieldT> constraint_system = pb.get_constraint_system();
  45. const r1cs_gg_ppzksnark_keypair<default_r1cs_gg_ppzksnark_pp> keypair = r1cs_gg_ppzksnark_generator<default_r1cs_gg_ppzksnark_pp>(constraint_system);
  46. // Add witness values
  47. cout << "Prover" << endl;
  48. pb.val(s) = FieldT::random_element();
  49. // A variable base point P
  50. pb.val(Px) = FieldT("1095194319010475832867263440470707690447963461907735667341232728633587089702");
  51. pb.val(Py) = FieldT("9185463202887631101218413269806857706246311016297504828581985913021301344974");
  52. cout << "Computing " << pb.val(s) << "*G" << endl;
  53. sm.generate_r1cs_witness();
  54. 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());
  55. cout << "Verifier" << endl;
  56. bool verified = r1cs_gg_ppzksnark_verifier_strong_IC<default_r1cs_gg_ppzksnark_pp>(keypair.vk, pb.primary_input(), proof);
  57. cout << "Number of R1CS constraints: " << constraint_system.num_constraints() << endl;
  58. cout << "Primary (public) input length: " << pb.primary_input().size() << endl;
  59. // cout << "Primary (public) input: " << pb.primary_input() << endl;
  60. cout << "Auxiliary (private) input length: " << pb.auxiliary_input().size() << endl;
  61. // cout << "Auxiliary (private) input: " << pb.auxiliary_input() << endl;
  62. cout << "Verification status: " << verified << endl;
  63. ofstream pkfile("pk_varscalarmul");
  64. pkfile << keypair.pk;
  65. pkfile.close();
  66. ofstream vkfile("vk_varscalarmul");
  67. vkfile << keypair.vk;
  68. vkfile.close();
  69. ofstream pffile("proof_varscalarmul");
  70. pffile << proof;
  71. pffile.close();
  72. cout << pb.val(s) << "*P" << " = (" << pb.val(outx) << ", " << pb.val(outy) << ")" << endl;
  73. return 0;
  74. }