scalarmul.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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> a, b;
  19. // Allocate variables
  20. outx.allocate(pb, "outx");
  21. outy.allocate(pb, "outy");
  22. a.allocate(pb, "a");
  23. // This sets up the protoboard variables so that the first n of them
  24. // represent the public input and the rest is private input
  25. pb.set_input_sizes(2);
  26. // Initialize gadget
  27. ec_scalarmul_gadget<FieldT> sm(pb, outx, outy, a, FieldT(0), FieldT("11977228949870389393715360594190192321220966033310912010610740966317727761886"));
  28. sm.generate_r1cs_constraints();
  29. const r1cs_constraint_system<FieldT> constraint_system = pb.get_constraint_system();
  30. const r1cs_gg_ppzksnark_keypair<default_r1cs_gg_ppzksnark_pp> keypair = r1cs_gg_ppzksnark_generator<default_r1cs_gg_ppzksnark_pp>(constraint_system);
  31. // Add witness values
  32. cout << "Prover" << endl;
  33. pb.val(a) = FieldT::random_element();
  34. cout << "Computing " << pb.val(a) << "*G" << endl;
  35. sm.generate_r1cs_witness();
  36. 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());
  37. cout << "Verifier" << endl;
  38. bool verified = r1cs_gg_ppzksnark_verifier_strong_IC<default_r1cs_gg_ppzksnark_pp>(keypair.vk, pb.primary_input(), proof);
  39. cout << "Number of R1CS constraints: " << constraint_system.num_constraints() << endl;
  40. cout << "Primary (public) input: " << pb.primary_input() << endl;
  41. cout << "Auxiliary (private) input: " << pb.auxiliary_input() << endl;
  42. cout << "Verification status: " << verified << endl;
  43. ofstream pkfile("pk_scalarmul");
  44. pkfile << keypair.pk;
  45. pkfile.close();
  46. ofstream vkfile("vk_scalarmul");
  47. vkfile << keypair.vk;
  48. vkfile.close();
  49. ofstream pffile("proof_scalarmul");
  50. pffile << proof;
  51. pffile.close();
  52. return 0;
  53. }