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

Support BoxedUint::gcd(_vartime) where self and rhs have a different number of limbs #642

Merged
merged 1 commit into from
Aug 15, 2024

Conversation

fjarri
Copy link
Contributor

@fjarri fjarri commented Aug 6, 2024

The PR currently has just a testcase illustrating the problem.

So the question is, what should the behavior be? We can equalize the sizes of the arguments at the top level, but that would make gcd() not constant time wrt numbers of limbs. Then again, would users assume that it is?

@fjarri fjarri force-pushed the gcd-different-sizes branch from 7b8d39e to b20e627 Compare August 6, 2024 20:59
@tarcieri
Copy link
Member

tarcieri commented Aug 6, 2024

It's part of the general implicit widening problem: #312. Implicit widening may make sense here.

In many places we currently just panic rather than performing implicit widening, relying on the caller to widen appropriately.

There are of course a whole heap of potential timing variability concerns to worry about here, as you noted. That's why the panicking approach seemed like the most conservative in order to punt the problem onto the caller.

If we stick with panicking, it should use a proper assert at least.

@fjarri
Copy link
Contributor Author

fjarri commented Aug 6, 2024

I see. Perhaps then I should just add a check for that in gcd() itself, and test that it panics, because otherwise the panic happens deep inside Bernstein-Yang machinery and can be confusing.

@fjarri fjarri force-pushed the gcd-different-sizes branch from b20e627 to a55d522 Compare August 6, 2024 21:23
@tarcieri
Copy link
Member

tarcieri commented Aug 6, 2024

I can try to add implicit widening in a separate PR, unless you want to add it in this one. I think it's appropriate here.

@fjarri
Copy link
Contributor Author

fjarri commented Aug 6, 2024

That's fine, it can wait until the resolution on #312. It's just that the error caught me off-guard.

@tarcieri
Copy link
Member

tarcieri commented Aug 6, 2024

BoxedUnsatInt needs to allocate anyway, so we can implicitly widen there while also converting to u62-ish limbs

@fjarri
Copy link
Contributor Author

fjarri commented Aug 6, 2024

We need to equalize the number of limbs earlier, when making sure the first argument is odd and conditionally swapping them.

@tarcieri
Copy link
Member

tarcieri commented Aug 6, 2024

@fjarri I was suggesting finding the max of the two limb counts, similar to how it finds the max of two bits, then pass a limb count hint in when constructing BoxedUnsatInt.

That would be as opposed to calling BoxedUint::widen which would add another allocation.

@fjarri
Copy link
Contributor Author

fjarri commented Aug 6, 2024

But the difference in limb numbers will already lead to panic before that, in BoxedUint::gcd()

@tarcieri
Copy link
Member

tarcieri commented Aug 6, 2024

Aah! Yes, that is a problem, though there are several paths through Bernstein-Yang that avoid that.

I'm surprised there isn't a better length assert_eq! for that function.

@fjarri
Copy link
Contributor Author

fjarri commented Aug 6, 2024

Actually, it won't even lead to panic if the first argument is longer, it'll just behave incorrectly.

Edit: that is, in release mode it will, in debug mode there will be a panic from debug_assert().

@tarcieri
Copy link
Member

Will merge this to have a set of test cases for widening behavior, and then I'll take a look at actually implementing it. It shouldn't be too hard.

@tarcieri tarcieri merged commit 62babd9 into RustCrypto:master Aug 15, 2024
18 checks passed
@fjarri fjarri deleted the gcd-different-sizes branch August 16, 2024 17: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