From 5d5dbb6c19b7bd477dba45cb959bfeab63e8faec Mon Sep 17 00:00:00 2001 From: John Adler Date: Sun, 20 Mar 2022 14:03:06 -0400 Subject: [PATCH 1/2] Bump to solc v0.8.4. --- contracts/ProtobufLib.sol | 2 +- contracts/TestFixture.sol | 2 +- hardhat.config.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/contracts/ProtobufLib.sol b/contracts/ProtobufLib.sol index 38cd04b..8b8bc90 100644 --- a/contracts/ProtobufLib.sol +++ b/contracts/ProtobufLib.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: Apache-2.0 -pragma solidity >=0.6.0 <8.0.0; +pragma solidity >=0.8.4 <0.9.0; library ProtobufLib { /// @notice Protobuf wire types. diff --git a/contracts/TestFixture.sol b/contracts/TestFixture.sol index 79846cb..9a9e667 100644 --- a/contracts/TestFixture.sol +++ b/contracts/TestFixture.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: Apache-2.0 -pragma solidity >=0.6.0 <8.0.0; +pragma solidity >=0.8.4 <0.9.0; import "./ProtobufLib.sol"; diff --git a/hardhat.config.js b/hardhat.config.js index b546963..0786f66 100644 --- a/hardhat.config.js +++ b/hardhat.config.js @@ -1,5 +1,5 @@ require("@nomiclabs/hardhat-truffle5"); module.exports = { - solidity: "0.6.12", + solidity: "0.8.4", }; From 513e8955dcd582fe9842d4d516f24d2bc088e126 Mon Sep 17 00:00:00 2001 From: John Adler Date: Sun, 20 Mar 2022 14:06:33 -0400 Subject: [PATCH 2/2] Fixes. --- contracts/ProtobufLib.sol | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/contracts/ProtobufLib.sol b/contracts/ProtobufLib.sol index 8b8bc90..3e25cd1 100644 --- a/contracts/ProtobufLib.sol +++ b/contracts/ProtobufLib.sol @@ -3,7 +3,15 @@ pragma solidity >=0.8.4 <0.9.0; library ProtobufLib { /// @notice Protobuf wire types. - enum WireType { Varint, Bits64, LengthDelimited, StartGroup, EndGroup, Bits32, WIRE_TYPE_MAX } + enum WireType { + Varint, + Bits64, + LengthDelimited, + StartGroup, + EndGroup, + Bits32, + WIRE_TYPE_MAX + } /// @dev Maximum number of bytes for a varint. /// @dev 64 bits, in groups of base-128 (7 bits). @@ -252,7 +260,10 @@ library ProtobufLib { } // https://stackoverflow.com/questions/2210923/zig-zag-decoding/2211086#2211086 - uint64 zigzag_val = (val >> 1) ^ (-(val & 1)); + uint64 zigzag_val; + unchecked { + zigzag_val = (val >> 1) - (~(val & 1) + 1); + } return (true, pos, int32(uint32(zigzag_val))); } @@ -278,7 +289,10 @@ library ProtobufLib { } // https://stackoverflow.com/questions/2210923/zig-zag-decoding/2211086#2211086 - uint64 zigzag_val = (val >> 1) ^ (-(val & 1)); + uint64 zigzag_val; + unchecked { + zigzag_val = (val >> 1) - (~(val & 1) + 1); + } return (true, pos, int64(zigzag_val)); } @@ -512,8 +526,10 @@ library ProtobufLib { } // Check for overflow - if (pos + size < pos) { - return (false, pos, 0); + unchecked { + if (pos + size < pos) { + return (false, pos, 0); + } } // Check that index is within bounds @@ -655,7 +671,7 @@ library ProtobufLib { /// @param n Number /// @return Marshaled bytes function encode_int32(int32 n) internal pure returns (bytes memory) { - return encode_varint(uint64(n)); + return encode_varint(uint64(int64(n))); } /// @notice Decode varint int64. @@ -686,7 +702,9 @@ library ProtobufLib { // https://developers.google.com/protocol-buffers/docs/encoding#signed_integers uint32 mask = 0; if (n < 0) { - mask -= 1; + unchecked { + mask -= 1; + } } uint32 zigzag_val = (uint32(n) << 1) ^ mask; @@ -700,7 +718,9 @@ library ProtobufLib { // https://developers.google.com/protocol-buffers/docs/encoding#signed_integers uint64 mask = 0; if (n < 0) { - mask -= 1; + unchecked { + mask -= 1; + } } uint64 zigzag_val = (uint64(n) << 1) ^ mask;