Skip to content

Commit 34c19aa

Browse files
committed
Make secp256k1_scalar_get_bits support 32-bit reads
1 parent ae821a8 commit 34c19aa

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

src/scalar_4x64_impl.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,19 @@ SECP256K1_INLINE static void secp256k1_scalar_set_int(secp256k1_scalar *r, unsig
4141
}
4242

4343
SECP256K1_INLINE static unsigned int secp256k1_scalar_get_bits(const secp256k1_scalar *a, unsigned int offset, unsigned int count) {
44+
VERIFY_CHECK(count > 0 && count <= 32);
4445
VERIFY_CHECK((offset + count - 1) >> 6 == offset >> 6);
45-
return (a->d[offset >> 6] >> (offset & 0x3F)) & ((((uint64_t)1) << count) - 1);
46+
return (a->d[offset >> 6] >> (offset & 0x3F)) & (0xFFFFFFFF >> (32 - count));
4647
}
4748

4849
SECP256K1_INLINE static unsigned int secp256k1_scalar_get_bits_var(const secp256k1_scalar *a, unsigned int offset, unsigned int count) {
49-
VERIFY_CHECK(count < 32);
50+
VERIFY_CHECK(count > 0 && count <= 32);
5051
VERIFY_CHECK(offset + count <= 256);
5152
if ((offset + count - 1) >> 6 == offset >> 6) {
5253
return secp256k1_scalar_get_bits(a, offset, count);
5354
} else {
5455
VERIFY_CHECK((offset >> 6) + 1 < 4);
55-
return ((a->d[offset >> 6] >> (offset & 0x3F)) | (a->d[(offset >> 6) + 1] << (64 - (offset & 0x3F)))) & ((((uint64_t)1) << count) - 1);
56+
return ((a->d[offset >> 6] >> (offset & 0x3F)) | (a->d[(offset >> 6) + 1] << (64 - (offset & 0x3F)))) & (0xFFFFFFFF >> (32 - count));
5657
}
5758
}
5859

src/scalar_8x32_impl.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,19 @@ SECP256K1_INLINE static void secp256k1_scalar_set_int(secp256k1_scalar *r, unsig
5959
}
6060

6161
SECP256K1_INLINE static unsigned int secp256k1_scalar_get_bits(const secp256k1_scalar *a, unsigned int offset, unsigned int count) {
62+
VERIFY_CHECK(count > 0 && count <= 32);
6263
VERIFY_CHECK((offset + count - 1) >> 5 == offset >> 5);
63-
return (a->d[offset >> 5] >> (offset & 0x1F)) & ((1 << count) - 1);
64+
return (a->d[offset >> 5] >> (offset & 0x1F)) & (0xFFFFFFFF >> (32 - count));
6465
}
6566

6667
SECP256K1_INLINE static unsigned int secp256k1_scalar_get_bits_var(const secp256k1_scalar *a, unsigned int offset, unsigned int count) {
67-
VERIFY_CHECK(count < 32);
68+
VERIFY_CHECK(count > 0 && count <= 32);
6869
VERIFY_CHECK(offset + count <= 256);
6970
if ((offset + count - 1) >> 5 == offset >> 5) {
7071
return secp256k1_scalar_get_bits(a, offset, count);
7172
} else {
7273
VERIFY_CHECK((offset >> 5) + 1 < 8);
73-
return ((a->d[offset >> 5] >> (offset & 0x1F)) | (a->d[(offset >> 5) + 1] << (32 - (offset & 0x1F)))) & ((((uint32_t)1) << count) - 1);
74+
return ((a->d[offset >> 5] >> (offset & 0x1F)) | (a->d[(offset >> 5) + 1] << (32 - (offset & 0x1F)))) & (0xFFFFFFFF >> (32 - count));
7475
}
7576
}
7677

src/scalar_low_impl.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ SECP256K1_INLINE static void secp256k1_scalar_clear(secp256k1_scalar *r) { *r =
1919
SECP256K1_INLINE static void secp256k1_scalar_set_int(secp256k1_scalar *r, unsigned int v) { *r = v; }
2020

2121
SECP256K1_INLINE static unsigned int secp256k1_scalar_get_bits(const secp256k1_scalar *a, unsigned int offset, unsigned int count) {
22-
if (offset < 32)
23-
return ((*a >> offset) & ((((uint32_t)1) << count) - 1));
24-
else
22+
VERIFY_CHECK(count > 0 && count <= 32);
23+
if (offset < 32) {
24+
return (*a >> offset) & (0xFFFFFFFF >> (32 - count));
25+
} else {
2526
return 0;
27+
}
2628
}
2729

2830
SECP256K1_INLINE static unsigned int secp256k1_scalar_get_bits_var(const secp256k1_scalar *a, unsigned int offset, unsigned int count) {

0 commit comments

Comments
 (0)