@@ -22,7 +22,9 @@ import (
2222 "math/big"
2323
2424 "github.com/ethereum/go-ethereum/common"
25+ "github.com/ethereum/go-ethereum/common/math"
2526 "github.com/ethereum/go-ethereum/crypto"
27+ "github.com/ethereum/go-ethereum/crypto/bn256"
2628 "github.com/ethereum/go-ethereum/params"
2729 "golang.org/x/crypto/ripemd160"
2830)
@@ -45,6 +47,19 @@ var PrecompiledContracts = map[common.Address]PrecompiledContract{
4547 common .BytesToAddress ([]byte {4 }): & dataCopy {},
4648}
4749
50+ // PrecompiledContractsMetropolis contains the default set of ethereum contracts
51+ // for metropolis hardfork
52+ var PrecompiledContractsMetropolis = map [common.Address ]PrecompiledContract {
53+ common .BytesToAddress ([]byte {1 }): & ecrecover {},
54+ common .BytesToAddress ([]byte {2 }): & sha256hash {},
55+ common .BytesToAddress ([]byte {3 }): & ripemd160hash {},
56+ common .BytesToAddress ([]byte {4 }): & dataCopy {},
57+ common .BytesToAddress ([]byte {5 }): & bigModexp {},
58+ common .BytesToAddress ([]byte {6 }): & bn256Add {},
59+ common .BytesToAddress ([]byte {7 }): & bn256ScalarMul {},
60+ common .BytesToAddress ([]byte {8 }): & pairing {},
61+ }
62+
4863// RunPrecompile runs and evaluate the output of a precompiled contract defined in contracts.go
4964func RunPrecompiledContract (p PrecompiledContract , input []byte , contract * Contract ) (ret []byte , err error ) {
5065 gas := p .RequiredGas (input )
@@ -132,3 +147,190 @@ func (c *dataCopy) RequiredGas(input []byte) uint64 {
132147func (c * dataCopy ) Run (in []byte ) ([]byte , error ) {
133148 return in , nil
134149}
150+
151+ // bigModexp implements a native big integer exponential modular operation.
152+ type bigModexp struct {}
153+
154+ // RequiredGas returns the gas required to execute the pre-compiled contract.
155+ //
156+ // This method does not require any overflow checking as the input size gas costs
157+ // required for anything significant is so high it's impossible to pay for.
158+ func (c * bigModexp ) RequiredGas (input []byte ) uint64 {
159+ // TODO reword required gas to have error reporting and convert arithmetic
160+ // to uint64.
161+ if len (input ) < 3 * 32 {
162+ input = append (input , make ([]byte , 3 * 32 - len (input ))... )
163+ }
164+ var (
165+ baseLen = new (big.Int ).SetBytes (input [:31 ])
166+ expLen = math .BigMax (new (big.Int ).SetBytes (input [32 :64 ]), big .NewInt (1 ))
167+ modLen = new (big.Int ).SetBytes (input [65 :97 ])
168+ )
169+ x := new (big.Int ).Set (math .BigMax (baseLen , modLen ))
170+ x .Mul (x , x )
171+ x .Mul (x , expLen )
172+ x .Div (x , new (big.Int ).SetUint64 (params .QuadCoeffDiv ))
173+
174+ return x .Uint64 ()
175+ }
176+
177+ func (c * bigModexp ) Run (input []byte ) ([]byte , error ) {
178+ if len (input ) < 3 * 32 {
179+ input = append (input , make ([]byte , 3 * 32 - len (input ))... )
180+ }
181+ // why 32-byte? These values won't fit anyway
182+ var (
183+ baseLen = new (big.Int ).SetBytes (input [:32 ]).Uint64 ()
184+ expLen = new (big.Int ).SetBytes (input [32 :64 ]).Uint64 ()
185+ modLen = new (big.Int ).SetBytes (input [64 :96 ]).Uint64 ()
186+ )
187+
188+ input = input [96 :]
189+ if uint64 (len (input )) < baseLen {
190+ input = append (input , make ([]byte , baseLen - uint64 (len (input )))... )
191+ }
192+ base := new (big.Int ).SetBytes (input [:baseLen ])
193+
194+ input = input [baseLen :]
195+ if uint64 (len (input )) < expLen {
196+ input = append (input , make ([]byte , expLen - uint64 (len (input )))... )
197+ }
198+ exp := new (big.Int ).SetBytes (input [:expLen ])
199+
200+ input = input [expLen :]
201+ if uint64 (len (input )) < modLen {
202+ input = append (input , make ([]byte , modLen - uint64 (len (input )))... )
203+ }
204+ mod := new (big.Int ).SetBytes (input [:modLen ])
205+
206+ return common .LeftPadBytes (base .Exp (base , exp , mod ).Bytes (), len (input [:modLen ])), nil
207+ }
208+
209+ type bn256Add struct {}
210+
211+ // RequiredGas returns the gas required to execute the pre-compiled contract.
212+ //
213+ // This method does not require any overflow checking as the input size gas costs
214+ // required for anything significant is so high it's impossible to pay for.
215+ func (c * bn256Add ) RequiredGas (input []byte ) uint64 {
216+ return 0 // TODO
217+ }
218+
219+ func (c * bn256Add ) Run (in []byte ) ([]byte , error ) {
220+ in = common .RightPadBytes (in , 128 )
221+
222+ x , onCurve := new (bn256.G1 ).Unmarshal (in [:64 ])
223+ if ! onCurve {
224+ return nil , errNotOnCurve
225+ }
226+ gx , gy , _ , _ := x .CurvePoints ()
227+ if gx .Cmp (bn256 .P ) >= 0 || gy .Cmp (bn256 .P ) >= 0 {
228+ return nil , errInvalidCurvePoint
229+ }
230+
231+ y , onCurve := new (bn256.G1 ).Unmarshal (in [64 :128 ])
232+ if ! onCurve {
233+ return nil , errNotOnCurve
234+ }
235+ gx , gy , _ , _ = y .CurvePoints ()
236+ if gx .Cmp (bn256 .P ) >= 0 || gy .Cmp (bn256 .P ) >= 0 {
237+ return nil , errInvalidCurvePoint
238+ }
239+ x .Add (x , y )
240+
241+ return x .Marshal (), nil
242+ }
243+
244+ type bn256ScalarMul struct {}
245+
246+ // RequiredGas returns the gas required to execute the pre-compiled contract.
247+ //
248+ // This method does not require any overflow checking as the input size gas costs
249+ // required for anything significant is so high it's impossible to pay for.
250+ func (c * bn256ScalarMul ) RequiredGas (input []byte ) uint64 {
251+ return 0 // TODO
252+ }
253+
254+ func (c * bn256ScalarMul ) Run (in []byte ) ([]byte , error ) {
255+ in = common .RightPadBytes (in , 96 )
256+
257+ g1 , onCurve := new (bn256.G1 ).Unmarshal (in [:64 ])
258+ if ! onCurve {
259+ return nil , errNotOnCurve
260+ }
261+ x , y , _ , _ := g1 .CurvePoints ()
262+ if x .Cmp (bn256 .P ) >= 0 || y .Cmp (bn256 .P ) >= 0 {
263+ return nil , errInvalidCurvePoint
264+ }
265+ g1 .ScalarMult (g1 , new (big.Int ).SetBytes (in [64 :96 ]))
266+
267+ return g1 .Marshal (), nil
268+ }
269+
270+ // pairing implements a pairing pre-compile for the bn256 curve
271+ type pairing struct {}
272+
273+ // RequiredGas returns the gas required to execute the pre-compiled contract.
274+ //
275+ // This method does not require any overflow checking as the input size gas costs
276+ // required for anything significant is so high it's impossible to pay for.
277+ func (c * pairing ) RequiredGas (input []byte ) uint64 {
278+ //return 0 // TODO
279+ k := (len (input ) + 191 ) / pairSize
280+ return uint64 (60000 * k + 40000 )
281+ }
282+
283+ const pairSize = 192
284+
285+ var (
286+ true32Byte = []byte {0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 }
287+ fals32Byte = make ([]byte , 32 )
288+ errNotOnCurve = errors .New ("point not on elliptic curve" )
289+ errInvalidCurvePoint = errors .New ("invalid elliptic curve point" )
290+ )
291+
292+ func (c * pairing ) Run (in []byte ) ([]byte , error ) {
293+ if len (in ) == 0 {
294+ return true32Byte , nil
295+ }
296+
297+ if len (in )% pairSize > 0 {
298+ return nil , errBadPrecompileInput
299+ }
300+
301+ var (
302+ g1s []* bn256.G1
303+ g2s []* bn256.G2
304+ )
305+ for i := 0 ; i < len (in ); i += pairSize {
306+ g1 , onCurve := new (bn256.G1 ).Unmarshal (in [i : i + 64 ])
307+ if ! onCurve {
308+ return nil , errNotOnCurve
309+ }
310+
311+ x , y , _ , _ := g1 .CurvePoints ()
312+ if x .Cmp (bn256 .P ) >= 0 || y .Cmp (bn256 .P ) >= 0 {
313+ return nil , errInvalidCurvePoint
314+ }
315+
316+ g2 , onCurve := new (bn256.G2 ).Unmarshal (in [i + 64 : i + 192 ])
317+ if ! onCurve {
318+ return nil , errNotOnCurve
319+ }
320+ x2 , y2 , _ , _ := g2 .CurvePoints ()
321+ if x2 .Real ().Cmp (bn256 .P ) >= 0 || x2 .Imag ().Cmp (bn256 .P ) >= 0 ||
322+ y2 .Real ().Cmp (bn256 .P ) >= 0 || y2 .Imag ().Cmp (bn256 .P ) >= 0 {
323+ return nil , errInvalidCurvePoint
324+ }
325+
326+ g1s = append (g1s , g1 )
327+ g2s = append (g2s , g2 )
328+ }
329+
330+ isOne := bn256 .PairingCheck (g1s , g2s )
331+ if isOne {
332+ return true32Byte , nil
333+ }
334+
335+ return fals32Byte , nil
336+ }
0 commit comments