Skip to content

Commit 6c31371

Browse files
committed
Abstract out verify logic for fe_normalize_var
1 parent e28b51f commit 6c31371

File tree

4 files changed

+16
-15
lines changed

4 files changed

+16
-15
lines changed

src/field.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ static const secp256k1_fe secp256k1_const_beta = SECP256K1_FE_CONST(
7777
* function call (even though presumably inlinable). */
7878
# define secp256k1_fe_normalize secp256k1_fe_impl_normalize
7979
# define secp256k1_fe_normalize_weak secp256k1_fe_impl_normalize_weak
80+
# define secp256k1_fe_normalize_var secp256k1_fe_impl_normalize_var
8081
#endif /* !defined(VERIFY) */
8182

8283
/** Normalize a field element.
@@ -93,7 +94,10 @@ static void secp256k1_fe_normalize(secp256k1_fe *r);
9394
*/
9495
static void secp256k1_fe_normalize_weak(secp256k1_fe *r);
9596

96-
/** Normalize a field element, without constant-time guarantee. */
97+
/** Normalize a field element, without constant-time guarantee.
98+
*
99+
* Identical in behavior to secp256k1_fe_normalize, but not constant time in r.
100+
*/
97101
static void secp256k1_fe_normalize_var(secp256k1_fe *r);
98102

99103
/** Verify whether a field element represents zero i.e. would normalize to a zero value. */

src/field_10x26_impl.h

+1-7
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ static void secp256k1_fe_impl_normalize_weak(secp256k1_fe *r) {
133133
r->n[5] = t5; r->n[6] = t6; r->n[7] = t7; r->n[8] = t8; r->n[9] = t9;
134134
}
135135

136-
static void secp256k1_fe_normalize_var(secp256k1_fe *r) {
136+
static void secp256k1_fe_impl_normalize_var(secp256k1_fe *r) {
137137
uint32_t t0 = r->n[0], t1 = r->n[1], t2 = r->n[2], t3 = r->n[3], t4 = r->n[4],
138138
t5 = r->n[5], t6 = r->n[6], t7 = r->n[7], t8 = r->n[8], t9 = r->n[9];
139139

@@ -181,12 +181,6 @@ static void secp256k1_fe_normalize_var(secp256k1_fe *r) {
181181

182182
r->n[0] = t0; r->n[1] = t1; r->n[2] = t2; r->n[3] = t3; r->n[4] = t4;
183183
r->n[5] = t5; r->n[6] = t6; r->n[7] = t7; r->n[8] = t8; r->n[9] = t9;
184-
185-
#ifdef VERIFY
186-
r->magnitude = 1;
187-
r->normalized = 1;
188-
secp256k1_fe_verify(r);
189-
#endif
190184
}
191185

192186
static int secp256k1_fe_normalizes_to_zero(const secp256k1_fe *r) {

src/field_5x52_impl.h

+1-7
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ static void secp256k1_fe_impl_normalize_weak(secp256k1_fe *r) {
108108
r->n[0] = t0; r->n[1] = t1; r->n[2] = t2; r->n[3] = t3; r->n[4] = t4;
109109
}
110110

111-
static void secp256k1_fe_normalize_var(secp256k1_fe *r) {
111+
static void secp256k1_fe_impl_normalize_var(secp256k1_fe *r) {
112112
uint64_t t0 = r->n[0], t1 = r->n[1], t2 = r->n[2], t3 = r->n[3], t4 = r->n[4];
113113

114114
/* Reduce t4 at the start so there will be at most a single carry from the first pass */
@@ -144,12 +144,6 @@ static void secp256k1_fe_normalize_var(secp256k1_fe *r) {
144144
}
145145

146146
r->n[0] = t0; r->n[1] = t1; r->n[2] = t2; r->n[3] = t3; r->n[4] = t4;
147-
148-
#ifdef VERIFY
149-
r->magnitude = 1;
150-
r->normalized = 1;
151-
secp256k1_fe_verify(r);
152-
#endif
153147
}
154148

155149
static int secp256k1_fe_normalizes_to_zero(const secp256k1_fe *r) {

src/field_impl.h

+9
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,15 @@ SECP256K1_INLINE static void secp256k1_fe_normalize_weak(secp256k1_fe *r) {
164164
r->magnitude = 1;
165165
secp256k1_fe_verify(r);
166166
}
167+
168+
static void secp256k1_fe_impl_normalize_var(secp256k1_fe *r);
169+
SECP256K1_INLINE static void secp256k1_fe_normalize_var(secp256k1_fe *r) {
170+
secp256k1_fe_verify(r);
171+
secp256k1_fe_impl_normalize_var(r);
172+
r->magnitude = 1;
173+
r->normalized = 1;
174+
secp256k1_fe_verify(r);
175+
}
167176
#endif /* defined(VERIFY) */
168177

169178
#endif /* SECP256K1_FIELD_IMPL_H */

0 commit comments

Comments
 (0)