Skip to content

Commit 33db8ed

Browse files
jonasnickreal-or-random
authored andcommitted
group: add ge_to_bytes and ge_from_bytes
1 parent de54a1e commit 33db8ed

File tree

4 files changed

+47
-14
lines changed

4 files changed

+47
-14
lines changed

src/group.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,14 @@ static void secp256k1_ge_storage_cmov(secp256k1_ge_storage *r, const secp256k1_g
183183
/** Rescale a jacobian point by b which must be non-zero. Constant-time. */
184184
static void secp256k1_gej_rescale(secp256k1_gej *r, const secp256k1_fe *b);
185185

186+
/** Convert a group element that is not infinity to a 64-byte array. The output
187+
* array is platform-dependent. */
188+
static void secp256k1_ge_to_bytes(unsigned char *buf, secp256k1_ge *a);
189+
190+
/** Convert a 64-byte array into group element. This function assumes that the
191+
* provided buffer correctly encodes a group element. */
192+
static void secp256k1_ge_from_bytes(secp256k1_ge *r, const unsigned char *buf);
193+
186194
/** Determine if a point (which is assumed to be on the curve) is in the correct (sub)group of the curve.
187195
*
188196
* 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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#ifndef SECP256K1_GROUP_IMPL_H
88
#define SECP256K1_GROUP_IMPL_H
99

10+
#include <string.h>
11+
1012
#include "field.h"
1113
#include "group.h"
1214
#include "util.h"
@@ -963,4 +965,24 @@ static int secp256k1_ge_x_frac_on_curve_var(const secp256k1_fe *xn, const secp25
963965
return secp256k1_fe_is_square_var(&r);
964966
}
965967

968+
static void secp256k1_ge_to_bytes(unsigned char *buf, secp256k1_ge *a) {
969+
secp256k1_ge_storage s;
970+
971+
/* We require that the secp256k1_ge_storage type is exactly 64 bytes.
972+
* This is formally not guaranteed by the C standard, but should hold on any
973+
* sane compiler in the real world. */
974+
STATIC_ASSERT(sizeof(secp256k1_ge_storage) == 64);
975+
VERIFY_CHECK(!secp256k1_ge_is_infinity(a));
976+
secp256k1_ge_to_storage(&s, a);
977+
memcpy(buf, &s, 64);
978+
}
979+
980+
static void secp256k1_ge_from_bytes(secp256k1_ge *r, const unsigned char *buf) {
981+
secp256k1_ge_storage s;
982+
983+
STATIC_ASSERT(sizeof(secp256k1_ge_storage) == 64);
984+
memcpy(&s, buf, 64);
985+
secp256k1_ge_from_storage(r, &s);
986+
}
987+
966988
#endif /* SECP256K1_GROUP_IMPL_H */

src/secp256k1.c

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -258,25 +258,13 @@ static SECP256K1_INLINE void secp256k1_declassify(const secp256k1_context* ctx,
258258
}
259259

260260
static int secp256k1_pubkey_load(const secp256k1_context* ctx, secp256k1_ge* ge, const secp256k1_pubkey* pubkey) {
261-
secp256k1_ge_storage s;
262-
263-
/* We require that the secp256k1_ge_storage type is exactly 64 bytes.
264-
* This is formally not guaranteed by the C standard, but should hold on any
265-
* sane compiler in the real world. */
266-
STATIC_ASSERT(sizeof(secp256k1_ge_storage) == 64);
267-
memcpy(&s, &pubkey->data[0], 64);
268-
secp256k1_ge_from_storage(ge, &s);
261+
secp256k1_ge_from_bytes(ge, pubkey->data);
269262
ARG_CHECK(!secp256k1_fe_is_zero(&ge->x));
270263
return 1;
271264
}
272265

273266
static void secp256k1_pubkey_save(secp256k1_pubkey* pubkey, secp256k1_ge* ge) {
274-
secp256k1_ge_storage s;
275-
276-
STATIC_ASSERT(sizeof(secp256k1_ge_storage) == 64);
277-
VERIFY_CHECK(!secp256k1_ge_is_infinity(ge));
278-
secp256k1_ge_to_storage(&s, ge);
279-
memcpy(&pubkey->data[0], &s, 64);
267+
secp256k1_ge_to_bytes(pubkey->data, ge);
280268
}
281269

282270
int secp256k1_ec_pubkey_parse(const secp256k1_context* ctx, secp256k1_pubkey* pubkey, const unsigned char *input, size_t inputlen) {

src/tests.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4072,13 +4072,28 @@ static void test_add_neg_y_diff_x(void) {
40724072
CHECK(secp256k1_gej_eq_ge_var(&sumj, &res));
40734073
}
40744074

4075+
static void test_ge_bytes(void) {
4076+
int i;
4077+
4078+
for (i = 0; i < COUNT; i++) {
4079+
unsigned char buf[64];
4080+
secp256k1_ge p, q;
4081+
4082+
random_group_element_test(&p);
4083+
secp256k1_ge_to_bytes(buf, &p);
4084+
secp256k1_ge_from_bytes(&q, buf);
4085+
CHECK(secp256k1_ge_eq_var(&p, &q));
4086+
}
4087+
}
4088+
40754089
static void run_ge(void) {
40764090
int i;
40774091
for (i = 0; i < COUNT * 32; i++) {
40784092
test_ge();
40794093
}
40804094
test_add_neg_y_diff_x();
40814095
test_intialized_inf();
4096+
test_ge_bytes();
40824097
}
40834098

40844099
static void test_gej_cmov(const secp256k1_gej *a, const secp256k1_gej *b) {

0 commit comments

Comments
 (0)