Skip to content

Commit

Permalink
musig: replace point_{save,load} with ge_{to,from}_bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasnick authored and real-or-random committed Jan 23, 2024
1 parent 54d3922 commit 764ff37
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 49 deletions.
9 changes: 0 additions & 9 deletions src/modules/musig/keyagg.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,6 @@ typedef struct {
int parity_acc;
} secp256k1_keyagg_cache_internal;

/* Save and load points to and from byte arrays, similar to
* secp256k1_pubkey_{save,load}. */
static void secp256k1_point_save(unsigned char *data, secp256k1_ge *ge);

/* In contrast to pubkey_load, point_load does not attempt to check that data
* has been initialized, since it is assumed that this check already happened
* (e.g. by comparing magic bytes) */
static void secp256k1_point_load(secp256k1_ge *ge, const unsigned char *data);

/* point_save_ext and point_load_ext are identical to point_save and point_load
* except that they allow saving and loading the point at infinity */
static void secp256k1_point_save_ext(unsigned char *data, secp256k1_ge *ge);
Expand Down
40 changes: 4 additions & 36 deletions src/modules/musig/keyagg_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,11 @@
#include "../../hash.h"
#include "../../util.h"

static void secp256k1_point_save(unsigned char *data, secp256k1_ge *ge) {
if (sizeof(secp256k1_ge_storage) == 64) {
secp256k1_ge_storage s;
secp256k1_ge_to_storage(&s, ge);
memcpy(data, &s, sizeof(s));
} else {
VERIFY_CHECK(!secp256k1_ge_is_infinity(ge));
secp256k1_fe_normalize_var(&ge->x);
secp256k1_fe_normalize_var(&ge->y);
secp256k1_fe_get_b32(data, &ge->x);
secp256k1_fe_get_b32(data + 32, &ge->y);
}
}

static void secp256k1_point_load(secp256k1_ge *ge, const unsigned char *data) {
if (sizeof(secp256k1_ge_storage) == 64) {
/* When the secp256k1_ge_storage type is exactly 64 byte, use its
* representation as conversion is very fast. */
secp256k1_ge_storage s;
memcpy(&s, data, sizeof(s));
secp256k1_ge_from_storage(ge, &s);
} else {
/* Otherwise, fall back to 32-byte big endian for X and Y. */
secp256k1_fe x, y;
int ret = 1;
ret &= secp256k1_fe_set_b32_limit(&x, data);
ret &= secp256k1_fe_set_b32_limit(&y, data + 32);
VERIFY_CHECK(ret);
secp256k1_ge_set_xy(ge, &x, &y);
}
}

static void secp256k1_point_save_ext(unsigned char *data, secp256k1_ge *ge) {
if (secp256k1_ge_is_infinity(ge)) {
memset(data, 0, 64);
} else {
secp256k1_point_save(data, ge);
secp256k1_ge_to_bytes(data, ge);
}
}

Expand All @@ -62,7 +30,7 @@ static void secp256k1_point_load_ext(secp256k1_ge *ge, const unsigned char *data
if (secp256k1_memcmp_var(data, zeros, sizeof(zeros)) == 0) {
secp256k1_ge_set_infinity(ge);
} else {
secp256k1_point_load(ge, data);
secp256k1_ge_from_bytes(ge, data);
}
}

Expand All @@ -82,7 +50,7 @@ static void secp256k1_keyagg_cache_save(secp256k1_musig_keyagg_cache *cache, sec
unsigned char *ptr = cache->data;
memcpy(ptr, secp256k1_musig_keyagg_cache_magic, 4);
ptr += 4;
secp256k1_point_save(ptr, &cache_i->pk);
secp256k1_ge_to_bytes(ptr, &cache_i->pk);
ptr += 64;
secp256k1_point_save_ext(ptr, &cache_i->second_pk);
ptr += 64;
Expand All @@ -97,7 +65,7 @@ static int secp256k1_keyagg_cache_load(const secp256k1_context* ctx, secp256k1_k
const unsigned char *ptr = cache->data;
ARG_CHECK(secp256k1_memcmp_var(ptr, secp256k1_musig_keyagg_cache_magic, 4) == 0);
ptr += 4;
secp256k1_point_load(&cache_i->pk, ptr);
secp256k1_ge_from_bytes(&cache_i->pk, ptr);
ptr += 64;
secp256k1_point_load_ext(&cache_i->second_pk, ptr);
ptr += 64;
Expand Down
8 changes: 4 additions & 4 deletions src/modules/musig/session_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ static void secp256k1_musig_secnonce_save(secp256k1_musig_secnonce *secnonce, co
memcpy(&secnonce->data[0], secp256k1_musig_secnonce_magic, 4);
secp256k1_scalar_get_b32(&secnonce->data[4], &k[0]);
secp256k1_scalar_get_b32(&secnonce->data[36], &k[1]);
secp256k1_point_save(&secnonce->data[68], pk);
secp256k1_ge_to_bytes(&secnonce->data[68], pk);
}

static int secp256k1_musig_secnonce_load(const secp256k1_context* ctx, secp256k1_scalar *k, secp256k1_ge *pk, secp256k1_musig_secnonce *secnonce) {
int is_zero;
ARG_CHECK(secp256k1_memcmp_var(&secnonce->data[0], secp256k1_musig_secnonce_magic, 4) == 0);
secp256k1_scalar_set_b32(&k[0], &secnonce->data[4], NULL);
secp256k1_scalar_set_b32(&k[1], &secnonce->data[36], NULL);
secp256k1_point_load(pk, &secnonce->data[68]);
secp256k1_ge_from_bytes(pk, &secnonce->data[68]);
/* We make very sure that the nonce isn't invalidated by checking the values
* in addition to the magic. */
is_zero = secp256k1_scalar_is_zero(&k[0]) & secp256k1_scalar_is_zero(&k[1]);
Expand Down Expand Up @@ -62,7 +62,7 @@ static void secp256k1_musig_pubnonce_save(secp256k1_musig_pubnonce* nonce, secp2
int i;
memcpy(&nonce->data[0], secp256k1_musig_pubnonce_magic, 4);
for (i = 0; i < 2; i++) {
secp256k1_point_save(nonce->data + 4+64*i, &ge[i]);
secp256k1_ge_to_bytes(nonce->data + 4+64*i, &ge[i]);
}
}

Expand All @@ -73,7 +73,7 @@ static int secp256k1_musig_pubnonce_load(const secp256k1_context* ctx, secp256k1

ARG_CHECK(secp256k1_memcmp_var(&nonce->data[0], secp256k1_musig_pubnonce_magic, 4) == 0);
for (i = 0; i < 2; i++) {
secp256k1_point_load(&ge[i], nonce->data + 4 + 64*i);
secp256k1_ge_from_bytes(&ge[i], nonce->data + 4 + 64*i);
}
return 1;
}
Expand Down

0 comments on commit 764ff37

Please sign in to comment.