From d1509f21b85e63cb012bc4d5d3f9a5a9a5546806 Mon Sep 17 00:00:00 2001
From: Andrew Ashikhmin <34320705+yperbasis@users.noreply.github.com>
Date: Mon, 8 Jun 2020 17:55:19 +0200
Subject: [PATCH] No-need to right-pad in makePush (#638)

---
 common/u256/big.go      | 34 ----------------------------------
 common/u256/big_test.go | 40 ----------------------------------------
 core/vm/instructions.go |  5 +++--
 3 files changed, 3 insertions(+), 76 deletions(-)
 delete mode 100644 common/u256/big_test.go

diff --git a/common/u256/big.go b/common/u256/big.go
index c43f2d896c..9e0b74c04c 100644
--- a/common/u256/big.go
+++ b/common/u256/big.go
@@ -29,37 +29,3 @@ var (
 	Num32 = uint256.NewInt().SetUint64(32)
 	Num35 = uint256.NewInt().SetUint64(35)
 )
-
-func SetBytes(z *uint256.Int, buf []byte, length int) *uint256.Int {
-	if len(buf) == length {
-		return z.SetBytes(buf)
-	} else {
-		return SetBytesRightPadded(z, buf, length)
-	}
-}
-
-func SetBytesRightPadded(z *uint256.Int, buf []byte, length int) *uint256.Int {
-	var d uint64
-	k := 0
-	s := uint64(0)
-	i := length
-	st := len(buf)
-	z[0], z[1], z[2], z[3] = 0, 0, 0, 0
-	for ; i > 0; i-- {
-		if i-1 < st {
-			d |= uint64(buf[i-1]) << s
-		}
-		if s += 8; s == 64 {
-			z[k] = d
-			k++
-			s, d = 0, 0
-			if k >= len(z) {
-				break
-			}
-		}
-	}
-	if k < len(z) {
-		z[k] = d
-	}
-	return z
-}
diff --git a/common/u256/big_test.go b/common/u256/big_test.go
deleted file mode 100644
index 2ee49f2583..0000000000
--- a/common/u256/big_test.go
+++ /dev/null
@@ -1,40 +0,0 @@
-package u256
-
-import (
-	"testing"
-
-	"github.com/davecgh/go-spew/spew"
-	"github.com/holiman/uint256"
-
-	"github.com/ledgerwatch/turbo-geth/common"
-)
-
-func TestRightPadding(t *testing.T) {
-	bs := []byte{0, 1, 0x10, 0x11, 0x20, 0xfa, 0xff}
-
-	for n := 1; n <= 4; n++ {
-		for l := n; l <= 4; l++ {
-			slice := make([]byte, n)
-			for _, b := range bs {
-				for i := range slice {
-					slice[i] = b
-
-					withPadding := setUint256WithPadding(slice, l)
-					withoutPadding := setUint256(slice, l)
-					if withoutPadding.Cmp(withPadding) != 0 {
-						t.Fatalf("not equal n=%d l=%d slice=%v\nGot\t\t%v\n!=\nExpect\t%v(%v)",
-							n, l, slice, withoutPadding.Bytes32(), withPadding.Bytes32(), spew.Sdump(withPadding))
-					}
-				}
-			}
-		}
-	}
-}
-
-func setUint256WithPadding(slice []byte, l int) *uint256.Int {
-	return new(uint256.Int).SetBytes(common.RightPadBytes(slice, l))
-}
-
-func setUint256(slice []byte, l int) *uint256.Int {
-	return SetBytesRightPadded(new(uint256.Int), slice, l)
-}
diff --git a/core/vm/instructions.go b/core/vm/instructions.go
index 4702016034..9d8824c86e 100644
--- a/core/vm/instructions.go
+++ b/core/vm/instructions.go
@@ -21,7 +21,6 @@ import (
 	"golang.org/x/crypto/sha3"
 
 	"github.com/ledgerwatch/turbo-geth/common"
-	"github.com/ledgerwatch/turbo-geth/common/u256"
 	"github.com/ledgerwatch/turbo-geth/core/types"
 	"github.com/ledgerwatch/turbo-geth/params"
 )
@@ -831,7 +830,9 @@ func makePush(size uint64, pushByteSize int) executionFunc {
 		}
 
 		integer := new(uint256.Int)
-		callContext.stack.Push(u256.SetBytes(integer, callContext.contract.Code[startMin:endMin], pushByteSize))
+		// No need to right-pad: if we're at the end of the code, the execution will stop.
+		// So it doesn't matter what we push onto the stack.
+		callContext.stack.Push(integer.SetBytes(callContext.contract.Code[startMin:endMin]))
 
 		*pc += size
 		return nil, nil
-- 
GitLab