|
| 1 | +#include <stdlib.h> |
| 2 | +#include <iostream> |
| 3 | +#include <boost/optional/optional_io.hpp> |
| 4 | + |
| 5 | +#include "algebra/fields/field_utils.hpp" |
| 6 | +#include "libsnark/zk_proof_systems/ppzksnark/r1cs_ppzksnark/r1cs_ppzksnark.hpp" |
| 7 | +#include "libsnark/common/default_types/r1cs_ppzksnark_pp.hpp" |
| 8 | +#include "libsnark/common/utils.hpp" |
| 9 | +#include "libsnark/gadgetlib1/pb_variable.hpp" |
| 10 | + |
| 11 | + |
| 12 | +using namespace libsnark; |
| 13 | +using namespace std; |
| 14 | + |
| 15 | +int main() |
| 16 | +{ |
| 17 | + |
| 18 | + typedef Fr<default_r1cs_ppzksnark_pp> FieldT; |
| 19 | + |
| 20 | + // Create protoboard |
| 21 | + |
| 22 | + protoboard<FieldT> pb; |
| 23 | + |
| 24 | + // Define variables |
| 25 | + |
| 26 | + pb_variable<FieldT> x; |
| 27 | + pb_variable<FieldT> out; |
| 28 | + pb_variable<FieldT> sym_1; |
| 29 | + pb_variable<FieldT> y; |
| 30 | + pb_variable<FieldT> sym_2; |
| 31 | + |
| 32 | + // Allocate variables to protoboard |
| 33 | + // The strings (like "x") are only for debugging purposes |
| 34 | + |
| 35 | + x.allocate(pb, "x"); |
| 36 | + out.allocate(pb, "out"); |
| 37 | + sym_1.allocate(pb, "sym_1"); |
| 38 | + y.allocate(pb, "y"); |
| 39 | + sym_2.allocate(pb, "sym_2"); |
| 40 | + |
| 41 | + // Add R1CS constraints to protoboard |
| 42 | + |
| 43 | + // x*x = sym_1 |
| 44 | + pb.add_r1cs_constraint(r1cs_constraint<FieldT>(x, x, sym_1)); |
| 45 | + |
| 46 | + // sym_1 * x = y |
| 47 | + pb.add_r1cs_constraint(r1cs_constraint<FieldT>(sym_1, x, y)); |
| 48 | + |
| 49 | + // y + x = sym_2 |
| 50 | + pb.add_r1cs_constraint(r1cs_constraint<FieldT>(y + x, 1, sym_2)); |
| 51 | + |
| 52 | + // sym_2 + 5 = ~out |
| 53 | + pb.add_r1cs_constraint(r1cs_constraint<FieldT>(sym_2 + 5, 1, out)); |
| 54 | + |
| 55 | + // Add witness values |
| 56 | + |
| 57 | + pb.val(x) = 3; |
| 58 | + pb.val(out) = 35; |
| 59 | + pb.val(sym_1) = 9; |
| 60 | + pb.val(y) = 27; |
| 61 | + pb.val(sym_2) = 30; |
| 62 | + |
| 63 | + |
| 64 | + if (pb.is_satisfied()) { |
| 65 | + cout << "Constraint system is satisfied." << endl; |
| 66 | + } |
| 67 | + else { |
| 68 | + cout << "Constraint system is not satisfied." << endl; |
| 69 | + } |
| 70 | + |
| 71 | + // This is not working for some reason |
| 72 | + cout << "Value of x: " << pb.val(x) << endl; |
| 73 | + |
| 74 | + const r1cs_constraint_system<FieldT> constraint_system = pb.get_constraint_system(); |
| 75 | + |
| 76 | + cout << "Number of R1CS constraints: " << constraint_system.num_constraints() << endl; |
| 77 | + cout << "Number of variables: " << pb.num_variables() << endl; |
| 78 | + |
| 79 | + // Initialize the curve parameters. |
| 80 | + default_r1cs_ppzksnark_pp::init_public_params(); |
| 81 | + |
| 82 | + r1cs_ppzksnark_keypair<default_r1cs_ppzksnark_pp> keypair = r1cs_ppzksnark_generator<default_r1cs_ppzksnark_pp>(constraint_system); |
| 83 | + |
| 84 | + r1cs_ppzksnark_proof<default_r1cs_ppzksnark_pp> proof = r1cs_ppzksnark_prover<default_r1cs_ppzksnark_pp>(keypair.pk, pb.primary_input(), pb.auxiliary_input()); |
| 85 | + |
| 86 | + vector<FieldT> empty; |
| 87 | + |
| 88 | + bool verified = r1cs_ppzksnark_verifier_strong_IC<default_r1cs_ppzksnark_pp>(keypair.vk, empty, proof); |
| 89 | + |
| 90 | + cout << "Verification status: " << verified << endl; |
| 91 | + |
| 92 | + cout << "made it here" << endl; |
| 93 | + |
| 94 | + return 0; |
| 95 | +} |
0 commit comments