Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/field.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ static void secp256k1_fe_inv_var(secp256k1_fe_t *r, const secp256k1_fe_t *a);
* outputs must not overlap in memory. */
static void secp256k1_fe_inv_all_var(size_t len, secp256k1_fe_t *r, const secp256k1_fe_t *a);

/** Convert a field element to a hexadecimal string. */
static void secp256k1_fe_get_hex(char *r, int *rlen, const secp256k1_fe_t *a);
/** Convert a field element to a 64-character hexadecimal string. */
static void secp256k1_fe_get_hex(char *r64, const secp256k1_fe_t *a);

/** Convert a hexadecimal string to a field element. */
static int secp256k1_fe_set_hex(secp256k1_fe_t *r, const char *a, int alen);
/** Convert a 64-character hexadecimal string to a field element. */
static int secp256k1_fe_set_hex(secp256k1_fe_t *r, const char *a64);

/** If flag is true, set *r equal to *a; otherwise leave it. Constant-time. */
static void secp256k1_fe_cmov(secp256k1_fe_t *r, const secp256k1_fe_t *a, int flag);
Expand Down
19 changes: 6 additions & 13 deletions src/field_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,23 @@
#error "Please select field implementation"
#endif

static void secp256k1_fe_get_hex(char *r, int *rlen, const secp256k1_fe_t *a) {
static void secp256k1_fe_get_hex(char *r64, const secp256k1_fe_t *a) {
secp256k1_fe_t b;
int i;
unsigned char tmp[32];
if (*rlen < 65) {
*rlen = 65;
return;
}
*rlen = 65;
b = *a;
secp256k1_fe_normalize(&b);
secp256k1_fe_get_b32(tmp, &b);
for (i=0; i<32; i++) {
static const char *c = "0123456789ABCDEF";
r[2*i] = c[(tmp[i] >> 4) & 0xF];
r[2*i+1] = c[(tmp[i]) & 0xF];
r64[2*i] = c[(tmp[i] >> 4) & 0xF];
r64[2*i+1] = c[(tmp[i]) & 0xF];
}
r[64] = 0x00;
}

static int secp256k1_fe_set_hex(secp256k1_fe_t *r, const char *a, int alen) {
static int secp256k1_fe_set_hex(secp256k1_fe_t *r, const char *a64) {
int i;
unsigned char tmp[32] = {0};
unsigned char tmp[32];
static const int cvt[256] = {0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
Expand All @@ -61,8 +55,7 @@ static int secp256k1_fe_set_hex(secp256k1_fe_t *r, const char *a, int alen) {
0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0};
for (i=0; i<32; i++) {
if (alen > i*2)
tmp[32 - alen/2 + i] = (cvt[(unsigned char)a[2*i]] << 4) + cvt[(unsigned char)a[2*i+1]];
tmp[i] = (cvt[(unsigned char)a64[2*i]] << 4) + cvt[(unsigned char)a64[2*i+1]];
}
return secp256k1_fe_set_b32(r, tmp);
}
Expand Down
8 changes: 4 additions & 4 deletions src/group.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ static int secp256k1_ge_is_valid_var(const secp256k1_ge_t *a);

static void secp256k1_ge_neg(secp256k1_ge_t *r, const secp256k1_ge_t *a);

/** Get a hex representation of a point. *rlen will be overwritten with the real length. */
static void secp256k1_ge_get_hex(char *r, int *rlen, const secp256k1_ge_t *a);
/** Get a 131-character hex representation of a point. */
static void secp256k1_ge_get_hex(char *r131, const secp256k1_ge_t *a);

/** Set a group element equal to another which is given in jacobian coordinates */
static void secp256k1_ge_set_gej(secp256k1_ge_t *r, secp256k1_gej_t *a);
Expand Down Expand Up @@ -85,8 +85,8 @@ static void secp256k1_gej_add_ge(secp256k1_gej_t *r, const secp256k1_gej_t *a, c
guarantee, and b is allowed to be infinity. */
static void secp256k1_gej_add_ge_var(secp256k1_gej_t *r, const secp256k1_gej_t *a, const secp256k1_ge_t *b);

/** Get a hex representation of a point. *rlen will be overwritten with the real length. */
static void secp256k1_gej_get_hex(char *r, int *rlen, const secp256k1_gej_t *a);
/** Get a 131-character hex representation of a point. */
static void secp256k1_gej_get_hex(char *r131, const secp256k1_gej_t *a);

#ifdef USE_ENDOMORPHISM
/** Set r to be equal to lambda times a, where lambda is chosen in a way such that this is very fast. */
Expand Down
31 changes: 8 additions & 23 deletions src/group_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,12 @@ static void secp256k1_ge_neg(secp256k1_ge_t *r, const secp256k1_ge_t *a) {
secp256k1_fe_negate(&r->y, &r->y, 1);
}

static void secp256k1_ge_get_hex(char *r, int *rlen, const secp256k1_ge_t *a) {
char cx[65]; int lx=65;
char cy[65]; int ly=65;
secp256k1_fe_get_hex(cx, &lx, &a->x);
secp256k1_fe_get_hex(cy, &ly, &a->y);
lx = strlen(cx);
ly = strlen(cy);
int len = lx + ly + 3 + 1;
if (*rlen < len) {
*rlen = len;
return;
}
*rlen = len;
r[0] = '(';
memcpy(r+1, cx, lx);
r[1+lx] = ',';
memcpy(r+2+lx, cy, ly);
r[2+lx+ly] = ')';
r[3+lx+ly] = 0;
static void secp256k1_ge_get_hex(char *r131, const secp256k1_ge_t *a) {
r131[0] = '(';
secp256k1_fe_get_hex(r131 + 1, &a->x);
r131[65] = ',';
secp256k1_fe_get_hex(r131 + 66, &a->y);
r131[130] = ')';
}

static void secp256k1_ge_set_gej(secp256k1_ge_t *r, secp256k1_gej_t *a) {
Expand Down Expand Up @@ -399,12 +386,10 @@ static void secp256k1_gej_add_ge(secp256k1_gej_t *r, const secp256k1_gej_t *a, c
r->infinity = infinity;
}



static void secp256k1_gej_get_hex(char *r, int *rlen, const secp256k1_gej_t *a) {
static void secp256k1_gej_get_hex(char *r131, const secp256k1_gej_t *a) {
secp256k1_gej_t c = *a;
secp256k1_ge_t t; secp256k1_ge_set_gej(&t, &c);
secp256k1_ge_get_hex(r, rlen, &t);
secp256k1_ge_get_hex(r131, &t);
}

#ifdef USE_ENDOMORPHISM
Expand Down
34 changes: 14 additions & 20 deletions src/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -928,8 +928,8 @@ void run_ge(void) {

void run_ecmult_chain(void) {
/* random starting point A (on the curve) */
secp256k1_fe_t ax; VERIFY_CHECK(secp256k1_fe_set_hex(&ax, "8b30bbe9ae2a990696b22f670709dff3727fd8bc04d3362c6c7bf458e2846004", 64));
secp256k1_fe_t ay; VERIFY_CHECK(secp256k1_fe_set_hex(&ay, "a357ae915c4a65281309edf20504740f0eb3343990216b4f81063cb65f2f7e0f", 64));
secp256k1_fe_t ax; VERIFY_CHECK(secp256k1_fe_set_hex(&ax, "8b30bbe9ae2a990696b22f670709dff3727fd8bc04d3362c6c7bf458e2846004"));
secp256k1_fe_t ay; VERIFY_CHECK(secp256k1_fe_set_hex(&ay, "a357ae915c4a65281309edf20504740f0eb3343990216b4f81063cb65f2f7e0f"));
secp256k1_gej_t a; secp256k1_gej_set_xy(&a, &ax, &ay);
/* two random initial factors xn and gn */
static const unsigned char xni[32] = {
Expand Down Expand Up @@ -976,19 +976,18 @@ void run_ecmult_chain(void) {

/* verify */
if (i == 19999) {
char res[132]; int resl = 132;
secp256k1_gej_get_hex(res, &resl, &x);
CHECK(strcmp(res, "(D6E96687F9B10D092A6F35439D86CEBEA4535D0D409F53586440BD74B933E830,B95CBCA2C77DA786539BE8FD53354D2D3B4F566AE658045407ED6015EE1B2A88)") == 0);
char res[131];
secp256k1_gej_get_hex(res, &x);
CHECK(memcmp(res, "(D6E96687F9B10D092A6F35439D86CEBEA4535D0D409F53586440BD74B933E830,B95CBCA2C77DA786539BE8FD53354D2D3B4F566AE658045407ED6015EE1B2A88)", 131) == 0);
}
}
/* redo the computation, but directly with the resulting ae and ge coefficients: */
secp256k1_gej_t x2; secp256k1_ecmult(&x2, &a, &ae, &ge);
char res[132]; int resl = 132;
char res2[132]; int resl2 = 132;
secp256k1_gej_get_hex(res, &resl, &x);
secp256k1_gej_get_hex(res2, &resl2, &x2);
CHECK(strcmp(res, res2) == 0);
CHECK(strlen(res) == 131);
char res[131];
char res2[131];
secp256k1_gej_get_hex(res, &x);
secp256k1_gej_get_hex(res2, &x2);
CHECK(memcmp(res, res2, 131) == 0);
}

void test_point_times_order(const secp256k1_gej_t *point) {
Expand All @@ -1015,7 +1014,7 @@ void test_point_times_order(const secp256k1_gej_t *point) {
}

void run_point_times_order(void) {
secp256k1_fe_t x; VERIFY_CHECK(secp256k1_fe_set_hex(&x, "02", 2));
secp256k1_fe_t x; VERIFY_CHECK(secp256k1_fe_set_hex(&x, "0000000000000000000000000000000000000000000000000000000000000002"));
for (int i=0; i<500; i++) {
secp256k1_ge_t p;
if (secp256k1_ge_set_xo_var(&p, &x, 1)) {
Expand All @@ -1027,14 +1026,9 @@ void run_point_times_order(void) {
}
secp256k1_fe_sqr(&x, &x);
}
char c[65];
int cl = 1;
c[1] = 123;
secp256k1_fe_get_hex(c, &cl, &x); /* Check that fe_get_hex handles a too short input. */
CHECK(c[1] == 123);
cl = 65;
secp256k1_fe_get_hex(c, &cl, &x);
CHECK(strcmp(c, "7603CB59B0EF6C63FE6084792A0C378CDB3233A80F8A9A09A877DEAD31B38C45") == 0);
char c[64];
secp256k1_fe_get_hex(c, &x);
CHECK(memcmp(c, "7603CB59B0EF6C63FE6084792A0C378CDB3233A80F8A9A09A877DEAD31B38C45", 64) == 0);
}

void test_wnaf(const secp256k1_scalar_t *number, int w) {
Expand Down