-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Improve checks for scalar _get_bits methods #1845
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -41,20 +41,22 @@ SECP256K1_INLINE static void secp256k1_scalar_set_int(secp256k1_scalar *r, unsig | |||||
| SECP256K1_INLINE static uint32_t secp256k1_scalar_get_bits_limb32(const secp256k1_scalar *a, unsigned int offset, unsigned int count) { | ||||||
| SECP256K1_SCALAR_VERIFY(a); | ||||||
| VERIFY_CHECK(count > 0 && count <= 32); | ||||||
| VERIFY_CHECK((offset + count - 1) >> 6 == offset >> 6); | ||||||
| VERIFY_CHECK(offset <= 256 - count); | ||||||
| VERIFY_CHECK((offset + count - 1) >> 5 == offset >> 5); | ||||||
|
|
||||||
| return (a->d[offset >> 6] >> (offset & 0x3F)) & (0xFFFFFFFF >> (32 - count)); | ||||||
| } | ||||||
|
|
||||||
| SECP256K1_INLINE static uint32_t secp256k1_scalar_get_bits_var(const secp256k1_scalar *a, unsigned int offset, unsigned int count) { | ||||||
| SECP256K1_SCALAR_VERIFY(a); | ||||||
| VERIFY_CHECK(count > 0 && count <= 32); | ||||||
| VERIFY_CHECK(offset + count <= 256); | ||||||
| VERIFY_CHECK(offset <= 256 - count); | ||||||
|
Comment on lines
-52
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if those aren't the same check. I mean, |
||||||
|
|
||||||
| if ((offset + count - 1) >> 6 == offset >> 6) { | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here, we are checking if we are not crossing limbs when collecting bits. I think it's ok to inline the function in the following, but not so much if we are testing a different condition compared to the called function, which is what this PR proposes by changing line R45. Why not
Suggested change
as well? |
||||||
| return secp256k1_scalar_get_bits_limb32(a, offset, count); | ||||||
| return (a->d[offset >> 6] >> (offset & 0x3F)) & (0xFFFFFFFF >> (32 - count)); | ||||||
| } else { | ||||||
| VERIFY_CHECK((offset >> 6) + 1 < 4); | ||||||
| VERIFY_CHECK((offset & 0x3F) > 0); | ||||||
| return ((a->d[offset >> 6] >> (offset & 0x3F)) | (a->d[(offset >> 6) + 1] << (64 - (offset & 0x3F)))) & (0xFFFFFFFF >> (32 - count)); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
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.
I'm convinced this will not break things and I like this more after fiddling with the bits.
But feels like the original check combined with line 43 was already sufficient for checking we are not able to cross limbs when trying to collect bits from the scalar.
One such case could be:
offset = 31.With the original check:
RHS:
offset >> 6 = 0LHS:
(31 + count - 1) >> 6=>count < 34so that RHS == LHS.But line 43 already guarantee that
count <= 32. Similar for the other limbs.