Skip to content

Commit b6b6f9c

Browse files
committed
Abstract out verify logic for fe_normalize
1 parent 7fa5195 commit b6b6f9c

File tree

4 files changed

+16
-16
lines changed

4 files changed

+16
-16
lines changed

src/field.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,13 @@ static const secp256k1_fe secp256k1_const_beta = SECP256K1_FE_CONST(
7575
/* In non-VERIFY mode, we #define the fe operations to be identical to their
7676
* internal field implementation, to avoid the potential overhead of a
7777
* function call (even though presumably inlinable). */
78+
# define secp256k1_fe_normalize secp256k1_fe_impl_normalize
7879
#endif /* !defined(VERIFY) */
7980

80-
/** Normalize a field element. This brings the field element to a canonical representation, reduces
81-
* its magnitude to 1, and reduces it modulo field size `p`.
81+
/** Normalize a field element.
82+
*
83+
* On input, r must be a valid field element.
84+
* On output, r represents the same value but has normalized=1 and magnitude=1.
8285
*/
8386
static void secp256k1_fe_normalize(secp256k1_fe *r);
8487

src/field_10x26_impl.h

+1-7
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static void secp256k1_fe_get_bounds(secp256k1_fe *r, int m) {
5858
#endif
5959
}
6060

61-
static void secp256k1_fe_normalize(secp256k1_fe *r) {
61+
static void secp256k1_fe_impl_normalize(secp256k1_fe *r) {
6262
uint32_t t0 = r->n[0], t1 = r->n[1], t2 = r->n[2], t3 = r->n[3], t4 = r->n[4],
6363
t5 = r->n[5], t6 = r->n[6], t7 = r->n[7], t8 = r->n[8], t9 = r->n[9];
6464

@@ -105,12 +105,6 @@ static void secp256k1_fe_normalize(secp256k1_fe *r) {
105105

106106
r->n[0] = t0; r->n[1] = t1; r->n[2] = t2; r->n[3] = t3; r->n[4] = t4;
107107
r->n[5] = t5; r->n[6] = t6; r->n[7] = t7; r->n[8] = t8; r->n[9] = t9;
108-
109-
#ifdef VERIFY
110-
r->magnitude = 1;
111-
r->normalized = 1;
112-
secp256k1_fe_verify(r);
113-
#endif
114108
}
115109

116110
static void secp256k1_fe_normalize_weak(secp256k1_fe *r) {

src/field_5x52_impl.h

+1-7
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ static void secp256k1_fe_get_bounds(secp256k1_fe *r, int m) {
5252
#endif
5353
}
5454

55-
static void secp256k1_fe_normalize(secp256k1_fe *r) {
55+
static void secp256k1_fe_impl_normalize(secp256k1_fe *r) {
5656
uint64_t t0 = r->n[0], t1 = r->n[1], t2 = r->n[2], t3 = r->n[3], t4 = r->n[4];
5757

5858
/* Reduce t4 at the start so there will be at most a single carry from the first pass */
@@ -87,12 +87,6 @@ static void secp256k1_fe_normalize(secp256k1_fe *r) {
8787
t4 &= 0x0FFFFFFFFFFFFULL;
8888

8989
r->n[0] = t0; r->n[1] = t1; r->n[2] = t2; r->n[3] = t3; r->n[4] = t4;
90-
91-
#ifdef VERIFY
92-
r->magnitude = 1;
93-
r->normalized = 1;
94-
secp256k1_fe_verify(r);
95-
#endif
9690
}
9791

9892
static void secp256k1_fe_normalize_weak(secp256k1_fe *r) {

src/field_impl.h

+9
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,15 @@ static void secp256k1_fe_verify(const secp256k1_fe *a) {
147147
/* Invoke implementation-specific checks. */
148148
secp256k1_fe_impl_verify(a);
149149
}
150+
151+
static void secp256k1_fe_impl_normalize(secp256k1_fe *r);
152+
SECP256K1_INLINE static void secp256k1_fe_normalize(secp256k1_fe *r) {
153+
secp256k1_fe_verify(r);
154+
secp256k1_fe_impl_normalize(r);
155+
r->magnitude = 1;
156+
r->normalized = 1;
157+
secp256k1_fe_verify(r);
158+
}
150159
#endif /* defined(VERIFY) */
151160

152161
#endif /* SECP256K1_FIELD_IMPL_H */

0 commit comments

Comments
 (0)