@@ -149,30 +149,41 @@ func (c *dataCopy) Run(in []byte) ([]byte, error) {
149149// bigModExp implements a native big integer exponential modular operation.
150150type bigModExp struct {}
151151
152+ var (
153+ big1 = big .NewInt (1 )
154+ big4 = big .NewInt (4 )
155+ big8 = big .NewInt (8 )
156+ big16 = big .NewInt (16 )
157+ big32 = big .NewInt (32 )
158+ big64 = big .NewInt (64 )
159+ big96 = big .NewInt (96 )
160+ big480 = big .NewInt (480 )
161+ big1024 = big .NewInt (1024 )
162+ big3072 = big .NewInt (3072 )
163+ big199680 = big .NewInt (199680 )
164+ )
165+
152166// RequiredGas returns the gas required to execute the pre-compiled contract.
153167func (c * bigModExp ) RequiredGas (input []byte ) uint64 {
154- // Pad the input with zeroes to the minimum size to read the field lengths
155- input = common .RightPadBytes (input , 96 )
156-
157168 var (
158- baseLen = new (big.Int ).SetBytes (input [: 32 ] )
159- expLen = new (big.Int ).SetBytes (input [ 32 : 64 ] )
160- modLen = new (big.Int ).SetBytes (input [ 64 : 96 ] )
169+ baseLen = new (big.Int ).SetBytes (getData ( input , 0 , 32 ) )
170+ expLen = new (big.Int ).SetBytes (getData ( input , 32 , 32 ) )
171+ modLen = new (big.Int ).SetBytes (getData ( input , 64 , 32 ) )
161172 )
162- input = input [96 :]
163-
173+ if len (input ) > 96 {
174+ input = input [96 :]
175+ } else {
176+ input = input [:0 ]
177+ }
164178 // Retrieve the head 32 bytes of exp for the adjusted exponent length
165179 var expHead * big.Int
166180 if big .NewInt (int64 (len (input ))).Cmp (baseLen ) <= 0 {
167181 expHead = new (big.Int )
168182 } else {
169- offset := int (baseLen .Uint64 ())
170-
171- input = common .RightPadBytes (input , offset + 32 )
172- if expLen .Cmp (big .NewInt (32 )) > 0 {
173- expHead = new (big.Int ).SetBytes (input [offset : offset + 32 ])
183+ if expLen .Cmp (big32 ) > 0 {
184+ expHead = new (big.Int ).SetBytes (getData (input , baseLen .Uint64 (), 32 ))
174185 } else {
175- expHead = new (big.Int ).SetBytes (input [ offset : offset + int ( expLen .Uint64 ())] )
186+ expHead = new (big.Int ).SetBytes (getData ( input , baseLen . Uint64 (), expLen .Uint64 ()))
176187 }
177188 }
178189 // Calculate the adjusted exponent length
@@ -181,29 +192,29 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 {
181192 msb = bitlen - 1
182193 }
183194 adjExpLen := new (big.Int )
184- if expLen .Cmp (big . NewInt ( 32 ) ) > 0 {
185- adjExpLen .Sub (expLen , big . NewInt ( 32 ) )
186- adjExpLen .Mul (big . NewInt ( 8 ) , adjExpLen )
195+ if expLen .Cmp (big32 ) > 0 {
196+ adjExpLen .Sub (expLen , big32 )
197+ adjExpLen .Mul (big8 , adjExpLen )
187198 }
188199 adjExpLen .Add (adjExpLen , big .NewInt (int64 (msb )))
189200
190201 // Calculate the gas cost of the operation
191202 gas := new (big.Int ).Set (math .BigMax (modLen , baseLen ))
192203 switch {
193- case gas .Cmp (big . NewInt ( 64 ) ) <= 0 :
204+ case gas .Cmp (big64 ) <= 0 :
194205 gas .Mul (gas , gas )
195- case gas .Cmp (big . NewInt ( 1024 ) ) <= 0 :
206+ case gas .Cmp (big1024 ) <= 0 :
196207 gas = new (big.Int ).Add (
197- new (big.Int ).Div (new (big.Int ).Mul (gas , gas ), big . NewInt ( 4 ) ),
198- new (big.Int ).Sub (new (big.Int ).Mul (big . NewInt ( 96 ) , gas ), big . NewInt ( 3072 ) ),
208+ new (big.Int ).Div (new (big.Int ).Mul (gas , gas ), big4 ),
209+ new (big.Int ).Sub (new (big.Int ).Mul (big96 , gas ), big3072 ),
199210 )
200211 default :
201212 gas = new (big.Int ).Add (
202- new (big.Int ).Div (new (big.Int ).Mul (gas , gas ), big . NewInt ( 16 ) ),
203- new (big.Int ).Sub (new (big.Int ).Mul (big . NewInt ( 480 ) , gas ), big . NewInt ( 199680 ) ),
213+ new (big.Int ).Div (new (big.Int ).Mul (gas , gas ), big16 ),
214+ new (big.Int ).Sub (new (big.Int ).Mul (big480 , gas ), big199680 ),
204215 )
205216 }
206- gas .Mul (gas , math .BigMax (adjExpLen , big . NewInt ( 1 ) ))
217+ gas .Mul (gas , math .BigMax (adjExpLen , big1 ))
207218 gas .Div (gas , new (big.Int ).SetUint64 (params .ModExpQuadCoeffDiv ))
208219
209220 if gas .BitLen () > 64 {
@@ -213,23 +224,25 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 {
213224}
214225
215226func (c * bigModExp ) Run (input []byte ) ([]byte , error ) {
216- // Pad the input with zeroes to the minimum size to read the field lengths
217- input = common .RightPadBytes (input , 96 )
218-
219227 var (
220- baseLen = new (big.Int ).SetBytes (input [: 32 ] ).Uint64 ()
221- expLen = new (big.Int ).SetBytes (input [ 32 : 64 ] ).Uint64 ()
222- modLen = new (big.Int ).SetBytes (input [ 64 : 96 ] ).Uint64 ()
228+ baseLen = new (big.Int ).SetBytes (getData ( input , 0 , 32 ) ).Uint64 ()
229+ expLen = new (big.Int ).SetBytes (getData ( input , 32 , 32 ) ).Uint64 ()
230+ modLen = new (big.Int ).SetBytes (getData ( input , 64 , 32 ) ).Uint64 ()
223231 )
224- input = input [96 :]
225-
226- // Pad the input with zeroes to the minimum size to read the field contents
227- input = common .RightPadBytes (input , int (baseLen + expLen + modLen ))
228-
232+ if len (input ) > 96 {
233+ input = input [96 :]
234+ } else {
235+ input = input [:0 ]
236+ }
237+ // Handle a special case when both the base and mod length is zero
238+ if baseLen == 0 && modLen == 0 {
239+ return []byte {}, nil
240+ }
241+ // Retrieve the operands and execute the exponentiation
229242 var (
230- base = new (big.Int ).SetBytes (input [: baseLen ] )
231- exp = new (big.Int ).SetBytes (input [ baseLen : baseLen + expLen ] )
232- mod = new (big.Int ).SetBytes (input [ baseLen + expLen : baseLen + expLen + modLen ] )
243+ base = new (big.Int ).SetBytes (getData ( input , 0 , baseLen ) )
244+ exp = new (big.Int ).SetBytes (getData ( input , baseLen , expLen ) )
245+ mod = new (big.Int ).SetBytes (getData ( input , baseLen + expLen , modLen ) )
233246 )
234247 if mod .BitLen () == 0 {
235248 // Modulo 0 is undefined, return zero
@@ -286,14 +299,11 @@ func (c *bn256Add) RequiredGas(input []byte) uint64 {
286299}
287300
288301func (c * bn256Add ) Run (input []byte ) ([]byte , error ) {
289- // Ensure we have enough data to operate on
290- input = common .RightPadBytes (input , 128 )
291-
292- x , err := newCurvePoint (input [:64 ])
302+ x , err := newCurvePoint (getData (input , 0 , 64 ))
293303 if err != nil {
294304 return nil , err
295305 }
296- y , err := newCurvePoint (input [ 64 : 128 ] )
306+ y , err := newCurvePoint (getData ( input , 64 , 64 ) )
297307 if err != nil {
298308 return nil , err
299309 }
@@ -310,14 +320,11 @@ func (c *bn256ScalarMul) RequiredGas(input []byte) uint64 {
310320}
311321
312322func (c * bn256ScalarMul ) Run (input []byte ) ([]byte , error ) {
313- // Ensure we have enough data to operate on
314- input = common .RightPadBytes (input , 96 )
315-
316- p , err := newCurvePoint (input [:64 ])
323+ p , err := newCurvePoint (getData (input , 0 , 64 ))
317324 if err != nil {
318325 return nil , err
319326 }
320- p .ScalarMult (p , new (big.Int ).SetBytes (input [ 64 : 96 ] ))
327+ p .ScalarMult (p , new (big.Int ).SetBytes (getData ( input , 64 , 32 ) ))
321328 return p .Marshal (), nil
322329}
323330
0 commit comments