Skip to content

refactor: replace is_power_of_ten lookup tables with log10_floor + pow macro#323

Merged
bidzyyys merged 12 commits into
mainfrom
295-n-04-redundant-lookup-tables-in-is_power_of_ten
May 20, 2026
Merged

refactor: replace is_power_of_ten lookup tables with log10_floor + pow macro#323
bidzyyys merged 12 commits into
mainfrom
295-n-04-redundant-lookup-tables-in-is_power_of_ten

Conversation

@bidzyyys

@bidzyyys bidzyyys commented May 19, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Adds public(package) macro fun is_power_of_ten<$Int> in internal/macros.move. The macro widens the input to u256, short-circuits on zero, and verifies n == 10^log10_floor(n) using the existing log10_floor and std::u256::pow primitives.
  • u128::is_power_of_ten and u256::is_power_of_ten now delegate to the macro. The hardcoded lookup tables (39 entries for u128, 78 entries for u256) are deleted.
  • u8 / u16 / u32 / u64 is_power_of_ten inline disjunctions are unchanged per the team's gas-report decision.
  • Public API unchanged.

Closes #295.

…w macro

Closes #295.

Public API unchanged. The new public(package) macro `is_power_of_ten!` in
internal/macros.move computes the predicate via the existing log10_floor
and std::u256::pow primitives, eliminating the duplicated power-of-ten
constants that previously caused the missing 10^77 entry (fixed in #291).
Only u128 and u256 wrappers are retargeted per the team's gas-report
decision; u8 through u64 keep their inline disjunctions.
@coderabbitai

coderabbitai Bot commented May 19, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 0975b130-314b-4710-ad59-b107e9930980

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Walkthrough

This PR replaces hardcoded lookup tables in u128::is_power_of_ten and u256::is_power_of_ten with a shared macro-based implementation using log10_floor and pow computation. The refactoring eliminates a missing 10^77 entry from the prior u256 lookup table while preserving public API signatures.

Changes

Power-of-Ten Computation Refactor

Layer / File(s) Summary
is_power_of_ten macro implementation
math/core/sources/internal/macros.move
New generic is_power_of_ten<$Int> macro casts input to u256, returns false for zero, computes exponent via log10_floor, and verifies n == std::u256::pow(10, exp).
u128 and u256 public API refactoring
math/core/sources/u128.move, math/core/sources/u256.move
Both u128::is_power_of_ten and u256::is_power_of_ten replace lookup vectors and binary search with single macros::is_power_of_ten! calls; outdated implementation comments removed.
Comprehensive macro test module
math/core/tests/macros/is_power_of_ten.move
New 17-test module covers zero, one, sampled powers, boundary values, non-powers, generic types (u8u64), log10_floor cascade threshold at k=64, and max-value edge cases.
Existing test function updates
math/core/tests/u128_tests.move, math/core/tests/u256_tests.move
Rename is_power_of_ten_binary_search_paths to is_power_of_ten_diverse_inputs and update comments to reflect test strategy rather than implementation details.
Changelog documentation
CHANGELOG.md
Document implementation change from lookup table to log10_floorpow computation, noting the fix for missing 10^77 entry with unchanged public API.

🎯 2 (Simple) | ⏱️ ~12 minutes

Suggested reviewers

  • ericnordelo
  • 0xNeshi

🐰 A table once stood tall with pow'rs of ten,
Now log10_floor does the math again,
No more lookup lists, just compute and check,
Ten-seventy-seven is back on the deck!
Generic macros span from eight to two-five-six,
Math's now cleaner—a most elegant fix!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main refactoring: replacing lookup tables with a macro-based approach using log10_floor and pow.
Linked Issues check ✅ Passed All requirements from #295 are met: lookup tables eliminated, is_power_of_ten implemented via log10_floor + pow, maintenance surface reduced, and logic centralized.
Out of Scope Changes check ✅ Passed All changes are directly related to the refactoring objective; test renaming/comment updates align with the implementation changes and audit recommendations.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description check ✅ Passed PR description comprehensively covers the changes: explains the new macro implementation, specifies which functions delegate to it, notes the lookup table removals, clarifies unchanged APIs, and references the resolved issue.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch 295-n-04-redundant-lookup-tables-in-is_power_of_ten

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@CHANGELOG.md`:
- Line 28: Update the CHANGELOG entry for u128::is_power_of_ten and
u256::is_power_of_ten to append the PR reference " (`#323`)" at the end of the
sentence, and apply the proposed wording tweaks: change "lookup table" to
"lookup tables" and "caused" to "led to" so the sentence reads with the PR
reference included and the clearer phrasing. Ensure the public API unchanged
clause remains intact and that the final line ends with the PR reference.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 9e5a7b92-6476-4ad8-b33a-0167dc2da0f0

📥 Commits

Reviewing files that changed from the base of the PR and between 4c036d1 and e0db19f.

📒 Files selected for processing (7)
  • CHANGELOG.md
  • math/core/sources/internal/macros.move
  • math/core/sources/u128.move
  • math/core/sources/u256.move
  • math/core/tests/macros/is_power_of_ten.move
  • math/core/tests/u128_tests.move
  • math/core/tests/u256_tests.move

Comment thread CHANGELOG.md Outdated
@codecov

codecov Bot commented May 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.88%. Comparing base (4c036d1) to head (586c542).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #323      +/-   ##
==========================================
- Coverage   95.91%   95.88%   -0.03%     
==========================================
  Files          22       22              
  Lines        2202     2186      -16     
  Branches      599      591       -8     
==========================================
- Hits         2112     2096      -16     
  Misses         65       65              
  Partials       25       25              
Flag Coverage Δ
contracts/access 64.03% <ø> (ø)
math/core 86.12% <100.00%> (-0.18%) ⬇️
math/fixed_point 55.61% <0.00%> (+0.30%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@bidzyyys bidzyyys marked this pull request as draft May 19, 2026 16:05
Comment thread math/core/tests/macros/is_power_of_ten.move Outdated
Comment thread math/core/sources/internal/macros.move Outdated
Comment thread CHANGELOG.md Outdated
@bidzyyys bidzyyys requested a review from 0xNeshi May 19, 2026 16:24
bidzyyys and others added 2 commits May 19, 2026 18:24
Co-authored-by: Nenad <nenad.misic@openzeppelin.com>
@bidzyyys bidzyyys marked this pull request as ready for review May 19, 2026 16:27
Comment thread math/core/sources/internal/macros.move Outdated
Comment thread math/core/sources/u128.move
Comment thread math/core/sources/u256.move
Comment thread math/core/tests/macros/is_power_of_ten.move
@bidzyyys bidzyyys requested a review from 0xNeshi May 20, 2026 06:51

@immrsd immrsd left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM!
Pushed couple commits to remove no longer used binary_search helper and add more tests

@bidzyyys bidzyyys merged commit 0c81bab into main May 20, 2026
13 checks passed
@bidzyyys bidzyyys deleted the 295-n-04-redundant-lookup-tables-in-is_power_of_ten branch May 20, 2026 12:23
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.

[N-04]: Redundant Lookup Tables in is_power_of_ten Can Be Replaced by Existing Primitives

3 participants