From 4371f98346b0a50c0a77e93948fe5e21d9346d06 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Thu, 11 May 2023 03:05:35 -0400 Subject: [PATCH] Abstract out verify logic for fe_add_int --- src/field.h | 7 ++++++- src/field_10x26_impl.h | 10 +--------- src/field_5x52_impl.h | 10 +--------- src/field_impl.h | 10 ++++++++++ 4 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/field.h b/src/field.h index c20638e45e..a515f38095 100644 --- a/src/field.h +++ b/src/field.h @@ -99,6 +99,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. @@ -213,7 +214,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. diff --git a/src/field_10x26_impl.h b/src/field_10x26_impl.h index ec53165e88..80f32aa460 100644 --- a/src/field_10x26_impl.h +++ b/src/field_10x26_impl.h @@ -389,16 +389,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) diff --git a/src/field_5x52_impl.h b/src/field_5x52_impl.h index 06a2e379ab..a9a436dec0 100644 --- a/src/field_5x52_impl.h +++ b/src/field_5x52_impl.h @@ -334,16 +334,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) { diff --git a/src/field_impl.h b/src/field_impl.h index 8dbc765f73..0d46b313d5 100644 --- a/src/field_impl.h +++ b/src/field_impl.h @@ -220,6 +220,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;