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

Added Cairo inv_mod function #3172

Merged
merged 1 commit into from
May 17, 2023
Merged

Added Cairo inv_mod function #3172

merged 1 commit into from
May 17, 2023

Conversation

yuvalsw
Copy link
Contributor

@yuvalsw yuvalsw commented May 16, 2023

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.

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


corelib/src/math.cairo line 63 at r1 (raw file):

        if sub_direction {
            let (_, inv) = TDivRemImpl::div_rem(s, n);
            Option::Some(inv)

Suggestion:

            // Extended gcd returns `s` <= [n / 2*g] - so it is already in range.
            Option::Some(s)

corelib/src/math.cairo line 68 at r1 (raw file):

            // sufficient to add `n` to make sure we have an unsigned integer.
            let (_, inv) = TDivRemImpl::div_rem(n.into() - s, n);
            Option::Some(inv)

Suggestion:

            // The absolute values of Bezout coefficients are guaranteed to be `< n`, 
            // so it will never be 0, so it is sufficient to return `n - s` to get a number in the correct range.
            Option::Some(n.into() - s)

@yuvalsw yuvalsw force-pushed the yuval/invmod branch 2 times, most recently from 988f10b to 7a8e2c5 Compare May 17, 2023 09:57
Copy link
Contributor Author

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


corelib/src/math.cairo line 63 at r1 (raw file):

        if sub_direction {
            let (_, inv) = TDivRemImpl::div_rem(s, n);
            Option::Some(inv)

Done.


corelib/src/math.cairo line 68 at r1 (raw file):

            // sufficient to add `n` to make sure we have an unsigned integer.
            let (_, inv) = TDivRemImpl::div_rem(n.into() - s, n);
            Option::Some(inv)

Done.

@yuvalsw yuvalsw changed the base branch from yuval/egcd2 to main May 17, 2023 09:58
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 6 of 6 files at r5, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @yuvalsw)

@yuvalsw yuvalsw enabled auto-merge May 17, 2023 11:14
@yuvalsw yuvalsw added this pull request to the merge queue May 17, 2023
Merged via the queue into main with commit 1cf52c0 May 17, 2023
milancermak pushed a commit to milancermak/cairo that referenced this pull request May 23, 2023
@orizi orizi deleted the yuval/invmod branch June 28, 2023 05:25
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