-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.go
326 lines (289 loc) · 6.31 KB
/
common.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
package neovm
import (
"crypto/sha1"
"crypto/sha256"
"hash"
"math/big"
"github.com/milechao/neovm/interfaces"
"github.com/milechao/neovm/types"
)
type BigIntSorter []big.Int
func (c BigIntSorter) Len() int {
return len(c)
}
func (c BigIntSorter) Swap(i, j int) {
if i >= 0 && i < len(c) && j >= 0 && j < len(c) {
// Unit Test modify
c[i], c[j] = c[j], c[i]
}
}
func (c BigIntSorter) Less(i, j int) bool {
if i >= 0 && i < len(c) && j >= 0 && j < len(c) {
// Unit Test modify
return c[i].Cmp(&c[j]) < 0
}
return false
}
func ToBigInt(data interface{}) *big.Int {
var bi big.Int
switch t := data.(type) {
case int64:
bi.SetInt64(int64(t))
case int32:
bi.SetInt64(int64(t))
case int16:
bi.SetInt64(int64(t))
case int8:
bi.SetInt64(int64(t))
case int:
bi.SetInt64(int64(t))
case uint64:
bi.SetUint64(uint64(t))
case uint32:
bi.SetUint64(uint64(t))
case uint16:
bi.SetUint64(uint64(t))
case uint8:
bi.SetUint64(uint64(t))
case uint:
bi.SetUint64(uint64(t))
case big.Int:
bi = t
case *big.Int:
bi = *t
}
return &bi
}
func Concat(array1 []byte, array2 []byte) []byte {
var r []byte
r = append(r, array1...)
return append(r, array2...)
}
func BigIntOp(bi *big.Int, op OpCode) *big.Int {
nb := new(big.Int)
switch op {
case INC:
nb.Add(bi, big.NewInt(int64(1)))
case DEC:
nb.Sub(bi, big.NewInt(int64(1)))
case NEGATE:
nb.Neg(bi)
case ABS:
nb.Abs(bi)
default:
nb.Set(bi)
}
return nb
}
func BigIntZip(ints1 *big.Int, ints2 *big.Int, op OpCode) *big.Int {
nb := new(big.Int)
switch op {
case AND:
nb.And(ints1, ints2)
case OR:
nb.Or(ints1, ints2)
case XOR:
nb.Xor(ints1, ints2)
case ADD:
nb.Add(ints1, ints2)
case SUB:
nb.Sub(ints1, ints2)
case MUL:
nb.Mul(ints1, ints2)
case DIV:
nb.Quo(ints1, ints2)
case MOD:
nb.Rem(ints1, ints2)
case SHL:
nb.Lsh(ints1, uint(ints2.Int64()))
case SHR:
nb.Rsh(ints1, uint(ints2.Int64()))
case MIN:
c := ints1.Cmp(ints2)
if c <= 0 {
nb.Set(ints1)
} else {
nb.Set(ints2)
}
case MAX:
c := ints1.Cmp(ints2)
if c <= 0 {
nb.Set(ints2)
} else {
nb.Set(ints1)
}
}
return nb
}
func BigIntComp(bigint *big.Int, op OpCode) bool {
var nb bool
switch op {
case NZ:
nb = bigint.Cmp(big.NewInt(int64(0))) != 0
}
return nb
}
func BigIntMultiComp(ints1 *big.Int, ints2 *big.Int, op OpCode) bool {
var nb bool
switch op {
case NUMEQUAL:
nb = ints1.Cmp(ints2) == 0
case NUMNOTEQUAL:
nb = ints1.Cmp(ints2) != 0
case LT:
nb = ints1.Cmp(ints2) < 0
case GT:
nb = ints1.Cmp(ints2) > 0
case LTE:
nb = ints1.Cmp(ints2) <= 0
case GTE:
nb = ints1.Cmp(ints2) >= 0
}
return nb
}
func BoolZip(bi1 bool, bi2 bool, op OpCode) bool {
var nb bool
switch op {
case BOOLAND:
nb = bi1 && bi2
case BOOLOR:
nb = bi1 || bi2
}
return nb
}
func WithInOp(int1 *big.Int, int2 *big.Int, int3 *big.Int) bool {
b1 := BigIntMultiComp(int1, int2, GTE)
b2 := BigIntMultiComp(int1, int3, LT)
return BoolZip(b1, b2, BOOLAND)
}
func NewStackItem(data interface{}) types.StackItems {
var stackItem types.StackItems
switch data.(type) {
case int8, int16, int32, int64, int, uint8, uint16, uint32, uint64, *big.Int, big.Int:
stackItem = types.NewInteger(ToBigInt(data))
case *types.Integer:
stackItem = data.(*types.Integer)
case *types.Array:
stackItem = data.(*types.Array)
case *types.Map:
stackItem = data.(*types.Map)
case *types.Boolean:
stackItem = data.(*types.Boolean)
case *types.ByteArray:
stackItem = data.(*types.ByteArray)
case *types.Struct:
stackItem = data.(*types.Struct)
case bool:
stackItem = types.NewBoolean(data.(bool))
case []byte:
stackItem = types.NewByteArray(data.([]byte))
case []types.StackItems:
stackItem = types.NewArray(data.([]types.StackItems))
case types.StackItems:
stackItem = data.(types.StackItems)
case interfaces.Interop:
stackItem = types.NewInteropInterface(data.(interfaces.Interop))
default:
panic("NewStackItemInterface Invalid Type!")
}
return stackItem
}
func Hash(b []byte, e *ExecutionEngine) []byte {
var sh hash.Hash
var bt []byte
switch e.OpCode {
case SHA1:
sh = sha1.New()
sh.Write(b)
bt = sh.Sum(nil)
case SHA256:
sh = sha256.New()
sh.Write(b)
bt = sh.Sum(nil)
}
return bt
}
func PopBigInt(e *ExecutionEngine) *big.Int {
x := PopStackItem(e)
return x.GetBigInteger()
}
func PopInt(e *ExecutionEngine) int {
x := PopBigInt(e)
n := int(x.Int64())
return n
}
func PopBoolean(e *ExecutionEngine) bool {
x := PopStackItem(e)
return x.GetBoolean()
}
func PopArray(e *ExecutionEngine) []types.StackItems {
x := PopStackItem(e)
return x.GetArray()
}
func PopMap(e *ExecutionEngine) map[types.StackItems]types.StackItems {
x := PopStackItem(e)
return x.GetMap()
}
func Pop(e *ExecutionEngine) []types.StackItems {
x := PopStackItem(e)
return x.GetArray()
}
func PopInteropInterface(e *ExecutionEngine) interfaces.Interop {
x := PopStackItem(e)
return x.GetInterface()
}
func PopByteArray(e *ExecutionEngine) []byte {
x := PopStackItem(e)
return x.GetByteArray()
}
func PopStackItem(e *ExecutionEngine) types.StackItems {
return e.EvaluationStack.Pop()
}
func PeekArray(e *ExecutionEngine) []types.StackItems {
x := PeekStackItem(e)
return x.GetArray()
}
func PeekInteropInterface(e *ExecutionEngine) interfaces.Interop {
x := PeekStackItem(e)
return x.GetInterface()
}
func PeekInt(e *ExecutionEngine) int {
x := PeekBigInteger(e)
n := int(x.Int64())
return n
}
func PeekBigInteger(e *ExecutionEngine) *big.Int {
x := PeekStackItem(e)
return x.GetBigInteger()
}
func PeekStackItem(e *ExecutionEngine) types.StackItems {
return e.EvaluationStack.Peek(0)
}
func PeekNInt(i int, e *ExecutionEngine) int {
x := PeekNBigInt(i, e)
n := int(x.Int64())
return n
}
func PeekNBigInt(i int, e *ExecutionEngine) *big.Int {
x := PeekNStackItem(i, e)
return x.GetBigInteger()
}
func PeekNByteArray(i int, e *ExecutionEngine) []byte {
x := PeekNStackItem(i, e)
return x.GetByteArray()
}
func PeekNStackItem(i int, e *ExecutionEngine) types.StackItems {
return e.EvaluationStack.Peek(i)
}
func EvaluationStackCount(e *ExecutionEngine) int {
return e.EvaluationStack.Count()
}
func Push(e *ExecutionEngine, element types.StackItems) {
e.EvaluationStack.Push(element)
}
func Count(e *ExecutionEngine) int {
return e.EvaluationStack.Count()
}
func PushData(e *ExecutionEngine, data interface{}) {
e.EvaluationStack.Push(NewStackItem(data))
}