Skip to content

Commit

Permalink
Merge pull request scipr-lab#74 from popoffka/fix-fmt
Browse files Browse the repository at this point in the history
Fix usage of FMT macro from libff
  • Loading branch information
madars authored Jun 6, 2017
2 parents 2f6e743 + b97f572 commit a6e32b3
Show file tree
Hide file tree
Showing 58 changed files with 822 additions and 822 deletions.
34 changes: 17 additions & 17 deletions src/gadgetlib1/gadgets/basic_gadgets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,24 +121,24 @@ class dual_variable_gadget : public gadget<FieldT> {
const std::string &annotation_prefix="") :
gadget<FieldT>(pb, annotation_prefix)
{
packed.allocate(pb, libff::FMT(this->annotation_prefix, " packed"));
bits.allocate(pb, width, libff::FMT(this->annotation_prefix, " bits"));
packed.allocate(pb, FMT(this->annotation_prefix, " packed"));
bits.allocate(pb, width, FMT(this->annotation_prefix, " bits"));
consistency_check.reset(new packing_gadget<FieldT>(pb,
bits,
packed,
libff::FMT(this->annotation_prefix, " consistency_check")));
FMT(this->annotation_prefix, " consistency_check")));
}

dual_variable_gadget(protoboard<FieldT> &pb,
const pb_variable_array<FieldT> &bits,
const std::string &annotation_prefix="") :
gadget<FieldT>(pb, annotation_prefix), bits(bits)
{
packed.allocate(pb, libff::FMT(this->annotation_prefix, " packed"));
packed.allocate(pb, FMT(this->annotation_prefix, " packed"));
consistency_check.reset(new packing_gadget<FieldT>(pb,
bits,
packed,
libff::FMT(this->annotation_prefix, " consistency_check")));
FMT(this->annotation_prefix, " consistency_check")));
}

dual_variable_gadget(protoboard<FieldT> &pb,
Expand All @@ -147,11 +147,11 @@ class dual_variable_gadget : public gadget<FieldT> {
const std::string &annotation_prefix="") :
gadget<FieldT>(pb, annotation_prefix), packed(packed)
{
bits.allocate(pb, width, libff::FMT(this->annotation_prefix, " bits"));
bits.allocate(pb, width, FMT(this->annotation_prefix, " bits"));
consistency_check.reset(new packing_gadget<FieldT>(pb,
bits,
packed,
libff::FMT(this->annotation_prefix, " consistency_check")));
FMT(this->annotation_prefix, " consistency_check")));
}

void generate_r1cs_constraints(const bool enforce_bitness);
Expand Down Expand Up @@ -183,7 +183,7 @@ class disjunction_gadget : public gadget<FieldT> {
gadget<FieldT>(pb, annotation_prefix), inputs(inputs), output(output)
{
assert(inputs.size() >= 1);
inv.allocate(pb, libff::FMT(this->annotation_prefix, " inv"));
inv.allocate(pb, FMT(this->annotation_prefix, " inv"));
}

void generate_r1cs_constraints();
Expand All @@ -208,7 +208,7 @@ class conjunction_gadget : public gadget<FieldT> {
gadget<FieldT>(pb, annotation_prefix), inputs(inputs), output(output)
{
assert(inputs.size() >= 1);
inv.allocate(pb, libff::FMT(this->annotation_prefix, " inv"));
inv.allocate(pb, FMT(this->annotation_prefix, " inv"));
}

void generate_r1cs_constraints();
Expand Down Expand Up @@ -243,19 +243,19 @@ class comparison_gadget : public gadget<FieldT> {
const std::string &annotation_prefix="") :
gadget<FieldT>(pb, annotation_prefix), n(n), A(A), B(B), less(less), less_or_eq(less_or_eq)
{
alpha.allocate(pb, n, libff::FMT(this->annotation_prefix, " alpha"));
alpha.allocate(pb, n, FMT(this->annotation_prefix, " alpha"));
alpha.emplace_back(less_or_eq); // alpha[n] is less_or_eq

alpha_packed.allocate(pb, libff::FMT(this->annotation_prefix, " alpha_packed"));
not_all_zeros.allocate(pb, libff::FMT(this->annotation_prefix, " not_all_zeros"));
alpha_packed.allocate(pb, FMT(this->annotation_prefix, " alpha_packed"));
not_all_zeros.allocate(pb, FMT(this->annotation_prefix, " not_all_zeros"));

pack_alpha.reset(new packing_gadget<FieldT>(pb, alpha, alpha_packed,
libff::FMT(this->annotation_prefix, " pack_alpha")));
FMT(this->annotation_prefix, " pack_alpha")));

all_zeros_test.reset(new disjunction_gadget<FieldT>(pb,
pb_variable_array<FieldT>(alpha.begin(), alpha.begin() + n),
not_all_zeros,
libff::FMT(this->annotation_prefix, " all_zeros_test")));
FMT(this->annotation_prefix, " all_zeros_test")));
};

