Clearmatics Libsnark  0.1
C++ library for zkSNARK proofs
protoboard.tcc
Go to the documentation of this file.
1 /** @file
2  *****************************************************************************
3  * @author This file is part of libsnark, developed by SCIPR Lab
4  * and contributors (see AUTHORS).
5  * @copyright MIT license (see LICENSE file)
6  *****************************************************************************/
7 
8 #ifndef PROTOBOARD_TCC_
9 #define PROTOBOARD_TCC_
10 
11 #include <cstdarg>
12 #include <cstdio>
13 #include <libff/common/profiling.hpp>
14 
15 namespace libsnark
16 {
17 
18 template<typename FieldT> protoboard<FieldT>::protoboard()
19 {
20  constant_term = FieldT::one();
21 
22 #ifdef DEBUG
23  constraint_system.variable_annotations[0] = "ONE";
24 #endif
25 
26  next_free_var = 1; /* to account for constant 1 term */
27  next_free_lc = 0;
28 }
29 
30 template<typename FieldT> void protoboard<FieldT>::clear_values()
31 {
32  std::fill(values.begin(), values.end(), FieldT::zero());
33 }
34 
35 template<typename FieldT>
36 var_index_t protoboard<FieldT>::allocate_var_index(
37  const std::string &annotation)
38 {
39 #ifdef DEBUG
40  assert(annotation != "");
41  constraint_system.variable_annotations[next_free_var] = annotation;
42 #else
43  libff::UNUSED(annotation);
44 #endif
45  ++constraint_system.auxiliary_input_size;
46  values.emplace_back(FieldT::zero());
47  return next_free_var++;
48 }
49 
50 template<typename FieldT> lc_index_t protoboard<FieldT>::allocate_lc_index()
51 {
52  lc_values.emplace_back(FieldT::zero());
53  return next_free_lc++;
54 }
55 
56 template<typename FieldT>
57 FieldT &protoboard<FieldT>::val(const pb_variable<FieldT> &var)
58 {
59  assert(var.index <= values.size());
60  return (var.index == 0 ? constant_term : values[var.index - 1]);
61 }
62 
63 template<typename FieldT>
64 FieldT protoboard<FieldT>::val(const pb_variable<FieldT> &var) const
65 {
66  assert(var.index <= values.size());
67  return (var.index == 0 ? constant_term : values[var.index - 1]);
68 }
69 
70 template<typename FieldT>
71 FieldT &protoboard<FieldT>::lc_val(const pb_linear_combination<FieldT> &lc)
72 {
73  if (lc.is_variable) {
74  return this->val(pb_variable<FieldT>(lc.index));
75  } else {
76  assert(lc.index < lc_values.size());
77  return lc_values[lc.index];
78  }
79 }
80 
81 template<typename FieldT>
82 FieldT protoboard<FieldT>::lc_val(const pb_linear_combination<FieldT> &lc) const
83 {
84  if (lc.is_variable) {
85  return this->val(pb_variable<FieldT>(lc.index));
86  } else {
87  assert(lc.index < lc_values.size());
88  return lc_values[lc.index];
89  }
90 }
91 
92 template<typename FieldT>
93 void protoboard<FieldT>::add_r1cs_constraint(
94  const r1cs_constraint<FieldT> &constr, const std::string &annotation)
95 {
96 #ifdef DEBUG
97  assert(annotation != "");
98  constraint_system
99  .constraint_annotations[constraint_system.constraints.size()] =
100  annotation;
101 #else
102  libff::UNUSED(annotation);
103 #endif
104  constraint_system.constraints.emplace_back(constr);
105 }
106 
107 template<typename FieldT>
108 void protoboard<FieldT>::augment_variable_annotation(
109  const pb_variable<FieldT> &v, const std::string &postfix)
110 {
111 #ifdef DEBUG
112  auto it = constraint_system.variable_annotations.find(v.index);
113  constraint_system.variable_annotations[v.index] =
114  (it == constraint_system.variable_annotations.end()
115  ? ""
116  : it->second + " ") +
117  postfix;
118 #else
119  libff::UNUSED(v);
120  libff::UNUSED(postfix);
121 #endif
122 }
123 
124 template<typename FieldT> bool protoboard<FieldT>::is_satisfied() const
125 {
126  return constraint_system.is_satisfied(primary_input(), auxiliary_input());
127 }
128 
129 template<typename FieldT> void protoboard<FieldT>::dump_variables() const
130 {
131 #ifdef DEBUG
132  for (size_t i = 0; i < constraint_system.num_variables(); ++i) {
133  printf("%-40s --> ", constraint_system.variable_annotations[i].c_str());
134  values[i].as_bigint().print_hex();
135  }
136 #endif
137 }
138 
139 template<typename FieldT> size_t protoboard<FieldT>::num_constraints() const
140 {
141  return constraint_system.num_constraints();
142 }
143 
144 template<typename FieldT> size_t protoboard<FieldT>::num_inputs() const
145 {
146  return constraint_system.num_inputs();
147 }
148 
149 template<typename FieldT> size_t protoboard<FieldT>::num_variables() const
150 {
151  return next_free_var - 1;
152 }
153 
154 template<typename FieldT>
155 void protoboard<FieldT>::set_input_sizes(const size_t primary_input_size)
156 {
157  assert(primary_input_size <= num_variables());
158  constraint_system.primary_input_size = primary_input_size;
159  constraint_system.auxiliary_input_size =
160  num_variables() - primary_input_size;
161 }
162 
163 template<typename FieldT>
164 const r1cs_variable_assignment<FieldT>
165  &protoboard<FieldT>::full_variable_assignment() const
166 {
167  return values;
168 }
169 
170 template<typename FieldT>
171 r1cs_primary_input<FieldT> protoboard<FieldT>::primary_input() const
172 {
173  return r1cs_primary_input<FieldT>(
174  values.begin(), values.begin() + num_inputs());
175 }
176 
177 template<typename FieldT>
178 r1cs_auxiliary_input<FieldT> protoboard<FieldT>::auxiliary_input() const
179 {
180  return r1cs_auxiliary_input<FieldT>(
181  values.begin() + num_inputs(), values.end());
182 }
183 
184 template<typename FieldT>
185 const r1cs_constraint_system<FieldT>
186  &protoboard<FieldT>::get_constraint_system() const
187 {
188  return constraint_system;
189 }
190 
191 } // namespace libsnark
192 #endif // PROTOBOARD_TCC_