Skip to content
This repository was archived by the owner on Sep 25, 2024. It is now read-only.

Commit b0d0266

Browse files
committed
Use BN_new() versus BN_init() with static BIGNUM
Fixes #4
1 parent 2ec8f2f commit b0d0266

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

base58.h

+12-12
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ char *bbp_base58(const uint8_t *bytes, size_t len) {
1212
size_t str_len;
1313
char *str;
1414
BN_CTX *ctx;
15-
BIGNUM base, x, r;
15+
BIGNUM *base, *x, *r;
1616
int i, j;
1717

1818
str_len = len * 138 / 100 + 2;
@@ -21,16 +21,16 @@ char *bbp_base58(const uint8_t *bytes, size_t len) {
2121
ctx = BN_CTX_new();
2222
BN_CTX_start(ctx);
2323

24-
BN_init(&base);
25-
BN_init(&x);
26-
BN_init(&r);
27-
BN_set_word(&base, 58);
28-
BN_bin2bn(bytes, len, &x);
24+
base = BN_new();
25+
x = BN_new();
26+
r = BN_new();
27+
BN_set_word(base, 58);
28+
BN_bin2bn(bytes, len, x);
2929

3030
i = 0;
31-
while (!BN_is_zero(&x)) {
32-
BN_div(&x, &r, &x, &base, ctx);
33-
str[i] = bbp_base58_alphabet[BN_get_word(&r)];
31+
while (!BN_is_zero(x)) {
32+
BN_div(x, r, x, base, ctx);
33+
str[i] = bbp_base58_alphabet[BN_get_word(r)];
3434
++i;
3535
}
3636
for (j = 0; j < len; ++j) {
@@ -42,9 +42,9 @@ char *bbp_base58(const uint8_t *bytes, size_t len) {
4242
}
4343
bbp_reverse((uint8_t *)str, i);
4444

45-
BN_clear_free(&r);
46-
BN_clear_free(&x);
47-
BN_free(&base);
45+
BN_clear_free(r);
46+
BN_clear_free(x);
47+
BN_free(base);
4848
BN_CTX_end(ctx);
4949
BN_CTX_free(ctx);
5050

ec.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
EC_KEY *bbp_ec_new_keypair(const uint8_t *priv_bytes) {
1010
EC_KEY *key;
11-
BIGNUM priv;
11+
BIGNUM *priv;
1212
BN_CTX *ctx;
1313
const EC_GROUP *group;
1414
EC_POINT *pub;
@@ -19,9 +19,9 @@ EC_KEY *bbp_ec_new_keypair(const uint8_t *priv_bytes) {
1919

2020
/* set private key through BIGNUM */
2121

22-
BN_init(&priv);
23-
BN_bin2bn(priv_bytes, 32, &priv);
24-
EC_KEY_set_private_key(key, &priv);
22+
priv = BN_new();
23+
BN_bin2bn(priv_bytes, 32, priv);
24+
EC_KEY_set_private_key(key, priv);
2525

2626
/* derive public key from private key and group */
2727

@@ -30,15 +30,15 @@ EC_KEY *bbp_ec_new_keypair(const uint8_t *priv_bytes) {
3030

3131
group = EC_KEY_get0_group(key);
3232
pub = EC_POINT_new(group);
33-
EC_POINT_mul(group, pub, &priv, NULL, NULL, ctx);
33+
EC_POINT_mul(group, pub, priv, NULL, NULL, ctx);
3434
EC_KEY_set_public_key(key, pub);
3535

3636
/* release resources */
3737

3838
EC_POINT_free(pub);
3939
BN_CTX_end(ctx);
4040
BN_CTX_free(ctx);
41-
BN_clear_free(&priv);
41+
BN_clear_free(priv);
4242

4343
return key;
4444
}

0 commit comments

Comments
 (0)