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