Clearmatics Libsnark  0.1
C++ library for zkSNARK proofs
Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | List of all members
gadgetlib2::Protoboard Class Reference

#include <protoboard.hpp>

Collaboration diagram for gadgetlib2::Protoboard:
Collaboration graph
[legend]

Public Member Functions

size_t numVars () const
 
size_t numInputs () const
 
ParamsCPtr params () const
 
FElemval (const Variable &var)
 
FElem val (const LinearCombination &lc) const
 
void setValuesAsBitArray (const VariableArray &varArray, const size_t srcValue)
 
void setDualWordValue (const DualWord &dualWord, const size_t srcValue)
 
void setMultipackedWordValue (const MultiPackedWord &multipackedWord, const size_t srcValue)
 
void addRank1Constraint (const LinearCombination &a, const LinearCombination &b, const LinearCombination &c, const ::std::string &name)
 
void addGeneralConstraint (const Polynomial &a, const Polynomial &b, const ::std::string &name)
 
void addUnaryConstraint (const LinearCombination &a, const ::std::string &name)
 adds a constraint of the form (a == 0) More...
 
bool isSatisfied (const PrintOptions &printOnFail=PrintOptions::NO_DBG_PRINT)
 
bool flagIsSet (const FlagVariable &flag) const
 
void setFlag (const FlagVariable &flag, bool newFlagState=true)
 
void clearFlag (const FlagVariable &flag)
 
void flipFlag (const FlagVariable &flag)
 
void enforceBooleanity (const Variable &var)
 
::std::string annotation () const
 
ConstraintSystem constraintSystem () const
 
VariableAssignment assignment () const
 
bool dualWordAssignmentEqualsValue (const DualWord &dualWord, const size_t expectedValue, const PrintOptions &printOption=PrintOptions::NO_DBG_PRINT) const
 
bool multipackedWordAssignmentEqualsValue (const MultiPackedWord &multipackedWord, const size_t expectedValue, const PrintOptions &printOption=PrintOptions::NO_DBG_PRINT) const
 
bool unpackedWordAssignmentEqualsValue (const UnpackedWord &unpackedWord, const size_t expectedValue, const PrintOptions &printOption=PrintOptions::NO_DBG_PRINT) const
 

Static Public Member Functions

static ProtoboardPtr create (const FieldType &fieldType, ParamsCPtr pParams=NULL)
 

Public Attributes

const FieldType fieldType_
 

Protected Member Functions

 Protoboard (const FieldType &fieldType, ParamsCPtr pParams)
 

Protected Attributes

VariableAssignment assignment_
 
ConstraintSystem constraintSystem_
 
size_t numInputs_
 
ParamsCPtr pParams_
 

Detailed Description

Definition at line 38 of file protoboard.hpp.

Constructor & Destructor Documentation

◆ Protoboard()

gadgetlib2::Protoboard::Protoboard ( const FieldType fieldType,
ParamsCPtr  pParams 
)
protected

Definition at line 29 of file protoboard.cpp.

30  : numInputs_(0), pParams_(pParams), fieldType_(fieldType)
31 {
32 }
Here is the caller graph for this function:

Member Function Documentation

◆ addGeneralConstraint()

void gadgetlib2::Protoboard::addGeneralConstraint ( const Polynomial a,
const Polynomial b,
const ::std::string &  name 
)

Definition at line 104 of file protoboard.cpp.

106 {
107  constraintSystem_.addConstraint(PolynomialConstraint(a, b, name));
108 }
Here is the call graph for this function:

◆ addRank1Constraint()

void gadgetlib2::Protoboard::addRank1Constraint ( const LinearCombination a,
const LinearCombination b,
const LinearCombination c,
const ::std::string &  name 
)

Definition at line 95 of file protoboard.cpp.

