mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-07-12 17:48:09 +00:00
refactor hdnode_public_ckd_address_optimized
This commit is contained in:
parent
cfe8f39cd4
commit
c950342063
28
bip32.c
28
bip32.c
@ -287,7 +287,7 @@ int hdnode_public_ckd(HDNode *inout, uint32_t i)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int hdnode_public_ckd_address_optimized(const curve_point *pub, const uint8_t *chain_code, uint32_t i, uint32_t version, char *addr, int addrsize, bool segwit)
|
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)
|
||||||
{
|
{
|
||||||
uint8_t child_pubkey[33];
|
uint8_t child_pubkey[33];
|
||||||
curve_point b;
|
curve_point b;
|
||||||
@ -296,25 +296,13 @@ int hdnode_public_ckd_address_optimized(const curve_point *pub, const uint8_t *c
|
|||||||
child_pubkey[0] = 0x02 | (b.y.val[0] & 0x01);
|
child_pubkey[0] = 0x02 | (b.y.val[0] & 0x01);
|
||||||
bn_write_be(&b.x, child_pubkey + 1);
|
bn_write_be(&b.x, child_pubkey + 1);
|
||||||
|
|
||||||
if (!segwit) {
|
switch (addrformat) {
|
||||||
ecdsa_get_address(child_pubkey, version, addr, addrsize);
|
case 1: // Segwit-in-P2SH
|
||||||
return 1;
|
ecdsa_get_address_segwit(child_pubkey, version, addr, addrsize);
|
||||||
} else {
|
break;
|
||||||
uint8_t raw[32];
|
default: // normal address
|
||||||
size_t prelen = address_prefix_bytes_len(version);
|
ecdsa_get_address(child_pubkey, version, addr, addrsize);
|
||||||
uint8_t digest[MAX_ADDR_RAW_SIZE];
|
break;
|
||||||
|
|
||||||
raw[0] = 0; // version byte
|
|
||||||
raw[1] = 20; // push 20 bytes
|
|
||||||
ecdsa_get_pubkeyhash(child_pubkey, raw + 2);
|
|
||||||
sha256_Raw(raw, 22, digest);
|
|
||||||
address_write_prefix_bytes(version, raw);
|
|
||||||
ripemd160(digest, 32, raw + prelen);
|
|
||||||
|
|
||||||
if (!base58_encode_check(raw, prelen + 20, addr, MAX_ADDR_SIZE)) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
2
bip32.h
2
bip32.h
@ -58,7 +58,7 @@ int hdnode_public_ckd_cp(const ecdsa_curve *curve, const curve_point *parent, co
|
|||||||
|
|
||||||
int hdnode_public_ckd(HDNode *inout, uint32_t i);
|
int hdnode_public_ckd(HDNode *inout, uint32_t i);
|
||||||
|
|
||||||
int hdnode_public_ckd_address_optimized(const curve_point *pub, const uint8_t *chain_code, uint32_t i, uint32_t version, char *addr, int addrsize, bool segwit);
|
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);
|
||||||
|
|
||||||
#if USE_BIP32_CACHE
|
#if USE_BIP32_CACHE
|
||||||
int hdnode_private_ckd_cached(HDNode *inout, const uint32_t *i, size_t i_count, uint32_t *fingerprint);
|
int hdnode_private_ckd_cached(HDNode *inout, const uint32_t *i, size_t i_count, uint32_t *fingerprint);
|
||||||
|
17
ecdsa.c
17
ecdsa.c
@ -907,6 +907,23 @@ void ecdsa_get_address(const uint8_t *pub_key, uint32_t version, char *addr, int
|
|||||||
MEMSET_BZERO(raw, sizeof(raw));
|
MEMSET_BZERO(raw, sizeof(raw));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ecdsa_get_address_segwit(const uint8_t *pub_key, uint32_t version, char *addr, int addrsize)
|
||||||
|
{
|
||||||
|
uint8_t raw[MAX_ADDR_RAW_SIZE];
|
||||||
|
size_t prefix_len = address_prefix_bytes_len(version);
|
||||||
|
uint8_t digest[32];
|
||||||
|
raw[0] = 0; // version byte
|
||||||
|
raw[1] = 20; // push 20 bytes
|
||||||
|
ecdsa_get_pubkeyhash(pub_key, raw + 2);
|
||||||
|
sha256_Raw(raw, 22, digest);
|
||||||
|
address_write_prefix_bytes(version, raw);
|
||||||
|
ripemd160(digest, 32, raw + prefix_len);
|
||||||
|
base58_encode_check(raw, prefix_len + 20, addr, addrsize);
|
||||||
|
// not as important to clear these, but we might as well
|
||||||
|
MEMSET_BZERO(raw, sizeof(raw));
|
||||||
|
MEMSET_BZERO(digest, sizeof(digest));
|
||||||
|
}
|
||||||
|
|
||||||
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, char *wif, int wifsize)
|
||||||
{
|
{
|
||||||
uint8_t wif_raw[MAX_WIF_RAW_SIZE];
|
uint8_t wif_raw[MAX_WIF_RAW_SIZE];
|
||||||
|
1
ecdsa.h
1
ecdsa.h
@ -74,6 +74,7 @@ void ecdsa_get_public_key65(const ecdsa_curve *curve, const uint8_t *priv_key, u
|
|||||||
void ecdsa_get_pubkeyhash(const uint8_t *pub_key, uint8_t *pubkeyhash);
|
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_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(const uint8_t *pub_key, uint32_t version, char *addr, int addrsize);
|
||||||
|
void ecdsa_get_address_segwit(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_wif(const uint8_t *priv_key, uint32_t version, 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, uint8_t *out);
|
||||||
|
@ -1021,7 +1021,7 @@ START_TEST(test_bip32_optimized)
|
|||||||
hdnode_fill_public_key(&node);
|
hdnode_fill_public_key(&node);
|
||||||
ecdsa_get_address(node.public_key, 0, addr1, sizeof(addr1));
|
ecdsa_get_address(node.public_key, 0, addr1, sizeof(addr1));
|
||||||
// optimized
|
// optimized
|
||||||
hdnode_public_ckd_address_optimized(&pub, root.chain_code, i, 0, addr2, sizeof(addr2), false);
|
hdnode_public_ckd_address_optimized(&pub, root.chain_code, i, 0, addr2, sizeof(addr2), 0);
|
||||||
// check
|
// check
|
||||||
ck_assert_str_eq(addr1, addr2);
|
ck_assert_str_eq(addr1, addr2);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user