2 *****************************************************************************
3 * @author This file is part of libsnark, developed by SCIPR Lab
4 * and contributors (see AUTHORS).
5 * @copyright MIT license (see LICENSE file)
6 *****************************************************************************/
8 #ifndef SIMPLE_EXAMPLE_TCC_
9 #define SIMPLE_EXAMPLE_TCC_
11 #include <libsnark/gadgetlib1/gadgets/basic_gadgets.hpp>
16 /* NOTE: all examples here actually generate one constraint less to account for
17 * soundness constraint in QAP */
19 template<typename FieldT>
20 r1cs_example<FieldT> gen_r1cs_example_from_protoboard(
21 const size_t num_constraints)
23 const size_t new_num_constraints = num_constraints - 1;
25 /* construct dummy example: inner products of two vectors */
26 protoboard<FieldT> pb;
27 pb_variable_array<FieldT> A;
28 pb_variable_array<FieldT> B;
29 pb_variable<FieldT> res;
31 // the variables on the protoboard are (ONE (constant 1 term), res, A[0],
32 // ..., A[num_constraints-1], B[0], ..., B[num_constraints-1])
33 res.allocate(pb, "res");
34 A.allocate(pb, new_num_constraints, "A");
35 B.allocate(pb, new_num_constraints, "B");
37 inner_product_gadget<FieldT> compute_inner_product(
38 pb, A, B, res, "compute_inner_product");
39 compute_inner_product.generate_r1cs_constraints();
41 /* fill in random example */
42 for (size_t i = 0; i < new_num_constraints; ++i) {
43 pb.val(A[i]) = FieldT::random_element();
44 pb.val(B[i]) = FieldT::random_element();
47 compute_inner_product.generate_r1cs_witness();
48 return r1cs_example<FieldT>(
49 pb.get_constraint_system(), pb.primary_input(), pb.auxiliary_input());
52 } // namespace libsnark
53 #endif // R1CS_EXAMPLES_TCC_