-
Notifications
You must be signed in to change notification settings - Fork 38
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
feat(starknet_os): pack usize in felts #4573
Conversation
Benchmark movements: |
There was a problem hiding this 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:
- Felt::MAX
- powers of two
- powers of two - 1
- 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
- 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)
- 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
e143521
to
be04639
Compare
bfa631d
to
424eaae
Compare
There was a problem hiding this 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:
- Felt::MAX
- powers of two
- powers of two - 1
- 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 answer0
in this case?
consider making the input aFelt
, not ausize
, so that
- 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)
- 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
(oru64
, 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.
There was a problem hiding this 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
be04639
to
6420376
Compare
424eaae
to
5a9e302
Compare
There was a problem hiding this 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.
There was a problem hiding this 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:complete! all files reviewed, all discussions resolved (waiting on @Yoni-Starkware)
There was a problem hiding this 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();
5a9e302
to
8e797d5
Compare
There was a problem hiding this 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 thatelm_bound
is a u32.
Done.
There was a problem hiding this 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)
6420376
to
8ca1934
Compare
8e797d5
to
7a22197
Compare
8ca1934
to
40f25b9
Compare
7a22197
to
e1e6492
Compare
There was a problem hiding this 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:complete! all files reviewed, all discussions resolved (waiting on @yoavGrs)
40f25b9
to
69683ad
Compare
e1e6492
to
b197099
Compare
There was a problem hiding this 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:complete! all files reviewed, all discussions resolved (waiting on @yoavGrs)
There was a problem hiding this 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:complete! all files reviewed, all discussions resolved (waiting on @yoavGrs)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status:
complete! all files reviewed, all discussions resolved (waiting on @yoavGrs)
No description provided.