-
Notifications
You must be signed in to change notification settings - Fork 1
/
parsing.cpp
263 lines (253 loc) · 6.91 KB
/
parsing.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include <iostream>
#include <string>
#include <boost/algorithm/string.hpp>
#include <boost/tokenizer.hpp>
#include <assert.h>
#include "parsing.h"
#include "Linear.h"
#include "ops.h"
#include "utils.h"
using namespace std;
map<string, Linear> varset;
struct expr {
string l;
string op;
string r;
};
string clean_expr(string s) {
boost::algorithm::trim(s);
if (s == "" || s[0] != '(' || s.back() != ')') {
return s;
}
int depth = 1;
for (int i = 1; i < s.length()-1; i++) {
if (s[i] == '(') {
depth += 1;
} else if (s[i] == ')') {
depth -= 1;
if (depth == 0)
return s;
}
}
return clean_expr(s.substr(1, s.length()-2));
}
bool split_expr_binary(string& s, vector<string> ops, struct expr& result) {
int depth = 0;
for (int i = s.length(); i > 1; i--) {
if (s[i-1] == ')')
depth += 1;
else if (s[i-1] == '(')
depth -= 1;
else if (depth == 0){
for (const string& op: ops) {
if (i-op.length() >= 1 && s.substr(i-op.length(), op.length()) == op) {
result.l = clean_expr(s.substr(0, i-op.length()));
result.r = clean_expr(s.substr(i));
result.op = op;
return true;
}
}
}
}
return false;
}
Linear parse_expression(string s, struct counts& cnts, vector<Linear>& eqs, vector<struct mul>& mul_data) {
s = clean_expr(s);
bool split;
if (s == "")
throw invalid_argument("Empty expresion");
struct expr sp;
vector<string> delim = {"^"};
split = split_expr_binary(s, delim, sp);
if (split) {
Linear left = parse_expression(sp.l, cnts, eqs, mul_data);
Linear right = parse_expression(sp.r, cnts, eqs, mul_data);
Linear ret = new_xor(left, right, cnts, eqs, mul_data);
// TODO: assert result correct
return ret;
}
delim = {"+", "-"};
split = split_expr_binary(s, delim, sp);
if (split) {
Linear left = parse_expression(sp.l, cnts, eqs, mul_data);
Linear right = parse_expression(sp.r, cnts, eqs, mul_data);
if (sp.op == "+")
left.add(right);
else
left.sub(right);
return left;
}
delim = {"*", "/"};
split = split_expr_binary(s, delim, sp);
if (split) {
Linear left = parse_expression(sp.l, cnts, eqs, mul_data);
Linear right = parse_expression(sp.r, cnts, eqs, mul_data);
if (sp.op == "*") {
Linear ret = new_multiplication(left, right, cnts, eqs, mul_data);
// assert((ret.real%mod) == ((left.real * right.real)%mod));
return ret;
} else {
Linear ret = new_division(left, right, cnts, eqs, mul_data);
// assert(left.real == ((ret.real * right.real)%mod));
return ret;
}
}
if (s.length() > 5 && s.substr(0,5) == "bool(") {
Linear ret = parse_expression(s.substr(4), cnts, eqs, mul_data);
Linear tmp = ret;
Linear tmp2 = ret;
Linear one;
new_const(1, one);
tmp2.sub(one);
new_multiplication(tmp, tmp2, cnts, eqs, mul_data, false);
cnts.bit_count += 1;
return ret;
}
if (s[0] == '-') {
Linear ret = parse_expression(s.substr(1), cnts, eqs, mul_data);
ret.mul(mod-1);
return ret;
}
if (regex_match(s, var_re)) {
if (varset.count(s) != 0) {
return varset[s];
}
else
throw invalid_argument("Variable not defined");
}
if (regex_match(s, secret_re)) {
Linear ret;
mpz_class mpz_val = mpz_class(s.substr(1));
new_temp(mpz_val, ret, cnts);
return ret;
}
if (regex_match(s, num_re)) {
Linear ret;
mpz_class mpz_val = mpz_class(s);
new_const(mpz_val, ret);
return ret;
}
throw invalid_argument("Cannot parse expression");
}
vector<Linear> parse_expressions(string s, struct counts& cnts, vector<Linear>& eqs, vector<struct mul>& mul_data) {
struct expr sp;
vector<string> delim = {","};
bool split = split_expr_binary(s, delim, sp);
if (split) {
vector<Linear> l = parse_expressions(sp.l, cnts, eqs, mul_data);
Linear r = parse_expression(sp.r, cnts, eqs, mul_data);
l.push_back(r);
return l;
}
Linear ret_val = parse_expression(s, cnts, eqs, mul_data);
vector<Linear> ret;
ret.push_back(ret_val);
return ret;
}
typedef boost::tokenizer<boost::char_separator<char>> tokenizer;
void parse_statement(vector<Linear>& eqs, string& s, struct counts& cnts, vector<struct mul>& mul_data) {
//TODO: strip s
if (s.length() > 6 && s.substr(0,6) == "debug "){
Linear lin = parse_expression(s.substr(6), cnts, eqs, mul_data);
cout << "DEBUG " << s.substr(6) << ": " << lin.real << endl;
lin.to_str();
cout << "const => " << lin.constant << endl;
return;
}
vector<string> assn_ops = {":=", "=:", "==", "=", "=?"};
struct expr sp;
bool match = split_expr_binary(s, assn_ops, sp);
if (match) {
string left = sp.l;
string op = sp.op;
string right = sp.r;
if (op == ":=") {
vector<string> bits;
boost::char_separator<char> sep{","};
tokenizer tok{left, sep};
for (const auto &b : tok) {
bits.push_back(b);
}
//TODO: Check bit names are valid
Linear val = parse_expression(right, cnts, eqs, mul_data);
//TODO: assert bits length <= 256
assert(bits.size() <= 256);
vector<Linear> bitvars;
if (val.is_const()) {
for (int i = 0; i < bits.size(); i++) {
Linear l;
mpz_class v = (val.real >> i) & 1;
new_const(v,l);
bitvars.push_back(l);
}
} else {
for (int i = 0; i < bits.size(); i++) {
Linear l;
mpz_class v = (val.real >> i) & 1;
new_temp(v, l, cnts);
bitvars.push_back(l);
Linear tmp = l;
Linear tmp2 = l;
Linear one;
new_const(1, one);
tmp2.sub(one);
Linear eq = new_multiplication(tmp, tmp2, cnts, eqs, mul_data);
eqs.push_back(eq);
tmp = l;
mpz_class shifted_i = shift_left(1, i);
tmp.mul(shifted_i);
val.sub(tmp);
}
eqs.push_back(val);
}
for (int i = 0; i < bits.size(); i++) {
varset[bits[i]] = bitvars[i];
}
} else if (op == "=:") {
vector<Linear> bits;
bits = parse_expressions(right, cnts, eqs, mul_data);
// TODO: Check varible name on left valid, len of bits <=256
assert(bits.size() <= 256);
mpz_class real = 0;
for (int i = 0; i < bits.size(); i++) {
//TODO: Assert bit either 0 or 1
real = real + bits[i].real * (1 << i);
}
if (all_const(bits)) {
Linear val;
new_const(real, val);
varset[left] = val;
} else {
Linear val;
new_temp(real, val, cnts);
varset[left] = val;
for (int i = 0; i < bits.size(); i++) {
Linear tmp = bits[i];
mpz_class shifted_i = shift_left(1, i);
tmp.mul(shifted_i);
val.sub(tmp);
}
eqs.push_back(val);
}
} else if (op == "=") {
//TODO: check left is proper variable name
Linear ex = parse_expression(right, cnts, eqs, mul_data);
varset[left] = ex;
} else if (op == "==") {
Linear l = parse_expression(left, cnts, eqs, mul_data);
Linear r = parse_expression(right, cnts, eqs, mul_data);
l.sub(r);
cout << "checking equality: " << endl;
//TODO: check l.val == r.val
eqs.push_back(l);
l.to_str();
} else if (op == "=?") {
Linear ex = parse_expression(right, cnts, eqs, mul_data);
int is_zero = ex.is_zero();
Linear lin;
lin.real = is_zero;
lin.constant = is_zero;
varset[left] = lin;
}
}
}