Skip to content

Commit c8fbdb1

Browse files
committed
group: add ge_to_bytes_ext and ge_from_bytes_ext
1 parent 85e224d commit c8fbdb1

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

src/group.h

+8
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,14 @@ static void secp256k1_ge_to_bytes(unsigned char *buf, const secp256k1_ge *a);
182182
* provided buffer correctly encodes a group element. */
183183
static void secp256k1_ge_from_bytes(secp256k1_ge *r, const unsigned char *buf);
184184

185+
/** Convert a group element (that is allowed to be infinity) to a 64-byte
186+
* array. The output array is platform-dependent. */
187+
static void secp256k1_ge_to_bytes_ext(unsigned char *data, const secp256k1_ge *ge);
188+
189+
/** Convert a 64-byte array into a group element. This function assumes that the
190+
* provided buffer is the output of secp256k1_ge_to_bytes_ext. */
191+
static void secp256k1_ge_from_bytes_ext(secp256k1_ge *ge, const unsigned char *data);
192+
185193
/** Determine if a point (which is assumed to be on the curve) is in the correct (sub)group of the curve.
186194
*
187195
* In normal mode, the used group is secp256k1, which has cofactor=1 meaning that every point on the curve is in the

src/group_impl.h

+17
Original file line numberDiff line numberDiff line change
@@ -963,4 +963,21 @@ static void secp256k1_ge_from_bytes(secp256k1_ge *r, const unsigned char *buf) {
963963
secp256k1_ge_from_storage(r, &s);
964964
}
965965

966+
static void secp256k1_ge_to_bytes_ext(unsigned char *data, const secp256k1_ge *ge) {
967+
if (secp256k1_ge_is_infinity(ge)) {
968+
memset(data, 0, 64);
969+
} else {
970+
secp256k1_ge_to_bytes(data, ge);
971+
}
972+
}
973+
974+
static void secp256k1_ge_from_bytes_ext(secp256k1_ge *ge, const unsigned char *data) {
975+
static const unsigned char zeros[64] = { 0 };
976+
if (secp256k1_memcmp_var(data, zeros, sizeof(zeros)) == 0) {
977+
secp256k1_ge_set_infinity(ge);
978+
} else {
979+
secp256k1_ge_from_bytes(ge, data);
980+
}
981+
}
982+
966983
#endif /* SECP256K1_GROUP_IMPL_H */

src/tests.c

+13-2
Original file line numberDiff line numberDiff line change
@@ -3985,17 +3985,28 @@ static void test_add_neg_y_diff_x(void) {
39853985
static void test_ge_bytes(void) {
39863986
int i;
39873987

3988-
for (i = 0; i < COUNT; i++) {
3988+
for (i = 0; i < COUNT + 1; i++) {
39893989
unsigned char buf[64];
39903990
secp256k1_ge p, q;
39913991

3992-
testutil_random_ge_test(&p);
3992+
if (i == 0) {
3993+
secp256k1_ge_set_infinity(&p);
3994+
} else {
3995+
testutil_random_ge_test(&p);
3996+
}
39933997

39943998
if (!secp256k1_ge_is_infinity(&p)) {
39953999
secp256k1_ge_to_bytes(buf, &p);
4000+
39964001
secp256k1_ge_from_bytes(&q, buf);
39974002
CHECK(secp256k1_ge_eq_var(&p, &q));
4003+
4004+
secp256k1_ge_from_bytes_ext(&q, buf);
4005+
CHECK(secp256k1_ge_eq_var(&p, &q));
39984006
}
4007+
secp256k1_ge_to_bytes_ext(buf, &p);
4008+
secp256k1_ge_from_bytes_ext(&q, buf);
4009+
CHECK(secp256k1_ge_eq_var(&p, &q));
39994010
}
40004011
}
40014012

0 commit comments

Comments
 (0)