Skip to content

Expose mul & div#64

Merged
hardyjosh merged 4 commits intomainfrom
feat/mul-div
Jun 17, 2025
Merged

Expose mul & div#64
hardyjosh merged 4 commits intomainfrom
feat/mul-div

Conversation

@0xgleb
Copy link
Copy Markdown
Contributor

@0xgleb 0xgleb commented Jun 16, 2025

Motivation

We need to use Solidity Float code in Rust

Solution

Expose mul and div methods

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
    • Added support for multiplication and division operations with the Float type.
  • Bug Fixes
    • Improved error handling for parsing errors, exponent overflows, and division by zero.
  • Tests
    • Expanded test coverage for multiplication, division, and various edge cases, including overflow, underflow, and error scenarios.
  • Documentation
    • Updated documentation to clarify decimal handling in formatting.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 16, 2025

Walkthrough

The changes introduce multiplication and division capabilities to the Float type by implementing the Mul and Div traits, which perform EVM contract calls for arithmetic operations. The test suite is expanded to cover these new operations, including property-based tests, error handling, and overflow/underflow scenarios. Documentation is also updated.

Changes

File(s) Change Summary
crates/float/src/lib.rs Added Mul and Div trait implementations for Float; updated imports; added note to format.
crates/float/tests/... Added and expanded tests for multiplication, division, error handling, and property-based scenarios.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Float
    participant EVMContract

    User->>Float: mul(self, b)
    Float->>EVMContract: call multiplication contract
    EVMContract-->>Float: return result
    Float-->>User: Result<Float, FloatError>

    User->>Float: div(self, b)
    Float->>EVMContract: call division contract
    EVMContract-->>Float: return result
    Float-->>User: Result<Float, FloatError>
Loading
✨ Finishing Touches
  • 📝 Generate Docstrings

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.

@0xgleb 0xgleb changed the title Feat/mul div Expose mul & div Jun 16, 2025
@0xgleb 0xgleb marked this pull request as ready for review June 16, 2025 15:28
@0xgleb 0xgleb self-assigned this Jun 16, 2025
@0xgleb 0xgleb linked an issue Jun 16, 2025 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.

Actionable comments posted: 4

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 3a1cdc0 and 1dd2427.

📒 Files selected for processing (2)
  • crates/float/src/lib.rs (8 hunks)
  • src/concrete/DecimalFloat.sol (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (7)
  • GitHub Check: rainix (ubuntu-latest, rainix-sol-test)
  • GitHub Check: rainix (ubuntu-latest, test-wasm-build)
  • GitHub Check: rainix (ubuntu-latest, rainix-rs-static)
  • GitHub Check: rainix (macos-latest, rainix-rs-test)
  • GitHub Check: rainix (ubuntu-latest, rainix-rs-test)
  • GitHub Check: rainix (ubuntu-latest, rainix-sol-static)
  • GitHub Check: git-clean

Comment on lines +175 to +182
/// Exposes `LibDecimalFloat.unpack` for offchain use.
/// @param float The float to unpack.
/// @return coefficient The coefficient of the float.
/// @return exponent The exponent of the float.
function unpack(Float float) external pure returns (int224, int32) {
(int256 coefficient, int256 exponent) = LibDecimalFloat.unpack(float);
return (int224(coefficient), int32(exponent));
}
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot Jun 16, 2025

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add explicit range-checks before narrowing int256int224/int32.

LibDecimalFloat.unpack returns int256. Directly casting to narrower types will silently wrap on overflow, producing corrupted results with no revert path. Even if upstream invariants guarantee the range, this external helper shouldn’t rely on them.

(int256 coeff, int256 exp) = LibDecimalFloat.unpack(float);
require(
    coeff >= type(int224).min && coeff <= type(int224).max &&
    exp   >= type(int32).min  && exp   <= type(int32).max,
    "DecimalFloat.unpack: overflow"
);
return (int224(coeff), int32(exp));

Also consider renaming the parameter from float to value to avoid shadowing the Float type.

🤖 Prompt for AI Agents
In src/concrete/DecimalFloat.sol lines 175 to 182, add explicit range checks
before casting the int256 values returned by LibDecimalFloat.unpack to int224
and int32 to prevent silent overflow and data corruption. Use require statements
to ensure the coefficient fits within int224 bounds and the exponent fits within
int32 bounds, reverting with an error message if not. Also, rename the function
parameter from float to value to avoid shadowing the Float type.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Float is designed to be a 224 bit signed coefficient with 32 bit signed exponent. It's represented as 32 bytes of data. See LibDecimalFloat.sol for details

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.

🧩 Analysis chain

@0xgleb Thank you for the clarification! I can see from the codebase that Float is indeed designed as a custom type wrapping uint256 with a specific bit layout where the coefficient occupies 224 bits and the exponent occupies 32 bits. Since the unpack function is extracting these values from their designated bit ranges within the 256-bit representation, the casting to int224 and int32 is safe by design - the values returned by LibDecimalFloat.unpack are guaranteed to fit within these ranges due to the internal bit layout.

The range checks I initially suggested would be redundant given this architectural constraint. The type system and bit manipulation ensure that overflow cannot occur during the casting operations.

🐰
📚✨
( ^.^ )
_)(/_


🏁 Script executed:

#!/bin/bash
# Find and examine LibDecimalFloat.sol to understand Float implementation
fd "LibDecimalFloat.sol" --type f --exec cat {}

Length of output: 31338


🏁 Script executed:

#!/bin/bash
# Search for pack and unpack function implementations in LibDecimalFloat
ast-grep --pattern 'function unpack($$$) {
  $$$
}'

