Skip to content

Commit 9dc0ec6

Browse files
committed
refactor: document Quant validation rationale, demote extreme benchmark to log-only
Finding 1 (Quant forgeable): intentional design choice, now documented in NatSpec. All functions are internal pure; the expected pattern is a single create() at construction. Per-call validation would penalize correct usage for a scenario that only arises from explicit wrap() misuse. Finding 2 (extreme benchmark): the 80% savings threshold was a property of the 12:1 slot ratio, not the library. Converted to log-only so it serves as documentation rather than a misleading regression gate. Real-life benchmark (>= 32%) remains as the CI gate.
1 parent 013cc6f commit 9dc0ec6

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

AGENTS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Production-style contracts for gas benchmarks only. Raw vs quantized pairs: `Raw
3535
### Tests: `test/`
3636

3737
- `UintQuantizationLib.t.sol`: `QuantHarness` exposes library via method-call syntax for external calls. `UintQuantizationLibSmokeTest` has concrete regression tests and fuzz tests. Fuzz parameters use `uint8` for discardedBitWidth/encodedBitWidth; use `bound()` over `vm.assume` for value-in-range constraints.
38-
- `showcase/ShowcaseGas.t.sol`: Asserts quantized paths save >= 32% (real-life) and >= 80% (extreme) gas vs raw on zero-to-nonzero writes.
38+
- `showcase/ShowcaseGas.t.sol`: Real-life benchmark asserts >= 32% gas savings vs raw on zero-to-nonzero writes. Extreme benchmark logs gas numbers for documentation (not a regression gate).
3939

4040
### Configuration
4141

src/UintQuantizationLib.sol

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ pragma solidity ^0.8.25;
2626
* **Important:** Always construct `Quant` values via `create()`. Using `Quant.wrap()`
2727
* directly bypasses validation and produces undefined behavior in all library functions.
2828
* Use `isValid()` to check a `Quant` of unknown origin.
29+
*
30+
* Design note: library functions do not validate `q` on every call. All functions are
31+
* `internal pure` and run inside the integrator's contract; the expected pattern is a
32+
* single `create()` at construction time (typically `immutable`). Adding per-call
33+
* validation would penalize the correct-usage path for a scenario that only arises
34+
* from explicit `Quant.wrap()` misuse.
2935
*/
3036
type Quant is uint16;
3137

test/showcase/ShowcaseGas.t.sol

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ contract ShowcaseGasTest is Test {
2020
uint256 internal constant EXT_LANE_MAX = (uint256(1) << EXT_WIDTH) - 1;
2121

2222
uint256 internal constant MIN_REAL_SAVINGS_BPS = 3_200; // >=32%
23-
uint256 internal constant MIN_EXTREME_SAVINGS_BPS = 8_000; // >=80%
2423

2524
uint256 internal constant REAL_STAKE_STRICT = uint256(2_500_000) << REAL_SHIFT;
2625
uint256 internal constant REAL_STAKE_STRICT_ALT = uint256(2_750_000) << REAL_SHIFT;
@@ -69,15 +68,18 @@ contract ShowcaseGasTest is Test {
6968
assertGe(_savingsBps(rawGas, strictGas), MIN_REAL_SAVINGS_BPS);
7069
}
7170

72-
function test_gas_extreme_solidity_zero_to_nonzero_savings_ge_target() public {
71+
/// @notice Logs extreme-packing gas numbers for documentation. Not a regression gate:
72+
/// the savings are a property of the 12:1 slot ratio, not the library's efficiency.
73+
function test_gas_extreme_solidity_zero_to_nonzero_logs() public {
7374
uint256 rawGas = _measureSolidityExtremeRaw();
7475
uint256 floorGas = _measureSolidityExtremeQuantFloor();
7576
uint256 strictGas = _measureSolidityExtremeQuantStrict();
7677

77-
assertGt(rawGas, floorGas);
78-
assertGt(rawGas, strictGas);
79-
assertGe(_savingsBps(rawGas, floorGas), MIN_EXTREME_SAVINGS_BPS);
80-
assertGe(_savingsBps(rawGas, strictGas), MIN_EXTREME_SAVINGS_BPS);
78+
emit log_named_uint("extreme raw gas", rawGas);
79+
emit log_named_uint("extreme floor gas", floorGas);
80+
emit log_named_uint("extreme strict gas", strictGas);
81+
emit log_named_uint("extreme floor savings bps", _savingsBps(rawGas, floorGas));
82+
emit log_named_uint("extreme strict savings bps", _savingsBps(rawGas, strictGas));
8183
}
8284

8385
function test_showcase_inputs_fit_lanes() public pure {

0 commit comments

Comments
 (0)