Skip to content

Commit 76d31e5

Browse files
committed
Abstract out verify logic for fe_to_storage
1 parent 1e6894b commit 76d31e5

File tree

4 files changed

+15
-9
lines changed

4 files changed

+15
-9
lines changed

src/field.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ static const secp256k1_fe secp256k1_const_beta = SECP256K1_FE_CONST(
9393
# define secp256k1_fe_mul secp256k1_fe_impl_mul
9494
# define secp256k1_fe_sqr secp256k1_fe_impl_sqr
9595
# define secp256k1_fe_cmov secp256k1_fe_impl_cmov
96+
# define secp256k1_fe_to_storage secp256k1_fe_impl_to_storage
9697
#endif /* !defined(VERIFY) */
9798

9899
/** Normalize a field element.
@@ -263,7 +264,11 @@ static void secp256k1_fe_inv(secp256k1_fe *r, const secp256k1_fe *a);
263264
/** Potentially faster version of secp256k1_fe_inv, without constant-time guarantee. */
264265
static void secp256k1_fe_inv_var(secp256k1_fe *r, const secp256k1_fe *a);
265266

266-
/** Convert a field element to the storage type. */
267+
/** Convert a field element to secp256k1_fe_storage.
268+
*
269+
* On input, a must be a valid normalized field element.
270+
* Performs {r = a}.
271+
*/
267272
static void secp256k1_fe_to_storage(secp256k1_fe_storage *r, const secp256k1_fe *a);
268273

269274
/** Convert a field element back from the storage type. */

src/field_10x26_impl.h

+1-4
Original file line numberDiff line numberDiff line change
@@ -1145,10 +1145,7 @@ static SECP256K1_INLINE void secp256k1_fe_storage_cmov(secp256k1_fe_storage *r,
11451145
r->n[7] = (r->n[7] & mask0) | (a->n[7] & mask1);
11461146
}
11471147

1148-
static void secp256k1_fe_to_storage(secp256k1_fe_storage *r, const secp256k1_fe *a) {
1149-
#ifdef VERIFY
1150-
VERIFY_CHECK(a->normalized);
1151-
#endif
1148+
static void secp256k1_fe_impl_to_storage(secp256k1_fe_storage *r, const secp256k1_fe *a) {
11521149
r->n[0] = a->n[0] | a->n[1] << 26;
11531150
r->n[1] = a->n[1] >> 6 | a->n[2] << 20;
11541151
r->n[2] = a->n[2] >> 12 | a->n[3] << 14;

src/field_5x52_impl.h

+1-4
Original file line numberDiff line numberDiff line change
@@ -459,10 +459,7 @@ static SECP256K1_INLINE void secp256k1_fe_storage_cmov(secp256k1_fe_storage *r,
459459
r->n[3] = (r->n[3] & mask0) | (a->n[3] & mask1);
460460
}
461461

462-
static void secp256k1_fe_to_storage(secp256k1_fe_storage *r, const secp256k1_fe *a) {
463-
#ifdef VERIFY
464-
VERIFY_CHECK(a->normalized);
465-
#endif
462+
static void secp256k1_fe_impl_to_storage(secp256k1_fe_storage *r, const secp256k1_fe *a) {
466463
r->n[0] = a->n[0] | a->n[1] << 52;
467464
r->n[1] = a->n[1] >> 12 | a->n[2] << 40;
468465
r->n[2] = a->n[2] >> 24 | a->n[3] << 28;

src/field_impl.h

+7
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,13 @@ SECP256K1_INLINE static void secp256k1_fe_cmov(secp256k1_fe *r, const secp256k1_
336336
}
337337
secp256k1_fe_verify(r);
338338
}
339+
340+
static void secp256k1_fe_impl_to_storage(secp256k1_fe_storage *r, const secp256k1_fe *a);
341+
SECP256K1_INLINE static void secp256k1_fe_to_storage(secp256k1_fe_storage *r, const secp256k1_fe *a) {
342+
secp256k1_fe_verify(a);
343+
VERIFY_CHECK(a->normalized);
344+
secp256k1_fe_impl_to_storage(r, a);
345+
}
339346
#endif /* defined(VERIFY) */
340347

341348
#endif /* SECP256K1_FIELD_IMPL_H */

0 commit comments

Comments
 (0)