Skip to content

Commit 8531daf

Browse files
committed
Abstract out verify logic for fe_half
1 parent 8609d9d commit 8531daf

File tree

4 files changed

+17
-28
lines changed

4 files changed

+17
-28
lines changed

src/field.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ static const secp256k1_fe secp256k1_const_beta = SECP256K1_FE_CONST(
9292
# define secp256k1_fe_inv secp256k1_fe_impl_inv
9393
# define secp256k1_fe_inv_var secp256k1_fe_impl_inv_var
9494
# define secp256k1_fe_get_bounds secp256k1_fe_impl_get_bounds
95+
# define secp256k1_fe_half secp256k1_fe_impl_half
9596
#endif /* defined(VERIFY) */
9697

9798
/** Normalize a field element.

src/field_10x26_impl.h

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,17 +1045,12 @@ SECP256K1_INLINE static void secp256k1_fe_impl_cmov(secp256k1_fe *r, const secp2
10451045
r->n[9] = (r->n[9] & mask0) | (a->n[9] & mask1);
10461046
}
10471047

1048-
static SECP256K1_INLINE void secp256k1_fe_half(secp256k1_fe *r) {
1048+
static SECP256K1_INLINE void secp256k1_fe_impl_half(secp256k1_fe *r) {
10491049
uint32_t t0 = r->n[0], t1 = r->n[1], t2 = r->n[2], t3 = r->n[3], t4 = r->n[4],
10501050
t5 = r->n[5], t6 = r->n[6], t7 = r->n[7], t8 = r->n[8], t9 = r->n[9];
10511051
uint32_t one = (uint32_t)1;
10521052
uint32_t mask = -(t0 & one) >> 6;
10531053

1054-
#ifdef VERIFY
1055-
secp256k1_fe_verify(r);
1056-
VERIFY_CHECK(r->magnitude < 32);
1057-
#endif
1058-
10591054
/* Bounds analysis (over the rationals).
10601055
*
10611056
* Let m = r->magnitude
@@ -1102,10 +1097,8 @@ static SECP256K1_INLINE void secp256k1_fe_half(secp256k1_fe *r) {
11021097
*
11031098
* Current bounds: t0..t8 <= C * (m/2 + 1/2)
11041099
* t9 <= D * (m/2 + 1/4)
1105-
*/
1106-
1107-
#ifdef VERIFY
1108-
/* Therefore the output magnitude (M) has to be set such that:
1100+
*
1101+
* Therefore the output magnitude (M) has to be set such that:
11091102
* t0..t8: C * M >= C * (m/2 + 1/2)
11101103
* t9: D * M >= D * (m/2 + 1/4)
11111104
*
@@ -1115,10 +1108,6 @@ static SECP256K1_INLINE void secp256k1_fe_half(secp256k1_fe *r) {
11151108
* and since we want the smallest such integer value for M:
11161109
* M == floor(m/2) + 1
11171110
*/
1118-
r->magnitude = (r->magnitude >> 1) + 1;
1119-
r->normalized = 0;
1120-
secp256k1_fe_verify(r);
1121-
#endif
11221111
}
11231112

11241113
static SECP256K1_INLINE void secp256k1_fe_storage_cmov(secp256k1_fe_storage *r, const secp256k1_fe_storage *a, int flag) {

src/field_5x52_impl.h

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -374,16 +374,11 @@ SECP256K1_INLINE static void secp256k1_fe_impl_cmov(secp256k1_fe *r, const secp2
374374
r->n[4] = (r->n[4] & mask0) | (a->n[4] & mask1);
375375
}
376376

377-
static SECP256K1_INLINE void secp256k1_fe_half(secp256k1_fe *r) {
377+
static SECP256K1_INLINE void secp256k1_fe_impl_half(secp256k1_fe *r) {
378378
uint64_t t0 = r->n[0], t1 = r->n[1], t2 = r->n[2], t3 = r->n[3], t4 = r->n[4];
379379
uint64_t one = (uint64_t)1;
380380
uint64_t mask = -(t0 & one) >> 12;
381381

382-
#ifdef VERIFY
383-
secp256k1_fe_verify(r);
384-
VERIFY_CHECK(r->magnitude < 32);
385-
#endif
386-
387382
/* Bounds analysis (over the rationals).
388383
*
389384
* Let m = r->magnitude
@@ -420,10 +415,8 @@ static SECP256K1_INLINE void secp256k1_fe_half(secp256k1_fe *r) {
420415
*
421416
* Current bounds: t0..t3 <= C * (m/2 + 1/2)
422417
* t4 <= D * (m/2 + 1/4)
423-
*/
424-
425-
#ifdef VERIFY
426-
/* Therefore the output magnitude (M) has to be set such that:
418+
*
419+
* Therefore the output magnitude (M) has to be set such that:
427420
* t0..t3: C * M >= C * (m/2 + 1/2)
428421
* t4: D * M >= D * (m/2 + 1/4)
429422
*
@@ -433,10 +426,6 @@ static SECP256K1_INLINE void secp256k1_fe_half(secp256k1_fe *r) {
433426
* and since we want the smallest such integer value for M:
434427
* M == floor(m/2) + 1
435428
*/
436-
r->magnitude = (r->magnitude >> 1) + 1;
437-
r->normalized = 0;
438-
secp256k1_fe_verify(r);
439-
#endif
440429
}
441430

442431
static SECP256K1_INLINE void secp256k1_fe_storage_cmov(secp256k1_fe_storage *r, const secp256k1_fe_storage *a, int flag) {

src/field_impl.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,16 @@ SECP256K1_INLINE static void secp256k1_fe_get_bounds(secp256k1_fe* r, int m) {
372372
secp256k1_fe_verify(r);
373373
}
374374

375+
static void secp256k1_fe_impl_half(secp256k1_fe *r);
376+
SECP256K1_INLINE static void secp256k1_fe_half(secp256k1_fe *r) {
377+
secp256k1_fe_verify(r);
378+
VERIFY_CHECK(r->magnitude < 32);
379+
secp256k1_fe_impl_half(r);
380+
r->magnitude = (r->magnitude >> 1) + 1;
381+
r->normalized = 0;
382+
secp256k1_fe_verify(r);
383+
}
384+
375385
#endif /* defined(VERIFY) */
376386

377387
#endif /* SECP256K1_FIELD_IMPL_H */

0 commit comments

Comments
 (0)