Skip to content

Commit f9e912d

Browse files
committed
Abstract out verify logic for fe_from_storage
1 parent e5e52cf commit f9e912d

File tree

4 files changed

+17
-13
lines changed

4 files changed

+17
-13
lines changed

src/field.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ static void secp256k1_fe_verify(const secp256k1_fe *a);
9797
# define secp256k1_fe_sqr secp256k1_fe_impl_sqr
9898
# define secp256k1_fe_cmov secp256k1_fe_impl_cmov
9999
# define secp256k1_fe_to_storage secp256k1_fe_impl_to_storage
100+
# define secp256k1_fe_from_storage secp256k1_fe_impl_from_storage
100101
#endif /* defined(VERIFY) */
101102

102103
/** Normalize a field element.
@@ -271,7 +272,12 @@ static void secp256k1_fe_inv_var(secp256k1_fe *r, const secp256k1_fe *a);
271272
*/
272273
static void secp256k1_fe_to_storage(secp256k1_fe_storage *r, const secp256k1_fe *a);
273274

274-
/** Convert a field element back from the storage type. */
275+
/** Convert a field element back from secp256k1_fe_storage.
276+
*
277+
* On input, r need not be initialized.
278+
* Performs {r = a}.
279+
* On output, r will be normalized and will have magnitude 1.
280+
*/
275281
static void secp256k1_fe_from_storage(secp256k1_fe *r, const secp256k1_fe_storage *a);
276282

277283
/** If flag is true, set *r equal to *a; otherwise leave it. Constant-time. Both *r and *a must be initialized.*/

src/field_10x26_impl.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,7 +1140,7 @@ static void secp256k1_fe_impl_to_storage(secp256k1_fe_storage *r, const secp256k
11401140
r->n[7] = a->n[8] >> 16 | a->n[9] << 10;
11411141
}
11421142

1143-
static SECP256K1_INLINE void secp256k1_fe_from_storage(secp256k1_fe *r, const secp256k1_fe_storage *a) {
1143+
static SECP256K1_INLINE void secp256k1_fe_impl_from_storage(secp256k1_fe *r, const secp256k1_fe_storage *a) {
11441144
r->n[0] = a->n[0] & 0x3FFFFFFUL;
11451145
r->n[1] = a->n[0] >> 26 | ((a->n[1] << 6) & 0x3FFFFFFUL);
11461146
r->n[2] = a->n[1] >> 20 | ((a->n[2] << 12) & 0x3FFFFFFUL);
@@ -1151,11 +1151,6 @@ static SECP256K1_INLINE void secp256k1_fe_from_storage(secp256k1_fe *r, const se
11511151
r->n[7] = a->n[5] >> 22 | ((a->n[6] << 10) & 0x3FFFFFFUL);
11521152
r->n[8] = a->n[6] >> 16 | ((a->n[7] << 16) & 0x3FFFFFFUL);
11531153
r->n[9] = a->n[7] >> 10;
1154-
#ifdef VERIFY
1155-
r->magnitude = 1;
1156-
r->normalized = 1;
1157-
secp256k1_fe_verify(r);
1158-
#endif
11591154
}
11601155

11611156
static void secp256k1_fe_from_signed30(secp256k1_fe *r, const secp256k1_modinv32_signed30 *a) {

src/field_5x52_impl.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -454,17 +454,12 @@ static void secp256k1_fe_impl_to_storage(secp256k1_fe_storage *r, const secp256k
454454
r->n[3] = a->n[3] >> 36 | a->n[4] << 16;
455455
}
456456

457-
static SECP256K1_INLINE void secp256k1_fe_from_storage(secp256k1_fe *r, const secp256k1_fe_storage *a) {
457+
static SECP256K1_INLINE void secp256k1_fe_impl_from_storage(secp256k1_fe *r, const secp256k1_fe_storage *a) {
458458
r->n[0] = a->n[0] & 0xFFFFFFFFFFFFFULL;
459459
r->n[1] = a->n[0] >> 52 | ((a->n[1] << 12) & 0xFFFFFFFFFFFFFULL);
460460
r->n[2] = a->n[1] >> 40 | ((a->n[2] << 24) & 0xFFFFFFFFFFFFFULL);
461461
r->n[3] = a->n[2] >> 28 | ((a->n[3] << 36) & 0xFFFFFFFFFFFFFULL);
462462
r->n[4] = a->n[3] >> 16;
463-
#ifdef VERIFY
464-
r->magnitude = 1;
465-
r->normalized = 1;
466-
secp256k1_fe_verify(r);
467-
#endif
468463
}
469464

470465
static void secp256k1_fe_from_signed62(secp256k1_fe *r, const secp256k1_modinv64_signed62 *a) {

src/field_impl.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,14 @@ SECP256K1_INLINE static void secp256k1_fe_to_storage(secp256k1_fe_storage *r, co
335335
VERIFY_CHECK(a->normalized);
336336
secp256k1_fe_impl_to_storage(r, a);
337337
}
338+
339+
static void secp256k1_fe_impl_from_storage(secp256k1_fe *r, const secp256k1_fe_storage *a);
340+
SECP256K1_INLINE static void secp256k1_fe_from_storage(secp256k1_fe *r, const secp256k1_fe_storage *a) {
341+
secp256k1_fe_impl_from_storage(r, a);
342+
r->magnitude = 1;
343+
r->normalized = 1;
344+
secp256k1_fe_verify(r);
345+
}
338346
#endif /* defined(VERIFY) */
339347

340348
#endif /* SECP256K1_FIELD_IMPL_H */

0 commit comments

Comments
 (0)