Clearmatics Libsnark  0.1
C++ library for zkSNARK proofs
Public Member Functions | Static Public Member Functions | List of all members
gadgetExamples::NAND_Gadget Class Reference
Inheritance diagram for gadgetExamples::NAND_Gadget:
Inheritance graph
[legend]
Collaboration diagram for gadgetExamples::NAND_Gadget:
Collaboration graph
[legend]

Public Member Functions

void generateConstraints ()
 
void generateWitness ()
 
- Public Member Functions inherited from gadgetlib2::Gadget
 Gadget (ProtoboardPtr pb)
 
void addUnaryConstraint (const LinearCombination &a, const ::std::string &name)
 
void addRank1Constraint (const LinearCombination &a, const LinearCombination &b, const LinearCombination &c, const ::std::string &name)
 
void enforceBooleanity (const Variable &var)
 
FElemval (const Variable &var)
 
FElem val (const LinearCombination &lc)
 
FieldType fieldType () const
 
bool flagIsSet (const FlagVariable &flag) const
 

Static Public Member Functions

static GadgetPtr create (ProtoboardPtr pb, const FlagVariableArray &inputs, const FlagVariable &output)
 

Additional Inherited Members

- Protected Attributes inherited from gadgetlib2::Gadget
ProtoboardPtr pb_
 

Detailed Description

Definition at line 113 of file tutorial.cpp.

Member Function Documentation

◆ create()

GadgetPtr gadgetExamples::NAND_Gadget::create ( ProtoboardPtr  pb,
const FlagVariableArray inputs,
const FlagVariable output 
)
static

Definition at line 182 of file tutorial.cpp.

186 {
187  GadgetPtr pGadget(new NAND_Gadget(pb, inputs, output));
188  pGadget->init();
189  return pGadget;
190 }
Here is the caller graph for this function:

◆ generateConstraints()

void gadgetExamples::NAND_Gadget::generateConstraints ( )
virtual

Implements gadgetlib2::Gadget.

Definition at line 192 of file tutorial.cpp.

193 {
194  // we will invoke the AND gate constraint generator
195  andGadget_->generateConstraints();
196  // and add our out negation constraint in order to make this a NAND gate
198  1, 1 - andResult_, output_, "1 * (1 - andResult) = output");
199  // Another way to write the same constraint is:
200  // addUnaryConstraint(1 - andResult_ - output_, "1 - andResult == output");
201  //
202  // At first look, it would seem that this is enough. However, the AND_Gadget
203  // expects all of its inputs to be boolean, a dishonest prover could put
204  // non-boolean inputs, so we must check this here. Notice 'FlagVariable'
205  // means a variable which we intend to hold only '0' or '1', but this is
206  // just a convention (it is a typedef for Variable) and we must enforce it.
207  // Look into the internals of the R1P implementation of AND_Gadget and see
208  // that {2, 1, 0} as inputs with {1} as output would satisfy all
209  // constraints, even though this is clearly not our intent!
210  for (const auto &input : inputs_) {
211  enforceBooleanity(input); // This adds a constraint of the form: input *
212  // (1 - input) == 0
213  }
214 }
Here is the call graph for this function:

◆ generateWitness()

void gadgetExamples::NAND_Gadget::generateWitness ( )
virtual

Reimplemented from gadgetlib2::Gadget.

Definition at line 216 of file tutorial.cpp.

217 {
218  // First we can assert that all input values are indeed boolean. The purpose
219  // of this assertion is simply to print a clear error message, it is not
220  // security critical. Notice the method val() which returns a reference to
221  // the current assignment for a variable
222  for (const auto &input : inputs_) {
224  val(input) == 0 || val(input) == 1, "NAND input is not boolean");
225  }
226  // we will invoke the AND gate witness generator, this will set andResult_
227  // correctly
228  andGadget_->generateWitness();
229  // and now we set the value of output_
230  val(output_) = 1 - val(andResult_);
231  // notice the use of 'val()' to tell the protoboard to assign this new value
232  // to the variable 'output_'. The variable itself is only a formal variable
233  // and never changes.
234 }
Here is the call graph for this function:

The documentation for this class was generated from the following file:
gadgetlib2::Gadget::addRank1Constraint
void addRank1Constraint(const LinearCombination &a, const LinearCombination &b, const LinearCombination &c, const ::std::string &name)
Definition: gadget.cpp:58
gadgetlib2::Gadget::val
FElem & val(const Variable &var)
Definition: gadget.hpp:109
gadgetlib2::GadgetPtr
::std::shared_ptr< Gadget > GadgetPtr
Definition: gadget.hpp:119
gadgetlib2::Gadget::enforceBooleanity
void enforceBooleanity(const Variable &var)
Definition: gadget.hpp:108
GADGETLIB_ASSERT
#define GADGETLIB_ASSERT(predicate, msg)
Definition: infrastructure.hpp:94