Skip to content

Commit ce4d209

Browse files
committed
Abstract out verify logic for fe_cmp_var
1 parent 7d7d43c commit ce4d209

File tree

4 files changed

+18
-15
lines changed

4 files changed

+18
-15
lines changed

src/field.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ static const secp256k1_fe secp256k1_const_beta = SECP256K1_FE_CONST(
8484
# define secp256k1_fe_clear secp256k1_fe_impl_clear
8585
# define secp256k1_fe_is_zero secp256k1_fe_impl_is_zero
8686
# define secp256k1_fe_is_odd secp256k1_fe_impl_is_odd
87+
# define secp256k1_fe_cmp_var secp256k1_fe_impl_cmp_var
8788
#endif /* !defined(VERIFY) */
8889

8990
/** Normalize a field element.
@@ -164,7 +165,12 @@ static int secp256k1_fe_equal(const secp256k1_fe *a, const secp256k1_fe *b);
164165
*/
165166
static int secp256k1_fe_equal_var(const secp256k1_fe *a, const secp256k1_fe *b);
166167

167-
/** Compare two field elements. Requires both inputs to be normalized */
168+
/** Compare the values represented by 2 field elements, without constant-time guarantee.
169+
*
170+
* On input, a and b must be valid normalized field elements.
171+
* Returns 1 if a > b, -1 if a < b, and 0 if a = b (comparisons are done as integers
172+
* in range 0..p-1).
173+
*/
168174
static int secp256k1_fe_cmp_var(const secp256k1_fe *a, const secp256k1_fe *b);
169175

170176
/** Set a field element equal to 32-byte big endian value.

src/field_10x26_impl.h

+1-7
Original file line numberDiff line numberDiff line change
@@ -285,14 +285,8 @@ SECP256K1_INLINE static void secp256k1_fe_impl_clear(secp256k1_fe *a) {
285285
}
286286
}
287287

288-
static int secp256k1_fe_cmp_var(const secp256k1_fe *a, const secp256k1_fe *b) {
288+
static int secp256k1_fe_impl_cmp_var(const secp256k1_fe *a, const secp256k1_fe *b) {
289289
int i;
290-
#ifdef VERIFY
291-
VERIFY_CHECK(a->normalized);
292-
VERIFY_CHECK(b->normalized);
293-
secp256k1_fe_verify(a);
294-
secp256k1_fe_verify(b);
295-
#endif
296290
for (i = 9; i >= 0; i--) {
297291
if (a->n[i] > b->n[i]) {
298292
return 1;

src/field_5x52_impl.h

+1-7
Original file line numberDiff line numberDiff line change
@@ -231,14 +231,8 @@ SECP256K1_INLINE static void secp256k1_fe_impl_clear(secp256k1_fe *a) {
231231
}
232232
}
233233

234-
static int secp256k1_fe_cmp_var(const secp256k1_fe *a, const secp256k1_fe *b) {
234+
static int secp256k1_fe_impl_cmp_var(const secp256k1_fe *a, const secp256k1_fe *b) {
235235
int i;
236-
#ifdef VERIFY
237-
VERIFY_CHECK(a->normalized);
238-
VERIFY_CHECK(b->normalized);
239-
secp256k1_fe_verify(a);
240-
secp256k1_fe_verify(b);
241-
#endif
242236
for (i = 4; i >= 0; i--) {
243237
if (a->n[i] > b->n[i]) {
244238
return 1;

src/field_impl.h

+9
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,15 @@ SECP256K1_INLINE static int secp256k1_fe_is_odd(const secp256k1_fe *a) {
228228
VERIFY_CHECK(a->normalized);
229229
return secp256k1_fe_impl_is_odd(a);
230230
}
231+
232+
static int secp256k1_fe_impl_cmp_var(const secp256k1_fe *a, const secp256k1_fe *b);
233+
SECP256K1_INLINE static int secp256k1_fe_cmp_var(const secp256k1_fe *a, const secp256k1_fe *b) {
234+
secp256k1_fe_verify(a);
235+
secp256k1_fe_verify(b);
236+
VERIFY_CHECK(a->normalized);
237+
VERIFY_CHECK(b->normalized);
238+
return secp256k1_fe_impl_cmp_var(a, b);
239+
}
231240
#endif /* defined(VERIFY) */
232241

233242
#endif /* SECP256K1_FIELD_IMPL_H */

0 commit comments

Comments
 (0)