Clearmatics Libsnark  0.1
C++ library for zkSNARK proofs
simple_example.tcc
Go to the documentation of this file.
1 /** @file
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  *****************************************************************************/
7 
8 #ifndef SIMPLE_EXAMPLE_TCC_
9 #define SIMPLE_EXAMPLE_TCC_
10 
11 #include <libsnark/gadgetlib1/gadgets/basic_gadgets.hpp>
12 
13 namespace libsnark
14 {
15 
16 /* NOTE: all examples here actually generate one constraint less to account for
17  * soundness constraint in QAP */
18 
19 template<typename FieldT>
20 r1cs_example<FieldT> gen_r1cs_example_from_protoboard(
21  const size_t num_constraints)
22 {
23  const size_t new_num_constraints = num_constraints - 1;
24 
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;
30 
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");
36 
37  inner_product_gadget<FieldT> compute_inner_product(
38  pb, A, B, res, "compute_inner_product");
39  compute_inner_product.generate_r1cs_constraints();
40 
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();
45  }
46 
47  compute_inner_product.generate_r1cs_witness();
48  return r1cs_example<FieldT>(
49  pb.get_constraint_system(), pb.primary_input(), pb.auxiliary_input());
50 }
51 
52 } // namespace libsnark
53 #endif // R1CS_EXAMPLES_TCC_