Skip to content

Commit 03cfd45

Browse files
committed
Abstract out verify logic for fe_get_bounds
1 parent f84bc75 commit 03cfd45

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
@@ -100,6 +100,7 @@ static void secp256k1_fe_verify(const secp256k1_fe *a);
100100
# define secp256k1_fe_from_storage secp256k1_fe_impl_from_storage
101101
# define secp256k1_fe_inv secp256k1_fe_impl_inv
102102
# define secp256k1_fe_inv_var secp256k1_fe_impl_inv_var
103+
# define secp256k1_fe_get_bounds secp256k1_fe_impl_get_bounds
103104
#endif /* defined(VERIFY) */
104105

105106
/** 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
#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
@@ -40,19 +40,12 @@ static void secp256k1_fe_impl_verify(const secp256k1_fe *a) {
4040
}
4141
#endif
4242

43-
static void secp256k1_fe_get_bounds(secp256k1_fe *r, int m) {
44-
VERIFY_CHECK(m >= 0);
45-
VERIFY_CHECK(m <= 2048);
43+
static void secp256k1_fe_impl_get_bounds(secp256k1_fe *r, int m) {
4644
r->n[0] = 0xFFFFFFFFFFFFFULL * 2 * m;
4745
r->n[1] = 0xFFFFFFFFFFFFFULL * 2 * m;
4846
r->n[2] = 0xFFFFFFFFFFFFFULL * 2 * m;
4947
r->n[3] = 0xFFFFFFFFFFFFFULL * 2 * m;
5048
r->n[4] = 0x0FFFFFFFFFFFFULL * 2 * m;
51-
#ifdef VERIFY
52-
r->magnitude = m;
53-
r->normalized = (m == 0);
54-
secp256k1_fe_verify(r);
55-
#endif
5649
}
5750

5851
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
@@ -365,6 +365,17 @@ SECP256K1_INLINE static void secp256k1_fe_inv_var(secp256k1_fe *r, const secp256
365365
VERIFY_CHECK(secp256k1_fe_normalizes_to_zero(r) == input_is_zero);
366366
secp256k1_fe_verify(r);
367367
}
368+
369+
static void secp256k1_fe_impl_get_bounds(secp256k1_fe* r, int m);
370+
SECP256K1_INLINE static void secp256k1_fe_get_bounds(secp256k1_fe* r, int m) {
371+
VERIFY_CHECK(m >= 0);
372+
VERIFY_CHECK(m <= 32);
373+
secp256k1_fe_impl_get_bounds(r, m);
374+
r->magnitude = m;
375+
r->normalized = (m == 0);
376+
secp256k1_fe_verify(r);
377+
}
378+
368379
#endif /* defined(VERIFY) */
369380

370381
#endif /* SECP256K1_FIELD_IMPL_H */

0 commit comments

Comments
 (0)