Skip to content

Commit e28b51f

Browse files
committed
Abstract out verify logic for fe_normalize_weak
1 parent b6b6f9c commit e28b51f

File tree

4 files changed

+16
-13
lines changed

4 files changed

+16
-13
lines changed

src/field.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ static const secp256k1_fe secp256k1_const_beta = SECP256K1_FE_CONST(
7676
* internal field implementation, to avoid the potential overhead of a
7777
* function call (even though presumably inlinable). */
7878
# define secp256k1_fe_normalize secp256k1_fe_impl_normalize
79+
# define secp256k1_fe_normalize_weak secp256k1_fe_impl_normalize_weak
7980
#endif /* !defined(VERIFY) */
8081

8182
/** Normalize a field element.
@@ -85,7 +86,11 @@ static const secp256k1_fe secp256k1_const_beta = SECP256K1_FE_CONST(
8586
*/
8687
static void secp256k1_fe_normalize(secp256k1_fe *r);
8788

88-
/** Weakly normalize a field element: reduce its magnitude to 1, but don't fully normalize. */
89+
/** Give a field element magnitude 1.
90+
*
91+
* On input, r must be a valid field element.
92+
* On output, r represents the same value but has magnitude=1. Normalized is unchanged.
93+
*/
8994
static void secp256k1_fe_normalize_weak(secp256k1_fe *r);
9095

9196
/** Normalize a field element, without constant-time guarantee. */

src/field_10x26_impl.h

+1-6
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ static void secp256k1_fe_impl_normalize(secp256k1_fe *r) {
107107
r->n[5] = t5; r->n[6] = t6; r->n[7] = t7; r->n[8] = t8; r->n[9] = t9;
108108
}
109109

110-
static void secp256k1_fe_normalize_weak(secp256k1_fe *r) {
110+
static void secp256k1_fe_impl_normalize_weak(secp256k1_fe *r) {
111111
uint32_t t0 = r->n[0], t1 = r->n[1], t2 = r->n[2], t3 = r->n[3], t4 = r->n[4],
112112
t5 = r->n[5], t6 = r->n[6], t7 = r->n[7], t8 = r->n[8], t9 = r->n[9];
113113

@@ -131,11 +131,6 @@ static void secp256k1_fe_normalize_weak(secp256k1_fe *r) {
131131

132132
r->n[0] = t0; r->n[1] = t1; r->n[2] = t2; r->n[3] = t3; r->n[4] = t4;
133133
r->n[5] = t5; r->n[6] = t6; r->n[7] = t7; r->n[8] = t8; r->n[9] = t9;
134-
135-
#ifdef VERIFY
136-
r->magnitude = 1;
137-
secp256k1_fe_verify(r);
138-
#endif
139134
}
140135

141136
static void secp256k1_fe_normalize_var(secp256k1_fe *r) {

src/field_5x52_impl.h

+1-6
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ static void secp256k1_fe_impl_normalize(secp256k1_fe *r) {
8989
r->n[0] = t0; r->n[1] = t1; r->n[2] = t2; r->n[3] = t3; r->n[4] = t4;
9090
}
9191

92-
static void secp256k1_fe_normalize_weak(secp256k1_fe *r) {
92+
static void secp256k1_fe_impl_normalize_weak(secp256k1_fe *r) {
9393
uint64_t t0 = r->n[0], t1 = r->n[1], t2 = r->n[2], t3 = r->n[3], t4 = r->n[4];
9494

9595
/* Reduce t4 at the start so there will be at most a single carry from the first pass */
@@ -106,11 +106,6 @@ static void secp256k1_fe_normalize_weak(secp256k1_fe *r) {
106106
VERIFY_CHECK(t4 >> 49 == 0);
107107

108108
r->n[0] = t0; r->n[1] = t1; r->n[2] = t2; r->n[3] = t3; r->n[4] = t4;
109-
110-
#ifdef VERIFY
111-
r->magnitude = 1;
112-
secp256k1_fe_verify(r);
113-
#endif
114109
}
115110

116111
static void secp256k1_fe_normalize_var(secp256k1_fe *r) {

src/field_impl.h

+8
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,14 @@ SECP256K1_INLINE static void secp256k1_fe_normalize(secp256k1_fe *r) {
156156
r->normalized = 1;
157157
secp256k1_fe_verify(r);
158158
}
159+
160+
static void secp256k1_fe_impl_normalize_weak(secp256k1_fe *r);
161+
SECP256K1_INLINE static void secp256k1_fe_normalize_weak(secp256k1_fe *r) {
162+
secp256k1_fe_verify(r);
163+
secp256k1_fe_impl_normalize_weak(r);
164+
r->magnitude = 1;
165+
secp256k1_fe_verify(r);
166+
}
159167
#endif /* defined(VERIFY) */
160168

161169
#endif /* SECP256K1_FIELD_IMPL_H */

0 commit comments

Comments
 (0)