Skip to content

Commit 7d7d43c

Browse files
committed
Improve comments/check for fe_equal{,_var}
1 parent c5e788d commit 7d7d43c

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/field.h

+10-2
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,18 @@ static int secp256k1_fe_is_zero(const secp256k1_fe *a);
150150
*/
151151
static int secp256k1_fe_is_odd(const secp256k1_fe *a);
152152

153-
/** Compare two field elements. Requires magnitude-1 inputs. */
153+
/** Determine whether two field elements are equal.
154+
*
155+
* On input, a and b must be valid field elements with magnitudes not exceeding
156+
* 1 and 31, respectively.
157+
* Returns a = b (mod p).
158+
*/
154159
static int secp256k1_fe_equal(const secp256k1_fe *a, const secp256k1_fe *b);
155160

156-
/** Same as secp256k1_fe_equal, but may be variable time. */
161+
/** Determine whether two field elements are equal, without constant-time guarantee.
162+
*
163+
* Identical in behavior to secp256k1_fe_equal, but not constant time in either a or b.
164+
*/
157165
static int secp256k1_fe_equal_var(const secp256k1_fe *a, const secp256k1_fe *b);
158166

159167
/** Compare two field elements. Requires both inputs to be normalized */

src/field_impl.h

+12
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,25 @@
2020

2121
SECP256K1_INLINE static int secp256k1_fe_equal(const secp256k1_fe *a, const secp256k1_fe *b) {
2222
secp256k1_fe na;
23+
#ifdef VERIFY
24+
secp256k1_fe_verify(a);
25+
secp256k1_fe_verify(b);
26+
VERIFY_CHECK(a->magnitude <= 1);
27+
VERIFY_CHECK(b->magnitude <= 31);
28+
#endif
2329
secp256k1_fe_negate(&na, a, 1);
2430
secp256k1_fe_add(&na, b);
2531
return secp256k1_fe_normalizes_to_zero(&na);
2632
}
2733

2834
SECP256K1_INLINE static int secp256k1_fe_equal_var(const secp256k1_fe *a, const secp256k1_fe *b) {
2935
secp256k1_fe na;
36+
#ifdef VERIFY
37+
secp256k1_fe_verify(a);
38+
secp256k1_fe_verify(b);
39+
VERIFY_CHECK(a->magnitude <= 1);
40+
VERIFY_CHECK(b->magnitude <= 31);
41+
#endif
3042
secp256k1_fe_negate(&na, a, 1);
3143
secp256k1_fe_add(&na, b);
3244
return secp256k1_fe_normalizes_to_zero_var(&na);

0 commit comments

Comments
 (0)