mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-11-12 18:49:07 +00:00
Use hasher_Raw instead of sha256_Raw
This commit is contained in:
parent
a70caa3dbc
commit
b41a51805f
16
base58.c
16
base58.c
@ -123,15 +123,15 @@ bool b58tobin(void *bin, size_t *binszp, const char *b58)
|
||||
return true;
|
||||
}
|
||||
|
||||
int b58check(const void *bin, size_t binsz, const char *base58str)
|
||||
int b58check(const void *bin, size_t binsz, HasherType hasher_type, const char *base58str)
|
||||
{
|
||||
unsigned char buf[32];
|
||||
const uint8_t *binc = bin;
|
||||
unsigned i;
|
||||
if (binsz < 4)
|
||||
return -4;
|
||||
sha256_Raw(bin, binsz - 4, buf);
|
||||
sha256_Raw(buf, 32, buf);
|
||||
hasher_Raw(hasher_type, bin, binsz - 4, buf);
|
||||
hasher_Raw(hasher_type, buf, 32, buf);
|
||||
if (memcmp(&binc[binsz - 4], buf, 4))
|
||||
return -1;
|
||||
|
||||
@ -188,7 +188,7 @@ bool b58enc(char *b58, size_t *b58sz, const void *data, size_t binsz)
|
||||
return true;
|
||||
}
|
||||
|
||||
int base58_encode_check(const uint8_t *data, int datalen, char *str, int strsize)
|
||||
int base58_encode_check(const uint8_t *data, int datalen, HasherType hasher_type, char *str, int strsize)
|
||||
{
|
||||
if (datalen > 128) {
|
||||
return 0;
|
||||
@ -196,15 +196,15 @@ int base58_encode_check(const uint8_t *data, int datalen, char *str, int strsize
|
||||
uint8_t buf[datalen + 32];
|
||||
uint8_t *hash = buf + datalen;
|
||||
memcpy(buf, data, datalen);
|
||||
sha256_Raw(data, datalen, hash);
|
||||
sha256_Raw(hash, 32, hash);
|
||||
hasher_Raw(hasher_type, data, datalen, hash);
|
||||
hasher_Raw(hasher_type, hash, 32, hash);
|
||||
size_t res = strsize;
|
||||
bool success = b58enc(str, &res, buf, datalen + 4);
|
||||
MEMSET_BZERO(buf, sizeof(buf));
|
||||
return success ? res : 0;
|
||||
}
|
||||
|
||||
int base58_decode_check(const char *str, uint8_t *data, int datalen)
|
||||
int base58_decode_check(const char *str, HasherType hasher_type, uint8_t *data, int datalen)
|
||||
{
|
||||
if (datalen > 128) {
|
||||
return 0;
|
||||
@ -215,7 +215,7 @@ int base58_decode_check(const char *str, uint8_t *data, int datalen)
|
||||
return 0;
|
||||
}
|
||||
uint8_t *nd = d + datalen + 4 - res;
|
||||
if (b58check(nd, res, str) < 0) {
|
||||
if (b58check(nd, res, hasher_type, str) < 0) {
|
||||
return 0;
|
||||
}
|
||||
memcpy(data, nd, res - 4);
|
||||
|
7
base58.h
7
base58.h
@ -26,14 +26,15 @@
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "hasher.h"
|
||||
#include "options.h"
|
||||
|
||||
int base58_encode_check(const uint8_t *data, int len, char *str, int strsize);
|
||||
int base58_decode_check(const char *str, uint8_t *data, int datalen);
|
||||
int base58_encode_check(const uint8_t *data, int len, HasherType hasher_type, char *str, int strsize);
|
||||
int base58_decode_check(const char *str, HasherType hasher_type, uint8_t *data, int datalen);
|
||||
|
||||
// Private
|
||||
bool b58tobin(void *bin, size_t *binszp, const char *b58);
|
||||
int b58check(const void *bin, size_t binsz, const char *base58str);
|
||||
int b58check(const void *bin, size_t binsz, HasherType hasher_type, const char *base58str);
|
||||
bool b58enc(char *b58, size_t *b58sz, const void *data, size_t binsz);
|
||||
|
||||
#if USE_GRAPHENE
|
||||
|
44
bip32.c
44
bip32.c
@ -49,29 +49,29 @@
|
||||
#endif
|
||||
|
||||
const curve_info ed25519_info = {
|
||||
/* bip32_name */
|
||||
"ed25519 seed",
|
||||
0
|
||||
.bip32_name = "ed25519 seed",
|
||||
.params = NULL,
|
||||
.hasher_type = HASHER_SHA2,
|
||||
};
|
||||
|
||||
const curve_info ed25519_sha3_info = {
|
||||
/* bip32_name */
|
||||
"ed25519-sha3 seed",
|
||||
0
|
||||
.bip32_name = "ed25519-sha3 seed",
|
||||
.params = NULL,
|
||||
.hasher_type = HASHER_SHA2,
|
||||
};
|
||||
|
||||
#if USE_KECCAK
|
||||
const curve_info ed25519_keccak_info = {
|
||||
/* bip32_name */
|
||||
"ed25519-keccak seed",
|
||||
0
|
||||
.bip32_name = "ed25519-keccak seed",
|
||||
.params = NULL,
|
||||
.hasher_type = HASHER_SHA2,
|
||||
};
|
||||
#endif
|
||||
|
||||
const curve_info curve25519_info = {
|
||||
/* bip32_name */
|
||||
"curve25519 seed",
|
||||
0
|
||||
.bip32_name = "curve25519 seed",
|
||||
.params = NULL,
|
||||
.hasher_type = HASHER_SHA2,
|
||||
};
|
||||
|
||||
int hdnode_from_xpub(uint32_t depth, uint32_t child_num, const uint8_t *chain_code, const uint8_t *public_key, const char* curve, HDNode *out)
|
||||
@ -166,7 +166,7 @@ uint32_t hdnode_fingerprint(HDNode *node)
|
||||
uint32_t fingerprint;
|
||||
|
||||
hdnode_fill_public_key(node);
|
||||
sha256_Raw(node->public_key, 33, digest);
|
||||
hasher_Raw(node->curve->hasher_type, node->public_key, 33, digest);
|
||||
ripemd160(digest, 32, digest);
|
||||
fingerprint = (digest[0] << 24) + (digest[1] << 16) + (digest[2] << 8) + digest[3];
|
||||
MEMSET_BZERO(digest, sizeof(digest));
|
||||
@ -300,7 +300,7 @@ int hdnode_public_ckd(HDNode *inout, uint32_t i)
|
||||
return 1;
|
||||
}
|
||||
|
||||
void hdnode_public_ckd_address_optimized(const curve_point *pub, const uint8_t *chain_code, uint32_t i, uint32_t version, char *addr, int addrsize, int addrformat)
|
||||
void hdnode_public_ckd_address_optimized(const curve_point *pub, const uint8_t *chain_code, uint32_t i, uint32_t version, HasherType hasher_type, char *addr, int addrsize, int addrformat)
|
||||
{
|
||||
uint8_t child_pubkey[33];
|
||||
curve_point b;
|
||||
@ -311,10 +311,10 @@ void hdnode_public_ckd_address_optimized(const curve_point *pub, const uint8_t *
|
||||
|
||||
switch (addrformat) {
|
||||
case 1: // Segwit-in-P2SH
|
||||
ecdsa_get_address_segwit_p2sh(child_pubkey, version, addr, addrsize);
|
||||
ecdsa_get_address_segwit_p2sh(child_pubkey, version, hasher_type, addr, addrsize);
|
||||
break;
|
||||
default: // normal address
|
||||
ecdsa_get_address(child_pubkey, version, addr, addrsize);
|
||||
ecdsa_get_address(child_pubkey, version, hasher_type, addr, addrsize);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -396,13 +396,13 @@ int hdnode_private_ckd_cached(HDNode *inout, const uint32_t *i, size_t i_count,
|
||||
void hdnode_get_address_raw(HDNode *node, uint32_t version, uint8_t *addr_raw)
|
||||
{
|
||||
hdnode_fill_public_key(node);
|
||||
ecdsa_get_address_raw(node->public_key, version, addr_raw);
|
||||
ecdsa_get_address_raw(node->public_key, version, node->curve->hasher_type, addr_raw);
|
||||
}
|
||||
|
||||
void hdnode_get_address(HDNode *node, uint32_t version, char *addr, int addrsize)
|
||||
{
|
||||
hdnode_fill_public_key(node);
|
||||
ecdsa_get_address(node->public_key, version, addr, addrsize);
|
||||
ecdsa_get_address(node->public_key, version, node->curve->hasher_type, addr, addrsize);
|
||||
}
|
||||
|
||||
void hdnode_fill_public_key(HDNode *node)
|
||||
@ -543,7 +543,7 @@ int hdnode_nem_decrypt(const HDNode *node, const ed25519_public_key public_key,
|
||||
int hdnode_sign(HDNode *node, const uint8_t *msg, uint32_t msg_len, uint8_t *sig, uint8_t *pby, int (*is_canonical)(uint8_t by, uint8_t sig[64]))
|
||||
{
|
||||
if (node->curve->params) {
|
||||
return ecdsa_sign(node->curve->params, node->private_key, msg, msg_len, sig, pby, is_canonical);
|
||||
return ecdsa_sign(node->curve->params, node->curve->hasher_type, node->private_key, msg, msg_len, sig, pby, is_canonical);
|
||||
} else if (node->curve == &curve25519_info) {
|
||||
return 1; // signatures are not supported
|
||||
} else {
|
||||
@ -609,7 +609,7 @@ static int hdnode_serialize(const HDNode *node, uint32_t fingerprint, uint32_t v
|
||||
node_data[45] = 0;
|
||||
memcpy(node_data + 46, node->private_key, 32);
|
||||
}
|
||||
int ret = base58_encode_check(node_data, sizeof(node_data), str, strsize);
|
||||
int ret = base58_encode_check(node_data, sizeof(node_data), node->curve->hasher_type, str, strsize);
|
||||
MEMSET_BZERO(node_data, sizeof(node_data));
|
||||
return ret;
|
||||
}
|
||||
@ -629,10 +629,10 @@ int hdnode_deserialize(const char *str, uint32_t version_public, uint32_t versio
|
||||
{
|
||||
uint8_t node_data[78];
|
||||
memset(node, 0, sizeof(HDNode));
|
||||
if (base58_decode_check(str, node_data, sizeof(node_data)) != sizeof(node_data)) {
|
||||
node->curve = get_curve_by_name(SECP256K1_NAME);
|
||||
if (base58_decode_check(str, node->curve->hasher_type, node_data, sizeof(node_data)) != sizeof(node_data)) {
|
||||
return -1;
|
||||
}
|
||||
node->curve = get_curve_by_name(SECP256K1_NAME);
|
||||
uint32_t version = read_be(node_data);
|
||||
if (version == version_public) {
|
||||
MEMSET_BZERO(node->private_key, sizeof(node->private_key));
|
||||
|
3
bip32.h
3
bip32.h
@ -34,6 +34,7 @@
|
||||
typedef struct {
|
||||
const char *bip32_name; // string for generating BIP32 xprv from seed
|
||||
const ecdsa_curve *params; // ecdsa curve parameters, null for ed25519
|
||||
HasherType hasher_type; // hasher type for BIP32 and ECDSA
|
||||
} curve_info;
|
||||
|
||||
typedef struct {
|
||||
@ -59,7 +60,7 @@ int hdnode_public_ckd_cp(const ecdsa_curve *curve, const curve_point *parent, co
|
||||
|
||||
int hdnode_public_ckd(HDNode *inout, uint32_t i);
|
||||
|
||||
void hdnode_public_ckd_address_optimized(const curve_point *pub, const uint8_t *chain_code, uint32_t i, uint32_t version, char *addr, int addrsize, int addrformat);
|
||||
void hdnode_public_ckd_address_optimized(const curve_point *pub, const uint8_t *chain_code, uint32_t i, uint32_t version, HasherType hasher_type, char *addr, int addrsize, int addrformat);
|
||||
|
||||
#if USE_BIP32_CACHE
|
||||
int hdnode_private_ckd_cached(HDNode *inout, const uint32_t *i, size_t i_count, uint32_t *fingerprint);
|
||||
|
64
ecdsa.c
64
ecdsa.c
@ -715,10 +715,10 @@ void generate_k_rfc6979(bignum256 *k, rfc6979_state *state)
|
||||
|
||||
// msg is a data to be signed
|
||||
// msg_len is the message length
|
||||
int ecdsa_sign(const ecdsa_curve *curve, const uint8_t *priv_key, const uint8_t *msg, uint32_t msg_len, uint8_t *sig, uint8_t *pby, int (*is_canonical)(uint8_t by, uint8_t sig[64]))
|
||||
int ecdsa_sign(const ecdsa_curve *curve, HasherType hasher_type, const uint8_t *priv_key, const uint8_t *msg, uint32_t msg_len, uint8_t *sig, uint8_t *pby, int (*is_canonical)(uint8_t by, uint8_t sig[64]))
|
||||
{
|
||||
uint8_t hash[32];
|
||||
sha256_Raw(msg, msg_len, hash);
|
||||
hasher_Raw(hasher_type, msg, msg_len, hash);
|
||||
int res = ecdsa_sign_digest(curve, priv_key, hash, sig, pby, is_canonical);
|
||||
MEMSET_BZERO(hash, sizeof(hash));
|
||||
return res;
|
||||
@ -727,11 +727,11 @@ int ecdsa_sign(const ecdsa_curve *curve, const uint8_t *priv_key, const uint8_t
|
||||
|
||||
// msg is a data to be signed
|
||||
// msg_len is the message length
|
||||
int ecdsa_sign_double(const ecdsa_curve *curve, const uint8_t *priv_key, const uint8_t *msg, uint32_t msg_len, uint8_t *sig, uint8_t *pby, int (*is_canonical)(uint8_t by, uint8_t sig[64]))
|
||||
int ecdsa_sign_double(const ecdsa_curve *curve, HasherType hasher_type, const uint8_t *priv_key, const uint8_t *msg, uint32_t msg_len, uint8_t *sig, uint8_t *pby, int (*is_canonical)(uint8_t by, uint8_t sig[64]))
|
||||
{
|
||||
uint8_t hash[32];
|
||||
sha256_Raw(msg, msg_len, hash);
|
||||
sha256_Raw(hash, 32, hash);
|
||||
hasher_Raw(hasher_type, msg, msg_len, hash);
|
||||
hasher_Raw(hasher_type, hash, 32, hash);
|
||||
int res = ecdsa_sign_digest(curve, priv_key, hash, sig, pby, is_canonical);
|
||||
MEMSET_BZERO(hash, sizeof(hash));
|
||||
return res;
|
||||
@ -880,75 +880,75 @@ int ecdsa_uncompress_pubkey(const ecdsa_curve *curve, const uint8_t *pub_key, ui
|
||||
return 1;
|
||||
}
|
||||
|
||||
void ecdsa_get_pubkeyhash(const uint8_t *pub_key, uint8_t *pubkeyhash)
|
||||
void ecdsa_get_pubkeyhash(const uint8_t *pub_key, HasherType hasher_type, uint8_t *pubkeyhash)
|
||||
{
|
||||
uint8_t h[32];
|
||||
uint8_t h[HASHER_DIGEST_LENGTH];
|
||||
if (pub_key[0] == 0x04) { // uncompressed format
|
||||
sha256_Raw(pub_key, 65, h);
|
||||
hasher_Raw(hasher_type, pub_key, 65, h);
|
||||
} else if (pub_key[0] == 0x00) { // point at infinity
|
||||
sha256_Raw(pub_key, 1, h);
|
||||
} else {
|
||||
sha256_Raw(pub_key, 33, h); // expecting compressed format
|
||||
hasher_Raw(hasher_type, pub_key, 1, h);
|
||||
} else { // expecting compressed format
|
||||
hasher_Raw(hasher_type, pub_key, 33, h);
|
||||
}
|
||||
ripemd160(h, 32, pubkeyhash);
|
||||
ripemd160(h, HASHER_DIGEST_LENGTH, pubkeyhash);
|
||||
MEMSET_BZERO(h, sizeof(h));
|
||||
}
|
||||
|
||||
void ecdsa_get_address_raw(const uint8_t *pub_key, uint32_t version, uint8_t *addr_raw)
|
||||
void ecdsa_get_address_raw(const uint8_t *pub_key, uint32_t version, HasherType hasher_type, uint8_t *addr_raw)
|
||||
{
|
||||
size_t prefix_len = address_prefix_bytes_len(version);
|
||||
address_write_prefix_bytes(version, addr_raw);
|
||||
ecdsa_get_pubkeyhash(pub_key, addr_raw + prefix_len);
|
||||
ecdsa_get_pubkeyhash(pub_key, hasher_type, addr_raw + prefix_len);
|
||||
}
|
||||
|
||||
void ecdsa_get_address(const uint8_t *pub_key, uint32_t version, char *addr, int addrsize)
|
||||
void ecdsa_get_address(const uint8_t *pub_key, uint32_t version, HasherType hasher_type, char *addr, int addrsize)
|
||||
{
|
||||
uint8_t raw[MAX_ADDR_RAW_SIZE];
|
||||
size_t prefix_len = address_prefix_bytes_len(version);
|
||||
ecdsa_get_address_raw(pub_key, version, raw);
|
||||
base58_encode_check(raw, 20 + prefix_len, addr, addrsize);
|
||||
ecdsa_get_address_raw(pub_key, version, hasher_type, raw);
|
||||
base58_encode_check(raw, 20 + prefix_len, hasher_type, addr, addrsize);
|
||||
// not as important to clear this one, but we might as well
|
||||
MEMSET_BZERO(raw, sizeof(raw));
|
||||
}
|
||||
|
||||
void ecdsa_get_address_segwit_p2sh_raw(const uint8_t *pub_key, uint32_t version, uint8_t *addr_raw)
|
||||
void ecdsa_get_address_segwit_p2sh_raw(const uint8_t *pub_key, uint32_t version, HasherType hasher_type, uint8_t *addr_raw)
|
||||
{
|
||||
size_t prefix_len = address_prefix_bytes_len(version);
|
||||
uint8_t digest[32];
|
||||
addr_raw[0] = 0; // version byte
|
||||
addr_raw[1] = 20; // push 20 bytes
|
||||
ecdsa_get_pubkeyhash(pub_key, addr_raw + 2);
|
||||
sha256_Raw(addr_raw, 22, digest);
|
||||
ecdsa_get_pubkeyhash(pub_key, hasher_type, addr_raw + 2);
|
||||
hasher_Raw(hasher_type, addr_raw, 22, digest);
|
||||
address_write_prefix_bytes(version, addr_raw);
|
||||
ripemd160(digest, 32, addr_raw + prefix_len);
|
||||
}
|
||||
|
||||
void ecdsa_get_address_segwit_p2sh(const uint8_t *pub_key, uint32_t version, char *addr, int addrsize)
|
||||
void ecdsa_get_address_segwit_p2sh(const uint8_t *pub_key, uint32_t version, HasherType hasher_type, char *addr, int addrsize)
|
||||
{
|
||||
uint8_t raw[MAX_ADDR_RAW_SIZE];
|
||||
size_t prefix_len = address_prefix_bytes_len(version);
|
||||
ecdsa_get_address_segwit_p2sh_raw(pub_key, version, raw);
|
||||
base58_encode_check(raw, prefix_len + 20, addr, addrsize);
|
||||
ecdsa_get_address_segwit_p2sh_raw(pub_key, version, hasher_type, raw);
|
||||
base58_encode_check(raw, prefix_len + 20, hasher_type, addr, addrsize);
|
||||
MEMSET_BZERO(raw, sizeof(raw));
|
||||
}
|
||||
|
||||
void ecdsa_get_wif(const uint8_t *priv_key, uint32_t version, char *wif, int wifsize)
|
||||
void ecdsa_get_wif(const uint8_t *priv_key, uint32_t version, HasherType hasher_type, char *wif, int wifsize)
|
||||
{
|
||||
uint8_t wif_raw[MAX_WIF_RAW_SIZE];
|
||||
size_t prefix_len = address_prefix_bytes_len(version);
|
||||
address_write_prefix_bytes(version, wif_raw);
|
||||
memcpy(wif_raw + prefix_len, priv_key, 32);
|
||||
wif_raw[prefix_len + 32] = 0x01;
|
||||
base58_encode_check(wif_raw, prefix_len + 32 + 1, wif, wifsize);
|
||||
base58_encode_check(wif_raw, prefix_len + 32 + 1, hasher_type, wif, wifsize);
|
||||
// private keys running around our stack can cause trouble
|
||||
MEMSET_BZERO(wif_raw, sizeof(wif_raw));
|
||||
}
|
||||
|
||||
int ecdsa_address_decode(const char *addr, uint32_t version, uint8_t *out)
|
||||
int ecdsa_address_decode(const char *addr, uint32_t version, HasherType hasher_type, uint8_t *out)
|
||||
{
|
||||
if (!addr) return 0;
|
||||
int prefix_len = address_prefix_bytes_len(version);
|
||||
return base58_decode_check(addr, out, 20 + prefix_len) == 20 + prefix_len
|
||||
return base58_decode_check(addr, hasher_type, out, 20 + prefix_len) == 20 + prefix_len
|
||||
&& address_check_prefix(out, version);
|
||||
}
|
||||
|
||||
@ -1029,20 +1029,20 @@ int ecdsa_validate_pubkey(const ecdsa_curve *curve, const curve_point *pub)
|
||||
// msg is a data that was signed
|
||||
// msg_len is the message length
|
||||
|
||||
int ecdsa_verify(const ecdsa_curve *curve, const uint8_t *pub_key, const uint8_t *sig, const uint8_t *msg, uint32_t msg_len)
|
||||
int ecdsa_verify(const ecdsa_curve *curve, HasherType hasher_type, const uint8_t *pub_key, const uint8_t *sig, const uint8_t *msg, uint32_t msg_len)
|
||||
{
|
||||
uint8_t hash[32];
|
||||
sha256_Raw(msg, msg_len, hash);
|
||||
hasher_Raw(hasher_type, msg, msg_len, hash);
|
||||
int res = ecdsa_verify_digest(curve, pub_key, sig, hash);
|
||||
MEMSET_BZERO(hash, sizeof(hash));
|
||||
return res;
|
||||
}
|
||||
|
||||
int ecdsa_verify_double(const ecdsa_curve *curve, const uint8_t *pub_key, const uint8_t *sig, const uint8_t *msg, uint32_t msg_len)
|
||||
int ecdsa_verify_double(const ecdsa_curve *curve, HasherType hasher_type, const uint8_t *pub_key, const uint8_t *sig, const uint8_t *msg, uint32_t msg_len)
|
||||
{
|
||||
uint8_t hash[32];
|
||||
sha256_Raw(msg, msg_len, hash);
|
||||
sha256_Raw(hash, 32, hash);
|
||||
hasher_Raw(hasher_type, msg, msg_len, hash);
|
||||
hasher_Raw(hasher_type, hash, 32, hash);
|
||||
int res = ecdsa_verify_digest(curve, pub_key, sig, hash);
|
||||
MEMSET_BZERO(hash, sizeof(hash));
|
||||
return res;
|
||||
|
23
ecdsa.h
23
ecdsa.h
@ -27,6 +27,7 @@
|
||||
#include <stdint.h>
|
||||
#include "options.h"
|
||||
#include "bignum.h"
|
||||
#include "hasher.h"
|
||||
|
||||
// curve point x and y
|
||||
typedef struct {
|
||||
@ -72,23 +73,23 @@ int ecdh_multiply(const ecdsa_curve *curve, const uint8_t *priv_key, const uint8
|
||||
void uncompress_coords(const ecdsa_curve *curve, uint8_t odd, const bignum256 *x, bignum256 *y);
|
||||
int ecdsa_uncompress_pubkey(const ecdsa_curve *curve, const uint8_t *pub_key, uint8_t *uncompressed);
|
||||
|
||||
int ecdsa_sign(const ecdsa_curve *curve, const uint8_t *priv_key, const uint8_t *msg, uint32_t msg_len, uint8_t *sig, uint8_t *pby, int (*is_canonical)(uint8_t by, uint8_t sig[64]));
|
||||
int ecdsa_sign_double(const ecdsa_curve *curve, const uint8_t *priv_key, const uint8_t *msg, uint32_t msg_len, uint8_t *sig, uint8_t *pby, int (*is_canonical)(uint8_t by, uint8_t sig[64]));
|
||||
int ecdsa_sign(const ecdsa_curve *curve, HasherType hasher_type, const uint8_t *priv_key, const uint8_t *msg, uint32_t msg_len, uint8_t *sig, uint8_t *pby, int (*is_canonical)(uint8_t by, uint8_t sig[64]));
|
||||
int ecdsa_sign_double(const ecdsa_curve *curve, HasherType hasher_type, const uint8_t *priv_key, const uint8_t *msg, uint32_t msg_len, uint8_t *sig, uint8_t *pby, int (*is_canonical)(uint8_t by, uint8_t sig[64]));
|
||||
int ecdsa_sign_digest(const ecdsa_curve *curve, const uint8_t *priv_key, const uint8_t *digest, uint8_t *sig, uint8_t *pby, int (*is_canonical)(uint8_t by, uint8_t sig[64]));
|
||||
void ecdsa_get_public_key33(const ecdsa_curve *curve, const uint8_t *priv_key, uint8_t *pub_key);
|
||||
void ecdsa_get_public_key65(const ecdsa_curve *curve, const uint8_t *priv_key, uint8_t *pub_key);
|
||||
void ecdsa_get_pubkeyhash(const uint8_t *pub_key, uint8_t *pubkeyhash);
|
||||
void ecdsa_get_address_raw(const uint8_t *pub_key, uint32_t version, uint8_t *addr_raw);
|
||||
void ecdsa_get_address(const uint8_t *pub_key, uint32_t version, char *addr, int addrsize);
|
||||
void ecdsa_get_address_segwit_p2sh_raw(const uint8_t *pub_key, uint32_t version, uint8_t *addr_raw);
|
||||
void ecdsa_get_address_segwit_p2sh(const uint8_t *pub_key, uint32_t version, char *addr, int addrsize);
|
||||
void ecdsa_get_wif(const uint8_t *priv_key, uint32_t version, char *wif, int wifsize);
|
||||
void ecdsa_get_pubkeyhash(const uint8_t *pub_key, HasherType hasher_type, uint8_t *pubkeyhash);
|
||||
void ecdsa_get_address_raw(const uint8_t *pub_key, uint32_t version, HasherType hasher_type, uint8_t *addr_raw);
|
||||
void ecdsa_get_address(const uint8_t *pub_key, uint32_t version, HasherType hasher_type, char *addr, int addrsize);
|
||||
void ecdsa_get_address_segwit_p2sh_raw(const uint8_t *pub_key, uint32_t version, HasherType hasher_type, uint8_t *addr_raw);
|
||||
void ecdsa_get_address_segwit_p2sh(const uint8_t *pub_key, uint32_t version, HasherType hasher_type, char *addr, int addrsize);
|
||||
void ecdsa_get_wif(const uint8_t *priv_key, uint32_t version, HasherType hasher_type, char *wif, int wifsize);
|
||||
|
||||
int ecdsa_address_decode(const char *addr, uint32_t version, uint8_t *out);
|
||||
int ecdsa_address_decode(const char *addr, uint32_t version, HasherType hasher_type, uint8_t *out);
|
||||
int ecdsa_read_pubkey(const ecdsa_curve *curve, const uint8_t *pub_key, curve_point *pub);
|
||||
int ecdsa_validate_pubkey(const ecdsa_curve *curve, const curve_point *pub);
|
||||
int ecdsa_verify(const ecdsa_curve *curve, const uint8_t *pub_key, const uint8_t *sig, const uint8_t *msg, uint32_t msg_len);
|
||||
int ecdsa_verify_double(const ecdsa_curve *curve, const uint8_t *pub_key, const uint8_t *sig, const uint8_t *msg, uint32_t msg_len);
|
||||
int ecdsa_verify(const ecdsa_curve *curve, HasherType hasher_type, const uint8_t *pub_key, const uint8_t *sig, const uint8_t *msg, uint32_t msg_len);
|
||||
int ecdsa_verify_double(const ecdsa_curve *curve, HasherType hasher_type, const uint8_t *pub_key, const uint8_t *sig, const uint8_t *msg, uint32_t msg_len);
|
||||
int ecdsa_verify_digest(const ecdsa_curve *curve, const uint8_t *pub_key, const uint8_t *sig, const uint8_t *digest);
|
||||
int ecdsa_verify_digest_recover(const ecdsa_curve *curve, uint8_t *pub_key, const uint8_t *sig, const uint8_t *digest, int recid);
|
||||
int ecdsa_sig_to_der(const uint8_t *sig, uint8_t *der);
|
||||
|
@ -56,7 +56,7 @@ const ecdsa_curve nist256p1 = {
|
||||
};
|
||||
|
||||
const curve_info nist256p1_info = {
|
||||
/* bip32_name */
|
||||
"Nist256p1 seed",
|
||||
&nist256p1
|
||||
.bip32_name = "Nist256p1 seed",
|
||||
.params = &nist256p1,
|
||||
.hasher_type = HASHER_SHA2,
|
||||
};
|
||||
|
8
script.c
8
script.c
@ -32,14 +32,14 @@ int script_output_to_address(const uint8_t *script, int scriptlen, char *addr, i
|
||||
if (scriptlen == 25 && script[0] == 0x76 && script[1] == 0xA9 && script[2] == 0x14 && script[23] == 0x88 && script[24] == 0xAC) {
|
||||
raw[0] = 0x00;
|
||||
memcpy(raw + 1, script + 3, 20);
|
||||
return base58_encode_check(raw, 1 + 20, addr, addrsize);
|
||||
return base58_encode_check(raw, 1 + 20, HASHER_SHA2, addr, addrsize);
|
||||
}
|
||||
|
||||
// P2SH
|
||||
if (scriptlen == 23 && script[0] == 0xA9 && script[1] == 0x14 && script[22] == 0x87) {
|
||||
raw[0] = 0x05;
|
||||
memcpy(raw + 1, script + 2, 20);
|
||||
return base58_encode_check(raw, 1 + 20, addr, addrsize);
|
||||
return base58_encode_check(raw, 1 + 20, HASHER_SHA2, addr, addrsize);
|
||||
}
|
||||
|
||||
// P2WPKH
|
||||
@ -48,7 +48,7 @@ int script_output_to_address(const uint8_t *script, int scriptlen, char *addr, i
|
||||
raw[1] = 0x00;
|
||||
raw[2] = 0x00;
|
||||
memcpy(raw + 3, script + 2, 20);
|
||||
return base58_encode_check(raw, 3 + 20, addr, addrsize);
|
||||
return base58_encode_check(raw, 3 + 20, HASHER_SHA2, addr, addrsize);
|
||||
}
|
||||
|
||||
// P2WSH
|
||||
@ -57,7 +57,7 @@ int script_output_to_address(const uint8_t *script, int scriptlen, char *addr, i
|
||||
raw[1] = 0x00;
|
||||
raw[2] = 0x00;
|
||||
memcpy(raw + 3, script + 2, 32);
|
||||
return base58_encode_check(raw, 3 + 32, addr, addrsize);
|
||||
return base58_encode_check(raw, 3 + 32, HASHER_SHA2, addr, addrsize);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
@ -56,7 +56,7 @@ const ecdsa_curve secp256k1 = {
|
||||
};
|
||||
|
||||
const curve_info secp256k1_info = {
|
||||
/* bip32_name */
|
||||
"Bitcoin seed",
|
||||
&secp256k1
|
||||
.bip32_name = "Bitcoin seed",
|
||||
.params = &secp256k1,
|
||||
.hasher_type = HASHER_SHA2,
|
||||
};
|
||||
|
148
test_check.c
148
test_check.c
@ -732,11 +732,11 @@ START_TEST(test_base58)
|
||||
int len = strlen(*raw) / 2;
|
||||
|
||||
memcpy(rawn, fromhex(*raw), len);
|
||||
r = base58_encode_check(rawn, len, strn, sizeof(strn));
|
||||
r = base58_encode_check(rawn, len, HASHER_SHA2, strn, sizeof(strn));
|
||||
ck_assert_int_eq((size_t)r, strlen(*str) + 1);
|
||||
ck_assert_str_eq(strn, *str);
|
||||
|
||||
r = base58_decode_check(strn, rawn, len);
|
||||
r = base58_decode_check(strn, HASHER_SHA2, rawn, len);
|
||||
ck_assert_int_eq(r, len);
|
||||
ck_assert_mem_eq(rawn, fromhex(*raw), len);
|
||||
|
||||
@ -1139,9 +1139,9 @@ START_TEST(test_bip32_optimized)
|
||||
memcpy(&node, &root, sizeof(HDNode));
|
||||
hdnode_public_ckd(&node, i);
|
||||
hdnode_fill_public_key(&node);
|
||||
ecdsa_get_address(node.public_key, 0, addr1, sizeof(addr1));
|
||||
ecdsa_get_address(node.public_key, 0, HASHER_SHA2, addr1, sizeof(addr1));
|
||||
// optimized
|
||||
hdnode_public_ckd_address_optimized(&pub, root.chain_code, i, 0, addr2, sizeof(addr2), 0);
|
||||
hdnode_public_ckd_address_optimized(&pub, root.chain_code, i, 0, HASHER_SHA2, addr2, sizeof(addr2), 0);
|
||||
// check
|
||||
ck_assert_str_eq(addr1, addr2);
|
||||
}
|
||||
@ -2433,50 +2433,50 @@ START_TEST(test_address)
|
||||
uint8_t pub_key[65];
|
||||
|
||||
memcpy(pub_key, fromhex("0226659c1cf7321c178c07437150639ff0c5b7679c7ea195253ed9abda2e081a37"), 33);
|
||||
ecdsa_get_address(pub_key, 0, address, sizeof(address)); ck_assert_str_eq(address, "139MaMHp3Vjo8o4x8N1ZLWEtovLGvBsg6s");
|
||||
ecdsa_get_address(pub_key, 111, address, sizeof(address)); ck_assert_str_eq(address, "mhfJsQNnrXB3uuYZqvywARTDfuvyjg4RBh");
|
||||
ecdsa_get_address(pub_key, 52, address, sizeof(address)); ck_assert_str_eq(address, "MxiimznnxsqMfLKTQBL8Z2PoY9jKpjgkCu");
|
||||
ecdsa_get_address(pub_key, 48, address, sizeof(address)); ck_assert_str_eq(address, "LMNJqZbe89yrPbm7JVzrcXJf28hZ1rKPaH");
|
||||
ecdsa_get_address_segwit_p2sh(pub_key, 5, address, sizeof(address)); ck_assert_str_eq(address, "34PyTHn74syS796eTgsyoLfwoBC3cwLn6p");
|
||||
ecdsa_get_address(pub_key, 0, HASHER_SHA2, address, sizeof(address)); ck_assert_str_eq(address, "139MaMHp3Vjo8o4x8N1ZLWEtovLGvBsg6s");
|
||||
ecdsa_get_address(pub_key, 111, HASHER_SHA2, address, sizeof(address)); ck_assert_str_eq(address, "mhfJsQNnrXB3uuYZqvywARTDfuvyjg4RBh");
|
||||
ecdsa_get_address(pub_key, 52, HASHER_SHA2, address, sizeof(address)); ck_assert_str_eq(address, "MxiimznnxsqMfLKTQBL8Z2PoY9jKpjgkCu");
|
||||
ecdsa_get_address(pub_key, 48, HASHER_SHA2, address, sizeof(address)); ck_assert_str_eq(address, "LMNJqZbe89yrPbm7JVzrcXJf28hZ1rKPaH");
|
||||
ecdsa_get_address_segwit_p2sh(pub_key, 5, HASHER_SHA2, address, sizeof(address)); ck_assert_str_eq(address, "34PyTHn74syS796eTgsyoLfwoBC3cwLn6p");
|
||||
|
||||
memcpy(pub_key, fromhex("025b1654a0e78d28810094f6c5a96b8efb8a65668b578f170ac2b1f83bc63ba856"), 33);
|
||||
ecdsa_get_address(pub_key, 0, address, sizeof(address)); ck_assert_str_eq(address, "19Ywfm3witp6C1yBMy4NRYHY2347WCRBfQ");
|
||||
ecdsa_get_address(pub_key, 111, address, sizeof(address)); ck_assert_str_eq(address, "mp4txp8vXvFLy8So5Y2kFTVrt2epN6YzdP");
|
||||
ecdsa_get_address(pub_key, 52, address, sizeof(address)); ck_assert_str_eq(address, "N58JsQYveGueiZDgdnNwe4SSkGTAToutAY");
|
||||
ecdsa_get_address(pub_key, 48, address, sizeof(address)); ck_assert_str_eq(address, "LTmtvyMmoZ49SpfLY73fhZMJEFRPdyohKh");
|
||||
ecdsa_get_address_segwit_p2sh(pub_key, 5, address, sizeof(address)); ck_assert_str_eq(address, "35trq6eeuHf6VL9L8pQv46x3vegHnHoTuB");
|
||||
ecdsa_get_address(pub_key, 0, HASHER_SHA2, address, sizeof(address)); ck_assert_str_eq(address, "19Ywfm3witp6C1yBMy4NRYHY2347WCRBfQ");
|
||||
ecdsa_get_address(pub_key, 111, HASHER_SHA2, address, sizeof(address)); ck_assert_str_eq(address, "mp4txp8vXvFLy8So5Y2kFTVrt2epN6YzdP");
|
||||
ecdsa_get_address(pub_key, 52, HASHER_SHA2, address, sizeof(address)); ck_assert_str_eq(address, "N58JsQYveGueiZDgdnNwe4SSkGTAToutAY");
|
||||
ecdsa_get_address(pub_key, 48, HASHER_SHA2, address, sizeof(address)); ck_assert_str_eq(address, "LTmtvyMmoZ49SpfLY73fhZMJEFRPdyohKh");
|
||||
ecdsa_get_address_segwit_p2sh(pub_key, 5, HASHER_SHA2, address, sizeof(address)); ck_assert_str_eq(address, "35trq6eeuHf6VL9L8pQv46x3vegHnHoTuB");
|
||||
|
||||
memcpy(pub_key, fromhex("03433f246a12e6486a51ff08802228c61cf895175a9b49ed4766ea9a9294a3c7fe"), 33);
|
||||
ecdsa_get_address(pub_key, 0, address, sizeof(address)); ck_assert_str_eq(address, "1FWE2bn3MWhc4QidcF6AvEWpK77sSi2cAP");
|
||||
ecdsa_get_address(pub_key, 111, address, sizeof(address)); ck_assert_str_eq(address, "mv2BKes2AY8rqXCFKp4Yk9j9B6iaMfWRLN");
|
||||
ecdsa_get_address(pub_key, 52, address, sizeof(address)); ck_assert_str_eq(address, "NB5bEFH2GtoAawy8t4Qk8kfj3LWvQs3MhB");
|
||||
ecdsa_get_address(pub_key, 48, address, sizeof(address)); ck_assert_str_eq(address, "LZjBHp5sSAwfKDQnnP5UCFaaXKV9YheGxQ");
|
||||
ecdsa_get_address_segwit_p2sh(pub_key, 5, address, sizeof(address)); ck_assert_str_eq(address, "3456DYaKUWuY6RWWw8Hp5CftHLcQN29h9Y");
|
||||
ecdsa_get_address(pub_key, 0, HASHER_SHA2, address, sizeof(address)); ck_assert_str_eq(address, "1FWE2bn3MWhc4QidcF6AvEWpK77sSi2cAP");
|
||||
ecdsa_get_address(pub_key, 111, HASHER_SHA2, address, sizeof(address)); ck_assert_str_eq(address, "mv2BKes2AY8rqXCFKp4Yk9j9B6iaMfWRLN");
|
||||
ecdsa_get_address(pub_key, 52, HASHER_SHA2, address, sizeof(address)); ck_assert_str_eq(address, "NB5bEFH2GtoAawy8t4Qk8kfj3LWvQs3MhB");
|
||||
ecdsa_get_address(pub_key, 48, HASHER_SHA2, address, sizeof(address)); ck_assert_str_eq(address, "LZjBHp5sSAwfKDQnnP5UCFaaXKV9YheGxQ");
|
||||
ecdsa_get_address_segwit_p2sh(pub_key, 5, HASHER_SHA2, address, sizeof(address)); ck_assert_str_eq(address, "3456DYaKUWuY6RWWw8Hp5CftHLcQN29h9Y");
|
||||
|
||||
memcpy(pub_key, fromhex("03aeb03abeee0f0f8b4f7a5d65ce31f9570cef9f72c2dd8a19b4085a30ab033d48"), 33);
|
||||
ecdsa_get_address(pub_key, 0, address, sizeof(address)); ck_assert_str_eq(address, "1yrZb8dhdevoqpUEGi2tUccUEeiMKeLcs");
|
||||
ecdsa_get_address(pub_key, 111, address, sizeof(address)); ck_assert_str_eq(address, "mgVoreDcWf6BaxJ5wqgQiPpwLEFRLSr8U8");
|
||||
ecdsa_get_address(pub_key, 52, address, sizeof(address)); ck_assert_str_eq(address, "MwZDmEdcd1kVLP4yW62c6zmXCU3mNbveDo");
|
||||
ecdsa_get_address(pub_key, 48, address, sizeof(address)); ck_assert_str_eq(address, "LLCopoSTnHtz4eWdQQhLAVgNgT1zTi4QBK");
|
||||
ecdsa_get_address_segwit_p2sh(pub_key, 5, address, sizeof(address)); ck_assert_str_eq(address, "3DBU4tJ9tkMR9fnmCtjW48kjvseoNLQZXd");
|
||||
ecdsa_get_address(pub_key, 0, HASHER_SHA2, address, sizeof(address)); ck_assert_str_eq(address, "1yrZb8dhdevoqpUEGi2tUccUEeiMKeLcs");
|
||||
ecdsa_get_address(pub_key, 111, HASHER_SHA2, address, sizeof(address)); ck_assert_str_eq(address, "mgVoreDcWf6BaxJ5wqgQiPpwLEFRLSr8U8");
|
||||
ecdsa_get_address(pub_key, 52, HASHER_SHA2, address, sizeof(address)); ck_assert_str_eq(address, "MwZDmEdcd1kVLP4yW62c6zmXCU3mNbveDo");
|
||||
ecdsa_get_address(pub_key, 48, HASHER_SHA2, address, sizeof(address)); ck_assert_str_eq(address, "LLCopoSTnHtz4eWdQQhLAVgNgT1zTi4QBK");
|
||||
ecdsa_get_address_segwit_p2sh(pub_key, 5, HASHER_SHA2, address, sizeof(address)); ck_assert_str_eq(address, "3DBU4tJ9tkMR9fnmCtjW48kjvseoNLQZXd");
|
||||
|
||||
memcpy(pub_key, fromhex("0496e8f2093f018aff6c2e2da5201ee528e2c8accbf9cac51563d33a7bb74a016054201c025e2a5d96b1629b95194e806c63eb96facaedc733b1a4b70ab3b33e3a"), 65);
|
||||
ecdsa_get_address(pub_key, 0, address, sizeof(address)); ck_assert_str_eq(address, "194SZbL75xCCGBbKtMsyWLE5r9s2V6mhVM");
|
||||
ecdsa_get_address(pub_key, 111, address, sizeof(address)); ck_assert_str_eq(address, "moaPreR5tydT3J4wbvrMLFSQi9TjPCiZc6");
|
||||
ecdsa_get_address(pub_key, 52, address, sizeof(address)); ck_assert_str_eq(address, "N4domEq61LHkniqqABCYirNzaPG5NRU8GH");
|
||||
ecdsa_get_address(pub_key, 48, address, sizeof(address)); ck_assert_str_eq(address, "LTHPpodwAcSFWzHV4VsGnMHr4NEJajMnKX");
|
||||
ecdsa_get_address(pub_key, 0, HASHER_SHA2, address, sizeof(address)); ck_assert_str_eq(address, "194SZbL75xCCGBbKtMsyWLE5r9s2V6mhVM");
|
||||
ecdsa_get_address(pub_key, 111, HASHER_SHA2, address, sizeof(address)); ck_assert_str_eq(address, "moaPreR5tydT3J4wbvrMLFSQi9TjPCiZc6");
|
||||
ecdsa_get_address(pub_key, 52, HASHER_SHA2, address, sizeof(address)); ck_assert_str_eq(address, "N4domEq61LHkniqqABCYirNzaPG5NRU8GH");
|
||||
ecdsa_get_address(pub_key, 48, HASHER_SHA2, address, sizeof(address)); ck_assert_str_eq(address, "LTHPpodwAcSFWzHV4VsGnMHr4NEJajMnKX");
|
||||
|
||||
memcpy(pub_key, fromhex("0498010f8a687439ff497d3074beb4519754e72c4b6220fb669224749591dde416f3961f8ece18f8689bb32235e436874d2174048b86118a00afbd5a4f33a24f0f"), 65);
|
||||
ecdsa_get_address(pub_key, 0, address, sizeof(address)); ck_assert_str_eq(address, "1A2WfBD4BJFwYHFPc5KgktqtbdJLBuVKc4");
|
||||
ecdsa_get_address(pub_key, 111, address, sizeof(address)); ck_assert_str_eq(address, "mpYTxEJ2zKhCKPj1KeJ4ap4DTcu39T3uzD");
|
||||
ecdsa_get_address(pub_key, 52, address, sizeof(address)); ck_assert_str_eq(address, "N5bsrpi36gMW4pVtsteFyQzoKrhPE7nkxK");
|
||||
ecdsa_get_address(pub_key, 48, address, sizeof(address)); ck_assert_str_eq(address, "LUFTvPWtFxVzo5wYnDJz2uueoqfcMYiuxH");
|
||||
ecdsa_get_address(pub_key, 0, HASHER_SHA2, address, sizeof(address)); ck_assert_str_eq(address, "1A2WfBD4BJFwYHFPc5KgktqtbdJLBuVKc4");
|
||||
ecdsa_get_address(pub_key, 111, HASHER_SHA2, address, sizeof(address)); ck_assert_str_eq(address, "mpYTxEJ2zKhCKPj1KeJ4ap4DTcu39T3uzD");
|
||||
ecdsa_get_address(pub_key, 52, HASHER_SHA2, address, sizeof(address)); ck_assert_str_eq(address, "N5bsrpi36gMW4pVtsteFyQzoKrhPE7nkxK");
|
||||
ecdsa_get_address(pub_key, 48, HASHER_SHA2, address, sizeof(address)); ck_assert_str_eq(address, "LUFTvPWtFxVzo5wYnDJz2uueoqfcMYiuxH");
|
||||
|
||||
memcpy(pub_key, fromhex("04f80490839af36d13701ec3f9eebdac901b51c362119d74553a3c537faff31b17e2a59ebddbdac9e87b816307a7ed5b826b8f40b92719086238e1bebf19b77a4d"), 65);
|
||||
ecdsa_get_address(pub_key, 0, address, sizeof(address)); ck_assert_str_eq(address, "19J81hrPnQxg9UGx45ibTieCkb2ttm8CLL");
|
||||
ecdsa_get_address(pub_key, 111, address, sizeof(address)); ck_assert_str_eq(address, "mop5JkwNbSPvvakZmegyHdrXcadbjLazww");
|
||||
ecdsa_get_address(pub_key, 52, address, sizeof(address)); ck_assert_str_eq(address, "N4sVDMMNho4Eg1XTKu3AgEo7UpRwq3aNbn");
|
||||
ecdsa_get_address(pub_key, 48, address, sizeof(address)); ck_assert_str_eq(address, "LTX5GvADs5CjQGy7EDhtjjhxxoQB2Uhicd");
|
||||
ecdsa_get_address(pub_key, 0, HASHER_SHA2, address, sizeof(address)); ck_assert_str_eq(address, "19J81hrPnQxg9UGx45ibTieCkb2ttm8CLL");
|
||||
ecdsa_get_address(pub_key, 111, HASHER_SHA2, address, sizeof(address)); ck_assert_str_eq(address, "mop5JkwNbSPvvakZmegyHdrXcadbjLazww");
|
||||
ecdsa_get_address(pub_key, 52, HASHER_SHA2, address, sizeof(address)); ck_assert_str_eq(address, "N4sVDMMNho4Eg1XTKu3AgEo7UpRwq3aNbn");
|
||||
ecdsa_get_address(pub_key, 48, HASHER_SHA2, address, sizeof(address)); ck_assert_str_eq(address, "LTX5GvADs5CjQGy7EDhtjjhxxoQB2Uhicd");
|
||||
}
|
||||
END_TEST
|
||||
|
||||
@ -2563,16 +2563,16 @@ START_TEST(test_wif)
|
||||
char wif[53];
|
||||
|
||||
memcpy(priv_key, fromhex("1111111111111111111111111111111111111111111111111111111111111111"), 32);
|
||||
ecdsa_get_wif(priv_key, 0x80, wif, sizeof(wif)); ck_assert_str_eq(wif, "KwntMbt59tTsj8xqpqYqRRWufyjGunvhSyeMo3NTYpFYzZbXJ5Hp");
|
||||
ecdsa_get_wif(priv_key, 0xEF, wif, sizeof(wif)); ck_assert_str_eq(wif, "cN9spWsvaxA8taS7DFMxnk1yJD2gaF2PX1npuTpy3vuZFJdwavaw");
|
||||
ecdsa_get_wif(priv_key, 0x80, HASHER_SHA2, wif, sizeof(wif)); ck_assert_str_eq(wif, "KwntMbt59tTsj8xqpqYqRRWufyjGunvhSyeMo3NTYpFYzZbXJ5Hp");
|
||||
ecdsa_get_wif(priv_key, 0xEF, HASHER_SHA2, wif, sizeof(wif)); ck_assert_str_eq(wif, "cN9spWsvaxA8taS7DFMxnk1yJD2gaF2PX1npuTpy3vuZFJdwavaw");
|
||||
|
||||
memcpy(priv_key, fromhex("dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"), 32);
|
||||
ecdsa_get_wif(priv_key, 0x80, wif, sizeof(wif)); ck_assert_str_eq(wif, "L4ezQvyC6QoBhxB4GVs9fAPhUKtbaXYUn8YTqoeXwbevQq4U92vN");
|
||||
ecdsa_get_wif(priv_key, 0xEF, wif, sizeof(wif)); ck_assert_str_eq(wif, "cV1ysqy3XUVSsPeKeugH2Utm6ZC1EyeArAgvxE73SiJvfa6AJng7");
|
||||
ecdsa_get_wif(priv_key, 0x80, HASHER_SHA2, wif, sizeof(wif)); ck_assert_str_eq(wif, "L4ezQvyC6QoBhxB4GVs9fAPhUKtbaXYUn8YTqoeXwbevQq4U92vN");
|
||||
ecdsa_get_wif(priv_key, 0xEF, HASHER_SHA2, wif, sizeof(wif)); ck_assert_str_eq(wif, "cV1ysqy3XUVSsPeKeugH2Utm6ZC1EyeArAgvxE73SiJvfa6AJng7");
|
||||
|
||||
memcpy(priv_key, fromhex("47f7616ea6f9b923076625b4488115de1ef1187f760e65f89eb6f4f7ff04b012"), 32);
|
||||
ecdsa_get_wif(priv_key, 0x80, wif, sizeof(wif)); ck_assert_str_eq(wif, "KydbzBtk6uc7M6dXwEgTEH2sphZxSPbmDSz6kUUHi4eUpSQuhEbq");
|
||||
ecdsa_get_wif(priv_key, 0xEF, wif, sizeof(wif)); ck_assert_str_eq(wif, "cPzbT6tbXyJNWY6oKeVabbXwSvsN6qhTHV8ZrtvoDBJV5BRY1G5Q");
|
||||
ecdsa_get_wif(priv_key, 0x80, HASHER_SHA2, wif, sizeof(wif)); ck_assert_str_eq(wif, "KydbzBtk6uc7M6dXwEgTEH2sphZxSPbmDSz6kUUHi4eUpSQuhEbq");
|
||||
ecdsa_get_wif(priv_key, 0xEF, HASHER_SHA2, wif, sizeof(wif)); ck_assert_str_eq(wif, "cPzbT6tbXyJNWY6oKeVabbXwSvsN6qhTHV8ZrtvoDBJV5BRY1G5Q");
|
||||
}
|
||||
END_TEST
|
||||
|
||||
@ -2581,48 +2581,48 @@ START_TEST(test_address_decode)
|
||||
int res;
|
||||
uint8_t decode[MAX_ADDR_RAW_SIZE];
|
||||
|
||||
res = ecdsa_address_decode("1JwSSubhmg6iPtRjtyqhUYYH7bZg3Lfy1T", 0, decode);
|
||||
res = ecdsa_address_decode("1JwSSubhmg6iPtRjtyqhUYYH7bZg3Lfy1T", 0, HASHER_SHA2, decode);
|
||||
ck_assert_int_eq(res, 1);
|
||||
ck_assert_mem_eq(decode, fromhex("00c4c5d791fcb4654a1ef5e03fe0ad3d9c598f9827"), 21);
|
||||
|
||||
res = ecdsa_address_decode("myTPjxggahXyAzuMcYp5JTkbybANyLsYBW", 111, decode);
|
||||
res = ecdsa_address_decode("myTPjxggahXyAzuMcYp5JTkbybANyLsYBW", 111, HASHER_SHA2, decode);
|
||||
ck_assert_int_eq(res, 1);
|
||||
ck_assert_mem_eq(decode, fromhex("6fc4c5d791fcb4654a1ef5e03fe0ad3d9c598f9827"), 21);
|
||||
|
||||
res = ecdsa_address_decode("NEWoeZ6gh4CGvRgFAoAGh4hBqpxizGT6gZ", 52, decode);
|
||||
res = ecdsa_address_decode("NEWoeZ6gh4CGvRgFAoAGh4hBqpxizGT6gZ", 52, HASHER_SHA2, decode);
|
||||
ck_assert_int_eq(res, 1);
|
||||
ck_assert_mem_eq(decode, fromhex("34c4c5d791fcb4654a1ef5e03fe0ad3d9c598f9827"), 21);
|
||||
|
||||
res = ecdsa_address_decode("LdAPi7uXrLLmeh7u57pzkZc3KovxEDYRJq", 48, decode);
|
||||
res = ecdsa_address_decode("LdAPi7uXrLLmeh7u57pzkZc3KovxEDYRJq", 48, HASHER_SHA2, decode);
|
||||
ck_assert_int_eq(res, 1);
|
||||
ck_assert_mem_eq(decode, fromhex("30c4c5d791fcb4654a1ef5e03fe0ad3d9c598f9827"), 21);
|
||||
|
||||
res = ecdsa_address_decode("1C7zdTfnkzmr13HfA2vNm5SJYRK6nEKyq8", 0, decode);
|
||||
res = ecdsa_address_decode("1C7zdTfnkzmr13HfA2vNm5SJYRK6nEKyq8", 0, HASHER_SHA2, decode);
|
||||
ck_assert_int_eq(res, 1);
|
||||
ck_assert_mem_eq(decode, fromhex("0079fbfc3f34e7745860d76137da68f362380c606c"), 21);
|
||||
|
||||
res = ecdsa_address_decode("mrdwvWkma2D6n9mGsbtkazedQQuoksnqJV", 111, decode);
|
||||
res = ecdsa_address_decode("mrdwvWkma2D6n9mGsbtkazedQQuoksnqJV", 111, HASHER_SHA2, decode);
|
||||
ck_assert_int_eq(res, 1);
|
||||
ck_assert_mem_eq(decode, fromhex("6f79fbfc3f34e7745860d76137da68f362380c606c"), 21);
|
||||
|
||||
res = ecdsa_address_decode("N7hMq7AmgNsQXaYARrEwybbDGei9mcPNqr", 52, decode);
|
||||
res = ecdsa_address_decode("N7hMq7AmgNsQXaYARrEwybbDGei9mcPNqr", 52, HASHER_SHA2, decode);
|
||||
ck_assert_int_eq(res, 1);
|
||||
ck_assert_mem_eq(decode, fromhex("3479fbfc3f34e7745860d76137da68f362380c606c"), 21);
|
||||
|
||||
res = ecdsa_address_decode("LWLwtfycqf1uFqypLAug36W4kdgNwrZdNs", 48, decode);
|
||||
res = ecdsa_address_decode("LWLwtfycqf1uFqypLAug36W4kdgNwrZdNs", 48, HASHER_SHA2, decode);
|
||||
ck_assert_int_eq(res, 1);
|
||||
ck_assert_mem_eq(decode, fromhex("3079fbfc3f34e7745860d76137da68f362380c606c"), 21);
|
||||
|
||||
// invalid char
|
||||
res = ecdsa_address_decode("1JwSSubhmg6i000jtyqhUYYH7bZg3Lfy1T", 0, decode);
|
||||
res = ecdsa_address_decode("1JwSSubhmg6i000jtyqhUYYH7bZg3Lfy1T", 0, HASHER_SHA2, decode);
|
||||
ck_assert_int_eq(res, 0);
|
||||
|
||||
// invalid address
|
||||
res = ecdsa_address_decode("1111Subhmg6iPtRjtyqhUYYH7bZg3Lfy1T", 0, decode);
|
||||
res = ecdsa_address_decode("1111Subhmg6iPtRjtyqhUYYH7bZg3Lfy1T", 0, HASHER_SHA2, decode);
|
||||
ck_assert_int_eq(res, 0);
|
||||
|
||||
// invalid version
|
||||
res = ecdsa_address_decode("LWLwtfycqf1uFqypLAug36W4kdgNwrZdNs", 0, decode);
|
||||
res = ecdsa_address_decode("LWLwtfycqf1uFqypLAug36W4kdgNwrZdNs", 0, HASHER_SHA2, decode);
|
||||
ck_assert_int_eq(res, 0);
|
||||
}
|
||||
END_TEST
|
||||
@ -3794,50 +3794,50 @@ START_TEST(test_multibyte_address)
|
||||
int res;
|
||||
|
||||
memcpy(priv_key, fromhex("47f7616ea6f9b923076625b4488115de1ef1187f760e65f89eb6f4f7ff04b012"), 32);
|
||||
ecdsa_get_wif(priv_key, 0, wif, sizeof(wif)); ck_assert_str_eq(wif, "13QtoXmbhELWcrwD9YA9KzvXy5rTaptiNuFR8L8ArpBNn4xmQj4N");
|
||||
ecdsa_get_wif(priv_key, 0x12, wif, sizeof(wif)); ck_assert_str_eq(wif, "3hrF6SFnqzpzABB36uGDf8dJSuUCcMmoJrTmCWMshRkBr2Vx86qJ");
|
||||
ecdsa_get_wif(priv_key, 0x1234, wif, sizeof(wif)); ck_assert_str_eq(wif, "CtPTF9awbVbfDWGepGdVhB3nBhr4HktUGya8nf8dLxgC8tbqBreB9");
|
||||
ecdsa_get_wif(priv_key, 0x123456, wif, sizeof(wif)); ck_assert_str_eq(wif, "uTrDevVQt5QZgoL3iJ1cPWHaCz7ZMBncM7QXZfCegtxiMHqBvWoYJa");
|
||||
ecdsa_get_wif(priv_key, 0x12345678, wif, sizeof(wif)); ck_assert_str_eq(wif, "4zZWMzv1SVbs95pmLXWrXJVp9ntPEam1mfwb6CXBLn9MpWNxLg9huYgv");
|
||||
ecdsa_get_wif(priv_key, 0xffffffff, wif, sizeof(wif)); ck_assert_str_eq(wif, "y9KVfV1RJXcTxpVjeuh6WYWh8tMwnAUeyUwDEiRviYdrJ61njTmnfUjE");
|
||||
ecdsa_get_wif(priv_key, 0, HASHER_SHA2, wif, sizeof(wif)); ck_assert_str_eq(wif, "13QtoXmbhELWcrwD9YA9KzvXy5rTaptiNuFR8L8ArpBNn4xmQj4N");
|
||||
ecdsa_get_wif(priv_key, 0x12, HASHER_SHA2, wif, sizeof(wif)); ck_assert_str_eq(wif, "3hrF6SFnqzpzABB36uGDf8dJSuUCcMmoJrTmCWMshRkBr2Vx86qJ");
|
||||
ecdsa_get_wif(priv_key, 0x1234, HASHER_SHA2, wif, sizeof(wif)); ck_assert_str_eq(wif, "CtPTF9awbVbfDWGepGdVhB3nBhr4HktUGya8nf8dLxgC8tbqBreB9");
|
||||
ecdsa_get_wif(priv_key, 0x123456, HASHER_SHA2, wif, sizeof(wif)); ck_assert_str_eq(wif, "uTrDevVQt5QZgoL3iJ1cPWHaCz7ZMBncM7QXZfCegtxiMHqBvWoYJa");
|
||||
ecdsa_get_wif(priv_key, 0x12345678, HASHER_SHA2, wif, sizeof(wif)); ck_assert_str_eq(wif, "4zZWMzv1SVbs95pmLXWrXJVp9ntPEam1mfwb6CXBLn9MpWNxLg9huYgv");
|
||||
ecdsa_get_wif(priv_key, 0xffffffff, HASHER_SHA2, wif, sizeof(wif)); ck_assert_str_eq(wif, "y9KVfV1RJXcTxpVjeuh6WYWh8tMwnAUeyUwDEiRviYdrJ61njTmnfUjE");
|
||||
|
||||
memcpy(pub_key, fromhex("0378d430274f8c5ec1321338151e9f27f4c676a008bdf8638d07c0b6be9ab35c71"), 33);
|
||||
ecdsa_get_address(pub_key, 0, address, sizeof(address)); ck_assert_str_eq(address, "1C7zdTfnkzmr13HfA2vNm5SJYRK6nEKyq8");
|
||||
ecdsa_get_address(pub_key, 0x12, address, sizeof(address)); ck_assert_str_eq(address, "8SCrMR2yYF7ciqoDbav7VLLTsVx5dTVPPq");
|
||||
ecdsa_get_address(pub_key, 0x1234, address, sizeof(address)); ck_assert_str_eq(address, "ZLH8q1UgMPg8o2s1MD55YVMpPV7vqms9kiV");
|
||||
ecdsa_get_address(pub_key, 0x123456, address, sizeof(address)); ck_assert_str_eq(address, "3ThqvsQVFnbiF66NwHtfe2j6AKn75DpLKpQSq");
|
||||
ecdsa_get_address(pub_key, 0x12345678, address, sizeof(address)); ck_assert_str_eq(address, "BrsGxAHga3VbopvSnb3gmLvMBhJNCGuDxBZL44");
|
||||
ecdsa_get_address(pub_key, 0xffffffff, address, sizeof(address)); ck_assert_str_eq(address, "3diW7paWGJyZRLGqMJZ55DMfPExob8QxQHkrfYT");
|
||||
ecdsa_get_address(pub_key, 0, HASHER_SHA2, address, sizeof(address)); ck_assert_str_eq(address, "1C7zdTfnkzmr13HfA2vNm5SJYRK6nEKyq8");
|
||||
ecdsa_get_address(pub_key, 0x12, HASHER_SHA2, address, sizeof(address)); ck_assert_str_eq(address, "8SCrMR2yYF7ciqoDbav7VLLTsVx5dTVPPq");
|
||||
ecdsa_get_address(pub_key, 0x1234, HASHER_SHA2, address, sizeof(address)); ck_assert_str_eq(address, "ZLH8q1UgMPg8o2s1MD55YVMpPV7vqms9kiV");
|
||||
ecdsa_get_address(pub_key, 0x123456, HASHER_SHA2, address, sizeof(address)); ck_assert_str_eq(address, "3ThqvsQVFnbiF66NwHtfe2j6AKn75DpLKpQSq");
|
||||
ecdsa_get_address(pub_key, 0x12345678, HASHER_SHA2, address, sizeof(address)); ck_assert_str_eq(address, "BrsGxAHga3VbopvSnb3gmLvMBhJNCGuDxBZL44");
|
||||
ecdsa_get_address(pub_key, 0xffffffff, HASHER_SHA2, address, sizeof(address)); ck_assert_str_eq(address, "3diW7paWGJyZRLGqMJZ55DMfPExob8QxQHkrfYT");
|
||||
|
||||
res = ecdsa_address_decode("1C7zdTfnkzmr13HfA2vNm5SJYRK6nEKyq8", 0, decode);
|
||||
res = ecdsa_address_decode("1C7zdTfnkzmr13HfA2vNm5SJYRK6nEKyq8", 0, HASHER_SHA2, decode);
|
||||
ck_assert_int_eq(res, 1);
|
||||
ck_assert_mem_eq(decode, fromhex("0079fbfc3f34e7745860d76137da68f362380c606c"), 21);
|
||||
res = ecdsa_address_decode("8SCrMR2yYF7ciqoDbav7VLLTsVx5dTVPPq", 0x12, decode);
|
||||
res = ecdsa_address_decode("8SCrMR2yYF7ciqoDbav7VLLTsVx5dTVPPq", 0x12, HASHER_SHA2, decode);
|
||||
ck_assert_int_eq(res, 1);
|
||||
ck_assert_mem_eq(decode, fromhex("1279fbfc3f34e7745860d76137da68f362380c606c"), 21);
|
||||
res = ecdsa_address_decode("ZLH8q1UgMPg8o2s1MD55YVMpPV7vqms9kiV", 0x1234, decode);
|
||||
res = ecdsa_address_decode("ZLH8q1UgMPg8o2s1MD55YVMpPV7vqms9kiV", 0x1234, HASHER_SHA2, decode);
|
||||
ck_assert_int_eq(res, 1);
|
||||
ck_assert_mem_eq(decode, fromhex("123479fbfc3f34e7745860d76137da68f362380c606c"), 21);
|
||||
res = ecdsa_address_decode("3ThqvsQVFnbiF66NwHtfe2j6AKn75DpLKpQSq", 0x123456, decode);
|
||||
res = ecdsa_address_decode("3ThqvsQVFnbiF66NwHtfe2j6AKn75DpLKpQSq", 0x123456, HASHER_SHA2, decode);
|
||||
ck_assert_int_eq(res, 1);
|
||||
ck_assert_mem_eq(decode, fromhex("12345679fbfc3f34e7745860d76137da68f362380c606c"), 21);
|
||||
res = ecdsa_address_decode("BrsGxAHga3VbopvSnb3gmLvMBhJNCGuDxBZL44", 0x12345678, decode);
|
||||
res = ecdsa_address_decode("BrsGxAHga3VbopvSnb3gmLvMBhJNCGuDxBZL44", 0x12345678, HASHER_SHA2, decode);
|
||||
ck_assert_int_eq(res, 1);
|
||||
ck_assert_mem_eq(decode, fromhex("1234567879fbfc3f34e7745860d76137da68f362380c606c"), 21);
|
||||
res = ecdsa_address_decode("3diW7paWGJyZRLGqMJZ55DMfPExob8QxQHkrfYT", 0xffffffff, decode);
|
||||
res = ecdsa_address_decode("3diW7paWGJyZRLGqMJZ55DMfPExob8QxQHkrfYT", 0xffffffff, HASHER_SHA2, decode);
|
||||
ck_assert_int_eq(res, 1);
|
||||
ck_assert_mem_eq(decode, fromhex("ffffffff79fbfc3f34e7745860d76137da68f362380c606c"), 21);
|
||||
|
||||
// wrong length
|
||||
res = ecdsa_address_decode("BrsGxAHga3VbopvSnb3gmLvMBhJNCGuDxBZL44", 0x123456, decode);
|
||||
res = ecdsa_address_decode("BrsGxAHga3VbopvSnb3gmLvMBhJNCGuDxBZL44", 0x123456, HASHER_SHA2, decode);
|
||||
ck_assert_int_eq(res, 0);
|
||||
|
||||
// wrong address prefix
|
||||
res = ecdsa_address_decode("BrsGxAHga3VbopvSnb3gmLvMBhJNCGuDxBZL44", 0x22345678, decode);
|
||||
res = ecdsa_address_decode("BrsGxAHga3VbopvSnb3gmLvMBhJNCGuDxBZL44", 0x22345678, HASHER_SHA2, decode);
|
||||
ck_assert_int_eq(res, 0);
|
||||
|
||||
// wrong checksum
|
||||
res = ecdsa_address_decode("BrsGxAHga3VbopvSnb3gmLvMBhJNCGuDxBZL45", 0x12345678, decode);
|
||||
res = ecdsa_address_decode("BrsGxAHga3VbopvSnb3gmLvMBhJNCGuDxBZL45", 0x12345678, HASHER_SHA2, decode);
|
||||
ck_assert_int_eq(res, 0);
|
||||
}
|
||||
END_TEST
|
||||
|
@ -44,7 +44,7 @@ int main(int argc, char **argv)
|
||||
fprintf(stderr, "\"%s\" is not a valid mnemonic\n", mnemonic);
|
||||
return 2;
|
||||
}
|
||||
if (!ecdsa_address_decode(address, 0, addr)) {
|
||||
if (!ecdsa_address_decode(address, 0, HASHER_SHA2, addr)) {
|
||||
fprintf(stderr, "\"%s\" is not a valid address\n", address);
|
||||
return 3;
|
||||
}
|
||||
@ -70,7 +70,7 @@ int main(int argc, char **argv)
|
||||
hdnode_private_ckd(&node, 0);
|
||||
hdnode_private_ckd(&node, 0);
|
||||
hdnode_fill_public_key(&node);
|
||||
ecdsa_get_pubkeyhash(node.public_key, pubkeyhash);
|
||||
ecdsa_get_pubkeyhash(node.public_key, HASHER_SHA2, pubkeyhash);
|
||||
if (memcmp(addr + 1, pubkeyhash, 20) == 0) {
|
||||
found = 1;
|
||||
break;
|
||||
|
@ -21,7 +21,7 @@ void process_job(uint32_t jobid, const char *xpub, uint32_t change, uint32_t fro
|
||||
for (i = from; i < to; i++) {
|
||||
memcpy(&child, &node, sizeof(HDNode));
|
||||
hdnode_public_ckd(&child, i);
|
||||
ecdsa_get_address(child.public_key, 0, address, sizeof(address));
|
||||
ecdsa_get_address(child.public_key, 0, HASHER_SHA2, address, sizeof(address));
|
||||
printf("%d %d %s\n", jobid, i, address);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user