Skip to content

fix(contracts): l-06 code simplifications#313

Open
0xisk wants to merge 2 commits into
mainfrom
fix/l-06
Open

fix(contracts): l-06 code simplifications#313
0xisk wants to merge 2 commits into
mainfrom
fix/l-06

Conversation

@0xisk
Copy link
Copy Markdown
Member

@0xisk 0xisk commented Apr 10, 2026

Types of changes

What types of changes does your code introduce to OpenZeppelin Midnight Contracts?
Put an x in the boxes that apply

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation Update (if none of the other choices apply)

Partially OpenZeppelin/compact-contracts#431

PR Checklist

  • I have read the Contributing Guide
  • I have added tests that prove my fix is effective or that my feature works
  • I have added documentation of new methods and any new behavior or changes to existing behavior
  • CI Workflows Are Passing

Further comments

If this is a relatively large or complex change, kick off the discussion by explaining why you chose the solution you did and what alternatives you considered, etc...

Summary by CodeRabbit

  • Refactor
    • Updated internal multiplication logic implementation for improved efficiency and maintainability.

@0xisk 0xisk requested a review from andrew-fleming April 10, 2026 12:09
@0xisk 0xisk self-assigned this Apr 10, 2026
@0xisk 0xisk requested a review from a team as a code owner April 10, 2026 12:09
@0xisk 0xisk added the audit Issues noted by the audit label Apr 10, 2026
@0xisk 0xisk requested review from a team as code owners April 10, 2026 12:09
@0xisk 0xisk moved this from Backlog to Needs Review in OZ Development for Midnight Apr 10, 2026
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 10, 2026

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: 16bf138d-ef99-4623-8767-2b49a15d8dc6

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

The pull request refactors the internal _mul multiplication function in Uint128 from a cross-term/partial-product carry scheme to a word-offset addition scheme using 64-bit word arithmetic, reducing line count while maintaining functional equivalence.

Changes

Cohort / File(s) Summary
Uint128 Multiplication Refactor
contracts/src/math/Uint128.compact
Refactored internal _mul(a: U128, b: U128): U256 implementation to replace cross-term carry propagation with word-offset addition scheme. Derives ll/hl/lh/hh from toU128 operations, computes sequential offset terms via summed high/low components, and adds explicit overflow assertion for offset3.high.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Poem

🐰 Hops of joy through math divine,
Offsets now align just fine,
Carries flow in words so neat,
Old cross-terms now obsolete!
Multiplication schemes align,
Cleaner code—a work sublime!

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Title check ❓ Inconclusive The title 'fix(contracts): l-06 code simplifications' is vague and generic, using the non-descriptive term 'code simplifications' that does not convey specific information about the actual changes to the multiplication logic. Replace 'code simplifications' with a specific description of the main change, such as 'refactor(contracts): simplify Uint128 multiplication carry logic' or 'fix(contracts): optimize U128 multiplication implementation in Uint128.compact'.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ 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 fix/l-06

Warning

Review ran into problems

🔥 Problems

Timed out fetching pipeline failures after 30000ms


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

@0xisk 0xisk linked an issue Apr 10, 2026 that may be closed by this pull request
Copy link
Copy Markdown
Contributor

@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 (2)
contracts/src/math/Uint128.compact (2)

618-623: Clarify that these are word carries, not single-bit carries.

offset1.high and offset2.high are propagated whole-word carries; they can be greater than 1. The current wording reads like a boolean carry bit, which makes this arithmetic easier to misread later.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@contracts/src/math/Uint128.compact` around lines 618 - 623, The comments
incorrectly imply single-bit carries; update the explanatory comments around the
multi-word add in Uint128.compact (referencing offset1, offset2, offset3 and
their .high fields, and the toU128 calls combining ll.high, hl.low, lh.low,
hh.low, hl.high, lh.high, hh.high) to state these are whole-word carries (i.e.,
full word values that can be >1) propagated to the next word, not
boolean/single-bit carries, so readers understand offset1.high and offset2.high
are multi-word carries.

623-627: Consider using direct comparison instead of isZero for clarity.

Line 627 calls isZero(offset3.high) where offset3.high is Uint<64>, but this module defines isZero only for Uint<128> and U128. While tests confirm the code works, the direct comparison is more explicit and avoids relying on any implicit type conversions.

💡 Suggested improvement
-    assert(isZero(offset3.high), "Uint128: unexpected multiplication overflow");
+    assert(offset3.high == 0, "Uint128: unexpected multiplication overflow");
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@contracts/src/math/Uint128.compact` around lines 623 - 627, The assert is
calling isZero(offset3.high) where offset3.high is a Uint<64> and isZero is
defined for Uint<128>/U128; replace that implicit call with an explicit
comparison against zero to be clearer and type-safe: change the assertion on
offset3.high in the multiplication overflow check (the assert that currently
references isZero and offset3.high) to use a direct equality check (offset3.high
== 0) in the assert message for Uint128/U128 resultHigh/resultLow handling.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@contracts/src/math/Uint128.compact`:
- Around line 618-623: The comments incorrectly imply single-bit carries; update
the explanatory comments around the multi-word add in Uint128.compact
(referencing offset1, offset2, offset3 and their .high fields, and the toU128
calls combining ll.high, hl.low, lh.low, hh.low, hl.high, lh.high, hh.high) to
state these are whole-word carries (i.e., full word values that can be >1)
propagated to the next word, not boolean/single-bit carries, so readers
understand offset1.high and offset2.high are multi-word carries.
- Around line 623-627: The assert is calling isZero(offset3.high) where
offset3.high is a Uint<64> and isZero is defined for Uint<128>/U128; replace
that implicit call with an explicit comparison against zero to be clearer and
type-safe: change the assertion on offset3.high in the multiplication overflow
check (the assert that currently references isZero and offset3.high) to use a
direct equality check (offset3.high == 0) in the assert message for Uint128/U128
resultHigh/resultLow handling.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 79ddc7a3-61d4-45bb-9f33-ee36d9f6d577

📥 Commits

Reviewing files that changed from the base of the PR and between 7e09763 and 63a163e.

📒 Files selected for processing (1)
  • contracts/src/math/Uint128.compact

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

audit Issues noted by the audit

Projects

Status: Needs Review

Development

Successfully merging this pull request may close these issues.

L-06: Code Simplifications

1 participant