Skip to content

Commit 45c5ca7

Browse files
Merge bitcoin-core/secp256k1#1350: scalar: introduce and use secp256k1_{read,write}_be64 helpers
7067ee5 tests: add tests for `secp256k1_{read,write}_be64` (Sebastian Falbesoner) 740528c scalar: use newly introduced `secp256k1_{read,write}_be64` helpers (4x64 impl.) (Sebastian Falbesoner) Pull request description: This is a simple follow-up to bitcoin#1339, as suggested in comment bitcoin-core/secp256k1#1339 (comment). ACKs for top commit: stratospher: ACK 7067ee5. real-or-random: utACK 7067ee5 Tree-SHA512: f9bc2ab610099948ffac1e6bb3c822bd90b81a7110ab74cec03175e2c92ed27694a15f9cdaa7c4f1b460fe459f61c3d1d102c99592169f127fdd7539a1a0c154
2 parents 67214f5 + 7067ee5 commit 45c5ca7

File tree

3 files changed

+55
-16
lines changed

3 files changed

+55
-16
lines changed

src/scalar_4x64_impl.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -133,21 +133,21 @@ static void secp256k1_scalar_cadd_bit(secp256k1_scalar *r, unsigned int bit, int
133133

134134
static void secp256k1_scalar_set_b32(secp256k1_scalar *r, const unsigned char *b32, int *overflow) {
135135
int over;
136-
r->d[0] = ((uint64_t)secp256k1_read_be32(&b32[24]) << 32) | (uint64_t)secp256k1_read_be32(&b32[28]);
137-
r->d[1] = ((uint64_t)secp256k1_read_be32(&b32[16]) << 32) | (uint64_t)secp256k1_read_be32(&b32[20]);
138-
r->d[2] = ((uint64_t)secp256k1_read_be32(&b32[8]) << 32) | (uint64_t)secp256k1_read_be32(&b32[12]);
139-
r->d[3] = ((uint64_t)secp256k1_read_be32(&b32[0]) << 32) | (uint64_t)secp256k1_read_be32(&b32[4]);
136+
r->d[0] = secp256k1_read_be64(&b32[24]);
137+
r->d[1] = secp256k1_read_be64(&b32[16]);
138+
r->d[2] = secp256k1_read_be64(&b32[8]);
139+
r->d[3] = secp256k1_read_be64(&b32[0]);
140140
over = secp256k1_scalar_reduce(r, secp256k1_scalar_check_overflow(r));
141141
if (overflow) {
142142
*overflow = over;
143143
}
144144
}
145145

146146
static void secp256k1_scalar_get_b32(unsigned char *bin, const secp256k1_scalar* a) {
147-
secp256k1_write_be32(&bin[0], a->d[3] >> 32); secp256k1_write_be32(&bin[4], a->d[3]);
148-
secp256k1_write_be32(&bin[8], a->d[2] >> 32); secp256k1_write_be32(&bin[12], a->d[2]);
149-
secp256k1_write_be32(&bin[16], a->d[1] >> 32); secp256k1_write_be32(&bin[20], a->d[1]);
150-
secp256k1_write_be32(&bin[24], a->d[0] >> 32); secp256k1_write_be32(&bin[28], a->d[0]);
147+
secp256k1_write_be64(&bin[0], a->d[3]);
148+
secp256k1_write_be64(&bin[8], a->d[2]);
149+
secp256k1_write_be64(&bin[16], a->d[1]);
150+
secp256k1_write_be64(&bin[24], a->d[0]);
151151
}
152152

153153
SECP256K1_INLINE static int secp256k1_scalar_is_zero(const secp256k1_scalar *a) {

src/tests.c

+23-8
Original file line numberDiff line numberDiff line change
@@ -7516,16 +7516,31 @@ static void run_secp256k1_memczero_test(void) {
75167516
}
75177517

75187518
static void run_secp256k1_byteorder_tests(void) {
7519-
const uint32_t x = 0xFF03AB45;
7520-
const unsigned char x_be[4] = {0xFF, 0x03, 0xAB, 0x45};
7521-
unsigned char buf[4];
7522-
uint32_t x_;
7519+
{
7520+
const uint32_t x = 0xFF03AB45;
7521+
const unsigned char x_be[4] = {0xFF, 0x03, 0xAB, 0x45};
7522+
unsigned char buf[4];
7523+
uint32_t x_;
7524+
7525+
secp256k1_write_be32(buf, x);
7526+
CHECK(secp256k1_memcmp_var(buf, x_be, sizeof(buf)) == 0);
75237527

7524-
secp256k1_write_be32(buf, x);
7525-
CHECK(secp256k1_memcmp_var(buf, x_be, sizeof(buf)) == 0);
7528+
x_ = secp256k1_read_be32(buf);
7529+
CHECK(x == x_);
7530+
}
75267531

7527-
x_ = secp256k1_read_be32(buf);
7528-
CHECK(x == x_);
7532+
{
7533+
const uint64_t x = 0xCAFE0123BEEF4567;
7534+
const unsigned char x_be[8] = {0xCA, 0xFE, 0x01, 0x23, 0xBE, 0xEF, 0x45, 0x67};
7535+
unsigned char buf[8];
7536+
uint64_t x_;
7537+
7538+
secp256k1_write_be64(buf, x);
7539+
CHECK(secp256k1_memcmp_var(buf, x_be, sizeof(buf)) == 0);
7540+
7541+
x_ = secp256k1_read_be64(buf);
7542+
CHECK(x == x_);
7543+
}
75297544
}
75307545

75317546
static void int_cmov_test(void) {

src/util.h

+24
Original file line numberDiff line numberDiff line change
@@ -353,4 +353,28 @@ SECP256K1_INLINE static void secp256k1_write_be32(unsigned char* p, uint32_t x)
353353
p[0] = x >> 24;
354354
}
355355

356+
/* Read a uint64_t in big endian */
357+
SECP256K1_INLINE static uint64_t secp256k1_read_be64(const unsigned char* p) {
358+
return (uint64_t)p[0] << 56 |
359+
(uint64_t)p[1] << 48 |
360+
(uint64_t)p[2] << 40 |
361+
(uint64_t)p[3] << 32 |
362+
(uint64_t)p[4] << 24 |
363+
(uint64_t)p[5] << 16 |
364+
(uint64_t)p[6] << 8 |
365+
(uint64_t)p[7];
366+
}
367+
368+
/* Write a uint64_t in big endian */
369+
SECP256K1_INLINE static void secp256k1_write_be64(unsigned char* p, uint64_t x) {
370+
p[7] = x;
371+
p[6] = x >> 8;
372+
p[5] = x >> 16;
373+
p[4] = x >> 24;
374+
p[3] = x >> 32;
375+
p[2] = x >> 40;
376+
p[1] = x >> 48;
377+
p[0] = x >> 56;
378+
}
379+
356380
#endif /* SECP256K1_UTIL_H */

0 commit comments

Comments
 (0)