Skip to content

Commit 205e2c8

Browse files
committed
group: remove unneeded normalize_weak in secp256k1_gej_eq_x_var
By requiring that the input group element's X coordinate (`a->x`) has a magnitude of <= 31, the normalize_weak call and also the field element variable `r2` are not needed anymore and hence can be dropped. This makes ECDSA verification a little faster. On my machine: Benchmark , Min(us) , Avg(us) , Max(us) [ master ] ecdsa_verify , 68.9 , 72.4 , 77.8 [ PR ] ecdsa_verify , 65.4 , 68.2 , 76.6
1 parent efa76c4 commit 205e2c8

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/group.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ static void secp256k1_gej_set_ge(secp256k1_gej *r, const secp256k1_ge *a);
100100
/** Check two group elements (jacobian) for equality in variable time. */
101101
static int secp256k1_gej_eq_var(const secp256k1_gej *a, const secp256k1_gej *b);
102102

103-
/** Compare the X coordinate of a group element (jacobian). */
103+
/** Compare the X coordinate of a group element (jacobian).
104+
* The magnitude of the group element's X coordinate must not exceed 31. */
104105
static int secp256k1_gej_eq_x_var(const secp256k1_fe *x, const secp256k1_gej *a);
105106

106107
/** Set r equal to the inverse of a (i.e., mirrored around the X axis) */

src/group_impl.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,13 +314,17 @@ static int secp256k1_gej_eq_var(const secp256k1_gej *a, const secp256k1_gej *b)
314314
}
315315

316316
static int secp256k1_gej_eq_x_var(const secp256k1_fe *x, const secp256k1_gej *a) {
317-
secp256k1_fe r, r2;
317+
secp256k1_fe r;
318+
319+
#ifdef VERIFY
318320
secp256k1_fe_verify(x);
321+
VERIFY_CHECK(a->x.magnitude <= 31);
319322
secp256k1_gej_verify(a);
320323
VERIFY_CHECK(!a->infinity);
324+
#endif
325+
321326
secp256k1_fe_sqr(&r, &a->z); secp256k1_fe_mul(&r, &r, x);
322-
r2 = a->x; secp256k1_fe_normalize_weak(&r2);
323-
return secp256k1_fe_equal_var(&r, &r2);
327+
return secp256k1_fe_equal_var(&r, &a->x);
324328
}
325329

326330
static void secp256k1_gej_neg(secp256k1_gej *r, const secp256k1_gej *a) {

0 commit comments

Comments
 (0)