100 {
101  constraintSystem_.addConstraint(Rank1Constraint(a, b, c, name));
102 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ addUnaryConstraint()

void gadgetlib2::Protoboard::addUnaryConstraint ( const LinearCombination a,
const ::std::string &  name 
)

adds a constraint of the form (a == 0)

Definition at line 110 of file protoboard.cpp.

112 {
113  addRank1Constraint(a, 1, 0, name);
114 }
Here is the call graph for this function:

◆ annotation()

string gadgetlib2::Protoboard::annotation ( ) const

Definition at line 135 of file protoboard.cpp.

136 {
137 #ifdef DEBUG
138  string retVal = constraintSystem_.annotation();
139  retVal += "Variable Assignments:\n";
140  for (const auto &assignmentPair : assignment_) {
141  const string varName = assignmentPair.first.name();
142  const string varAssignedValue = assignmentPair.second.asString();
143  retVal += varName + ": " + varAssignedValue + "\n";
144  }
145  return retVal;
146 #else
147  return "";
148 #endif
149 }
Here is the call graph for this function:

◆ assignment()

VariableAssignment gadgetlib2::Protoboard::assignment ( ) const
inline

Definition at line 102 of file protoboard.hpp.

102 { return assignment_; }
Here is the caller graph for this function:

◆ clearFlag()

void gadgetlib2::Protoboard::clearFlag ( const FlagVariable flag)
inline

Definition at line 97 of file protoboard.hpp.

97 { val(flag) = 0; }
Here is the call graph for this function:

◆ constraintSystem()

ConstraintSystem gadgetlib2::Protoboard::constraintSystem ( ) const
inline

Definition at line 101 of file protoboard.hpp.

101 { return constraintSystem_; }
Here is the caller graph for this function:

◆ create()

static ProtoboardPtr gadgetlib2::Protoboard::create ( const FieldType fieldType,
ParamsCPtr  pParams = NULL 
)
inlinestatic

Definition at line 54 of file protoboard.hpp.

56  {
57  return ProtoboardPtr(new Protoboard(fieldType, pParams));
58  }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ dualWordAssignmentEqualsValue()

bool gadgetlib2::Protoboard::dualWordAssignmentEqualsValue ( const DualWord dualWord,
const size_t  expectedValue,
const PrintOptions printOption = PrintOptions::NO_DBG_PRINT 
) const

Definition at line 174 of file protoboard.cpp.

178 {
179  bool multipackedEqualsValue = multipackedWordAssignmentEqualsValue(
180  dualWord.multipacked(), expectedValue, printOption);
181  bool unpackedEqualsValue = unpackedWordAssignmentEqualsValue(
182  dualWord.unpacked(), expectedValue, printOption);
184  multipackedEqualsValue, unpackedEqualsValue)) {
186  multipackedEqualsValue, unpackedEqualsValue);
187  }
188  return multipackedEqualsValue && unpackedEqualsValue;
189 }
Here is the call graph for this function:

◆ enforceBooleanity()

void gadgetlib2::Protoboard::enforceBooleanity ( const Variable var)

Definition at line 126 of file protoboard.cpp.

127 {
129  var,
130  var - 1,
131  0,
132  GADGETLIB2_FMT("enforceBooleanity(%s)", var.name().c_str()));
133 }
Here is the call graph for this function:

◆ flagIsSet()

bool gadgetlib2::Protoboard::flagIsSet ( const FlagVariable flag) const
inline

Definition at line 95 of file protoboard.hpp.

95 { return val(flag) == 1; }
Here is the call graph for this function:

◆ flipFlag()

void gadgetlib2::Protoboard::flipFlag ( const FlagVariable flag)
inline

Definition at line 98 of file protoboard.hpp.

98 { val(flag) = 1 - val(flag); }
Here is the call graph for this function:

◆ isSatisfied()

bool gadgetlib2::Protoboard::isSatisfied ( const PrintOptions printOnFail = PrintOptions::NO_DBG_PRINT)

Definition at line 116 of file protoboard.cpp.

117 {
118  return constraintSystem_.isSatisfied(assignment_, printOnFail);
119 }
Here is the call graph for this function:

◆ multipackedWordAssignmentEqualsValue()

bool gadgetlib2::Protoboard::multipackedWordAssignmentEqualsValue ( const MultiPackedWord multipackedWord,
const size_t  expectedValue,
const PrintOptions printOption = PrintOptions::NO_DBG_PRINT 
) const

Definition at line 200 of file protoboard.cpp.

204 {
205  bool retval = true;
206  if (fieldType_ == R1P) {
208  multipackedWord.size() == 1, "R1P multipacked size mismatch");
209  if (val(multipackedWord[0]) == expectedValue) {
210  retval = true;
211  } else {
212  retval = false;
213  }
214  if (expectedToPrintValues(retval, printOption)) {
215  cout << "Expected value for multipacked word \""
216  << multipackedWord.name() << "\" is: " << expectedValue
217  << endl;
218  cout << "Actual value is: " << val(multipackedWord[0]) << endl;
219  }
220  } else {
222  "Unknown field type in "
223  "Protoboard::multipackedWordAssignmentEqualsValue(...)");
224  }
225  return retval;
226 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ numInputs()

size_t gadgetlib2::Protoboard::numInputs ( ) const
inline

Definition at line 67 of file protoboard.hpp.

