Skip to content

Clean up the handling of bool types in assembly for pedantic correctness#559

Open
duncancmt wants to merge 37 commits into
masterfrom
dcmt/bool
Open

Clean up the handling of bool types in assembly for pedantic correctness#559
duncancmt wants to merge 37 commits into
masterfrom
dcmt/bool

Conversation

@duncancmt

Copy link
Copy Markdown
Collaborator

duncancmt and others added 9 commits April 15, 2026 16:32
Solidity `bool` may be any nonzero value for truthy when it enters an
assembly block. This commit normalizes bool values at every site where
they are used in arithmetic, bitwise operations, ABI encoding for
external calls, or (transient) storage writes.

Utility libraries (highest leverage — inlined throughout codebase):
- Ternary: use iszero-inversion trick (single iszero, 3 gas) for
  ternary/maybeSwap; lt(0x00, c) for orZero
- FastLogic.and: rewrite via De Morgan (was wrong: and(2,1)=0)
- FastLogic.andNot: rewrite via gt(iszero(b), iszero(a))
- FastLogic.toUint, Math.toInt: normalize with lt(0x00, b)
- UnsafeMath.unsafeInc/Dec(bool), Math.inc/dec: normalize with lt(0x00, b)

External call encoding (mstore/mstore8/shl of bool):
- EkuboV2, EkuboV3, MaverickV2, PancakeInfinity, UniswapV3Fork,
  FlashAccountingCommon, SafePermit, Hanji: wrap bool in lt(0x00, x)

Transient storage:
- CurveTricrypto: clean on write and read

External callback data validation:
- EkuboV2: revert with empty reason if bool > 1

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- FastLogic.andNot: use iszero(or(iszero(a), b)) — cleaner formulation
- CurveTricrypto: remove redundant clean-on-read (write-side suffices)
- CrossChainReceiverFactory: normalize bool immutable in mul (london EVM,
  uses iszero(iszero) since no PUSH0)
- SettlerIntent.setSolver: normalize addNotRemove for xor/mul/sstore use
- EulerSwap: validate bool from external staticcall returndata; normalize
  zeroForOne before shl
- UniswapV2: normalize zeroForOne before shl in both getReserves and swap
- FlashAccountingCommon, UniswapV3Fork: upgrade silent and(0x01,...) mask
  to reverting validation for callback bool data

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- FastLogic.and: mul(a, lt(0x00, b)) — cheaper than De Morgan
  (10 gas on Osaka vs 12; 11 on London vs 12)
- FastLogic.andNot: mul(a, iszero(b)) — 8 gas, no PUSH0 needed
- CrossChainReceiverFactory.deploy: normalize setOwnerNotCleanup
  bool before arithmetic use (iszero/iszero for London EVM)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@duncancmt duncancmt self-assigned this Apr 15, 2026
@duncancmt duncancmt requested review from e1Ru1o and jparklev June 12, 2026 10:52
@duncancmt duncancmt marked this pull request as ready for review June 12, 2026 10:52
@duncancmt duncancmt requested a review from dekz as a code owner June 12, 2026 10:52
@immunefi-magnus

Copy link
Copy Markdown

🛡️ Immunefi PR Reviews

We noticed that your project isn't set up for automatic code reviews. If you'd like this PR reviewed by the Immunefi team, you can request it manually using the link below:

🔗 Send this PR in for review

Once submitted, we'll take care of assigning a reviewer and follow up here.

duncancmt and others added 3 commits June 12, 2026 13:24
Co-Authored-By: Codex <codex@openai.com>
Co-Authored-By: Codex <codex@openai.com>

@e1Ru1o e1Ru1o left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general I wonder if it is better just to mock/expect calls instead of having all of this Mock contracts

Also in UniswapV3UnitTest.t.sol you did a test about isForwarded being dirty, I might want to do that for the other files with a similar modification, e.g EkuboV2, FlashAccounting

Comment thread src/CrossChainReceiverFactory.sol
Comment thread test/0.8.25/BoolBoundary.t.sol Outdated
Comment thread test/0.8.25/BoolBoundary.t.sol Outdated
Comment thread test/0.8.25/BoolBoundary.t.sol
Comment thread test/0.8.25/BoolBoundary.t.sol Outdated
Comment thread test/0.8.25/BoolBoundary.t.sol
Comment thread test/0.8.25/BoolBoundary.t.sol

@jparklev jparklev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yay pendantic correctness, boo dirty bits. a few small things

Comment thread src/CrossChainReceiverFactory.sol
Comment thread test/unit/core/UniswapV3UnitTest.t.sol Outdated
Comment thread test/0.8.25/BoolBoundary.t.sol
@duncancmt duncancmt requested review from e1Ru1o and jparklev June 19, 2026 14:46
duncancmt and others added 2 commits June 19, 2026 17:15
Co-Authored-By: OpenAI Codex <codex@openai.com>
Update integration gas snapshots and regenerated README gas tables.

Co-Authored-By: OpenAI Codex <codex@openai.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants