-
Notifications
You must be signed in to change notification settings - Fork 1
/
ops.cpp
93 lines (86 loc) · 2.12 KB
/
ops.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
#include <assert.h>
#include "ops.h"
#include "utils.h"
void new_mul(mpz_class l, mpz_class r, Linear& nl, Linear& nr, Linear& no, struct counts& cnts, vector<struct mul>& mul_data) {
mpz_class o = (l*r) % mod;
struct mul m = {l, r, o};
mul_data.push_back(m);
nl.real = l % mod;
nl.constant = 0;
nl.add_var('L', cnts.mul_count, 1);
nr.real = r % mod;
nr.constant = 0;
nr.add_var('R', cnts.mul_count, 1);
no.real = o;
no.constant = 0;
no.add_var('O', cnts.mul_count, 1);
cnts.mul_count += 1;
}
void new_temp(mpz_class v, Linear& nt, struct counts& cnts) {
nt.real = v % mod;
nt.constant = 0;
nt.add_var('T', cnts.temp_count, 1);
cnts.temp_count += 1;
}
void new_const(mpz_class v, Linear& nc) {
nc.real = v % mod;
nc.constant = v % mod;
}
// mutates l and/or r
Linear new_multiplication(Linear& l, Linear& r, struct counts& cnts, vector<Linear>& eqs, vector<struct mul>& mul_data, bool addeqs) {
if (l.is_const()) {
r.mul(l.constant);
return r;
}
if (r.is_const()) {
l.mul(r.constant);
return l;
}
if (r.constant < l.constant) {
Linear tmp = l;
l = r;
r = tmp;
}
Linear lv = Linear();
Linear rv = Linear();
Linear ret = Linear();
new_mul(l.real, r.real, lv, rv, ret, cnts, mul_data);
assert(l.real == lv.real);
assert(r.real == rv.real);
l.sub(lv);
eqs.push_back(l);
if (addeqs){
r.sub(rv);
eqs.push_back(r);
}
return ret;
}
// mutates l and/or r
Linear new_division(Linear& l, Linear& r, struct counts& cnts, vector<Linear>& eqs, vector<struct mul>& mul_data) {
if (r.is_const()) {
l.div(r.constant);
return l;
}
Linear lv = Linear();
Linear rv = Linear();
Linear ret = Linear();
new_mul((l.real * modinv(r.real, mod)) % mod, r.real, ret, rv, lv, cnts, mul_data);
assert(l.real == lv.real);
assert(r.real == rv.real);
l.sub(lv);
r.sub(rv);
eqs.push_back(l);
eqs.push_back(r);
return ret;
}
// mutates l and/or r
Linear new_xor(Linear& l, Linear& r, struct counts& cnts, vector<Linear>& eqs, vector<struct mul>& mul_data) {
Linear lv = Linear();
Linear rv = Linear();
Linear mul = Linear();
new_multiplication(l, r, cnts, eqs, mul_data);
l.add(r);
mul.mul(2);
l.sub(mul);
return l;
}