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

feat(starknet_os): pack usize in felts #4573

Merged
merged 1 commit into from
Mar 4, 2025

Conversation

yoavGrs
Copy link
Contributor

@yoavGrs yoavGrs commented Mar 2, 2025

No description provided.

@reviewable-StarkWare
Copy link

This change is Reviewable

@yoavGrs yoavGrs self-assigned this Mar 2, 2025
@yoavGrs yoavGrs marked this pull request as ready for review March 2, 2025 14:29
Copy link

github-actions bot commented Mar 2, 2025

Benchmark movements:
full_committer_flow performance regressed!
full_committer_flow time: [30.426 ms 30.502 ms 30.605 ms]
change: [+1.0206% +1.3006% +1.6629%] (p = 0.00 < 0.05)
Performance has regressed.
Found 4 outliers among 100 measurements (4.00%)
3 (3.00%) high mild
1 (1.00%) high severe

Copy link
Collaborator

@dorimedini-starkware dorimedini-starkware left a comment

Choose a reason for hiding this comment

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

Reviewed 4 of 4 files at r1, all commit messages.
Reviewable status: all files reviewed, 4 unresolved discussions (waiting on @Yoni-Starkware)


crates/starknet_os/src/hints/hint_implementation/stateless_compression/tests.rs line 121 at r1 (raw file):

    let unpacked = unpack_felts_to_usize(packed.as_ref(), nums.len(), elm_bound);
    assert_eq!(nums, unpacked);
}

edge cases please:

  1. Felt::MAX
  2. powers of two
  3. powers of two - 1
  4. powers of two + 1

Code quote:

#[test]
fn test_usize_pack_and_unpack() {
    let nums = vec![34, 0, 11111, 1034, 3404];
    let elm_bound = 12345;
    let packed = pack_usize_in_felts(&nums, elm_bound);
    let unpacked = unpack_felts_to_usize(packed.as_ref(), nums.len(), elm_bound);
    assert_eq!(nums, unpacked);
}

crates/starknet_os/src/hints/hint_implementation/stateless_compression/utils.rs line 415 at r1 (raw file):

}

/// Calculates the number of elements that can fit in a single felt value, given the element bound.

update docstring (I think there was a discussion on a previous iteration of this PR)

Code quote:

/// Calculates the number of elements that can fit in a single felt value, given the element bound.

crates/starknet_os/src/hints/hint_implementation/stateless_compression/utils.rs line 419 at r1 (raw file):

    let n_bits_required =
        max(1, usize::try_from(usize::BITS - elm_bound.leading_zeros()).expect("usize overflow"));
    if n_bits_required > MAX_N_BITS {

can this ever happen?
isn't the correct answer 0 in this case?
consider making the input a Felt, not a usize, so that

  1. we can give an elm_bound of more than usize::MAX (which is much less then 256 bits - or even 128 bits - unless running on a CPU from the future)
  2. if you have Felt as input, converting to biguint and counting bits always results in at most MAX_N_BITS, right?

if (1) is never required (i.e. elm_bounds is always at most 128 bits), please change the type to u128 (or u64, smallest possible); usize depends on architecture and unclear from reading that the bits are always within allowed limit

Code quote:

if n_bits_required > MAX_N_BITS

crates/starknet_os/src/hints/hint_implementation/stateless_compression/utils.rs line 434 at r1 (raw file):

/// Packs a chunk of elements into a single felt.
pub fn pack_usize_in_felt(elms: &[usize], elm_bound: usize) -> Felt {

this function can fail if you provide invalid input; make it private please

Suggestion:

fn pack_usize_in_felt

@yoavGrs yoavGrs force-pushed the yoav/snos/hints/compression_hint/compression_set branch 2 times, most recently from e143521 to be04639 Compare March 2, 2025 16:05
@yoavGrs yoavGrs force-pushed the yoav/snos/hints/compression_hint/pack_usize_in_felt branch from bfa631d to 424eaae Compare March 2, 2025 16:25
Copy link
Contributor Author

@yoavGrs yoavGrs left a comment

Choose a reason for hiding this comment

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

Reviewable status: 1 of 4 files reviewed, 4 unresolved discussions (waiting on @dorimedini-starkware and @Yoni-Starkware)


crates/starknet_os/src/hints/hint_implementation/stateless_compression/tests.rs line 121 at r1 (raw file):

Previously, dorimedini-starkware wrote…

edge cases please:

  1. Felt::MAX
  2. powers of two
  3. powers of two - 1
  4. powers of two + 1

It for usize, Felt::MAX is too large.


crates/starknet_os/src/hints/hint_implementation/stateless_compression/utils.rs line 415 at r1 (raw file):

Previously, dorimedini-starkware wrote…

update docstring (I think there was a discussion on a previous iteration of this PR)

What's wrong with the current doc?


crates/starknet_os/src/hints/hint_implementation/stateless_compression/utils.rs line 419 at r1 (raw file):

Previously, dorimedini-starkware wrote…

can this ever happen?
isn't the correct answer 0 in this case?
consider making the input a Felt, not a usize, so that

  1. we can give an elm_bound of more than usize::MAX (which is much less then 256 bits - or even 128 bits - unless running on a CPU from the future)
  2. if you have Felt as input, converting to biguint and counting bits always results in at most MAX_N_BITS, right?

if (1) is never required (i.e. elm_bounds is always at most 128 bits), please change the type to u128 (or u64, smallest possible); usize depends on architecture and unclear from reading that the bits are always within allowed limit

The upper bound is 20 bits.


crates/starknet_os/src/hints/hint_implementation/stateless_compression/utils.rs line 434 at r1 (raw file):

Previously, dorimedini-starkware wrote…

this function can fail if you provide invalid input; make it private please

Done.

Copy link
Collaborator

@dorimedini-starkware dorimedini-starkware left a comment

Choose a reason for hiding this comment

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

Reviewed 3 of 3 files at r2, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @yoavGrs and @Yoni-Starkware)


crates/starknet_os/src/hints/hint_implementation/stateless_compression/utils.rs line 415 at r1 (raw file):

Previously, yoavGrs wrote…

What's wrong with the current doc?

as yoni mentioned, this function doesn't exactly return the number of elements up to the bound that can be packed - the packing is assuming an actual bound of ceil(log2(elm_bound)) bits

@yoavGrs yoavGrs force-pushed the yoav/snos/hints/compression_hint/compression_set branch from be04639 to 6420376 Compare March 4, 2025 07:51
@yoavGrs yoavGrs force-pushed the yoav/snos/hints/compression_hint/pack_usize_in_felt branch from 424eaae to 5a9e302 Compare March 4, 2025 08:00
Copy link
Contributor Author

@yoavGrs yoavGrs left a comment

Choose a reason for hiding this comment

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

Reviewable status: 1 of 4 files reviewed, 1 unresolved discussion (waiting on @dorimedini-starkware and @Yoni-Starkware)


crates/starknet_os/src/hints/hint_implementation/stateless_compression/utils.rs line 415 at r1 (raw file):

Previously, dorimedini-starkware wrote…

as yoni mentioned, this function doesn't exactly return the number of elements up to the bound that can be packed - the packing is assuming an actual bound of ceil(log2(elm_bound)) bits

Changed.

Copy link
Collaborator

@dorimedini-starkware dorimedini-starkware left a comment

Choose a reason for hiding this comment

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

Reviewed 3 of 3 files at r3, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @Yoni-Starkware)

