Skip to content

Commit 04c4200

Browse files
committed
Abstract out verify logic for fe_from_storage
1 parent e3a3324 commit 04c4200

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
@@ -78,6 +78,7 @@ static void secp256k1_fe_verify(const secp256k1_fe *a);
7878
# define secp256k1_fe_sqr secp256k1_fe_impl_sqr
7979
# define secp256k1_fe_cmov secp256k1_fe_impl_cmov
8080
# define secp256k1_fe_to_storage secp256k1_fe_impl_to_storage
81+
# define secp256k1_fe_from_storage secp256k1_fe_impl_from_storage
8182
#endif /* defined(VERIFY) */
8283

8384
/** Normalize a field element.
@@ -253,7 +254,12 @@ static void secp256k1_fe_inv_var(secp256k1_fe *r, const secp256k1_fe *a);
253254
*/
254255
static void secp256k1_fe_to_storage(secp256k1_fe_storage *r, const secp256k1_fe *a);
255256

256-
/** Convert a field element back from the storage type. */
257+
/** Convert a field element back from secp256k1_fe_storage.
258+
*
259+
* On input, r need not be initialized.
260+
* Performs {r = a}.
261+
* On output, r will be normalized and will have magnitude 1.
262+
*/
257263
static void secp256k1_fe_from_storage(secp256k1_fe *r, const secp256k1_fe_storage *a);
258264

259265
/** 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
@@ -1037,7 +1037,7 @@ static void secp256k1_fe_impl_to_storage(secp256k1_fe_storage *r, const secp256k
10371037
r->n[7] = a->n[8] >> 16 | a->n[9] << 10;
10381038
}
10391039

1040-
static SECP256K1_INLINE void secp256k1_fe_from_storage(secp256k1_fe *r, const secp256k1_fe_storage *a) {
1040+
static SECP256K1_INLINE void secp256k1_fe_impl_from_storage(secp256k1_fe *r, const secp256k1_fe_storage *a) {
10411041
r->n[0] = a->n[0] & 0x3FFFFFFUL;
10421042
r->n[1] = a->n[0] >> 26 | ((a->n[1] << 6) & 0x3FFFFFFUL);
10431043
r->n[2] = a->n[1] >> 20 | ((a->n[2] << 12) & 0x3FFFFFFUL);
@@ -1048,11 +1048,6 @@ static SECP256K1_INLINE void secp256k1_fe_from_storage(secp256k1_fe *r, const se
10481048
r->n[7] = a->n[5] >> 22 | ((a->n[6] << 10) & 0x3FFFFFFUL);
10491049
r->n[8] = a->n[6] >> 16 | ((a->n[7] << 16) & 0x3FFFFFFUL);
10501050
r->n[9] = a->n[7] >> 10;
1051-
#ifdef VERIFY
1052-
r->magnitude = 1;
1053-
r->normalized = 1;
1054-
secp256k1_fe_verify(r);
1055-
#endif
10561051
}
10571052

10581053
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
@@ -368,17 +368,12 @@ static void secp256k1_fe_impl_to_storage(secp256k1_fe_storage *r, const secp256k
368368
r->n[3] = a->n[3] >> 36 | a->n[4] << 16;
369369
}
370370

371-
static SECP256K1_INLINE void secp256k1_fe_from_storage(secp256k1_fe *r, const secp256k1_fe_storage *a) {
371+
static SECP256K1_INLINE void secp256k1_fe_impl_from_storage(secp256k1_fe *r, const secp256k1_fe_storage *a) {
372372
r->n[0] = a->n[0] & 0xFFFFFFFFFFFFFULL;
373373
r->n[1] = a->n[0] >> 52 | ((a->n[1] << 12) & 0xFFFFFFFFFFFFFULL);
374374
r->n[2] = a->n[1] >> 40 | ((a->n[2] << 24) & 0xFFFFFFFFFFFFFULL);
375375
r->n[3] = a->n[2] >> 28 | ((a->n[3] << 36) & 0xFFFFFFFFFFFFFULL);
376376
r->n[4] = a->n[3] >> 16;
377-
#ifdef VERIFY
378-
r->magnitude = 1;
379-
r->normalized = 1;
380-
secp256k1_fe_verify(r);
381-
#endif
382377
}
383378

384379
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)