68  {
69  return numInputs_;
70  } // TODO Madars How do we book keep this?
Here is the caller graph for this function:

◆ numVars()

size_t gadgetlib2::Protoboard::numVars ( ) const
inline

Definition at line 59 of file protoboard.hpp.

60  {
61  return assignment_.size();
62  } // TODO change to take num from constraintSys_

◆ params()

ParamsCPtr gadgetlib2::Protoboard::params ( ) const
inline

Definition at line 71 of file protoboard.hpp.

71 { return pParams_; }

◆ setDualWordValue()

void gadgetlib2::Protoboard::setDualWordValue ( const DualWord dualWord,
const size_t  srcValue 
)

Definition at line 71 of file protoboard.cpp.

73 {
74  setMultipackedWordValue(dualWord.multipacked(), srcValue);
75  setValuesAsBitArray(dualWord.unpacked(), srcValue);
76 }
Here is the call graph for this function:

◆ setFlag()

void gadgetlib2::Protoboard::setFlag ( const FlagVariable flag,
bool  newFlagState = true 
)

Definition at line 121 of file protoboard.cpp.

122 {
123  val(flag) = newFlagState ? 1 : 0;
124 }
Here is the call graph for this function:

◆ setMultipackedWordValue()

void gadgetlib2::Protoboard::setMultipackedWordValue ( const MultiPackedWord multipackedWord,
const size_t  srcValue 
)

Definition at line 78 of file protoboard.cpp.

80 {
81  if (fieldType_ == R1P) {
83  multipackedWord.size() == 1,
84  "Multipacked word size mismatch in R1P");
85  val(multipackedWord[0]) = srcValue;
86  } else {
88  "Unknown protoboard type in Protoboard::setMultipackedWordValue");
89  }
90 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ setValuesAsBitArray()

void gadgetlib2::Protoboard::setValuesAsBitArray ( const VariableArray varArray,
const size_t  srcValue 
)

Definition at line 50 of file protoboard.cpp.

52 {
54  varArray.size() >= Log2ceil(srcValue),
56  "Variable array of size %u too small to hold value %u. Array must "
57  "be of size "
58  "at least %u",
59  varArray.size(),
60  srcValue,
61  Log2ceil(srcValue)));
62  size_t i = 0;
63  for (i = 0; i < Log2ceil(srcValue); ++i) {
64  val(varArray[i]) = srcValue & (1u << i) ? 1 : 0;
65  }
66  for (; i < varArray.size(); ++i) {
67  val(varArray[i]) = 0;
68  }
69 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ unpackedWordAssignmentEqualsValue()

bool gadgetlib2::Protoboard::unpackedWordAssignmentEqualsValue ( const UnpackedWord unpackedWord,
const size_t  expectedValue,
const PrintOptions printOption = PrintOptions::NO_DBG_PRINT 
) const

Definition at line 228 of file protoboard.cpp.

232 {
233  bool retval = true;
234  size_t expectedValueCopy = expectedValue;
235  for (size_t i = 0; i < unpackedWord.size(); ++i) {
236  if (val(unpackedWord[i]) != (expectedValueCopy & 1u)) {
237  retval = false;
238  break;
239  }
240  expectedValueCopy >>= 1;
241  }
242  if (expectedValueCopy != 0) {
243  retval = false;
244  }
245  if (expectedToPrintValues(retval, printOption)) {
246  cout << "Expected value for unpacked word \"" << unpackedWord.name()
247  << "\" is: " << expectedValue << endl;
248  cout << "Actual values are: " << endl;
249  for (size_t i = 0; i < unpackedWord.size(); ++i) {
250  cout << "bit " << i << ": " << val(unpackedWord[i]) << endl;
251  }
252  }
253  return retval;
254 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ val() [1/2]

FElem gadgetlib2::Protoboard::val ( const LinearCombination lc) const

Definition at line 45 of file protoboard.cpp.

46 {
47  return lc.eval(assignment_);
48 }
Here is the call graph for this function:

◆ val() [2/2]

FElem & gadgetlib2::Protoboard::val ( const Variable var)

Definition at line 34 of file protoboard.cpp.

35 {
36  FElem &retval = assignment_[var];
38  retval.fieldType() == fieldType_ || retval.fieldType() == AGNOSTIC,
40  "Assigned field element of incorrect field type in Variable \"%s\"",
41  var.name().c_str()));
42  return retval;
43 }
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ assignment_

VariableAssignment gadgetlib2::Protoboard::assignment_
protected

Definition at line 41 of file protoboard.hpp.

◆ constraintSystem_

ConstraintSystem gadgetlib2::Protoboard::constraintSystem_
protected

