Skip to content

Port highly-optimized sqrt and cbrt from Solady and 0x-Settler - #6

Merged
ZeframLou merged 21 commits into
Bacon-labs:mainfrom
duncancmt:dcmt/golf-roots
May 26, 2026
Merged

Port highly-optimized sqrt and cbrt from Solady and 0x-Settler#6
ZeframLou merged 21 commits into
Bacon-labs:mainfrom
duncancmt:dcmt/golf-roots

Conversation

@duncancmt

@duncancmt duncancmt commented May 14, 2026

Copy link
Copy Markdown
Contributor

Vectorized/solady#1515 , Vectorized/solady#1523 , and Vectorized/solady#1527 implement highly-optimized sqrt and cbrt routines. 0xProject/0x-settler#511 includes a Lean proof of correctness for that implementation. Port both the implementation and the proof into Tamago. Commit messages correctly attribute which changes were produced with AI assistance (porting the proof) and which are "meatbag" changes (porting the implementation).

The implementation of these optimized routines requires clz, which is not implemented in Verity. Accordingly, the optimized, pre-Osaka clz implementation using the de Bruijn table trick has also been ported to Verity. Obviously, this can be removed quite simply once support for clz arrives in Verity.

duncancmt and others added 9 commits May 13, 2026 17:05
Add a shared `clz` helper using the de Bruijn trick
Add clz, sqrt, and cbrt proofs that connect the Verity equations to the mathematical specifications for the optimized implementations.

Co-Authored-By: Codex <codex@openai.com>
Compute the final sqrt and cbrt correction by subtracting the comparison flag directly from the Newton estimate.

Co-Authored-By: Codex <codex@openai.com>
Update the sqrt and cbrt correctness proofs for the flag-subtraction correction and keep the cbrt small-range bridge over the generated Verity execution path.

Co-Authored-By: Codex <codex@openai.com>
Restructure the proof suites into parallel Sqrt/ and Cbrt/ directories
with consistent naming: Model, FloorBound, Contraction, OctaveCert,
ErrorChain, Wiring, Correctness (+ OverflowSafety for cbrt).

Key changes:
- Rename bstep -> sqrtStep, babylon_step_floor_bound -> sqrt_step_floor_bound
- Consistent namespaces: Sqrt.{File} / Cbrt.{File} everywhere
- Split monolithic SqrtCorrect (375L) and CbrtCorrect (747L) into focused modules
- Remove ~335 lines of dead code: StepMono, SmallRange, Sqrt512Cert,
  perfect-cube exactness, unused bracket/witness wrappers, cbrt run6From
- Add Sqrt and Cbrt lean_libs to lakefile.toml
- Update FixedPointMathLibProof consumer imports and identifiers

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Remove an unused proof option, correct the cbrt wiring module comment, and fix proof file whitespace.

Co-Authored-By: Codex <codex@openai.com>
Export clz through the FixedPointMathLib helper namespace, add its spec/proof/test/generated surface, and move Sqrt/Cbrt proof modules under Tamago.Proof.Utils.

Co-Authored-By: Codex <codex@openai.com>
duncancmt and others added 4 commits May 17, 2026 11:24
Co-Authored-By: atarpara <akpatel0618@gmail.com>
Co-Authored-By: Codex <codex@openai.com>
Co-Authored-By: atarpara <akpatel0618@gmail.com>
Co-Authored-By: Codex <codex@openai.com>
Co-Authored-By: Codex <codex@openai.com>
duncancmt and others added 2 commits May 23, 2026 16:38
Co-Authored-By: Codex <codex@openai.com>
Co-Authored-By: Codex <codex@openai.com>
@ZeframLou

Copy link
Copy Markdown
Contributor

Looks great, appreciate this PR! CI is failing because selfBalance needs to be included in [trust.allow_surfaces], I'm making a fix.

ZeframLou and others added 2 commits May 25, 2026 18:01
The Verity pin bump surfaces WETH's withdraw/internal_withdraw reads of
SELFBALANCE as a partially modeled runtime introspection surface, which
`tama audit` rejects unless declared. Add `selfBalance` to
[trust.allow_surfaces] to keep the trust boundary explicit.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
`tama.lock` tracks a SHA-256 of `tama.toml` under [inputs]. Adding the
[trust.allow_surfaces] section changed that hash, so the lock was stale
and CI's `git diff --exit-code -- tama.lock ...` guard failed. Update the
tracked hash to match.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@ZeframLou
ZeframLou merged commit db6a2ac into Bacon-labs:main May 26, 2026
1 check passed
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.

2 participants