Clearmatics Libsnark  0.1
C++ library for zkSNARK proofs
Public Member Functions | Public Attributes | Friends | List of all members
libsnark::tbcs_gate Class Reference

#include <tbcs.hpp>

Public Member Functions

bool evaluate (const tbcs_variable_assignment &input) const
 
void print (const std::map< size_t, std::string > &variable_annotations=std::map< size_t, std::string >()) const
 
bool operator== (const tbcs_gate &other) const
 

Public Attributes

tbcs_wire_t left_wire
 
tbcs_wire_t right_wire
 
tbcs_gate_type type
 
tbcs_wire_t output
 
bool is_circuit_output
 

Friends

std::ostream & operator<< (std::ostream &out, const tbcs_gate &g)
 
std::istream & operator>> (std::istream &in, tbcs_gate &g)
 

Detailed Description

A TBCS gate is a formal expression of the form

           g(left_wire,right_wire) = output ,

where 'left_wire' and 'right_wire' are the two input wires, and 'output' is the output wire. In other words, a TBCS gate is a 2-input boolean gate; there are 16 possible such gates (see tbcs_gate_type above).

A TBCS gate is used to construct a TBCS circuit (see below).

Definition at line 87 of file tbcs.hpp.

Member Function Documentation

◆ evaluate()

bool libsnark::tbcs_gate::evaluate ( const tbcs_variable_assignment input) const

This function is very tricky. See comment in tbcs.hpp .

Definition at line 24 of file tbcs.cpp.

25 {
31  const bool X = (left_wire == 0 ? true : input[left_wire - 1]);
32  const bool Y = (right_wire == 0 ? true : input[right_wire - 1]);
33 
34  // 3 - ... inverts position
35  const size_t pos = 3 - ((X ? 2 : 0) + (Y ? 1 : 0));
36 
37  return (((int)type) & (1u << pos));
38 }
Here is the caller graph for this function:

◆ operator==()

bool libsnark::tbcs_gate::operator== ( const tbcs_gate other) const

Definition at line 125 of file tbcs.cpp.

126 {
127  return (
128  this->left_wire == other.left_wire &&
129  this->right_wire == other.right_wire && this->type == other.type &&
130  this->output == other.output &&
131  this->is_circuit_output == other.is_circuit_output);
132 }

◆ print()

void libsnark::tbcs_gate::print ( const std::map< size_t, std::string > &  variable_annotations = std::map<size_t, std::string>()) const

Definition at line 60 of file tbcs.cpp.

62 {
63  switch (this->type) {
65  printf("CONSTANT_0");
66  break;
67  case TBCS_GATE_AND:
68  printf("AND");
69  break;
71  printf("X_AND_NOT_Y");
72  break;
73  case TBCS_GATE_X:
74  printf("X");
75  break;
77  printf("NOT_X_AND_Y");
78  break;
79  case TBCS_GATE_Y:
80  printf("Y");
81  break;
82  case TBCS_GATE_XOR:
83  printf("XOR");
84  break;
85  case TBCS_GATE_OR:
86  printf("OR");
87  break;
88  case TBCS_GATE_NOR:
89  printf("NOR");
90  break;
92  printf("EQUIVALENCE");
93  break;
94  case TBCS_GATE_NOT_Y:
95  printf("NOT_Y");
96  break;
98  printf("IF_Y_THEN_X");
99  break;
100  case TBCS_GATE_NOT_X:
101  printf("NOT_X");
102  break;
104  printf("IF_X_THEN_Y");
105  break;
106  case TBCS_GATE_NAND:
107  printf("NAND");
108  break;
110  printf("CONSTANT_1");
111  break;
112  default:
113  printf("Invalid type");
114  }
115 
116  printf("\n(\n");
117  print_tbcs_wire(left_wire, variable_annotations);
118  printf(",\n");
119  print_tbcs_wire(right_wire, variable_annotations);
120  printf("\n) ->\n");
121  print_tbcs_wire(output, variable_annotations);
122  printf(" (%s)\n", is_circuit_output ? "circuit output" : "internal wire");
123 }
Here is the call graph for this function:

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  out,
const tbcs_gate g 
)
friend

Definition at line 134 of file tbcs.cpp.

135 {
136  out << g.left_wire << "\n";
137  out << g.right_wire << "\n";
138  out << (int)g.type << "\n";
139  out << g.output << "\n";
140  libff::output_bool(out, g.is_circuit_output);
141 
142  return out;
143 }

◆ operator>>

std::istream& operator>> ( std::istream &  in,
tbcs_gate g 
)
friend

Definition at line 145 of file tbcs.cpp.

146 {
147  in >> g.left_wire;
148  libff::consume_newline(in);
149  in >> g.right_wire;
150  libff::consume_newline(in);
151  int tmp;
152  in >> tmp;
153  g.type = (tbcs_gate_type)tmp;
154  libff::consume_newline(in);
155  in >> g.output;
156  libff::input_bool(in, g.is_circuit_output);
157 
158  return in;
159 }

Member Data Documentation

◆ is_circuit_output

bool libsnark::tbcs_gate::is_circuit_output

Definition at line 97 of file tbcs.hpp.

◆ left_wire

tbcs_wire_t libsnark::tbcs_gate::left_wire

Definition at line 90 of file tbcs.hpp.

◆ output

tbcs_wire_t libsnark::tbcs_gate::output

Definition at line 95 of file tbcs.hpp.

◆ right_wire

tbcs_wire_t libsnark::tbcs_gate::right_wire

Definition at line 91 of file tbcs.hpp.

◆ type

tbcs_gate_type libsnark::tbcs_gate::type

Definition at line 93 of file tbcs.hpp.


The documentation for this class was generated from the following files:
libsnark::TBCS_GATE_X
@ TBCS_GATE_X
Definition: tbcs.hpp:59
libsnark::tbcs_gate::output
tbcs_wire_t output
Definition: tbcs.hpp:95
libsnark::TBCS_GATE_NOT_X
@ TBCS_GATE_NOT_X
Definition: tbcs.hpp:68
libsnark::tbcs_gate::left_wire
tbcs_wire_t left_wire
Definition: tbcs.hpp:90
libsnark::tbcs_gate::right_wire
tbcs_wire_t right_wire
Definition: tbcs.hpp:91
libsnark::TBCS_GATE_IF_X_THEN_Y
@ TBCS_GATE_IF_X_THEN_Y
Definition: tbcs.hpp:69
libsnark::TBCS_GATE_NOT_X_AND_Y
@ TBCS_GATE_NOT_X_AND_Y
Definition: tbcs.hpp:60
libsnark::TBCS_GATE_X_AND_NOT_Y
@ TBCS_GATE_X_AND_NOT_Y
Definition: tbcs.hpp:58
libsnark::print_tbcs_wire
void print_tbcs_wire(const tbcs_wire_t wire, const std::map< size_t, std::string > &variable_annotations)
Definition: tbcs.cpp:40
libsnark::TBCS_GATE_Y
@ TBCS_GATE_Y
Definition: tbcs.hpp:61
libsnark::TBCS_GATE_NOT_Y
@ TBCS_GATE_NOT_Y
Definition: tbcs.hpp:66
libsnark::tbcs_gate_type
tbcs_gate_type
Definition: tbcs.hpp:55
libsnark::TBCS_GATE_NOR
@ TBCS_GATE_NOR
Definition: tbcs.hpp:64
libsnark::TBCS_GATE_XOR
@ TBCS_GATE_XOR
Definition: tbcs.hpp:62
libsnark::TBCS_GATE_AND
@ TBCS_GATE_AND
Definition: tbcs.hpp:57
libsnark::TBCS_GATE_IF_Y_THEN_X
@ TBCS_GATE_IF_Y_THEN_X
Definition: tbcs.hpp:67
libsnark::TBCS_GATE_OR
@ TBCS_GATE_OR
Definition: tbcs.hpp:63
libsnark::TBCS_GATE_EQUIVALENCE
@ TBCS_GATE_EQUIVALENCE
Definition: tbcs.hpp:65
libsnark::tbcs_gate::type
tbcs_gate_type type
Definition: tbcs.hpp:93
libsnark::TBCS_GATE_CONSTANT_0
@ TBCS_GATE_CONSTANT_0
Definition: tbcs.hpp:56
libsnark::tbcs_gate::is_circuit_output
bool is_circuit_output
Definition: tbcs.hpp:97
libsnark::TBCS_GATE_NAND
@ TBCS_GATE_NAND
Definition: tbcs.hpp:70
libsnark::TBCS_GATE_CONSTANT_1
@ TBCS_GATE_CONSTANT_1
Definition: tbcs.hpp:71