Skip to content

Commit 4e9f543

Browse files
committed
Abstract out verify logic for fe_to_storage
1 parent 581f9e8 commit 4e9f543

File tree

4 files changed

+15
-9
lines changed

4 files changed

+15
-9
lines changed

src/field.h

Lines changed: 6 additions & 1 deletion
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_mul secp256k1_fe_impl_mul
9797
# define secp256k1_fe_sqr secp256k1_fe_impl_sqr
9898
# define secp256k1_fe_cmov secp256k1_fe_impl_cmov
99+
# define secp256k1_fe_to_storage secp256k1_fe_impl_to_storage
99100
#endif /* defined(VERIFY) */
100101

101102
/** 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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,10 +1033,7 @@ static SECP256K1_INLINE void secp256k1_fe_storage_cmov(secp256k1_fe_storage *r,
10331033
r->n[7] = (r->n[7] & mask0) | (a->n[7] & mask1);
10341034
}
10351035

1036-
static void secp256k1_fe_to_storage(secp256k1_fe_storage *r, const secp256k1_fe *a) {
1037-
#ifdef VERIFY
1038-
VERIFY_CHECK(a->normalized);
1039-
#endif
1036+
static void secp256k1_fe_impl_to_storage(secp256k1_fe_storage *r, const secp256k1_fe *a) {
10401037
r->n[0] = a->n[0] | a->n[1] << 26;
10411038
r->n[1] = a->n[1] >> 6 | a->n[2] << 20;
10421039
r->n[2] = a->n[2] >> 12 | a->n[3] << 14;

src/field_5x52_impl.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,7 @@ static SECP256K1_INLINE void secp256k1_fe_storage_cmov(secp256k1_fe_storage *r,
367367
r->n[3] = (r->n[3] & mask0) | (a->n[3] & mask1);
368368
}
369369

370-
static void secp256k1_fe_to_storage(secp256k1_fe_storage *r, const secp256k1_fe *a) {
371-
#ifdef VERIFY
372-
VERIFY_CHECK(a->normalized);
373-
#endif
370+
static void secp256k1_fe_impl_to_storage(secp256k1_fe_storage *r, const secp256k1_fe *a) {
374371
r->n[0] = a->n[0] | a->n[1] << 52;
375372
r->n[1] = a->n[1] >> 12 | a->n[2] << 40;
376373
r->n[2] = a->n[2] >> 24 | a->n[3] << 28;

src/field_impl.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,13 @@ SECP256K1_INLINE static void secp256k1_fe_cmov(secp256k1_fe *r, const secp256k1_
328328
}
329329
secp256k1_fe_verify(r);
330330
}
331+
332+
static void secp256k1_fe_impl_to_storage(secp256k1_fe_storage *r, const secp256k1_fe *a);
333+
SECP256K1_INLINE static void secp256k1_fe_to_storage(secp256k1_fe_storage *r, const secp256k1_fe *a) {
334+
secp256k1_fe_verify(a);
335+
VERIFY_CHECK(a->normalized);
336+
secp256k1_fe_impl_to_storage(r, a);
337+
}
331338
#endif /* defined(VERIFY) */
332339

333340
#endif /* SECP256K1_FIELD_IMPL_H */

0 commit comments

Comments
 (0)