Skip to content

Commit 4371f98

Browse files
committed
Abstract out verify logic for fe_add_int
1 parent 89e324c commit 4371f98

File tree

4 files changed

+18
-19
lines changed

4 files changed

+18
-19
lines changed

src/field.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ static const secp256k1_fe secp256k1_const_beta = SECP256K1_FE_CONST(
9999
# define secp256k1_fe_inv_var secp256k1_fe_impl_inv_var
100100
# define secp256k1_fe_get_bounds secp256k1_fe_impl_get_bounds
101101
# define secp256k1_fe_half secp256k1_fe_impl_half
102+
# define secp256k1_fe_add_int secp256k1_fe_impl_add_int
102103
#endif /* !defined(VERIFY) */
103104

104105
/** Normalize a field element.
@@ -213,7 +214,11 @@ static void secp256k1_fe_get_b32(unsigned char *r, const secp256k1_fe *a);
213214
*/
214215
static void secp256k1_fe_negate(secp256k1_fe *r, const secp256k1_fe *a, int m);
215216

216-
/** Adds a small integer (up to 0x7FFF) to r. The resulting magnitude increases by one. */
217+
/** Add a small integer to a field element.
218+
*
219+
* Performs {r += a}. The magnitude of r increases by 1, and normalized is cleared.
220+
* a must be in range [0,0xFFFF].
221+
*/
217222
static void secp256k1_fe_add_int(secp256k1_fe *r, int a);
218223

219224
/** Multiply a field element with a small integer.

src/field_10x26_impl.h

+1-9
Original file line numberDiff line numberDiff line change
@@ -389,16 +389,8 @@ SECP256K1_INLINE static void secp256k1_fe_impl_add(secp256k1_fe *r, const secp25
389389
r->n[9] += a->n[9];
390390
}
391391

392-
SECP256K1_INLINE static void secp256k1_fe_add_int(secp256k1_fe *r, int a) {
393-
secp256k1_fe_verify(r);
394-
VERIFY_CHECK(a >= 0);
395-
VERIFY_CHECK(a <= 0x7FFF);
392+
SECP256K1_INLINE static void secp256k1_fe_impl_add_int(secp256k1_fe *r, int a) {
396393
r->n[0] += a;
397-
#ifdef VERIFY
398-
r->magnitude += 1;
399-
r->normalized = 0;
400-
secp256k1_fe_verify(r);
401-
#endif
402394
}
403395

404396
#if defined(USE_EXTERNAL_ASM)

src/field_5x52_impl.h

+1-9
Original file line numberDiff line numberDiff line change
@@ -334,16 +334,8 @@ SECP256K1_INLINE static void secp256k1_fe_impl_mul_int(secp256k1_fe *r, int a) {
334334
r->n[4] *= a;
335335
}
336336

337-
SECP256K1_INLINE static void secp256k1_fe_add_int(secp256k1_fe *r, int a) {
338-
secp256k1_fe_verify(r);
339-
VERIFY_CHECK(a >= 0);
340-
VERIFY_CHECK(a <= 0x7FFF);
337+
SECP256K1_INLINE static void secp256k1_fe_impl_add_int(secp256k1_fe *r, int a) {
341338
r->n[0] += a;
342-
#ifdef VERIFY
343-
r->magnitude += 1;
344-
r->normalized = 0;
345-
secp256k1_fe_verify(r);
346-
#endif
347339
}
348340

349341
SECP256K1_INLINE static void secp256k1_fe_impl_add(secp256k1_fe *r, const secp256k1_fe *a) {

src/field_impl.h

+10
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,16 @@ SECP256K1_INLINE static void secp256k1_fe_set_int(secp256k1_fe *r, int a) {
220220
secp256k1_fe_verify(r);
221221
}
222222

223+
static void secp256k1_fe_impl_add_int(secp256k1_fe *r, int a);
224+
SECP256K1_INLINE static void secp256k1_fe_add_int(secp256k1_fe *r, int a) {
225+
VERIFY_CHECK(0 <= a && a <= 0x7FFF);
226+
secp256k1_fe_verify(r);
227+
secp256k1_fe_impl_add_int(r, a);
228+
r->magnitude += 1;
229+
r->normalized = 0;
230+
secp256k1_fe_verify(r);
231+
}
232+
223233
static void secp256k1_fe_impl_clear(secp256k1_fe *a);
224234
SECP256K1_INLINE static void secp256k1_fe_clear(secp256k1_fe *a) {
225235
a->magnitude = 0;

0 commit comments

Comments
 (0)