scalarmul.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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()
  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> s;
  19. // The constant base point P
  20. const FieldT Px = FieldT(0);
  21. const FieldT Py = FieldT("11977228949870389393715360594190192321220966033310912010610740966317727761886");
  22. // Allocate variables
  23. outx.allocate(pb, "outx");
  24. outy.allocate(pb, "outy");
  25. s.allocate(pb, "s");
  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_constant_scalarmul_gadget<FieldT> sm(pb, outx, outy, s, Px, Py);
  31. sm.generate_r1cs_constraints();
  32. const r1cs_constraint_system<FieldT> constraint_system = pb.get_constraint_system();
  33. const r1cs_gg_ppzksnark_keypair<default_r1cs_gg_ppzksnark_pp> keypair = r1cs_gg_ppzksnark_generator<default_r1cs_gg_ppzksnark_pp>(constraint_system);
  34. // Add witness values
  35. cout << "Prover" << endl;
  36. pb.val(s) = FieldT::random_element();
  37. cout << "Computing " << pb.val(s) << "*G" << endl;
  38. sm.generate_r1cs_witness();
  39. 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());
  40. cout << "Verifier" << endl;
  41. bool verified = r1cs_gg_ppzksnark_verifier_strong_IC<default_r1cs_gg_ppzksnark_pp>(keypair.vk, pb.primary_input(), proof);
  42. cout << "Number of R1CS constraints: " << constraint_system.num_constraints() << endl;
  43. cout << "Primary (public) input: " << pb.primary_input() << endl;
  44. cout << "Auxiliary (private) input length: " << pb.auxiliary_input().size() << endl;
  45. // cout << "Auxiliary (private) input: " << pb.auxiliary_input() << endl;
  46. cout << "Verification status: " << verified << endl;
  47. ofstream pkfile("pk_scalarmul");
  48. pkfile << keypair.pk;
  49. pkfile.close();
  50. ofstream vkfile("vk_scalarmul");
  51. vkfile << keypair.vk;
  52. vkfile.close();
  53. ofstream pffile("proof_scalarmul");
  54. pffile << proof;
  55. pffile.close();
  56. cout << pb.val(s) << "*G" << " = (" << pb.val(outx) << ", " << pb.val(outy) << ")" << endl;
  57. return 0;
  58. }