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

fix(lowering): normalize storage_base_address_from_felt252 const folding - #10210

Merged
orizi merged 1 commit into
mainfrom
orizi/07-18-fix_lowering_normalize_storage_base_address_from_felt252_const_folding
Jul 19, 2026
Merged

orizi merged 1 commit into
mainfrom
orizi/07-18-fix_lowering_normalize_storage_base_address_from_felt252_const_folding

Conversation

@orizi

@orizi orizi commented Jul 18, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

During const folding of storage_base_address_from_felt252, the input felt252 value is now normalized before being embedded as a storage_base_address_const generic argument. Values at or above 0x7ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00 are reduced by that bound, matching the runtime behavior of storage_base_address_from_felt252.


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?

storage_base_address_from_felt252 clamps its input at runtime: values in the high range (≥ ADDR_BOUND) are wrapped by subtracting ADDR_BOUND. The const folding optimization was previously passing the raw felt252 value directly into storage_base_address_const without applying this normalization, producing a different constant than what the runtime would compute. For example, -1 as a felt252 would fold to an incorrect address instead of the expected normalized value.


What was the behavior or documentation before?

When storage_base_address_from_felt252 was called with a constant felt252 value ≥ ADDR_BOUND (including negative values like -1 and -2), the const folding pass emitted storage_base_address_const with the raw, un-normalized value, diverging from the runtime result.


What is the behavior or documentation after?

The const folding pass now applies the same normalization as the runtime: if the felt252 value is ≥ ADDR_BOUND, it subtracts ADDR_BOUND before constructing the constant. For example, -1 now correctly folds to storage_base_address_const::<106710729501573572985208420194530329073740042555888586719488>().


Related issue or discussion (if any)


Additional context

A new test case (StorageBaseAddress const normalization) covers the six cases: small positive values (unchanged), a value just below ADDR_BOUND (unchanged), a value at ADDR_BOUND (normalized to 255), a value just below ADDR_BOUND that is not normalized, and -1/-2 (normalized to their correct addresses).

Const folding rewrote `storage_base_address_from_felt252(c)` into
`storage_base_address_const::<c>` using the raw felt value, but the runtime
libfunc normalizes the address into `[0, 2**251 - 256)` (subtracting the
bound for values at or above it, see sierra-to-casm storage.rs).
`storage_base_address_const` rejects out-of-range arguments at Sierra
specialization, so folding a valid const in `[2**251 - 256, PRIME)` — e.g.
any negative felt, whose canonical value is near PRIME — turned a running
program into a compile error.

Perform the same normalization on the folded value. The math is done in
`Felt252` (canonicalizing the input and avoiding extra `BigInt`
allocations), converting to `BigInt` once at the end.
@reviewable-StarkWare

Copy link
Copy Markdown

This change is Reviewable

orizi commented Jul 18, 2026

Copy link
Copy Markdown
Collaborator Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@orizi
orizi marked this pull request as ready for review July 18, 2026 07:33
@cursor

cursor Bot commented Jul 18, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Incorrect folding could change Starknet storage base addresses in optimized builds; the change is a targeted correctness fix in the lowering optimizer with new regression tests.

Overview
Fixes a const-folding bug where compile-time replacement of storage_base_address_from_felt252 with storage_base_address_const used the raw felt252 literal instead of the value after runtime address normalization.

When folding a constant input, the pass now applies the same rule as execution: if the value is ≥ ADDR_BOUND (0x7fff…ff00), it subtracts that bound before building the storage_base_address_const generic argument. Small positives stay unchanged; high-range and negative felts (e.g. -1, -2) fold to the correct addresses.

Adds optimizer test coverage StorageBaseAddress const normalization with six inputs (small values, boundary cases, and negatives) asserting the expected storage_base_address_const::<…>() forms after optimization.

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

@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 2 files and all commit messages, and made 1 comment.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on eytan-starkware).

@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 2 files and all commit messages, and made 1 comment.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on orizi).

@orizi
orizi added this pull request to the merge queue Jul 19, 2026
Merged via the queue into main with commit 6b4eed7 Jul 19, 2026
55 checks passed
@orizi
orizi deleted the orizi/07-18-fix_lowering_normalize_storage_base_address_from_felt252_const_folding branch July 19, 2026 16:13
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