From e56e814e53ae9da743c2e3b4051f34c518707dcb Mon Sep 17 00:00:00 2001 From: thedavidmeister Date: Thu, 14 May 2026 22:26:46 +0400 Subject: [PATCH] test: pin fromFixedDecimalLosslessPacked(x, 0) bitwise identity MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Asserts `Float.unwrap(fromFixedDecimalLosslessPacked(value, 0)) == bytes32(value)` for every value in `[0, type(int224).max]`. Pulls the invariant into this library where it belongs — downstream callers (e.g. Rainlang's EVM opcodes for block.number / block.timestamp / chainid) can write the raw integer straight to the stack as a documented optimization without re-asserting it in their own test suites. Co-Authored-By: Claude Opus 4.7 (1M context) --- test/src/lib/LibDecimalFloat.decimalLossless.t.sol | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/src/lib/LibDecimalFloat.decimalLossless.t.sol b/test/src/lib/LibDecimalFloat.decimalLossless.t.sol index c632d42..cc460e1 100644 --- a/test/src/lib/LibDecimalFloat.decimalLossless.t.sol +++ b/test/src/lib/LibDecimalFloat.decimalLossless.t.sol @@ -33,6 +33,18 @@ contract LibDecimalFloatDecimalLosslessTest is Test { return float.toFixedDecimalLossless(decimals); } + /// `fromFixedDecimalLosslessPacked(value, 0)` is the bitwise identity + /// `bytes32(value)` for every `value` that fits in `int224`. Pinned + /// here so the float library owns the invariant — callers (e.g. the + /// Rainlang EVM opcodes for `block.number`, `block.timestamp`, + /// `chainid`) can write the raw integer to the stack as a documented + /// optimization without re-discovering or re-asserting it. + function testFromFixedDecimalLosslessPackedZeroDecimalsIsIdentity(uint256 value) external pure { + value = bound(value, 0, uint256(int256(type(int224).max))); + Float result = LibDecimalFloat.fromFixedDecimalLosslessPacked(value, 0); + assertEq(Float.unwrap(result), bytes32(value)); + } + function testFromFixedDecimalLosslessMem(uint256 value, uint8 decimals) external { value = bound(value, 0, uint256(int256(type(int224).max))); (,, bool losslessPreflight) = LibDecimalFloat.fromFixedDecimalLossy(value, decimals);