Skip to content

sub#352

Merged
thedavidmeister merged 4 commits into
mainfrom
2025-07-11-subb
Jul 11, 2025
Merged

sub#352
thedavidmeister merged 4 commits into
mainfrom
2025-07-11-subb

Conversation

@thedavidmeister
Copy link
Copy Markdown
Contributor

@thedavidmeister thedavidmeister commented Jul 11, 2025

Motivation

Solution

Checks

By submitting this for review, I'm confirming I've done the following:

  • made this PR as small as possible
  • unit-tested any new functionality
  • linked any relevant issues or PRs
  • included screenshots (if this involves a front-end change)

Summary by CodeRabbit

  • New Features

    • Enabled the subtraction (sub) operation, now supporting floating-point decimal arithmetic.
    • Users can perform subtraction with multiple floating-point numbers in supported operations.
  • Bug Fixes

    • Improved subtraction logic to correctly handle floating-point overflow and underflow scenarios.
  • Tests

    • Migrated and expanded tests to validate floating-point subtraction behavior and edge cases.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jul 11, 2025

Walkthrough

The changes fully enable and update the sub operation in a smart contract library. The subtraction logic is migrated from unsigned integer to floating-point arithmetic, with all related code and tests refactored to use floating-point types and libraries. The subtraction operation is now available in the standard operations set and thoroughly tested for floating-point behavior.

Changes

File(s) Change Summary
src/lib/op/LibAllStandardOps.sol Enabled the sub operation: imported LibOpSub, updated opcodes length, metadata, operand handler, and function pointers for integrity and execution.
src/lib/op/math/LibOpSub.sol Migrated subtraction logic from unsigned integer to floating-point arithmetic; updated function signatures, imports, and removed saturating math.
test/src/lib/op/math/LibOpSub.t.sol Uncommented and refactored all tests for floating-point subtraction; updated input types, overflow/underflow logic, and helper functions.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant Interpreter
    participant LibAllStandardOps
    participant LibOpSub
    participant LibDecimalFloat

    Caller->>Interpreter: Request execution with 'sub' opcode
    Interpreter->>LibAllStandardOps: Lookup 'sub' opcode
    LibAllStandardOps->>LibOpSub: Call integrity and run functions
    LibOpSub->>LibDecimalFloat: Perform floating-point subtraction
    LibDecimalFloat-->>LibOpSub: Return result
    LibOpSub-->>Interpreter: Return result to stack
    Interpreter-->>Caller: Return final result
Loading

Possibly related PRs

  • 2025 07 09 div #351: Enabled and integrated the LibOpDiv division opcode with floating-point decimal arithmetic in LibAllStandardOps.sol, similar to this PR’s integration of the sub opcode.

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

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.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 13deeaa and a177d6b.

