Skip to content

Commit 1446708

Browse files
committed
Abstract out verify logic for fe_get_b32
1 parent f7a7666 commit 1446708

File tree

4 files changed

+14
-11
lines changed

4 files changed

+14
-11
lines changed

src/field.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ static const secp256k1_fe secp256k1_const_beta = SECP256K1_FE_CONST(
8686
# define secp256k1_fe_is_odd secp256k1_fe_impl_is_odd
8787
# define secp256k1_fe_cmp_var secp256k1_fe_impl_cmp_var
8888
# define secp256k1_fe_set_b32 secp256k1_fe_impl_set_b32
89+
# define secp256k1_fe_get_b32 secp256k1_fe_impl_get_b32
8990
#endif /* !defined(VERIFY) */
9091

9192
/** Normalize a field element.
@@ -185,7 +186,10 @@ static int secp256k1_fe_cmp_var(const secp256k1_fe *a, const secp256k1_fe *b);
185186
*/
186187
static int secp256k1_fe_set_b32(secp256k1_fe *r, const unsigned char *a);
187188

188-
/** Convert a field element to a 32-byte big endian value. Requires the input to be normalized */
189+
/** Convert a field element to 32-byte big endian byte array.
190+
* On input, a must be a valid normalized field element, and r a pointer to a 32-byte array.
191+
* On output, r = a (mod p).
192+
*/
189193
static void secp256k1_fe_get_b32(unsigned char *r, const secp256k1_fe *a);
190194

191195
/** Set a field element equal to the additive inverse of another. Takes a maximum magnitude of the input

src/field_10x26_impl.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -314,11 +314,7 @@ static int secp256k1_fe_impl_set_b32(secp256k1_fe *r, const unsigned char *a) {
314314
}
315315

316316
/** Convert a field element to a 32-byte big endian value. Requires the input to be normalized */
317-
static void secp256k1_fe_get_b32(unsigned char *r, const secp256k1_fe *a) {
318-
#ifdef VERIFY
319-
VERIFY_CHECK(a->normalized);
320-
secp256k1_fe_verify(a);
321-
#endif
317+
static void secp256k1_fe_impl_get_b32(unsigned char *r, const secp256k1_fe *a) {
322318
r[0] = (a->n[9] >> 14) & 0xff;
323319
r[1] = (a->n[9] >> 6) & 0xff;
324320
r[2] = ((a->n[9] & 0x3F) << 2) | ((a->n[8] >> 24) & 0x3);

src/field_5x52_impl.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,7 @@ static int secp256k1_fe_impl_set_b32(secp256k1_fe *r, const unsigned char *a) {
283283
}
284284

285285
/** Convert a field element to a 32-byte big endian value. Requires the input to be normalized */
286-
static void secp256k1_fe_get_b32(unsigned char *r, const secp256k1_fe *a) {
287-
#ifdef VERIFY
288-
VERIFY_CHECK(a->normalized);
289-
secp256k1_fe_verify(a);
290-
#endif
286+
static void secp256k1_fe_impl_get_b32(unsigned char *r, const secp256k1_fe *a) {
291287
r[0] = (a->n[4] >> 40) & 0xFF;
292288
r[1] = (a->n[4] >> 32) & 0xFF;
293289
r[2] = (a->n[4] >> 24) & 0xFF;

src/field_impl.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,13 @@ SECP256K1_INLINE static int secp256k1_fe_set_b32(secp256k1_fe *r, const unsigned
246246
secp256k1_fe_verify(r);
247247
return ret;
248248
}
249+
250+
static void secp256k1_fe_impl_get_b32(unsigned char *r, const secp256k1_fe *a);
251+
SECP256K1_INLINE static void secp256k1_fe_get_b32(unsigned char *r, const secp256k1_fe *a) {
252+
secp256k1_fe_verify(a);
253+
VERIFY_CHECK(a->normalized);
254+
secp256k1_fe_impl_get_b32(r, a);
255+
}
249256
#endif /* defined(VERIFY) */
250257

251258
#endif /* SECP256K1_FIELD_IMPL_H */

0 commit comments

Comments
 (0)