Skip to content

Commit 1e6894b

Browse files
committed
Abstract out verify logic for fe_cmov
1 parent be82bd8 commit 1e6894b

File tree

4 files changed

+22
-15
lines changed

4 files changed

+22
-15
lines changed

src/field.h

+7-1
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_add secp256k1_fe_impl_add
9393
# define secp256k1_fe_mul secp256k1_fe_impl_mul
9494
# define secp256k1_fe_sqr secp256k1_fe_impl_sqr
95+
# define secp256k1_fe_cmov secp256k1_fe_impl_cmov
9596
#endif /* !defined(VERIFY) */
9697

9798
/** Normalize a field element.
@@ -271,7 +272,12 @@ static void secp256k1_fe_from_storage(secp256k1_fe *r, const secp256k1_fe_storag
271272
/** If flag is true, set *r equal to *a; otherwise leave it. Constant-time. Both *r and *a must be initialized.*/
272273
static void secp256k1_fe_storage_cmov(secp256k1_fe_storage *r, const secp256k1_fe_storage *a, int flag);
273274

274-
/** If flag is true, set *r equal to *a; otherwise leave it. Constant-time. Both *r and *a must be initialized.*/
275+
/** Conditionally move a field element in constant time.
276+
*
277+
* On input, both r and a must be valid field elements. Flag must be 0 or 1.
278+
* Performs {r = flag ? a : r}.
279+
* On output, r's magnitude and normalized will equal a's in case of flag=1, unchanged otherwise.
280+
*/
275281
static void secp256k1_fe_cmov(secp256k1_fe *r, const secp256k1_fe *a, int flag);
276282

277283
/** Halves the value of a field element modulo the field prime. Constant-time.

src/field_10x26_impl.h

+1-7
Original file line numberDiff line numberDiff line change
@@ -1035,7 +1035,7 @@ SECP256K1_INLINE static void secp256k1_fe_impl_sqr(secp256k1_fe *r, const secp25
10351035
secp256k1_fe_sqr_inner(r->n, a->n);
10361036
}
10371037

1038-
static SECP256K1_INLINE void secp256k1_fe_cmov(secp256k1_fe *r, const secp256k1_fe *a, int flag) {
1038+
SECP256K1_INLINE static void secp256k1_fe_impl_cmov(secp256k1_fe *r, const secp256k1_fe *a, int flag) {
10391039
uint32_t mask0, mask1;
10401040
volatile int vflag = flag;
10411041
SECP256K1_CHECKMEM_CHECK_VERIFY(r->n, sizeof(r->n));
@@ -1051,12 +1051,6 @@ static SECP256K1_INLINE void secp256k1_fe_cmov(secp256k1_fe *r, const secp256k1_
10511051
r->n[7] = (r->n[7] & mask0) | (a->n[7] & mask1);
10521052
r->n[8] = (r->n[8] & mask0) | (a->n[8] & mask1);
10531053
r->n[9] = (r->n[9] & mask0) | (a->n[9] & mask1);
1054-
#ifdef VERIFY
1055-
if (flag) {
1056-
r->magnitude = a->magnitude;
1057-
r->normalized = a->normalized;
1058-
}
1059-
#endif
10601054
}
10611055

10621056
static SECP256K1_INLINE void secp256k1_fe_half(secp256k1_fe *r) {

src/field_5x52_impl.h

+1-7
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ SECP256K1_INLINE static void secp256k1_fe_impl_sqr(secp256k1_fe *r, const secp25
369369
secp256k1_fe_sqr_inner(r->n, a->n);
370370
}
371371

372-
static SECP256K1_INLINE void secp256k1_fe_cmov(secp256k1_fe *r, const secp256k1_fe *a, int flag) {
372+
SECP256K1_INLINE static void secp256k1_fe_impl_cmov(secp256k1_fe *r, const secp256k1_fe *a, int flag) {
373373
uint64_t mask0, mask1;
374374
volatile int vflag = flag;
375375
SECP256K1_CHECKMEM_CHECK_VERIFY(r->n, sizeof(r->n));
@@ -380,12 +380,6 @@ static SECP256K1_INLINE void secp256k1_fe_cmov(secp256k1_fe *r, const secp256k1_
380380
r->n[2] = (r->n[2] & mask0) | (a->n[2] & mask1);
381381
r->n[3] = (r->n[3] & mask0) | (a->n[3] & mask1);
382382
r->n[4] = (r->n[4] & mask0) | (a->n[4] & mask1);
383-
#ifdef VERIFY
384-
if (flag) {
385-
r->magnitude = a->magnitude;
386-
r->normalized = a->normalized;
387-
}
388-
#endif
389383
}
390384

391385
static SECP256K1_INLINE void secp256k1_fe_half(secp256k1_fe *r) {

src/field_impl.h

+13
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,19 @@ SECP256K1_INLINE static void secp256k1_fe_sqr(secp256k1_fe *r, const secp256k1_f
323323
r->normalized = 0;
324324
secp256k1_fe_verify(r);
325325
}
326+
327+
static void secp256k1_fe_impl_cmov(secp256k1_fe *r, const secp256k1_fe *a, int flag);
328+
SECP256K1_INLINE static void secp256k1_fe_cmov(secp256k1_fe *r, const secp256k1_fe *a, int flag) {
329+
VERIFY_CHECK(flag == 0 || flag == 1);
330+
secp256k1_fe_verify(a);
331+
secp256k1_fe_verify(r);
332+
secp256k1_fe_impl_cmov(r, a, flag);
333+
if (flag) {
334+
r->magnitude = a->magnitude;
335+
r->normalized = a->normalized;
336+
}
337+
secp256k1_fe_verify(r);
338+
}
326339
#endif /* defined(VERIFY) */
327340

328341
#endif /* SECP256K1_FIELD_IMPL_H */

0 commit comments

Comments
 (0)