Skip to content

Commit d86adb1

Browse files
committed
Abstract out verify logic for fe_half
1 parent ee48e53 commit d86adb1

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
@@ -96,6 +96,7 @@ static void secp256k1_fe_verify(const secp256k1_fe *a);
9696
# define secp256k1_fe_inv secp256k1_fe_impl_inv
9797
# define secp256k1_fe_inv_var secp256k1_fe_impl_inv_var
9898
# define secp256k1_fe_get_bounds secp256k1_fe_impl_get_bounds
99+
# define secp256k1_fe_half secp256k1_fe_impl_half
99100
#endif /* defined(VERIFY) */
100101

101102
/** Normalize a field element.

src/field_10x26_impl.h

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

1034-
static SECP256K1_INLINE void secp256k1_fe_half(secp256k1_fe *r) {
1034+
static SECP256K1_INLINE void secp256k1_fe_impl_half(secp256k1_fe *r) {
10351035
uint32_t t0 = r->n[0], t1 = r->n[1], t2 = r->n[2], t3 = r->n[3], t4 = r->n[4],
10361036
t5 = r->n[5], t6 = r->n[6], t7 = r->n[7], t8 = r->n[8], t9 = r->n[9];
10371037
uint32_t one = (uint32_t)1;
10381038
uint32_t mask = -(t0 & one) >> 6;
10391039

1040-
#ifdef VERIFY
1041-
secp256k1_fe_verify(r);
1042-
VERIFY_CHECK(r->magnitude < 32);
1043-
#endif
1044-
10451040
/* Bounds analysis (over the rationals).
10461041
*
10471042
* Let m = r->magnitude
@@ -1088,10 +1083,8 @@ static SECP256K1_INLINE void secp256k1_fe_half(secp256k1_fe *r) {
10881083
*
10891084
* Current bounds: t0..t8 <= C * (m/2 + 1/2)
10901085
* t9 <= D * (m/2 + 1/4)
1091-
*/
1092-
1093-
#ifdef VERIFY
1094-
/* Therefore the output magnitude (M) has to be set such that:
1086+
*
1087+
* Therefore the output magnitude (M) has to be set such that:
10951088
* t0..t8: C * M >= C * (m/2 + 1/2)
10961089
* t9: D * M >= D * (m/2 + 1/4)
10971090
*
@@ -1101,10 +1094,6 @@ static SECP256K1_INLINE void secp256k1_fe_half(secp256k1_fe *r) {
11011094
* and since we want the smallest such integer value for M:
11021095
* M == floor(m/2) + 1
11031096
*/
1104-
r->magnitude = (r->magnitude >> 1) + 1;
1105-
r->normalized = 0;
1106-
secp256k1_fe_verify(r);
1107-
#endif
11081097
}
11091098

11101099
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
@@ -360,16 +360,11 @@ SECP256K1_INLINE static void secp256k1_fe_impl_cmov(secp256k1_fe *r, const secp2
360360
r->n[4] = (r->n[4] & mask0) | (a->n[4] & mask1);
361361
}
362362

363-
static SECP256K1_INLINE void secp256k1_fe_half(secp256k1_fe *r) {
363+
static SECP256K1_INLINE void secp256k1_fe_impl_half(secp256k1_fe *r) {
364364
uint64_t t0 = r->n[0], t1 = r->n[1], t2 = r->n[2], t3 = r->n[3], t4 = r->n[4];
365365
uint64_t one = (uint64_t)1;
366366
uint64_t mask = -(t0 & one) >> 12;
367367

368-
#ifdef VERIFY
369-
secp256k1_fe_verify(r);
370-
VERIFY_CHECK(r->magnitude < 32);
371-
#endif
372-
373368
/* Bounds analysis (over the rationals).
374369
*
375370
* Let m = r->magnitude
@@ -406,10 +401,8 @@ static SECP256K1_INLINE void secp256k1_fe_half(secp256k1_fe *r) {
406401
*
407402
* Current bounds: t0..t3 <= C * (m/2 + 1/2)
408403
* t4 <= D * (m/2 + 1/4)
409-
*/
410-
411-
#ifdef VERIFY
412-
/* Therefore the output magnitude (M) has to be set such that:
404+
*
405+
* Therefore the output magnitude (M) has to be set such that:
413406
* t0..t3: C * M >= C * (m/2 + 1/2)
414407
* t4: D * M >= D * (m/2 + 1/4)
415408
*
@@ -419,10 +412,6 @@ static SECP256K1_INLINE void secp256k1_fe_half(secp256k1_fe *r) {
419412
* and since we want the smallest such integer value for M:
420413
* M == floor(m/2) + 1
421414
*/
422-
r->magnitude = (r->magnitude >> 1) + 1;
423-
r->normalized = 0;
424-
secp256k1_fe_verify(r);
425-
#endif
426415
}
427416

428417
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)