Sitelet https://github.com/starkware-libs/cairo/pull/10042
Skip to content

bugfix(corelib): Made the panic message more exact for i128_mul. - #10042

Merged
orizi merged 1 commit into
mainfrom
orizi/06-06-bugfix_corelib_made_the_panic_message_more_exact_for_i128_mul_
Jun 7, 2026
Merged

orizi merged 1 commit into
mainfrom
orizi/06-06-bugfix_corelib_made_the_panic_message_more_exact_for_i128_mul_

Conversation

@orizi

@orizi orizi commented Jun 6, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes i128 multiplication to correctly detect overflow by using u128_wide_mul instead of relying on a felt252 round-trip conversion. The result's high 128-bit word is checked to be zero, and the low word is then validated against the appropriate signed bounds (i128::MAX for positive results, i128::MIN magnitude for negative results) using downcast before converting. Also adds a test for the previously undetected overflow case of i128::MIN * -1.


Type of change

Please check one:

  • Bug fix (fixes incorrect behavior)
  • New feature
  • Performance improvement
  • Documentation change with concrete technical impact
  • Style, wording, formatting, or typo-only change

Why is this change needed?

The previous implementation converted the product of two u127 values to felt252 and then attempted a try_into to i128. Because felt252 arithmetic wraps around the field modulus rather than at 128-bit boundaries, certain overflow cases (e.g., i128::MIN * -1) were silently accepted instead of panicking.


What was the behavior or documentation before?

i128 multiplication could silently produce incorrect results or fail to panic on overflows that crossed the i128 range but happened to round-trip through felt252 without triggering the try_into failure.


What is the behavior or documentation after?

i128 multiplication now uses u128_wide_mul to obtain a 256-bit product, immediately panics with 'i128_mul Overflow' if the high word is non-zero, and then separately checks the low word against the correct signed bound for positive and negative results. Overflow tests now assert the specific panic message 'i128_mul Overflow', and a new test covers the MIN * -1 case.


Related issue or discussion (if any)

None provided.


Additional context

The existing overflow tests were updated from bare #[should_panic] to #[should_panic(expected: ('i128_mul Overflow',))], making them more precise and ensuring the correct panic message is emitted.

@reviewable-StarkWare

Copy link
Copy Markdown

This change is Reviewable

orizi commented Jun 6, 2026 •

Copy link
Copy Markdown
Collaborator Author

@orizi
orizi marked this pull request as ready for review June 6, 2026 17:53
@cursor

cursor Bot commented Jun 6, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes core integer semantics for all i128 multiplies and shifts compiled Starknet class hashes; behavior is stricter (more panics) but localized to corelib math.

Overview
i128 multiplication no longer routes the product through felt252 and try_into. It now multiplies absolute magnitudes with u128_wide_mul, panics with 'i128_mul Overflow' when the high 128 bits are non-zero, then **downcast**s the low word into the correct signed bound before applying the sign.

Tests assert that overflow panics use 'i128_mul Overflow' (including i128::MIN * -1), add a MAX_I128 multiply case, and refresh Starknet libfuncs coverage bytecode / compiled class hash golden files from the new corelib lowering.

Reviewed by Cursor Bugbot for commit 5fc5ece. Bugbot is set up for automated code reviews on this repo. Configure here.

@orizi
orizi force-pushed the orizi/06-06-bugfix_corelib_made_the_panic_message_more_exact_for_i128_mul_ branch from 862e8bd to c5f3655 Compare June 6, 2026 22:08

@eytan-starkware eytan-starkware left a comment

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.

@eytan-starkware reviewed 3 files and all commit messages, and made 2 comments.
Reviewable status: 3 of 6 files reviewed, 2 unresolved discussions (waiting on orizi and TomerStarkware).


corelib/src/test/integer_test.cairo line 1837 at r1 (raw file):

#[test]
#[should_panic(expected: ('i128_mul Overflow',))]

What error did we have before?


crates/cairo-lang-starknet/test_data/libfuncs_coverage__libfuncs_coverage.sierra line 13310 at r1 (raw file):

downcast<u128, BoundedInt<0, 170141183460469231731687303715884105727>>([33], [31]) { fallthrough([37], [38]) F179_B4([39]) };
branch_align() -> ();
upcast<BoundedInt<0, 170141183460469231731687303715884105727>, i128>([38]) -> ([40]);

Why such a big regression?

Additionally improved performance of the implementation.
@orizi
orizi changed the base branch from orizi/06-06-refactor_corelib_refactored_integer.cairo_by_additional_uses to graphite-base/10042 June 7, 2026 07:27
@orizi
orizi force-pushed the orizi/06-06-bugfix_corelib_made_the_panic_message_more_exact_for_i128_mul_ branch from c5f3655 to 5fc5ece Compare June 7, 2026 07:27
@orizi
orizi force-pushed the graphite-base/10042 branch from 0a554e2 to 219825c Compare June 7, 2026 07:27
@orizi
orizi changed the base branch from graphite-base/10042 to main June 7, 2026 07:27

@orizi orizi left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@orizi made 2 comments.
Reviewable status: 3 of 6 files reviewed, 2 unresolved discussions (waiting on eytan-starkware and TomerStarkware).


corelib/src/test/integer_test.cairo line 1837 at r1 (raw file):

Previously, eytan-starkware wrote…

What error did we have before?

just u128_mul - as this was the internal implementation.


crates/cairo-lang-starknet/test_data/libfuncs_coverage__libfuncs_coverage.sierra line 13310 at r1 (raw file):

Previously, eytan-starkware wrote…

Why such a big regression?

it is a full change of the i128-mul - so it all changes.
you can see there's only one panic message here - and the usage of more efficient libfuncs.

@eytan-starkware eytan-starkware left a comment

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.

:lgtm:

@eytan-starkware reviewed 3 files, made 1 comment, and resolved 2 discussions.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on TomerStarkware).

@orizi
orizi added this pull request to the merge queue Jun 7, 2026
Merged via the queue into main with commit 7662e60 Jun 7, 2026
105 checks passed

@TomerStarkware TomerStarkware left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

:lgtm:

@TomerStarkware reviewed all commit messages and made 1 comment.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved.

@orizi
orizi deleted the orizi/06-06-bugfix_corelib_made_the_panic_message_more_exact_for_i128_mul_ branch June 7, 2026 11:25
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.

4 participants