Skip to content

Commit d3f3fe8

Browse files
committed
Abstract out verify logic for fe_is_zero
1 parent c701d9a commit d3f3fe8

File tree

4 files changed

+18
-11
lines changed

4 files changed

+18
-11
lines changed

src/field.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ static const secp256k1_fe secp256k1_const_beta = SECP256K1_FE_CONST(
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
8484
# define secp256k1_fe_clear secp256k1_fe_impl_clear
85+
# define secp256k1_fe_is_zero secp256k1_fe_impl_is_zero
8586
#endif /* !defined(VERIFY) */
8687

8788
/** Normalize a field element.
@@ -131,7 +132,14 @@ static void secp256k1_fe_set_int(secp256k1_fe *r, int a);
131132
*/
132133
static void secp256k1_fe_clear(secp256k1_fe *a);
133134

134-
/** Verify whether a field element is zero. Requires the input to be normalized. */
135+
/** Determine whether a represents field element 0.
136+
*
137+
* On input, a must be a valid normalized field element.
138+
* Returns whether a = 0 (mod p).
139+
*
140+
* This behaves identical to secp256k1_normalizes_to_zero{,_var}, but requires
141+
* normalized input (and is much faster).
142+
*/
135143
static int secp256k1_fe_is_zero(const secp256k1_fe *a);
136144

137145
/** Check the "oddness" of a field element. Requires the input to be normalized. */

src/field_10x26_impl.h

+1-5
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,8 @@ SECP256K1_INLINE static void secp256k1_fe_impl_set_int(secp256k1_fe *r, int a) {
269269
r->n[1] = r->n[2] = r->n[3] = r->n[4] = r->n[5] = r->n[6] = r->n[7] = r->n[8] = r->n[9] = 0;
270270
}
271271

272-
SECP256K1_INLINE static int secp256k1_fe_is_zero(const secp256k1_fe *a) {
272+
SECP256K1_INLINE static int secp256k1_fe_impl_is_zero(const secp256k1_fe *a) {
273273
const uint32_t *t = a->n;
274-
#ifdef VERIFY
275-
VERIFY_CHECK(a->normalized);
276-
secp256k1_fe_verify(a);
277-
#endif
278274
return (t[0] | t[1] | t[2] | t[3] | t[4] | t[5] | t[6] | t[7] | t[8] | t[9]) == 0;
279275
}
280276

src/field_5x52_impl.h

+1-5
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,8 @@ SECP256K1_INLINE static void secp256k1_fe_impl_set_int(secp256k1_fe *r, int a) {
215215
r->n[1] = r->n[2] = r->n[3] = r->n[4] = 0;
216216
}
217217

218-
SECP256K1_INLINE static int secp256k1_fe_is_zero(const secp256k1_fe *a) {
218+
SECP256K1_INLINE static int secp256k1_fe_impl_is_zero(const secp256k1_fe *a) {
219219
const uint64_t *t = a->n;
220-
#ifdef VERIFY
221-
VERIFY_CHECK(a->normalized);
222-
secp256k1_fe_verify(a);
223-
#endif
224220
return (t[0] | t[1] | t[2] | t[3] | t[4]) == 0;
225221
}
226222

src/field_impl.h

+7
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,13 @@ SECP256K1_INLINE static void secp256k1_fe_clear(secp256k1_fe *a) {
202202
secp256k1_fe_impl_clear(a);
203203
secp256k1_fe_verify(a);
204204
}
205+
206+
static int secp256k1_fe_impl_is_zero(const secp256k1_fe *a);
207+
SECP256K1_INLINE static int secp256k1_fe_is_zero(const secp256k1_fe *a) {
208+
secp256k1_fe_verify(a);
209+
VERIFY_CHECK(a->normalized);
210+
return secp256k1_fe_impl_is_zero(a);
211+
}
205212
#endif /* defined(VERIFY) */
206213

207214
#endif /* SECP256K1_FIELD_IMPL_H */

0 commit comments

Comments
 (0)