Skip to content

Commit 19a2bfe

Browse files
committed
Abstract out verify logic for fe_set_int
1 parent 864f9db commit 19a2bfe

File tree

4 files changed

+16
-16
lines changed

4 files changed

+16
-16
lines changed

src/field.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ static const secp256k1_fe secp256k1_const_beta = SECP256K1_FE_CONST(
8080
# define secp256k1_fe_normalize_var secp256k1_fe_impl_normalize_var
8181
# define secp256k1_fe_normalizes_to_zero secp256k1_fe_impl_normalizes_to_zero
8282
# define secp256k1_fe_normalizes_to_zero_var secp256k1_fe_impl_normalizes_to_zero_var
83+
# define secp256k1_fe_set_int secp256k1_fe_impl_set_int
8384
#endif /* !defined(VERIFY) */
8485

8586
/** Normalize a field element.
@@ -115,8 +116,10 @@ static int secp256k1_fe_normalizes_to_zero(const secp256k1_fe *r);
115116
*/
116117
static int secp256k1_fe_normalizes_to_zero_var(const secp256k1_fe *r);
117118

118-
/** Set a field element equal to a small (not greater than 0x7FFF), non-negative integer.
119-
* Resulting field element is normalized; it has magnitude 0 if a == 0, and magnitude 1 otherwise.
119+
/** Set a field element to an integer in range [0,0x7FFF].
120+
*
121+
* On input, r does not need to be initialized, a must be in [0,0x7FFF].
122+
* On output, r represents value a, is normalized and has magnitude (a!=0).
120123
*/
121124
static void secp256k1_fe_set_int(secp256k1_fe *r, int a);
122125

src/field_10x26_impl.h

+1-7
Original file line numberDiff line numberDiff line change
@@ -264,15 +264,9 @@ static int secp256k1_fe_impl_normalizes_to_zero_var(const secp256k1_fe *r) {
264264
return (z0 == 0) | (z1 == 0x3FFFFFFUL);
265265
}
266266

267-
SECP256K1_INLINE static void secp256k1_fe_set_int(secp256k1_fe *r, int a) {
268-
VERIFY_CHECK(0 <= a && a <= 0x7FFF);
267+
SECP256K1_INLINE static void secp256k1_fe_impl_set_int(secp256k1_fe *r, int a) {
269268
r->n[0] = a;
270269
r->n[1] = r->n[2] = r->n[3] = r->n[4] = r->n[5] = r->n[6] = r->n[7] = r->n[8] = r->n[9] = 0;
271-
#ifdef VERIFY
272-
r->magnitude = (a != 0);
273-
r->normalized = 1;
274-
secp256k1_fe_verify(r);
275-
#endif
276270
}
277271

278272
SECP256K1_INLINE static int secp256k1_fe_is_zero(const secp256k1_fe *a) {

src/field_5x52_impl.h

+1-7
Original file line numberDiff line numberDiff line change
@@ -210,15 +210,9 @@ static int secp256k1_fe_impl_normalizes_to_zero_var(const secp256k1_fe *r) {
210210
return (z0 == 0) | (z1 == 0xFFFFFFFFFFFFFULL);
211211
}
212212

213-
SECP256K1_INLINE static void secp256k1_fe_set_int(secp256k1_fe *r, int a) {
214-
VERIFY_CHECK(0 <= a && a <= 0x7FFF);
213+
SECP256K1_INLINE static void secp256k1_fe_impl_set_int(secp256k1_fe *r, int a) {
215214
r->n[0] = a;
216215
r->n[1] = r->n[2] = r->n[3] = r->n[4] = 0;
217-
#ifdef VERIFY
218-
r->magnitude = (a != 0);
219-
r->normalized = 1;
220-
secp256k1_fe_verify(r);
221-
#endif
222216
}
223217

224218
SECP256K1_INLINE static int secp256k1_fe_is_zero(const secp256k1_fe *a) {

src/field_impl.h

+9
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,15 @@ SECP256K1_INLINE static int secp256k1_fe_normalizes_to_zero_var(const secp256k1_
185185
secp256k1_fe_verify(r);
186186
return secp256k1_fe_impl_normalizes_to_zero_var(r);
187187
}
188+
189+
static void secp256k1_fe_impl_set_int(secp256k1_fe *r, int a);
190+
SECP256K1_INLINE static void secp256k1_fe_set_int(secp256k1_fe *r, int a) {
191+
VERIFY_CHECK(0 <= a && a <= 0x7FFF);
192+
secp256k1_fe_impl_set_int(r, a);
193+
r->magnitude = (a != 0);
194+
r->normalized = 1;
195+
secp256k1_fe_verify(r);
196+
}
188197
#endif /* defined(VERIFY) */
189198

190199
#endif /* SECP256K1_FIELD_IMPL_H */

0 commit comments

Comments
 (0)