Copy link
Collaborator

@Yoni-Starkware Yoni-Starkware left a comment

Choose a reason for hiding this comment

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

Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @yoavGrs)


crates/starknet_os/src/hints/hint_implementation/stateless_compression/utils.rs line 426 at r3 (raw file):

        return MAX_N_BITS;
    }
    let n_bits_required = 32 - elm_bound.leading_zeros();

Why not elm_bound.ilog2() + 1?
Seems safer. You are using here implicitly that elm_bound is a u32.

Code quote:

let n_bits_required = 32 - elm_bound.leading_zeros();

@yoavGrs yoavGrs force-pushed the yoav/snos/hints/compression_hint/pack_usize_in_felt branch from 5a9e302 to 8e797d5 Compare March 4, 2025 09:07
Copy link
Contributor Author

@yoavGrs yoavGrs left a comment

Choose a reason for hiding this comment

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

Reviewable status: 3 of 4 files reviewed, 1 unresolved discussion (waiting on @dorimedini-starkware and @Yoni-Starkware)


crates/starknet_os/src/hints/hint_implementation/stateless_compression/utils.rs line 426 at r3 (raw file):

Previously, Yoni-Starkware (Yoni) wrote…

Why not elm_bound.ilog2() + 1?
Seems safer. You are using here implicitly that elm_bound is a u32.

Done.

Copy link
Collaborator

@dorimedini-starkware dorimedini-starkware left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 1 files at r4, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @Yoni-Starkware)

@yoavGrs yoavGrs force-pushed the yoav/snos/hints/compression_hint/compression_set branch from 6420376 to 8ca1934 Compare March 4, 2025 11:17
@yoavGrs yoavGrs force-pushed the yoav/snos/hints/compression_hint/pack_usize_in_felt branch from 8e797d5 to 7a22197 Compare March 4, 2025 11:18
@yoavGrs yoavGrs force-pushed the yoav/snos/hints/compression_hint/compression_set branch from 8ca1934 to 40f25b9 Compare March 4, 2025 11:47
@yoavGrs yoavGrs force-pushed the yoav/snos/hints/compression_hint/pack_usize_in_felt branch from 7a22197 to e1e6492 Compare March 4, 2025 12:06
Copy link
Collaborator

@dorimedini-starkware dorimedini-starkware left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 1 files at r5, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @yoavGrs)

@yoavGrs yoavGrs force-pushed the yoav/snos/hints/compression_hint/compression_set branch from 40f25b9 to 69683ad Compare March 4, 2025 12:13
@yoavGrs yoavGrs force-pushed the yoav/snos/hints/compression_hint/pack_usize_in_felt branch from e1e6492 to b197099 Compare March 4, 2025 12:53
@yoavGrs yoavGrs changed the base branch from yoav/snos/hints/compression_hint/compression_set to main March 4, 2025 12:53
Copy link
Collaborator

@dorimedini-starkware dorimedini-starkware left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 1 files at r6, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @yoavGrs)

Copy link
Collaborator

@Yoni-Starkware Yoni-Starkware left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 4 files at r1, 1 of 1 files at r4, 1 of 1 files at r5, 1 of 1 files at r6, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @yoavGrs)

Copy link
Collaborator

@Yoni-Starkware Yoni-Starkware left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @yoavGrs)

Merged via the queue into main with commit b0c4839 Mar 4, 2025
20 checks passed
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