I'm a newbie who has just started reading Bitcoin Core code. I have a lot of questions about seemingly trivial code. Right now, I have two questions.
- When branching to an
if
statement, why is the buffers
used instead of copying directly from thepubkey
to the parameterge
(and vice versa) in the secp256k1_pubkey_load/save function?
Wouldn't the code below work just as well?
// secp256k1.c
static int secp256k1_pubkey_load(const secp256k1_context* ctx, secp256k1_ge* ge, const secp256k1_pubkey* pubkey) {
if (sizeof(secp256k1_ge_storage) == 64) {
/* When the secp256k1_ge_storage type is exactly 64 byte, use its
* representation inside secp256k1_pubkey, as conversion is very fast.
* Note that secp256k1_pubkey_save must use the same representation. */
#if (0)
secp256k1_ge_storage s;
memcpy(&s, &pubkey->data[0], sizeof(s));
secp256k1_ge_from_storage(ge, &s);
#else
secp256k1_ge_from_storage(ge, (secp256k1_ge_storage *)&pubkey->data[0]);
#endif /* (0) */
} else {
/* Otherwise, fall back to 32-byte big endian for X and Y. */
secp256k1_fe x, y;
secp256k1_fe_set_b32(&x, pubkey->data);
secp256k1_fe_set_b32(&y, pubkey->data + 32);
secp256k1_ge_set_xy(ge, &x, &y);
}
ARG_CHECK(!secp256k1_fe_is_zero(&ge->x));
return 1;
}
- Is it possible to branch to
else
?
Looking at the structure below, it seems impossible.
// group.h
typedef struct {
secp256k1_fe_storage x;
secp256k1_fe_storage y;
} secp256k1_ge_storage;
// field_5x52.h
typedef struct {
uint64_t n[4];
} secp256k1_fe_storage;
// field_10x26.h
typedef struct {
uint32_t n[8];
} secp256k1_fe_storage;