Clearmatics Libsnark  0.1
C++ library for zkSNARK proofs
bar_gadget.tcc
Go to the documentation of this file.
1 /** @file
2  *****************************************************************************
3 
4  Implementation of interfaces for an auxiliary gadget for the FOORAM CPU.
5 
6  See bar_gadget.hpp .
7 
8  *****************************************************************************
9  * @author This file is part of libsnark, developed by SCIPR Lab
10  * and contributors (see AUTHORS).
11  * @copyright MIT license (see LICENSE file)
12  *****************************************************************************/
13 
14 #ifndef BAR_GADGET_TCC_
15 #define BAR_GADGET_TCC_
16 
17 namespace libsnark
18 {
19 
20 template<typename FieldT>
21 bar_gadget<FieldT>::bar_gadget(
22  protoboard<FieldT> &pb,
23  const pb_linear_combination_array<FieldT> &X,
24  const FieldT &a,
25  const pb_linear_combination_array<FieldT> &Y,
26  const FieldT &b,
27  const pb_linear_combination<FieldT> &Z_packed,
28  const std::string &annotation_prefix)
29  : gadget<FieldT>(pb, annotation_prefix)
30  , X(X)
31  , a(a)
32  , Y(Y)
33  , b(b)
34  , Z_packed(Z_packed)
35 {
36  assert(X.size() == Y.size());
37  width = X.size();
38 
39  result.allocate(pb, FMT(annotation_prefix, " result"));
40  Z_bits.allocate(pb, width, FMT(annotation_prefix, " Z_bits"));
41  overflow.allocate(pb, 2 * width, FMT(annotation_prefix, " overflow"));
42 
43  unpacked_result.insert(unpacked_result.end(), Z_bits.begin(), Z_bits.end());
44  unpacked_result.insert(
45  unpacked_result.end(), overflow.begin(), overflow.end());
46 
47  unpack_result.reset(new packing_gadget<FieldT>(
48  pb, unpacked_result, result, FMT(annotation_prefix, " unpack_result")));
49  pack_Z.reset(new packing_gadget<FieldT>(
50  pb, Z_bits, Z_packed, FMT(annotation_prefix, " pack_Z")));
51 }
52 
53 template<typename FieldT> void bar_gadget<FieldT>::generate_r1cs_constraints()
54 {
55  unpack_result->generate_r1cs_constraints(true);
56  pack_Z->generate_r1cs_constraints(false);
57 
58  this->pb.add_r1cs_constraint(
59  r1cs_constraint<FieldT>(
60  1,
61  a * pb_packing_sum<FieldT>(X) + b * pb_packing_sum<FieldT>(Y),
62  result),
63  FMT(this->annotation_prefix, " compute_result"));
64 }
65 
66 template<typename FieldT> void bar_gadget<FieldT>::generate_r1cs_witness()
67 {
68  this->pb.val(result) = X.get_field_element_from_bits(this->pb) * a +
69  Y.get_field_element_from_bits(this->pb) * b;
70  unpack_result->generate_r1cs_witness_from_packed();
71 
72  pack_Z->generate_r1cs_witness_from_bits();
73 }
74 
75 } // namespace libsnark
76 
77 #endif // BAR_GADGET_TCC_