From c871bea14e612cac45b77fd767ad007917738ba6 Mon Sep 17 00:00:00 2001 From: maradini77 <140460067+maradini77@users.noreply.github.com> Date: Mon, 13 Oct 2025 11:21:40 +0200 Subject: [PATCH 1/3] Update Math.sol --- contracts/utils/math/Math.sol | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/contracts/utils/math/Math.sol b/contracts/utils/math/Math.sol index d7fd2f64d15..2c8dd20d440 100644 --- a/contracts/utils/math/Math.sol +++ b/contracts/utils/math/Math.sol @@ -474,8 +474,26 @@ library Math { * @dev Returns whether the provided byte array is zero. */ function _zeroBytes(bytes memory byteArray) private pure returns (bool) { - for (uint256 i = 0; i < byteArray.length; ++i) { - if (byteArray[i] != 0) { + // Fast path: scan 32-byte words first to reduce gas on large inputs. + uint256 len = byteArray.length; + uint256 offset = 0; + + while (len >= 32) { + bytes32 word; + assembly ("memory-safe") { + // Load a full 32-byte word starting at current offset + word := mload(add(add(byteArray, 0x20), offset)) + } + if (word != 0) { + return false; + } + offset += 32; + len -= 32; + } + + // Tail check (< 32 bytes) + for (uint256 i = 0; i < len; ++i) { + if (byteArray[offset + i] != 0) { return false; } } From 1b0ab67126849d9de7b45a3f7ab9e8ad4cf4bc48 Mon Sep 17 00:00:00 2001 From: maradini77 <140460067+maradini77@users.noreply.github.com> Date: Fri, 31 Oct 2025 15:24:47 +0100 Subject: [PATCH 2/3] Update Math.sol --- contracts/utils/math/Math.sol | 25 ++++++------------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/contracts/utils/math/Math.sol b/contracts/utils/math/Math.sol index 2c8dd20d440..03961ceaf23 100644 --- a/contracts/utils/math/Math.sol +++ b/contracts/utils/math/Math.sol @@ -473,27 +473,14 @@ library Math { /** * @dev Returns whether the provided byte array is zero. */ - function _zeroBytes(bytes memory byteArray) private pure returns (bool) { - // Fast path: scan 32-byte words first to reduce gas on large inputs. - uint256 len = byteArray.length; - uint256 offset = 0; - - while (len >= 32) { - bytes32 word; + function _zeroBytes(bytes memory buffer) private pure returns (bool) { + for (uint256 i = 0; i < buffer.length; i += 0x20) { + // See _unsafeReadBytesOffset from utils/Bytes.sol + uint256 value; assembly ("memory-safe") { - // Load a full 32-byte word starting at current offset - word := mload(add(add(byteArray, 0x20), offset)) - } - if (word != 0) { - return false; + value := mload(add(add(buffer, 0x20), i)) } - offset += 32; - len -= 32; - } - - // Tail check (< 32 bytes) - for (uint256 i = 0; i < len; ++i) { - if (byteArray[offset + i] != 0) { + if (value >> (8 * saturatingSub(i + 0x20, buffer.length)) != 0) { return false; } } From 892926b723a71211dcaefb0d8faf76619b60b66e Mon Sep 17 00:00:00 2001 From: Hadrien Croubois Date: Fri, 31 Oct 2025 16:13:52 +0100 Subject: [PATCH 3/3] Update Math.sol --- contracts/utils/math/Math.sol | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/contracts/utils/math/Math.sol b/contracts/utils/math/Math.sol index 03961ceaf23..828b441fc01 100644 --- a/contracts/utils/math/Math.sol +++ b/contracts/utils/math/Math.sol @@ -474,13 +474,13 @@ library Math { * @dev Returns whether the provided byte array is zero. */ function _zeroBytes(bytes memory buffer) private pure returns (bool) { + uint256 chunk; for (uint256 i = 0; i < buffer.length; i += 0x20) { // See _unsafeReadBytesOffset from utils/Bytes.sol - uint256 value; assembly ("memory-safe") { - value := mload(add(add(buffer, 0x20), i)) + chunk := mload(add(add(buffer, 0x20), i)) } - if (value >> (8 * saturatingSub(i + 0x20, buffer.length)) != 0) { + if (chunk >> (8 * saturatingSub(i + 0x20, buffer.length)) != 0) { return false; } }