Skip to content

Commit 864f9db

Browse files
committed
Abstract out verify logic for fe_normalizes_to_zero{,_var}
1 parent 6c31371 commit 864f9db

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

src/field.h

+11-3
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ static const secp256k1_fe secp256k1_const_beta = SECP256K1_FE_CONST(
7878
# define secp256k1_fe_normalize secp256k1_fe_impl_normalize
7979
# define secp256k1_fe_normalize_weak secp256k1_fe_impl_normalize_weak
8080
# define secp256k1_fe_normalize_var secp256k1_fe_impl_normalize_var
81+
# define secp256k1_fe_normalizes_to_zero secp256k1_fe_impl_normalizes_to_zero
82+
# define secp256k1_fe_normalizes_to_zero_var secp256k1_fe_impl_normalizes_to_zero_var
8183
#endif /* !defined(VERIFY) */
8284

8385
/** Normalize a field element.
@@ -100,11 +102,17 @@ static void secp256k1_fe_normalize_weak(secp256k1_fe *r);
100102
*/
101103
static void secp256k1_fe_normalize_var(secp256k1_fe *r);
102104

103-
/** Verify whether a field element represents zero i.e. would normalize to a zero value. */
105+
/** Determine whether r represents field element 0.
106+
*
107+
* On input, r must be a valid field element.
108+
* Returns whether r = 0 (mod p).
109+
*/
104110
static int secp256k1_fe_normalizes_to_zero(const secp256k1_fe *r);
105111

106-
/** Verify whether a field element represents zero i.e. would normalize to a zero value,
107-
* without constant-time guarantee. */
112+
/** Determine whether r represents field element 0, without constant-time guarantee.
113+
*
114+
* Identical in behavior to secp256k1_normalizes_to_zero, but not constant time in r.
115+
*/
108116
static int secp256k1_fe_normalizes_to_zero_var(const secp256k1_fe *r);
109117

110118
/** Set a field element equal to a small (not greater than 0x7FFF), non-negative integer.

src/field_10x26_impl.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ static void secp256k1_fe_impl_normalize_var(secp256k1_fe *r) {
183183
r->n[5] = t5; r->n[6] = t6; r->n[7] = t7; r->n[8] = t8; r->n[9] = t9;
184184
}
185185

186-
static int secp256k1_fe_normalizes_to_zero(const secp256k1_fe *r) {
186+
static int secp256k1_fe_impl_normalizes_to_zero(const secp256k1_fe *r) {
187187
uint32_t t0 = r->n[0], t1 = r->n[1], t2 = r->n[2], t3 = r->n[3], t4 = r->n[4],
188188
t5 = r->n[5], t6 = r->n[6], t7 = r->n[7], t8 = r->n[8], t9 = r->n[9];
189189

@@ -212,7 +212,7 @@ static int secp256k1_fe_normalizes_to_zero(const secp256k1_fe *r) {
212212
return (z0 == 0) | (z1 == 0x3FFFFFFUL);
213213
}
214214

215-
static int secp256k1_fe_normalizes_to_zero_var(const secp256k1_fe *r) {
215+
static int secp256k1_fe_impl_normalizes_to_zero_var(const secp256k1_fe *r) {
216216
uint32_t t0, t1, t2, t3, t4, t5, t6, t7, t8, t9;
217217
uint32_t z0, z1;
218218
uint32_t x;

src/field_5x52_impl.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ static void secp256k1_fe_impl_normalize_var(secp256k1_fe *r) {
146146
r->n[0] = t0; r->n[1] = t1; r->n[2] = t2; r->n[3] = t3; r->n[4] = t4;
147147
}
148148

149-
static int secp256k1_fe_normalizes_to_zero(const secp256k1_fe *r) {
149+
static int secp256k1_fe_impl_normalizes_to_zero(const secp256k1_fe *r) {
150150
uint64_t t0 = r->n[0], t1 = r->n[1], t2 = r->n[2], t3 = r->n[3], t4 = r->n[4];
151151

152152
/* z0 tracks a possible raw value of 0, z1 tracks a possible raw value of P */
@@ -169,7 +169,7 @@ static int secp256k1_fe_normalizes_to_zero(const secp256k1_fe *r) {
169169
return (z0 == 0) | (z1 == 0xFFFFFFFFFFFFFULL);
170170
}
171171

172-
static int secp256k1_fe_normalizes_to_zero_var(const secp256k1_fe *r) {
172+
static int secp256k1_fe_impl_normalizes_to_zero_var(const secp256k1_fe *r) {
173173
uint64_t t0, t1, t2, t3, t4;
174174
uint64_t z0, z1;
175175
uint64_t x;

src/field_impl.h

+12
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,18 @@ SECP256K1_INLINE static void secp256k1_fe_normalize_var(secp256k1_fe *r) {
173173
r->normalized = 1;
174174
secp256k1_fe_verify(r);
175175
}
176+
177+
static int secp256k1_fe_impl_normalizes_to_zero(const secp256k1_fe *r);
178+
SECP256K1_INLINE static int secp256k1_fe_normalizes_to_zero(const secp256k1_fe *r) {
179+
secp256k1_fe_verify(r);
180+
return secp256k1_fe_impl_normalizes_to_zero(r);
181+
}
182+
183+
static int secp256k1_fe_impl_normalizes_to_zero_var(const secp256k1_fe *r);
184+
SECP256K1_INLINE static int secp256k1_fe_normalizes_to_zero_var(const secp256k1_fe *r) {
185+
secp256k1_fe_verify(r);
186+
return secp256k1_fe_impl_normalizes_to_zero_var(r);
187+
}
176188
#endif /* defined(VERIFY) */
177189

178190
#endif /* SECP256K1_FIELD_IMPL_H */

0 commit comments

Comments
 (0)