Skip to content

fix: Fixed-Point pow Now Uses Binary Exponentiation#281

Open
0xNeshi wants to merge 26 commits intorelease-v1.1from
fix/audit-l-04
Open

fix: Fixed-Point pow Now Uses Binary Exponentiation#281
0xNeshi wants to merge 26 commits intorelease-v1.1from
fix/audit-l-04

Conversation

@0xNeshi
Copy link
Copy Markdown
Collaborator

@0xNeshi 0xNeshi commented Mar 30, 2026

Switches UD30x9 and SD29x9 pow from linear repeated multiplication to binary exponentiation, addressing the performance and accumulated truncation concerns from issue #272.

Changes:

  • Simplified UD30x9::pow and SD29x9::pow to delegate to the shared helper, eliminating per-iteration casts and redundant loop code.
  • Updated pow documentation in both modules to clarify truncation and associativity implications under binary exponentiation grouping.
  • Changed the SD29x9 overflow test to use the minimal failing case sd29x9::max().pow(3) (fails the base_mag <= MIN_NEGATIVE_VALUE assertion).

PR Checklist

  • Tests
  • Documentation
  • Changelog

Summary by CodeRabbit

  • Refactor

    • Updated power calculation implementation to use an optimized exponentiation algorithm with improved overflow handling. The computation of intermediate steps may differ from previous versions, particularly in edge cases with high exponents and fixed-point truncation.
  • Tests

    • Refactored test assertions for power functions to verify updated computation behavior and edge case handling.

@0xNeshi 0xNeshi self-assigned this Mar 30, 2026
Copilot AI review requested due to automatic review settings March 30, 2026 16:23
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 30, 2026

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and concisely summarizes the primary change: switching fixed-point pow implementations to binary exponentiation.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description check ✅ Passed The PR description clearly describes the changes, includes context linking to issue #272, explains the rationale, and addresses the checklist items. Tests and documentation are marked complete; changelog is acknowledged as pending.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/audit-l-04

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

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 30, 2026

Codecov Report

❌ Patch coverage is 95.45455% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 89.79%. Comparing base (e8dea76) to head (9c1bbae).

Files with missing lines Patch % Lines
math/fixed_point/sources/ud30x9/ud30x9_base.move 90.90% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@               Coverage Diff                @@
##           release-v1.1     #281      +/-   ##
================================================
- Coverage         89.87%   89.79%   -0.08%     
================================================
  Files                19       19              
  Lines              1787     1784       -3     
  Branches            484      490       +6     
================================================
- Hits               1606     1602       -4     
  Misses              168      168              
- Partials             13       14       +1     
Flag Coverage Δ
contracts/access 44.87% <ø> (ø)
math/core 86.12% <ø> (ø)
math/fixed_point 58.07% <95.45%> (-0.42%) ⬇️

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.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Updates fixed-point pow implementations to use binary exponentiation, addressing issue #272’s concern about linear exponentiation (performance and accumulated truncation effects) in the fixed-point math packages.

Changes:

  • Replaced step-by-step repeated multiplication in UD30x9/SD29x9 pow with a binary-exponentiation loop.
  • Expanded pow documentation to clarify truncation/associativity implications under the new grouping.
  • Updated SD29x9 pow tests to assert new expected values for higher exponents and overflow behavior.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.

File Description
math/fixed_point/sources/ud30x9/ud30x9_base.move Switches UD30x9::pow to binary exponentiation and updates docstring semantics accordingly.
math/fixed_point/sources/sd29x9/sd29x9_base.move Switches SD29x9::pow to binary exponentiation while preserving sign handling, and updates documentation.
math/fixed_point/tests/sd29x9_tests/pow_tests.move Adjusts high-exponent expectations and overflow test parameters to align with new pow behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 31, 2026 07:41
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

0xNeshi added 2 commits March 31, 2026 09:56
* Enhance pow_tests with grouping behavior check

Add test for rounding behavior in binary exponentiation with large exponents.

* Add expect_ne to sd29x9_pow_tests

* Add inequality assertion macro for SD29x9

