From 0fcc2819d0a010415dbf1af64d8e54c59fe9974f Mon Sep 17 00:00:00 2001 From: Vectorized Date: Tue, 22 Apr 2025 05:33:04 +0000 Subject: [PATCH 01/15] Optimize cdCompress and cdDecompress more --- src/utils/LibZip.sol | 40 +++++++++++++++++------------- test/LibZip.t.sol | 59 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 17 deletions(-) diff --git a/src/utils/LibZip.sol b/src/utils/LibZip.sol index b72eef0e6a..6ad18c0a9a 100644 --- a/src/utils/LibZip.sol +++ b/src/utils/LibZip.sol @@ -183,17 +183,16 @@ library LibZip { data := add(data, 1) let c := byte(31, mload(data)) if iszero(c) { - let z := 0 for {} 1 {} { let r := 0x20 let x := mload(add(data, r)) if x { r := countLeadingZeroBytes(x) } - r := min(min(sub(end, data), r), sub(0x7f, z)) + r := min(min(sub(end, data), r), sub(0x7f, c)) data := add(data, r) - z := add(z, r) + c := add(c, r) if iszero(gt(r, 0x1f)) { break } } - mstore(o, shl(240, z)) + mstore(o, shl(240, c)) o := add(o, 2) continue } @@ -224,25 +223,32 @@ library LibZip { assembly { if mload(data) { result := mload(0x40) - let o := add(result, 0x20) let s := add(data, 4) let v := mload(s) - let end := add(data, mload(data)) + let end := add(add(0x20, data), mload(data)) + let o := add(result, 0x20) mstore(s, not(v)) // Bitwise negate the first 4 bytes. - for {} lt(data, end) {} { - data := add(data, 1) - let c := byte(31, mload(data)) - if iszero(c) { - data := add(data, 1) - let d := byte(31, mload(data)) - // Fill with either 0xff or 0x00. - mstore(o, not(0)) - if iszero(gt(d, 0x7f)) { calldatacopy(o, calldatasize(), add(d, 1)) } - o := add(o, add(and(d, 0x7f), 1)) + for { let i := add(0x20, data) } 1 {} { + let c := mload(i) + if iszero(byte(0, c)) { + c := byte(1, c) + if iszero(gt(c, 0x7f)) { + calldatacopy(o, calldatasize(), add(c, 1)) // Fill with 0x00. + o := add(o, add(c, 1)) + i := add(i, 2) + if iszero(lt(i, end)) { break } + continue + } + mstore(o, not(0)) // Fill with 0xff. + o := add(o, add(c, sub(1, 0x80))) + i := add(i, 2) + if iszero(lt(i, end)) { break } continue } - mstore8(o, c) + mstore(o, c) o := add(o, 1) + i := add(i, 1) + if iszero(lt(i, end)) { break } } mstore(s, v) // Restore the first 4 bytes. mstore(result, sub(o, add(result, 0x20))) // Store the length. diff --git a/test/LibZip.t.sol b/test/LibZip.t.sol index fd2df26412..f205f89b56 100644 --- a/test/LibZip.t.sol +++ b/test/LibZip.t.sol @@ -38,6 +38,19 @@ contract LibZipTest is SoladyTest { bytes internal constant _CD_COMPRESS_INPUT = hex"00000000000000000000000000000000000000000000000000000000000ae11c0000000000000000000000000000000000000000000000000000002b9cdca0ab0000000000000000000000000000000000003961790f8baa365051889e4c367d00000000000000000000000000000000000026d85539440bc844167ac0cc42320000000000000000000000000000000000000000000000007b55939986433925"; + bytes internal constant _CD_COMPRESS_OUTPUT = + hex"ffe3f51e1c001a2b9cdca0ab00113961790f8baa365051889e4c367d001126d85539440bc844167ac0cc423200177b55939986433925"; + + function testCdDecompressGas() public { + bytes memory data = _CD_COMPRESS_OUTPUT; + assertGt(LibZip.cdDecompress(data).length, data.length); + } + + function testCdDecompressOriginalGas() public { + bytes memory data = _CD_COMPRESS_OUTPUT; + assertGt(_cdDecompressOriginal(data).length, data.length); + } + function testCdCompressGas() public { bytes memory data = _CD_COMPRESS_INPUT; assertLt(LibZip.cdCompress(data).length, data.length); @@ -80,6 +93,19 @@ contract LibZipTest is SoladyTest { assertEq(LibZip.cdCompress(data), _cdCompressOriginal(data)); } + function testCdDecompressDifferential(bytes32) public { + bytes memory data = _randomCd(); + if (_randomChance(2)) { + testCdDecompressDifferential(LibZip.cdCompress(data)); + } else { + testCdDecompressDifferential(data); + } + } + + function testCdDecompressDifferential(bytes memory data) public { + assertEq(LibZip.cdDecompress(data), _cdDecompressOriginal(data)); + } + function _cdCompressOriginal(bytes memory data) internal pure returns (bytes memory result) { /// @solidity memory-safe-assembly assembly { @@ -121,6 +147,39 @@ contract LibZipTest is SoladyTest { } } + function _cdDecompressOriginal(bytes memory data) internal pure returns (bytes memory result) { + /// @solidity memory-safe-assembly + assembly { + if mload(data) { + result := mload(0x40) + let o := add(result, 0x20) + let s := add(data, 4) + let v := mload(s) + let end := add(data, mload(data)) + mstore(s, not(v)) // Bitwise negate the first 4 bytes. + for {} lt(data, end) {} { + data := add(data, 1) + let c := byte(31, mload(data)) + if iszero(c) { + data := add(data, 1) + let d := byte(31, mload(data)) + // Fill with either 0xff or 0x00. + mstore(o, not(0)) + if iszero(gt(d, 0x7f)) { calldatacopy(o, calldatasize(), add(d, 1)) } + o := add(o, add(and(d, 0x7f), 1)) + continue + } + mstore8(o, c) + o := add(o, 1) + } + mstore(s, v) // Restore the first 4 bytes. + mstore(result, sub(o, add(result, 0x20))) // Store the length. + mstore(o, 0) // Zeroize the slot after the string. + mstore(0x40, add(o, 0x20)) // Allocate the memory. + } + } + } + function testFlzCompressDecompress() public brutalizeMemory { assertEq(LibZip.flzCompress(""), ""); assertEq(LibZip.flzDecompress(""), ""); From f78be633914fb9073d4e535efd78651692e3f0cc Mon Sep 17 00:00:00 2001 From: Vectorized Date: Tue, 22 Apr 2025 10:28:22 +0000 Subject: [PATCH 02/15] Opt --- src/utils/LibZip.sol | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/utils/LibZip.sol b/src/utils/LibZip.sol index 6ad18c0a9a..c20d889864 100644 --- a/src/utils/LibZip.sol +++ b/src/utils/LibZip.sol @@ -231,16 +231,16 @@ library LibZip { for { let i := add(0x20, data) } 1 {} { let c := mload(i) if iszero(byte(0, c)) { - c := byte(1, c) - if iszero(gt(c, 0x7f)) { - calldatacopy(o, calldatasize(), add(c, 1)) // Fill with 0x00. - o := add(o, add(c, 1)) + c := add(byte(1, c), 1) + if iszero(gt(c, 0x80)) { + calldatacopy(o, calldatasize(), c) // Fill with 0x00. + o := add(o, c) i := add(i, 2) if iszero(lt(i, end)) { break } continue } mstore(o, not(0)) // Fill with 0xff. - o := add(o, add(c, sub(1, 0x80))) + o := add(o, sub(c, 0x80)) i := add(i, 2) if iszero(lt(i, end)) { break } continue From 7c5c9cd112f4b3e4914f19ffda69d25842a56d95 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Tue, 22 Apr 2025 10:37:33 +0000 Subject: [PATCH 03/15] Opt --- src/utils/LibZip.sol | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/utils/LibZip.sol b/src/utils/LibZip.sol index c20d889864..de405fca3d 100644 --- a/src/utils/LibZip.sol +++ b/src/utils/LibZip.sol @@ -184,13 +184,19 @@ library LibZip { let c := byte(31, mload(data)) if iszero(c) { for {} 1 {} { - let r := 0x20 - let x := mload(add(data, r)) - if x { r := countLeadingZeroBytes(x) } + let x := mload(add(data, 0x20)) + if iszero(x) { + let r := min(min(sub(end, data), 0x20), sub(0x7f, c)) + data := add(data, r) + c := add(c, r) + if iszero(gt(r, 0x1f)) { break } + continue + } + let r := countLeadingZeroBytes(x) r := min(min(sub(end, data), r), sub(0x7f, c)) data := add(data, r) c := add(c, r) - if iszero(gt(r, 0x1f)) { break } + break } mstore(o, shl(240, c)) o := add(o, 2) From 3c71d3791e10210084d662ddace84b114a393efc Mon Sep 17 00:00:00 2001 From: Vectorized Date: Tue, 22 Apr 2025 10:45:52 +0000 Subject: [PATCH 04/15] Strengthen test --- test/LibZip.t.sol | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/test/LibZip.t.sol b/test/LibZip.t.sol index f205f89b56..615cfd6499 100644 --- a/test/LibZip.t.sol +++ b/test/LibZip.t.sol @@ -270,7 +270,18 @@ contract LibZipTest is SoladyTest { function _randomCd() internal returns (bytes memory data) { uint256 n = _randomChance(8) ? _random() % 2048 : _random() % 256; data = new bytes(n); - if (_randomChance(2)) { + if (_randomChance(32)) { + uint256 r = _randomUniform(); + /// @solidity memory-safe-assembly + assembly { + mstore(0x00, r) + for { let i := 0 } lt(i, n) { i := add(i, 0x20) } { + mstore(0x20, xor("randomUniform", i)) + mstore(add(add(data, 0x20), i), keccak256(0x00, 0x40)) + } + } + } + if (_randomChance(4)) { /// @solidity memory-safe-assembly assembly { for { let i := 0 } lt(i, n) { i := add(i, 0x20) } { @@ -284,15 +295,30 @@ contract LibZipTest is SoladyTest { assembly { mstore(0x00, r) for { let i := 0 } lt(i, n) { i := add(i, 0x20) } { - mstore(0x20, i) - if and(1, keccak256(0x00, 0x40)) { mstore(add(add(data, 0x20), i), 0) } + mstore(0x20, xor("0", i)) + let p := keccak256(0x00, 0x40) + if and(0x01, p) { mstore(add(add(data, 0x20), i), 0) } + } + } + } + if (_randomChance(16)) { + uint256 r = _randomUniform(); + /// @solidity memory-safe-assembly + assembly { + mstore(0x00, r) + for { let i := 0 } lt(i, n) { i := add(i, 0x20) } { + mstore(0x20, xor("not(0)", i)) + let p := keccak256(0x00, 0x40) + if and(0x10, p) { mstore(add(add(data, 0x20), i), not(0)) } } } } - if (n != 0) { - uint256 m = _random() % 8; - for (uint256 j; j < m; ++j) { - data[_random() % n] = bytes1(uint8(_random())); + if (_randomChance(2)) { + if (n != 0) { + uint256 m = _random() % 8; + for (uint256 j; j < m; ++j) { + data[_random() % n] = bytes1(uint8(_random())); + } } } } From 325995e2d380c4232b19350fb1752c029a06e543 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Tue, 22 Apr 2025 10:53:52 +0000 Subject: [PATCH 05/15] Opt --- src/utils/LibZip.sol | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/utils/LibZip.sol b/src/utils/LibZip.sol index de405fca3d..3e9ceac18a 100644 --- a/src/utils/LibZip.sol +++ b/src/utils/LibZip.sol @@ -193,7 +193,8 @@ library LibZip { continue } let r := countLeadingZeroBytes(x) - r := min(min(sub(end, data), r), sub(0x7f, c)) + r := min(sub(end, data), r) + r := min(sub(0x7f, c), r) data := add(data, r) c := add(c, r) break From 302b81f9fda5287480c70f697d6506bf2ba7f841 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Tue, 22 Apr 2025 11:30:59 +0000 Subject: [PATCH 06/15] Opt --- src/utils/LibZip.sol | 1 - test/LibZip.t.sol | 25 ++++++++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/utils/LibZip.sol b/src/utils/LibZip.sol index 3e9ceac18a..a9ad592446 100644 --- a/src/utils/LibZip.sol +++ b/src/utils/LibZip.sol @@ -194,7 +194,6 @@ library LibZip { } let r := countLeadingZeroBytes(x) r := min(sub(end, data), r) - r := min(sub(0x7f, c), r) data := add(data, r) c := add(c, r) break diff --git a/test/LibZip.t.sol b/test/LibZip.t.sol index 615cfd6499..ce3efa02a3 100644 --- a/test/LibZip.t.sol +++ b/test/LibZip.t.sol @@ -86,7 +86,16 @@ contract LibZipTest is SoladyTest { } function testCdCompressDifferential(bytes32) public { - testCdCompressDifferential(_randomCd()); + bytes memory data; + if (_randomChance(8)) data = _randomCd(); + uint256 t = _randomUniform() % 4; + for (uint256 i; i < t; ++i) { + if (_randomChance(2)) data = abi.encodePacked(data, _random()); + if (_randomChance(2)) data = abi.encodePacked(data, new bytes(_random() & 0x3ff)); + if (_randomChance(2)) data = abi.encodePacked(data, _random()); + if (_randomChance(32)) data = abi.encodePacked(data, _randomCd()); + } + testCdCompressDifferential(data); } function testCdCompressDifferential(bytes memory data) public { @@ -289,6 +298,20 @@ contract LibZipTest is SoladyTest { } } } + if (_randomChance(16)) { + uint256 r = _randomUniform(); + /// @solidity memory-safe-assembly + assembly { + mstore(0x00, r) + mstore(0x20, xor("mode", not(0))) + let mode := and(1, keccak256(0x00, 0x40)) + for { let i := 0 } lt(i, n) { i := add(i, 0x20) } { + mstore(0x20, xor("mode", i)) + mode := xor(mode, iszero(and(keccak256(0x00, 0x40), 7))) + mstore(add(add(data, 0x20), i), mul(iszero(mode), not(0))) + } + } + } if (_randomChance(16)) { uint256 r = _randomUniform(); /// @solidity memory-safe-assembly From 2952bb1463585052450ffb70f110fcef713ee6c2 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Tue, 22 Apr 2025 11:45:37 +0000 Subject: [PATCH 07/15] Opt --- src/utils/LibZip.sol | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/utils/LibZip.sol b/src/utils/LibZip.sol index a9ad592446..b800dc99ac 100644 --- a/src/utils/LibZip.sol +++ b/src/utils/LibZip.sol @@ -186,7 +186,8 @@ library LibZip { for {} 1 {} { let x := mload(add(data, 0x20)) if iszero(x) { - let r := min(min(sub(end, data), 0x20), sub(0x7f, c)) + let r := min(sub(end, data), 0x20) + r := min(sub(0x7f, c), r) data := add(data, r) c := add(c, r) if iszero(gt(r, 0x1f)) { break } From 94376d518a635f6adffa3e3b456640c3e7b43924 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Tue, 22 Apr 2025 11:48:49 +0000 Subject: [PATCH 08/15] T --- test/LibZip.t.sol | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/LibZip.t.sol b/test/LibZip.t.sol index ce3efa02a3..0756515b8f 100644 --- a/test/LibZip.t.sol +++ b/test/LibZip.t.sol @@ -99,7 +99,10 @@ contract LibZipTest is SoladyTest { } function testCdCompressDifferential(bytes memory data) public { - assertEq(LibZip.cdCompress(data), _cdCompressOriginal(data)); + if (_randomChance(32)) _misalignFreeMemoryPointer(); + if (_randomChance(32)) _brutalizeMemory(); + bytes memory computed = LibZip.cdCompress(data); + assertEq(computed, _cdCompressOriginal(data)); } function testCdDecompressDifferential(bytes32) public { From 07b3e796e87501bba000a6de754ce0281b6f8207 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Tue, 22 Apr 2025 12:27:01 +0000 Subject: [PATCH 09/15] Opt --- src/utils/LibZip.sol | 18 ++++++++++++++++-- test/LibZip.t.sol | 23 +++++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/utils/LibZip.sol b/src/utils/LibZip.sol index b800dc99ac..804585762f 100644 --- a/src/utils/LibZip.sol +++ b/src/utils/LibZip.sol @@ -228,8 +228,19 @@ library LibZip { function cdDecompress(bytes memory data) internal pure returns (bytes memory result) { /// @solidity memory-safe-assembly assembly { + function countLeadingZeroBytes(x_) -> _r { + _r := shl(7, lt(0xffffffffffffffffffffffffffffffff, x_)) + _r := or(_r, shl(6, lt(0xffffffffffffffff, shr(_r, x_)))) + _r := or(_r, shl(5, lt(0xffffffff, shr(_r, x_)))) + _r := or(_r, shl(4, lt(0xffff, shr(_r, x_)))) + _r := xor(31, or(shr(3, _r), lt(0xff, shr(_r, x_)))) + } + function min(x_, y_) -> _z { + _z := xor(x_, mul(xor(x_, y_), lt(y_, x_))) + } if mload(data) { result := mload(0x40) + let m := 0x7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F let s := add(data, 4) let v := mload(s) let end := add(add(0x20, data), mload(data)) @@ -253,8 +264,11 @@ library LibZip { continue } mstore(o, c) - o := add(o, 1) - i := add(i, 1) + let r := 0x20 + let x := not(or(or(add(and(c, m), m), c), m)) + if x { r := countLeadingZeroBytes(x) } + o := add(min(sub(end, i), r), o) + i := add(i, r) if iszero(lt(i, end)) { break } } mstore(s, v) // Restore the first 4 bytes. diff --git a/test/LibZip.t.sol b/test/LibZip.t.sol index 0756515b8f..0c17c3957e 100644 --- a/test/LibZip.t.sol +++ b/test/LibZip.t.sol @@ -504,4 +504,27 @@ contract LibZipTest is SoladyTest { } assertEq(a, b); } + + function testCountLeadingNonZeroBytes(bytes32 s) public { + uint256 expected; + uint256 computed; + /// @solidity memory-safe-assembly + assembly { + let n := 0 + for {} byte(n, s) { n := add(n, 1) } {} // Scan for '\0'. + expected := n + let m := 0x7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F + let x := not(or(or(add(and(s, m), m), s), m)) + computed := 0x20 + if x { + let r := shl(7, lt(0xffffffffffffffffffffffffffffffff, x)) + r := or(r, shl(6, lt(0xffffffffffffffff, shr(r, x)))) + r := or(r, shl(5, lt(0xffffffff, shr(r, x)))) + r := or(r, shl(4, lt(0xffff, shr(r, x)))) + r := xor(31, or(shr(3, r), lt(0xff, shr(r, x)))) + computed := r + } + } + assertEq(computed, expected); + } } From af51b5237f6d15dcea78a2c558bbb51335da9081 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Tue, 22 Apr 2025 12:31:18 +0000 Subject: [PATCH 10/15] Opt --- src/utils/LibZip.sol | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/utils/LibZip.sol b/src/utils/LibZip.sol index 804585762f..c303cc28cb 100644 --- a/src/utils/LibZip.sol +++ b/src/utils/LibZip.sol @@ -264,9 +264,8 @@ library LibZip { continue } mstore(o, c) - let r := 0x20 - let x := not(or(or(add(and(c, m), m), c), m)) - if x { r := countLeadingZeroBytes(x) } + let r := not(or(or(add(and(c, m), m), c), m)) + r := add(countLeadingZeroBytes(r), iszero(r)) o := add(min(sub(end, i), r), o) i := add(i, r) if iszero(lt(i, end)) { break } From 93a13c9bb6be03a4be57fe3658374c32f41fa560 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Tue, 22 Apr 2025 12:31:31 +0000 Subject: [PATCH 11/15] Opt --- src/utils/LibZip.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/LibZip.sol b/src/utils/LibZip.sol index c303cc28cb..171308405c 100644 --- a/src/utils/LibZip.sol +++ b/src/utils/LibZip.sol @@ -265,7 +265,7 @@ library LibZip { } mstore(o, c) let r := not(or(or(add(and(c, m), m), c), m)) - r := add(countLeadingZeroBytes(r), iszero(r)) + r := add(iszero(r), countLeadingZeroBytes(r)) o := add(min(sub(end, i), r), o) i := add(i, r) if iszero(lt(i, end)) { break } From 1459f1fa60021b73463649dbe00ed6053bf7e633 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Tue, 22 Apr 2025 12:32:40 +0000 Subject: [PATCH 12/15] T --- src/utils/LibZip.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/LibZip.sol b/src/utils/LibZip.sol index 171308405c..93207fd011 100644 --- a/src/utils/LibZip.sol +++ b/src/utils/LibZip.sol @@ -240,7 +240,7 @@ library LibZip { } if mload(data) { result := mload(0x40) - let m := 0x7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F + let m := 0x7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f let s := add(data, 4) let v := mload(s) let end := add(add(0x20, data), mload(data)) From 57c80293cfd4c62026fac45b5334f24a02cb8bfe Mon Sep 17 00:00:00 2001 From: Vectorized Date: Tue, 22 Apr 2025 12:42:18 +0000 Subject: [PATCH 13/15] T --- test/LibZip.t.sol | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/test/LibZip.t.sol b/test/LibZip.t.sol index 0c17c3957e..a2e3ab394e 100644 --- a/test/LibZip.t.sol +++ b/test/LibZip.t.sol @@ -41,6 +41,16 @@ contract LibZipTest is SoladyTest { bytes internal constant _CD_COMPRESS_OUTPUT = hex"ffe3f51e1c001a2b9cdca0ab00113961790f8baa365051889e4c367d001126d85539440bc844167ac0cc423200177b55939986433925"; + function testABCCdCompressAndDecompressGas() public { + bytes memory data = abi.encode(_A, _B, _C); + assertEq(LibZip.cdDecompress(LibZip.cdCompress(data)).length, data.length); + } + + function testABCCdCompressAndDecompressOriginalGas() public { + bytes memory data = abi.encode(_A, _B, _C); + assertEq(_cdDecompressOriginal(_cdCompressOriginal(data)).length, data.length); + } + function testCdDecompressGas() public { bytes memory data = _CD_COMPRESS_OUTPUT; assertGt(LibZip.cdDecompress(data).length, data.length); @@ -61,15 +71,15 @@ contract LibZipTest is SoladyTest { assertLt(_cdCompressOriginal(data).length, data.length); } - function testStoreABCWithCdCompressGas() public { + function testABCStoreWithCdCompressGas() public { _bytesStorage.set(LibZip.cdCompress(abi.encode(_A, _B, _C))); } - function testStoreABCWithCdCompressOriginalGas() public { + function testABCStoreWithCdCompressOriginalGas() public { _bytesStorage.set(_cdCompressOriginal(abi.encode(_A, _B, _C))); } - function testStoreABCWithFlzCompressGas() public { + function testABCStoreWithFlzCompressGas() public { _bytesStorage.set(LibZip.flzCompress(abi.encode(_A, _B, _C))); } From 6b351f8fb383d61a4c6ebcd38a8dd66071469f8c Mon Sep 17 00:00:00 2001 From: Vectorized Date: Tue, 22 Apr 2025 12:57:07 +0000 Subject: [PATCH 14/15] T --- src/utils/LibZip.sol | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/utils/LibZip.sol b/src/utils/LibZip.sol index 93207fd011..797e6f6974 100644 --- a/src/utils/LibZip.sol +++ b/src/utils/LibZip.sol @@ -264,10 +264,10 @@ library LibZip { continue } mstore(o, c) - let r := not(or(or(add(and(c, m), m), c), m)) - r := add(iszero(r), countLeadingZeroBytes(r)) - o := add(min(sub(end, i), r), o) - i := add(i, r) + c := not(or(or(add(and(c, m), m), c), m)) + c := add(iszero(c), countLeadingZeroBytes(c)) + o := add(min(sub(end, i), c), o) + i := add(c, i) if iszero(lt(i, end)) { break } } mstore(s, v) // Restore the first 4 bytes. From 8648b210c8fa9ad2ab1ff7ee7abebd7e8d9653e8 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Tue, 22 Apr 2025 13:07:54 +0000 Subject: [PATCH 15/15] T --- src/utils/LibZip.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/LibZip.sol b/src/utils/LibZip.sol index 797e6f6974..9fe31a8771 100644 --- a/src/utils/LibZip.sol +++ b/src/utils/LibZip.sol @@ -240,10 +240,10 @@ library LibZip { } if mload(data) { result := mload(0x40) - let m := 0x7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f let s := add(data, 4) let v := mload(s) let end := add(add(0x20, data), mload(data)) + let m := not(shl(7, div(not(iszero(end)), 255))) // `0x7f7f ...`. let o := add(result, 0x20) mstore(s, not(v)) // Bitwise negate the first 4 bytes. for { let i := add(0x20, data) } 1 {} {