This repository has been archived by the owner on Jun 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontracts_test.go
189 lines (163 loc) · 5.22 KB
/
contracts_test.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
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package eip2537
import (
"bytes"
"fmt"
"math/big"
"reflect"
"testing"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"
)
// precompiledTest defines the input/output pairs for precompiled contract tests.
type precompiledTest struct {
input, expected string
name string
noBenchmark bool // Benchmark primarily the worst-cases
}
// precompiledFailureTest defines the input/error pairs for precompiled
// contract failure tests.
type precompiledFailureTest struct {
input string
expectedError error
name string
}
func testPrecompiled(addr string, test precompiledTest, t *testing.T) {
p := PrecompiledContractsBerlinOnly[common.HexToAddress(addr)]
in := common.Hex2Bytes(test.input)
contract := vm.NewContract(vm.AccountRef(common.HexToAddress("1337")),
nil, new(big.Int), p.RequiredGas(in))
t.Run(fmt.Sprintf("%s-Gas=%d", test.name, contract.Gas), func(t *testing.T) {
if res, err := vm.RunPrecompiledContract(p, in, contract); err != nil {
t.Error(err)
} else if common.Bytes2Hex(res) != test.expected {
t.Errorf("Expected %v, got %v", test.expected, common.Bytes2Hex(res))
}
// Verify that the precompile did not touch the input buffer
exp := common.Hex2Bytes(test.input)
if !bytes.Equal(in, exp) {
t.Errorf("Precompiled %v modified input data", addr)
}
})
}
func testPrecompiledFailure(addr string, test precompiledFailureTest, t *testing.T) {
p := PrecompiledContractsBerlinOnly[common.HexToAddress(addr)]
in := common.Hex2Bytes(test.input)
contract := vm.NewContract(vm.AccountRef(common.HexToAddress("31337")),
nil, new(big.Int), p.RequiredGas(in))
t.Run(test.name, func(t *testing.T) {
_, err := vm.RunPrecompiledContract(p, in, contract)
if !reflect.DeepEqual(err, test.expectedError) {
t.Errorf("Expected error [%v], got [%v]", test.expectedError, err)
}
// Verify that the precompile did not touch the input buffer
exp := common.Hex2Bytes(test.input)
if !bytes.Equal(in, exp) {
t.Errorf("Precompiled %v modified input data", addr)
}
})
}
func TestPrecompiledBLS12381G1Add(t *testing.T) {
for _, test := range blsG1AddTests {
testPrecompiled("0a", test, t)
}
}
func TestPrecompiledBLS12381G1Mul(t *testing.T) {
for _, test := range blsG1MulTests {
testPrecompiled("0b", test, t)
}
}
func TestPrecompiledBLS12381G1MultiExp(t *testing.T) {
for _, test := range blsG1MultiExpTests {
testPrecompiled("0c", test, t)
}
}
func TestPrecompiledBLS12381G2Add(t *testing.T) {
for _, test := range blsG2AddTests {
testPrecompiled("0d", test, t)
}
}
func TestPrecompiledBLS12381G2Mul(t *testing.T) {
for _, test := range blsG2MulTests {
testPrecompiled("0e", test, t)
}
}
func TestPrecompiledBLS12381G2MultiExp(t *testing.T) {
for _, test := range blsG2MultiExpTests {
testPrecompiled("0f", test, t)
}
}
func TestPrecompiledBLS12381Pairing(t *testing.T) {
for _, test := range blsPairingTests {
testPrecompiled("10", test, t)
}
}
func TestPrecompiledBLS12381MapG1(t *testing.T) {
for _, test := range blsMapG1Tests {
testPrecompiled("11", test, t)
}
}
func TestPrecompiledBLS12381MapG2(t *testing.T) {
for _, test := range blsMapG2Tests {
testPrecompiled("12", test, t)
}
}
func TestPrecompiledBLS12381G1AddFail(t *testing.T) {
for _, test := range blsG1AddFailTests {
testPrecompiledFailure("0a", test, t)
}
}
func TestPrecompiledBLS12381G1MulFail(t *testing.T) {
for _, test := range blsG1MulFailTests {
testPrecompiledFailure("0b", test, t)
}
}
func TestPrecompiledBLS12381G1MultiExpFail(t *testing.T) {
for _, test := range blsG1MultiExpFailTests {
testPrecompiledFailure("0c", test, t)
}
}
func TestPrecompiledBLS12381G2AddFail(t *testing.T) {
for _, test := range blsG2AddFailTests {
testPrecompiledFailure("0d", test, t)
}
}
func TestPrecompiledBLS12381G2MulFail(t *testing.T) {
for _, test := range blsG2MulFailTests {
testPrecompiledFailure("0e", test, t)
}
}
func TestPrecompiledBLS12381G2MultiExpFail(t *testing.T) {
for _, test := range blsG2MultiExpFailTests {
testPrecompiledFailure("0f", test, t)
}
}
func TestPrecompiledBLS12381PairingFail(t *testing.T) {
for _, test := range blsPairingFailTests {
testPrecompiledFailure("10", test, t)
}
}
func TestPrecompiledBLS12381MapG1Fail(t *testing.T) {
for _, test := range blsMapG1FailTests {
testPrecompiledFailure("11", test, t)
}
}
func TestPrecompiledBLS12381MapG2Fail(t *testing.T) {
for _, test := range blsMapG2FailTests {
testPrecompiledFailure("12", test, t)
}
}