Clearmatics Libsnark  0.1
C++ library for zkSNARK proofs
Classes | Functions
gadgetExamples Namespace Reference

Classes

class  HashDifficultyEnforcer_Gadget
 
class  NAND_Gadget
 
class  R1P_VerifyTransactionAmounts_Gadget
 

Functions

 TEST (Examples, ProtoboardUsage)
 
 TEST (Examples, NAND_Gadget)
 
 TEST (Examples, HashDifficultyEnforcer_Gadget)
 
 CREATE_GADGET_BASE_CLASS (VerifyTransactionAmounts_GadgetBase)
 
 CREATE_GADGET_FACTORY_CLASS_3 (VerifyTransactionAmounts_Gadget, VariableArray, txInputAmounts, VariableArray, txOutputAmounts, Variable, minersFee)
 
 TEST (Examples, R1P_VerifyTransactionAmounts_Gadget)
 
 TEST (gadgetLib2, Integration)
 

Function Documentation

◆ CREATE_GADGET_BASE_CLASS()

gadgetExamples::CREATE_GADGET_BASE_CLASS ( VerifyTransactionAmounts_GadgetBase  )

◆ CREATE_GADGET_FACTORY_CLASS_3()

gadgetExamples::CREATE_GADGET_FACTORY_CLASS_3 ( VerifyTransactionAmounts_Gadget  ,
VariableArray  ,
txInputAmounts  ,
VariableArray  ,
txOutputAmounts  ,
Variable  ,
minersFee   
)

◆ TEST() [1/5]

gadgetExamples::TEST ( Examples  ,
HashDifficultyEnforcer_Gadget   
)

Definition at line 398 of file tutorial.cpp.

399 {
401  auto pb = Protoboard::create(R1P);
402  const MultiPackedWord hashValue(64, R1P, "hashValue");
403  const size_t difficulty = 10;
404  auto difficultyEnforcer =
405  HashDifficultyEnforcer_Gadget::create(pb, hashValue, difficulty);
406  difficultyEnforcer->generateConstraints();
407  // constraints are created but no assignment yet. Will throw error on
408  // evaluation
409  EXPECT_ANY_THROW(pb->isSatisfied());
410  pb->val(hashValue[0]) = 42;
411  difficultyEnforcer->generateWitness();
412  // First 10 bits of 42 (when represented as a 64 bit number) are '0' so this
413  // should work
414  EXPECT_TRUE(pb->isSatisfied(PrintOptions::DBG_PRINT_IF_NOT_SATISFIED));
415  pb->val(hashValue[0]) = 1000000000000000000;
416  // This is a value > 2^54 so we expect constraint system not to be
417  // satisfied.
418  difficultyEnforcer
419  ->generateWitness(); // This would have failed had we put an assertion
420  EXPECT_FALSE(pb->isSatisfied());
421 }
Here is the call graph for this function:

◆ TEST() [2/5]

gadgetExamples::TEST ( Examples  ,
NAND_Gadget   
)

Definition at line 237 of file tutorial.cpp.

238 {
239  // initialize the field
241  // create a protoboard for a system of rank 1 constraints over a prime
242  // field.
243  ProtoboardPtr pb = Protoboard::create(R1P);
244  // create 5 variables inputs[0]...inputs[4]. The string "inputs" is used for
245  // debug messages
246  FlagVariableArray inputs(5, "inputs");
247  FlagVariable output("output");
248  GadgetPtr nandGadget = NAND_Gadget::create(pb, inputs, output);
249  // now we can generate a constraint system (or circuit)
250  nandGadget->generateConstraints();
251  // if we try to evaluate the circuit now, an exception will be thrown,
252  // because we will be attempting to evaluate unassigned variables.
253  EXPECT_ANY_THROW(pb->isSatisfied());
254  // so let's assign the input variables for NAND and try again after creating
255  // the witness
256  for (const auto &input : inputs) {
257  pb->val(input) = 1;
258  }
259  nandGadget->generateWitness();
260  EXPECT_TRUE(pb->isSatisfied());
261  EXPECT_TRUE(pb->val(output) == 0);
262  // now let's try to ruin something and see what happens
263  pb->val(inputs[2]) = 0;
264  EXPECT_FALSE(pb->isSatisfied());
265  // now let's try to cheat. If we hadn't enforced booleanity, this would have
266  // worked!
267  pb->val(inputs[1]) = 2;
268  EXPECT_FALSE(pb->isSatisfied());
269  // now let's reset inputs[1] to a valid value
270  pb->val(inputs[1]) = 1;
271  // before, we set both the inputs and the output. Notice the output is still
272  // set to '0'
273  EXPECT_TRUE(pb->val(output) == 0);
274  // Now we will let the gadget compute the result using generateWitness() and
275  // see what happens
276  nandGadget->generateWitness();
277  EXPECT_TRUE(pb->val(output) == 1);
278  EXPECT_TRUE(pb->isSatisfied());
279 }
Here is the call graph for this function:

◆ TEST() [3/5]

gadgetExamples::TEST ( Examples  ,
ProtoboardUsage   
)

Definition at line 50 of file tutorial.cpp.