Definition at line 42 of file protoboard.hpp.

◆ fieldType_

const FieldType gadgetlib2::Protoboard::fieldType_

Definition at line 53 of file protoboard.hpp.

◆ numInputs_

size_t gadgetlib2::Protoboard::numInputs_
protected

Definition at line 43 of file protoboard.hpp.

◆ pParams_

ParamsCPtr gadgetlib2::Protoboard::pParams_
protected

Definition at line 45 of file protoboard.hpp.


The documentation for this class was generated from the following files:
gadgetlib2::ConstraintSystem::addConstraint
void addConstraint(const Rank1Constraint &c)
Definition: constraint.cpp:202
gadgetlib2::Protoboard::setValuesAsBitArray
void setValuesAsBitArray(const VariableArray &varArray, const size_t srcValue)
Definition: protoboard.cpp:50
gadgetlib2::expectedToPrintValues
bool expectedToPrintValues(const bool boolValue, const PrintOptions &printOption)
Definition: protoboard.cpp:191
gadgetlib2::Protoboard::numInputs_
size_t numInputs_
Definition: protoboard.hpp:43
gadgetlib2::GADGETLIB2_FMT
::std::string GADGETLIB2_FMT(const char *format,...)
Definition: infrastructure.cpp:49
gadgetlib2::Protoboard::pParams_
ParamsCPtr pParams_
Definition: protoboard.hpp:45
gadgetlib2::Protoboard::fieldType_
const FieldType fieldType_
Definition: protoboard.hpp:53
gadgetlib2::Protoboard::assignment_
VariableAssignment assignment_
Definition: protoboard.hpp:41
gadgetlib2::Protoboard::setMultipackedWordValue
void setMultipackedWordValue(const MultiPackedWord &multipackedWord, const size_t srcValue)
Definition: protoboard.cpp:78
gadgetlib2::Protoboard::val
FElem & val(const Variable &var)
Definition: protoboard.cpp:34
gadgetlib2::AGNOSTIC
@ AGNOSTIC
Definition: variable.hpp:37
gadgetlib2::printInformativeNoticeMessage
void printInformativeNoticeMessage(const bool multipackedEqualsValue, const bool unpackedEqualsValue)
Definition: protoboard.cpp:157
gadgetlib2::Log2ceil
unsigned int Log2ceil(uint64_t i)
Definition: infrastructure.cpp:120
gadgetlib2::multipackedAndUnpackedValuesDisagree
bool multipackedAndUnpackedValuesDisagree(const bool multipackedEqualsValue, const bool unpackedEqualsValue)
Definition: protoboard.cpp:151
gadgetlib2::ConstraintSystem::annotation
::std::string annotation() const
Definition: constraint.cpp:225
GADGETLIB_FATAL
#define GADGETLIB_FATAL(msg)
Definition: infrastructure.hpp:85
gadgetlib2::Protoboard::constraintSystem_
ConstraintSystem constraintSystem_
Definition: protoboard.hpp:42
gadgetlib2::Protoboard::multipackedWordAssignmentEqualsValue
bool multipackedWordAssignmentEqualsValue(const MultiPackedWord &multipackedWord, const size_t expectedValue, const PrintOptions &printOption=PrintOptions::NO_DBG_PRINT) const
Definition: protoboard.cpp:200
gadgetlib2::Protoboard::addRank1Constraint
void addRank1Constraint(const LinearCombination &a, const LinearCombination &b, const LinearCombination &c, const ::std::string &name)
Definition: protoboard.cpp:95
GADGETLIB_ASSERT
#define GADGETLIB_ASSERT(predicate, msg)
Definition: infrastructure.hpp:94
gadgetlib2::ProtoboardPtr
::std::shared_ptr< Protoboard > ProtoboardPtr
Definition: variable.hpp:42
gadgetlib2::ConstraintSystem::isSatisfied
bool isSatisfied(const VariableAssignment &assignment, const PrintOptions &printOnFail=PrintOptions::NO_DBG_PRINT) const
Definition: constraint.cpp:214
gadgetlib2::Protoboard::Protoboard
Protoboard(const FieldType &fieldType, ParamsCPtr pParams)
Definition: protoboard.cpp:29
gadgetlib2::Protoboard::unpackedWordAssignmentEqualsValue
bool unpackedWordAssignmentEqualsValue(const UnpackedWord &unpackedWord, const size_t expectedValue, const PrintOptions &printOption=PrintOptions::NO_DBG_PRINT) const
Definition: protoboard.cpp:228
gadgetlib2::R1P
@ R1P
Definition: variable.hpp:37