void generate_r1cs_constraints();
Expand Down Expand Up @@ -285,7 +285,7 @@ class inner_product_gadget : public gadget<FieldT> {
assert(A.size() >= 1);
assert(A.size() == B.size());

S.allocate(pb, A.size()-1, libff::FMT(this->annotation_prefix, " S"));
S.allocate(pb, A.size()-1, FMT(this->annotation_prefix, " S"));
}

void generate_r1cs_constraints();
Expand Down Expand Up @@ -321,8 +321,8 @@ class loose_multiplexing_gadget : public gadget<FieldT> {
const std::string &annotation_prefix="") :
gadget<FieldT>(pb, annotation_prefix), arr(arr), index(index), result(result), success_flag(success_flag)
{
alpha.allocate(pb, arr.size(), libff::FMT(this->annotation_prefix, " alpha"));
compute_result.reset(new inner_product_gadget<FieldT>(pb, alpha, arr, result, libff::FMT(this->annotation_prefix, " compute_result")));
alpha.allocate(pb, arr.size(), FMT(this->annotation_prefix, " alpha"));
compute_result.reset(new inner_product_gadget<FieldT>(pb, alpha, arr, result, FMT(this->annotation_prefix, " compute_result")));
};

void generate_r1cs_constraints();
Expand Down
46 changes: 23 additions & 23 deletions src/gadgetlib1/gadgets/basic_gadgets.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,27 @@ void generate_boolean_r1cs_constraint(protoboard<FieldT> &pb, const pb_linear_co
/* forces lc to take value 0 or 1 by adding constraint lc * (1-lc) = 0 */
{
pb.add_r1cs_constraint(r1cs_constraint<FieldT>(lc, 1-lc, 0),
libff::FMT(annotation_prefix, " boolean_r1cs_constraint"));
FMT(annotation_prefix, " boolean_r1cs_constraint"));
}

template<typename FieldT>
void generate_r1cs_equals_const_constraint(protoboard<FieldT> &pb, const pb_linear_combination<FieldT> &lc, const FieldT& c, const std::string &annotation_prefix)
{
pb.add_r1cs_constraint(r1cs_constraint<FieldT>(1, lc, c),
libff::FMT(annotation_prefix, " constness_constraint"));
FMT(annotation_prefix, " constness_constraint"));
}

template<typename FieldT>
void packing_gadget<FieldT>::generate_r1cs_constraints(const bool enforce_bitness)
/* adds constraint result = \sum bits[i] * 2^i */
{
this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(1, pb_packing_sum<FieldT>(bits), packed), libff::FMT(this->annotation_prefix, " packing_constraint"));
this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(1, pb_packing_sum<FieldT>(bits), packed), FMT(this->annotation_prefix, " packing_constraint"));

if (enforce_bitness)
{
for (size_t i = 0; i < bits.size(); ++i)
{
generate_boolean_r1cs_constraint<FieldT>(this->pb, bits[i], libff::FMT(this->annotation_prefix, " bitness_%zu", i));
generate_boolean_r1cs_constraint<FieldT>(this->pb, bits[i], FMT(this->annotation_prefix, " bitness_%zu", i));
}
}
}
Expand Down Expand Up @@ -74,7 +74,7 @@ multipacking_gadget<FieldT>::multipacking_gadget(protoboard<FieldT> &pb,
{
packers.emplace_back(packing_gadget<FieldT>(this->pb, pb_linear_combination_array<FieldT>(bits.begin() + i * chunk_size,
bits.begin() + std::min((i+1) * chunk_size, bits.size())),
packed_vars[i], libff::FMT(this->annotation_prefix, " packers_%zu", i)));
packed_vars[i], FMT(this->annotation_prefix, " packers_%zu", i)));
}
}

Expand Down Expand Up @@ -128,7 +128,7 @@ void field_vector_copy_gadget<FieldT>::generate_r1cs_constraints()
for (size_t i = 0; i < source.size(); ++i)
{
this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(do_copy, source[i] - target[i], 0),
libff::FMT(this->annotation_prefix, " copying_check_%zu", i));
FMT(this->annotation_prefix, " copying_check_%zu", i));
}
}

