Skip to content

Conversation

@ZumZoom
Copy link
Contributor

@ZumZoom ZumZoom commented Oct 26, 2025

Optimizes Math.average by removing redundant checked math.

PR Checklist

  • Tests
  • Documentation
  • Changeset entry (run npx changeset add)

@ZumZoom ZumZoom requested a review from a team as a code owner October 26, 2025 10:34
@changeset-bot
Copy link

changeset-bot bot commented Oct 26, 2025

⚠️ No Changeset found

Latest commit: 3ceeabd

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coderabbitai
Copy link

coderabbitai bot commented Oct 26, 2025

Walkthrough

A changeset entry was added to openzeppelin-solidity marking a minor version bump with the note "Math: Optimize average by using unchecked math". The average(uint256, uint256) function in contracts/utils/math/Math.sol was modified to wrap its bitwise average calculation in an unchecked block. The computation formula (a & b) + (a ^ b) / 2 remains unchanged, but the calculation now executes without Solidity's overflow checks enabled for this operation.

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title Check ✅ Passed The PR title "Optimize Math.average with unchecked math" directly and clearly describes the primary change in the pull request. The raw summary confirms that the Math.average function was modified to execute its calculation inside an unchecked block to optimize by removing redundant overflow checks. The title is concise, specific, and would clearly communicate to a teammate scanning history that this change optimizes the average function using unchecked math.
Description Check ✅ Passed The PR description "Optimizes Math.average by removing redundant checked math" is directly related to the changeset. The raw summary confirms the change involves wrapping the average calculation in an unchecked block to optimize by removing overflow checks, which aligns with the description's claim of removing redundant checked math. The description is adequately related to the actual changes made in the pull request.
✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1cf1377 and a658d33.

📒 Files selected for processing (2)
  • .changeset/light-dogs-dance.md (1 hunks)
  • contracts/utils/math/Math.sol (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (9)
  • GitHub Check: slither
  • GitHub Check: tests-foundry
  • GitHub Check: tests-upgradeable
  • GitHub Check: coverage
  • GitHub Check: Redirect rules - solidity-contracts
  • GitHub Check: halmos
  • GitHub Check: tests
  • GitHub Check: Header rules - solidity-contracts
  • GitHub Check: Pages changed - solidity-contracts
🔇 Additional comments (2)
.changeset/light-dogs-dance.md (1)

1-5: LGTM!

The changeset entry is correctly formatted and appropriately marks this as a minor version bump for the optimization.

contracts/utils/math/Math.sol (1)

171-174: LGTM! Valid and safe optimization.

The unchecked block is mathematically sound. The bitwise formula (a & b) + (a ^ b) / 2 cannot overflow because (a & b) + (a ^ b) / 2 ≤ (a & b) + (a ^ b) = (a | b) ≤ 2²⁵⁶ - 1. This optimization removes redundant overflow checks and saves gas.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@Amxx Amxx merged commit 8d6294f into OpenZeppelin:master Oct 29, 2025
24 of 25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants