From e2931d25e6d934fe79f16e69adc6c1e1a5fb1851 Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Sun, 31 May 2020 23:37:51 +0200 Subject: [PATCH 1/4] conversion: length-specific setbyte methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit also improve SetBytes-methods via compiler hints name old time/op new time/op delta SetBytes/SetBytes-generic-6 1.34µs ± 1% 1.34µs ± 3% ~ (p=0.465 n=8+9) SetBytes/SetBytes-specific-6 178ns ± 5% 165ns ± 3% -7.39% (p=0.000 n=10+9) --- conversion.go | 271 ++++++++++++++++++++++++++++++++++++++++++++++++ uint256.go | 2 +- uint256_test.go | 184 +++++++++++++++++++++++++++++++- 3 files changed, 453 insertions(+), 4 deletions(-) diff --git a/conversion.go b/conversion.go index a3aa3859..01b4766b 100644 --- a/conversion.go +++ b/conversion.go @@ -5,6 +5,7 @@ package uint256 import ( + "encoding/binary" "fmt" "math/big" "math/bits" @@ -104,3 +105,273 @@ func (z *Int) SetFromBig(b *big.Int) bool { func (z *Int) Format(s fmt.State, ch rune) { z.ToBig().Format(s, ch) } + +// SetBytes8 is identical to SetBytes(in[:8]), but panics is input is too short +func (z *Int) SetBytes8(in []byte) *Int { + _ = in[7] // bounds check hint to compiler; see golang.org/issue/14808 + + z.SetUint64(binary.BigEndian.Uint64(in[0:8])) + return z +} + +// SetBytes16 is identical to SetBytes(in[:16]), but panics is input is too short +func (z *Int) SetBytes16(in []byte) *Int { + _ = in[15] // bounds check hint to compiler; see golang.org/issue/14808 + + z[3], z[2] = 0, 0 + z[1] = binary.BigEndian.Uint64(in[0:8]) + z[0] = binary.BigEndian.Uint64(in[8:16]) + return z +} + +// SetBytes16 is identical to SetBytes(in[:24]), but panics is input is too short +func (z *Int) SetBytes24(in []byte) *Int { + _ = in[23] // bounds check hint to compiler; see golang.org/issue/14808 + + z[3] = 0 + z[2] = binary.BigEndian.Uint64(in[0:8]) + z[1] = binary.BigEndian.Uint64(in[8:16]) + z[0] = binary.BigEndian.Uint64(in[16:24]) + return z +} + +func (z *Int) SetBytes32(in []byte) *Int { + _ = in[31] // bounds check hint to compiler; see golang.org/issue/14808 + + z[3] = binary.BigEndian.Uint64(in[0:8]) + z[2] = binary.BigEndian.Uint64(in[8:16]) + z[1] = binary.BigEndian.Uint64(in[16:24]) + z[0] = binary.BigEndian.Uint64(in[24:32]) + return z +} + +func (z *Int) SetBytes1(in []byte) *Int { + z.SetUint64(uint64(in[0])) + return z +} + +func (z *Int) SetBytes9(in []byte) *Int { + _ = in[8] // bounds check hint to compiler; see golang.org/issue/14808 + + z.SetBytes8(in[1:]) + z[1] = uint64(in[0]) + return z +} + +func (z *Int) SetBytes17(in []byte) *Int { + _ = in[16] // bounds check hint to compiler; see golang.org/issue/14808 + + z.SetBytes16(in[1:]) + z[2] = uint64(in[0]) + return z +} + +func (z *Int) SetBytes25(in []byte) *Int { + _ = in[24] // bounds check hint to compiler; see golang.org/issue/14808 + + z.SetBytes24(in[1:]) + z[3] = uint64(in[0]) + return z +} + +func (z *Int) SetBytes2(in []byte) *Int { + _ = in[1] // bounds check hint to compiler; see golang.org/issue/14808 + + z.SetUint64(uint64(binary.BigEndian.Uint16(in[0:2]))) + return z +} + +func (z *Int) SetBytes10(in []byte) *Int { + _ = in[9] // bounds check hint to compiler; see golang.org/issue/14808 + + z.SetBytes8(in[2:]) + z[1] = uint64(binary.BigEndian.Uint16(in[0:2])) + return z +} + +func (z *Int) SetBytes18(in []byte) *Int { + _ = in[17] // bounds check hint to compiler; see golang.org/issue/14808 + + z.SetBytes16(in[2:]) + z[2] = uint64(binary.BigEndian.Uint16(in[0:2])) + return z +} + +func (z *Int) SetBytes26(in []byte) *Int { + _ = in[25] // bounds check hint to compiler; see golang.org/issue/14808 + + z.SetBytes24(in[2:]) + z[3] = uint64(binary.BigEndian.Uint16(in[0:2])) + return z +} + +func (z *Int) SetBytes3(in []byte) *Int { + _ = in[2] // bounds check hint to compiler; see golang.org/issue/14808 + z.SetUint64(uint64(binary.BigEndian.Uint16(in[1:3])) | uint64(in[0])<<16) + return z +} + +func (z *Int) SetBytes11(in []byte) *Int { + _ = in[10] // bounds check hint to compiler; see golang.org/issue/14808 + z.SetBytes8(in[3:]) + z[1] = uint64(binary.BigEndian.Uint16(in[1:3])) | uint64(in[0])<<16 + return z +} + +func (z *Int) SetBytes19(in []byte) *Int { + _ = in[18] // bounds check hint to compiler; see golang.org/issue/14808 + + z.SetBytes16(in[3:]) + z[2] = uint64(binary.BigEndian.Uint16(in[1:3])) | uint64(in[0])<<16 + return z +} + +func (z *Int) SetBytes27(in []byte) *Int { + _ = in[26] // bounds check hint to compiler; see golang.org/issue/14808 + + z.SetBytes24(in[3:]) + z[3] = uint64(binary.BigEndian.Uint16(in[1:3])) | uint64(in[0])<<16 + return z +} + +func (z *Int) SetBytes4(in []byte) *Int { + _ = in[3] // bounds check hint to compiler; see golang.org/issue/14808 + + z.SetUint64(uint64(binary.BigEndian.Uint32(in[0:4]))) + return z +} + +func (z *Int) SetBytes12(in []byte) *Int { + _ = in[11] // bounds check hint to compiler; see golang.org/issue/14808 + + z.SetBytes8(in[4:]) + z[1] = uint64(binary.BigEndian.Uint32(in[0:4])) + return z +} + +func (z *Int) SetBytes20(in []byte) *Int { + _ = in[19] // bounds check hint to compiler; see golang.org/issue/14808 + + z.SetBytes16(in[4:]) + z[2] = uint64(binary.BigEndian.Uint32(in[0:4])) + return z +} + +func (z *Int) SetBytes28(in []byte) *Int { + _ = in[27] // bounds check hint to compiler; see golang.org/issue/14808 + + z.SetBytes24(in[4:]) + z[3] = uint64(binary.BigEndian.Uint32(in[0:4])) + return z +} + +func (z *Int) SetBytes5(in []byte) *Int { + _ = in[4] // bounds check hint to compiler; see golang.org/issue/14808 + + z.SetUint64(bigEndianUint40(in[0:5])) + return z +} + +func (z *Int) SetBytes13(in []byte) *Int { + z.SetBytes8(in[5:]) + z[1] = bigEndianUint40(in[0:5]) + return z +} + +func (z *Int) SetBytes21(in []byte) *Int { + _ = in[20] // bounds check hint to compiler; see golang.org/issue/14808 + + z.SetBytes16(in[5:]) + z[2] = bigEndianUint40(in[0:5]) + return z +} + +func (z *Int) SetBytes29(in []byte) *Int { + _ = in[23] // bounds check hint to compiler; see golang.org/issue/14808 + + z.SetBytes24(in[5:]) + z[3] = bigEndianUint40(in[0:5]) + return z +} + +func (z *Int) SetBytes6(in []byte) *Int { + _ = in[5] // bounds check hint to compiler; see golang.org/issue/14808 + + z.SetUint64(bigEndianUint48(in[0:6])) + return z +} + +func (z *Int) SetBytes14(in []byte) *Int { + _ = in[13] // bounds check hint to compiler; see golang.org/issue/14808 + + z.SetBytes8(in[6:]) + z[1] = bigEndianUint48(in[0:6]) + return z +} + +func (z *Int) SetBytes22(in []byte) *Int { + _ = in[21] // bounds check hint to compiler; see golang.org/issue/14808 + + z.SetBytes16(in[6:]) + z[2] = bigEndianUint48(in[0:6]) + return z +} + +func (z *Int) SetBytes30(in []byte) *Int { + _ = in[29] // bounds check hint to compiler; see golang.org/issue/14808 + + z.SetBytes24(in[6:]) + z[3] = bigEndianUint48(in[0:6]) + return z +} + +func (z *Int) SetBytes7(in []byte) *Int { + _ = in[6] // bounds check hint to compiler; see golang.org/issue/14808 + + z.SetUint64(bigEndianUint56(in[0:7])) + return z +} + +func (z *Int) SetBytes15(in []byte) *Int { + _ = in[14] // bounds check hint to compiler; see golang.org/issue/14808 + + z.SetBytes8(in[7:]) + z[1] = bigEndianUint56(in[0:7]) + return z +} + +func (z *Int) SetBytes23(in []byte) *Int { + _ = in[22] // bounds check hint to compiler; see golang.org/issue/14808 + + z.SetBytes16(in[7:]) + z[2] = bigEndianUint56(in[0:7]) + return z +} + +func (z *Int) SetBytes31(in []byte) *Int { + _ = in[30] // bounds check hint to compiler; see golang.org/issue/14808 + + z.SetBytes24(in[7:]) + z[3] = bigEndianUint56(in[0:7]) + return z +} + +// Utility methods that are "missing" among the bigEndian.UintXX methods. + +func bigEndianUint40(b []byte) uint64 { + _ = b[4] // bounds check hint to compiler; see golang.org/issue/14808 + return uint64(b[4]) | uint64(b[3])<<8 | uint64(b[2])<<16 | uint64(b[1])<<24 | + uint64(b[0])<<32 +} + +func bigEndianUint48(b []byte) uint64 { + _ = b[5] // bounds check hint to compiler; see golang.org/issue/14808 + return uint64(b[5]) | uint64(b[4])<<8 | uint64(b[3])<<16 | uint64(b[2])<<24 | + uint64(b[1])<<32 | uint64(b[0])<<40 +} + +func bigEndianUint56(b []byte) uint64 { + _ = b[6] // bounds check hint to compiler; see golang.org/issue/14808 + return uint64(b[6]) | uint64(b[5])<<8 | uint64(b[4])<<16 | uint64(b[3])<<24 | + uint64(b[2])<<32 | uint64(b[1])<<40 | uint64(b[0])<<48 +} diff --git a/uint256.go b/uint256.go index 564fc1d8..be704249 100644 --- a/uint256.go +++ b/uint256.go @@ -111,7 +111,7 @@ func (z *Int) Uint64() uint64 { // Uint64WithOverflow returns the lower 64-bits of z and bool whether overflow occurred func (z *Int) Uint64WithOverflow() (uint64, bool) { - return z[0], z[1] != 0 || z[2] != 0 || z[3] != 0 + return z[0], (z[1] | z[2] | z[3]) != 0 } // Clone creates a new Int identical to z diff --git a/uint256_test.go b/uint256_test.go index 58f3111b..f3b1277b 100644 --- a/uint256_test.go +++ b/uint256_test.go @@ -941,9 +941,15 @@ func TestCmpOp(t *testing.T) { t.Run("Gt", func(t *testing.T) { proc(t, (*Int).Gt, func(a, b *big.Int) bool { return a.Cmp(b) > 0 }) }) t.Run("SLt", func(t *testing.T) { proc(t, (*Int).Slt, func(a, b *big.Int) bool { return S256(a).Cmp(S256(b)) < 0 }) }) t.Run("SGt", func(t *testing.T) { proc(t, (*Int).Sgt, func(a, b *big.Int) bool { return S256(a).Cmp(S256(b)) > 0 }) }) - t.Run("CmpEq", func(t *testing.T) { proc(t, func(a, b *Int) bool { return a.Cmp(b) == 0 }, func(a, b *big.Int) bool { return a.Cmp(b) == 0 }) }) - t.Run("CmpLt", func(t *testing.T) { proc(t, func(a, b *Int) bool { return a.Cmp(b) < 0 }, func(a, b *big.Int) bool { return a.Cmp(b) < 0 }) }) - t.Run("CmpGt", func(t *testing.T) { proc(t, func(a, b *Int) bool { return a.Cmp(b) > 0 }, func(a, b *big.Int) bool { return a.Cmp(b) > 0 }) }) + t.Run("CmpEq", func(t *testing.T) { + proc(t, func(a, b *Int) bool { return a.Cmp(b) == 0 }, func(a, b *big.Int) bool { return a.Cmp(b) == 0 }) + }) + t.Run("CmpLt", func(t *testing.T) { + proc(t, func(a, b *Int) bool { return a.Cmp(b) < 0 }, func(a, b *big.Int) bool { return a.Cmp(b) < 0 }) + }) + t.Run("CmpGt", func(t *testing.T) { + proc(t, func(a, b *Int) bool { return a.Cmp(b) > 0 }, func(a, b *big.Int) bool { return a.Cmp(b) > 0 }) + }) t.Run("LtUint64", func(t *testing.T) { proc(t, func(a, b *Int) bool { @@ -1184,3 +1190,175 @@ func TestByte32Representation(t *testing.T) { } } } + +func TestSetBytes(t *testing.T) { + for i := 0; i < 33; i++ { + bytearr := hex2Bytes("12131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031") + a := big.NewInt(0).SetBytes(bytearr[0:i]) + exp := bytesToHash(a.Bytes()) + + b := NewInt().SetAllOne().SetBytes(bytearr[0:i]) + + got := gethHash(b.Bytes32()) + // uint256.Int -> address + if got != exp { + t.Errorf("testcase %d: got %x exp %x", i, got, exp) + } + b.SetAllOne() + switch i { + case 1: + b.SetBytes1(bytearr) + case 2: + b.SetBytes2(bytearr) + case 3: + b.SetBytes3(bytearr) + case 4: + b.SetBytes4(bytearr) + case 5: + b.SetBytes5(bytearr) + case 6: + b.SetBytes6(bytearr) + case 7: + b.SetBytes7(bytearr) + case 8: + b.SetBytes8(bytearr) + case 9: + b.SetBytes9(bytearr) + case 10: + b.SetBytes10(bytearr) + case 11: + b.SetBytes11(bytearr) + case 12: + b.SetBytes12(bytearr) + case 13: + b.SetBytes13(bytearr) + case 14: + b.SetBytes14(bytearr) + case 15: + b.SetBytes15(bytearr) + case 16: + b.SetBytes16(bytearr) + case 17: + b.SetBytes17(bytearr) + case 18: + b.SetBytes18(bytearr) + case 19: + b.SetBytes19(bytearr) + case 20: + b.SetBytes20(bytearr) + case 21: + b.SetBytes21(bytearr) + case 22: + b.SetBytes22(bytearr) + case 23: + b.SetBytes23(bytearr) + case 24: + b.SetBytes24(bytearr) + case 25: + b.SetBytes25(bytearr) + case 26: + b.SetBytes26(bytearr) + case 27: + b.SetBytes27(bytearr) + case 28: + b.SetBytes28(bytearr) + case 29: + b.SetBytes29(bytearr) + case 30: + b.SetBytes30(bytearr) + case 31: + b.SetBytes31(bytearr) + case 32: + b.SetBytes32(bytearr) + default: + continue + } + got = gethHash(b.Bytes32()) + // uint256.Int -> address + if got != exp { + t.Errorf("testcase %d: got %x exp %x", i, got, exp) + } + } +} + +func BenchmarkSetBytes(b *testing.B) { + + val := NewInt() + bytearr := hex2Bytes("12131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031") + b.Run("SetBytes-generic", func(b *testing.B) { + b.ReportAllocs() + b.ResetTimer() + for i := 0; i < b.N; i++ { + val.SetBytes(bytearr[:1]) + val.SetBytes(bytearr[:2]) + val.SetBytes(bytearr[:3]) + val.SetBytes(bytearr[:4]) + val.SetBytes(bytearr[:5]) + val.SetBytes(bytearr[:6]) + val.SetBytes(bytearr[:7]) + val.SetBytes(bytearr[:8]) + val.SetBytes(bytearr[:9]) + val.SetBytes(bytearr[:10]) + val.SetBytes(bytearr[:11]) + val.SetBytes(bytearr[:12]) + val.SetBytes(bytearr[:13]) + val.SetBytes(bytearr[:14]) + val.SetBytes(bytearr[:15]) + val.SetBytes(bytearr[:16]) + val.SetBytes(bytearr[:17]) + val.SetBytes(bytearr[:18]) + val.SetBytes(bytearr[:19]) + val.SetBytes(bytearr[:20]) + val.SetBytes(bytearr[:21]) + val.SetBytes(bytearr[:22]) + val.SetBytes(bytearr[:23]) + val.SetBytes(bytearr[:24]) + val.SetBytes(bytearr[:25]) + val.SetBytes(bytearr[:26]) + val.SetBytes(bytearr[:27]) + val.SetBytes(bytearr[:28]) + val.SetBytes(bytearr[:29]) + val.SetBytes(bytearr[:20]) + val.SetBytes(bytearr[:31]) + val.SetBytes(bytearr[:32]) + } + }) + b.Run("SetBytes-specific", func(b *testing.B) { + b.ReportAllocs() + b.ResetTimer() + for i := 0; i < b.N; i++ { + val.SetBytes1(bytearr) + val.SetBytes2(bytearr) + val.SetBytes3(bytearr) + val.SetBytes4(bytearr) + val.SetBytes5(bytearr) + val.SetBytes6(bytearr) + val.SetBytes7(bytearr) + val.SetBytes8(bytearr) + val.SetBytes9(bytearr) + val.SetBytes10(bytearr) + val.SetBytes11(bytearr) + val.SetBytes12(bytearr) + val.SetBytes13(bytearr) + val.SetBytes14(bytearr) + val.SetBytes15(bytearr) + val.SetBytes16(bytearr) + val.SetBytes17(bytearr) + val.SetBytes18(bytearr) + val.SetBytes19(bytearr) + val.SetBytes20(bytearr) + val.SetBytes21(bytearr) + val.SetBytes22(bytearr) + val.SetBytes23(bytearr) + val.SetBytes24(bytearr) + val.SetBytes25(bytearr) + val.SetBytes26(bytearr) + val.SetBytes27(bytearr) + val.SetBytes28(bytearr) + val.SetBytes29(bytearr) + val.SetBytes30(bytearr) + val.SetBytes31(bytearr) + val.SetBytes32(bytearr) + } + }) +} From ce6a5d54e1c701105c835cc0138712ffd5b3f05f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Thu, 4 Jun 2020 16:47:25 +0200 Subject: [PATCH 2/4] Inline implementations of SetBytesN --- conversion.go | 120 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 90 insertions(+), 30 deletions(-) diff --git a/conversion.go b/conversion.go index 01b4766b..726dd6fa 100644 --- a/conversion.go +++ b/conversion.go @@ -110,7 +110,8 @@ func (z *Int) Format(s fmt.State, ch rune) { func (z *Int) SetBytes8(in []byte) *Int { _ = in[7] // bounds check hint to compiler; see golang.org/issue/14808 - z.SetUint64(binary.BigEndian.Uint64(in[0:8])) + z[3], z[2], z[1] = 0, 0, 0 + z[0] = binary.BigEndian.Uint64(in[0:8]) return z } @@ -146,213 +147,272 @@ func (z *Int) SetBytes32(in []byte) *Int { } func (z *Int) SetBytes1(in []byte) *Int { - z.SetUint64(uint64(in[0])) + z[3], z[2], z[1], z[0] = 0, 0, 0, uint64(in[0]) return z } func (z *Int) SetBytes9(in []byte) *Int { _ = in[8] // bounds check hint to compiler; see golang.org/issue/14808 - z.SetBytes8(in[1:]) - z[1] = uint64(in[0]) + z[3], z[2], z[1], z[0] = 0, 0, uint64(in[0]), binary.BigEndian.Uint64(in[1:9]) return z } func (z *Int) SetBytes17(in []byte) *Int { _ = in[16] // bounds check hint to compiler; see golang.org/issue/14808 - z.SetBytes16(in[1:]) + z[3] = 0 z[2] = uint64(in[0]) + z[1] = binary.BigEndian.Uint64(in[1:9]) + z[0] = binary.BigEndian.Uint64(in[9:17]) return z } func (z *Int) SetBytes25(in []byte) *Int { _ = in[24] // bounds check hint to compiler; see golang.org/issue/14808 - z.SetBytes24(in[1:]) z[3] = uint64(in[0]) + z[2] = binary.BigEndian.Uint64(in[1:9]) + z[1] = binary.BigEndian.Uint64(in[9:17]) + z[0] = binary.BigEndian.Uint64(in[17:25]) return z } func (z *Int) SetBytes2(in []byte) *Int { _ = in[1] // bounds check hint to compiler; see golang.org/issue/14808 - z.SetUint64(uint64(binary.BigEndian.Uint16(in[0:2]))) + z[3] = 0 + z[2] = 0 + z[1] = 0 + z[0] = uint64(binary.BigEndian.Uint16(in[0:2])) return z } func (z *Int) SetBytes10(in []byte) *Int { _ = in[9] // bounds check hint to compiler; see golang.org/issue/14808 - z.SetBytes8(in[2:]) + z[3] = 0 + z[2] = 0 z[1] = uint64(binary.BigEndian.Uint16(in[0:2])) + z[0] = binary.BigEndian.Uint64(in[2:10]) return z } func (z *Int) SetBytes18(in []byte) *Int { _ = in[17] // bounds check hint to compiler; see golang.org/issue/14808 - z.SetBytes16(in[2:]) + z[3] = 0 z[2] = uint64(binary.BigEndian.Uint16(in[0:2])) + z[1] = binary.BigEndian.Uint64(in[2:10]) + z[0] = binary.BigEndian.Uint64(in[10:18]) return z } func (z *Int) SetBytes26(in []byte) *Int { _ = in[25] // bounds check hint to compiler; see golang.org/issue/14808 - z.SetBytes24(in[2:]) z[3] = uint64(binary.BigEndian.Uint16(in[0:2])) + z[2] = binary.BigEndian.Uint64(in[2:10]) + z[1] = binary.BigEndian.Uint64(in[10:18]) + z[0] = binary.BigEndian.Uint64(in[18:26]) return z } func (z *Int) SetBytes3(in []byte) *Int { _ = in[2] // bounds check hint to compiler; see golang.org/issue/14808 - z.SetUint64(uint64(binary.BigEndian.Uint16(in[1:3])) | uint64(in[0])<<16) + z[3] = 0 + z[2] = 0 + z[1] = 0 + z[0] = uint64(binary.BigEndian.Uint16(in[1:3])) | uint64(in[0])<<16 return z } func (z *Int) SetBytes11(in []byte) *Int { _ = in[10] // bounds check hint to compiler; see golang.org/issue/14808 - z.SetBytes8(in[3:]) + z[3] = 0 + z[2] = 0 z[1] = uint64(binary.BigEndian.Uint16(in[1:3])) | uint64(in[0])<<16 + z[0] = binary.BigEndian.Uint64(in[3:11]) return z } func (z *Int) SetBytes19(in []byte) *Int { _ = in[18] // bounds check hint to compiler; see golang.org/issue/14808 - z.SetBytes16(in[3:]) + z[3] = 0 z[2] = uint64(binary.BigEndian.Uint16(in[1:3])) | uint64(in[0])<<16 + z[1] = binary.BigEndian.Uint64(in[3:11]) + z[0] = binary.BigEndian.Uint64(in[11:19]) return z } func (z *Int) SetBytes27(in []byte) *Int { _ = in[26] // bounds check hint to compiler; see golang.org/issue/14808 - z.SetBytes24(in[3:]) z[3] = uint64(binary.BigEndian.Uint16(in[1:3])) | uint64(in[0])<<16 + z[2] = binary.BigEndian.Uint64(in[3:11]) + z[1] = binary.BigEndian.Uint64(in[11:19]) + z[0] = binary.BigEndian.Uint64(in[19:27]) return z } func (z *Int) SetBytes4(in []byte) *Int { _ = in[3] // bounds check hint to compiler; see golang.org/issue/14808 - z.SetUint64(uint64(binary.BigEndian.Uint32(in[0:4]))) + z[3] = 0 + z[2] = 0 + z[1] = 0 + z[0] = uint64(binary.BigEndian.Uint32(in[0:4])) return z } func (z *Int) SetBytes12(in []byte) *Int { _ = in[11] // bounds check hint to compiler; see golang.org/issue/14808 - z.SetBytes8(in[4:]) + z[3] = 0 + z[2] = 0 z[1] = uint64(binary.BigEndian.Uint32(in[0:4])) + z[0] = binary.BigEndian.Uint64(in[4:12]) return z } func (z *Int) SetBytes20(in []byte) *Int { _ = in[19] // bounds check hint to compiler; see golang.org/issue/14808 - z.SetBytes16(in[4:]) + z[3] = 0 z[2] = uint64(binary.BigEndian.Uint32(in[0:4])) + z[1] = binary.BigEndian.Uint64(in[4:12]) + z[0] = binary.BigEndian.Uint64(in[12:20]) return z } func (z *Int) SetBytes28(in []byte) *Int { _ = in[27] // bounds check hint to compiler; see golang.org/issue/14808 - z.SetBytes24(in[4:]) z[3] = uint64(binary.BigEndian.Uint32(in[0:4])) + z[2] = binary.BigEndian.Uint64(in[4:12]) + z[1] = binary.BigEndian.Uint64(in[12:20]) + z[0] = binary.BigEndian.Uint64(in[20:28]) return z } func (z *Int) SetBytes5(in []byte) *Int { _ = in[4] // bounds check hint to compiler; see golang.org/issue/14808 - z.SetUint64(bigEndianUint40(in[0:5])) + z[3] = 0 + z[2] = 0 + z[1] = 0 + z[0] = bigEndianUint40(in[0:5]) return z } func (z *Int) SetBytes13(in []byte) *Int { - z.SetBytes8(in[5:]) + _ = in[12] // bounds check hint to compiler; see golang.org/issue/14808 + + z[3] = 0 + z[2] = 0 z[1] = bigEndianUint40(in[0:5]) + z[0] = binary.BigEndian.Uint64(in[5:13]) return z } func (z *Int) SetBytes21(in []byte) *Int { _ = in[20] // bounds check hint to compiler; see golang.org/issue/14808 - z.SetBytes16(in[5:]) + z[3] = 0 z[2] = bigEndianUint40(in[0:5]) + z[1] = binary.BigEndian.Uint64(in[5:13]) + z[0] = binary.BigEndian.Uint64(in[13:21]) return z } func (z *Int) SetBytes29(in []byte) *Int { _ = in[23] // bounds check hint to compiler; see golang.org/issue/14808 - z.SetBytes24(in[5:]) z[3] = bigEndianUint40(in[0:5]) + z[2] = binary.BigEndian.Uint64(in[5:13]) + z[1] = binary.BigEndian.Uint64(in[13:21]) + z[0] = binary.BigEndian.Uint64(in[21:29]) return z } func (z *Int) SetBytes6(in []byte) *Int { _ = in[5] // bounds check hint to compiler; see golang.org/issue/14808 - z.SetUint64(bigEndianUint48(in[0:6])) + z[3] = 0 + z[2] = 0 + z[1] = 0 + z[0] = bigEndianUint48(in[0:6]) return z } func (z *Int) SetBytes14(in []byte) *Int { _ = in[13] // bounds check hint to compiler; see golang.org/issue/14808 - z.SetBytes8(in[6:]) + z[3] = 0 + z[2] = 0 z[1] = bigEndianUint48(in[0:6]) + z[0] = binary.BigEndian.Uint64(in[6:14]) return z } func (z *Int) SetBytes22(in []byte) *Int { _ = in[21] // bounds check hint to compiler; see golang.org/issue/14808 - z.SetBytes16(in[6:]) + z[3] = 0 z[2] = bigEndianUint48(in[0:6]) + z[1] = binary.BigEndian.Uint64(in[6:14]) + z[0] = binary.BigEndian.Uint64(in[14:22]) return z } func (z *Int) SetBytes30(in []byte) *Int { _ = in[29] // bounds check hint to compiler; see golang.org/issue/14808 - z.SetBytes24(in[6:]) z[3] = bigEndianUint48(in[0:6]) + z[2] = binary.BigEndian.Uint64(in[6:14]) + z[1] = binary.BigEndian.Uint64(in[14:22]) + z[0] = binary.BigEndian.Uint64(in[22:30]) return z } func (z *Int) SetBytes7(in []byte) *Int { _ = in[6] // bounds check hint to compiler; see golang.org/issue/14808 - z.SetUint64(bigEndianUint56(in[0:7])) + z[3] = 0 + z[2] = 0 + z[1] = 0 + z[0] = bigEndianUint56(in[0:7]) return z } func (z *Int) SetBytes15(in []byte) *Int { _ = in[14] // bounds check hint to compiler; see golang.org/issue/14808 - z.SetBytes8(in[7:]) + z[3] = 0 + z[2] = 0 z[1] = bigEndianUint56(in[0:7]) + z[0] = binary.BigEndian.Uint64(in[7:15]) return z } func (z *Int) SetBytes23(in []byte) *Int { _ = in[22] // bounds check hint to compiler; see golang.org/issue/14808 - z.SetBytes16(in[7:]) + z[3] = 0 z[2] = bigEndianUint56(in[0:7]) + z[1] = binary.BigEndian.Uint64(in[7:15]) + z[0] = binary.BigEndian.Uint64(in[15:23]) return z } func (z *Int) SetBytes31(in []byte) *Int { _ = in[30] // bounds check hint to compiler; see golang.org/issue/14808 - z.SetBytes24(in[7:]) z[3] = bigEndianUint56(in[0:7]) + z[2] = binary.BigEndian.Uint64(in[7:15]) + z[1] = binary.BigEndian.Uint64(in[15:23]) + z[0] = binary.BigEndian.Uint64(in[23:31]) return z } From 052e2924e4f4ef15f721b2b0d943d32b35207b53 Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Thu, 4 Jun 2020 18:35:16 +0200 Subject: [PATCH 3/4] review concerns + move tests to conversion_tests + generic dispatcher --- conversion_test.go | 102 +++++++++++++++++++++++++++ uint256.go | 87 ++++++++++++++++++----- uint256_test.go | 172 --------------------------------------------- 3 files changed, 171 insertions(+), 190 deletions(-) diff --git a/conversion_test.go b/conversion_test.go index a95026bc..5cc1d5bd 100644 --- a/conversion_test.go +++ b/conversion_test.go @@ -174,3 +174,105 @@ func TestFormat(t *testing.T) { } } } + +// TestSetBytes tests all setbyte-methods from 0 to overlong, +// - verifies that all non-set bits are properly cleared +// - verifies that overlong input is correctly cropped +func TestSetBytes(t *testing.T) { + for i := 0; i < 35; i++ { + buf := hex2Bytes("aaaa12131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031bbbb") + exp, _ := FromBig(new(big.Int).SetBytes(buf[0:i])) + z := NewInt().SetAllOne().SetBytes(buf[0:i]) + if !z.Eq(exp) { + t.Errorf("testcase %d: exp %x, got %x", i, exp, z) + } + } + // nil check + exp, _ := FromBig(new(big.Int).SetBytes(nil)) + z := NewInt().SetAllOne().SetBytes(nil) + if !z.Eq(exp) { + t.Errorf("nil-test : exp %x, got %x", exp, z) + } +} + +func BenchmarkSetBytes(b *testing.B) { + + val := NewInt() + bytearr := hex2Bytes("12131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031") + b.Run("generic", func(b *testing.B) { + b.ReportAllocs() + b.ResetTimer() + for i := 0; i < b.N; i++ { + val.SetBytes(bytearr[:1]) + val.SetBytes(bytearr[:2]) + val.SetBytes(bytearr[:3]) + val.SetBytes(bytearr[:4]) + val.SetBytes(bytearr[:5]) + val.SetBytes(bytearr[:6]) + val.SetBytes(bytearr[:7]) + val.SetBytes(bytearr[:8]) + val.SetBytes(bytearr[:9]) + val.SetBytes(bytearr[:10]) + val.SetBytes(bytearr[:11]) + val.SetBytes(bytearr[:12]) + val.SetBytes(bytearr[:13]) + val.SetBytes(bytearr[:14]) + val.SetBytes(bytearr[:15]) + val.SetBytes(bytearr[:16]) + val.SetBytes(bytearr[:17]) + val.SetBytes(bytearr[:18]) + val.SetBytes(bytearr[:19]) + val.SetBytes(bytearr[:20]) + val.SetBytes(bytearr[:21]) + val.SetBytes(bytearr[:22]) + val.SetBytes(bytearr[:23]) + val.SetBytes(bytearr[:24]) + val.SetBytes(bytearr[:25]) + val.SetBytes(bytearr[:26]) + val.SetBytes(bytearr[:27]) + val.SetBytes(bytearr[:28]) + val.SetBytes(bytearr[:29]) + val.SetBytes(bytearr[:20]) + val.SetBytes(bytearr[:31]) + val.SetBytes(bytearr[:32]) + } + }) + b.Run("specific", func(b *testing.B) { + b.ReportAllocs() + b.ResetTimer() + for i := 0; i < b.N; i++ { + val.SetBytes1(bytearr) + val.SetBytes2(bytearr) + val.SetBytes3(bytearr) + val.SetBytes4(bytearr) + val.SetBytes5(bytearr) + val.SetBytes6(bytearr) + val.SetBytes7(bytearr) + val.SetBytes8(bytearr) + val.SetBytes9(bytearr) + val.SetBytes10(bytearr) + val.SetBytes11(bytearr) + val.SetBytes12(bytearr) + val.SetBytes13(bytearr) + val.SetBytes14(bytearr) + val.SetBytes15(bytearr) + val.SetBytes16(bytearr) + val.SetBytes17(bytearr) + val.SetBytes18(bytearr) + val.SetBytes19(bytearr) + val.SetBytes20(bytearr) + val.SetBytes21(bytearr) + val.SetBytes22(bytearr) + val.SetBytes23(bytearr) + val.SetBytes24(bytearr) + val.SetBytes25(bytearr) + val.SetBytes26(bytearr) + val.SetBytes27(bytearr) + val.SetBytes28(bytearr) + val.SetBytes29(bytearr) + val.SetBytes30(bytearr) + val.SetBytes31(bytearr) + val.SetBytes32(bytearr) + } + }) +} diff --git a/uint256.go b/uint256.go index be704249..99fc4930 100644 --- a/uint256.go +++ b/uint256.go @@ -23,25 +23,76 @@ func NewInt() *Int { // SetBytes interprets buf as the bytes of a big-endian unsigned // integer, sets z to that value, and returns z. +// If buf is larger than 32 bytes, the last 32 bytes is used. This operation +// is semantically equivalent to `FromBig(new(big.Int).SetBytes(buf))` func (z *Int) SetBytes(buf []byte) *Int { - var d uint64 - k := 0 - s := uint64(0) - i := len(buf) - z[0], z[1], z[2], z[3] = 0, 0, 0, 0 - for ; i > 0; i-- { - 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 + switch l := len(buf); l { + case 0: + z.Clear() + case 1: + z.SetBytes1(buf) + case 2: + z.SetBytes2(buf) + case 3: + z.SetBytes3(buf) + case 4: + z.SetBytes4(buf) + case 5: + z.SetBytes5(buf) + case 6: + z.SetBytes6(buf) + case 7: + z.SetBytes7(buf) + case 8: + z.SetBytes8(buf) + case 9: + z.SetBytes9(buf) + case 10: + z.SetBytes10(buf) + case 11: + z.SetBytes11(buf) + case 12: + z.SetBytes12(buf) + case 13: + z.SetBytes13(buf) + case 14: + z.SetBytes14(buf) + case 15: + z.SetBytes15(buf) + case 16: + z.SetBytes16(buf) + case 17: + z.SetBytes17(buf) + case 18: + z.SetBytes18(buf) + case 19: + z.SetBytes19(buf) + case 20: + z.SetBytes20(buf) + case 21: + z.SetBytes21(buf) + case 22: + z.SetBytes22(buf) + case 23: + z.SetBytes23(buf) + case 24: + z.SetBytes24(buf) + case 25: + z.SetBytes25(buf) + case 26: + z.SetBytes26(buf) + case 27: + z.SetBytes27(buf) + case 28: + z.SetBytes28(buf) + case 29: + z.SetBytes29(buf) + case 30: + z.SetBytes30(buf) + case 31: + z.SetBytes31(buf) + default: + z.SetBytes32(buf[l-32:]) } return z } diff --git a/uint256_test.go b/uint256_test.go index f3b1277b..4fc47187 100644 --- a/uint256_test.go +++ b/uint256_test.go @@ -1190,175 +1190,3 @@ func TestByte32Representation(t *testing.T) { } } } - -func TestSetBytes(t *testing.T) { - for i := 0; i < 33; i++ { - bytearr := hex2Bytes("12131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031") - a := big.NewInt(0).SetBytes(bytearr[0:i]) - exp := bytesToHash(a.Bytes()) - - b := NewInt().SetAllOne().SetBytes(bytearr[0:i]) - - got := gethHash(b.Bytes32()) - // uint256.Int -> address - if got != exp { - t.Errorf("testcase %d: got %x exp %x", i, got, exp) - } - b.SetAllOne() - switch i { - case 1: - b.SetBytes1(bytearr) - case 2: - b.SetBytes2(bytearr) - case 3: - b.SetBytes3(bytearr) - case 4: - b.SetBytes4(bytearr) - case 5: - b.SetBytes5(bytearr) - case 6: - b.SetBytes6(bytearr) - case 7: - b.SetBytes7(bytearr) - case 8: - b.SetBytes8(bytearr) - case 9: - b.SetBytes9(bytearr) - case 10: - b.SetBytes10(bytearr) - case 11: - b.SetBytes11(bytearr) - case 12: - b.SetBytes12(bytearr) - case 13: - b.SetBytes13(bytearr) - case 14: - b.SetBytes14(bytearr) - case 15: - b.SetBytes15(bytearr) - case 16: - b.SetBytes16(bytearr) - case 17: - b.SetBytes17(bytearr) - case 18: - b.SetBytes18(bytearr) - case 19: - b.SetBytes19(bytearr) - case 20: - b.SetBytes20(bytearr) - case 21: - b.SetBytes21(bytearr) - case 22: - b.SetBytes22(bytearr) - case 23: - b.SetBytes23(bytearr) - case 24: - b.SetBytes24(bytearr) - case 25: - b.SetBytes25(bytearr) - case 26: - b.SetBytes26(bytearr) - case 27: - b.SetBytes27(bytearr) - case 28: - b.SetBytes28(bytearr) - case 29: - b.SetBytes29(bytearr) - case 30: - b.SetBytes30(bytearr) - case 31: - b.SetBytes31(bytearr) - case 32: - b.SetBytes32(bytearr) - default: - continue - } - got = gethHash(b.Bytes32()) - // uint256.Int -> address - if got != exp { - t.Errorf("testcase %d: got %x exp %x", i, got, exp) - } - } -} - -func BenchmarkSetBytes(b *testing.B) { - - val := NewInt() - bytearr := hex2Bytes("12131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031") - b.Run("SetBytes-generic", func(b *testing.B) { - b.ReportAllocs() - b.ResetTimer() - for i := 0; i < b.N; i++ { - val.SetBytes(bytearr[:1]) - val.SetBytes(bytearr[:2]) - val.SetBytes(bytearr[:3]) - val.SetBytes(bytearr[:4]) - val.SetBytes(bytearr[:5]) - val.SetBytes(bytearr[:6]) - val.SetBytes(bytearr[:7]) - val.SetBytes(bytearr[:8]) - val.SetBytes(bytearr[:9]) - val.SetBytes(bytearr[:10]) - val.SetBytes(bytearr[:11]) - val.SetBytes(bytearr[:12]) - val.SetBytes(bytearr[:13]) - val.SetBytes(bytearr[:14]) - val.SetBytes(bytearr[:15]) - val.SetBytes(bytearr[:16]) - val.SetBytes(bytearr[:17]) - val.SetBytes(bytearr[:18]) - val.SetBytes(bytearr[:19]) - val.SetBytes(bytearr[:20]) - val.SetBytes(bytearr[:21]) - val.SetBytes(bytearr[:22]) - val.SetBytes(bytearr[:23]) - val.SetBytes(bytearr[:24]) - val.SetBytes(bytearr[:25]) - val.SetBytes(bytearr[:26]) - val.SetBytes(bytearr[:27]) - val.SetBytes(bytearr[:28]) - val.SetBytes(bytearr[:29]) - val.SetBytes(bytearr[:20]) - val.SetBytes(bytearr[:31]) - val.SetBytes(bytearr[:32]) - } - }) - b.Run("SetBytes-specific", func(b *testing.B) { - b.ReportAllocs() - b.ResetTimer() - for i := 0; i < b.N; i++ { - val.SetBytes1(bytearr) - val.SetBytes2(bytearr) - val.SetBytes3(bytearr) - val.SetBytes4(bytearr) - val.SetBytes5(bytearr) - val.SetBytes6(bytearr) - val.SetBytes7(bytearr) - val.SetBytes8(bytearr) - val.SetBytes9(bytearr) - val.SetBytes10(bytearr) - val.SetBytes11(bytearr) - val.SetBytes12(bytearr) - val.SetBytes13(bytearr) - val.SetBytes14(bytearr) - val.SetBytes15(bytearr) - val.SetBytes16(bytearr) - val.SetBytes17(bytearr) - val.SetBytes18(bytearr) - val.SetBytes19(bytearr) - val.SetBytes20(bytearr) - val.SetBytes21(bytearr) - val.SetBytes22(bytearr) - val.SetBytes23(bytearr) - val.SetBytes24(bytearr) - val.SetBytes25(bytearr) - val.SetBytes26(bytearr) - val.SetBytes27(bytearr) - val.SetBytes28(bytearr) - val.SetBytes29(bytearr) - val.SetBytes30(bytearr) - val.SetBytes31(bytearr) - val.SetBytes32(bytearr) - } - }) -} From cf7da23a5e396279e11ac1b479af70b0761338d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Fri, 5 Jun 2020 12:44:03 +0200 Subject: [PATCH 4/4] Bring some consistency to SetBytesN implementations --- conversion.go | 78 +++++++++++---------------------------------------- 1 file changed, 17 insertions(+), 61 deletions(-) diff --git a/conversion.go b/conversion.go index 726dd6fa..64262f63 100644 --- a/conversion.go +++ b/conversion.go @@ -109,7 +109,6 @@ func (z *Int) Format(s fmt.State, ch rune) { // SetBytes8 is identical to SetBytes(in[:8]), but panics is input is too short func (z *Int) SetBytes8(in []byte) *Int { _ = in[7] // bounds check hint to compiler; see golang.org/issue/14808 - z[3], z[2], z[1] = 0, 0, 0 z[0] = binary.BigEndian.Uint64(in[0:8]) return z @@ -118,7 +117,6 @@ func (z *Int) SetBytes8(in []byte) *Int { // SetBytes16 is identical to SetBytes(in[:16]), but panics is input is too short func (z *Int) SetBytes16(in []byte) *Int { _ = in[15] // bounds check hint to compiler; see golang.org/issue/14808 - z[3], z[2] = 0, 0 z[1] = binary.BigEndian.Uint64(in[0:8]) z[0] = binary.BigEndian.Uint64(in[8:16]) @@ -128,7 +126,6 @@ func (z *Int) SetBytes16(in []byte) *Int { // SetBytes16 is identical to SetBytes(in[:24]), but panics is input is too short func (z *Int) SetBytes24(in []byte) *Int { _ = in[23] // bounds check hint to compiler; see golang.org/issue/14808 - z[3] = 0 z[2] = binary.BigEndian.Uint64(in[0:8]) z[1] = binary.BigEndian.Uint64(in[8:16]) @@ -138,7 +135,6 @@ func (z *Int) SetBytes24(in []byte) *Int { func (z *Int) SetBytes32(in []byte) *Int { _ = in[31] // bounds check hint to compiler; see golang.org/issue/14808 - z[3] = binary.BigEndian.Uint64(in[0:8]) z[2] = binary.BigEndian.Uint64(in[8:16]) z[1] = binary.BigEndian.Uint64(in[16:24]) @@ -147,20 +143,21 @@ func (z *Int) SetBytes32(in []byte) *Int { } func (z *Int) SetBytes1(in []byte) *Int { - z[3], z[2], z[1], z[0] = 0, 0, 0, uint64(in[0]) + z[3], z[2], z[1] = 0, 0, 0 + z[0] = uint64(in[0]) return z } func (z *Int) SetBytes9(in []byte) *Int { _ = in[8] // bounds check hint to compiler; see golang.org/issue/14808 - - z[3], z[2], z[1], z[0] = 0, 0, uint64(in[0]), binary.BigEndian.Uint64(in[1:9]) + z[3], z[2] = 0, 0 + z[1] = uint64(in[0]) + z[0] = binary.BigEndian.Uint64(in[1:9]) return z } func (z *Int) SetBytes17(in []byte) *Int { _ = in[16] // bounds check hint to compiler; see golang.org/issue/14808 - z[3] = 0 z[2] = uint64(in[0]) z[1] = binary.BigEndian.Uint64(in[1:9]) @@ -170,7 +167,6 @@ func (z *Int) SetBytes17(in []byte) *Int { func (z *Int) SetBytes25(in []byte) *Int { _ = in[24] // bounds check hint to compiler; see golang.org/issue/14808 - z[3] = uint64(in[0]) z[2] = binary.BigEndian.Uint64(in[1:9]) z[1] = binary.BigEndian.Uint64(in[9:17]) @@ -180,19 +176,14 @@ func (z *Int) SetBytes25(in []byte) *Int { func (z *Int) SetBytes2(in []byte) *Int { _ = in[1] // bounds check hint to compiler; see golang.org/issue/14808 - - z[3] = 0 - z[2] = 0 - z[1] = 0 + z[3], z[2], z[1] = 0, 0, 0 z[0] = uint64(binary.BigEndian.Uint16(in[0:2])) return z } func (z *Int) SetBytes10(in []byte) *Int { _ = in[9] // bounds check hint to compiler; see golang.org/issue/14808 - - z[3] = 0 - z[2] = 0 + z[3], z[2] = 0, 0 z[1] = uint64(binary.BigEndian.Uint16(in[0:2])) z[0] = binary.BigEndian.Uint64(in[2:10]) return z @@ -200,7 +191,6 @@ func (z *Int) SetBytes10(in []byte) *Int { func (z *Int) SetBytes18(in []byte) *Int { _ = in[17] // bounds check hint to compiler; see golang.org/issue/14808 - z[3] = 0 z[2] = uint64(binary.BigEndian.Uint16(in[0:2])) z[1] = binary.BigEndian.Uint64(in[2:10]) @@ -210,7 +200,6 @@ func (z *Int) SetBytes18(in []byte) *Int { func (z *Int) SetBytes26(in []byte) *Int { _ = in[25] // bounds check hint to compiler; see golang.org/issue/14808 - z[3] = uint64(binary.BigEndian.Uint16(in[0:2])) z[2] = binary.BigEndian.Uint64(in[2:10]) z[1] = binary.BigEndian.Uint64(in[10:18]) @@ -220,17 +209,14 @@ func (z *Int) SetBytes26(in []byte) *Int { func (z *Int) SetBytes3(in []byte) *Int { _ = in[2] // bounds check hint to compiler; see golang.org/issue/14808 - z[3] = 0 - z[2] = 0 - z[1] = 0 + z[3], z[2], z[1] = 0, 0, 0 z[0] = uint64(binary.BigEndian.Uint16(in[1:3])) | uint64(in[0])<<16 return z } func (z *Int) SetBytes11(in []byte) *Int { _ = in[10] // bounds check hint to compiler; see golang.org/issue/14808 - z[3] = 0 - z[2] = 0 + z[3], z[2] = 0, 0 z[1] = uint64(binary.BigEndian.Uint16(in[1:3])) | uint64(in[0])<<16 z[0] = binary.BigEndian.Uint64(in[3:11]) return z @@ -238,7 +224,6 @@ func (z *Int) SetBytes11(in []byte) *Int { func (z *Int) SetBytes19(in []byte) *Int { _ = in[18] // bounds check hint to compiler; see golang.org/issue/14808 - z[3] = 0 z[2] = uint64(binary.BigEndian.Uint16(in[1:3])) | uint64(in[0])<<16 z[1] = binary.BigEndian.Uint64(in[3:11]) @@ -248,7 +233,6 @@ func (z *Int) SetBytes19(in []byte) *Int { func (z *Int) SetBytes27(in []byte) *Int { _ = in[26] // bounds check hint to compiler; see golang.org/issue/14808 - z[3] = uint64(binary.BigEndian.Uint16(in[1:3])) | uint64(in[0])<<16 z[2] = binary.BigEndian.Uint64(in[3:11]) z[1] = binary.BigEndian.Uint64(in[11:19]) @@ -258,19 +242,14 @@ func (z *Int) SetBytes27(in []byte) *Int { func (z *Int) SetBytes4(in []byte) *Int { _ = in[3] // bounds check hint to compiler; see golang.org/issue/14808 - - z[3] = 0 - z[2] = 0 - z[1] = 0 + z[3], z[2], z[1] = 0, 0, 0 z[0] = uint64(binary.BigEndian.Uint32(in[0:4])) return z } func (z *Int) SetBytes12(in []byte) *Int { _ = in[11] // bounds check hint to compiler; see golang.org/issue/14808 - - z[3] = 0 - z[2] = 0 + z[3], z[2] = 0, 0 z[1] = uint64(binary.BigEndian.Uint32(in[0:4])) z[0] = binary.BigEndian.Uint64(in[4:12]) return z @@ -278,7 +257,6 @@ func (z *Int) SetBytes12(in []byte) *Int { func (z *Int) SetBytes20(in []byte) *Int { _ = in[19] // bounds check hint to compiler; see golang.org/issue/14808 - z[3] = 0 z[2] = uint64(binary.BigEndian.Uint32(in[0:4])) z[1] = binary.BigEndian.Uint64(in[4:12]) @@ -288,7 +266,6 @@ func (z *Int) SetBytes20(in []byte) *Int { func (z *Int) SetBytes28(in []byte) *Int { _ = in[27] // bounds check hint to compiler; see golang.org/issue/14808 - z[3] = uint64(binary.BigEndian.Uint32(in[0:4])) z[2] = binary.BigEndian.Uint64(in[4:12]) z[1] = binary.BigEndian.Uint64(in[12:20]) @@ -298,19 +275,14 @@ func (z *Int) SetBytes28(in []byte) *Int { func (z *Int) SetBytes5(in []byte) *Int { _ = in[4] // bounds check hint to compiler; see golang.org/issue/14808 - - z[3] = 0 - z[2] = 0 - z[1] = 0 + z[3], z[2], z[1] = 0, 0, 0 z[0] = bigEndianUint40(in[0:5]) return z } func (z *Int) SetBytes13(in []byte) *Int { _ = in[12] // bounds check hint to compiler; see golang.org/issue/14808 - - z[3] = 0 - z[2] = 0 + z[3], z[2] = 0, 0 z[1] = bigEndianUint40(in[0:5]) z[0] = binary.BigEndian.Uint64(in[5:13]) return z @@ -318,7 +290,6 @@ func (z *Int) SetBytes13(in []byte) *Int { func (z *Int) SetBytes21(in []byte) *Int { _ = in[20] // bounds check hint to compiler; see golang.org/issue/14808 - z[3] = 0 z[2] = bigEndianUint40(in[0:5]) z[1] = binary.BigEndian.Uint64(in[5:13]) @@ -328,7 +299,6 @@ func (z *Int) SetBytes21(in []byte) *Int { func (z *Int) SetBytes29(in []byte) *Int { _ = in[23] // bounds check hint to compiler; see golang.org/issue/14808 - z[3] = bigEndianUint40(in[0:5]) z[2] = binary.BigEndian.Uint64(in[5:13]) z[1] = binary.BigEndian.Uint64(in[13:21]) @@ -338,19 +308,14 @@ func (z *Int) SetBytes29(in []byte) *Int { func (z *Int) SetBytes6(in []byte) *Int { _ = in[5] // bounds check hint to compiler; see golang.org/issue/14808 - - z[3] = 0 - z[2] = 0 - z[1] = 0 + z[3], z[2], z[1] = 0, 0, 0 z[0] = bigEndianUint48(in[0:6]) return z } func (z *Int) SetBytes14(in []byte) *Int { _ = in[13] // bounds check hint to compiler; see golang.org/issue/14808 - - z[3] = 0 - z[2] = 0 + z[3], z[2] = 0, 0 z[1] = bigEndianUint48(in[0:6]) z[0] = binary.BigEndian.Uint64(in[6:14]) return z @@ -358,7 +323,6 @@ func (z *Int) SetBytes14(in []byte) *Int { func (z *Int) SetBytes22(in []byte) *Int { _ = in[21] // bounds check hint to compiler; see golang.org/issue/14808 - z[3] = 0 z[2] = bigEndianUint48(in[0:6]) z[1] = binary.BigEndian.Uint64(in[6:14]) @@ -368,7 +332,6 @@ func (z *Int) SetBytes22(in []byte) *Int { func (z *Int) SetBytes30(in []byte) *Int { _ = in[29] // bounds check hint to compiler; see golang.org/issue/14808 - z[3] = bigEndianUint48(in[0:6]) z[2] = binary.BigEndian.Uint64(in[6:14]) z[1] = binary.BigEndian.Uint64(in[14:22]) @@ -378,19 +341,14 @@ func (z *Int) SetBytes30(in []byte) *Int { func (z *Int) SetBytes7(in []byte) *Int { _ = in[6] // bounds check hint to compiler; see golang.org/issue/14808 - - z[3] = 0 - z[2] = 0 - z[1] = 0 + z[3], z[2], z[1] = 0, 0, 0 z[0] = bigEndianUint56(in[0:7]) return z } func (z *Int) SetBytes15(in []byte) *Int { _ = in[14] // bounds check hint to compiler; see golang.org/issue/14808 - - z[3] = 0 - z[2] = 0 + z[3], z[2] = 0, 0 z[1] = bigEndianUint56(in[0:7]) z[0] = binary.BigEndian.Uint64(in[7:15]) return z @@ -398,7 +356,6 @@ func (z *Int) SetBytes15(in []byte) *Int { func (z *Int) SetBytes23(in []byte) *Int { _ = in[22] // bounds check hint to compiler; see golang.org/issue/14808 - z[3] = 0 z[2] = bigEndianUint56(in[0:7]) z[1] = binary.BigEndian.Uint64(in[7:15]) @@ -408,7 +365,6 @@ func (z *Int) SetBytes23(in []byte) *Int { func (z *Int) SetBytes31(in []byte) *Int { _ = in[30] // bounds check hint to compiler; see golang.org/issue/14808 - z[3] = bigEndianUint56(in[0:7]) z[2] = binary.BigEndian.Uint64(in[7:15]) z[1] = binary.BigEndian.Uint64(in[15:23])