ERC-3772 · Stagnant · ERC · 2021-08-27
ERC-3772: Compressed Integers
A convention for squeezing uint256 money amounts into 64-bit (or other smaller) values using significant bits plus a shift, trading a tiny, controlled precision loss for cheaper storage.
What problem does it solve?
Token amounts are almost always stored as uint256, which takes a full 32-byte storage slot. Yet the values people actually use span a much smaller range: the author estimates that about 1015 distinct amounts (roughly 50 bits of information) cover real-world balances for a given token. Most of the 256 bits are wasted, and storage is the most expensive thing a contract does.
ERC-3772 proposes a standard lossy compression so several amounts can be packed into one slot, cutting SSTORE costs.
How it works
A compressed integer cintx is an x-bit value split into two parts:
- significant: the top bits of the original number, starting from its first
1bit. - shift: how many places to shift
significantleft to rebuild the number.
For cint64 up to cint120, the lowest 8 bits hold the shift and the rest hold the significant bits (cint64 = 56 significant bits + 8 shift bits). For cint128 up to cint248, 7 bits are enough for the shift.
The significant bits are placed in the high part on purpose: if a developer forgets to decompress, a small value will not accidentally look correct.
Two decompression modes are defined, because compression always rounds down:
decompress: shift the significant bits back left, filling with zeros (rounds down).decompressRoundingUp: same, but fill the dropped low bits with ones (rounds up).
The recommended rule: when the contract pays out, use the rounded-down value; when it charges a user, use the rounded-up value. That way rounding never costs the contract money, similar to how careful integer division is handled.
Compressed values are meant for internal storage only. Contracts should expose and do arithmetic on full uint256 values.
Sample code
A minimal, self-contained cint64 codec (illustrative; not audited):
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
library CInt64 {
function compress(uint256 full) internal pure returns (uint64) {
if (full < (1 << 56)) return uint64(full) << 8; // fits, shift = 0
uint256 bits; // bit length of `full`
for (uint256 v = full; v != 0; v >>= 1) bits++;
uint256 shift = bits - 56; // keep top 56 bits
return (uint64(full >> shift) << 8) | uint64(shift);
}
function decompress(uint64 c) internal pure returns (uint256) {
return uint256(c >> 8) << (c & 0xff);
}
function decompressRoundingUp(uint64 c) internal pure returns (uint256) {
uint256 shift = c & 0xff;
return (uint256(c >> 8) << shift) | ((1 << shift) - 1);
}
}
contract Vault {
using CInt64 for uint256;
using CInt64 for uint64;
struct Position { uint64 amountC; address owner; } // packs into one slot
mapping(uint256 => Position) public positions;
}
Things to know
- Status: Stagnant. It is a coding convention, not a protocol change, and was never finalised.
- Compression is lossy:
decompress(compress(x)) <= x. Forcint64the error is below 2shift, i.e. the 57th significant bit onward is dropped. - Gas savings only appear if the compiler can pack the smaller value with other fields into the same slot (field order in structs matters).
- Do not multiply two compressed values and recompress; precision collapses. Decompress first, then do maths.
- The spec's own reference snippet has small bugs (e.g. masking the shift with
% (1 << 9)); write and test your own codec.