-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprotoboard_pub.i
78 lines (65 loc) · 2.22 KB
/
protoboard_pub.i
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
%inline %{
class ProtoboardPub: public libsnark::protoboard<Ft> {
vector<var_index_t> pubixs;
public:
void setpublic(const libsnark::pb_variable<Ft> &var) {
if (pubixs.size()>0 && pubixs.back()>=var.index) {
cerr << "*** setpublic: pb_variables should be marked public in order, ignoring" << endl;
} else {
pubixs.push_back(var.index);
}
}
libsnark::r1cs_constraint_system<Ft> get_constraint_system_pubs() {
r1cs_constraint_system<Ft> pbcs = get_constraint_system();
r1cs_constraint_system<Ft> cs;
// build translation table
int ntot = num_variables();
vector<int> table(ntot+1);
int cur = 1, curpub = 1, curshift = pubixs.size();
table[0] = 0;
for (auto const& ix: pubixs) {
while (cur<ix) {
table[cur] = cur+curshift;
cur++;
}
table[cur++] = curpub++;
curshift--;
}
while (cur <= ntot) {
table[cur] = cur;
cur++;
}
// reorganize constraint system
for (auto csi: pbcs.constraints) {
for (auto &ai: csi.a.terms) ai.index = table[ai.index];
for (auto &bi: csi.b.terms) bi.index = table[bi.index];
for (auto &ci: csi.c.terms) ci.index = table[ci.index];
cs.add_constraint(csi);
}
cs.primary_input_size = pubixs.size();
cs.auxiliary_input_size = num_variables() - pubixs.size();
return cs;
}
libsnark::r1cs_primary_input<Ft> primary_input_pubs() {
r1cs_primary_input<Ft> ret;
for (auto const& ix: pubixs) {
ret.push_back(full_variable_assignment()[ix-1]);
}
return ret;
}
libsnark::r1cs_auxiliary_input<Ft> auxiliary_input_pubs() {
r1cs_auxiliary_input<Ft> ret;
int ix = 1;
vector<var_index_t>::iterator it = pubixs.begin();
for (auto const& val: full_variable_assignment()) {
if (it != pubixs.end() && *it==ix) {
it++;
} else {
ret.push_back(val);
}
ix++;
}
return ret;
}
};
%}