Skip to content

Commit 89e324c

Browse files
committed
Abstract out verify logic for fe_half
1 parent 283cd80 commit 89e324c

File tree

4 files changed

+23
-31
lines changed

4 files changed

+23
-31
lines changed

src/field.h

+7-3
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ static const secp256k1_fe secp256k1_const_beta = SECP256K1_FE_CONST(
9898
# define secp256k1_fe_inv secp256k1_fe_impl_inv
9999
# define secp256k1_fe_inv_var secp256k1_fe_impl_inv_var
100100
# define secp256k1_fe_get_bounds secp256k1_fe_impl_get_bounds
101+
# define secp256k1_fe_half secp256k1_fe_impl_half
101102
#endif /* !defined(VERIFY) */
102103

103104
/** Normalize a field element.
@@ -302,9 +303,12 @@ static void secp256k1_fe_storage_cmov(secp256k1_fe_storage *r, const secp256k1_f
302303
*/
303304
static void secp256k1_fe_cmov(secp256k1_fe *r, const secp256k1_fe *a, int flag);
304305

305-
/** Halves the value of a field element modulo the field prime. Constant-time.
306-
* For an input magnitude 'm', the output magnitude is set to 'floor(m/2) + 1'.
307-
* The output is not guaranteed to be normalized, regardless of the input. */
306+
/** Halve the value of a field element modulo the field prime in constant-time.
307+
*
308+
* On input, r must be a valid field element.
309+
* On output, r will be normalized and have magnitude floor(m/2) + 1 where m is
310+
* the magnitude of r on input.
311+
*/
308312
static void secp256k1_fe_half(secp256k1_fe *r);
309313

310314
/** Sets r to a field element with magnitude m, normalized if (and only if) m==0.

src/field_10x26_impl.h

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

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

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

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

src/field_5x52_impl.h

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

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

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

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

src/field_impl.h

+10
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,16 @@ SECP256K1_INLINE static void secp256k1_fe_get_bounds(secp256k1_fe* r, int m) {
384384
secp256k1_fe_verify(r);
385385
}
386386

387+
static void secp256k1_fe_impl_half(secp256k1_fe *r);
388+
SECP256K1_INLINE static void secp256k1_fe_half(secp256k1_fe *r) {
389+
secp256k1_fe_verify(r);
390+
VERIFY_CHECK(r->magnitude < 32);
391+
secp256k1_fe_impl_half(r);
392+
r->magnitude = (r->magnitude >> 1) + 1;
393+
r->normalized = 0;
394+
secp256k1_fe_verify(r);
395+
}
396+
387397
#endif /* defined(VERIFY) */
388398

389399
#endif /* SECP256K1_FIELD_IMPL_H */

0 commit comments

Comments
 (0)