common/math: delete some further dead code (#30639)
This commit is contained in:
parent
bb527b949a
commit
9015a05f31
@ -24,12 +24,9 @@ import (
|
|||||||
|
|
||||||
// Various big integer limit values.
|
// Various big integer limit values.
|
||||||
var (
|
var (
|
||||||
tt255 = BigPow(2, 255)
|
|
||||||
tt256 = BigPow(2, 256)
|
tt256 = BigPow(2, 256)
|
||||||
tt256m1 = new(big.Int).Sub(tt256, big.NewInt(1))
|
tt256m1 = new(big.Int).Sub(tt256, big.NewInt(1))
|
||||||
tt63 = BigPow(2, 63)
|
|
||||||
MaxBig256 = new(big.Int).Set(tt256m1)
|
MaxBig256 = new(big.Int).Set(tt256m1)
|
||||||
MaxBig63 = new(big.Int).Sub(tt63, big.NewInt(1))
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -162,16 +159,6 @@ func BigMin(x, y *big.Int) *big.Int {
|
|||||||
return x
|
return x
|
||||||
}
|
}
|
||||||
|
|
||||||
// FirstBitSet returns the index of the first 1 bit in v, counting from LSB.
|
|
||||||
func FirstBitSet(v *big.Int) int {
|
|
||||||
for i := 0; i < v.BitLen(); i++ {
|
|
||||||
if v.Bit(i) > 0 {
|
|
||||||
return i
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return v.BitLen()
|
|
||||||
}
|
|
||||||
|
|
||||||
// PaddedBigBytes encodes a big integer as a big-endian byte slice. The length
|
// PaddedBigBytes encodes a big integer as a big-endian byte slice. The length
|
||||||
// of the slice is at least n bytes.
|
// of the slice is at least n bytes.
|
||||||
func PaddedBigBytes(bigint *big.Int, n int) []byte {
|
func PaddedBigBytes(bigint *big.Int, n int) []byte {
|
||||||
@ -183,34 +170,6 @@ func PaddedBigBytes(bigint *big.Int, n int) []byte {
|
|||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
// bigEndianByteAt returns the byte at position n,
|
|
||||||
// in Big-Endian encoding
|
|
||||||
// So n==0 returns the least significant byte
|
|
||||||
func bigEndianByteAt(bigint *big.Int, n int) byte {
|
|
||||||
words := bigint.Bits()
|
|
||||||
// Check word-bucket the byte will reside in
|
|
||||||
i := n / wordBytes
|
|
||||||
if i >= len(words) {
|
|
||||||
return byte(0)
|
|
||||||
}
|
|
||||||
word := words[i]
|
|
||||||
// Offset of the byte
|
|
||||||
shift := 8 * uint(n%wordBytes)
|
|
||||||
|
|
||||||
return byte(word >> shift)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Byte returns the byte at position n,
|
|
||||||
// with the supplied padlength in Little-Endian encoding.
|
|
||||||
// n==0 returns the MSB
|
|
||||||
// Example: bigint '5', padlength 32, n=31 => 5
|
|
||||||
func Byte(bigint *big.Int, padlength, n int) byte {
|
|
||||||
if n >= padlength {
|
|
||||||
return byte(0)
|
|
||||||
}
|
|
||||||
return bigEndianByteAt(bigint, padlength-1-n)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadBits encodes the absolute value of bigint as big-endian bytes. Callers must ensure
|
// ReadBits encodes the absolute value of bigint as big-endian bytes. Callers must ensure
|
||||||
// that buf has enough space. If buf is too short the result will be incomplete.
|
// that buf has enough space. If buf is too short the result will be incomplete.
|
||||||
func ReadBits(bigint *big.Int, buf []byte) {
|
func ReadBits(bigint *big.Int, buf []byte) {
|
||||||
|
@ -21,8 +21,6 @@ import (
|
|||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"math/big"
|
"math/big"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestHexOrDecimal256(t *testing.T) {
|
func TestHexOrDecimal256(t *testing.T) {
|
||||||
@ -100,23 +98,6 @@ func TestBigMin(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFirstBigSet(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
num *big.Int
|
|
||||||
ix int
|
|
||||||
}{
|
|
||||||
{big.NewInt(0), 0},
|
|
||||||
{big.NewInt(1), 0},
|
|
||||||
{big.NewInt(2), 1},
|
|
||||||
{big.NewInt(0x100), 8},
|
|
||||||
}
|
|
||||||
for _, test := range tests {
|
|
||||||
if ix := FirstBitSet(test.num); ix != test.ix {
|
|
||||||
t.Errorf("FirstBitSet(b%b) = %d, want %d", test.num, ix, test.ix)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPaddedBigBytes(t *testing.T) {
|
func TestPaddedBigBytes(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
num *big.Int
|
num *big.Int
|
||||||
@ -220,60 +201,3 @@ func TestU256Bytes(t *testing.T) {
|
|||||||
t.Errorf("expected %x got %x", ubytes, unsigned)
|
t.Errorf("expected %x got %x", ubytes, unsigned)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBigEndianByteAt(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
x string
|
|
||||||
y int
|
|
||||||
exp byte
|
|
||||||
}{
|
|
||||||
{"00", 0, 0x00},
|
|
||||||
{"01", 1, 0x00},
|
|
||||||
{"00", 1, 0x00},
|
|
||||||
{"01", 0, 0x01},
|
|
||||||
{"0000000000000000000000000000000000000000000000000000000000102030", 0, 0x30},
|
|
||||||
{"0000000000000000000000000000000000000000000000000000000000102030", 1, 0x20},
|
|
||||||
{"ABCDEF0908070605040302010000000000000000000000000000000000000000", 31, 0xAB},
|
|
||||||
{"ABCDEF0908070605040302010000000000000000000000000000000000000000", 32, 0x00},
|
|
||||||
{"ABCDEF0908070605040302010000000000000000000000000000000000000000", 500, 0x00},
|
|
||||||
}
|
|
||||||
for _, test := range tests {
|
|
||||||
v := new(big.Int).SetBytes(common.Hex2Bytes(test.x))
|
|
||||||
actual := bigEndianByteAt(v, test.y)
|
|
||||||
if actual != test.exp {
|
|
||||||
t.Fatalf("Expected [%v] %v:th byte to be %v, was %v.", test.x, test.y, test.exp, actual)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
func TestLittleEndianByteAt(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
x string
|
|
||||||
y int
|
|
||||||
exp byte
|
|
||||||
}{
|
|
||||||
{"00", 0, 0x00},
|
|
||||||
{"01", 1, 0x00},
|
|
||||||
{"00", 1, 0x00},
|
|
||||||
{"01", 0, 0x00},
|
|
||||||
{"0000000000000000000000000000000000000000000000000000000000102030", 0, 0x00},
|
|
||||||
{"0000000000000000000000000000000000000000000000000000000000102030", 1, 0x00},
|
|
||||||
{"ABCDEF0908070605040302010000000000000000000000000000000000000000", 31, 0x00},
|
|
||||||
{"ABCDEF0908070605040302010000000000000000000000000000000000000000", 32, 0x00},
|
|
||||||
{"ABCDEF0908070605040302010000000000000000000000000000000000000000", 0, 0xAB},
|
|
||||||
{"ABCDEF0908070605040302010000000000000000000000000000000000000000", 1, 0xCD},
|
|
||||||
{"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", 0, 0x00},
|
|
||||||
{"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", 1, 0xCD},
|
|
||||||
{"0000000000000000000000000000000000000000000000000000000000102030", 31, 0x30},
|
|
||||||
{"0000000000000000000000000000000000000000000000000000000000102030", 30, 0x20},
|
|
||||||
{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 32, 0x0},
|
|
||||||
{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 31, 0xFF},
|
|
||||||
{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 0xFFFF, 0x0},
|
|
||||||
}
|
|
||||||
for _, test := range tests {
|
|
||||||
v := new(big.Int).SetBytes(common.Hex2Bytes(test.x))
|
|
||||||
actual := Byte(v, 32, test.y)
|
|
||||||
if actual != test.exp {
|
|
||||||
t.Fatalf("Expected [%v] %v:th byte to be %v, was %v.", test.x, test.y, test.exp, actual)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user