51 {
52  // Initialize prime field parameters. This is always needed for R1P.
54  // The protoboard is the 'memory manager' which holds all constraints (when
55  // creating the verifying circuit) and variable assignments (when creating
56  // the proof witness). We specify the type as R1P, this can be augmented in
57  // the future to allow for BOOLEAN or GF2_EXTENSION fields in the future.
58  ProtoboardPtr pb = Protoboard::create(R1P);
59  // We now create 3 input variables and one output
60  VariableArray input(3, "input");
61  Variable output("output");
62  // We can now add some constraints. The string is for debugging purposes and
63  // can be a textual description of the constraint
64  pb->addRank1Constraint(
65  input[0],
66  5 + input[2],
67  output,
68  "Constraint 1: input[0] * (5 + input[2]) == output");
69  // The second form addUnaryConstraint(LinearCombination) means
70  // (LinearCombination == 0).
71  pb->addUnaryConstraint(
72  input[1] - output, "Constraint 2: input[1] - output == 0");
73  // Notice this could also have been written:
74  // pb->addRank1Constraint(1, input[1] - input[2], 0, "");
75  //
76  // For fields with more general forms, once implemented, we could use
77  // addGeneralConstraint(Polynomial1, Polynomial2, string) which translates
78  // to the constraint (Polynomial1 == Polynomial2). Example:
79  // pb->addGeneralConstraint(input[0] * (3 + input[1]) * input[2], output +
80  // 5,
81  // "input[0] * (3 + input[1]) * input[2] == output
82  // + 5");
83  //
84  // Now we can assign values to the variables and see if the constraints are
85  // satisfied. Later, when we will run a SNARK (or any other proof system),
86  // the constraints will be used by the verifier, and the assigned values
87  // will be used by the prover. Notice the protoboard stores the assignment
88  // values.
89  pb->val(input[0]) = pb->val(input[1]) = pb->val(input[2]) =
90  pb->val(output) = 42;
91  EXPECT_FALSE(pb->isSatisfied());
92  // The constraint system is not satisfied. Now let's try values which
93  // satisfy the two equations above:
94  pb->val(input[0]) = 1;
95  pb->val(input[1]) = pb->val(output) = 42; // input[1] - output == 0
96  pb->val(input[2]) = 37; // 1 * (5 + 37) == 42
97  EXPECT_TRUE(pb->isSatisfied());
98 }
Here is the call graph for this function:

◆ TEST() [4/5]

gadgetExamples::TEST ( Examples  ,
R1P_VerifyTransactionAmounts_Gadget   
)

Definition at line 573 of file tutorial.cpp.

574 {
576  auto pb = Protoboard::create(R1P);
577  const VariableArray inputAmounts(2, "inputAmounts");
578  const VariableArray outputAmounts(3, "outputAmounts");
579  const Variable minersFee("minersFee");
580  auto verifyTx = VerifyTransactionAmounts_Gadget::create(
581  pb, inputAmounts, outputAmounts, minersFee);
582  verifyTx->generateConstraints();
583  pb->val(inputAmounts[0]) = pb->val(inputAmounts[1]) = 2;
584  pb->val(outputAmounts[0]) = pb->val(outputAmounts[1]) =
585  pb->val(outputAmounts[2]) = 1;
586  verifyTx->generateWitness();
587  EXPECT_TRUE(pb->isSatisfied());
588  EXPECT_EQ(pb->val(minersFee), 1);
589  pb->val(minersFee) = 3;
590  EXPECT_FALSE(pb->isSatisfied());
591 }
Here is the call graph for this function:

◆ TEST() [5/5]

gadgetExamples::TEST ( gadgetLib2  ,
Integration   
)

Definition at line 598 of file tutorial.cpp.

599 {
601  // Create an example constraint system and translate to libsnark format
604  const bool test_serialization = false;
605  // Run ppzksnark. Jump into function for breakdown
606  const bool bit = libsnark::run_r1cs_ppzksnark<libff::default_ec_pp>(
607  example, test_serialization);
608  EXPECT_TRUE(bit);
609 };
Here is the call graph for this function:
libsnark::r1cs_example
Definition: r1cs_examples.hpp:25
gadgetlib2::VariableArray
VariableArray.
Definition: variable.hpp:353
gadgetlib2::MultiPackedWord
Definition: variable.hpp:404
gadgetlib2::GadgetPtr
::std::shared_ptr< Gadget > GadgetPtr
Definition: gadget.hpp:119
gadgetlib2::initPublicParamsFromDefaultPp
PublicParams initPublicParamsFromDefaultPp()
Definition: pp.cpp:23
gadgetlib2::Variable
A formal variable, field agnostic.
Definition: variable.hpp:306
libsnark::gen_r1cs_example_from_gadgetlib2_protoboard
r1cs_example< libff::Fr< libff::default_ec_pp > > gen_r1cs_example_from_gadgetlib2_protoboard(const size_t size)
Definition: simple_example.cpp:19
gadgetlib2::ProtoboardPtr
::std::shared_ptr< Protoboard > ProtoboardPtr
Definition: variable.hpp:42
gadgetlib2::R1P
@ R1P
Definition: variable.hpp:37