Skip to content

Commit 788038d

Browse files
committed
Use size_t for lengths (at least in external API)
1 parent c9d7c2a commit 788038d

File tree

12 files changed

+50
-44
lines changed

12 files changed

+50
-44
lines changed

include/secp256k1.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
extern "C" {
66
# endif
77

8+
#include <stddef.h>
9+
810
/* These rules specify the order of arguments in API calls:
911
*
1012
* 1. Context pointers go first, followed by output arguments, combined
@@ -228,7 +230,7 @@ SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_parse(
228230
const secp256k1_context_t* ctx,
229231
secp256k1_pubkey_t* pubkey,
230232
const unsigned char *input,
231-
int inputlen
233+
size_t inputlen
232234
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
233235

234236
/** Serialize a pubkey object into a serialized byte sequence.
@@ -246,7 +248,7 @@ SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_parse(
246248
int secp256k1_ec_pubkey_serialize(
247249
const secp256k1_context_t* ctx,
248250
unsigned char *output,
249-
int *outputlen,
251+
size_t *outputlen,
250252
const secp256k1_pubkey_t* pubkey,
251253
int compressed
252254
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
@@ -265,7 +267,7 @@ int secp256k1_ecdsa_signature_parse_der(
265267
const secp256k1_context_t* ctx,
266268
secp256k1_ecdsa_signature_t* sig,
267269
const unsigned char *input,
268-
int inputlen
270+
size_t inputlen
269271
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
270272

271273
/** Serialize an ECDSA signature in DER format.
@@ -282,7 +284,7 @@ int secp256k1_ecdsa_signature_parse_der(
282284
int secp256k1_ecdsa_signature_serialize_der(
283285
const secp256k1_context_t* ctx,
284286
unsigned char *output,
285-
int *outputlen,
287+
size_t *outputlen,
286288
const secp256k1_ecdsa_signature_t* sig
287289
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
288290

@@ -406,7 +408,7 @@ SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_create(
406408
SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_export(
407409
const secp256k1_context_t* ctx,
408410
unsigned char *privkey,
409-
int *privkeylen,
411+
size_t *privkeylen,
410412
const unsigned char *seckey,
411413
int compressed
412414
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
@@ -429,7 +431,7 @@ SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_import(
429431
const secp256k1_context_t* ctx,
430432
unsigned char *seckey,
431433
const unsigned char *privkey,
432-
int privkeylen
434+
size_t privkeylen
433435
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
434436

435437
/** Tweak a private key by adding tweak to it.

src/bench_recover.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ void bench_recover(void* arg) {
2323

2424
for (i = 0; i < 20000; i++) {
2525
int j;
26-
int pubkeylen = 33;
26+
size_t pubkeylen = 33;
2727
secp256k1_ecdsa_recoverable_signature_t sig;
2828
CHECK(secp256k1_ecdsa_recoverable_signature_parse_compact(data->ctx, &sig, data->sig, i % 2));
2929
CHECK(secp256k1_ecdsa_recover(data->ctx, &pubkey, &sig, data->msg));

src/bench_schnorr_verify.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ typedef struct {
1616
unsigned char key[32];
1717
unsigned char sig[64];
1818
unsigned char pubkey[33];
19-
int pubkeylen;
19+
size_t pubkeylen;
2020
} benchmark_schnorr_sig_t;
2121

2222
typedef struct {

src/bench_sign.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ static void bench_sign(void* arg) {
2828

2929
unsigned char sig[74];
3030
for (i = 0; i < 20000; i++) {
31-
int siglen = 74;
31+
size_t siglen = 74;
3232
int j;
3333
secp256k1_ecdsa_signature_t signature;
3434
CHECK(secp256k1_ecdsa_sign(data->ctx, &signature, data->msg, data->key, NULL, NULL));

src/bench_verify.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ typedef struct {
1616
unsigned char msg[32];
1717
unsigned char key[32];
1818
unsigned char sig[72];
19-
int siglen;
19+
size_t siglen;
2020
unsigned char pubkey[33];
21-
int pubkeylen;
21+
size_t pubkeylen;
2222
} benchmark_verify_t;
2323

2424
static void benchmark_verify(void* arg) {

src/ecdsa.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
#ifndef _SECP256K1_ECDSA_
88
#define _SECP256K1_ECDSA_
99

10+
#include <stddef.h>
11+
1012
#include "scalar.h"
1113
#include "group.h"
1214
#include "ecmult.h"
1315

14-
static int secp256k1_ecdsa_sig_parse(secp256k1_scalar_t *r, secp256k1_scalar_t *s, const unsigned char *sig, int size);
15-
static int secp256k1_ecdsa_sig_serialize(unsigned char *sig, int *size, const secp256k1_scalar_t *r, const secp256k1_scalar_t *s);
16+
static int secp256k1_ecdsa_sig_parse(secp256k1_scalar_t *r, secp256k1_scalar_t *s, const unsigned char *sig, size_t size);
17+
static int secp256k1_ecdsa_sig_serialize(unsigned char *sig, size_t *size, const secp256k1_scalar_t *r, const secp256k1_scalar_t *s);
1618
static int secp256k1_ecdsa_sig_verify(const secp256k1_ecmult_context_t *ctx, const secp256k1_scalar_t* r, const secp256k1_scalar_t* s, const secp256k1_ge_t *pubkey, const secp256k1_scalar_t *message);
1719
static int secp256k1_ecdsa_sig_sign(const secp256k1_ecmult_gen_context_t *ctx, secp256k1_scalar_t* r, secp256k1_scalar_t* s, const secp256k1_scalar_t *seckey, const secp256k1_scalar_t *message, const secp256k1_scalar_t *nonce, int *recid);
1820
static int secp256k1_ecdsa_sig_recover(const secp256k1_ecmult_context_t *ctx, const secp256k1_scalar_t* r, const secp256k1_scalar_t* s, secp256k1_ge_t *pubkey, const secp256k1_scalar_t *message, int recid);

src/ecdsa_impl.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ static const secp256k1_fe_t secp256k1_ecdsa_const_p_minus_order = SECP256K1_FE_C
4646
0, 0, 0, 1, 0x45512319UL, 0x50B75FC4UL, 0x402DA172UL, 0x2FC9BAEEUL
4747
);
4848

49-
static int secp256k1_ecdsa_sig_parse(secp256k1_scalar_t *rr, secp256k1_scalar_t *rs, const unsigned char *sig, int size) {
49+
static int secp256k1_ecdsa_sig_parse(secp256k1_scalar_t *rr, secp256k1_scalar_t *rs, const unsigned char *sig, size_t size) {
5050
unsigned char ra[32] = {0}, sa[32] = {0};
5151
const unsigned char *rp;
5252
const unsigned char *sp;
53-
int lenr;
54-
int lens;
53+
size_t lenr;
54+
size_t lens;
5555
int overflow;
5656
if (sig[0] != 0x30) {
5757
return 0;
@@ -109,10 +109,10 @@ static int secp256k1_ecdsa_sig_parse(secp256k1_scalar_t *rr, secp256k1_scalar_t
109109
return 1;
110110
}
111111

112-
static int secp256k1_ecdsa_sig_serialize(unsigned char *sig, int *size, const secp256k1_scalar_t* ar, const secp256k1_scalar_t* as) {
112+
static int secp256k1_ecdsa_sig_serialize(unsigned char *sig, size_t *size, const secp256k1_scalar_t* ar, const secp256k1_scalar_t* as) {
113113
unsigned char r[33] = {0}, s[33] = {0};
114114
unsigned char *rp = r, *sp = s;
115-
int lenR = 33, lenS = 33;
115+
size_t lenR = 33, lenS = 33;
116116
secp256k1_scalar_get_b32(&r[1], ar);
117117
secp256k1_scalar_get_b32(&s[1], as);
118118
while (lenR > 1 && rp[0] == 0 && rp[1] < 0x80) { lenR--; rp++; }

src/eckey.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@
77
#ifndef _SECP256K1_ECKEY_
88
#define _SECP256K1_ECKEY_
99

10+
#include <stddef.h>
11+
1012
#include "group.h"
1113
#include "scalar.h"
1214
#include "ecmult.h"
1315
#include "ecmult_gen.h"
1416

15-
static int secp256k1_eckey_pubkey_parse(secp256k1_ge_t *elem, const unsigned char *pub, int size);
16-
static int secp256k1_eckey_pubkey_serialize(secp256k1_ge_t *elem, unsigned char *pub, int *size, int compressed);
17+
static int secp256k1_eckey_pubkey_parse(secp256k1_ge_t *elem, const unsigned char *pub, size_t size);
18+
static int secp256k1_eckey_pubkey_serialize(secp256k1_ge_t *elem, unsigned char *pub, size_t *size, int compressed);
1719

18-
static int secp256k1_eckey_privkey_parse(secp256k1_scalar_t *key, const unsigned char *privkey, int privkeylen);
19-
static int secp256k1_eckey_privkey_serialize(const secp256k1_ecmult_gen_context_t *ctx, unsigned char *privkey, int *privkeylen, const secp256k1_scalar_t *key, int compressed);
20+
static int secp256k1_eckey_privkey_parse(secp256k1_scalar_t *key, const unsigned char *privkey, size_t privkeylen);
21+
static int secp256k1_eckey_privkey_serialize(const secp256k1_ecmult_gen_context_t *ctx, unsigned char *privkey, size_t *privkeylen, const secp256k1_scalar_t *key, int compressed);
2022

2123
static int secp256k1_eckey_privkey_tweak_add(secp256k1_scalar_t *key, const secp256k1_scalar_t *tweak);
2224
static int secp256k1_eckey_pubkey_tweak_add(const secp256k1_ecmult_context_t *ctx, secp256k1_ge_t *key, const secp256k1_scalar_t *tweak);

src/eckey_impl.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include "group.h"
1515
#include "ecmult_gen.h"
1616

17-
static int secp256k1_eckey_pubkey_parse(secp256k1_ge_t *elem, const unsigned char *pub, int size) {
17+
static int secp256k1_eckey_pubkey_parse(secp256k1_ge_t *elem, const unsigned char *pub, size_t size) {
1818
if (size == 33 && (pub[0] == 0x02 || pub[0] == 0x03)) {
1919
secp256k1_fe_t x;
2020
return secp256k1_fe_set_b32(&x, pub+1) && secp256k1_ge_set_xo_var(elem, &x, pub[0] == 0x03);
@@ -33,7 +33,7 @@ static int secp256k1_eckey_pubkey_parse(secp256k1_ge_t *elem, const unsigned cha
3333
}
3434
}
3535

36-
static int secp256k1_eckey_pubkey_serialize(secp256k1_ge_t *elem, unsigned char *pub, int *size, int compressed) {
36+
static int secp256k1_eckey_pubkey_serialize(secp256k1_ge_t *elem, unsigned char *pub, size_t *size, int compressed) {
3737
if (secp256k1_ge_is_infinity(elem)) {
3838
return 0;
3939
}
@@ -51,7 +51,7 @@ static int secp256k1_eckey_pubkey_serialize(secp256k1_ge_t *elem, unsigned char
5151
return 1;
5252
}
5353

54-
static int secp256k1_eckey_privkey_parse(secp256k1_scalar_t *key, const unsigned char *privkey, int privkeylen) {
54+
static int secp256k1_eckey_privkey_parse(secp256k1_scalar_t *key, const unsigned char *privkey, size_t privkeylen) {
5555
unsigned char c[32] = {0};
5656
const unsigned char *end = privkey + privkeylen;
5757
int lenb = 0;
@@ -94,10 +94,10 @@ static int secp256k1_eckey_privkey_parse(secp256k1_scalar_t *key, const unsigned
9494
return !overflow;
9595
}
9696

97-
static int secp256k1_eckey_privkey_serialize(const secp256k1_ecmult_gen_context_t *ctx, unsigned char *privkey, int *privkeylen, const secp256k1_scalar_t *key, int compressed) {
97+
static int secp256k1_eckey_privkey_serialize(const secp256k1_ecmult_gen_context_t *ctx, unsigned char *privkey, size_t *privkeylen, const secp256k1_scalar_t *key, int compressed) {
9898
secp256k1_gej_t rp;
9999
secp256k1_ge_t r;
100-
int pubkeylen = 0;
100+
size_t pubkeylen = 0;
101101
secp256k1_ecmult_gen(ctx, &rp, key);
102102
secp256k1_ge_set_gej(&r, &rp);
103103
if (compressed) {

src/modules/ecdh/tests_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ void test_ecdh_generator_basepoint(void) {
2020
unsigned char output_ecdh[32];
2121
unsigned char output_ser[32];
2222
unsigned char point_ser[33];
23-
int point_ser_len = sizeof(point_ser);
23+
size_t point_ser_len = sizeof(point_ser);
2424
secp256k1_scalar_t s;
2525

2626
random_scalar_order(&s);

0 commit comments

Comments
 (0)