Skip to content

Commit 3167646

Browse files
committed
Abstract out verify logic for fe_from_storage
1 parent 76d31e5 commit 3167646

File tree

4 files changed

+17
-13
lines changed

4 files changed

+17
-13
lines changed

src/field.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ static const secp256k1_fe secp256k1_const_beta = SECP256K1_FE_CONST(
9494
# define secp256k1_fe_sqr secp256k1_fe_impl_sqr
9595
# define secp256k1_fe_cmov secp256k1_fe_impl_cmov
9696
# define secp256k1_fe_to_storage secp256k1_fe_impl_to_storage
97+
# define secp256k1_fe_from_storage secp256k1_fe_impl_from_storage
9798
#endif /* !defined(VERIFY) */
9899

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

+1-6
Original file line numberDiff line numberDiff line change
@@ -1156,7 +1156,7 @@ static void secp256k1_fe_impl_to_storage(secp256k1_fe_storage *r, const secp256k
11561156
r->n[7] = a->n[8] >> 16 | a->n[9] << 10;
11571157
}
11581158

1159-
static SECP256K1_INLINE void secp256k1_fe_from_storage(secp256k1_fe *r, const secp256k1_fe_storage *a) {
1159+
static SECP256K1_INLINE void secp256k1_fe_impl_from_storage(secp256k1_fe *r, const secp256k1_fe_storage *a) {
11601160
r->n[0] = a->n[0] & 0x3FFFFFFUL;
11611161
r->n[1] = a->n[0] >> 26 | ((a->n[1] << 6) & 0x3FFFFFFUL);
11621162
r->n[2] = a->n[1] >> 20 | ((a->n[2] << 12) & 0x3FFFFFFUL);
@@ -1167,11 +1167,6 @@ static SECP256K1_INLINE void secp256k1_fe_from_storage(secp256k1_fe *r, const se
11671167
r->n[7] = a->n[5] >> 22 | ((a->n[6] << 10) & 0x3FFFFFFUL);
11681168
r->n[8] = a->n[6] >> 16 | ((a->n[7] << 16) & 0x3FFFFFFUL);
11691169
r->n[9] = a->n[7] >> 10;
1170-
#ifdef VERIFY
1171-
r->magnitude = 1;
1172-
r->normalized = 1;
1173-
secp256k1_fe_verify(r);
1174-
#endif
11751170
}
11761171

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

src/field_5x52_impl.h

+1-6
Original file line numberDiff line numberDiff line change
@@ -466,17 +466,12 @@ static void secp256k1_fe_impl_to_storage(secp256k1_fe_storage *r, const secp256k
466466
r->n[3] = a->n[3] >> 36 | a->n[4] << 16;
467467
}
468468

469-
static SECP256K1_INLINE void secp256k1_fe_from_storage(secp256k1_fe *r, const secp256k1_fe_storage *a) {
469+
static SECP256K1_INLINE void secp256k1_fe_impl_from_storage(secp256k1_fe *r, const secp256k1_fe_storage *a) {
470470
r->n[0] = a->n[0] & 0xFFFFFFFFFFFFFULL;
471471
r->n[1] = a->n[0] >> 52 | ((a->n[1] << 12) & 0xFFFFFFFFFFFFFULL);
472472
r->n[2] = a->n[1] >> 40 | ((a->n[2] << 24) & 0xFFFFFFFFFFFFFULL);
473473
r->n[3] = a->n[2] >> 28 | ((a->n[3] << 36) & 0xFFFFFFFFFFFFFULL);
474474
r->n[4] = a->n[3] >> 16;
475-
#ifdef VERIFY
476-
r->magnitude = 1;
477-
r->normalized = 1;
478-
secp256k1_fe_verify(r);
479-
#endif
480475
}
481476

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

src/field_impl.h

+8
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,14 @@ SECP256K1_INLINE static void secp256k1_fe_to_storage(secp256k1_fe_storage *r, co
343343
VERIFY_CHECK(a->normalized);
344344
secp256k1_fe_impl_to_storage(r, a);
345345
}
346+
347+
static void secp256k1_fe_impl_from_storage(secp256k1_fe *r, const secp256k1_fe_storage *a);
348+
SECP256K1_INLINE static void secp256k1_fe_from_storage(secp256k1_fe *r, const secp256k1_fe_storage *a) {
349+
secp256k1_fe_impl_from_storage(r, a);
350+
r->magnitude = 1;
351+
r->normalized = 1;
352+
secp256k1_fe_verify(r);
353+
}
346354
#endif /* defined(VERIFY) */
347355

348356
#endif /* SECP256K1_FIELD_IMPL_H */

0 commit comments

Comments
 (0)