Skip to content

Commit 9fcaf25

Browse files
committed
Update group element helpers
Based on: 57de68994ae18d20b0b6e1a9e4531c3d88b5ec81 and 3f9bb4d868a2a27caacdaf19b08ce91ce73c1fb4 Responds to: BlockstreamResearch#278 (comment)
1 parent 35f453d commit 9fcaf25

File tree

7 files changed

+53
-35
lines changed

7 files changed

+53
-35
lines changed

src/group.h

+9-5
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,20 @@ static void secp256k1_gej_rescale(secp256k1_gej *r, const secp256k1_fe *b);
185185

186186
/** Convert a group element that is not infinity to a 64-byte array. The output
187187
* array is platform-dependent. */
188-
static void secp256k1_ge_to_bytes(unsigned char *buf, secp256k1_ge *a);
188+
static void secp256k1_ge_to_bytes(unsigned char *buf, const secp256k1_ge *a);
189189

190190
/** Convert a 64-byte array into group element. This function assumes that the
191191
* provided buffer correctly encodes a group element. */
192192
static void secp256k1_ge_from_bytes(secp256k1_ge *r, const unsigned char *buf);
193193

194+
/** Convert a group element (that is allowed to be infinity) to a 64-byte
195+
* array. The output array is platform-dependent. */
196+
static void secp256k1_ge_to_bytes_ext(unsigned char *data, const secp256k1_ge *ge);
197+
198+
/** Convert a 64-byte array into a group element. This function assumes that the
199+
* provided buffer is the output of secp256k1_ge_to_bytes_ext. */
200+
static void secp256k1_ge_from_bytes_ext(secp256k1_ge *ge, const unsigned char *data);
201+
194202
/** Determine if a point (which is assumed to be on the curve) is in the correct (sub)group of the curve.
195203
*
196204
* In normal mode, the used group is secp256k1, which has cofactor=1 meaning that every point on the curve is in the
@@ -202,10 +210,6 @@ static void secp256k1_ge_from_bytes(secp256k1_ge *r, const unsigned char *buf);
202210
*/
203211
static int secp256k1_ge_is_in_correct_subgroup(const secp256k1_ge* ge);
204212

205-
static void secp256k1_point_save_ext(unsigned char *data, secp256k1_ge *ge);
206-
207-
static void secp256k1_point_load_ext(secp256k1_ge *ge, const unsigned char *data);
208-
209213
/** Check invariants on an affine group element (no-op unless VERIFY is enabled). */
210214
static void secp256k1_ge_verify(const secp256k1_ge *a);
211215
#define SECP256K1_GE_VERIFY(a) secp256k1_ge_verify(a)

src/group_impl.h

+18-18
Original file line numberDiff line numberDiff line change
@@ -914,23 +914,6 @@ static int secp256k1_gej_has_quad_y_var(const secp256k1_gej *a) {
914914
return secp256k1_fe_is_square_var(&yz);
915915
}
916916

