From 144670893eccd84d638951f6c5bae43fc97e3c7b Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Fri, 28 Jan 2022 18:23:54 -0500 Subject: [PATCH] Abstract out verify logic for fe_get_b32 --- src/field.h | 6 +++++- src/field_10x26_impl.h | 6 +----- src/field_5x52_impl.h | 6 +----- src/field_impl.h | 7 +++++++ 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/field.h b/src/field.h index b1c434eaf6b7f..327e63fc96fd8 100644 --- a/src/field.h +++ b/src/field.h @@ -86,6 +86,7 @@ static const secp256k1_fe secp256k1_const_beta = SECP256K1_FE_CONST( # define secp256k1_fe_is_odd secp256k1_fe_impl_is_odd # define secp256k1_fe_cmp_var secp256k1_fe_impl_cmp_var # define secp256k1_fe_set_b32 secp256k1_fe_impl_set_b32 +# define secp256k1_fe_get_b32 secp256k1_fe_impl_get_b32 #endif /* !defined(VERIFY) */ /** Normalize a field element. @@ -185,7 +186,10 @@ static int secp256k1_fe_cmp_var(const secp256k1_fe *a, const secp256k1_fe *b); */ static int secp256k1_fe_set_b32(secp256k1_fe *r, const unsigned char *a); -/** Convert a field element to a 32-byte big endian value. Requires the input to be normalized */ +/** Convert a field element to 32-byte big endian byte array. + * On input, a must be a valid normalized field element, and r a pointer to a 32-byte array. + * On output, r = a (mod p). + */ static void secp256k1_fe_get_b32(unsigned char *r, const secp256k1_fe *a); /** Set a field element equal to the additive inverse of another. Takes a maximum magnitude of the input diff --git a/src/field_10x26_impl.h b/src/field_10x26_impl.h index 5e0b65d652278..c3f49c86eb0dc 100644 --- a/src/field_10x26_impl.h +++ b/src/field_10x26_impl.h @@ -314,11 +314,7 @@ static int secp256k1_fe_impl_set_b32(secp256k1_fe *r, const unsigned char *a) { } /** Convert a field element to a 32-byte big endian value. Requires the input to be normalized */ -static void secp256k1_fe_get_b32(unsigned char *r, const secp256k1_fe *a) { -#ifdef VERIFY - VERIFY_CHECK(a->normalized); - secp256k1_fe_verify(a); -#endif +static void secp256k1_fe_impl_get_b32(unsigned char *r, const secp256k1_fe *a) { r[0] = (a->n[9] >> 14) & 0xff; r[1] = (a->n[9] >> 6) & 0xff; r[2] = ((a->n[9] & 0x3F) << 2) | ((a->n[8] >> 24) & 0x3); diff --git a/src/field_5x52_impl.h b/src/field_5x52_impl.h index b06b485b7fc86..0994087c56af6 100644 --- a/src/field_5x52_impl.h +++ b/src/field_5x52_impl.h @@ -283,11 +283,7 @@ static int secp256k1_fe_impl_set_b32(secp256k1_fe *r, const unsigned char *a) { } /** Convert a field element to a 32-byte big endian value. Requires the input to be normalized */ -static void secp256k1_fe_get_b32(unsigned char *r, const secp256k1_fe *a) { -#ifdef VERIFY - VERIFY_CHECK(a->normalized); - secp256k1_fe_verify(a); -#endif +static void secp256k1_fe_impl_get_b32(unsigned char *r, const secp256k1_fe *a) { r[0] = (a->n[4] >> 40) & 0xFF; r[1] = (a->n[4] >> 32) & 0xFF; r[2] = (a->n[4] >> 24) & 0xFF; diff --git a/src/field_impl.h b/src/field_impl.h index 304c428cb7490..a09c7a6be15fc 100644 --- a/src/field_impl.h +++ b/src/field_impl.h @@ -246,6 +246,13 @@ SECP256K1_INLINE static int secp256k1_fe_set_b32(secp256k1_fe *r, const unsigned secp256k1_fe_verify(r); return ret; } + +static void secp256k1_fe_impl_get_b32(unsigned char *r, const secp256k1_fe *a); +SECP256K1_INLINE static void secp256k1_fe_get_b32(unsigned char *r, const secp256k1_fe *a) { + secp256k1_fe_verify(a); + VERIFY_CHECK(a->normalized); + secp256k1_fe_impl_get_b32(r, a); +} #endif /* defined(VERIFY) */ #endif /* SECP256K1_FIELD_IMPL_H */