Skip to content

Commit 9cae2d8

Browse files
committed
Abstract out verify logic for fe_half
1 parent d4eedf5 commit 9cae2d8

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
@@ -101,6 +101,7 @@ static void secp256k1_fe_verify(const secp256k1_fe *a);
101101
# define secp256k1_fe_inv secp256k1_fe_impl_inv
102102
# define secp256k1_fe_inv_var secp256k1_fe_impl_inv_var
103103
# define secp256k1_fe_get_bounds secp256k1_fe_impl_get_bounds
104+
# define secp256k1_fe_half secp256k1_fe_impl_half
104105
#endif /* defined(VERIFY) */
105106

106107
/** 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
@@ -364,16 +364,11 @@ SECP256K1_INLINE static void secp256k1_fe_impl_cmov(secp256k1_fe *r, const secp2
364364
r->n[4] = (r->n[4] & mask0) | (a->n[4] & mask1);
365365
}
366366

367-
static SECP256K1_INLINE void secp256k1_fe_half(secp256k1_fe *r) {
367+
static SECP256K1_INLINE void secp256k1_fe_impl_half(secp256k1_fe *r) {
368368
uint64_t t0 = r->n[0], t1 = r->n[1], t2 = r->n[2], t3 = r->n[3], t4 = r->n[4];
369369
uint64_t one = (uint64_t)1;
370370
uint64_t mask = -(t0 & one) >> 12;
371371

372-
#ifdef VERIFY
373-
secp256k1_fe_verify(r);
374-
VERIFY_CHECK(r->magnitude < 32);
375-
#endif
376-
377372
/* Bounds analysis (over the rationals).
378373
*
379374
* Let m = r->magnitude
@@ -410,10 +405,8 @@ static SECP256K1_INLINE void secp256k1_fe_half(secp256k1_fe *r) {
410405
*
411406
* Current bounds: t0..t3 <= C * (m/2 + 1/2)
412407
* t4 <= D * (m/2 + 1/4)
413-
*/
414-
415-
#ifdef VERIFY
416-
/* Therefore the output magnitude (M) has to be set such that:
408+
*
409+
* Therefore the output magnitude (M) has to be set such that:
417410
* t0..t3: C * M >= C * (m/2 + 1/2)
418411
* t4: D * M >= D * (m/2 + 1/4)
419412
*
@@ -423,10 +416,6 @@ static SECP256K1_INLINE void secp256k1_fe_half(secp256k1_fe *r) {
423416
* and since we want the smallest such integer value for M:
424417
* M == floor(m/2) + 1
425418
*/
426-
r->magnitude = (r->magnitude >> 1) + 1;
427-
r->normalized = 0;
428-
secp256k1_fe_verify(r);
429-
#endif
430419
}
431420

432421
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
@@ -376,6 +376,16 @@ SECP256K1_INLINE static void secp256k1_fe_get_bounds(secp256k1_fe* r, int m) {
376376
secp256k1_fe_verify(r);
377377
}
378378

379+
static void secp256k1_fe_impl_half(secp256k1_fe *r);
380+
SECP256K1_INLINE static void secp256k1_fe_half(secp256k1_fe *r) {
381+
secp256k1_fe_verify(r);
382+
VERIFY_CHECK(r->magnitude < 32);
383+
secp256k1_fe_impl_half(r);
384+
r->magnitude = (r->magnitude >> 1) + 1;
385+
r->normalized = 0;
386+
secp256k1_fe_verify(r);
387+
}
388+
379389
#endif /* defined(VERIFY) */
380390

381391
#endif /* SECP256K1_FIELD_IMPL_H */

0 commit comments

Comments
 (0)