pedersen.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include <stdlib.h>
  2. #include <iostream>
  3. #include <fstream>
  4. #include "libff/algebra/fields/field_utils.hpp"
  5. #include "libsnark/zk_proof_systems/ppzksnark/r1cs_ppzksnark/r1cs_ppzksnark.hpp"
  6. #include "libsnark/common/default_types/r1cs_ppzksnark_pp.hpp"
  7. #include "libsnark/gadgetlib1/pb_variable.hpp"
  8. #include "ecgadget.hpp"
  9. using namespace libsnark;
  10. using namespace std;
  11. int main()
  12. {
  13. // Initialize the curve parameters
  14. default_r1cs_ppzksnark_pp::init_public_params();
  15. typedef libff::Fr<default_r1cs_ppzksnark_pp> FieldT;
  16. // Create protoboard
  17. libff::start_profiling();
  18. cout << "Keypair" << endl;
  19. protoboard<FieldT> pb;
  20. pb_variable<FieldT> outx, outy;
  21. pb_variable<FieldT> a, b;
  22. // Allocate variables
  23. outx.allocate(pb, "outx");
  24. outy.allocate(pb, "outy");
  25. a.allocate(pb, "a");
  26. b.allocate(pb, "b");
  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 gadget
  31. ec_pedersen_gadget<FieldT> ped(pb, outx, outy, a, b);
  32. ped.generate_r1cs_constraints();
  33. const r1cs_constraint_system<FieldT> constraint_system = pb.get_constraint_system();
  34. const r1cs_ppzksnark_keypair<default_r1cs_ppzksnark_pp> keypair = r1cs_ppzksnark_generator<default_r1cs_ppzksnark_pp>(constraint_system);
  35. // Add witness values
  36. cout << "Prover" << endl;
  37. pb.val(a) = FieldT::random_element();
  38. pb.val(b) = FieldT::random_element();
  39. cout << "Computing " << pb.val(a) << "*G + " << pb.val(b) << "*H" << endl;
  40. ped.generate_r1cs_witness();
  41. const r1cs_ppzksnark_proof<default_r1cs_ppzksnark_pp> proof = r1cs_ppzksnark_prover<default_r1cs_ppzksnark_pp>(keypair.pk, pb.primary_input(), pb.auxiliary_input());
  42. cout << "Verifier" << endl;
  43. bool verified = r1cs_ppzksnark_verifier_strong_IC<default_r1cs_ppzksnark_pp>(keypair.vk, pb.primary_input(), proof);
  44. cout << "Number of R1CS constraints: " << constraint_system.num_constraints() << endl;
  45. cout << "Primary (public) input: " << pb.primary_input() << endl;
  46. cout << "Auxiliary (private) input: " << pb.auxiliary_input() << endl;
  47. cout << "Verification status: " << verified << endl;
  48. ofstream pkfile("pk_pedersen");
  49. pkfile << keypair.pk;
  50. pkfile.close();
  51. ofstream vkfile("vk_pedersen");
  52. vkfile << keypair.vk;
  53. vkfile.close();
  54. ofstream pffile("proof_pedersen");
  55. pffile << proof;
  56. pffile.close();
  57. return 0;
  58. }