Skip to content

Commit

Permalink
Abstract out verify logic for fe_add_int
Browse files Browse the repository at this point in the history
  • Loading branch information
sipa committed May 11, 2023
1 parent d08dde0 commit 01af80e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
7 changes: 6 additions & 1 deletion src/field.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ static const secp256k1_fe secp256k1_const_beta = SECP256K1_FE_CONST(
# define secp256k1_fe_inv_var secp256k1_fe_impl_inv_var
# define secp256k1_fe_get_bounds secp256k1_fe_impl_get_bounds
# define secp256k1_fe_half secp256k1_fe_impl_half
# define secp256k1_fe_add_int secp256k1_fe_impl_add_int
#endif /* defined(VERIFY) */

/** Normalize a field element.
Expand Down Expand Up @@ -207,7 +208,11 @@ static void secp256k1_fe_get_b32(unsigned char *r, const secp256k1_fe *a);
*/
static void secp256k1_fe_negate(secp256k1_fe *r, const secp256k1_fe *a, int m);

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

/** Multiply a field element with a small integer.
Expand Down
10 changes: 1 addition & 9 deletions src/field_10x26_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -388,16 +388,8 @@ SECP256K1_INLINE static void secp256k1_fe_impl_add(secp256k1_fe *r, const secp25
r->n[9] += a->n[9];
}

SECP256K1_INLINE static void secp256k1_fe_add_int(secp256k1_fe *r, int a) {
secp256k1_fe_verify(r);
VERIFY_CHECK(a >= 0);
VERIFY_CHECK(a <= 0x7FFF);
SECP256K1_INLINE static void secp256k1_fe_impl_add_int(secp256k1_fe *r, int a) {
r->n[0] += a;
#ifdef VERIFY
r->magnitude += 1;
r->normalized = 0;
secp256k1_fe_verify(r);
#endif
}

#if defined(USE_EXTERNAL_ASM)
Expand Down
10 changes: 1 addition & 9 deletions src/field_5x52_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -333,16 +333,8 @@ SECP256K1_INLINE static void secp256k1_fe_impl_mul_int(secp256k1_fe *r, int a) {
r->n[4] *= a;
}

SECP256K1_INLINE static void secp256k1_fe_add_int(secp256k1_fe *r, int a) {
secp256k1_fe_verify(r);
VERIFY_CHECK(a >= 0);
VERIFY_CHECK(a <= 0x7FFF);
SECP256K1_INLINE static void secp256k1_fe_impl_add_int(secp256k1_fe *r, int a) {
r->n[0] += a;
#ifdef VERIFY
r->magnitude += 1;
r->normalized = 0;
secp256k1_fe_verify(r);
#endif
}

SECP256K1_INLINE static void secp256k1_fe_impl_add(secp256k1_fe *r, const secp256k1_fe *a) {
Expand Down
10 changes: 10 additions & 0 deletions src/field_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,16 @@ SECP256K1_INLINE static void secp256k1_fe_set_int(secp256k1_fe *r, int a) {
secp256k1_fe_verify(r);
}

static void secp256k1_fe_impl_add_int(secp256k1_fe *r, int a);
SECP256K1_INLINE static void secp256k1_fe_add_int(secp256k1_fe *r, int a) {
VERIFY_CHECK(0 <= a && a <= 0x7FFF);
secp256k1_fe_verify(r);
secp256k1_fe_impl_add_int(r, a);
r->magnitude += 1;
r->normalized = 0;
secp256k1_fe_verify(r);
}

static void secp256k1_fe_impl_clear(secp256k1_fe *a);
SECP256K1_INLINE static void secp256k1_fe_clear(secp256k1_fe *a) {
a->magnitude = 0;
Expand Down

0 comments on commit 01af80e

Please sign in to comment.