⛔ Files ignored due to path filters (3)
  • src/generated/Rainterpreter.pointers.sol is excluded by !**/generated/**
  • src/generated/RainterpreterExpressionDeployer.pointers.sol is excluded by !**/generated/**
  • src/generated/RainterpreterParser.pointers.sol is excluded by !**/generated/**
📒 Files selected for processing (3)
  • src/lib/op/LibAllStandardOps.sol (6 hunks)
  • src/lib/op/math/LibOpSub.sol (1 hunks)
  • test/src/lib/op/math/LibOpSub.t.sol (1 hunks)
🔇 Additional comments (2)
src/lib/op/LibAllStandardOps.sol (1)

96-96: LGTM! The sub operation is properly integrated.

The changes correctly integrate the LibOpSub operation into the standard ops library:

  • Import is added in the correct alphabetical position
  • Operation count is properly incremented from 28 to 29
  • Metadata description accurately describes the subtraction behavior
  • Operand handler and function pointers are correctly configured

Also applies to: 108-108, 308-308, 510-510, 623-623, 737-737

test/src/lib/op/math/LibOpSub.t.sol (1)

1-136: Test suite properly updated for floating-point implementation.

The tests comprehensively cover the new floating-point subtraction implementation:

  • Integrity tests correctly validate operand handling
  • Runtime tests properly check for floating-point overflow conditions
  • Eval tests use appropriate floating-point values
  • Good practice to reference GitHub issues for known limitations

Comment on lines +11 to +12
/// @title LibOpSub
/// @notice Opcode to subtract N integers.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Update the documentation to reflect floating-point arithmetic.

The comment states "Opcode to subtract N integers" but the implementation actually performs floating-point subtraction using the Float type and LibDecimalFloat library.

Apply this diff to fix the documentation:

-/// @title LibOpSub
-/// @notice Opcode to subtract N integers.
+/// @title LibOpSub
+/// @notice Opcode to subtract N floating-point numbers.
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
/// @title LibOpSub
/// @notice Opcode to subtract N integers.
/// @title LibOpSub
/// @notice Opcode to subtract N floating-point numbers.
🤖 Prompt for AI Agents
In src/lib/op/math/LibOpSub.sol around lines 11 to 12, update the documentation
comment to accurately describe the operation as performing floating-point
subtraction using the Float type and LibDecimalFloat library instead of
subtracting integers. Replace "Opcode to subtract N integers" with a description
that reflects floating-point arithmetic.

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.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a177d6b and 94780f0.

⛔ Files ignored due to path filters (1)
  • src/generated/RainterpreterExpressionDeployer.pointers.sol is excluded by !**/generated/**
📒 Files selected for processing (1)
  • test/src/lib/op/math/LibOpSub.t.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). (7)
  • GitHub Check: rainix (ubuntu-latest, rainix-sol-static)
  • GitHub Check: rainix (macos-latest, rainix-rs-test)
  • GitHub Check: rainix (ubuntu-latest, rainix-rs-static)
  • GitHub Check: rainix (ubuntu-latest, rainix-sol-artifacts)
  • GitHub Check: rainix (ubuntu-latest, rainix-rs-artifacts)
  • GitHub Check: rainix (ubuntu-latest, rainix-sol-test)
  • GitHub Check: git-clean
🔇 Additional comments (6)
test/src/lib/op/math/LibOpSub.t.sol (6)

4-9: Imports are appropriate for floating-point subtraction tests.

The imports correctly include all necessary dependencies for testing the floating-point subtraction operation.


14-23: Integrity happy path test is well-implemented.

The test correctly validates that the integrity function returns the expected number of inputs and outputs for valid operands.


27-42: Unhappy path tests correctly validate minimum input requirements.

Both tests appropriately verify that the integrity function enforces a minimum of 2 inputs for the subtraction operation, regardless of the operand specification.


44-80: Runtime test correctly handles floating-point subtraction and overflow detection.

The test properly:

  • Validates input assumptions
  • Unpacks floating-point values
  • Detects exponent overflows at each step
  • Expects revert when any overflow occurs

83-93: Evaluation tests correctly validate input requirements.

The tests appropriately use checkBadInputs to verify that subtraction fails with fewer than 2 inputs.


96-114: Two-input evaluation tests are comprehensive.

Good coverage of various subtraction scenarios including edge cases. The commented tests with GitHub issue references provide valuable documentation of known floating-point limitations.

function testOpSubEvalThreeInputs() external view {
checkHappy("_: sub(1 0 0);", Float.unwrap(LibDecimalFloat.packLossless(1, 0)), "1 0 0");
checkHappy("_: sub(1 1 0);", Float.unwrap(LibDecimalFloat.packLossless(0, 0)), "1 1 0");
checkHappy("_: sub(2 1 1);", Float.unwrap(LibDecimalFloat.packLossless(0, -37)), "2 1 1");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick (assertive)

Consider clarifying the comment to reflect the floating-point representation.

The comment "2 1 1" doesn't clearly indicate that the result is a floating-point zero with exponent -37, which might be confusing when reading the expected value.

-        checkHappy("_: sub(2 1 1);", Float.unwrap(LibDecimalFloat.packLossless(0, -37)), "2 1 1");
+        checkHappy("_: sub(2 1 1);", Float.unwrap(LibDecimalFloat.packLossless(0, -37)), "2 1 1"); // 0e-37
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
checkHappy("_: sub(2 1 1);", Float.unwrap(LibDecimalFloat.packLossless(0, -37)), "2 1 1");
checkHappy("_: sub(2 1 1);", Float.unwrap(LibDecimalFloat.packLossless(0, -37)), "2 1 1"); // 0e-37
🤖 Prompt for AI Agents
In test/src/lib/op/math/LibOpSub.t.sol at line 120, the comment "2 1 1" does not
clearly describe the expected floating-point result with exponent -37. Update
the comment to explicitly indicate that the expected value is a floating-point
zero with exponent -37 to improve clarity and avoid confusion when reading the
test.

@thedavidmeister thedavidmeister merged commit 7b52756 into main Jul 11, 2025
11 checks passed
This was referenced Jul 12, 2025
@coderabbitai coderabbitai Bot mentioned this pull request Aug 12, 2025
4 tasks
@coderabbitai coderabbitai Bot mentioned this pull request Sep 3, 2025
4 tasks
@coderabbitai coderabbitai Bot mentioned this pull request Sep 19, 2025
4 tasks
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.

1 participant