Expand Down Expand Up @@ -158,13 +158,13 @@ bit_vector_copy_gadget<FieldT>::bit_vector_copy_gadget(protoboard<FieldT> &pb,
{
assert(source_bits.size() == target_bits.size());

packed_source.allocate(pb, num_chunks, libff::FMT(annotation_prefix, " packed_source"));
pack_source.reset(new multipacking_gadget<FieldT>(pb, source_bits, packed_source, chunk_size, libff::FMT(annotation_prefix, " pack_source")));
packed_source.allocate(pb, num_chunks, FMT(annotation_prefix, " packed_source"));
pack_source.reset(new multipacking_gadget<FieldT>(pb, source_bits, packed_source, chunk_size, FMT(annotation_prefix, " pack_source")));

packed_target.allocate(pb, num_chunks, libff::FMT(annotation_prefix, " packed_target"));
pack_target.reset(new multipacking_gadget<FieldT>(pb, target_bits, packed_target, chunk_size, libff::FMT(annotation_prefix, " pack_target")));
packed_target.allocate(pb, num_chunks, FMT(annotation_prefix, " packed_target"));
pack_target.reset(new multipacking_gadget<FieldT>(pb, target_bits, packed_target, chunk_size, FMT(annotation_prefix, " pack_target")));

copier.reset(new field_vector_copy_gadget<FieldT>(pb, packed_source, packed_target, do_copy, libff::FMT(annotation_prefix, " copier")));
copier.reset(new field_vector_copy_gadget<FieldT>(pb, packed_source, packed_target, do_copy, FMT(annotation_prefix, " copier")));
}

template<typename FieldT>
Expand Down Expand Up @@ -223,7 +223,7 @@ void disjunction_gadget<FieldT>::generate_r1cs_constraints()
}
c1.add_term(output);

this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(a1, b1, c1), libff::FMT(this->annotation_prefix, " inv*sum=output"));
this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(a1, b1, c1), FMT(this->annotation_prefix, " inv*sum=output"));

/* (1-output) * sum = 0 */
linear_combination<FieldT> a2, b2, c2;
Expand All @@ -235,7 +235,7 @@ void disjunction_gadget<FieldT>::generate_r1cs_constraints()
}
c2.add_term(ONE, 0);

this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(a2, b2, c2), libff::FMT(this->annotation_prefix, " (1-output)*sum=0"));
this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(a2, b2, c2), FMT(this->annotation_prefix, " (1-output)*sum=0"));
}

template<typename FieldT>
Expand Down Expand Up @@ -314,7 +314,7 @@ void conjunction_gadget<FieldT>::generate_r1cs_constraints()
c1.add_term(ONE);
c1.add_term(output, -1);

this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(a1, b1, c1), libff::FMT(this->annotation_prefix, " inv*(n-sum)=(1-output)"));
this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(a1, b1, c1), FMT(this->annotation_prefix, " inv*(n-sum)=(1-output)"));

/* output * (n-sum) = 0 */
linear_combination<FieldT> a2, b2, c2;
Expand All @@ -326,7 +326,7 @@ void conjunction_gadget<FieldT>::generate_r1cs_constraints()
}
c2.add_term(ONE, 0);

this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(a2, b2, c2), libff::FMT(this->annotation_prefix, " output*(n-sum)=0"));
this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(a2, b2, c2), FMT(this->annotation_prefix, " output*(n-sum)=0"));
}

template<typename FieldT>
Expand Down Expand Up @@ -411,16 +411,16 @@ void comparison_gadget<FieldT>::generate_r1cs_constraints()

/* not_all_zeros to be Boolean, alpha_i are Boolean by packing gadget */
generate_boolean_r1cs_constraint<FieldT>(this->pb, not_all_zeros,
libff::FMT(this->annotation_prefix, " not_all_zeros"));
FMT(this->annotation_prefix, " not_all_zeros"));

/* constraints for packed(alpha) = 2^n + B - A */
pack_alpha->generate_r1cs_constraints(true);
this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(1, (FieldT(2)^n) + B - A, alpha_packed), libff::FMT(this->annotation_prefix, " main_constraint"));
this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(1, (FieldT(2)^n) + B - A, alpha_packed), FMT(this->annotation_prefix, " main_constraint"));

/* compute result */
all_zeros_test->generate_r1cs_constraints();
this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(less_or_eq, not_all_zeros, less),
libff::FMT(this->annotation_prefix, " less"));
FMT(this->annotation_prefix, " less"));
}

