scalarmul.cpp 3.4 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()
  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> accinx, acciny, accoutx, accouty;
  19. pb_variable<FieldT> s;
  20. // An accumulator initial value. Its DL representation with respect
  21. // to C and P should be unknown.
  22. const FieldT Ax = FieldT("7536839002660211356286040193441766649532044555061394833845553337792579131020");
  23. const FieldT Ay = FieldT("11391058648720923807988142436733355540810929560298907319389650598553246451302");
  24. FieldT AXSx = Ax;
  25. FieldT AXSy = Ay;
  26. const FieldT Px = FieldT(0);
  27. const FieldT Py = FieldT("11977228949870389393715360594190192321220966033310912010610740966317727761886");
  28. // Allocate variables
  29. outx.allocate(pb, "outx");
  30. outy.allocate(pb, "outy");
  31. accinx.allocate(pb, "accinx");
  32. acciny.allocate(pb, "acciny");
  33. accoutx.allocate(pb, "accoutx");
  34. accouty.allocate(pb, "accouty");
  35. s.allocate(pb, "s");
  36. // This sets up the protoboard variables so that the first n of them
  37. // represent the public input and the rest is private input
  38. pb.set_input_sizes(2);
  39. // Initialize the accumulator
  40. pb.add_r1cs_constraint(r1cs_constraint<FieldT>(accinx, 1, Ax));
  41. pb.add_r1cs_constraint(r1cs_constraint<FieldT>(acciny, 1, Ay));
  42. // Initialize the gadget
  43. ec_constant_scalarmul_gadget<FieldT> sm(pb, accoutx, accouty, accinx, acciny, s, Px, Py, AXSx, AXSy);
  44. sm.generate_r1cs_constraints();
  45. // Subtract the accumulator excess to get the result
  46. ec_constant_add_gadget<FieldT> ad(pb, outx, outy, accoutx, accouty, AXSx, -AXSy);
  47. const r1cs_constraint_system<FieldT> constraint_system = pb.get_constraint_system();
  48. const r1cs_gg_ppzksnark_keypair<default_r1cs_gg_ppzksnark_pp> keypair = r1cs_gg_ppzksnark_generator<default_r1cs_gg_ppzksnark_pp>(constraint_system);
  49. // Add witness values
  50. cout << "Prover" << endl;
  51. pb.val(accinx) = Ax;
  52. pb.val(acciny) = Ay;
  53. pb.val(s) = FieldT::random_element();
  54. cout << "Computing " << pb.val(s) << "*G" << endl;
  55. sm.generate_r1cs_witness();
  56. ad.generate_r1cs_witness();
  57. 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());
  58. cout << "Verifier" << endl;
  59. bool verified = r1cs_gg_ppzksnark_verifier_strong_IC<default_r1cs_gg_ppzksnark_pp>(keypair.vk, pb.primary_input(), proof);
  60. cout << "Number of R1CS constraints: " << constraint_system.num_constraints() << endl;
  61. cout << "Primary (public) input: " << pb.primary_input() << endl;
  62. cout << "Auxiliary (private) input length: " << pb.auxiliary_input().size() << endl;
  63. //cout << "Auxiliary (private) input: " << pb.auxiliary_input() << endl;
  64. cout << "Verification status: " << verified << endl;
  65. ofstream pkfile("pk_scalarmul");
  66. pkfile << keypair.pk;
  67. pkfile.close();
  68. ofstream vkfile("vk_scalarmul");
  69. vkfile << keypair.vk;
  70. vkfile.close();
  71. ofstream pffile("proof_scalarmul");
  72. pffile << proof;
  73. pffile.close();
  74. cout << pb.val(s) << "*G" << " = (" << pb.val(outx) << ", " << pb.val(outy) << ")" << endl;
  75. return 0;
  76. }