-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.cpp
81 lines (72 loc) · 2.08 KB
/
common.cpp
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
#include <iostream>
#include "common.h"
using namespace libsnark;
using ethsnarks::FieldT;
using ethsnarks::ppT;
using ethsnarks::ProtoboardT;
void constraint_to_json(linear_combination<FieldT>& constraints, std::stringstream &ss)
{
ss << "{";
uint count = 0;
for (const auto& lt : constraints.terms)
{
if (lt.coeff == 0) {
continue;
}
if (count != 0) {
ss << ",";
}
ss << '"' << lt.index << '"' << ":" << '"' << lt.coeff.as_bigint() << '"';
count++;
}
ss << "}";
}
std::string r1cs_to_json(ProtoboardT& pb)
{
auto constraints = pb.get_constraint_system();
std::stringstream ss;
for (size_t c = 0; c < constraints.num_constraints(); ++c)
{
ss << "[";// << "\"A\"=";
constraint_to_json(constraints.constraints[c].a, ss);
ss << ",";// << "\"B\"=";
constraint_to_json(constraints.constraints[c].b, ss);
ss << ",";// << "\"C\"=";;
constraint_to_json(constraints.constraints[c].c, ss);
ss << "]\n";
}
ss.rdbuf()->pubseekpos(0, std::ios_base::out);
return ss.str();
}
std::string witness_to_json(ProtoboardT& pb) {
auto values = pb.full_variable_assignment();
std::stringstream ss;
for (size_t i = 0; i < values.size(); ++i) {
ss << values[i].as_bigint() << " ";
}
return ss.str();
}
int main(int argc, char **argv)
{
ppT::init_public_params();
if (argc != 2) {
std::cout << "Needs to be called with a command.\n";
return 1;
}
if (strcmp("size", argv[1]) == 0) {
auto pb = getProtoboard(nullptr);
std::cout << (pb.num_variables() - inputSize() - outputSize()) << "\n";
} else if (strcmp("constraints", argv[1]) == 0) {
auto pb = getProtoboard(nullptr);
std::cout << r1cs_to_json(pb);
} else if (strcmp("witness", argv[1]) == 0) {
std::string input_line;
std::getline(std::cin, input_line);
auto pb = getProtoboard(input_line.c_str());
std::cout << witness_to_json(pb);
} else {
std::cout << "Unknown command `" << argv[1] << "`! Expecting size, constraints or witness.\n";
return 1;
}
return 0;
}