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

Add TryInto<u512, u256> #5031

Merged
merged 9 commits into from
Feb 14, 2024
Merged

Conversation

delaaxe
Copy link
Contributor

@delaaxe delaaxe commented Feb 7, 2024

This change is Reviewable

Copy link
Collaborator

@orizi orizi 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 2 files reviewed, 1 unresolved discussion (waiting on @delaaxe)

a discussion (no related file):
any particular usecase for this?
in general we only use u512 for modular operation back into u256.


@delaaxe
Copy link
Contributor Author

delaaxe commented Feb 13, 2024

Our use case is using the first return value of u512_safe_div_rem_by_u256 as a u256

Copy link
Collaborator

@orizi orizi 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 r1, all commit messages.
Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @delaaxe)


corelib/src/integer.cairo line 1384 at r2 (raw file):

        }
    }
}

no need (it was required in some specific other instances)

Suggestion:

impl U512TryIntoU256 of TryInto<u512, u256> {
    fn try_into(self: u512) -> Option<u256> {
        if self.limb2 != 0 || self.limb3 != 0 {
            Option::None
        } else {
            Option::Some(u256 { low: self.limb0, high: self.limb1 })
        }
    }
}

corelib/src/test/integer_test.cairo line 811 at r2 (raw file):

    };
    let option: Option<u256> = num.try_into();
    assert(option.is_none(), 'u512 -/-> u256');

Suggestion:

    assert!(u512 { limb0: 1, limb1: 2, limb2: 0, limb3: 0 }.try_into() == Option::Some(0x200000000000000000000000000000001_u256));
    assert!(u512 { limb0: 1, limb1: 2, limb2: 3, limb3: 0 }.try_into() == Option::<u256>::None);
    assert!(u512 { limb0: 1, limb1: 2, limb2: 0, limb3: 4 }.try_into() == Option::<u256>::None);
    assert!(u512 { limb0: 1, limb1: 2, limb2: 3, limb3: 4 }.try_into() == Option::<u256>::None);

Copy link
Contributor Author

@delaaxe delaaxe 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 10 files reviewed, 2 unresolved discussions (waiting on @orizi)


corelib/src/integer.cairo line 1384 at r2 (raw file):

Previously, orizi wrote…

no need (it was required in some specific other instances)

Done.


corelib/src/test/integer_test.cairo line 811 at r2 (raw file):

    };
    let option: Option<u256> = num.try_into();
    assert(option.is_none(), 'u512 -/-> u256');

Done.

Copy link
Collaborator

@orizi orizi 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 1 of 2 files at r4, 8 of 8 files at r5, 1 of 1 files at r6, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @delaaxe)

a discussion (no related file):
@gilbens-starkware for 2nd eye.


Copy link
Collaborator

@orizi orizi 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 @delaaxe)

Copy link
Contributor

@gilbens-starkware gilbens-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: all files reviewed, 1 unresolved discussion (waiting on @delaaxe)

@orizi orizi added this pull request to the merge queue Feb 14, 2024
Copy link
Collaborator

@orizi orizi 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: :shipit: complete! all files reviewed, all discussions resolved (waiting on @delaaxe)

Merged via the queue into starkware-libs:main with commit 1ac6771 Feb 14, 2024
43 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.

6 participants