Solidity Library

Stop paying for resolution you don't need.

A Solidity library that compresses uint256 values into smaller types for tighter struct packing and lower gas costs.

Solidity EVM Gas Optimization MIT License

Quick Start

Terminal
forge soldeer install uint-quantization-lib
MyContract.sol
import {Quant, UintQuantizationLib} from "uint-quantization-lib/src/UintQuantizationLib.sol"; Quant private immutable SCHEME = UintQuantizationLib.create(32, 24); uint24 stored = uint24(SCHEME.encode(largeValue)); // compress uint256 restored = SCHEME.decode(stored); // decompress

Why Quantization?

Gas Savings

Compress values that don't need full uint256 resolution so more fields fit per storage slot, cutting SSTORE/SLOAD costs.

API

create(shift, targetBits)Create a quantization scheme
encode(value)Floor-encode a value (reverts on overflow)
encode(value, true)Strict encode (reverts if value is not step-aligned)
decode(encoded)Restore lower-bound value
decodeMax(encoded)Restore upper-bound value
fits(value)Check if value is representable
floor(value) / ceil(value)Snap to nearest step boundary
stepSize() / max()Query scheme parameters