Skip to content

Commit 13b226e

Browse files
committed
group: add ge_to_bytes and ge_from_bytes
1 parent e1ba262 commit 13b226e

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
@@ -174,6 +174,14 @@ static void secp256k1_ge_storage_cmov(secp256k1_ge_storage *r, const secp256k1_g
174174
/** Rescale a jacobian point by b which must be non-zero. Constant-time. */
175175
static void secp256k1_gej_rescale(secp256k1_gej *r, const secp256k1_fe *b);
176176

177+
/** Convert a group element that is not infinity to a 64-byte array. The output
178+
* array is platform-dependent. */
179+
static void secp256k1_ge_to_bytes(unsigned char *buf, secp256k1_ge *a);
180+
181+
/** Convert a 64-byte array into group element. This function assumes that the
182+
* provided buffer correctly encodes a group element. */
183+
static void secp256k1_ge_from_bytes(secp256k1_ge *r, const unsigned char *buf);
184+
177185
/** Determine if a point (which is assumed to be on the curve) is in the correct (sub)group of the curve.
178186
*
179187
* 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"
@@ -941,4 +943,24 @@ static int secp256k1_ge_x_frac_on_curve_var(const secp256k1_fe *xn, const secp25
941943
return secp256k1_fe_is_square_var(&r);
942944
}
943945

946+
static void secp256k1_ge_to_bytes(unsigned char *buf, secp256k1_ge *a) {
947+
secp256k1_ge_storage s;
948+
949+
/* We require that the secp256k1_ge_storage type is exactly 64 bytes.
950+
* This is formally not guaranteed by the C standard, but should hold on any
951+
* sane compiler in the real world. */
952+
STATIC_ASSERT(sizeof(secp256k1_ge_storage) == 64);
953+
VERIFY_CHECK(!secp256k1_ge_is_infinity(a));
954+
secp256k1_ge_to_storage(&s, a);
955+
memcpy(buf, &s, 64);
956+
}
957+
958+
static void secp256k1_ge_from_bytes(secp256k1_ge *r, const unsigned char *buf) {
959+
secp256k1_ge_storage s;
960+
961+
STATIC_ASSERT(sizeof(secp256k1_ge_storage) == 64);
962+
memcpy(&s, buf, 64);
963+
secp256k1_ge_from_storage(r, &s);
964+
}
965+
944966
#endif /* SECP256K1_GROUP_IMPL_H */

src/secp256k1.c

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

239239
static int secp256k1_pubkey_load(const secp256k1_context* ctx, secp256k1_ge* ge, const secp256k1_pubkey* pubkey) {
240-
secp256k1_ge_storage s;
241-
242-
/* We require that the secp256k1_ge_storage type is exactly 64 bytes.
243-
* This is formally not guaranteed by the C standard, but should hold on any
244-
* sane compiler in the real world. */
245-
STATIC_ASSERT(sizeof(secp256k1_ge_storage) == 64);
246-
memcpy(&s, &pubkey->data[0], 64);
247-
secp256k1_ge_from_storage(ge, &s);
240+
secp256k1_ge_from_bytes(ge, pubkey->data);
248241
ARG_CHECK(!secp256k1_fe_is_zero(&ge->x));
249242
return 1;
250243
}
251244

252245
static void secp256k1_pubkey_save(secp256k1_pubkey* pubkey, secp256k1_ge* ge) {
253-
secp256k1_ge_storage s;
254-
255-
STATIC_ASSERT(sizeof(secp256k1_ge_storage) == 64);
256-
VERIFY_CHECK(!secp256k1_ge_is_infinity(ge));
257-
secp256k1_ge_to_storage(&s, ge);
258-
memcpy(&pubkey->data[0], &s, 64);
246+
secp256k1_ge_to_bytes(pubkey->data, ge);
259247
}
260248

261249
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
@@ -4016,13 +4016,28 @@ static void test_add_neg_y_diff_x(void) {
40164016
CHECK(secp256k1_gej_eq_ge_var(&sumj, &res));
40174017
}
40184018

4019+
static void test_ge_bytes(void) {
4020+
int i;
4021+
4022+
for (i = 0; i < COUNT; i++) {
4023+
unsigned char buf[64];
4024+
secp256k1_ge p, q;
4025+
4026+
random_group_element_test(&p);
4027+
secp256k1_ge_to_bytes(buf, &p);
4028+
secp256k1_ge_from_bytes(&q, buf);
4029+
CHECK(secp256k1_ge_eq_var(&p, &q));
4030+
}
4031+
}
4032+
40194033
static void run_ge(void) {
40204034
int i;
40214035
for (i = 0; i < COUNT * 32; i++) {
40224036
test_ge();
40234037
}
40244038
test_add_neg_y_diff_x();
40254039
test_intialized_inf();
4040+
test_ge_bytes();
40264041
}
40274042

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

0 commit comments

Comments
 (0)