Skip to content

Compression libraries: FastLZ, LZ4 and Snappy #5820

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 20 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion contracts/mocks/Stateless.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ import {ERC4337Utils} from "../account/utils/draft-ERC4337Utils.sol";
import {ERC7579Utils} from "../account/utils/draft-ERC7579Utils.sol";
import {ERC7913P256Verifier} from "../utils/cryptography/verifiers/ERC7913P256Verifier.sol";
import {ERC7913RSAVerifier} from "../utils/cryptography/verifiers/ERC7913RSAVerifier.sol";
import {FastLZ} from "../utils/compression/FastLZ.sol";
import {Heap} from "../utils/structs/Heap.sol";
import {InteroperableAddress} from "../utils/draft-InteroperableAddress.sol";
import {LZ4} from "../utils/compression/LZ4.sol";
import {Math} from "../utils/math/Math.sol";
import {Memory} from "../utils/Memory.sol";
import {MerkleProof} from "../utils/cryptography/MerkleProof.sol";
import {MessageHashUtils} from "../utils/cryptography/MessageHashUtils.sol";
import {Nonces} from "../utils/Nonces.sol";
Expand All @@ -47,9 +50,9 @@ import {SafeERC20} from "../token/ERC20/utils/SafeERC20.sol";
import {ShortStrings} from "../utils/ShortStrings.sol";
import {SignatureChecker} from "../utils/cryptography/SignatureChecker.sol";
import {SignedMath} from "../utils/math/SignedMath.sol";
import {Snappy} from "../utils/compression/Snappy.sol";
import {StorageSlot} from "../utils/StorageSlot.sol";
import {Strings} from "../utils/Strings.sol";
import {Memory} from "../utils/Memory.sol";
import {Time} from "../utils/types/Time.sol";

contract Dummy1234 {}
136 changes: 136 additions & 0 deletions contracts/utils/compression/FastLZ.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.20;

/**
* @dev Library for decompressing data using FastLZ.
*
* See https://ariya.github.io/FastLZ/
*/
library FastLZ {
/**
* @dev FastLZ level 1 decompression.
*
* Based on the reference implementation available here:
* https://github.com/ariya/FastLZ?tab=readme-ov-file#decompressor-reference-implementation
*/
function decompress(bytes memory input) internal pure returns (bytes memory output) {
assembly ("memory-safe") {
let inputPtr := add(input, 0x20)
let inputEnd := add(add(input, 0x20), mload(input))

// Use new memory allocate at the FMP
output := mload(0x40)
let outputPtr := add(output, 0x20)

for {} lt(inputPtr, inputEnd) {} {
let chunk := mload(inputPtr)
let first := byte(0, chunk)
let type_ := shr(5, first)

switch type_
case 0 {
mstore(outputPtr, mload(add(inputPtr, 1)))
inputPtr := add(inputPtr, add(2, first))
outputPtr := add(outputPtr, add(1, first))
}
case 7 {
let len := add(9, byte(1, chunk))
for {
let i := 0
let ofs := add(add(shl(8, and(first, 31)), byte(2, chunk)), 1)
let ref := sub(outputPtr, ofs)
let step := xor(len, mul(lt(ofs, len), xor(ofs, len)))
} lt(i, len) {
i := add(i, step)
} {
mcopy(add(outputPtr, i), add(ref, i), step)
}
inputPtr := add(inputPtr, 3)
outputPtr := add(outputPtr, len)
}
default {
let len := add(2, type_)
for {
let i := 0
let ofs := add(add(shl(8, and(first, 31)), byte(1, chunk)), 1)
let ref := sub(outputPtr, ofs)
let step := xor(len, mul(lt(ofs, len), xor(ofs, len)))
} lt(i, len) {
i := add(i, step)
} {
mcopy(add(outputPtr, i), add(ref, i), step)
}
inputPtr := add(inputPtr, 2)
outputPtr := add(outputPtr, len)
}
}
if iszero(eq(inputPtr, inputEnd)) {
revert(0, 0)
}

mstore(output, sub(outputPtr, add(output, 0x20)))
mstore(0x40, outputPtr)
}
}

function decompressCalldata(bytes calldata input) internal pure returns (bytes memory output) {
assembly ("memory-safe") {
let inputPtr := input.offset
let inputEnd := add(input.offset, input.length)

// Use new memory allocate at the FMP
output := mload(0x40)
let outputPtr := add(output, 0x20)

for {} lt(inputPtr, inputEnd) {} {
let chunk := calldataload(inputPtr)
let first := byte(0, chunk)
let type_ := shr(5, first)

switch type_
case 0 {
mstore(outputPtr, calldataload(add(inputPtr, 1)))
inputPtr := add(inputPtr, add(2, first))
outputPtr := add(outputPtr, add(1, first))
}
case 7 {
let len := add(9, byte(1, chunk))
for {
let i := 0
let ofs := add(add(shl(8, and(first, 31)), byte(2, chunk)), 1)
let ref := sub(outputPtr, ofs)
let step := xor(len, mul(lt(ofs, len), xor(ofs, len)))
} lt(i, len) {
i := add(i, step)
} {
mcopy(add(outputPtr, i), add(ref, i), step)
}
inputPtr := add(inputPtr, 3)
outputPtr := add(outputPtr, len)
}
default {
let len := add(2, type_)
for {
let i := 0
let ofs := add(add(shl(8, and(first, 31)), byte(1, chunk)), 1)
let ref := sub(outputPtr, ofs)
let step := xor(len, mul(lt(ofs, len), xor(ofs, len)))
} lt(i, len) {
i := add(i, step)
} {
mcopy(add(outputPtr, i), add(ref, i), step)
}
inputPtr := add(inputPtr, 2)
outputPtr := add(outputPtr, len)
}
}
if iszero(eq(inputPtr, inputEnd)) {
revert(0, 0)
}

mstore(output, sub(outputPtr, add(output, 0x20)))
mstore(0x40, outputPtr)
}
}
}
Loading
Loading