917-
static void secp256k1_point_save_ext(unsigned char *data, secp256k1_ge *ge) {
918-
if (secp256k1_ge_is_infinity(ge)) {
919-
memset(data, 0, 64);
920-
} else {
921-
secp256k1_ge_to_bytes(data, ge);
922-
}
923-
}
924-
925-
static void secp256k1_point_load_ext(secp256k1_ge *ge, const unsigned char *data) {
926-
unsigned char zeros[64] = { 0 };
927-
if (secp256k1_memcmp_var(data, zeros, sizeof(zeros)) == 0) {
928-
secp256k1_ge_set_infinity(ge);
929-
} else {
930-
secp256k1_ge_from_bytes(ge, data);
931-
}
932-
}
933-
934917
static int secp256k1_ge_is_in_correct_subgroup(const secp256k1_ge* ge) {
935918
#ifdef EXHAUSTIVE_TEST_ORDER
936919
secp256k1_gej out;
@@ -982,7 +965,7 @@ static int secp256k1_ge_x_frac_on_curve_var(const secp256k1_fe *xn, const secp25
982965
return secp256k1_fe_is_square_var(&r);
983966
}
984967

985-
static void secp256k1_ge_to_bytes(unsigned char *buf, secp256k1_ge *a) {
968+
static void secp256k1_ge_to_bytes(unsigned char *buf, const secp256k1_ge *a) {
986969
secp256k1_ge_storage s;
987970

988971
/* We require that the secp256k1_ge_storage type is exactly 64 bytes.
@@ -1002,4 +985,21 @@ static void secp256k1_ge_from_bytes(secp256k1_ge *r, const unsigned char *buf) {
1002985
secp256k1_ge_from_storage(r, &s);
1003986
}
1004987

988+
static void secp256k1_ge_to_bytes_ext(unsigned char *data, const secp256k1_ge *ge) {
989+
if (secp256k1_ge_is_infinity(ge)) {
990+
memset(data, 0, 64);
991+
} else {
992+
secp256k1_ge_to_bytes(data, ge);
993+
}
994+
}
995+
996+
static void secp256k1_ge_from_bytes_ext(secp256k1_ge *ge, const unsigned char *data) {
997+
unsigned char zeros[64] = { 0 };
998+
if (secp256k1_memcmp_var(data, zeros, sizeof(zeros)) == 0) {
999+
secp256k1_ge_set_infinity(ge);
1000+
} else {
1001+
secp256k1_ge_from_bytes(ge, data);
1002+
}
1003+
}
1004+
10051005
#endif /* SECP256K1_GROUP_IMPL_H */

src/modules/frost/keygen_impl.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ static void secp256k1_tweak_cache_save(secp256k1_frost_tweak_cache *cache, secp2
3434
unsigned char *ptr = cache->data;
3535
memcpy(ptr, secp256k1_frost_tweak_cache_magic, 4);
3636
ptr += 4;
37-
secp256k1_point_save_ext(ptr, &cache_i->pk);
37+
secp256k1_ge_to_bytes_ext(ptr, &cache_i->pk);
3838
ptr += 64;
3939
*ptr = cache_i->parity_acc;
4040
ptr += 1;
@@ -45,7 +45,7 @@ static int secp256k1_tweak_cache_load(const secp256k1_context* ctx, secp256k1_tw
4545
const unsigned char *ptr = cache->data;
4646
ARG_CHECK(secp256k1_memcmp_var(ptr, secp256k1_frost_tweak_cache_magic, 4) == 0);
4747
ptr += 4;
48-
secp256k1_point_load_ext(&cache_i->pk, ptr);
48+
secp256k1_ge_from_bytes_ext(&cache_i->pk, ptr);
4949
ptr += 64;
5050
cache_i->parity_acc = *ptr & 1;
5151
ptr += 1;

src/modules/frost/session_impl.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static void secp256k1_frost_pubnonce_save(secp256k1_frost_pubnonce* nonce, secp2
5858
int i;
5959
memcpy(&nonce->data[0], secp256k1_frost_pubnonce_magic, 4);
6060
for (i = 0; i < 2; i++) {
61-
secp256k1_point_save_ext(nonce->data + 4+64*i, &ge[i]);
61+
secp256k1_ge_to_bytes_ext(nonce->data + 4+64*i, &ge[i]);
6262
}
6363
}
6464

@@ -69,7 +69,7 @@ static int secp256k1_frost_pubnonce_load(const secp256k1_context* ctx, secp256k1
6969

7070
ARG_CHECK(secp256k1_memcmp_var(&nonce->data[0], secp256k1_frost_pubnonce_magic, 4) == 0);
7171
for (i = 0; i < 2; i++) {
72-
secp256k1_point_load_ext(&ge[i], nonce->data + 4+64*i);
72+
secp256k1_ge_from_bytes_ext(&ge[i], nonce->data + 4+64*i);
7373
}
7474
return 1;
7575
}

src/modules/musig/keyagg_impl.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static void secp256k1_keyagg_cache_save(secp256k1_musig_keyagg_cache *cache, sec
3535
ptr += 4;
3636
secp256k1_ge_to_bytes(ptr, &cache_i->pk);
3737
ptr += 64;
38-
secp256k1_point_save_ext(ptr, &cache_i->second_pk);
38+
secp256k1_ge_to_bytes_ext(ptr, &cache_i->second_pk);
3939
ptr += 64;
4040
memcpy(ptr, cache_i->pk_hash, 32);
4141
ptr += 32;
@@ -50,7 +50,7 @@ static int secp256k1_keyagg_cache_load(const secp256k1_context* ctx, secp256k1_k
5050
ptr += 4;
5151
secp256k1_ge_from_bytes(&cache_i->pk, ptr);
5252
ptr += 64;
53-
secp256k1_point_load_ext(&cache_i->second_pk, ptr);
53+
secp256k1_ge_from_bytes_ext(&cache_i->second_pk, ptr);
5454
ptr += 64;
5555
memcpy(cache_i->pk_hash, ptr, 32);
5656
ptr += 32;

src/modules/musig/session_impl.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ static void secp256k1_musig_aggnonce_save(secp256k1_musig_aggnonce* nonce, secp2
8484
int i;
8585
memcpy(&nonce->data[0], secp256k1_musig_aggnonce_magic, 4);
8686
for (i = 0; i < 2; i++) {
87-
secp256k1_point_save_ext(&nonce->data[4 + 64*i], &ge[i]);
87+
secp256k1_ge_to_bytes_ext(&nonce->data[4 + 64*i], &ge[i]);
8888
}
8989
}
9090

@@ -93,7 +93,7 @@ static int secp256k1_musig_aggnonce_load(const secp256k1_context* ctx, secp256k1
9393

9494
ARG_CHECK(secp256k1_memcmp_var(&nonce->data[0], secp256k1_musig_aggnonce_magic, 4) == 0);
9595
for (i = 0; i < 2; i++) {
96-
secp256k1_point_load_ext(&ge[i], &nonce->data[4 + 64*i]);
96+
secp256k1_ge_from_bytes_ext(&ge[i], &nonce->data[4 + 64*i]);
9797
}
9898
return 1;
9999
}

src/tests.c

+18-4
Original file line numberDiff line numberDiff line change
@@ -4075,13 +4075,27 @@ static void test_add_neg_y_diff_x(void) {
40754075
static void test_ge_bytes(void) {
40764076
int i;
40774077

4078-
for (i = 0; i < COUNT; i++) {
4078+
for (i = 0; i < COUNT + 1; i++) {
40794079
unsigned char buf[64];
40804080
secp256k1_ge p, q;
40814081

4082-
random_group_element_test(&p);
4083-
secp256k1_ge_to_bytes(buf, &p);
4084-
secp256k1_ge_from_bytes(&q, buf);
4082+
if (i == 0) {
4083+
secp256k1_ge_set_infinity(&p);
4084+
} else {
4085+
random_group_element_test(&p);
4086+
}
4087+
4088+
if (!secp256k1_ge_is_infinity(&p)) {
4089+
secp256k1_ge_to_bytes(buf, &p);
4090+
4091+
secp256k1_ge_from_bytes(&q, buf);
4092+
CHECK(secp256k1_ge_eq_var(&p, &q));
4093+
4094+
secp256k1_ge_from_bytes_ext(&q, buf);
4095+
CHECK(secp256k1_ge_eq_var(&p, &q));
4096+
}
4097+
secp256k1_ge_to_bytes_ext(buf, &p);
4098+
secp256k1_ge_from_bytes_ext(&q, buf);
40854099
CHECK(secp256k1_ge_eq_var(&p, &q));
40864100
}
40874101
}

0 commit comments

Comments
 (0)