ERC-3772 Saving storage with compressed integers (Compressed Integers)
Stagnant · Other · created 2021-08-27
Defines a compression scheme that splits a uint256 amount into a significant bits portion and a shift value to fit it into a smaller type such as cint64. This reduces the storage cost of storing money by up to 4x.
Why it is needed
Storage slots are expensive, costing about $0.80 to initialize and about $0.20 to update (at 20 gwei, ETH at $2000). Yet while uint256, the type commonly used to store amounts, can represent values from 10^-18 to 10^58, the amounts actually handled are usually concentrated between 0.001 and 1 trillion (10^12), so most of this wide range goes to waste. Representing a value around 10^15 only needs about 50 bits, yet the full 256 bits get used anyway. Without a common standard, each contract ends up designing its own bit-packing scheme to cut costs, and since compression methods and rounding handling differ by implementation, the risk of fund loss or misinterpretation grows.
What to implement
function compress(uint256) internal returns (cint64) {}: Takes a uint256 amount and converts it into a cint64 compressed value made up of significant and shift.function decompress(cint64) internal returns (uint256) {}: Restores a uint256 by shifting the significant bits of a cint64 left by shift (rounding down, with lower bits set to 0).function decompressRoundingUp(cint64) internal returns (uint256) {}: Restores a value the same way as decompress, but fills all the emptied lower shift bits with 1, returning a rounded-up value.
In depth
The cintx structure: significant and shift
A compressed value is represented as a struct called cintx, split into two parts. The significant bits, starting from the highest set bit of the uint256, are stored in significant, and the number of times those significant bits must be shifted left to recover the original value is stored in shift. From cint64 through cint120, the lowest 8 bits are used for shift and the rest for significant (e.g., cint64 = uint56 significant + uint8 shift). From cint128 through cint248, the lowest 7 bits are used for shift and the rest for significant (e.g., cint128 = uint121 significant + uint7 shift). Placing significant in the upper bits and shift in the lower bits is the arrangement fixed by the spec.
The compress operation
compress takes the value starting from the leftmost set bit of a uint256, keeps as many bits as the significant bit width calls for and puts them into significant, and records the number of bits the value was shifted right by into shift. For example, compressing a value of 2^100 into cint64 gives a significant of 1 followed by 55 zeros, with shift equal to 45. In this process, lower bits smaller than the significant bit width are discarded, so compression is always a lossy process that produces a result less than or equal to the actual value.
Decompression: decompress and decompressRoundingUp
decompress restores the value by shifting the significant bits left by shift and filling the vacated positions with 0 (rounding down). decompressRoundingUp shifts in the same way but fills the vacated lower shift bits with all 1s (rounding up). The difference between the two values is the error range introduced by compression. The spec mandates using decompress when a contract sends money to a user, and decompressRoundingUp when the contract receives money from a user. This way the error always goes slightly against the user, and the contract never takes a loss.
Magnitude of error
The significant field of cint64 is 56 bits. When a value is near 2^m, the maximum error is 2^(m-56) minus 1, which in decimal terms is roughly on the order of 10^(n-17) (where n is the number of digits plus 1). For example, storing a value on the order of 1 trillion in cint64 gives a maximum error of around 0.0001, and for a value on the order of 1 million the error is close to zero. The spec explains that this error has the same character as the rounding loss commonly seen in integer division, and is negligible compared to the benefit of saving a storage slot.
Caution during computation: entropy compounding
Multiplying or dividing two compressed values combines the roughly 50 bits of information (entropy) each carries, increasing the information content of the result, and compressing that result back into cint64 (56 bits) sharply cuts precision. The spec cites directly multiplying compressed shares and price and then reusing the compressed value as an incorrect example, and instead recommends decompressing the values first, performing the multiplication or division, and only recompressing when needed. In other words, cintx is a storage-only format, and arithmetic must always be done on decompressed values.
Scope of application: limited to monetary amounts
This compression scheme is only suitable for amount values with a narrow distribution, such as those between 0.001 and 1 trillion. Compressing an arbitrary uint256 with an unknown distribution using the same method can introduce unexpected error, so the spec explicitly states not to apply compression when uncertain. Also, when dealing with tokens that are extremely volatile relative to the native currency (very rare cases where price can spike by up to 2^56 times), the 56 bits of significant in cint64 may not be enough, and the spec recommends using a format with more significant bits, such as cint96 or cint128, in those cases.
Not exposed outside the contract boundary
The spec states that cintx values are meant for internal contract state management and must not be exposed to the outside world, such as other contracts or clients. If a value needs to be shown externally, the contract must expose the decompressed value.
Proposal status
This proposal is in Stagnant status, meaning discussion has stalled and it has not yet been finalized.
Things to watch out for
- Compression is always a lossy process that rounds values down: Decompressing a compressed value yields a result less than or equal to the original value. To avoid putting the contract at a loss, decompress (round down) must be used when paying out and decompressRoundingUp (round up) must be used when collecting.
- Computing with compressed values causes a large loss of precision: Multiplying or dividing compressed values and then recompressing the result sharply cuts precision, so arithmetic must always be performed on decompressed values.
- Must not be used for values with an unknown distribution: This compression is only suitable for amounts distributed within a narrow range; compressing an arbitrary uint256 with an uncertain distribution can introduce unexpected error.
- Extremely volatile tokens need a larger format: Tokens whose price spikes extremely high may exceed what cint64's 56 bits of significant can represent, so a larger format such as cint96 or cint128 should be considered.