echo "=== PACK FUNCTIONS ==="
ast-grep --pattern 'function pack($$$) {
  $$$
}'

echo "=== FLOAT TYPE DEFINITION ==="
rg "type Float" -A 2 -B 2

Length of output: 556


🏁 Script executed:

#!/bin/bash
# Search for bit manipulation and constants in LibDecimalFloat
rg "224|32" src/lib/LibDecimalFloat.sol -A 2 -B 2

echo "=== BIT SHIFTS AND MASKS ==="
rg "<<|>>|\&|\|" src/lib/LibDecimalFloat.sol -A 1 -B 1

echo "=== UNPACK FUNCTION SPECIFICALLY ==="
rg "function unpack" src/lib/LibDecimalFloat.sol -A 10 -B 2

Length of output: 3725


@0xgleb You're absolutely right! After examining the LibDecimalFloat.sol implementation, I can see that Float is designed with a precise bit layout:

  • Type definition: type Float is bytes32; (256 bits total)
  • Bit allocation: 224 bits for coefficient + 32 bits for exponent
  • Bit layout: Coefficient in lower 224 bits, exponent in upper 32 bits

The unpack function uses bit manipulation to extract values from their designated ranges:

function unpack(Float float) internal pure returns (int256 signedCoefficient, int256 exponent) {
    uint256 mask = type(uint224).max;
    assembly ("memory-safe") {
        signedCoefficient := signextend(27, and(float, mask))  // Extract lower 224 bits
        exponent := sar(0xe0, float)                           // Extract upper 32 bits
    }
}

The casting to int224/int32 in DecimalFloat.sol is safe by design because the unpack function can never return values outside these ranges - the bit extraction guarantees this. The range checks I suggested would indeed be redundant.

Thank you for the clarification about the architectural design!

