2 *****************************************************************************
4 Implementation of interfaces for an auxiliary gadget for the FOORAM CPU.
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 *****************************************************************************/
14 #ifndef BAR_GADGET_TCC_
15 #define BAR_GADGET_TCC_
20 template<typename FieldT>
21 bar_gadget<FieldT>::bar_gadget(
22 protoboard<FieldT> &pb,
23 const pb_linear_combination_array<FieldT> &X,
25 const pb_linear_combination_array<FieldT> &Y,
27 const pb_linear_combination<FieldT> &Z_packed,
28 const std::string &annotation_prefix)
29 : gadget<FieldT>(pb, annotation_prefix)
36 assert(X.size() == Y.size());
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"));
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());
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")));
53 template<typename FieldT> void bar_gadget<FieldT>::generate_r1cs_constraints()
55 unpack_result->generate_r1cs_constraints(true);
56 pack_Z->generate_r1cs_constraints(false);
58 this->pb.add_r1cs_constraint(
59 r1cs_constraint<FieldT>(
61 a * pb_packing_sum<FieldT>(X) + b * pb_packing_sum<FieldT>(Y),
63 FMT(this->annotation_prefix, " compute_result"));
66 template<typename FieldT> void bar_gadget<FieldT>::generate_r1cs_witness()
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();
72 pack_Z->generate_r1cs_witness_from_bits();
75 } // namespace libsnark
77 #endif // BAR_GADGET_TCC_