Skip to content

Commit

Permalink
group: add ge_to_bytes and ge_from_bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasnick committed Sep 2, 2024
1 parent 1988855 commit 85e224d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 14 deletions.
8 changes: 8 additions & 0 deletions src/group.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,14 @@ static void secp256k1_ge_storage_cmov(secp256k1_ge_storage *r, const secp256k1_g
/** Rescale a jacobian point by b which must be non-zero. Constant-time. */
static void secp256k1_gej_rescale(secp256k1_gej *r, const secp256k1_fe *b);

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

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

/** Determine if a point (which is assumed to be on the curve) is in the correct (sub)group of the curve.
*
* In normal mode, the used group is secp256k1, which has cofactor=1 meaning that every point on the curve is in the
Expand Down
22 changes: 22 additions & 0 deletions src/group_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#ifndef SECP256K1_GROUP_IMPL_H
#define SECP256K1_GROUP_IMPL_H

#include <string.h>

#include "field.h"
#include "group.h"
#include "util.h"
Expand Down Expand Up @@ -941,4 +943,24 @@ static int secp256k1_ge_x_frac_on_curve_var(const secp256k1_fe *xn, const secp25
return secp256k1_fe_is_square_var(&r);
}

static void secp256k1_ge_to_bytes(unsigned char *buf, const secp256k1_ge *a) {
secp256k1_ge_storage s;

/* We require that the secp256k1_ge_storage type is exactly 64 bytes.
* This is formally not guaranteed by the C standard, but should hold on any
* sane compiler in the real world. */
STATIC_ASSERT(sizeof(secp256k1_ge_storage) == 64);
VERIFY_CHECK(!secp256k1_ge_is_infinity(a));
secp256k1_ge_to_storage(&s, a);
memcpy(buf, &s, 64);
}

static void secp256k1_ge_from_bytes(secp256k1_ge *r, const unsigned char *buf) {
secp256k1_ge_storage s;

STATIC_ASSERT(sizeof(secp256k1_ge_storage) == 64);
memcpy(&s, buf, 64);
secp256k1_ge_from_storage(r, &s);
}

#endif /* SECP256K1_GROUP_IMPL_H */
16 changes: 2 additions & 14 deletions src/secp256k1.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,25 +238,13 @@ static SECP256K1_INLINE void secp256k1_declassify(const secp256k1_context* ctx,
}

static int secp256k1_pubkey_load(const secp256k1_context* ctx, secp256k1_ge* ge, const secp256k1_pubkey* pubkey) {
secp256k1_ge_storage s;

/* We require that the secp256k1_ge_storage type is exactly 64 bytes.
* This is formally not guaranteed by the C standard, but should hold on any
* sane compiler in the real world. */
STATIC_ASSERT(sizeof(secp256k1_ge_storage) == 64);
memcpy(&s, &pubkey->data[0], 64);
secp256k1_ge_from_storage(ge, &s);
secp256k1_ge_from_bytes(ge, pubkey->data);
ARG_CHECK(!secp256k1_fe_is_zero(&ge->x));
return 1;
}

static void secp256k1_pubkey_save(secp256k1_pubkey* pubkey, secp256k1_ge* ge) {
secp256k1_ge_storage s;

STATIC_ASSERT(sizeof(secp256k1_ge_storage) == 64);
VERIFY_CHECK(!secp256k1_ge_is_infinity(ge));
secp256k1_ge_to_storage(&s, ge);
memcpy(&pubkey->data[0], &s, 64);
secp256k1_ge_to_bytes(pubkey->data, ge);
}

int secp256k1_ec_pubkey_parse(const secp256k1_context* ctx, secp256k1_pubkey* pubkey, const unsigned char *input, size_t inputlen) {
Expand Down
18 changes: 18 additions & 0 deletions src/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -3982,13 +3982,31 @@ static void test_add_neg_y_diff_x(void) {
CHECK(secp256k1_gej_eq_ge_var(&sumj, &res));
}

static void test_ge_bytes(void) {
int i;

for (i = 0; i < COUNT; i++) {
unsigned char buf[64];
secp256k1_ge p, q;

testutil_random_ge_test(&p);

if (!secp256k1_ge_is_infinity(&p)) {
secp256k1_ge_to_bytes(buf, &p);
secp256k1_ge_from_bytes(&q, buf);
CHECK(secp256k1_ge_eq_var(&p, &q));
}
}
}

static void run_ge(void) {
int i;
for (i = 0; i < COUNT * 32; i++) {
test_ge();
}
test_add_neg_y_diff_x();
test_intialized_inf();
test_ge_bytes();
}

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

0 comments on commit 85e224d

Please sign in to comment.