Skip to content

Commit f8d8b56

Browse files
committed
core/vm: optimize copy-less data retrievals
1 parent d8aaa3a commit f8d8b56

4 files changed

Lines changed: 75 additions & 54 deletions

File tree

‎core/vm/common.go‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,21 @@ func calcMemSize(off, l *big.Int) *big.Int {
3434

3535
// getData returns a slice from the data based on the start and size and pads
3636
// up to size with zero's. This function is overflow safe.
37-
func getData(data []byte, start, size *big.Int) []byte {
37+
func getData(data []byte, start uint64, size uint64) []byte {
38+
length := uint64(len(data))
39+
if start > length {
40+
start = length
41+
}
42+
end := start + size
43+
if end > length {
44+
end = length
45+
}
46+
return common.RightPadBytes(data[start:end], int(size))
47+
}
48+
49+
// getDataBig returns a slice from the data based on the start and size and pads
50+
// up to size with zero's. This function is overflow safe.
51+
func getDataBig(data []byte, start *big.Int, size *big.Int) []byte {
3852
dlen := big.NewInt(int64(len(data)))
3953

4054
s := math.BigMin(start, dlen)

‎core/vm/contracts.go‎

Lines changed: 55 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -149,30 +149,41 @@ func (c *dataCopy) Run(in []byte) ([]byte, error) {
149149
// bigModExp implements a native big integer exponential modular operation.
150150
type 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.
153167
func (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

215226
func (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

288301
func (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

312322
func (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

‎core/vm/contracts_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ func TestPrecompiledBn256Pairing(t *testing.T) {
325325
}
326326

327327
// Behcnmarks the sample inputs from the elliptic curve pairing check EIP 197.
328-
func BenchmarkPrecompiledPairing(bench *testing.B) {
328+
func BenchmarkPrecompiledBn256Pairing(bench *testing.B) {
329329
for _, test := range bn256PairingTests {
330330
benchmarkPrecompiled("08", test, bench)
331331
}

‎core/vm/instructions.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ func opCallValue(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack
337337
}
338338

339339
func opCalldataLoad(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
340-
stack.push(new(big.Int).SetBytes(getData(contract.Input, stack.pop(), common.Big32)))
340+
stack.push(new(big.Int).SetBytes(getDataBig(contract.Input, stack.pop(), big32)))
341341
return nil, nil
342342
}
343343

@@ -352,7 +352,7 @@ func opCalldataCopy(pc *uint64, evm *EVM, contract *Contract, memory *Memory, st
352352
cOff = stack.pop()
353353
l = stack.pop()
354354
)
355-
memory.Set(mOff.Uint64(), l.Uint64(), getData(contract.Input, cOff, l))
355+
memory.Set(mOff.Uint64(), l.Uint64(), getDataBig(contract.Input, cOff, l))
356356

357357
evm.interpreter.intPool.put(mOff, cOff, l)
358358
return nil, nil
@@ -380,7 +380,7 @@ func opCodeCopy(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack
380380
cOff = stack.pop()
381381
l = stack.pop()
382382
)
383-
codeCopy := getData(contract.Code, cOff, l)
383+
codeCopy := getDataBig(contract.Code, cOff, l)
384384

385385
memory.Set(mOff.Uint64(), l.Uint64(), codeCopy)
386386

@@ -395,7 +395,7 @@ func opExtCodeCopy(pc *uint64, evm *EVM, contract *Contract, memory *Memory, sta
395395
cOff = stack.pop()
396396
l = stack.pop()
397397
)
398-
codeCopy := getData(evm.StateDB.GetCode(addr), cOff, l)
398+
codeCopy := getDataBig(evm.StateDB.GetCode(addr), cOff, l)
399399

400400
memory.Set(mOff.Uint64(), l.Uint64(), codeCopy)
401401

0 commit comments

Comments
 (0)