Skip to content

Commit c701d9a

Browse files
committed
Abstract out verify logic for fe_clear
1 parent 19a2bfe commit c701d9a

File tree

4 files changed

+16
-11
lines changed

4 files changed

+16
-11
lines changed

src/field.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ static const secp256k1_fe secp256k1_const_beta = SECP256K1_FE_CONST(
8181
# define secp256k1_fe_normalizes_to_zero secp256k1_fe_impl_normalizes_to_zero
8282
# define secp256k1_fe_normalizes_to_zero_var secp256k1_fe_impl_normalizes_to_zero_var
8383
# define secp256k1_fe_set_int secp256k1_fe_impl_set_int
84+
# define secp256k1_fe_clear secp256k1_fe_impl_clear
8485
#endif /* !defined(VERIFY) */
8586

8687
/** Normalize a field element.
@@ -123,7 +124,11 @@ static int secp256k1_fe_normalizes_to_zero_var(const secp256k1_fe *r);
123124
*/
124125
static void secp256k1_fe_set_int(secp256k1_fe *r, int a);
125126

126-
/** Sets a field element equal to zero, initializing all fields. */
127+
/** Set a field element to 0.
128+
*
129+
* On input, a does not need to be initialized.
130+
* On output, a represents 0, is normalized and has magnitude 0.
131+
*/
127132
static void secp256k1_fe_clear(secp256k1_fe *a);
128133

129134
/** Verify whether a field element is zero. Requires the input to be normalized. */

src/field_10x26_impl.h

+1-5
Original file line numberDiff line numberDiff line change
@@ -286,12 +286,8 @@ SECP256K1_INLINE static int secp256k1_fe_is_odd(const secp256k1_fe *a) {
286286
return a->n[0] & 1;
287287
}
288288

289-
SECP256K1_INLINE static void secp256k1_fe_clear(secp256k1_fe *a) {
289+
SECP256K1_INLINE static void secp256k1_fe_impl_clear(secp256k1_fe *a) {
290290
int i;
291-
#ifdef VERIFY
292-
a->magnitude = 0;
293-
a->normalized = 1;
294-
#endif
295291
for (i=0; i<10; i++) {
296292
a->n[i] = 0;
297293
}

src/field_5x52_impl.h

+1-5
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,8 @@ SECP256K1_INLINE static int secp256k1_fe_is_odd(const secp256k1_fe *a) {
232232
return a->n[0] & 1;
233233
}
234234

235-
SECP256K1_INLINE static void secp256k1_fe_clear(secp256k1_fe *a) {
235+
SECP256K1_INLINE static void secp256k1_fe_impl_clear(secp256k1_fe *a) {
236236
int i;
237-
#ifdef VERIFY
238-
a->magnitude = 0;
239-
a->normalized = 1;
240-
#endif
241237
for (i=0; i<5; i++) {
242238
a->n[i] = 0;
243239
}

src/field_impl.h

+8
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,14 @@ SECP256K1_INLINE static void secp256k1_fe_set_int(secp256k1_fe *r, int a) {
194194
r->normalized = 1;
195195
secp256k1_fe_verify(r);
196196
}
197+
198+
static void secp256k1_fe_impl_clear(secp256k1_fe *a);
199+
SECP256K1_INLINE static void secp256k1_fe_clear(secp256k1_fe *a) {
200+
a->magnitude = 0;
201+
a->normalized = 1;
202+
secp256k1_fe_impl_clear(a);
203+
secp256k1_fe_verify(a);
204+
}
197205
#endif /* defined(VERIFY) */
198206

199207
#endif /* SECP256K1_FIELD_IMPL_H */

0 commit comments

Comments
 (0)