-
Notifications
You must be signed in to change notification settings - Fork 0
/
embed.go
677 lines (601 loc) · 19.3 KB
/
embed.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
package easyfl
import (
"bytes"
"crypto/ed25519"
"encoding/binary"
"encoding/hex"
"fmt"
"reflect"
"golang.org/x/crypto/blake2b"
)
// list of standard embedded functions
// TODO
// - certain function could be optimized.
// - do we need short end long embedding?
var (
embedShortBase = []*EmbeddedFunctionData{
{"fail", 1, evalFail},
{"slice", 3, evalSlice},
{"byte", 2, evalByte},
{"tail", 2, evalTail},
{"equal", 2, evalEqual},
{"hasPrefix", 2, evalHasPrefix},
{"len", 1, evalLen},
{"not", 1, evalNot},
{"if", 3, evalIf},
{"isZero", 1, evalIsZero},
}
embedLongBase = []*EmbeddedFunctionData{
{"concat", -1, evalConcat},
{"and", -1, evalAnd},
{"or", -1, evalOr},
{"repeat", 2, evalRepeat},
{"firstCaseIndex", -1, evalFirstCaseIndex},
{"firstEqualIndex", -1, evalFirstEqualIndex},
{"selectCaseByIndex", -1, evalSelectCaseByIndex},
}
embedArithmeticsShort = []*EmbeddedFunctionData{
{"add", 2, evalAddUint},
{"sub", 2, evalSubUint},
{"mul", 2, evalMulUint},
{"div", 2, evalDivUint},
{"mod", 2, evalModuloUint},
{"uint64Bytes", 1, evalUint64Bytes},
}
embedBitwiseAndCmpShort = []*EmbeddedFunctionData{
{"lessThan", 2, evalLessThan},
{"bitwiseOR", 2, evalBitwiseOR},
{"bitwiseAND", 2, evalBitwiseAND},
{"bitwiseNOT", 1, evalBitwiseNOT},
{"bitwiseXOR", 2, evalBitwiseXOR},
}
embedBitwiseAndCmpLong = []*EmbeddedFunctionData{
{"lshift64", 2, evalLShift64},
{"rshift64", 2, evalRShift64},
}
embedBaseCrypto = []*EmbeddedFunctionData{
{"validSignatureED25519", 3, evalValidSigED25519},
{"blake2b", -1, evalBlake2b},
}
embedBytecodeManipulation = func(lib *Library) []*EmbeddedFunctionData {
return []*EmbeddedFunctionData{
{"parseArgumentBytecode", 3, lib.evalParseArgumentBytecode},
{"parsePrefixBytecode", 1, lib.evalParsePrefixBytecode},
{"eval", 1, lib.evalBytecode}, // evaluates closed formula
}
}
)
// embedding functions with inline tests
func (lib *Library) embedMain() {
lib.UpgradeWithEmbeddedShort(embedShortBase...)
lib.UpgradeWthEmbeddedLong(embedLongBase...)
// inline tests
lib.MustEqual("concat", "0x")
lib.MustEqual("concat(1,2)", "0x0102")
lib.MustEqual("concat(1,2,3,4)", "concat(concat(1,2),concat(3,4))")
lib.MustError("fail(100)", "SCRIPT FAIL: error #100")
lib.MustError("!!!hello,_world!", "hello, world!")
lib.MustError("!!!fail_error_message_31415", "31415")
lib.MustEqual("slice(0x010203,1,2)", "0x0203")
lib.MustEqual("byte(0x010203, 2)", "3")
lib.MustEqual("tail(0x010203, 2)", "3")
lib.MustTrue("hasPrefix(0xf10203,0xf1)")
lib.MustEqual("repeat(1,5)", "0x0101010101")
lib.MustTrue("equal(len(nil), u64/0)")
lib.MustEqual("not(1)", "0x")
lib.MustTrue("and")
lib.MustTrue("not(and(concat))")
lib.MustTrue("not(or)")
lib.MustTrue("not(or(concat))")
lib.MustTrue("or(1)")
lib.MustTrue("isZero(0)")
lib.MustTrue("isZero(repeat(0,100))")
lib.MustTrue("not(isZero(0x0000000003))")
}
func (lib *Library) embedArithmetics() {
lib.UpgradeWithEmbeddedShort(embedArithmeticsShort...)
lib.MustEqual("add(5,6)", "add(10,1)")
lib.MustEqual("add(5,6)", "u64/11")
lib.MustEqual("add(0, 0)", "u64/0")
lib.MustEqual("add(u16/1337, 0)", "u64/1337")
lib.MustError("add(nil, 0)", "wrong size of parameter")
lib.MustEqual("sub(6,6)", "u64/0")
lib.MustEqual("sub(6,5)", "u64/1")
lib.MustEqual("sub(0, 0)", "u64/0")
lib.MustEqual("sub(u16/1337, 0)", "u64/1337")
lib.MustError("sub(nil, 0)", "wrong size of parameter")
lib.MustError("sub(10, 100)", "underflow in subtraction")
lib.MustEqual("mul(5,6)", "mul(15,2)")
lib.MustEqual("mul(5,6)", "u64/30")
lib.MustEqual("mul(u16/1337, 0)", "u64/0")
lib.MustEqual("mul(0, u32/1337133700)", "u64/0")
lib.MustError("mul(nil, 5)", "wrong size of parameter")
lib.MustEqual("div(100,100)", "u64/1")
lib.MustEqual("div(100,110)", "u64/0")
lib.MustEqual("div(u32/10000,u16/10000)", "u64/1")
lib.MustEqual("div(0, u32/1337133700)", "u64/0")
lib.MustError("div(u32/1337133700, 0)", "integer divide by zero")
lib.MustError("div(nil, 5)", "wrong size of parameter")
lib.MustEqual("mod(100,100)", "u64/0")
lib.MustEqual("mod(107,100)", "u64/7")
lib.MustEqual("mod(u32/10100,u16/10000)", "u64/100")
lib.MustEqual("mod(0, u32/1337133700)", "u64/0")
lib.MustError("mod(u32/1337133700, 0)", "integer divide by zero")
lib.MustError("mod(nil, 5)", "wrong size of parameter")
lib.MustEqual("add(mul(div(u32/27, u16/4), 4), mod(u32/27, 4))", "u64/27")
}
func (lib *Library) embedBitwiseAndCmp() {
lib.UpgradeWithEmbeddedShort(embedBitwiseAndCmpShort...)
lib.UpgradeWthEmbeddedLong(embedBitwiseAndCmpLong...)
// comparison lexicographical (equivalent to bigendian for binary integers)
lib.MustTrue("lessThan(1,2)")
lib.MustTrue("not(lessThan(2,1))")
lib.MustTrue("not(lessThan(2,2))")
// bitwise
//lib.embedShort("bitwiseOR", 2, evalBitwiseOR)
lib.MustEqual("bitwiseOR(0x01, 0x80)", "0x81")
//lib.embedShort("bitwiseAND", 2, evalBitwiseAND)
lib.MustEqual("bitwiseAND(0x03, 0xf2)", "0x02")
lib.MustEqual("bitwiseAND(0x0102, 0xff00)", "0x0100")
//lib.embedShort("bitwiseNOT", 1, evalBitwiseNOT)
lib.MustEqual("bitwiseNOT(0x00ff)", "0xff00")
//lib.embedShort("bitwiseXOR", 2, evalBitwiseXOR)
lib.MustEqual("bitwiseXOR(0x1234, 0x1234)", "0x0000")
lib.MustEqual("bitwiseXOR(0x1234, 0xffff)", "bitwiseNOT(0x1234)")
// other
//lib.embedLong("lshift64", 2, evalLShift64)
lib.MustEqual("lshift64(u64/3, u64/2)", "u64/12")
lib.MustTrue("isZero(lshift64(u64/2001, u64/64))")
lib.MustTrue("equal(lshift64(u64/2001, u64/4), mul(u64/2001, u16/16))")
lib.MustError("lshift64(u64/2001, nil)", "wrong size of parameter")
//lib.embedLong("rshift64", 2, evalRShift64)
lib.MustEqual("rshift64(u64/15, u64/2)", "u64/3")
lib.MustTrue("isZero(rshift64(0xffffffffffffffff, u64/64))")
lib.MustTrue("equal(rshift64(u64/2001, u64/3), div(u64/2001, 8))")
lib.MustError("rshift64(u64/2001, nil)", "wrong size of parameter")
}
func (lib *Library) embedBaseCrypto() {
lib.UpgradeWthEmbeddedLong(embedBaseCrypto...)
h := blake2b.Sum256([]byte{1})
lib.MustEqual("len(blake2b(1))", "u64/32")
lib.MustEqual("blake2b(1)", fmt.Sprintf("0x%s", hex.EncodeToString(h[:])))
}
func (lib *Library) embedBytecodeManipulation() {
// code parsing
lib.UpgradeWthEmbeddedLong(embedBytecodeManipulation(lib)...)
_, _, binCode, err := lib.CompileExpression("slice(0x01020304,1,2)")
AssertNoError(err)
src := fmt.Sprintf("eval(parseArgumentBytecode(0x%s, #slice, %d))", hex.EncodeToString(binCode), 0)
lib.MustEqual(src, "0x01020304")
src = fmt.Sprintf("eval(parseArgumentBytecode(0x%s, #slice, %d))", hex.EncodeToString(binCode), 1)
lib.MustEqual(src, "1")
src = fmt.Sprintf("eval(parseArgumentBytecode(0x%s, #slice, %d))", hex.EncodeToString(binCode), 2)
lib.MustEqual(src, "2")
src = fmt.Sprintf("parsePrefixBytecode(0x%s)", hex.EncodeToString(binCode))
lib.MustEqual(src, "#slice")
}
// -----------------------------------------------------------------
func isNil(p interface{}) bool {
return p == nil || (reflect.ValueOf(p).Kind() == reflect.Ptr && reflect.ValueOf(p).IsNil())
}
func evalFail(par *CallParams) []byte {
c := par.Arg(0)
if len(c) == 1 {
par.TracePanic("SCRIPT FAIL: error #%d", c[0])
}
par.TracePanic("SCRIPT FAIL: '%s'", string(c))
return nil
}
// slices first argument 'from' 'to' inclusive 'to'
func evalSlice(par *CallParams) []byte {
data := par.Arg(0)
from := par.Arg(1)
to := par.Arg(2)
if len(from) != 1 || len(to) != 1 {
par.TracePanic("slice:: data: %s, from: %s, to: %s -- wrong bound values", Fmt(data), Fmt(from), Fmt(to))
}
if from[0] > to[0] {
par.TracePanic("slice:: data: %s, from: %s, to: %s -- wrong slice bounds. ", Fmt(data), Fmt(from), Fmt(to))
}
upper := int(to[0]) + 1
if upper > len(data) {
par.TracePanic("slice:: data: %s, from: %s, to: %s -- slice out of bounds. ", Fmt(data), Fmt(from), Fmt(to))
}
ret := data[from[0]:upper]
par.Trace("slice:: data: %s, from: %s, to: %s -> %s", Fmt(data), Fmt(from), Fmt(to), Fmt(ret))
return ret
}
func evalByte(par *CallParams) []byte {
data := par.Arg(0)
idx := par.Arg(1)
if len(idx) != 1 || int(idx[0]) >= len(data) {
par.TracePanic("byte:: data: %s, idx: %s -- wrong index value", Fmt(data), Fmt(idx))
}
ret := data[idx[0] : idx[0]+1]
par.Trace("byte:: data: %s, idx: %s -> %s", Fmt(data), Fmt(idx), Fmt(ret))
return ret
}
func evalTail(par *CallParams) []byte {
data := par.Arg(0)
from := par.Arg(1)
if len(from) != 1 || int(from[0]) >= len(data) {
par.TracePanic("tail:: data: %s, from: %s -- index out of bounds. ", Fmt(data), Fmt(from))
}
ret := data[from[0]:]
par.Trace("tail:: data: %s, from: %s -> %s", Fmt(data), Fmt(from), Fmt(ret))
return ret
}
func evalEqual(par *CallParams) []byte {
var ret []byte
p0 := par.Arg(0)
p1 := par.Arg(1)
if bytes.Equal(p0, p1) {
ret = []byte{0xff}
}
par.Trace("equal:: %s, %s -> %s", Fmt(p0), Fmt(p1), Fmt(ret))
return ret
}
func evalHasPrefix(par *CallParams) []byte {
var ret []byte
data := par.Arg(0)
prefix := par.Arg(1)
if bytes.HasPrefix(data, prefix) {
ret = []byte{0xff}
}
par.Trace("hasPrefix:: %s, %s -> %s", Fmt(data), Fmt(prefix), Fmt(ret))
return ret
}
func evalRepeat(par *CallParams) []byte {
fragment := par.Arg(0)
n := par.Arg(1)
if len(n) != 1 {
par.TracePanic("evalRepeat: count must be 1-byte long")
}
ret := bytes.Repeat(fragment, int(n[0]))
par.Trace("hasPrefix:: %s, %s -> %s", Fmt(fragment), Fmt(n), Fmt(ret))
return ret
}
func evalLen(par *CallParams) []byte {
data := par.Arg(0)
var ret [8]byte
binary.BigEndian.PutUint64(ret[:], uint64(len(data)))
par.Trace("len:: %s -> %s", Fmt(data), Fmt(ret[:]))
return ret[:]
}
func evalIf(par *CallParams) []byte {
cond := par.Arg(0)
if len(cond) != 0 {
yes := par.Arg(1)
par.Trace("if:: %s -> %s", Fmt(cond), Fmt(yes))
return yes
}
no := par.Arg(2)
par.Trace("if:: %s -> %s", Fmt(cond), Fmt(no))
return no
}
func evalFirstCaseIndex(par *CallParams) []byte {
for i := byte(0); i < par.Arity(); i++ {
if ret := par.Arg(i); len(ret) > 0 {
par.Trace("firstCaseIndex:: -> %d", i)
return []byte{i}
}
}
par.Trace("firstCaseIndex:: -> nil")
return nil
}
func evalFirstEqualIndex(par *CallParams) []byte {
if par.Arity() == 0 {
return nil
}
v := par.Arg(0)
for i := byte(1); i < par.Arity(); i++ {
if bytes.Equal(v, par.Arg(i)) {
par.Trace("firstEqualIndex:: -> %d", i)
return []byte{i - 1}
}
}
par.Trace("firstEqualIndex:: -> nil")
return nil
}
func evalSelectCaseByIndex(par *CallParams) []byte {
if par.Arity() == 0 {
par.TracePanic("evalSelectCaseByIndex: must be at least 1 argument")
}
idx := par.Arg(0)
if len(idx) != 1 || idx[0]+1 >= par.Arity() {
return nil
}
return par.Arg(idx[0] + 1)
}
func evalIsZero(par *CallParams) []byte {
arg := par.Arg(0)
for _, b := range arg {
if b != 0 {
par.Trace("isZero:: %s -> nil", Fmt(arg))
return nil
}
}
par.Trace("isZero:: %s -> true", Fmt(arg))
return []byte{0xff}
}
func evalNot(par *CallParams) []byte {
arg := par.Arg(0)
if len(arg) == 0 {
par.Trace("not:: %s -> true", Fmt(arg))
return []byte{0xff}
}
par.Trace("not:: %s -> nil", Fmt(arg))
return nil
}
func evalConcat(par *CallParams) []byte {
var buf bytes.Buffer
for i := byte(0); i < par.Arity(); i++ {
buf.Write(par.Arg(i))
}
ret := buf.Bytes()
par.Trace("Concat:: %d params -> %s", par.Arity(), Fmt(ret))
return ret
}
func evalAnd(par *CallParams) []byte {
for i := byte(0); i < par.Arity(); i++ {
if len(par.Arg(i)) == 0 {
par.Trace("and:: param %d nil -> nil", i)
return nil
}
}
par.Trace("and:: %d params -> true", par.Arity())
return []byte{0xff}
}
func evalOr(par *CallParams) []byte {
for i := byte(0); i < par.Arity(); i++ {
if len(par.Arg(i)) != 0 {
par.Trace("or:: param %d -> true", i)
return []byte{0xff}
}
}
par.Trace("or:: %d params -> nil", par.Arity())
return nil
}
func ensureUint64Bytes(data []byte) ([]byte, bool) {
if len(data) == 8 {
return data, true
}
if len(data) == 0 || len(data) > 8 {
return nil, false
}
ret := make([]byte, 8)
copy(ret[8-len(data):], data)
return ret, true
}
// mustArithmeticArgs makes uint64 from both params (bigendian)
// Parameters must be not nil with size <= 8. They are padded with 0 in upper bytes, if necessary
func mustArithmeticArgs(par *CallParams, name string) (uint64, uint64) {
a0, ok := ensureUint64Bytes(par.Arg(0))
if !ok {
par.TracePanic("%s:: wrong size of parameter 0", name)
}
a1, ok := ensureUint64Bytes(par.Arg(1))
if !ok {
par.TracePanic("%s:: wrong size of parameter 1", name)
}
return binary.BigEndian.Uint64(a0), binary.BigEndian.Uint64(a1)
}
func evalAddUint(par *CallParams) []byte {
a0, a1 := mustArithmeticArgs(par, "addUint")
var ret [8]byte
binary.BigEndian.PutUint64(ret[:], a0+a1)
return ret[:]
}
func evalSubUint(par *CallParams) []byte {
a0, a1 := mustArithmeticArgs(par, "subUint")
if a0 < a1 {
par.TracePanic("evalSubUint:: %d - %d -> underflow in subtraction", a0, a1)
}
var ret [8]byte
binary.BigEndian.PutUint64(ret[:], a0-a1)
return ret[:]
}
func evalMulUint(par *CallParams) []byte {
a0, a1 := mustArithmeticArgs(par, "mulUint")
var ret [8]byte
binary.BigEndian.PutUint64(ret[:], a0*a1)
return ret[:]
}
func evalDivUint(par *CallParams) []byte {
a0, a1 := mustArithmeticArgs(par, "divUint")
var ret [8]byte
binary.BigEndian.PutUint64(ret[:], a0/a1)
return ret[:]
}
func evalModuloUint(par *CallParams) []byte {
a0, a1 := mustArithmeticArgs(par, "moduloUint")
var ret [8]byte
binary.BigEndian.PutUint64(ret[:], a0%a1)
return ret[:]
}
func evalUint64Bytes(par *CallParams) []byte {
ret, ok := ensureUint64Bytes(par.Arg(0))
if !ok {
par.TracePanic("%s:: wrong size of parameter", "uint64Bytes")
}
return ret
}
// lexicographical comparison of two slices of equal length
func evalLessThan(par *CallParams) []byte {
a0 := par.Arg(0)
a1 := par.Arg(1)
if len(a0) != len(a1) {
par.TracePanic("lessThan: operands must be equal length. %s, %s", Fmt(a0), Fmt(a1))
}
for i := range a0 {
switch {
case a0[i] < a1[i]:
par.Trace("lessThan: %s, %s -> true", Fmt(a0), Fmt(a1))
return []byte{0xff} // true
case a0[i] > a1[i]:
par.Trace("lessThan: %s, %s -> false", Fmt(a0), Fmt(a1))
return nil //false
}
}
par.Trace("lessThan: %s, %s -> false", Fmt(a0), Fmt(a1))
return nil // equal -> false
}
func evalValidSigED25519(par *CallParams) []byte {
msg := par.Arg(0)
signature := par.Arg(1)
pubKey := par.Arg(2)
if ed25519.Verify(pubKey, msg, signature) {
par.Trace("ValidSigED25519: msg=%s, sig=%s, pubKey=%s -> true",
Fmt(msg), Fmt(signature), Fmt(pubKey))
return []byte{0xff}
}
par.Trace("ValidSigED25519: msg=%s, sig=%s, pubKey=%s -> false",
Fmt(msg), Fmt(signature), Fmt(pubKey))
return nil
}
func evalBlake2b(par *CallParams) []byte {
var buf bytes.Buffer
for i := byte(0); i < par.Arity(); i++ {
buf.Write(par.Arg(i))
}
ret := blake2b.Sum256(buf.Bytes())
par.Trace("blake2b: %d params -> %s", par.Arity(), Fmt(ret[:]))
return ret[:]
}
func evalBitwiseAND(par *CallParams) []byte {
a0 := par.Arg(0)
a1 := par.Arg(1)
if len(a0) != len(a1) {
par.TracePanic("evalBitwiseAND: equal length arguments expected: %s -- %s", Fmt(a0), Fmt(a1))
}
ret := make([]byte, len(a0))
for i := range a0 {
ret[i] = a0[i] & a1[i]
}
par.Trace("evalBitwiseAND: %s, %s -> %s", Fmt(a0), Fmt(a1), Fmt(ret))
return ret
}
func evalBitwiseOR(par *CallParams) []byte {
a0 := par.Arg(0)
a1 := par.Arg(1)
if len(a0) != len(a1) {
par.TracePanic("evalBitwiseOR: equal length arguments expected: %s -- %s", Fmt(a0), Fmt(a1))
}
ret := make([]byte, len(a0))
for i := range a0 {
ret[i] = a0[i] | a1[i]
}
par.Trace("evalBitwiseOR: %s, %s -> %s", Fmt(a0), Fmt(a1), Fmt(ret))
return ret
}
func evalBitwiseXOR(par *CallParams) []byte {
a0 := par.Arg(0)
a1 := par.Arg(1)
if len(a0) != len(a1) {
par.TracePanic("evalBitwiseXOR: equal length arguments expected: %s -- %s", Fmt(a0), Fmt(a1))
}
ret := make([]byte, len(a0))
for i := range a0 {
ret[i] = a0[i] ^ a1[i]
}
par.Trace("evalBitwiseXOR: %s, %s -> %s", Fmt(a0), Fmt(a1), Fmt(ret))
return ret
}
func evalBitwiseNOT(par *CallParams) []byte {
a0 := par.Arg(0)
ret := make([]byte, len(a0))
for i := range a0 {
ret[i] = ^a0[i]
}
par.Trace("evalBitwiseNOT: %s -> %s", Fmt(a0), Fmt(ret))
return ret
}
func evalLShift64(par *CallParams) []byte {
a0, a1 := mustArithmeticArgs(par, "lshift64")
var ret [8]byte
binary.BigEndian.PutUint64(ret[:], a0<<a1)
return ret[:]
}
func evalRShift64(par *CallParams) []byte {
a0, a1 := mustArithmeticArgs(par, "lshift64")
var ret [8]byte
binary.BigEndian.PutUint64(ret[:], a0>>a1)
return ret[:]
}
// evalParseArgumentBytecode takes bytecode of the argument as is.
// Note: data prefix is not stripped. To get data it muts be evaluated
func (lib *Library) evalParseArgumentBytecode(par *CallParams) []byte {
a0 := par.Arg(0)
_, prefix, args, err := lib.ParseBytecodeOneLevel(a0)
if err != nil {
par.TracePanic("evalParseArgumentBytecode:: %v", err)
}
expectedPrefix := par.Arg(1)
idx := par.Arg(2)
if !bytes.Equal(prefix, expectedPrefix) {
_, _, _, symPrefix, err := lib.parseCallPrefix(prefix)
if err != nil {
par.TracePanic("evalParseArgumentBytecode: can't parse prefix '%s': %v", Fmt(prefix), err)
}
_, _, _, symExpectedPrefix, err := lib.parseCallPrefix(expectedPrefix)
if err != nil {
par.TracePanic("evalParseArgumentBytecode: can't parse expected prefix '%s': %v", Fmt(expectedPrefix), err)
}
par.TracePanic("evalParseArgumentBytecode: unexpected function prefix. Expected '%s'('%s'), got '%s'('%s')",
Fmt(expectedPrefix), symExpectedPrefix, Fmt(prefix), symPrefix)
}
if len(idx) != 1 || len(args) <= int(idx[0]) {
par.TracePanic("evalParseArgumentBytecode: wrong parameter index")
}
//ret := StripDataPrefix(args[idx[0]])
ret := args[idx[0]]
par.Trace("unwrapBytecodeArg:: %s, %s, %s -> %s", Fmt(a0), Fmt(expectedPrefix), Fmt(idx), Fmt(ret))
return ret
}
func (lib *Library) evalParsePrefixBytecode(par *CallParams) []byte {
code := par.Arg(0)
prefix, err := lib.ParsePrefixBytecode(code)
if err != nil {
par.TracePanic("evalParsePrefixBytecode: %v", err)
}
par.Trace("parseBytecodePrefix::%s -> %s", Fmt(code), Fmt(prefix))
return prefix
}
func (lib *Library) evalBytecodeArg(par *CallParams) []byte {
a0 := par.Arg(0)
_, prefix, args, err := lib.ParseBytecodeOneLevel(a0)
if err != nil {
par.TracePanic("evalParseArgumentBytecode:: %v", err)
}
expectedPrefix := par.Arg(1)
idx := par.Arg(2)
if !bytes.Equal(prefix, expectedPrefix) {
_, _, _, symPrefix, err := lib.parseCallPrefix(prefix)
if err != nil {
par.TracePanic("evalBytecodeArg: can't parse prefix '%s': %v", Fmt(prefix), err)
}
_, _, _, symExpectedPrefix, err := lib.parseCallPrefix(expectedPrefix)
if err != nil {
par.TracePanic("evalBytecodeArg: can't parse expected prefix '%s': %v", Fmt(expectedPrefix), err)
}
par.TracePanic("evalBytecodeArg: unexpected function prefix. Expected '%s'('%s'), got '%s'('%s')",
Fmt(expectedPrefix), symExpectedPrefix, Fmt(prefix), symPrefix)
}
if len(idx) != 1 || len(args) <= int(idx[0]) {
par.TracePanic("evalParseArgumentBytecode: wrong parameter index")
}
ret, err := lib.EvalFromBytecode(par.ctx.glb, args[idx[0]])
if err != nil {
par.TracePanic("evalBytecodeArg:: %s, %s, %s", Fmt(a0), Fmt(expectedPrefix), Fmt(idx))
}
par.Trace("evalBytecodeArg:: %s, %s, %s -> %s", Fmt(a0), Fmt(expectedPrefix), Fmt(idx), Fmt(ret))
return ret
}
func (lib *Library) evalBytecode(par *CallParams) []byte {
ret, err := lib.EvalFromBytecode(par.ctx.glb, par.Arg(0))
if err != nil {
par.TracePanic("evalBytecode:: %v", err)
}
par.Trace("evalBytecode:: %s} -> %s", Fmt(par.Arg(0)), Fmt(ret))
return ret
}