diff --git a/bip32.c b/bip32.c index ad9ea46b75..5135efdc7b 100644 --- a/bip32.c +++ b/bip32.c @@ -323,12 +323,16 @@ static struct { HDNode node; } private_ckd_cache[BIP32_CACHE_SIZE]; -int hdnode_private_ckd_cached(HDNode *inout, const uint32_t *i, size_t i_count) +int hdnode_private_ckd_cached(HDNode *inout, const uint32_t *i, size_t i_count, uint32_t *fingerprint) { if (i_count == 0) { + // no way how to compute parent fingerprint return 1; } if (i_count == 1) { + if (fingerprint) { + *fingerprint = hdnode_fingerprint(inout); + } if (hdnode_private_ckd(inout, i[0]) == 0) return 0; return 1; } @@ -372,6 +376,9 @@ int hdnode_private_ckd_cached(HDNode *inout, const uint32_t *i, size_t i_count) private_ckd_cache_index = (private_ckd_cache_index + 1) % BIP32_CACHE_SIZE; } + if (fingerprint) { + *fingerprint = hdnode_fingerprint(inout); + } if (hdnode_private_ckd(inout, i[i_count - 1]) == 0) return 0; return 1; diff --git a/bip32.h b/bip32.h index 8ec1614b41..3a3adaa34e 100644 --- a/bip32.h +++ b/bip32.h @@ -59,7 +59,7 @@ int hdnode_public_ckd_address_optimized(const curve_point *pub, const uint8_t *p #if USE_BIP32_CACHE -int hdnode_private_ckd_cached(HDNode *inout, const uint32_t *i, size_t i_count); +int hdnode_private_ckd_cached(HDNode *inout, const uint32_t *i, size_t i_count, uint32_t *fingerprint); #endif diff --git a/tests.c b/tests.c index 0040705c24..2c917dcdd6 100644 --- a/tests.c +++ b/tests.c @@ -817,7 +817,7 @@ START_TEST(test_bip32_cache_1) for (i = 0; i < 8; i++) { r = hdnode_private_ckd(&node1, ii[i]); ck_assert_int_eq(r, 1); } - r = hdnode_private_ckd_cached(&node2, ii, 8); ck_assert_int_eq(r, 1); + r = hdnode_private_ckd_cached(&node2, ii, 8, NULL); ck_assert_int_eq(r, 1); ck_assert_mem_eq(&node1, &node2, sizeof(HDNode)); hdnode_from_seed(fromhex("301133282ad079cbeb59bc446ad39d333928f74c46997d3609cd3e2801ca69d62788f9f174429946ff4e9be89f67c22fae28cb296a9b37734f75e73d1477af19"), 64, SECP256K1_NAME, &node1); @@ -828,7 +828,7 @@ START_TEST(test_bip32_cache_1) for (i = 0; i < 8; i++) { r = hdnode_private_ckd(&node1, ii[i]); ck_assert_int_eq(r, 1); } - r = hdnode_private_ckd_cached(&node2, ii, 8); ck_assert_int_eq(r, 1); + r = hdnode_private_ckd_cached(&node2, ii, 8, NULL); ck_assert_int_eq(r, 1); ck_assert_mem_eq(&node1, &node2, sizeof(HDNode)); // test different root node @@ -838,7 +838,7 @@ START_TEST(test_bip32_cache_1) for (i = 0; i < 8; i++) { r = hdnode_private_ckd(&node1, ii[i]); ck_assert_int_eq(r, 1); } - r = hdnode_private_ckd_cached(&node2, ii, 8); ck_assert_int_eq(r, 1); + r = hdnode_private_ckd_cached(&node2, ii, 8, NULL); ck_assert_int_eq(r, 1); ck_assert_mem_eq(&node1, &node2, sizeof(HDNode)); } END_TEST @@ -860,7 +860,7 @@ START_TEST(test_bip32_cache_2) r = hdnode_private_ckd(&(nodea[j]), ii[i - 1]); ck_assert_int_eq(r, 1); } // cached - r = hdnode_private_ckd_cached(&(nodeb[j]), ii, j); ck_assert_int_eq(r, 1); + r = hdnode_private_ckd_cached(&(nodeb[j]), ii, j, NULL); ck_assert_int_eq(r, 1); } ck_assert_mem_eq(&(nodea[0]), &(nodeb[0]), sizeof(HDNode));