Skip to content

Commit 6ab3508

Browse files
committed
Abstract out verify logic for fe_sqr
1 parent 4c25f6e commit 6ab3508

File tree

4 files changed

+20
-22
lines changed

4 files changed

+20
-22
lines changed

src/field.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ static const secp256k1_fe secp256k1_const_beta = SECP256K1_FE_CONST(
9191
# define secp256k1_fe_mul_int secp256k1_fe_impl_mul_int
9292
# define secp256k1_fe_add secp256k1_fe_impl_add
9393
# define secp256k1_fe_mul secp256k1_fe_impl_mul
94+
# define secp256k1_fe_sqr secp256k1_fe_impl_sqr
9495
#endif /* !defined(VERIFY) */
9596

9697
/** Normalize a field element.
@@ -236,8 +237,13 @@ static void secp256k1_fe_add(secp256k1_fe *r, const secp256k1_fe *a);
236237
*/
237238
static void secp256k1_fe_mul(secp256k1_fe *r, const secp256k1_fe *a, const secp256k1_fe * SECP256K1_RESTRICT b);
238239

239-
/** Sets a field element to be the square of another. Requires the input's magnitude to be at most 8.
240-
* The output magnitude is 1 (but not guaranteed to be normalized). */
240+
/** Square a field element.
241+
*
242+
* On input, a must be a valid field element; r does not need to be initialized. The magnitude
243+
* of a must not exceed 8.
244+
* Performs {r = a**2}
245+
* On output, r will have magnitude 1, but won't be normalized.
246+
*/
241247
static void secp256k1_fe_sqr(secp256k1_fe *r, const secp256k1_fe *a);
242248

243249
/** If a has a square root, it is computed in r and 1 is returned. If a does not

src/field_10x26_impl.h

+1-10
Original file line numberDiff line numberDiff line change
@@ -1031,17 +1031,8 @@ SECP256K1_INLINE static void secp256k1_fe_impl_mul(secp256k1_fe *r, const secp25
10311031
secp256k1_fe_mul_inner(r->n, a->n, b->n);
10321032
}
10331033

1034-
static void secp256k1_fe_sqr(secp256k1_fe *r, const secp256k1_fe *a) {
1035-
#ifdef VERIFY
1036-
VERIFY_CHECK(a->magnitude <= 8);
1037-
secp256k1_fe_verify(a);
1038-
#endif
1034+
SECP256K1_INLINE static void secp256k1_fe_impl_sqr(secp256k1_fe *r, const secp256k1_fe *a) {
10391035
secp256k1_fe_sqr_inner(r->n, a->n);
1040-
#ifdef VERIFY
1041-
r->magnitude = 1;
1042-
r->normalized = 0;
1043-
secp256k1_fe_verify(r);
1044-
#endif
10451036
}
10461037

10471038
static SECP256K1_INLINE void secp256k1_fe_cmov(secp256k1_fe *r, const secp256k1_fe *a, int flag) {

src/field_5x52_impl.h

+1-10
Original file line numberDiff line numberDiff line change
@@ -365,17 +365,8 @@ SECP256K1_INLINE static void secp256k1_fe_impl_mul(secp256k1_fe *r, const secp25
365365
secp256k1_fe_mul_inner(r->n, a->n, b->n);
366366
}
367367

368-
static void secp256k1_fe_sqr(secp256k1_fe *r, const secp256k1_fe *a) {
369-
#ifdef VERIFY
370-
VERIFY_CHECK(a->magnitude <= 8);
371-
secp256k1_fe_verify(a);
372-
#endif
368+
SECP256K1_INLINE static void secp256k1_fe_impl_sqr(secp256k1_fe *r, const secp256k1_fe *a) {
373369
secp256k1_fe_sqr_inner(r->n, a->n);
374-
#ifdef VERIFY
375-
r->magnitude = 1;
376-
r->normalized = 0;
377-
secp256k1_fe_verify(r);
378-
#endif
379370
}
380371

381372
static SECP256K1_INLINE void secp256k1_fe_cmov(secp256k1_fe *r, const secp256k1_fe *a, int flag) {

src/field_impl.h

+10
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,16 @@ SECP256K1_INLINE static void secp256k1_fe_mul(secp256k1_fe *r, const secp256k1_f
300300
r->normalized = 0;
301301
secp256k1_fe_verify(r);
302302
}
303+
304+
static void secp256k1_fe_impl_sqr(secp256k1_fe *r, const secp256k1_fe *a);
305+
SECP256K1_INLINE static void secp256k1_fe_sqr(secp256k1_fe *r, const secp256k1_fe *a) {
306+
secp256k1_fe_verify(a);
307+
VERIFY_CHECK(a->magnitude <= 8);
308+
secp256k1_fe_impl_sqr(r, a);
309+
r->magnitude = 1;
310+
r->normalized = 0;
311+
secp256k1_fe_verify(r);
312+
}
303313
#endif /* defined(VERIFY) */
304314

305315
#endif /* SECP256K1_FIELD_IMPL_H */

0 commit comments

Comments
 (0)