Added a new macro 'expect_ne' to assert inequality between two SD29x9 values.
Refactor expect_ne macro to unwrap values and add debug output on assertion failure.
Copilot AI review requested due to automatic review settings March 31, 2026 08:02
Refactor expect_ne macro to unwrap values after assignment.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copilot AI review requested due to automatic review settings March 31, 2026 08:13
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Collaborator

@bidzyyys bidzyyys left a comment

Choose a reason for hiding this comment

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

Left some comments.

… pow_tests, use pow(2) for overflow test

Agent-Logs-Url: https://github.com/OpenZeppelin/contracts-sui/sessions/96584ab5-2b3d-4442-9775-45db402402a7

Co-authored-by: bidzyyys <25967634+bidzyyys@users.noreply.github.com>
Copilot AI requested a review from bidzyyys March 31, 2026 10:13
Copilot AI review requested due to automatic review settings March 31, 2026 10:28
Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
math/fixed_point/sources/internal/pow_u256.move (1)

11-11: Documentation inaccuracy: exp does not need to be non-zero.

The documentation states "Must be non-zero" for the exp parameter, but the implementation correctly handles exp == 0 (returns scale, representing 1.0) and exp == 1 (returns base). Since callers short-circuit these cases anyway, consider either removing this requirement or clarifying it as a precondition assumed by the callers.

📝 Suggested documentation fix
 /// #### Parameters
 /// - `base`: The base value in fixed-point representation (already scaled).
-/// - `exp`: The exponent. Must be non-zero.
+/// - `exp`: The exponent.
 /// - `scale`: The fixed-point scale factor (e.g. `10^9`).
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@math/fixed_point/sources/internal/pow_u256.move` at line 11, The docstring
for the exponent parameter is inaccurate: it claims `exp` "Must be non-zero"
even though the implementation of pow_u256 (and callers that short‑circuit
exp==0 to return `scale` and exp==1 to return `base`) supports exp == 0; update
the documentation in pow_u256 (the `exp` parameter description) to either remove
the "Must be non-zero" requirement or change it to state that exp may be zero
(with the behavior that exp==0 returns `scale` and exp==1 returns `base`), so
the comment matches the implementation and caller assumptions.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@math/fixed_point/sources/internal/pow_u256.move`:
- Line 11: The docstring for the exponent parameter is inaccurate: it claims
`exp` "Must be non-zero" even though the implementation of pow_u256 (and callers
that short‑circuit exp==0 to return `scale` and exp==1 to return `base`)
supports exp == 0; update the documentation in pow_u256 (the `exp` parameter
description) to either remove the "Must be non-zero" requirement or change it to
state that exp may be zero (with the behavior that exp==0 returns `scale` and
exp==1 returns `base`), so the comment matches the implementation and caller
assumptions.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 5f4aea67-b438-4e36-a750-e4b949ba623a

📥 Commits

Reviewing files that changed from the base of the PR and between 14ac990 and 4d68c67.

📒 Files selected for processing (5)
  • math/fixed_point/sources/internal/pow_u256.move
  • math/fixed_point/sources/sd29x9/sd29x9_base.move
  • math/fixed_point/sources/ud30x9/ud30x9_base.move
  • math/fixed_point/tests/sd29x9_tests/pow_tests.move
  • math/fixed_point/tests/ud30x9_tests/pow_tests.move

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copilot AI review requested due to automatic review settings March 31, 2026 10:45
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@0xNeshi 0xNeshi requested a review from ericnordelo March 31, 2026 11:07
Copilot AI review requested due to automatic review settings April 1, 2026 09:05
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@bidzyyys bidzyyys requested a review from ericnordelo April 1, 2026 09:44
Copy link
Copy Markdown
Member

@ericnordelo ericnordelo left a comment

Choose a reason for hiding this comment

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

LGTM!

It would be great to merge this PR first to avoid conflicts with constants, but non blocking by any means.

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.

[L-04]: Fixed-Point pow Uses Linear Exponentiation, Accumulating Unnecessary Rounding Error

5 participants