Skip to content

Commit 0768bd5

Browse files
committed
Get rid of variable-length hex string conversions
1 parent e84e761 commit 0768bd5

File tree

5 files changed

+36
-64
lines changed

5 files changed

+36
-64
lines changed

src/field.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,11 @@ static void secp256k1_fe_inv_var(secp256k1_fe_t *r, const secp256k1_fe_t *a);
104104
* outputs must not overlap in memory. */
105105
static void secp256k1_fe_inv_all_var(size_t len, secp256k1_fe_t *r, const secp256k1_fe_t *a);
106106

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

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

113113
/** If flag is true, set *r equal to *a; otherwise leave it. Constant-time. */
114114
static void secp256k1_fe_cmov(secp256k1_fe_t *r, const secp256k1_fe_t *a, int flag);

src/field_impl.h

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,23 @@
2121
#error "Please select field implementation"
2222
#endif
2323

24-
static void secp256k1_fe_get_hex(char *r, int *rlen, const secp256k1_fe_t *a) {
24+
static void secp256k1_fe_get_hex(char *r64, const secp256k1_fe_t *a) {
2525
secp256k1_fe_t b;
2626
int i;
2727
unsigned char tmp[32];
28-
if (*rlen < 65) {
29-
*rlen = 65;
30-
return;
31-
}
32-
*rlen = 65;
3328
b = *a;
3429
secp256k1_fe_normalize(&b);
3530
secp256k1_fe_get_b32(tmp, &b);
3631
for (i=0; i<32; i++) {
3732
static const char *c = "0123456789ABCDEF";
38-
r[2*i] = c[(tmp[i] >> 4) & 0xF];
39-
r[2*i+1] = c[(tmp[i]) & 0xF];
33+
r64[2*i] = c[(tmp[i] >> 4) & 0xF];
34+
r64[2*i+1] = c[(tmp[i]) & 0xF];
4035
}
41-
r[64] = 0x00;
4236
}
4337

44-
static int secp256k1_fe_set_hex(secp256k1_fe_t *r, const char *a, int alen) {
38+
static int secp256k1_fe_set_hex(secp256k1_fe_t *r, const char *a64) {
4539
int i;
46-
unsigned char tmp[32] = {0};
40+
unsigned char tmp[32];
4741
static const int cvt[256] = {0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
4842
0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
4943
0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
@@ -61,8 +55,7 @@ static int secp256k1_fe_set_hex(secp256k1_fe_t *r, const char *a, int alen) {
6155
0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
6256
0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0};
6357
for (i=0; i<32; i++) {
64-
if (alen > i*2)
65-
tmp[32 - alen/2 + i] = (cvt[(unsigned char)a[2*i]] << 4) + cvt[(unsigned char)a[2*i+1]];
58+
tmp[i] = (cvt[(unsigned char)a64[2*i]] << 4) + cvt[(unsigned char)a64[2*i+1]];
6659
}
6760
return secp256k1_fe_set_b32(r, tmp);
6861
}

src/group.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ static int secp256k1_ge_is_valid_var(const secp256k1_ge_t *a);
4343

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

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

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

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

9191
#ifdef USE_ENDOMORPHISM
9292
/** Set r to be equal to lambda times a, where lambda is chosen in a way such that this is very fast. */

src/group_impl.h

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,12 @@ static void secp256k1_ge_neg(secp256k1_ge_t *r, const secp256k1_ge_t *a) {
4545
secp256k1_fe_negate(&r->y, &r->y, 1);
4646
}
4747

48-
static void secp256k1_ge_get_hex(char *r, int *rlen, const secp256k1_ge_t *a) {
49-
char cx[65]; int lx=65;
50-
char cy[65]; int ly=65;
51-
secp256k1_fe_get_hex(cx, &lx, &a->x);
52-
secp256k1_fe_get_hex(cy, &ly, &a->y);
53-
lx = strlen(cx);
54-
ly = strlen(cy);
55-
int len = lx + ly + 3 + 1;
56-
if (*rlen < len) {
57-
*rlen = len;
58-
return;
59-
}
60-
*rlen = len;
61-
r[0] = '(';
62-
memcpy(r+1, cx, lx);
63-
r[1+lx] = ',';
64-
memcpy(r+2+lx, cy, ly);
65-
r[2+lx+ly] = ')';
66-
r[3+lx+ly] = 0;
48+
static void secp256k1_ge_get_hex(char *r131, const secp256k1_ge_t *a) {
49+
r131[0] = '(';
50+
secp256k1_fe_get_hex(r131 + 1, &a->x);
51+
r131[65] = ',';
52+
secp256k1_fe_get_hex(r131 + 66, &a->y);
53+
r131[130] = ')';
6754
}
6855

6956
static void secp256k1_ge_set_gej(secp256k1_ge_t *r, secp256k1_gej_t *a) {
@@ -399,12 +386,10 @@ static void secp256k1_gej_add_ge(secp256k1_gej_t *r, const secp256k1_gej_t *a, c
399386
r->infinity = infinity;
400387
}
401388

402-
403-
404-
static void secp256k1_gej_get_hex(char *r, int *rlen, const secp256k1_gej_t *a) {
389+
static void secp256k1_gej_get_hex(char *r131, const secp256k1_gej_t *a) {
405390
secp256k1_gej_t c = *a;
406391
secp256k1_ge_t t; secp256k1_ge_set_gej(&t, &c);
407-
secp256k1_ge_get_hex(r, rlen, &t);
392+
secp256k1_ge_get_hex(r131, &t);
408393
}
409394

410395
#ifdef USE_ENDOMORPHISM

src/tests.c

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -928,8 +928,8 @@ void run_ge(void) {
928928

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

977977
/* verify */
978978
if (i == 19999) {
979-
char res[132]; int resl = 132;
980-
secp256k1_gej_get_hex(res, &resl, &x);
981-
CHECK(strcmp(res, "(D6E96687F9B10D092A6F35439D86CEBEA4535D0D409F53586440BD74B933E830,B95CBCA2C77DA786539BE8FD53354D2D3B4F566AE658045407ED6015EE1B2A88)") == 0);
979+
char res[131];
980+
secp256k1_gej_get_hex(res, &x);
981+
CHECK(memcmp(res, "(D6E96687F9B10D092A6F35439D86CEBEA4535D0D409F53586440BD74B933E830,B95CBCA2C77DA786539BE8FD53354D2D3B4F566AE658045407ED6015EE1B2A88)", 131) == 0);
982982
}
983983
}
984984
/* redo the computation, but directly with the resulting ae and ge coefficients: */
985985
secp256k1_gej_t x2; secp256k1_ecmult(&x2, &a, &ae, &ge);
986-
char res[132]; int resl = 132;
987-
char res2[132]; int resl2 = 132;
988-
secp256k1_gej_get_hex(res, &resl, &x);
989-
secp256k1_gej_get_hex(res2, &resl2, &x2);
990-
CHECK(strcmp(res, res2) == 0);
991-
CHECK(strlen(res) == 131);
986+
char res[131];
987+
char res2[131];
988+
secp256k1_gej_get_hex(res, &x);
989+
secp256k1_gej_get_hex(res2, &x2);
990+
CHECK(memcmp(res, res2, 131) == 0);
992991
}
993992

994993
void test_point_times_order(const secp256k1_gej_t *point) {
@@ -1015,7 +1014,7 @@ void test_point_times_order(const secp256k1_gej_t *point) {
10151014
}
10161015

10171016
void run_point_times_order(void) {
1018-
secp256k1_fe_t x; VERIFY_CHECK(secp256k1_fe_set_hex(&x, "02", 2));
1017+
secp256k1_fe_t x; VERIFY_CHECK(secp256k1_fe_set_hex(&x, "0000000000000000000000000000000000000000000000000000000000000002"));
10191018
for (int i=0; i<500; i++) {
10201019
secp256k1_ge_t p;
10211020
if (secp256k1_ge_set_xo_var(&p, &x, 1)) {
@@ -1027,14 +1026,9 @@ void run_point_times_order(void) {
10271026
}
10281027
secp256k1_fe_sqr(&x, &x);
10291028
}
1030-
char c[65];
1031-
int cl = 1;
1032-
c[1] = 123;
1033-
secp256k1_fe_get_hex(c, &cl, &x); /* Check that fe_get_hex handles a too short input. */
1034-
CHECK(c[1] == 123);
1035-
cl = 65;
1036-
secp256k1_fe_get_hex(c, &cl, &x);
1037-
CHECK(strcmp(c, "7603CB59B0EF6C63FE6084792A0C378CDB3233A80F8A9A09A877DEAD31B38C45") == 0);
1029+
char c[64];
1030+
secp256k1_fe_get_hex(c, &x);
1031+
CHECK(memcmp(c, "7603CB59B0EF6C63FE6084792A0C378CDB3233A80F8A9A09A877DEAD31B38C45", 64) == 0);
10381032
}
10391033

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

0 commit comments

Comments
 (0)