-
Notifications
You must be signed in to change notification settings - Fork 12
/
Alg_uint.hpp
266 lines (215 loc) · 7.33 KB
/
Alg_uint.hpp
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
264
265
266
#ifndef _SNARKFRONT_ALG_UINT_HPP_
#define _SNARKFRONT_ALG_UINT_HPP_
#include <algorithm>
#include <cassert>
#include <snarkfront/Alg.hpp>
#include <snarkfront/Alg_internal.hpp>
namespace snarkfront {
////////////////////////////////////////////////////////////////////////////////
// Alg_uint8
// Alg_uint32
// Alg_uint64
//
template <typename ALG>
void evalStackOp_Bitwise(std::stack<ALG>& S, const BitwiseOps op)
{
typedef typename ALG::ValueType Value;
typedef typename ALG::FrType Fr;
typedef typename ALG::R1T R1T;
auto& RS = TL<R1C<Fr>>::singleton();
typedef cryptl::BitwiseINT<Value> BitOps;
// y is right argument
const auto R = S.top();
S.pop();
const Value yvalue = R.value();
const Fr ywitness = R.witness();
// modulo addition
if (BitwiseOps::ADDMOD == op) {
// x is left argument
const auto L = S.top();
S.pop();
const Value xvalue = L.value();
const Fr xwitness = L.witness();
// left argument
R1T x;
Fr x_witness;
Value xhigh, xlow;
std::size_t xhighCnt;
if (L.splitBits().size() < 2 * sizeBits(xvalue)) {
x_witness = xwitness;
x = RS->argScalar(L);
const std::vector<int> xrem = bitsValue(xlow, L.splitBits());
bitsValue(xhigh, xrem);
xhighCnt = xrem.size();
#ifdef USE_ASSERT
assert(xlow == xvalue);
assert(xhighCnt < sizeBits(xvalue));
#endif
} else {
x_witness = ALG::valueToString(xvalue);
const std::vector<R1T> xbits = RS->argBits(L);
const std::vector<R1T> xfit = rank1_xword(xbits, sizeBits(xvalue));
x = RS->bitsToWitness(xfit, x_witness);
xlow = xvalue;
xhigh = 0;
xhighCnt = 0;
}
// right argument
R1T y;
Fr y_witness;
Value yhigh, ylow;
std::size_t yhighCnt;
if (R.splitBits().size() < 2 * sizeBits(yvalue)) {
y_witness = ywitness;
y = RS->argScalar(R);
const std::vector<int> yrem = bitsValue(ylow, R.splitBits());
bitsValue(yhigh, yrem);
yhighCnt = yrem.size();
#ifdef USE_ASSERT
assert(ylow == yvalue);
assert(yhighCnt < sizeBits(yvalue));
#endif
} else {
y_witness = ALG::valueToString(yvalue);
const std::vector<R1T> ybits = RS->argBits(R);
const std::vector<R1T> yfit = rank1_xword(ybits, sizeBits(yvalue));
y = RS->bitsToWitness(yfit, y_witness);
ylow = yvalue;
yhigh = 0;
yhighCnt = 0;
}
// overflow addition
Value high = xhigh + yhigh, low = xlow;
addover(high, low, ylow);
const Value zvalue = BitOps::ADDMOD(xvalue, yvalue);
#ifdef USE_ASSERT
assert(zvalue == low);
#endif
std::vector<int> zbits = valueBits(low);
for (std::size_t i = 0; i < xhighCnt + yhighCnt + 1; ++i) {
zbits.push_back(high & 0x1);
high >>= 1;
}
#ifdef USE_ASSERT
assert(0 == high);
#endif
const Fr zwitness = x_witness + y_witness;
const R1T z = RS->createResult(op, x, y, zwitness);
S.push(
ALG(zvalue, zwitness, zbits, {z}));
} else if (BitwiseOps::MULMOD == op) {
// x is left argument
const auto L = S.top();
S.pop();
const Value xvalue = L.value();
const Fr x_witness = ALG::valueToString(xvalue);
const std::vector<R1T> xbits = RS->argBits(L);
const std::vector<R1T> xfit = rank1_xword(xbits, sizeBits(xvalue));
const R1T x = RS->bitsToWitness(xfit, x_witness);
// right argument
const Fr y_witness = ALG::valueToString(yvalue);
const std::vector<R1T> ybits = RS->argBits(R);
const std::vector<R1T> yfit = rank1_xword(ybits, sizeBits(yvalue));
const R1T y = RS->bitsToWitness(yfit, y_witness);
// overflow multiplication
Value high, low;
mulover(high, low, xvalue, yvalue);
const Value zvalue = BitOps::MULMOD(xvalue, yvalue);
#ifdef USE_ASSERT
assert(zvalue == low);
#endif
std::vector<int> zbits = valueBits(low);
for (std::size_t i = 0; i < sizeBits(high); ++i) {
zbits.push_back(high & 0x1);
high >>= 1;
}
#ifdef USE_ASSERT
assert(0 == high);
#endif
const Fr zwitness = x_witness * y_witness;
const R1T z = RS->createResult(op, x, y, zwitness);
S.push(
ALG(zvalue, zwitness, zbits, {z}));
} else if (BitwiseOps::CMPLMNT == op) {
// y is only argument
const std::vector<R1T> y = RS->argBits(R);
#ifdef USE_ASSERT
assert(y.size() >= sizeBits(yvalue));
assert(y.size() == R.splitBits().size());
#endif
// z is result
const Value zvalue = BitOps::CMPLMNT(yvalue);
std::vector<int> zbits;
std::vector<R1T> z;
zbits.reserve(sizeBits(zvalue));
z.reserve(sizeBits(zvalue));
for (std::size_t i = 0; i < sizeBits(zvalue); ++i) {
const bool b = ! R.splitBits()[i];
zbits.push_back(b);
z.emplace_back(
RS->createResult(op, y[i], y[i], boolTo<Fr>(b)));
}
#ifdef USE_ASSERT
assert(zbits == valueBits(zvalue));
#endif
S.push(
ALG(zvalue, ALG::valueToString(zvalue), zbits, z));
} else {
// x is left argument
const auto L = S.top();
S.pop();
const Value xvalue = L.value();
const Fr xwitness = L.witness();
const std::vector<R1T> x = RS->argBits(L);
#ifdef USE_ASSERT
assert(x.size() >= sizeBits(xvalue));
#endif
// z is result
const Value zvalue = evalOp(op, xvalue, yvalue);
std::vector<R1T> z;
z.reserve(sizeBits(zvalue));
if (isPermute(op)) {
const std::vector<R1T> xfit = rank1_xword(x, sizeBits(xvalue));
z = RS->permuteBits(op, xfit, yvalue);
} else {
const std::vector<R1T> y = RS->argBits(R);
#ifdef USE_ASSERT
assert(y.size() >= sizeBits(yvalue));
#endif
Value mask = 0x1;
for (std::size_t i = 0; i < sizeBits(zvalue); ++i) {
z.emplace_back(
RS->createResult(op, x[i], y[i], boolTo<Fr>(zvalue & mask)));
mask <<= 1;
}
}
S.push(
ALG(zvalue, ALG::valueToString(zvalue), valueBits(zvalue), z));
}
}
template <typename FR>
void evalStackOp(std::stack<Alg_uint8<FR>>& S, const BitwiseOps op) {
evalStackOp_Bitwise<Alg_uint8<FR>>(S, op);
}
template <typename FR>
void evalStackOp(std::stack<Alg_uint32<FR>>& S, const BitwiseOps op) {
evalStackOp_Bitwise<Alg_uint32<FR>>(S, op);
}
template <typename FR>
void evalStackOp(std::stack<Alg_uint64<FR>>& S, const BitwiseOps op) {
evalStackOp_Bitwise<Alg_uint64<FR>>(S, op);
}
template <typename FR>
void evalStackCmp(std::stack<Alg_uint8<FR>>& S, const EqualityCmp op) {
evalStackCmp_Equality(S, op);
}
template <typename FR>
void evalStackCmp(std::stack<Alg_uint32<FR>>& S, const EqualityCmp op) {
evalStackCmp_Equality(S, op);
}
template <typename FR>
void evalStackCmp(std::stack<Alg_uint64<FR>>& S, const EqualityCmp op) {
evalStackCmp_Equality(S, op);
}
} // namespace snarkfront
#endif