Skip to content

Commit c5e788d

Browse files
committed
Abstract out verify logic for fe_is_odd
1 parent d3f3fe8 commit c5e788d

File tree

4 files changed

+15
-11
lines changed

4 files changed

+15
-11
lines changed

src/field.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ static const secp256k1_fe secp256k1_const_beta = SECP256K1_FE_CONST(
8383
# define secp256k1_fe_set_int secp256k1_fe_impl_set_int
8484
# define secp256k1_fe_clear secp256k1_fe_impl_clear
8585
# define secp256k1_fe_is_zero secp256k1_fe_impl_is_zero
86+
# define secp256k1_fe_is_odd secp256k1_fe_impl_is_odd
8687
#endif /* !defined(VERIFY) */
8788

8889
/** Normalize a field element.
@@ -142,7 +143,11 @@ static void secp256k1_fe_clear(secp256k1_fe *a);
142143
*/
143144
static int secp256k1_fe_is_zero(const secp256k1_fe *a);
144145

145-
/** Check the "oddness" of a field element. Requires the input to be normalized. */
146+
/** Determine whether a (mod p) is odd.
147+
*
148+
* On input, a must be a valid normalized field element.
149+
* Returns (int(a) mod p) & 1.
150+
*/
146151
static int secp256k1_fe_is_odd(const secp256k1_fe *a);
147152

148153
/** Compare two field elements. Requires magnitude-1 inputs. */

src/field_10x26_impl.h

+1-5
Original file line numberDiff line numberDiff line change
@@ -274,11 +274,7 @@ SECP256K1_INLINE static int secp256k1_fe_impl_is_zero(const secp256k1_fe *a) {
274274
return (t[0] | t[1] | t[2] | t[3] | t[4] | t[5] | t[6] | t[7] | t[8] | t[9]) == 0;
275275
}
276276

277-
SECP256K1_INLINE static int secp256k1_fe_is_odd(const secp256k1_fe *a) {
278-
#ifdef VERIFY
279-
VERIFY_CHECK(a->normalized);
280-
secp256k1_fe_verify(a);
281-
#endif
277+
SECP256K1_INLINE static int secp256k1_fe_impl_is_odd(const secp256k1_fe *a) {
282278
return a->n[0] & 1;
283279
}
284280

src/field_5x52_impl.h

+1-5
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,7 @@ SECP256K1_INLINE static int secp256k1_fe_impl_is_zero(const secp256k1_fe *a) {
220220
return (t[0] | t[1] | t[2] | t[3] | t[4]) == 0;
221221
}
222222

223-
SECP256K1_INLINE static int secp256k1_fe_is_odd(const secp256k1_fe *a) {
224-
#ifdef VERIFY
225-
VERIFY_CHECK(a->normalized);
226-
secp256k1_fe_verify(a);
227-
#endif
223+
SECP256K1_INLINE static int secp256k1_fe_impl_is_odd(const secp256k1_fe *a) {
228224
return a->n[0] & 1;
229225
}
230226

src/field_impl.h

+7
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,13 @@ SECP256K1_INLINE static int secp256k1_fe_is_zero(const secp256k1_fe *a) {
209209
VERIFY_CHECK(a->normalized);
210210
return secp256k1_fe_impl_is_zero(a);
211211
}
212+
213+
static int secp256k1_fe_impl_is_odd(const secp256k1_fe *a);
214+
SECP256K1_INLINE static int secp256k1_fe_is_odd(const secp256k1_fe *a) {
215+
secp256k1_fe_verify(a);
216+
VERIFY_CHECK(a->normalized);
217+
return secp256k1_fe_impl_is_odd(a);
218+
}
212219
#endif /* defined(VERIFY) */
213220

214221
#endif /* SECP256K1_FIELD_IMPL_H */

0 commit comments

Comments
 (0)