Clearmatics Libsnark  0.1
C++ library for zkSNARK proofs
variable.cpp
Go to the documentation of this file.
1 
10 #include <climits>
11 #include <iostream>
15 #include <set>
16 #include <stdexcept>
17 #include <vector>
18 
19 using ::std::cout;
20 using ::std::dynamic_pointer_cast;
21 using ::std::endl;
22 using ::std::set;
23 using ::std::shared_ptr;
24 using ::std::string;
25 using ::std::stringstream;
26 using ::std::vector;
27 
28 namespace gadgetlib2
29 {
30 
31 // Optimization: In the future we may want to port most of the member functions
32 // from this file to the .hpp files in order to allow for compiler inlining. As
33 // inlining has tradeoffs this should be profiled before doing so.
34 
35 /*************************************************************************************************/
36 /*************************************************************************************************/
37 /******************* ******************/
38 /******************* class FElem ******************/
39 /******************* ******************/
40 /*************************************************************************************************/
41 /*************************************************************************************************/
42 
43 FElem::FElem(const FElemInterface &elem) : elem_(elem.clone()) {}
44 FElem::FElem() : elem_(new FConst(0)) {}
45 FElem::FElem(const long n) : elem_(new FConst(n)) {}
46 FElem::FElem(const int i) : elem_(new FConst(i)) {}
47 FElem::FElem(const size_t n) : elem_(new FConst(n)) {}
48 FElem::FElem(const Fp &elem) : elem_(new R1P_Elem(elem)) {}
49 FElem::FElem(const FElem &src) : elem_(src.elem_->clone()) {}
50 
52 {
53  if (fieldType() == other.fieldType() || fieldType() == AGNOSTIC) {
54  elem_ = other.elem_->clone();
55  } else if (other.fieldType() != AGNOSTIC) {
56  GADGETLIB_FATAL("Attempted to assign field element of incorrect type");
57  } else {
58  *elem_ = dynamic_cast<FConst *>(other.elem_.get())->asLong();
59  }
60  return *this;
61 }
62 
64 {
65  if (fieldType() == other.fieldType() || fieldType() == AGNOSTIC) {
66  elem_ = ::std::move(other.elem_);
67  } else if (other.elem_->fieldType() != AGNOSTIC) {
69  "Attempted to move assign field element of incorrect type");
70  } else {
71  *elem_ = dynamic_cast<FConst *>(other.elem_.get())->asLong();
72  }
73  return *this;
74 }
75 
77  const FieldType &lhsField, const FieldType &rhsField)
78 {
79  if (lhsField == rhsField)
80  return false;
81  if (rhsField == AGNOSTIC)
82  return false;
83  return true;
84 }
85 
87 {
88  if (!fieldMustBePromotedForArithmetic(this->fieldType(), type)) {
89  return;
90  }
91  if (type == R1P) {
92  const FConst *fConst = dynamic_cast<FConst *>(elem_.get());
94  fConst != NULL, "Cannot convert between specialized field types.");
95  elem_.reset(new R1P_Elem(fConst->asLong()));
96  } else {
97  GADGETLIB_FATAL("Attempted to promote to unknown field type");
98  }
99 }
100 
102 {
103  promoteToFieldType(other.fieldType());
104  *elem_ *= *other.elem_;
105  return *this;
106 }
107 
109 {
110  promoteToFieldType(other.fieldType());
111  *elem_ += *other.elem_;
112  return *this;
113 }
114 
116 {
117  promoteToFieldType(other.fieldType());
118  *elem_ -= *other.elem_;
119  return *this;
120 }
121 
122 FElem FElem::inverse(const FieldType &fieldType)
123 {
125  return FElem(*(elem_->inverse()));
126 }
127 
128 int FElem::getBit(unsigned int i, const FieldType &fieldType)
129 {
131  if (this->fieldType() == fieldType) {
132  return elem_->getBit(i);
133  } else {
135  "Attempted to extract bits from incompatible field type.");
136  }
137 }
138 
139 FElem power(const FElem &base, long exponent)
140 { // TODO .cpp
141  FElem retval(base);
142  retval.elem_->power(exponent);
143  return retval;
144 }
145 
146 /***********************************/
147 /*** END OF CLASS DEFINITION ***/
148 /***********************************/
149 
150 /*************************************************************************************************/
151 /*************************************************************************************************/
152 /******************* ******************/
153 /******************* class FConst ******************/
154 /******************* ******************/
155 /*************************************************************************************************/
156 /*************************************************************************************************/
157 
159 {
160  contents_ += dynamic_cast<const FConst &>(other).contents_;
161  return *this;
162 }
163 
165 {
166  contents_ -= dynamic_cast<const FConst &>(other).contents_;
167  return *this;
168 }
169 
171 {
172  contents_ *= dynamic_cast<const FConst &>(other).contents_;
173  return *this;
174 }
175 
177 {
178  GADGETLIB_FATAL("Attempted to invert an FConst element.");
179 }
180 
182 {
183  contents_ = 0.5 + ::std::pow(double(contents_), double(exponent));
184  return *this;
185 }
186 
187 /***********************************/
188 /*** END OF CLASS DEFINITION ***/
189 /***********************************/
190 
191 /*************************************************************************************************/
192 /*************************************************************************************************/
193 /******************* ******************/
194 /******************* class R1P_Elem ******************/
195 /******************* ******************/
196 /*************************************************************************************************/
197 /*************************************************************************************************/
198 
200 {
201  if (other.fieldType() == R1P) {
202  elem_ += dynamic_cast<const R1P_Elem &>(other).elem_;
203  } else if (other.fieldType() == AGNOSTIC) {
204  elem_ += dynamic_cast<const FConst &>(other).asLong();
205  } else {
206  GADGETLIB_FATAL("Attempted to add incompatible type to R1P_Elem.");
207  }
208  return *this;
209 }
210 
212 {
213  if (other.fieldType() == R1P) {
214  elem_ -= dynamic_cast<const R1P_Elem &>(other).elem_;
215  } else if (other.fieldType() == AGNOSTIC) {
216  elem_ -= dynamic_cast<const FConst &>(other).asLong();
217  } else {
218  GADGETLIB_FATAL("Attempted to add incompatible type to R1P_Elem.");
219  }
220  return *this;
221 }
222 
224 {
225  if (other.fieldType() == R1P) {
226  elem_ *= dynamic_cast<const R1P_Elem &>(other).elem_;
227  } else if (other.fieldType() == AGNOSTIC) {
228  elem_ *= dynamic_cast<const FConst &>(other).asLong();
229  } else {
230  GADGETLIB_FATAL("Attempted to add incompatible type to R1P_Elem.");
231  }
232  return *this;
233 }
234 
235 bool R1P_Elem::operator==(const FElemInterface &other) const
236 {
237  const R1P_Elem *pOther = dynamic_cast<const R1P_Elem *>(&other);
238  if (pOther) {
239  return elem_ == pOther->elem_;
240  }
241  const FConst *pConst = dynamic_cast<const FConst *>(&other);
242  if (pConst) {
243  return *this == *pConst;
244  }
245  GADGETLIB_FATAL("Attempted to Compare R1P_Elem with incompatible type.");
246 }
247 
249 {
250  return FElemInterfacePtr(new R1P_Elem(elem_.inverse()));
251 }
252 
253 long R1P_Elem::asLong() const
254 {
255  // GADGETLIB_ASSERT(elem_.as_ulong() <= LONG_MAX, "long overflow occured.");
256  return long(elem_.as_ulong());
257 }
258 
259 /***********************************/
260 /*** END OF CLASS DEFINITION ***/
261 /***********************************/
262 
263 /*************************************************************************************************/
264 /*************************************************************************************************/
265 /******************* ******************/
266 /******************* class Variable ******************/
267 /******************* ******************/
268 /*************************************************************************************************/
269 /*************************************************************************************************/
270 VarIndex_t Variable::nextFreeIndex_ = 0;
271 
272 #ifdef DEBUG
273 Variable::Variable(const string &name) : index_(nextFreeIndex_++), name_(name)
274 {
276  nextFreeIndex_ > 0,
278  "Variable index overflow has occured, maximum number of "
279  "Variables is %lu",
280  ULONG_MAX));
281 }
282 #else
283 Variable::Variable(const string &name) : index_(nextFreeIndex_++)
284 {
285  libff::UNUSED(name);
287  nextFreeIndex_ > 0,
289  "Variable index overflow has occured, maximum number of "
290  "Variables is %lu",
291  ULONG_MAX));
292 }
293 #endif
294 
296 
297 string Variable::name() const
298 {
299 #ifdef DEBUG
300  return name_;
301 #else
302  return "";
303 #endif
304 }
305 
306 FElem Variable::eval(const VariableAssignment &assignment) const
307 {
308  try {
309  return assignment.at(*this);
310  } catch (::std::out_of_range &) {
312  "Attempted to evaluate unassigned Variable \"%s\", idx:%lu",
313  name().c_str(),
314  index_));
315  }
316 }
317 
318 /***********************************/
319 /*** END OF CLASS DEFINITION ***/
320 /***********************************/
321 
322 /*************************************************************************************************/
323 /*************************************************************************************************/
324 /******************* ******************/
325 /******************* class VariableArray ******************/
326 /******************* ******************/
327 /*************************************************************************************************/
328 /*************************************************************************************************/
329 
330 #ifdef DEBUG
331 VariableArray::VariableArray(const string &name)
332  : VariableArrayContents(), name_(name)
333 {
334 }
335 VariableArray::VariableArray(const int size, const ::std::string &name)
337 {
338  for (int i = 0; i < size; ++i) {
339  push_back(Variable(GADGETLIB2_FMT("%s[%d]", name.c_str(), i)));
340  }
341 }
342 
343 VariableArray::VariableArray(const size_t size, const ::std::string &name)
345 {
346  for (size_t i = 0; i < size; ++i) {
347  push_back(Variable(GADGETLIB2_FMT("%s[%d]", name.c_str(), i)));
348  }
349 }
350 ::std::string VariableArray::name() const { return name_; }
351 
352 #else
353 ::std::string VariableArray::name() const { return ""; }
354 
356 {
357  libff::UNUSED(name);
358 }
359 VariableArray::VariableArray(const int size, const ::std::string &name)
360  : VariableArrayContents(size)
361 {
362  libff::UNUSED(name);
363 }
364 VariableArray::VariableArray(const size_t size, const ::std::string &name)
365  : VariableArrayContents(size)
366 {
367  libff::UNUSED(name);
368 }
369 #endif
370 
371 /***********************************/
372 /*** END OF CLASS DEFINITION ***/
373 /***********************************/
374 
375 /*************************************************************************************************/
376 /*************************************************************************************************/
377 /******************* ******************/
378 /******************* Custom Variable classes ******************/
379 /******************* ******************/
380 /*************************************************************************************************/
381 /*************************************************************************************************/
382 
384  : VariableArray(), numBits_(0), fieldType_(fieldType)
385 {
386 }
387 
389  const size_t numBits, const FieldType &fieldType, const ::std::string &name)
390  : VariableArray(), numBits_(numBits), fieldType_(fieldType)
391 {
392  size_t packedSize = getMultipackedSize();
393  VariableArray varArray(packedSize, name);
394  VariableArray::swap(varArray);
395 }
396 
397 void MultiPackedWord::resize(const size_t numBits)
398 {
399  numBits_ = numBits;
400  size_t packedSize = getMultipackedSize();
401  VariableArray::resize(packedSize);
402 }
403 
404 size_t MultiPackedWord::getMultipackedSize() const
405 {
406  size_t packedSize = 0;
407  if (fieldType_ == R1P) {
408  packedSize = 1; // TODO add assertion that numBits can fit in the field
409  // characteristic
410  } else {
411  GADGETLIB_FATAL("Unknown field type for packed variable.");
412  }
413  return packedSize;
414 }
415 
417  const size_t numBits, const FieldType &fieldType, const ::std::string &name)
418  : multipacked_(numBits, fieldType, name + "_p")
419  , unpacked_(numBits, name + "_u")
420 {
421 }
422 
424  const MultiPackedWord &multipacked, const UnpackedWord &unpacked)
425  : multipacked_(multipacked), unpacked_(unpacked)
426 {
427 }
428 
429 void DualWord::resize(size_t newSize)
430 {
431  multipacked_.resize(newSize);
432  unpacked_.resize(newSize);
433 }
434 
436  : multipackedContents_(0, MultiPackedWord(fieldType))
437  , unpackedContents_(0)
438  , numElements_(0)
439 {
440 }
441 
443  const MultiPackedWordArray &multipackedContents, // TODO delete, for dev
444  const UnpackedWordArray &unpackedContents)
445  : multipackedContents_(multipackedContents)
446  , unpackedContents_(unpackedContents)
447  , numElements_(multipackedContents_.size())
448 {
450  multipackedContents_.size() == numElements_,
451  "Dual Variable multipacked contents size mismatch");
453  unpackedContents_.size() == numElements_,
454  "Dual Variable packed contents size mismatch");
455 }
456 
458 {
459  return multipackedContents_;
460 }
461 UnpackedWordArray DualWordArray::unpacked() const { return unpackedContents_; }
463 {
465  numElements_ == multipackedContents_.size(),
466  "multipacked contents size mismatch")
467  PackedWordArray retval(numElements_);
468  for (size_t i = 0; i < numElements_; ++i) {
469  const auto element = multipackedContents_[i];
471  element.size() == 1, "Cannot convert from multipacked to packed");
472  retval[i] = element[0];
473  }
474  return retval;
475 }
476 
477 void DualWordArray::push_back(const DualWord &dualWord)
478 {
479  multipackedContents_.push_back(dualWord.multipacked());
480  unpackedContents_.push_back(dualWord.unpacked());
481  ++numElements_;
482 }
483 
485 {
486  // const MultiPackedWord multipackedRep = multipacked()[i];
487  // const UnpackedWord unpackedRep = unpacked()[i];
488  // const DualWord retval(multipackedRep, unpackedRep);
489  // return retval;
490  return DualWord(multipacked()[i], unpacked()[i]);
491 }
492 
493 size_t DualWordArray::size() const
494 {
496  multipackedContents_.size() == numElements_,
497  "Dual Variable multipacked contents size mismatch");
499  unpackedContents_.size() == numElements_,
500  "Dual Variable packed contents size mismatch");
501  return numElements_;
502 }
503 
504 /*************************************************************************************************/
505 /*************************************************************************************************/
506 /******************* ******************/
507 /******************* class LinearTerm ******************/
508 /******************* ******************/
509 /*************************************************************************************************/
510 /*************************************************************************************************/
511 
512 ::std::string LinearTerm::asString() const
513 {
514  if (coeff_ == 1) {
515  return variable_.name();
516  } else if (coeff_ == -1) {
517  return GADGETLIB2_FMT("-1 * %s", variable_.name().c_str());
518  } else if (coeff_ == 0) {
519  return GADGETLIB2_FMT("0 * %s", variable_.name().c_str());
520  } else {
521  return GADGETLIB2_FMT(
522  "%s * %s", coeff_.asString().c_str(), variable_.name().c_str());
523  }
524 }
525 
526 FElem LinearTerm::eval(const VariableAssignment &assignment) const
527 {
528  return FElem(coeff_) *= variable_.eval(assignment);
529 }
530 
531 /***********************************/
532 /*** END OF CLASS DEFINITION ***/
533 /***********************************/
534 
535 /*************************************************************************************************/
536 /*************************************************************************************************/
537 /******************* ******************/
538 /******************* class LinearCombination
539  * ******************/
540 /******************* ******************/
541 /*************************************************************************************************/
542 /*************************************************************************************************/
543 
545 {
546  linearTerms_.insert(
547  linearTerms_.end(),
548  other.linearTerms_.cbegin(),
549  other.linearTerms_.cend());
550  constant_ += other.constant_;
551  return *this;
552 }
553 
555 {
556  for (const LinearTerm &lt : other.linearTerms_) {
557  linearTerms_.push_back(-lt);
558  }
559  constant_ -= other.constant_;
560  return *this;
561 }
562 
564 {
565  constant_ *= other;
566  for (LinearTerm &lt : linearTerms_) {
567  lt *= other;
568  }
569  return *this;
570 }
571 
573 {
574  FElem evaluation = constant_;
575  for (const LinearTerm &lt : linearTerms_) {
576  evaluation += lt.eval(assignment);
577  }
578  return evaluation;
579 }
580 
581 ::std::string LinearCombination::asString() const
582 {
583 #ifdef DEBUG
584  ::std::string retval;
585  auto it = linearTerms_.begin();
586  if (it == linearTerms_.end()) {
587  return constant_.asString();
588  } else {
589  retval += it->asString();
590  }
591  for (++it; it != linearTerms_.end(); ++it) {
592  retval += " + " + it->asString();
593  }
594  if (constant_ != 0) {
595  retval += " + " + constant_.asString();
596  }
597  return retval;
598 #else
599  return "";
600 #endif
601 }
602 
604 {
605  Variable::set retSet;
606  for (const LinearTerm &lt : linearTerms_) {
607  retSet.insert(lt.variable());
608  }
609  return retSet;
610 }
611 
612 /***********************************/
613 /*** END OF CLASS DEFINITION ***/
614 /***********************************/
615 
617 {
618  LinearCombination retval(0);
619  for (const Variable &var : inputs) {
620  retval += var;
621  }
622  return retval;
623 }
624 
625 LinearCombination negate(const LinearCombination &lc) { return (1 - lc); }
626 
627 /*************************************************************************************************/
628 /*************************************************************************************************/
629 /******************* ******************/
630 /******************* class Monomial ******************/
631 /******************* ******************/
632 /*************************************************************************************************/
633 /*************************************************************************************************/
634 
635 Monomial::Monomial(const LinearTerm &linearTerm)
636  : coeff_(linearTerm.coeff_), variables_()
637 {
638  variables_.insert(linearTerm.variable_);
639 }
640 
641 FElem Monomial::eval(const VariableAssignment &assignment) const
642 {
643  FElem retval = coeff_;
644  for (const Variable &var : variables_) {
645  retval *= var.eval(assignment);
646  }
647  return retval;
648 }
649 
651 {
652  return Variable::set(variables_.begin(), variables_.end());
653 }
654 
655 const FElem Monomial::getCoefficient() const { return coeff_; }
656 
657 ::std::string Monomial::asString() const
658 {
659 #ifdef DEBUG
660  if (variables_.size() == 0) {
661  return coeff_.asString();
662  }
663  string retval;
664  if (coeff_ != 1) {
665  retval += coeff_.asString() + "*";
666  }
667  auto iter = variables_.begin();
668  retval += iter->name();
669  for (++iter; iter != variables_.end(); ++iter) {
670  retval += "*" + iter->name();
671  }
672  return retval;
673 #else
674  return "";
675 #endif
676 }
677 
679 {
680  Monomial retval = *this;
681  retval.coeff_ = -retval.coeff_;
682  return retval;
683 }
684 
686 {
687  coeff_ *= other.coeff_;
688  variables_.insert(other.variables_.begin(), other.variables_.end());
689  return *this;
690 }
691 
692 /***********************************/
693 /*** END OF CLASS DEFINITION ***/
694 /***********************************/
695 
696 /*************************************************************************************************/
697 /*************************************************************************************************/
698 /******************* ******************/
699 /******************* class Polynomial ******************/
700 /******************* ******************/
701 /*************************************************************************************************/
702 /*************************************************************************************************/
703 
704 Polynomial::Polynomial(const LinearCombination &linearCombination)
705  : monomials_(), constant_(linearCombination.constant_)
706 {
707  for (const LinearTerm &linearTerm : linearCombination.linearTerms_) {
708  monomials_.push_back(Monomial(linearTerm));
709  }
710 }
711 
712 FElem Polynomial::eval(const VariableAssignment &assignment) const
713 {
714  FElem retval = constant_;
715  for (const Monomial &monomial : monomials_) {
716  retval += monomial.eval(assignment);
717  }
718  return retval;
719 }
720 
722 {
723  Variable::set retset;
724  for (const Monomial &monomial : monomials_) {
725  const Variable::set curSet = monomial.getUsedVariables();
726  retset.insert(curSet.begin(), curSet.end());
727  }
728  return retset;
729 }
730 
731 const vector<Monomial> &Polynomial::getMonomials() const { return monomials_; }
732 
733 const FElem Polynomial::getConstant() const { return constant_; }
734 
735 ::std::string Polynomial::asString() const
736 {
737 #ifndef DEBUG
738  return "";
739 #endif
740  if (monomials_.size() == 0) {
741  return constant_.asString();
742  }
743  string retval;
744  auto iter = monomials_.begin();
745  retval += iter->asString();
746  for (++iter; iter != monomials_.end(); ++iter) {
747  retval += " + " + iter->asString();
748  }
749  if (constant_ != 0) {
750  retval += " + " + constant_.asString();
751  }
752  return retval;
753 }
754 
756 {
757  constant_ += other.constant_;
758  monomials_.insert(
759  monomials_.end(), other.monomials_.begin(), other.monomials_.end());
760  return *this;
761 }
762 
764 {
765  vector<Monomial> newMonomials;
766  for (const Monomial &thisMonomial : monomials_) {
767  for (const Monomial &otherMonomial : other.monomials_) {
768  newMonomials.push_back(thisMonomial * otherMonomial);
769  }
770  newMonomials.push_back(thisMonomial * other.constant_);
771  }
772  for (const Monomial &otherMonomial : other.monomials_) {
773  newMonomials.push_back(otherMonomial * this->constant_);
774  }
775  constant_ *= other.constant_;
776  monomials_ = ::std::move(newMonomials);
777  return *this;
778 }
779 
781 {
782  constant_ -= other.constant_;
783  for (const Monomial &otherMonomial : other.monomials_) {
784  monomials_.push_back(-otherMonomial);
785  }
786  return *this;
787 }
788 
789 /***********************************/
790 /*** END OF CLASS DEFINITION ***/
791 /***********************************/
792 
793 } // namespace gadgetlib2
gadgetlib2::Polynomial::eval
FElem eval(const VariableAssignment &assignment) const
Definition: variable.cpp:712
gadgetlib2::DualWord
Holds both representations of a word, both multipacked and unpacked.
Definition: variable.hpp:424
gadgetlib2::Polynomial::Polynomial
Polynomial()
Definition: variable.hpp:581
gadgetlib2::FConst::operator-=
virtual FConst & operator-=(const FElemInterface &other)
Definition: variable.cpp:164
gadgetlib2::LinearCombination::operator+=
LinearCombination & operator+=(const LinearCombination &other)
Definition: variable.cpp:544
gadgetlib2::LinearCombination::asString
::std::string asString() const
Definition: variable.cpp:581
gadgetlib2::Monomial::getUsedVariables
const Variable::set getUsedVariables() const
Definition: variable.cpp:650
gadgetlib2::LinearCombination::eval
FElem eval(const VariableAssignment &assignment) const
Definition: variable.cpp:572
gadgetlib2::VariableArrayContents
::std::vector< Variable > VariableArrayContents
Definition: variable.hpp:350
gadgetlib2::MultiPackedWord::MultiPackedWord
MultiPackedWord(const FieldType &fieldType=AGNOSTIC)
Definition: variable.cpp:383
gadgetlib2::FConst::operator+=
virtual FConst & operator+=(const FElemInterface &other)
Definition: variable.cpp:158
gadgetlib2::R1P_Elem::asLong
long asLong() const
Definition: variable.cpp:253
gadgetlib2::Polynomial::getConstant
const FElem getConstant() const
Definition: variable.cpp:733
gadgetlib2::GADGETLIB2_FMT
::std::string GADGETLIB2_FMT(const char *format,...)
Definition: infrastructure.cpp:49
gadgetlib2::FElem::operator-=
FElem & operator-=(const FElem &other)
Definition: variable.cpp:115
gadgetlib2::FConst::power
virtual FElemInterface & power(long exponent)
Definition: variable.cpp:181
gadgetlib2::LinearCombination::operator-=
LinearCombination & operator-=(const LinearCombination &other)
Definition: variable.cpp:554
gadgetlib2::FElem::promoteToFieldType
void promoteToFieldType(FieldType type)
Definition: variable.cpp:86
gadgetlib2::DualWordArray::packed
PackedWordArray packed() const
Definition: variable.cpp:462
gadgetlib2::Variable::Variable
Variable(const ::std::string &name="")
allocates the variable
Definition: variable.cpp:283
gadgetlib2::VarIndex_t
unsigned long VarIndex_t
Definition: variable.hpp:43
gadgetlib2::Variable::name
::std::string name() const
Definition: variable.cpp:297
gadgetlib2::LinearTerm
LinearTerm.
Definition: variable.hpp:474
infrastructure.hpp
gadgetlib2::LinearTerm::eval
FElem eval(const VariableAssignment &assignment) const
Definition: variable.cpp:526
gadgetlib2::MultiPackedWordArray
::std::vector< MultiPackedWord > MultiPackedWordArray
Definition: variable.hpp:421
gadgetlib2::Polynomial
Polynomial.
Definition: variable.hpp:574
gadgetlib2::DualWord::DualWord
DualWord(const FieldType &fieldType)
Definition: variable.hpp:431
gadgetlib2::FElem::FElem
FElem()
Definition: variable.cpp:44
gadgetlib2::Polynomial::operator-=
Polynomial & operator-=(const Polynomial &other)
Definition: variable.cpp:780
gadgetlib2::Monomial::operator*=
Monomial & operator*=(const Monomial &other)
Definition: variable.cpp:685
gadgetlib2::negate
LinearCombination negate(const LinearCombination &lc)
Definition: variable.cpp:625
gadgetlib2::FieldType
FieldType
Definition: variable.hpp:37
gadgetlib2::FConst::asLong
long asLong() const
Definition: variable.hpp:232
gadgetlib2::DualWordArray::multipacked
MultiPackedWordArray multipacked() const
Definition: variable.cpp:457
gadgetlib2::FElemInterface
Definition: variable.hpp:53
gadgetlib2::AGNOSTIC
@ AGNOSTIC
Definition: variable.hpp:37
gadgetlib2::VariableAssignment
::std::map< Variable, FElem, Variable::VariableStrictOrder > VariableAssignment
Definition: variable.hpp:348
gadgetlib2::R1P_Elem::operator-=
virtual R1P_Elem & operator-=(const FElemInterface &other)
Definition: variable.cpp:211
gadgetlib2::Monomial::Monomial
Monomial(const Variable &var)
Definition: variable.hpp:553
gadgetlib2::VariableArray
VariableArray.
Definition: variable.hpp:353
gadgetlib2::Polynomial::operator+=
Polynomial & operator+=(const Polynomial &other)
Definition: variable.cpp:755
gadgetlib2::FConst::inverse
virtual FElemInterfacePtr inverse() const
Definition: variable.cpp:176
gadgetlib2::MultiPackedWord
Definition: variable.hpp:404
gadgetlib2::FElem::asLong
long asLong() const
Definition: variable.hpp:140
gadgetlib2::LinearCombination::operator*=
LinearCombination & operator*=(const FElem &other)
Definition: variable.cpp:563
gadgetlib2::LinearCombination::constant_
FElem constant_
Definition: variable.hpp:507
gadgetlib2::Variable::VariableAssignment
::std::map< Variable, FElem, Variable::VariableStrictOrder > VariableAssignment
Definition: variable.hpp:337
gadgetlib2::R1P_Elem::operator==
virtual bool operator==(const FElemInterface &other) const
Definition: variable.cpp:235
gadgetlib2::DualWordArray::at
DualWord at(size_t i) const
Definition: variable.cpp:484
gadgetlib2::Monomial::getCoefficient
const FElem getCoefficient() const
Definition: variable.cpp:655
GADGETLIB_FATAL
#define GADGETLIB_FATAL(msg)
Definition: infrastructure.hpp:85
gadgetlib2::Monomial::operator-
Monomial operator-() const
Definition: variable.cpp:678
gadgetlib2::UnpackedWordArray
::std::vector< UnpackedWord > UnpackedWordArray
Definition: variable.hpp:399
gadgetlib2::Monomial::eval
FElem eval(const VariableAssignment &assignment) const
Definition: variable.cpp:641
gadgetlib2::MultiPackedWord::resize
void resize(const size_t numBits)
Definition: variable.cpp:397
gadgetlib2::DualWord::resize
void resize(size_t newSize)
Definition: variable.cpp:429
gadgetlib2::Variable::set
::std::set< Variable, VariableStrictOrder > set
A set of Variables should be declared as follows: Variable::set s1;.
Definition: variable.hpp:341
gadgetlib2::power
FElem power(const FElem &base, long exponent)
Definition: variable.cpp:139
gadgetlib2::Polynomial::getUsedVariables
const Variable::set getUsedVariables() const
Definition: variable.cpp:721
gadgetlib2::FElem
Definition: variable.hpp:101
gadgetlib2::UnpackedWord
Definition: variable.hpp:389
variable.hpp
gadgetlib2::DualWordArray::DualWordArray
DualWordArray(const FieldType &fieldType)
Definition: variable.cpp:435
gadgetlib2::FElemInterface::fieldType
virtual FieldType fieldType() const =0
gadgetlib2::Variable::~Variable
virtual ~Variable()
Definition: variable.cpp:295
gadgetlib2::FElemInterfacePtr
::std::unique_ptr< FElemInterface > FElemInterfacePtr
Definition: variable.hpp:41
pp.hpp
gadgetlib2::FElem::fieldType
FieldType fieldType() const
Definition: variable.hpp:128
gadgetlib2::FElem::operator+=
FElem & operator+=(const FElem &other)
Definition: variable.cpp:108
gadgetlib2::R1P_Elem::inverse
virtual FElemInterfacePtr inverse() const
Definition: variable.cpp:248
gadgetlib2::FConst
Definition: variable.hpp:189
gadgetlib2::VariableArray::name
::std::string name() const
Definition: variable.cpp:353
gadgetlib2::DualWord::multipacked
MultiPackedWord multipacked() const
Definition: variable.hpp:439
gadgetlib2::Variable
A formal variable, field agnostic.
Definition: variable.hpp:306
gadgetlib2::fieldMustBePromotedForArithmetic
bool fieldMustBePromotedForArithmetic(const FieldType &lhsField, const FieldType &rhsField)
Definition: variable.cpp:76
GADGETLIB_ASSERT
#define GADGETLIB_ASSERT(predicate, msg)
Definition: infrastructure.hpp:94
gadgetlib2::FElem::inverse
FElem inverse(const FieldType &fieldType)
Definition: variable.cpp:122
gadgetlib2::MultiPackedWord::name
::std::string name() const
Definition: variable.hpp:418
gadgetlib2::DualWord::unpacked
UnpackedWord unpacked() const
Definition: variable.hpp:440
gadgetlib2::Monomial
Monomial.
Definition: variable.hpp:543
gadgetlib2::R1P_Elem::operator*=
virtual R1P_Elem & operator*=(const FElemInterface &other)
Definition: variable.cpp:223
gadgetlib2::sum
LinearCombination sum(const VariableArray &inputs)
Definition: variable.cpp:616
gadgetlib2::LinearTerm::asString
::std::string asString() const
Definition: variable.cpp:512
gadgetlib2::LinearCombination::linearTerms_
::std::vector< LinearTerm > linearTerms_
Definition: variable.hpp:506
gadgetlib2::R1P_Elem::R1P_Elem
R1P_Elem(const Fp &elem)
Definition: variable.hpp:251
gadgetlib2::R1P_Elem::operator+=
virtual R1P_Elem & operator+=(const FElemInterface &other)
Definition: variable.cpp:199
gadgetlib2::LinearCombination
LinearCombination.
Definition: variable.hpp:503
gadgetlib2::Polynomial::getMonomials
const std::vector< Monomial > & getMonomials() const
Definition: variable.cpp:731
gadgetlib2::LinearCombination::getUsedVariables
const Variable::set getUsedVariables() const
Definition: variable.cpp:603
gadgetlib2::VariableArray::VariableArray
VariableArray(const ::std::string &name="")
gadgetlib2::FElem::getBit
int getBit(unsigned int i, const FieldType &fieldType)
Definition: variable.cpp:128
gadgetlib2::FElem::operator*=
FElem & operator*=(const FElem &other)
Definition: variable.cpp:101
gadgetlib2::DualWordArray::size
size_t size() const
Definition: variable.cpp:493
gadgetlib2::Monomial::asString
::std::string asString() const
Definition: variable.cpp:657
gadgetlib2::Variable::eval
FElem eval(const VariableAssignment &assignment) const
Definition: variable.cpp:306
gadgetlib2::FElem::asString
::std::string asString() const
Definition: variable.hpp:127
gadgetlib2
Definition: adapters.cpp:15
gadgetlib2::FElem::operator=
FElem & operator=(const FElem &other)
Definition: variable.cpp:51
gadgetlib2::DualWordArray::push_back
void push_back(const DualWord &dualWord)
Definition: variable.cpp:477
gadgetlib2::FConst::operator*=
virtual FConst & operator*=(const FElemInterface &other)
Definition: variable.cpp:170
gadgetlib2::R1P
@ R1P
Definition: variable.hpp:37
gadgetlib2::DualWordArray::unpacked
UnpackedWordArray unpacked() const
Definition: variable.cpp:461
gadgetlib2::Polynomial::asString
::std::string asString() const
Definition: variable.cpp:735
gadgetlib2::Polynomial::operator*=
Polynomial & operator*=(const Polynomial &other)
Definition: variable.cpp:763
gadgetlib2::Fp
libff::Fr< libff::default_ec_pp > Fp
Definition: pp.hpp:29
gadgetlib2::R1P_Elem
Definition: variable.hpp:245