@@ -22,6 +22,7 @@ import (
22
22
"encoding/binary"
23
23
"errors"
24
24
"fmt"
25
+ "maps"
25
26
"math/big"
26
27
27
28
"github.com/prysmaticlabs/prysm/v5/crypto/bls"
@@ -51,105 +52,123 @@ type PrecompiledContract interface {
51
52
Run (input []byte ) ([]byte , error ) // Run runs the precompiled contract
52
53
}
53
54
55
+ // PrecompiledContracts contains the precompiled contracts supported at the given fork.
56
+ type PrecompiledContracts map [common.Address ]PrecompiledContract
57
+
54
58
// PrecompiledContractsHomestead contains the default set of pre-compiled Ethereum
55
59
// contracts used in the Frontier and Homestead releases.
56
- var PrecompiledContractsHomestead = map [common. Address ] PrecompiledContract {
57
- common .BytesToAddress ([]byte {1 }): & ecrecover {},
58
- common .BytesToAddress ([]byte {2 }): & sha256hash {},
59
- common .BytesToAddress ([]byte {3 }): & ripemd160hash {},
60
- common .BytesToAddress ([]byte {4 }): & dataCopy {},
60
+ var PrecompiledContractsHomestead = PrecompiledContracts {
61
+ common .BytesToAddress ([]byte {0x1 }): & ecrecover {},
62
+ common .BytesToAddress ([]byte {0x2 }): & sha256hash {},
63
+ common .BytesToAddress ([]byte {0x3 }): & ripemd160hash {},
64
+ common .BytesToAddress ([]byte {0x4 }): & dataCopy {},
61
65
}
62
66
63
67
// PrecompiledContractsByzantium contains the default set of pre-compiled Ethereum
64
68
// contracts used in the Byzantium release.
65
- var PrecompiledContractsByzantium = map [common. Address ] PrecompiledContract {
66
- common .BytesToAddress ([]byte {1 }): & ecrecover {},
67
- common .BytesToAddress ([]byte {2 }): & sha256hash {},
68
- common .BytesToAddress ([]byte {3 }): & ripemd160hash {},
69
- common .BytesToAddress ([]byte {4 }): & dataCopy {},
70
- common .BytesToAddress ([]byte {5 }): & bigModExp {eip2565 : false },
71
- common .BytesToAddress ([]byte {6 }): & bn256AddByzantium {},
72
- common .BytesToAddress ([]byte {7 }): & bn256ScalarMulByzantium {},
73
- common .BytesToAddress ([]byte {8 }): & bn256PairingByzantium {},
69
+ var PrecompiledContractsByzantium = PrecompiledContracts {
70
+ common .BytesToAddress ([]byte {0x1 }): & ecrecover {},
71
+ common .BytesToAddress ([]byte {0x2 }): & sha256hash {},
72
+ common .BytesToAddress ([]byte {0x3 }): & ripemd160hash {},
73
+ common .BytesToAddress ([]byte {0x4 }): & dataCopy {},
74
+ common .BytesToAddress ([]byte {0x5 }): & bigModExp {eip2565 : false },
75
+ common .BytesToAddress ([]byte {0x6 }): & bn256AddByzantium {},
76
+ common .BytesToAddress ([]byte {0x7 }): & bn256ScalarMulByzantium {},
77
+ common .BytesToAddress ([]byte {0x8 }): & bn256PairingByzantium {},
74
78
}
75
79
76
80
// PrecompiledContractsIstanbul contains the default set of pre-compiled Ethereum
77
81
// contracts used in the Istanbul release.
78
82
var PrecompiledContractsIstanbul = map [common.Address ]PrecompiledContract {
79
- common .BytesToAddress ([]byte {1 }): & ecrecover {},
80
- common .BytesToAddress ([]byte {2 }): & sha256hash {},
81
- common .BytesToAddress ([]byte {3 }): & ripemd160hash {},
82
- common .BytesToAddress ([]byte {4 }): & dataCopy {},
83
- common .BytesToAddress ([]byte {5 }): & bigModExp {},
84
- common .BytesToAddress ([]byte {6 }): & bn256AddIstanbul {},
85
- common .BytesToAddress ([]byte {7 }): & bn256ScalarMulIstanbul {},
86
- common .BytesToAddress ([]byte {8 }): & bn256PairingIstanbul {},
87
- common .BytesToAddress ([]byte {9 }): & blake2F {},
83
+ common .BytesToAddress ([]byte {0x1 }): & ecrecover {},
84
+ common .BytesToAddress ([]byte {0x2 }): & sha256hash {},
85
+ common .BytesToAddress ([]byte {0x3 }): & ripemd160hash {},
86
+ common .BytesToAddress ([]byte {0x4 }): & dataCopy {},
87
+ common .BytesToAddress ([]byte {0x5 }): & bigModExp {eip2565 : false },
88
+ common .BytesToAddress ([]byte {0x6 }): & bn256AddIstanbul {},
89
+ common .BytesToAddress ([]byte {0x7 }): & bn256ScalarMulIstanbul {},
90
+ common .BytesToAddress ([]byte {0x8 }): & bn256PairingIstanbul {},
91
+ common .BytesToAddress ([]byte {0x9 }): & blake2F {},
88
92
89
- common .BytesToAddress ([]byte {100 }): & btcValidate {},
93
+ common .BytesToAddress ([]byte {0x64 }): & btcValidate {},
90
94
}
91
95
92
96
// PrecompiledContractsIstanbul contains the default set of pre-compiled Ethereum
93
97
// contracts used in the HashPower release.
94
98
var PrecompiledContractsHashPower = map [common.Address ]PrecompiledContract {
95
- common .BytesToAddress ([]byte {1 }): & ecrecover {},
96
- common .BytesToAddress ([]byte {2 }): & sha256hash {},
97
- common .BytesToAddress ([]byte {3 }): & ripemd160hash {},
98
- common .BytesToAddress ([]byte {4 }): & dataCopy {},
99
- common .BytesToAddress ([]byte {5 }): & bigModExp {},
100
- common .BytesToAddress ([]byte {6 }): & bn256AddIstanbul {},
101
- common .BytesToAddress ([]byte {7 }): & bn256ScalarMulIstanbul {},
102
- common .BytesToAddress ([]byte {8 }): & bn256PairingIstanbul {},
103
- common .BytesToAddress ([]byte {9 }): & blake2F {},
99
+ common .BytesToAddress ([]byte {0x1 }): & ecrecover {},
100
+ common .BytesToAddress ([]byte {0x2 }): & sha256hash {},
101
+ common .BytesToAddress ([]byte {0x3 }): & ripemd160hash {},
102
+ common .BytesToAddress ([]byte {0x4 }): & dataCopy {},
103
+ common .BytesToAddress ([]byte {0x5 }): & bigModExp {eip2565 : false },
104
+ common .BytesToAddress ([]byte {0x6 }): & bn256AddIstanbul {},
105
+ common .BytesToAddress ([]byte {0x7 }): & bn256ScalarMulIstanbul {},
106
+ common .BytesToAddress ([]byte {0x8 }): & bn256PairingIstanbul {},
107
+ common .BytesToAddress ([]byte {0x9 }): & blake2F {},
104
108
105
- common .BytesToAddress ([]byte {100 }): & btcValidateV2 {},
109
+ common .BytesToAddress ([]byte {0x64 }): & btcValidateV2 {},
106
110
}
107
111
108
112
// PrecompiledContractsBerlin contains the default set of pre-compiled Ethereum
109
113
// contracts used in the Berlin release.
110
114
var PrecompiledContractsBerlin = map [common.Address ]PrecompiledContract {
111
- common .BytesToAddress ([]byte {1 }): & ecrecover {},
112
- common .BytesToAddress ([]byte {2 }): & sha256hash {},
113
- common .BytesToAddress ([]byte {3 }): & ripemd160hash {},
114
- common .BytesToAddress ([]byte {4 }): & dataCopy {},
115
- common .BytesToAddress ([]byte {5 }): & bigModExp {eip2565 : true },
116
- common .BytesToAddress ([]byte {6 }): & bn256AddIstanbul {},
117
- common .BytesToAddress ([]byte {7 }): & bn256ScalarMulIstanbul {},
118
- common .BytesToAddress ([]byte {8 }): & bn256PairingIstanbul {},
119
- common .BytesToAddress ([]byte {9 }): & blake2F {},
115
+ common .BytesToAddress ([]byte {0x1 }): & ecrecover {},
116
+ common .BytesToAddress ([]byte {0x2 }): & sha256hash {},
117
+ common .BytesToAddress ([]byte {0x3 }): & ripemd160hash {},
118
+ common .BytesToAddress ([]byte {0x4 }): & dataCopy {},
119
+ common .BytesToAddress ([]byte {0x5 }): & bigModExp {eip2565 : false },
120
+ common .BytesToAddress ([]byte {0x6 }): & bn256AddIstanbul {},
121
+ common .BytesToAddress ([]byte {0x7 }): & bn256ScalarMulIstanbul {},
122
+ common .BytesToAddress ([]byte {0x8 }): & bn256PairingIstanbul {},
123
+ common .BytesToAddress ([]byte {0x9 }): & blake2F {},
120
124
121
- common .BytesToAddress ([]byte {100 }): & btcValidateV2 {},
125
+ common .BytesToAddress ([]byte {0x64 }): & btcValidateV2 {},
122
126
}
123
127
124
128
// PrecompiledContractsCancun contains the default set of pre-compiled Ethereum
125
129
// contracts used in the Cancun release.
126
- var PrecompiledContractsCancun = map [common.Address ]PrecompiledContract {
127
- common .BytesToAddress ([]byte {1 }): & ecrecover {},
128
- common .BytesToAddress ([]byte {2 }): & sha256hash {},
129
- common .BytesToAddress ([]byte {3 }): & ripemd160hash {},
130
- common .BytesToAddress ([]byte {4 }): & dataCopy {},
131
- common .BytesToAddress ([]byte {5 }): & bigModExp {eip2565 : true },
132
- common .BytesToAddress ([]byte {6 }): & bn256AddIstanbul {},
133
- common .BytesToAddress ([]byte {7 }): & bn256ScalarMulIstanbul {},
134
- common .BytesToAddress ([]byte {8 }): & bn256PairingIstanbul {},
135
- common .BytesToAddress ([]byte {9 }): & blake2F {},
130
+ var PrecompiledContractsCancun = PrecompiledContracts {
131
+ common .BytesToAddress ([]byte {0x1 }): & ecrecover {},
132
+ common .BytesToAddress ([]byte {0x2 }): & sha256hash {},
133
+ common .BytesToAddress ([]byte {0x3 }): & ripemd160hash {},
134
+ common .BytesToAddress ([]byte {0x4 }): & dataCopy {},
135
+ common .BytesToAddress ([]byte {0x5 }): & bigModExp {eip2565 : true },
136
+ common .BytesToAddress ([]byte {0x6 }): & bn256AddIstanbul {},
137
+ common .BytesToAddress ([]byte {0x7 }): & bn256ScalarMulIstanbul {},
138
+ common .BytesToAddress ([]byte {0x8 }): & bn256PairingIstanbul {},
139
+ common .BytesToAddress ([]byte {0x9 }): & blake2F {},
140
+ common .BytesToAddress ([]byte {0xa }): & kzgPointEvaluation {},
141
+ }
142
+
143
+ // PrecompiledContractsPrague contains the set of pre-compiled Ethereum
144
+ // contracts used in the Prague release.
145
+ var PrecompiledContractsPrague = PrecompiledContracts {
146
+ common .BytesToAddress ([]byte {0x01 }): & ecrecover {},
147
+ common .BytesToAddress ([]byte {0x02 }): & sha256hash {},
148
+ common .BytesToAddress ([]byte {0x03 }): & ripemd160hash {},
149
+ common .BytesToAddress ([]byte {0x04 }): & dataCopy {},
150
+ common .BytesToAddress ([]byte {0x05 }): & bigModExp {eip2565 : true },
151
+ common .BytesToAddress ([]byte {0x06 }): & bn256AddIstanbul {},
152
+ common .BytesToAddress ([]byte {0x07 }): & bn256ScalarMulIstanbul {},
153
+ common .BytesToAddress ([]byte {0x08 }): & bn256PairingIstanbul {},
154
+ common .BytesToAddress ([]byte {0x09 }): & blake2F {},
136
155
common .BytesToAddress ([]byte {0x0a }): & kzgPointEvaluation {},
137
156
138
- common .BytesToAddress ([]byte {100 }): & btcValidateV2 {},
157
+ common .BytesToAddress ([]byte {0x64 }): & btcValidateV2 {},
139
158
}
140
159
141
160
// PrecompiledContractsBLS contains the set of pre-compiled Ethereum
142
161
// contracts specified in EIP-2537. These are exported for testing purposes.
143
162
var PrecompiledContractsBLS = map [common.Address ]PrecompiledContract {
144
- common .BytesToAddress ([]byte {10 }): & bls12381G1Add {},
145
- common .BytesToAddress ([]byte {11 }): & bls12381G1Mul {},
146
- common .BytesToAddress ([]byte {12 }): & bls12381G1MultiExp {},
147
- common .BytesToAddress ([]byte {13 }): & bls12381G2Add {},
148
- common .BytesToAddress ([]byte {14 }): & bls12381G2Mul {},
149
- common .BytesToAddress ([]byte {15 }): & bls12381G2MultiExp {},
150
- common .BytesToAddress ([]byte {16 }): & bls12381Pairing {},
151
- common .BytesToAddress ([]byte {17 }): & bls12381MapG1 {},
152
- common .BytesToAddress ([]byte {18 }): & bls12381MapG2 {},
163
+ common .BytesToAddress ([]byte {0xa }): & bls12381G1Add {},
164
+ common .BytesToAddress ([]byte {0xb }): & bls12381G1Mul {},
165
+ common .BytesToAddress ([]byte {0xc }): & bls12381G1MultiExp {},
166
+ common .BytesToAddress ([]byte {0xd }): & bls12381G2Add {},
167
+ common .BytesToAddress ([]byte {0xe }): & bls12381G2Mul {},
168
+ common .BytesToAddress ([]byte {0xf }): & bls12381G2MultiExp {},
169
+ common .BytesToAddress ([]byte {0x10 }): & bls12381Pairing {},
170
+ common .BytesToAddress ([]byte {0x11 }): & bls12381MapG1 {},
171
+ common .BytesToAddress ([]byte {0x12 }): & bls12381MapG2 {},
153
172
}
154
173
155
174
var (
@@ -182,7 +201,31 @@ func init() {
182
201
}
183
202
}
184
203
185
- // ActivePrecompiles returns the precompiles enabled with the current configuration.
204
+ func activePrecompiledContracts (rules params.Rules ) PrecompiledContracts {
205
+ switch {
206
+ // case rules.IsVerkle:
207
+ // return PrecompiledContractsVerkle
208
+ case rules .IsPrague :
209
+ return PrecompiledContractsPrague
210
+ case rules .IsCancun :
211
+ return PrecompiledContractsCancun
212
+ case rules .IsBerlin :
213
+ return PrecompiledContractsBerlin
214
+ case rules .IsIstanbul :
215
+ return PrecompiledContractsIstanbul
216
+ case rules .IsByzantium :
217
+ return PrecompiledContractsByzantium
218
+ default :
219
+ return PrecompiledContractsHomestead
220
+ }
221
+ }
222
+
223
+ // ActivePrecompiledContracts returns a copy of precompiled contracts enabled with the current configuration.
224
+ func ActivePrecompiledContracts (rules params.Rules ) PrecompiledContracts {
225
+ return maps .Clone (activePrecompiledContracts (rules ))
226
+ }
227
+
228
+ // ActivePrecompiles returns the precompile addresses enabled with the current configuration.
186
229
func ActivePrecompiles (rules params.Rules ) []common.Address {
187
230
switch {
188
231
case rules .IsCancun :
0 commit comments