Skip to content

Commit ee48e53

Browse files
committed
Abstract out verify logic for fe_get_bounds
1 parent ea0223f commit ee48e53

File tree

4 files changed

+17
-18
lines changed

4 files changed

+17
-18
lines changed

src/field.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ static void secp256k1_fe_verify(const secp256k1_fe *a);
9595
# define secp256k1_fe_from_storage secp256k1_fe_impl_from_storage
9696
# define secp256k1_fe_inv secp256k1_fe_impl_inv
9797
# define secp256k1_fe_inv_var secp256k1_fe_impl_inv_var
98+
# define secp256k1_fe_get_bounds secp256k1_fe_impl_get_bounds
9899
#endif /* defined(VERIFY) */
99100

100101
/** Normalize a field element.
@@ -301,8 +302,9 @@ static void secp256k1_fe_cmov(secp256k1_fe *r, const secp256k1_fe *a, int flag);
301302
* The output is not guaranteed to be normalized, regardless of the input. */
302303
static void secp256k1_fe_half(secp256k1_fe *r);
303304

304-
/** Sets each limb of 'r' to its upper bound at magnitude 'm'. The output will also have its
305-
* magnitude set to 'm' and is normalized if (and only if) 'm' is zero. */
305+
/** Sets r to a field element with magnitude m, normalized if (and only if) m==0.
306+
* The value is chosen so that it is likely to trigger edge cases related to
307+
* internal overflows. */
306308
static void secp256k1_fe_get_bounds(secp256k1_fe *r, int m);
307309

308310
#endif /* SECP256K1_FIELD_H */

src/field_10x26_impl.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ static void secp256k1_fe_impl_verify(const secp256k1_fe *a) {
3737
}
3838
#endif
3939

40-
static void secp256k1_fe_get_bounds(secp256k1_fe *r, int m) {
41-
VERIFY_CHECK(m >= 0);
42-
VERIFY_CHECK(m <= 2048);
40+
static void secp256k1_fe_impl_get_bounds(secp256k1_fe *r, int m) {
4341
r->n[0] = 0x3FFFFFFUL * 2 * m;
4442
r->n[1] = 0x3FFFFFFUL * 2 * m;
4543
r->n[2] = 0x3FFFFFFUL * 2 * m;
@@ -50,11 +48,6 @@ static void secp256k1_fe_get_bounds(secp256k1_fe *r, int m) {
5048
r->n[7] = 0x3FFFFFFUL * 2 * m;
5149
r->n[8] = 0x3FFFFFFUL * 2 * m;
5250
r->n[9] = 0x03FFFFFUL * 2 * m;
53-
#ifdef VERIFY
54-
r->magnitude = m;
55-
r->normalized = (m == 0);
56-
secp256k1_fe_verify(r);
57-
#endif
5851
}
5952

6053
static void secp256k1_fe_impl_normalize(secp256k1_fe *r) {

src/field_5x52_impl.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,12 @@ static void secp256k1_fe_impl_verify(const secp256k1_fe *a) {
3636
}
3737
#endif
3838

39-
static void secp256k1_fe_get_bounds(secp256k1_fe *r, int m) {
40-
VERIFY_CHECK(m >= 0);
41-
VERIFY_CHECK(m <= 2048);
39+
static void secp256k1_fe_impl_get_bounds(secp256k1_fe *r, int m) {
4240
r->n[0] = 0xFFFFFFFFFFFFFULL * 2 * m;
4341
r->n[1] = 0xFFFFFFFFFFFFFULL * 2 * m;
4442
r->n[2] = 0xFFFFFFFFFFFFFULL * 2 * m;
4543
r->n[3] = 0xFFFFFFFFFFFFFULL * 2 * m;
4644
r->n[4] = 0x0FFFFFFFFFFFFULL * 2 * m;
47-
#ifdef VERIFY
48-
r->magnitude = m;
49-
r->normalized = (m == 0);
50-
secp256k1_fe_verify(r);
51-
#endif
5245
}
5346

5447
static void secp256k1_fe_impl_normalize(secp256k1_fe *r) {

src/field_impl.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,17 @@ SECP256K1_INLINE static void secp256k1_fe_inv_var(secp256k1_fe *r, const secp256
361361
VERIFY_CHECK(secp256k1_fe_normalizes_to_zero(r) == input_is_zero);
362362
secp256k1_fe_verify(r);
363363
}
364+
365+
static void secp256k1_fe_impl_get_bounds(secp256k1_fe* r, int m);
366+
SECP256K1_INLINE static void secp256k1_fe_get_bounds(secp256k1_fe* r, int m) {
367+
VERIFY_CHECK(m >= 0);
368+
VERIFY_CHECK(m <= 32);
369+
secp256k1_fe_impl_get_bounds(r, m);
370+
r->magnitude = m;
371+
r->normalized = (m == 0);
372+
secp256k1_fe_verify(r);
373+
}
374+
364375
#endif /* defined(VERIFY) */
365376

366377
#endif /* SECP256K1_FIELD_IMPL_H */

0 commit comments

Comments
 (0)