scalarmul.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. init_curveparams();
  13. typedef libff::Fr<default_r1cs_gg_ppzksnark_pp> FieldT;
  14. // Create protoboard
  15. libff::start_profiling();
  16. cout << "Keypair" << endl;
  17. protoboard<FieldT> pb;
  18. pb_variable<FieldT> outx, outy;
  19. pb_variable<FieldT> s;
  20. // The constant base point P
  21. const FieldT Px = FieldT(0);
  22. const FieldT Py = FieldT("11977228949870389393715360594190192321220966033310912010610740966317727761886");
  23. // Allocate variables
  24. outx.allocate(pb, "outx");
  25. outy.allocate(pb, "outy");
  26. s.allocate(pb, "s");
  27. // This sets up the protoboard variables so that the first n of them
  28. // represent the public input and the rest is private input
  29. pb.set_input_sizes(2);
  30. // Initialize the gadget
  31. ec_constant_scalarmul_gadget<FieldT> sm(pb, outx, outy, s, Px, Py);
  32. sm.generate_r1cs_constraints();
  33. const r1cs_constraint_system<FieldT> constraint_system = pb.get_constraint_system();
  34. const r1cs_gg_ppzksnark_keypair<default_r1cs_gg_ppzksnark_pp> keypair = r1cs_gg_ppzksnark_generator<default_r1cs_gg_ppzksnark_pp>(constraint_system);
  35. // Add witness values
  36. cout << "Prover" << endl;
  37. pb.val(s) = FieldT::random_element();
  38. cout << "Computing " << pb.val(s) << "*G" << endl;
  39. sm.generate_r1cs_witness();
  40. 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());
  41. cout << "Verifier" << endl;
  42. bool verified = r1cs_gg_ppzksnark_verifier_strong_IC<default_r1cs_gg_ppzksnark_pp>(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_scalarmul");
  49. pkfile << keypair.pk;
  50. pkfile.close();
  51. ofstream vkfile("vk_scalarmul");
  52. vkfile << keypair.vk;
  53. vkfile.close();
  54. ofstream pffile("proof_scalarmul");
  55. pffile << proof;
  56. pffile.close();
  57. cout << pb.val(s) << "*G" << " = (" << pb.val(outx) << ", " << pb.val(outy) << ")" << endl;
  58. return 0;
  59. }