🐰
📚✨
( ^.^ )
_)(/_


✏️ Learnings added
Learnt from: 0xgleb
PR: rainlanguage/rain.math.float#64
File: src/concrete/DecimalFloat.sol:0-0
Timestamp: 2025-06-17T10:02:01.373Z
Learning: In the rain.math.float project, the Float type is designed with a specific bit layout: 224-bit signed coefficient and 32-bit signed exponent, represented as 32 bytes total. The LibDecimalFloat.unpack function returns values that are guaranteed to fit within int224 and int32 ranges due to this internal representation, making explicit range checks before casting unnecessary.

Learnt from: 0xgleb
PR: rainlanguage/rain.math.float#64
File: src/concrete/DecimalFloat.sol:0-0
Timestamp: 2025-06-17T10:02:01.373Z
Learning: In the rain.math.float project, the Float type is designed with a specific bit layout: 224-bit signed coefficient and 32-bit signed exponent, represented as 32 bytes total. The LibDecimalFloat.unpack function returns values that are guaranteed to fit within int224 and int32 ranges due to this internal representation, making explicit range checks before casting unnecessary.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

Comment thread crates/float/src/lib.rs
Comment on lines +152 to +172
#[cfg(test)]
fn unpack(self) -> Result<(I224, i32), FloatError> {
let Float(float) = self;
let calldata = DecimalFloat::unpackCall { float }.abi_encode();

execute_call(Bytes::from(calldata), |output| {
let DecimalFloat::unpackReturn {
_0: coefficient,
_1: exponent,
} = DecimalFloat::unpackCall::abi_decode_returns(output.as_ref())?;

Ok((coefficient, exponent))
})
}

#[cfg(test)]
fn show_unpacked(self) -> Result<String, FloatError> {
let (coefficient, exponent) = self.unpack()?;
Ok(format!("{coefficient}e{exponent}"))
}

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot Jun 16, 2025

Choose a reason for hiding this comment

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

🧹 Nitpick (assertive)

Limit test-only helpers’ visibility.

unpack and show_unpacked are cfg(test) but still pub (implicitly) and usable via extern crate in test builds. Mark them pub(crate) (or even fn) to avoid accidental exposure and keep the public API surface minimal.

🤖 Prompt for AI Agents
In crates/float/src/lib.rs around lines 152 to 172, the test-only helper
functions unpack and show_unpacked are implicitly public due to missing
visibility modifiers. To limit their visibility and avoid accidental exposure in
test builds, explicitly mark these functions as pub(crate) or simply fn to
restrict their scope to the current crate or module, thereby keeping the public
API surface minimal.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

They're literally marked as fn

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.

@0xgleb You're absolutely right! I apologize for the incorrect comment. The functions unpack and show_unpacked are indeed already marked as fn (private visibility), not pub fn. They are properly scoped within the module and not exposed in the public API. No changes are needed here.


If you found this review helpful, would you consider giving us a shout-out on X?

Thank you for using CodeRabbit!

Comment thread crates/float/src/lib.rs
Comment on lines +202 to +233
pub fn lt(self, b: Self) -> Result<bool, FloatError> {
let Float(a) = self;
let Float(b) = b;
let calldata = DecimalFloat::ltCall { a, b }.abi_encode();

execute_call(Bytes::from(calldata), |output| {
let decoded = DecimalFloat::ltCall::abi_decode_returns(output.as_ref())?;
Ok(decoded)
})
}

pub fn eq(self, b: Self) -> Result<bool, FloatError> {
let Float(a) = self;
let Float(b) = b;
let calldata = DecimalFloat::eqCall { a, b }.abi_encode();

execute_call(Bytes::from(calldata), |output| {
let decoded = DecimalFloat::eqCall::abi_decode_returns(output.as_ref())?;
Ok(decoded)
})
}

pub fn gt(self, b: Self) -> Result<bool, FloatError> {
let Float(a) = self;
let Float(b) = b;
let calldata = DecimalFloat::gtCall { a, b }.abi_encode();

execute_call(Bytes::from(calldata), |output| {
let decoded = DecimalFloat::gtCall::abi_decode_returns(output.as_ref())?;
Ok(decoded)
})
}
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot Jun 16, 2025

Choose a reason for hiding this comment

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

🧹 Nitpick (assertive)

Factor out duplicate comparison call logic.

lt, eq, and gt share identical boilerplate (destructuring, calldata build, execute_call). Extract a private helper:

fn compare(self, b: Self, f: fn(FixedBytes<32>, FixedBytes<32>) -> Bytes)
    -> Result<bool, FloatError> {}

reducing three nearly-identical functions to thin wrappers, improving maintainability.

🤖 Prompt for AI Agents
In crates/float/src/lib.rs between lines 202 and 233, the functions lt, eq, and
gt contain duplicated code for destructuring, calldata encoding, and executing
the call. To fix this, create a private helper function named compare that takes
self, b, and a function pointer f for the specific call encoding. This helper
should handle the common logic of destructuring the Float values, encoding
calldata using f, and executing the call with execute_call. Then refactor lt,
eq, and gt to be thin wrappers that call compare with the appropriate encoding
function, eliminating duplication and improving maintainability.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The largest duplication was refactored into execute_call. The rest is not significant

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.

@0xgleb You're absolutely right! The execute_call function already provides the key abstraction for handling the EVM execution boilerplate, error processing, and result handling - which was indeed the most significant duplication.

Based on the retrieved learning from your previous work, this approach makes perfect sense: the execute_call function handles the complex execution logic, while individual methods like lt, eq, and gt only need to handle their specific call encoding and result decoding. The remaining duplication is minimal and method-specific, so further abstraction would likely over-complicate the code without significant benefit.

Your architectural decision to keep the abstraction at this level maintains good readability and separation of concerns. 👍


🧠 Learnings used
Learnt from: 0xgleb
PR: rainlanguage/rain.math.float#58
File: crates/float/src/lib.rs:201-232
Timestamp: 2025-06-16T13:14:38.395Z
Learning: In the rain.math.float Rust crate, the `execute_call` function already provides sufficient abstraction for EVM contract calls by handling execution boilerplate, error handling, and result processing. Individual methods like `lt`, `eq`, `gt` only need to handle their specific call encoding and result decoding, making further abstraction unnecessary.

Comment thread crates/float/src/lib.rs
@0xgleb 0xgleb mentioned this pull request Jun 16, 2025
4 tasks
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: 2

♻️ Duplicate comments (1)
crates/float/src/lib.rs (1)

281-294: Return type ergonomics remain unconventional

Div (and Mul) continue the pattern type Output = Result<Self, FloatError>.
Call-sites must now write (a / b)? or .unwrap(), which goes against the ergonomic expectations of Rust’s operator traits.

The previous review already suggested introducing a lightweight alias/new-type (ResultFloat) to make this fact explicit and less surprising.
Re-consider adopting it, or expose non-fallible mul/div helpers in addition to the trait impls to keep both worlds happy.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 1dd2427 and 7940a0c.

📒 Files selected for processing (1)
  • crates/float/src/lib.rs (6 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (7)
  • GitHub Check: rainix (ubuntu-latest, rainix-sol-static)
  • GitHub Check: rainix (ubuntu-latest, rainix-sol-test)
  • GitHub Check: rainix (macos-latest, rainix-rs-test)
  • GitHub Check: rainix (ubuntu-latest, test-wasm-build)
  • GitHub Check: rainix (ubuntu-latest, rainix-rs-static)
  • GitHub Check: rainix (ubuntu-latest, rainix-rs-test)
  • GitHub Check: git-clean
🔇 Additional comments (1)
crates/float/src/lib.rs (1)

585-593: Underflow test matches ExponentOverflow

test_mul_exponent_underflow_error expects ExponentOverflow, which is confusing for readers and may hide a genuine underflow condition in the contract.

Clarify whether the contract deliberately re-uses the overflow selector for both directions; if not, update the expectation to the correct ExponentUnderflow (or similar) variant when it becomes available.

Comment thread crates/float/src/lib.rs
Comment thread crates/float/src/lib.rs
@0xgleb 0xgleb requested review from findolor and hardyjosh June 17, 2025 10:12
@0xgleb 0xgleb mentioned this pull request Jun 17, 2025
4 tasks
@hardyjosh hardyjosh merged commit 64d2ac3 into main Jun 17, 2025
8 checks passed
This was referenced Jul 22, 2025
This was referenced Jul 29, 2025
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.

Expose mul and div methods

3 participants