template<typename FieldT>
Expand Down Expand Up @@ -488,7 +488,7 @@ void inner_product_gadget<FieldT>::generate_r1cs_constraints()
this->pb.add_r1cs_constraint(
r1cs_constraint<FieldT>(A[i], B[i],
(i == A.size()-1 ? result : S[i]) + (i == 0 ? 0 * ONE : -S[i-1])),
libff::FMT(this->annotation_prefix, " S_%zu", i));
FMT(this->annotation_prefix, " S_%zu", i));
}
}

Expand Down Expand Up @@ -561,7 +561,7 @@ void loose_multiplexing_gadget<FieldT>::generate_r1cs_constraints()
{
this->pb.add_r1cs_constraint(
r1cs_constraint<FieldT>(alpha[i], index - i, 0),
libff::FMT(this->annotation_prefix, " alpha_%zu", i));
FMT(this->annotation_prefix, " alpha_%zu", i));
}

/* 1 * (\sum \alpha_i) = success_flag */
Expand All @@ -572,11 +572,11 @@ void loose_multiplexing_gadget<FieldT>::generate_r1cs_constraints()
b.add_term(alpha[i]);
}
c.add_term(success_flag);
this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(a, b, c), libff::FMT(this->annotation_prefix, " main_constraint"));
this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(a, b, c), FMT(this->annotation_prefix, " main_constraint"));

/* now success_flag is constrained to either 0 (if index is out of
range) or \alpha_i. constrain it and \alpha_i to zero */
generate_boolean_r1cs_constraint<FieldT>(this->pb, success_flag, libff::FMT(this->annotation_prefix, " success_flag"));
generate_boolean_r1cs_constraint<FieldT>(this->pb, success_flag, FMT(this->annotation_prefix, " success_flag"));

/* compute result */
compute_result->generate_r1cs_constraints();
Expand Down Expand Up @@ -680,7 +680,7 @@ void create_linear_combination_constraints(protoboard<FieldT> &pb,

c.add_term(target.all_vars[i]);

pb.add_r1cs_constraint(r1cs_constraint<FieldT>(a, b, c), libff::FMT(annotation_prefix, " linear_combination_%zu", i));
pb.add_r1cs_constraint(r1cs_constraint<FieldT>(a, b, c), FMT(annotation_prefix, " linear_combination_%zu", i));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ bar_gadget<FieldT>::bar_gadget(protoboard<FieldT> &pb,
assert(X.size() == Y.size());
width = X.size();

result.allocate(pb, libff::FMT(annotation_prefix, " result"));
Z_bits.allocate(pb, width, libff::FMT(annotation_prefix, " Z_bits"));
overflow.allocate(pb, 2*width, libff::FMT(annotation_prefix, " overflow"));
result.allocate(pb, FMT(annotation_prefix, " result"));
Z_bits.allocate(pb, width, FMT(annotation_prefix, " Z_bits"));
overflow.allocate(pb, 2*width, FMT(annotation_prefix, " overflow"));

unpacked_result.insert(unpacked_result.end(), Z_bits.begin(), Z_bits.end());
unpacked_result.insert(unpacked_result.end(), overflow.begin(), overflow.end());

unpack_result.reset(new packing_gadget<FieldT>(pb, unpacked_result, result, libff::FMT(annotation_prefix, " unpack_result")));
pack_Z.reset(new packing_gadget<FieldT>(pb, Z_bits, Z_packed, libff::FMT(annotation_prefix, " pack_Z")));
unpack_result.reset(new packing_gadget<FieldT>(pb, unpacked_result, result, FMT(annotation_prefix, " unpack_result")));
pack_Z.reset(new packing_gadget<FieldT>(pb, Z_bits, Z_packed, FMT(annotation_prefix, " pack_Z")));
}

template<typename FieldT>
Expand All @@ -51,7 +51,7 @@ void bar_gadget<FieldT>::generate_r1cs_constraints()
unpack_result->generate_r1cs_constraints(true);
pack_Z->generate_r1cs_constraints(false);

this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(1, a * pb_packing_sum<FieldT>(X) + b * pb_packing_sum<FieldT>(Y), result), libff::FMT(this->annotation_prefix, " compute_result"));
this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(1, a * pb_packing_sum<FieldT>(X) + b * pb_packing_sum<FieldT>(Y), result), FMT(this->annotation_prefix, " compute_result"));
}

template<typename FieldT>
Expand Down
Loading

0 comments on commit a6e32b3

Please sign in to comment.