1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- #include <stdlib.h>
- #include <iostream>
- #include <fstream>
- #include "libff/algebra/fields/field_utils.hpp"
- #include "libsnark/zk_proof_systems/ppzksnark/r1cs_ppzksnark/r1cs_ppzksnark.hpp"
- #include "libsnark/common/default_types/r1cs_ppzksnark_pp.hpp"
- #include "libsnark/gadgetlib1/pb_variable.hpp"
- #include "ecgadget.hpp"
- using namespace libsnark;
- using namespace std;
- int main()
- {
- // Initialize the curve parameters
- default_r1cs_ppzksnark_pp::init_public_params();
- typedef libff::Fr<default_r1cs_ppzksnark_pp> FieldT;
-
- // Create protoboard
- libff::start_profiling();
- cout << "Keypair" << endl;
- protoboard<FieldT> pb;
- pb_variable<FieldT> outx, outy;
- pb_variable<FieldT> a, b;
- // Allocate variables
- outx.allocate(pb, "outx");
- outy.allocate(pb, "outy");
- a.allocate(pb, "a");
- b.allocate(pb, "b");
- // This sets up the protoboard variables so that the first n of them
- // represent the public input and the rest is private input
- pb.set_input_sizes(2);
- // Initialize gadget
- ec_pedersen_gadget<FieldT> ped(pb, outx, outy, a, b);
- ped.generate_r1cs_constraints();
-
- const r1cs_constraint_system<FieldT> constraint_system = pb.get_constraint_system();
- const r1cs_ppzksnark_keypair<default_r1cs_ppzksnark_pp> keypair = r1cs_ppzksnark_generator<default_r1cs_ppzksnark_pp>(constraint_system);
- // Add witness values
- cout << "Prover" << endl;
-
- pb.val(a) = FieldT::random_element();
- pb.val(b) = FieldT::random_element();
- cout << "Computing " << pb.val(a) << "*G + " << pb.val(b) << "*H" << endl;
- ped.generate_r1cs_witness();
- const r1cs_ppzksnark_proof<default_r1cs_ppzksnark_pp> proof = r1cs_ppzksnark_prover<default_r1cs_ppzksnark_pp>(keypair.pk, pb.primary_input(), pb.auxiliary_input());
- cout << "Verifier" << endl;
- bool verified = r1cs_ppzksnark_verifier_strong_IC<default_r1cs_ppzksnark_pp>(keypair.vk, pb.primary_input(), proof);
- cout << "Number of R1CS constraints: " << constraint_system.num_constraints() << endl;
- cout << "Primary (public) input: " << pb.primary_input() << endl;
- cout << "Auxiliary (private) input: " << pb.auxiliary_input() << endl;
- cout << "Verification status: " << verified << endl;
- ofstream pkfile("pk_pedersen");
- pkfile << keypair.pk;
- pkfile.close();
- ofstream vkfile("vk_pedersen");
- vkfile << keypair.vk;
- vkfile.close();
- ofstream pffile("proof_pedersen");
- pffile << proof;
- pffile.close();
- return 0;
- }
|