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: impl From<EthAddress> for StarkFelt #164

Merged

Conversation

tdelabro
Copy link
Contributor

@tdelabro tdelabro commented Dec 20, 2023

This change is Reviewable

Copy link
Contributor

@giladchase giladchase 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: 0 of 3 files reviewed, 2 unresolved discussions (waiting on @tdelabro)


src/core.rs line 271 at r1 (raw file):

        // Padding H160 with zeros to 32 bytes (big endian)
        bytes[12..32].copy_from_slice(value.0.as_bytes());
        StarkFelt::new_unchecked(bytes)

I think the risk of adding this unsafe constructor is warranted for this usage.
WDYT?

Suggestion:

        StarkFelt::new(bytes).expect("EthAddress should be within StarkFelt bounds")

src/hash.rs line 60 at r1 (raw file):

    }

    pub const fn new_unchecked(bytes: [u8; 32]) -> StarkFelt {

See comment above please.

@tdelabro
Copy link
Contributor Author

tdelabro commented Jan 3, 2024

The unchecked constructor is used there, but it won't be the only place. We added it to our fork as it is useful in multiple places.
The main thing is that it is const while new cannot be const because the check returns an error that allocates memory:

        Err(StarknetApiError::OutOfRange { string: hex_str_from_bytes::<32, true>(bytes) })

So there is now a way to create StarkFelt in a const environment, which is useful/required.

Copy link
Contributor

@giladchase giladchase left a comment

Choose a reason for hiding this comment

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

Fair enough, so please add a docstring to this method that sufficiently scares off users against abusing it.
In other words, when a StarkFelt is created this way for an out of bounds number, here be dragons...

Reviewable status: 0 of 3 files reviewed, all discussions resolved

@tdelabro
Copy link
Contributor Author

tdelabro commented Jan 8, 2024

Fair enough, so please add a docstring to this method that sufficiently scares off users against abusing it.
In other words, when a StarkFelt is created this way for an out of bounds number, here be dragons...

@giladchase Done

Copy link
Contributor

@giladchase giladchase left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 2 of 3 files at r1, 1 of 1 files at r2, all commit messages.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @tdelabro)


src/hash.rs line 68 at r2 (raw file):

    /// The `StarkFelt::new` method make sure that you can't initialize a `StarkFelt` greater than
    /// it's specification maximum. This method does not not. It's your responsability make sure
    /// it's okay to call this method.

For brevity, also to avoid duplicated instructions.

Suggestion:

/// To avoid undefined behavior, refer to [`StarkFelt`] struct's docstring for the required constraints on the `bytes` argument, or use [`StarkFelt::new`] instead of this method.

src/hash.rs line 75 at r2 (raw file):

    /// - creating instances of `StarkFelt` at compile time
    /// - implementing `From<T> for StarkFelt` on types that have a smaller binary representation
    ///   than `StarkFelt`

Suggestion:

    /// Most of the time you should use `new` instead, but it comes in handy for a few cases:
    /// - creating instances of `StarkFelt` at compile time
    /// - implementing `From<T> for StarkFelt` for types that have a smaller binary representation
    ///   than `StarkFelt`

Copy link
Contributor

@giladchase giladchase 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 r3, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @tdelabro)

Copy link
Contributor

@giladchase giladchase left a comment

Choose a reason for hiding this comment

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

Plz rebase and i'll merge it.

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

@tdelabro tdelabro force-pushed the StarkFelt-from-EthAddress branch from 6b1c7cd to a69acc8 Compare January 10, 2024 18:51
@tdelabro
Copy link
Contributor Author

Plz rebase and i'll merge it.

Just did :)

Copy link
Contributor

@giladchase giladchase left a comment

Choose a reason for hiding this comment

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

Reviewed 2 of 2 files at r4, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @tdelabro)

@giladchase giladchase added this pull request to the merge queue Jan 11, 2024
Merged via the queue into starkware-libs:main with commit 10a5b78 Jan 11, 2024
6 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.

2 participants