Skip to content

Commit 283cd80

Browse files
committed
Abstract out verify logic for fe_get_bounds
1 parent d5aa2f0 commit 283cd80

File tree

4 files changed

+17
-18
lines changed

4 files changed

+17
-18
lines changed

src/field.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ static const secp256k1_fe secp256k1_const_beta = SECP256K1_FE_CONST(
9797
# define secp256k1_fe_from_storage secp256k1_fe_impl_from_storage
9898
# define secp256k1_fe_inv secp256k1_fe_impl_inv
9999
# define secp256k1_fe_inv_var secp256k1_fe_impl_inv_var
100+
# define secp256k1_fe_get_bounds secp256k1_fe_impl_get_bounds
100101
#endif /* !defined(VERIFY) */
101102

102103
/** Normalize a field element.
@@ -306,8 +307,9 @@ static void secp256k1_fe_cmov(secp256k1_fe *r, const secp256k1_fe *a, int flag);
306307
* The output is not guaranteed to be normalized, regardless of the input. */
307308
static void secp256k1_fe_half(secp256k1_fe *r);
308309

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

313315
/** Determine whether a is a square (modulo p). */

src/field_10x26_impl.h

+1-8
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ static void secp256k1_fe_impl_verify(const secp256k1_fe *a) {
3838
}
3939
#endif
4040

41-
static void secp256k1_fe_get_bounds(secp256k1_fe *r, int m) {
42-
VERIFY_CHECK(m >= 0);
43-
VERIFY_CHECK(m <= 2048);
41+
static void secp256k1_fe_impl_get_bounds(secp256k1_fe *r, int m) {
4442
r->n[0] = 0x3FFFFFFUL * 2 * m;
4543
r->n[1] = 0x3FFFFFFUL * 2 * m;
4644
r->n[2] = 0x3FFFFFFUL * 2 * m;
@@ -51,11 +49,6 @@ static void secp256k1_fe_get_bounds(secp256k1_fe *r, int m) {
5149
r->n[7] = 0x3FFFFFFUL * 2 * m;
5250
r->n[8] = 0x3FFFFFFUL * 2 * m;
5351
r->n[9] = 0x03FFFFFUL * 2 * m;
54-
#ifdef VERIFY
55-
r->magnitude = m;
56-
r->normalized = (m == 0);
57-
secp256k1_fe_verify(r);
58-
#endif
5952
}
6053

6154
static void secp256k1_fe_impl_normalize(secp256k1_fe *r) {

src/field_5x52_impl.h

+1-8
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,12 @@ 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] = 0xFFFFFFFFFFFFFULL * 2 * m;
4442
r->n[1] = 0xFFFFFFFFFFFFFULL * 2 * m;
4543
r->n[2] = 0xFFFFFFFFFFFFFULL * 2 * m;
4644
r->n[3] = 0xFFFFFFFFFFFFFULL * 2 * m;
4745
r->n[4] = 0x0FFFFFFFFFFFFULL * 2 * m;
48-
#ifdef VERIFY
49-
r->magnitude = m;
50-
r->normalized = (m == 0);
51-
secp256k1_fe_verify(r);
52-
#endif
5346
}
5447

5548
static void secp256k1_fe_impl_normalize(secp256k1_fe *r) {

src/field_impl.h

+11
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,17 @@ SECP256K1_INLINE static void secp256k1_fe_inv_var(secp256k1_fe *r, const secp256
373373
VERIFY_CHECK(secp256k1_fe_normalizes_to_zero(r) == input_is_zero);
374374
secp256k1_fe_verify(r);
375375
}
376+
377+
static void secp256k1_fe_impl_get_bounds(secp256k1_fe* r, int m);
378+
SECP256K1_INLINE static void secp256k1_fe_get_bounds(secp256k1_fe* r, int m) {
379+
VERIFY_CHECK(m >= 0);
380+
VERIFY_CHECK(m <= 32);
381+
secp256k1_fe_impl_get_bounds(r, m);
382+
r->magnitude = m;
383+
r->normalized = (m == 0);
384+
secp256k1_fe_verify(r);
385+
}
386+
376387
#endif /* defined(VERIFY) */
377388

378389
#endif /* SECP256K1_FIELD_IMPL_H */

0 commit comments

Comments
 (0)