Skip to content

Commit 7fa5195

Browse files
committed
Bugfix: correct SECP256K1_FE_CONST mag/norm fields
1 parent b29566c commit 7fa5195

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

src/field.h

+20
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,26 @@
4545
#error "Please select wide multiplication implementation"
4646
#endif
4747

48+
#ifdef VERIFY
49+
/* Magnitude and normalized value for constants. */
50+
#define SECP256K1_FE_VERIFY_CONST(d7, d6, d5, d4, d3, d2, d1, d0) \
51+
/* Magnitude is 0 for constant 0; 1 otherwise. */ \
52+
, (((d7) | (d6) | (d5) | (d4) | (d3) | (d2) | (d1) | (d0)) != 0) \
53+
/* Normalized is 1 unless sum(d_i<<(32*i) for i=0..7) exceeds field modulus. */ \
54+
, (!(((d7) & (d6) & (d5) & (d4) & (d3) & (d2)) == 0xfffffffful && ((d1) == 0xfffffffful || ((d1) == 0xfffffffe && (d0 >= 0xfffffc2f)))))
55+
#else
56+
#define SECP256K1_FE_VERIFY_CONST(d7, d6, d5, d4, d3, d2, d1, d0)
57+
#endif
58+
59+
/** This expands to an initializer for a secp256k1_fe valued sum((i*32) * d_i, i=0..7) mod p.
60+
*
61+
* It has magnitude 1, unless d_i are all 0, in which case the magnitude is 0.
62+
* It is normalized, unless sum(2^(i*32) * d_i, i=0..7) >= p.
63+
*
64+
* SECP256K1_FE_CONST_INNER is provided by the implementation.
65+
*/
66+
#define SECP256K1_FE_CONST(d7, d6, d5, d4, d3, d2, d1, d0) {SECP256K1_FE_CONST_INNER((d7), (d6), (d5), (d4), (d3), (d2), (d1), (d0)) SECP256K1_FE_VERIFY_CONST((d7), (d6), (d5), (d4), (d3), (d2), (d1), (d0)) }
67+
4868
static const secp256k1_fe secp256k1_fe_one = SECP256K1_FE_CONST(0, 0, 0, 0, 0, 0, 0, 1);
4969
static const secp256k1_fe secp256k1_const_beta = SECP256K1_FE_CONST(
5070
0x7ae96a2bul, 0x657c0710ul, 0x6e64479eul, 0xac3434e9ul,

src/field_10x26.h

-6
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,6 @@ typedef struct {
4747
(((uint32_t)d7) >> 10) \
4848
}
4949

50-
#ifdef VERIFY
51-
#define SECP256K1_FE_CONST(d7, d6, d5, d4, d3, d2, d1, d0) {SECP256K1_FE_CONST_INNER((d7), (d6), (d5), (d4), (d3), (d2), (d1), (d0)), 1, 1}
52-
#else
53-
#define SECP256K1_FE_CONST(d7, d6, d5, d4, d3, d2, d1, d0) {SECP256K1_FE_CONST_INNER((d7), (d6), (d5), (d4), (d3), (d2), (d1), (d0))}
54-
#endif
55-
5650
typedef struct {
5751
uint32_t n[8];
5852
} secp256k1_fe_storage;

src/field_5x52.h

-6
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,6 @@ typedef struct {
4242
((uint64_t)(d6) >> 16) | (((uint64_t)(d7)) << 16) \
4343
}
4444

45-
#ifdef VERIFY
46-
#define SECP256K1_FE_CONST(d7, d6, d5, d4, d3, d2, d1, d0) {SECP256K1_FE_CONST_INNER((d7), (d6), (d5), (d4), (d3), (d2), (d1), (d0)), 1, 1}
47-
#else
48-
#define SECP256K1_FE_CONST(d7, d6, d5, d4, d3, d2, d1, d0) {SECP256K1_FE_CONST_INNER((d7), (d6), (d5), (d4), (d3), (d2), (d1), (d0))}
49-
#endif
50-
5145
typedef struct {
5246
uint64_t n[4];
5347
} secp256k1_fe_storage;

0 commit comments

Comments
 (0)