Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Comment Syntax in Code Update storage.cairo #201

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions src/starkware/starknet/common/storage.cairo
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from starkware.cairo.common.math import assert_250_bit

const MAX_STORAGE_ITEM_SIZE = 256;
// Valid storage addresses should satisfy address + offset < 2**251 where
// offset < MAX_STORAGE_ITEM_SIZE and address < ADDR_BOUND.
# Valid storage addresses should satisfy address + offset < 2**251 where
# offset < MAX_STORAGE_ITEM_SIZE and address < ADDR_BOUND.
const ADDR_BOUND = 2 ** 251 - MAX_STORAGE_ITEM_SIZE;

// Computes addr % ADDR_BOUND so that the result will form a valid storage item address in the
// storage tree. In particular, we need the result + MAX_STORAGE_ITEM_SIZE to be less than
// 2**251.
# Computes addr % ADDR_BOUND so that the result will form a valid storage item address in the
# storage tree. In particular, we need the result + MAX_STORAGE_ITEM_SIZE to be less than
# 2**251.
@known_ap_change
func normalize_address{range_check_ptr}(addr: felt) -> (res: felt) {
tempvar is_small;
Expand All @@ -24,27 +24,27 @@ func normalize_address{range_check_ptr}(addr: felt) -> (res: felt) {
%{ ids.is_250 = 1 if ids.addr < 2**250 else 0 %}
if (is_250 != 0) {
ap += 11;
// In this case addr < 2**250 < ADDR_BOUND, so addr % ADDR_BOUND = addr.
# In this case addr < 2**250 < ADDR_BOUND, so addr % ADDR_BOUND = addr.
assert_250_bit(addr);
} else {
ap += 10;
// In this case 0 <= ADDR_BOUND - 1 - addr < 2**250, so addr < ADDR_BOUND.
# In this case 0 <= ADDR_BOUND - 1 - addr < 2**250, so addr < ADDR_BOUND.
assert_250_bit(ADDR_BOUND - 1 - addr);
}
return (res=addr);
} else {
// The first call to assert_250_bit() checks that 0 <= (addr - ADDR_BOUND) % PRIME < 2**250.
// The second call checks that 0 <= (-1 - addr) % PRIME < 2**250.
// Let x = (addr - ADDR_BOUND) % PRIME and y = (-1 - addr) % PRIME. Then,
// x + y = (addr - ADDR_BOUND) + (-1 - addr) = PRIME - 1 - ADDR_BOUND (mod PRIME).
// So we have:
// x + y = PRIME - 1 - ADDR_BOUND (mod PRIME).
// Since both hands are in the range [0, PRIME) (since 2 * 2**250 < PRIME),
// they must be equal as integers.
// In particular, x + ADDR_BOUND = PRIME - 1 - y < PRIME.
// Since addr = x + ADDR_BOUND (mod PRIME) and both sides are in the range [0, PRIME)
// we have addr = x + ADDR_BOUND (as integers).
// Therefore, we should subtract ADDR_BOUND to get addr % ADDR_BOUND.
# The first call to assert_250_bit() checks that 0 <= (addr - ADDR_BOUND) % PRIME < 2**250.
# The second call checks that 0 <= (-1 - addr) % PRIME < 2**250.
# Let x = (addr - ADDR_BOUND) % PRIME and y = (-1 - addr) % PRIME. Then,
# x + y = (addr - ADDR_BOUND) + (-1 - addr) = PRIME - 1 - ADDR_BOUND (mod PRIME).
# So we have:
# x + y = PRIME - 1 - ADDR_BOUND (mod PRIME).
# Since both hands are in the range [0, PRIME) (since 2 * 2**250 < PRIME),
# they must be equal as integers.
# In particular, x + ADDR_BOUND = PRIME - 1 - y < PRIME.
# Since addr = x + ADDR_BOUND (mod PRIME) and both sides are in the range [0, PRIME)
# we have addr = x + ADDR_BOUND (as integers).
# Therefore, we should subtract ADDR_BOUND to get addr % ADDR_BOUND.
let x = addr - ADDR_BOUND;
let y = (-1) - addr;
assert_250_bit(x);
Expand Down