forked from Consensys/gnark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uint8.go
342 lines (298 loc) · 8.88 KB
/
uint8.go
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Package uints implements optimised byte and long integer operations.
//
// Usually arithmetic in a circuit is performed in the native field, which is of
// prime order. However, for compatibility with native operations we rely on
// operating on smaller primitive types as 8-bit, 32-bit and 64-bit integer.
// Naively, these operations have to be implemented bitwise as there are no
// closed equations for boolean operations (XOR, AND, OR).
//
// However, the bitwise approach is very inefficient and leads to several
// constraints per bit. Accumulating over a long integer, it leads to very
// inefficients circuits.
//
// This package performs boolean operations using lookup tables on bytes. So,
// long integers are split into 4 or 8 bytes and we perform the operations
// bytewise. In the lookup tables, we store results for all possible 2^8×2^8
// inputs. With this approach, every bytewise operation costs as single lookup,
// which depending on the backend is relatively cheap (one to three
// constraints).
//
// NB! The package is still work in progress. The interfaces and implementation
// details most certainly changes over time. We cannot ensure the soundness of
// the operations.
package uints
import (
"fmt"
"github.com/aakash4dev/gnark2/frontend"
"github.com/aakash4dev/gnark2/std/internal/logderivprecomp"
"github.com/aakash4dev/gnark2/std/math/bitslice"
"github.com/aakash4dev/gnark2/std/rangecheck"
)
// TODO: if internal then enforce range check!
// TODO: all operations can take rand linear combinations instead. Then instead
// of one check can perform multiple at the same time.
// TODO: implement versions which take multiple inputs. Maybe can combine multiple together
// TODO: instantiate tables only when we first query. Maybe do not need to build!
// TODO: maybe can store everything in a single table? Later! Or if we have a
// lot of queries then makes sense to extract into separate table?
// TODO: in ValueOf ensure consistency
// TODO: distinguish between when we set constant in-circuit or witness
// assignment. For constant we don't have to range check but for witness
// assignment we have to.
// TODO: add something which allows to store array in native element
// TODO: add methods for checking if U8/Long is constant.
// TODO: should something for byte-only ops. Implement a type and then embed it in BinaryField
// TODO: add helper method to call hints which allows to pass in uint8s (bytes)
// and returns bytes. Then can to byte array manipluation nicely. It is useful
// for X509. For the implementation we want to pack as much bytes into a field
// element as possible.
// TODO: methods for converting uint array into emulated element and native
// element. Most probably should add the implementation for non-native in its
// package, but for native we should add it here.
type U8 struct {
Val frontend.Variable
internal bool
}
// GnarkInitHook describes how to initialise the element.
func (e *U8) GnarkInitHook() {
if e.Val == nil {
e.Val = 0
e.internal = false // we need to constrain in later.
}
}
type U64 [8]U8
type U32 [4]U8
type Long interface{ U32 | U64 }
type BinaryField[T U32 | U64] struct {
api frontend.API
xorT, andT *logderivprecomp.Precomputed
rchecker frontend.Rangechecker
allOne U8
}
func New[T Long](api frontend.API) (*BinaryField[T], error) {
xorT, err := logderivprecomp.New(api, xorHint, []uint{8})
if err != nil {
return nil, fmt.Errorf("new xor table: %w", err)
}
andT, err := logderivprecomp.New(api, andHint, []uint{8})
if err != nil {
return nil, fmt.Errorf("new and table: %w", err)
}
rchecker := rangecheck.New(api)
bf := &BinaryField[T]{
api: api,
xorT: xorT,
andT: andT,
rchecker: rchecker,
}
// TODO: this is const. add way to init constants
allOne := bf.ByteValueOf(0xff)
bf.allOne = allOne
return bf, nil
}
func NewU8(v uint8) U8 {
// TODO: don't have to check constants
return U8{Val: v, internal: true}
}
func NewU32(v uint32) U32 {
return [4]U8{
NewU8(uint8((v >> (0 * 8)) & 0xff)),
NewU8(uint8((v >> (1 * 8)) & 0xff)),
NewU8(uint8((v >> (2 * 8)) & 0xff)),
NewU8(uint8((v >> (3 * 8)) & 0xff)),
}
}
func NewU64(v uint64) U64 {
return [8]U8{
NewU8(uint8((v >> (0 * 8)) & 0xff)),
NewU8(uint8((v >> (1 * 8)) & 0xff)),
NewU8(uint8((v >> (2 * 8)) & 0xff)),
NewU8(uint8((v >> (3 * 8)) & 0xff)),
NewU8(uint8((v >> (4 * 8)) & 0xff)),
NewU8(uint8((v >> (5 * 8)) & 0xff)),
NewU8(uint8((v >> (6 * 8)) & 0xff)),
NewU8(uint8((v >> (7 * 8)) & 0xff)),
}
}
func NewU8Array(v []uint8) []U8 {
ret := make([]U8, len(v))
for i := range v {
ret[i] = NewU8(v[i])
}
return ret
}
func NewU32Array(v []uint32) []U32 {
ret := make([]U32, len(v))
for i := range v {
ret[i] = NewU32(v[i])
}
return ret
}
func NewU64Array(v []uint64) []U64 {
ret := make([]U64, len(v))
for i := range v {
ret[i] = NewU64(v[i])
}
return ret
}
func (bf *BinaryField[T]) ByteValueOf(a frontend.Variable) U8 {
bf.rchecker.Check(a, 8)
return U8{Val: a, internal: true}
}
func (bf *BinaryField[T]) ValueOf(a frontend.Variable) T {
var r T
bts, err := bf.api.Compiler().NewHint(toBytes, len(r), len(r), a)
if err != nil {
panic(err)
}
// TODO: add constraint which ensures that map back to
for i := range bts {
r[i] = bf.ByteValueOf(bts[i])
}
return r
}
func (bf *BinaryField[T]) ToValue(a T) frontend.Variable {
v := make([]frontend.Variable, len(a))
for i := range v {
v[i] = bf.api.Mul(a[i].Val, 1<<(i*8))
}
vv := bf.api.Add(v[0], v[1], v[2:]...)
return vv
}
func (bf *BinaryField[T]) PackMSB(a ...U8) T {
var ret T
for i := range a {
ret[len(a)-i-1] = a[i]
}
return ret
}
func (bf *BinaryField[T]) PackLSB(a ...U8) T {
var ret T
for i := range a {
ret[i] = a[i]
}
return ret
}
func (bf *BinaryField[T]) UnpackMSB(a T) []U8 {
ret := make([]U8, len(a))
for i := 0; i < len(a); i++ {
ret[len(a)-i-1] = a[i]
}
return ret
}
func (bf *BinaryField[T]) UnpackLSB(a T) []U8 {
// cannot deduce that a can be cast to []U8
ret := make([]U8, len(a))
for i := 0; i < len(a); i++ {
ret[i] = a[i]
}
return ret
}
func (bf *BinaryField[T]) twoArgFn(tbl *logderivprecomp.Precomputed, a ...U8) U8 {
ret := tbl.Query(a[0].Val, a[1].Val)[0]
for i := 2; i < len(a); i++ {
ret = tbl.Query(ret, a[i].Val)[0]
}
return U8{Val: ret}
}
func (bf *BinaryField[T]) twoArgWideFn(tbl *logderivprecomp.Precomputed, a ...T) T {
var r T
for i, v := range reslice(a) {
r[i] = bf.twoArgFn(tbl, v...)
}
return r
}
func (bf *BinaryField[T]) And(a ...T) T { return bf.twoArgWideFn(bf.andT, a...) }
func (bf *BinaryField[T]) Xor(a ...T) T { return bf.twoArgWideFn(bf.xorT, a...) }
func (bf *BinaryField[T]) not(a U8) U8 {
ret := bf.xorT.Query(a.Val, bf.allOne.Val)
return U8{Val: ret[0]}
}
func (bf *BinaryField[T]) Not(a T) T {
var r T
for i := 0; i < len(a); i++ {
r[i] = bf.not(a[i])
}
return r
}
func (bf *BinaryField[T]) Add(a ...T) T {
va := make([]frontend.Variable, len(a))
for i := range a {
va[i] = bf.ToValue(a[i])
}
vres := bf.api.Add(va[0], va[1], va[2:]...)
res := bf.ValueOf(vres)
// TODO: should also check the that carry we omitted is correct.
return res
}
func (bf *BinaryField[T]) Lrot(a T, c int) T {
l := len(a)
if c < 0 {
c = l*8 + c
}
shiftBl := c / 8
shiftBt := c % 8
revShiftBt := 8 - shiftBt
if revShiftBt == 8 {
revShiftBt = 0
}
partitioned := make([][2]frontend.Variable, l)
for i := range partitioned {
lower, upper := bitslice.Partition(bf.api, a[i].Val, uint(revShiftBt), bitslice.WithNbDigits(8))
partitioned[i] = [2]frontend.Variable{lower, upper}
}
var ret T
for i := 0; i < l; i++ {
if shiftBt != 0 {
ret[(i+shiftBl)%l].Val = bf.api.Add(bf.api.Mul(1<<(shiftBt), partitioned[i][0]), partitioned[(i+l-1)%l][1])
} else {
ret[(i+shiftBl)%l].Val = partitioned[i][1]
}
}
return ret
}
func (bf *BinaryField[T]) Rshift(a T, c int) T {
shiftBl := c / 8
shiftBt := c % 8
partitioned := make([][2]frontend.Variable, len(a)-shiftBl)
for i := range partitioned {
lower, upper := bitslice.Partition(bf.api, a[i+shiftBl].Val, uint(shiftBt), bitslice.WithNbDigits(8))
partitioned[i] = [2]frontend.Variable{lower, upper}
}
var ret T
for i := 0; i < len(a)-shiftBl-1; i++ {
if shiftBt != 0 {
ret[i].Val = bf.api.Add(partitioned[i][1], bf.api.Mul(1<<(8-shiftBt), partitioned[i+1][0]))
} else {
ret[i].Val = partitioned[i][1]
}
}
ret[len(a)-shiftBl-1].Val = partitioned[len(a)-shiftBl-1][1]
for i := len(a) - shiftBl; i < len(ret); i++ {
ret[i] = NewU8(0)
}
return ret
}
func (bf *BinaryField[T]) ByteAssertEq(a, b U8) {
bf.api.AssertIsEqual(a.Val, b.Val)
}
func (bf *BinaryField[T]) AssertEq(a, b T) {
for i := 0; i < len(a); i++ {
bf.ByteAssertEq(a[i], b[i])
}
}
func reslice[T U32 | U64](in []T) [][]U8 {
if len(in) == 0 {
panic("zero-length input")
}
ret := make([][]U8, len(in[0]))
for i := range ret {
ret[i] = make([]U8, len(in))
}
for i := 0; i < len(in); i++ {
for j := 0; j < len(in[0]); j++ {
ret[j][i] = in[i][j]
}
}
return ret
}