1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-22 22:38:08 +00:00

remove version_byte and address from bip32

This commit is contained in:
Pavol Rusnak 2014-02-21 22:51:49 +01:00
parent 8c47b572df
commit d5e1ff8be8
3 changed files with 11 additions and 42 deletions

19
bip32.c
View File

@ -7,7 +7,7 @@
#include "sha2.h"
#include "ripemd160.h"
void hdnode_from_xpub(uint8_t version_byte, uint32_t depth, uint32_t fingerprint, uint32_t child_num, uint8_t *chain_code, uint8_t *public_key, HDNode *out)
void hdnode_from_xpub(uint32_t depth, uint32_t fingerprint, uint32_t child_num, uint8_t *chain_code, uint8_t *public_key, HDNode *out)
{
out->depth = depth;
out->fingerprint = fingerprint;
@ -15,11 +15,9 @@ void hdnode_from_xpub(uint8_t version_byte, uint32_t depth, uint32_t fingerprint
memcpy(out->chain_code, chain_code, 32);
memset(out->private_key, 0, 32);
memcpy(out->public_key, public_key, 33);
out->version_byte = version_byte;
hdnode_fill_address(out);
}
void hdnode_from_xprv(uint8_t version_byte, uint32_t depth, uint32_t fingerprint, uint32_t child_num, uint8_t *chain_code, uint8_t *private_key, HDNode *out)
void hdnode_from_xprv(uint32_t depth, uint32_t fingerprint, uint32_t child_num, uint8_t *chain_code, uint8_t *private_key, HDNode *out)
{
out->depth = depth;
out->fingerprint = fingerprint;
@ -27,11 +25,9 @@ void hdnode_from_xprv(uint8_t version_byte, uint32_t depth, uint32_t fingerprint
memcpy(out->chain_code, chain_code, 32);
memcpy(out->private_key, private_key, 32);
hdnode_fill_public_key(out);
out->version_byte = version_byte;
hdnode_fill_address(out);
}
void hdnode_from_seed(uint8_t version_byte, uint8_t *seed, int seed_len, HDNode *out)
void hdnode_from_seed(uint8_t *seed, int seed_len, HDNode *out)
{
uint8_t I[32 + 32];
out->depth = 0;
@ -41,8 +37,6 @@ void hdnode_from_seed(uint8_t version_byte, uint8_t *seed, int seed_len, HDNode
memcpy(out->chain_code, I + 32, 32);
memcpy(out->private_key, I, 32);
hdnode_fill_public_key(out);
out->version_byte = version_byte;
hdnode_fill_address(out);
}
int hdnode_private_ckd(HDNode *inout, uint32_t i)
@ -78,7 +72,6 @@ int hdnode_private_ckd(HDNode *inout, uint32_t i)
bn_write_be(&a, inout->private_key);
hdnode_fill_public_key(inout);
hdnode_fill_address(inout);
return 1;
}
@ -118,7 +111,6 @@ int hdnode_public_ckd(HDNode *inout, uint32_t i)
inout->depth++;
inout->child_num = i;
hdnode_fill_address(inout);
return 1;
}
@ -126,8 +118,3 @@ void hdnode_fill_public_key(HDNode *node)
{
ecdsa_get_public_key33(node->private_key, node->public_key);
}
void hdnode_fill_address(HDNode *node)
{
ecdsa_get_address(node->public_key, node->version_byte, node->address);
}

View File

@ -4,22 +4,19 @@
#include <stdint.h>
typedef struct {
// uint32_t version;
uint32_t depth;
uint32_t fingerprint;
uint32_t child_num;
uint8_t chain_code[32];
uint8_t private_key[32];
uint8_t public_key[33];
char address[35];
uint8_t version_byte;
} HDNode;
void hdnode_from_xpub(uint8_t version_byte, uint32_t depth, uint32_t fingerprint, uint32_t child_num, uint8_t *chain_code, uint8_t *public_key, HDNode *out);
void hdnode_from_xpub(uint32_t depth, uint32_t fingerprint, uint32_t child_num, uint8_t *chain_code, uint8_t *public_key, HDNode *out);
void hdnode_from_xprv(uint8_t version_byte, uint32_t depth, uint32_t fingerprint, uint32_t child_num, uint8_t *chain_code, uint8_t *private_key, HDNode *out);
void hdnode_from_xprv(uint32_t depth, uint32_t fingerprint, uint32_t child_num, uint8_t *chain_code, uint8_t *private_key, HDNode *out);
void hdnode_from_seed(uint8_t version_byte, uint8_t *seed, int seed_len, HDNode *out);
void hdnode_from_seed(uint8_t *seed, int seed_len, HDNode *out);
#define hdnode_private_ckd_prime(X, I) hdnode_private_ckd((X), ((I) | 0x80000000))

25
tests.c
View File

@ -80,14 +80,13 @@ START_TEST(test_bip32_vector_1)
HDNode node;
// init m
hdnode_from_seed(0x00, fromhex("000102030405060708090a0b0c0d0e0f"), 16, &node);
hdnode_from_seed(fromhex("000102030405060708090a0b0c0d0e0f"), 16, &node);
// [Chain m]
ck_assert_int_eq(node.fingerprint, 0x00000000);
ck_assert_mem_eq(node.chain_code, fromhex("873dff81c02f525623fd1fe5167eac3a55a049de3d314bb42ee227ffed37d508"), 32);
ck_assert_mem_eq(node.private_key, fromhex("e8f32e723decf4051aefac8e2c93c9c5b214313817cdb01a1494b917c8436b35"), 32);
ck_assert_mem_eq(node.public_key, fromhex("0339a36013301597daef41fbe593a02cc513d0b55527ec2df1050e2e8ff49c85c2"), 33);
ck_assert_str_eq(node.address, "15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma");
// [Chain m/0']
hdnode_private_ckd_prime(&node, 0);
@ -95,7 +94,6 @@ START_TEST(test_bip32_vector_1)
ck_assert_mem_eq(node.chain_code, fromhex("47fdacbd0f1097043b78c63c20c34ef4ed9a111d980047ad16282c7ae6236141"), 32);
ck_assert_mem_eq(node.private_key, fromhex("edb2e14f9ee77d26dd93b4ecede8d16ed408ce149b6cd80b0715a2d911a0afea"), 32);
ck_assert_mem_eq(node.public_key, fromhex("035a784662a4a20a65bf6aab9ae98a6c068a81c52e4b032c0fb5400c706cfccc56"), 33);
ck_assert_str_eq(node.address, "19Q2WoS5hSS6T8GjhK8KZLMgmWaq4neXrh");
// [Chain m/0'/1]
hdnode_private_ckd(&node, 1);
@ -103,7 +101,6 @@ START_TEST(test_bip32_vector_1)
ck_assert_mem_eq(node.chain_code, fromhex("2a7857631386ba23dacac34180dd1983734e444fdbf774041578e9b6adb37c19"), 32);
ck_assert_mem_eq(node.private_key, fromhex("3c6cb8d0f6a264c91ea8b5030fadaa8e538b020f0a387421a12de9319dc93368"), 32);
ck_assert_mem_eq(node.public_key, fromhex("03501e454bf00751f24b1b489aa925215d66af2234e3891c3b21a52bedb3cd711c"), 33);
ck_assert_str_eq(node.address, "1JQheacLPdM5ySCkrZkV66G2ApAXe1mqLj");
// [Chain m/0'/1/2']
hdnode_private_ckd_prime(&node, 2);
@ -111,7 +108,6 @@ START_TEST(test_bip32_vector_1)
ck_assert_mem_eq(node.chain_code, fromhex("04466b9cc8e161e966409ca52986c584f07e9dc81f735db683c3ff6ec7b1503f"), 32);
ck_assert_mem_eq(node.private_key, fromhex("cbce0d719ecf7431d88e6a89fa1483e02e35092af60c042b1df2ff59fa424dca"), 32);
ck_assert_mem_eq(node.public_key, fromhex("0357bfe1e341d01c69fe5654309956cbea516822fba8a601743a012a7896ee8dc2"), 33);
ck_assert_str_eq(node.address, "1NjxqbA9aZWnh17q1UW3rB4EPu79wDXj7x");
// [Chain m/0'/1/2'/2]
hdnode_private_ckd(&node, 2);
@ -119,7 +115,6 @@ START_TEST(test_bip32_vector_1)
ck_assert_mem_eq(node.chain_code, fromhex("cfb71883f01676f587d023cc53a35bc7f88f724b1f8c2892ac1275ac822a3edd"), 32);
ck_assert_mem_eq(node.private_key, fromhex("0f479245fb19a38a1954c5c7c0ebab2f9bdfd96a17563ef28a6a4b1a2a764ef4"), 32);
ck_assert_mem_eq(node.public_key, fromhex("02e8445082a72f29b75ca48748a914df60622a609cacfce8ed0e35804560741d29"), 33);
ck_assert_str_eq(node.address, "1LjmJcdPnDHhNTUgrWyhLGnRDKxQjoxAgt");
// [Chain m/0'/1/2'/2/1000000000]
hdnode_private_ckd(&node, 1000000000);
@ -127,7 +122,6 @@ START_TEST(test_bip32_vector_1)
ck_assert_mem_eq(node.chain_code, fromhex("c783e67b921d2beb8f6b389cc646d7263b4145701dadd2161548a8b078e65e9e"), 32);
ck_assert_mem_eq(node.private_key, fromhex("471b76e389e528d6de6d816857e012c5455051cad6660850e58372a6c3e6e7c8"), 32);
ck_assert_mem_eq(node.public_key, fromhex("022a471424da5e657499d1ff51cb43c47481a03b1e77f951fe64cec9f5a48f7011"), 33);
ck_assert_str_eq(node.address, "1LZiqrop2HGR4qrH1ULZPyBpU6AUP49Uam");
}
END_TEST
@ -138,14 +132,13 @@ START_TEST(test_bip32_vector_2)
int r;
// init m
hdnode_from_seed(0x00, fromhex("fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542"), 64, &node);
hdnode_from_seed(fromhex("fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542"), 64, &node);
// [Chain m]
ck_assert_int_eq(node.fingerprint, 0x00000000);
ck_assert_mem_eq(node.chain_code, fromhex("60499f801b896d83179a4374aeb7822aaeaceaa0db1f85ee3e904c4defbd9689"), 32);
ck_assert_mem_eq(node.private_key, fromhex("4b03d6fc340455b363f51020ad3ecca4f0850280cf436c70c727923f6db46c3e"), 32);
ck_assert_mem_eq(node.public_key, fromhex("03cbcaa9c98c877a26977d00825c956a238e8dddfbd322cce4f74b0b5bd6ace4a7"), 33);
ck_assert_str_eq(node.address, "1JEoxevbLLG8cVqeoGKQiAwoWbNYSUyYjg");
// [Chain m/0]
r = hdnode_private_ckd(&node, 0);
@ -154,7 +147,6 @@ START_TEST(test_bip32_vector_2)
ck_assert_mem_eq(node.chain_code, fromhex("f0909affaa7ee7abe5dd4e100598d4dc53cd709d5a5c2cac40e7412f232f7c9c"), 32);
ck_assert_mem_eq(node.private_key, fromhex("abe74a98f6c7eabee0428f53798f0ab8aa1bd37873999041703c742f15ac7e1e"), 32);
ck_assert_mem_eq(node.public_key, fromhex("02fc9e5af0ac8d9b3cecfe2a888e2117ba3d089d8585886c9c826b6b22a98d12ea"), 33);
ck_assert_str_eq(node.address, "19EuDJdgfRkwCmRzbzVBHZWQG9QNWhftbZ");
// [Chain m/0/2147483647']
r = hdnode_private_ckd_prime(&node, 2147483647);
@ -163,7 +155,6 @@ START_TEST(test_bip32_vector_2)
ck_assert_mem_eq(node.chain_code, fromhex("be17a268474a6bb9c61e1d720cf6215e2a88c5406c4aee7b38547f585c9a37d9"), 32);
ck_assert_mem_eq(node.private_key, fromhex("877c779ad9687164e9c2f4f0f4ff0340814392330693ce95a58fe18fd52e6e93"), 32);
ck_assert_mem_eq(node.public_key, fromhex("03c01e7425647bdefa82b12d9bad5e3e6865bee0502694b94ca58b666abc0a5c3b"), 33);
ck_assert_str_eq(node.address, "1Lke9bXGhn5VPrBuXgN12uGUphrttUErmk");
// [Chain m/0/2147483647'/1]
r = hdnode_private_ckd(&node, 1);
@ -172,7 +163,6 @@ START_TEST(test_bip32_vector_2)
ck_assert_mem_eq(node.chain_code, fromhex("f366f48f1ea9f2d1d3fe958c95ca84ea18e4c4ddb9366c336c927eb246fb38cb"), 32);
ck_assert_mem_eq(node.private_key, fromhex("704addf544a06e5ee4bea37098463c23613da32020d604506da8c0518e1da4b7"), 32);
ck_assert_mem_eq(node.public_key, fromhex("03a7d1d856deb74c508e05031f9895dab54626251b3806e16b4bd12e781a7df5b9"), 33);
ck_assert_str_eq(node.address, "1BxrAr2pHpeBheusmd6fHDP2tSLAUa3qsW");
// [Chain m/0/2147483647'/1/2147483646']
r = hdnode_private_ckd_prime(&node, 2147483646);
@ -181,7 +171,6 @@ START_TEST(test_bip32_vector_2)
ck_assert_mem_eq(node.chain_code, fromhex("637807030d55d01f9a0cb3a7839515d796bd07706386a6eddf06cc29a65a0e29"), 32);
ck_assert_mem_eq(node.private_key, fromhex("f1c7c871a54a804afe328b4c83a1c33b8e5ff48f5087273f04efa83b247d6a2d"), 32);
ck_assert_mem_eq(node.public_key, fromhex("02d2b36900396c9282fa14628566582f206a5dd0bcc8d5e892611806cafb0301f0"), 33);
ck_assert_str_eq(node.address, "15XVotxCAV7sRx1PSCkQNsGw3W9jT9A94R");
// [Chain m/0/2147483647'/1/2147483646'/2]
r = hdnode_private_ckd(&node, 2);
@ -190,10 +179,9 @@ START_TEST(test_bip32_vector_2)
ck_assert_mem_eq(node.chain_code, fromhex("9452b549be8cea3ecb7a84bec10dcfd94afe4d129ebfd3b3cb58eedf394ed271"), 32);
ck_assert_mem_eq(node.private_key, fromhex("bb7d39bdb83ecf58f2fd82b6d918341cbef428661ef01ab97c28a4842125ac23"), 32);
ck_assert_mem_eq(node.public_key, fromhex("024d902e1a2fc7a8755ab5b694c575fce742c48d9ff192e63df5193e4c7afe1f9c"), 33);
ck_assert_str_eq(node.address, "14UKfRV9ZPUp6ZC9PLhqbRtxdihW9em3xt");
// init m
hdnode_from_seed(0x00, fromhex("fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542"), 64, &node);
hdnode_from_seed(fromhex("fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542"), 64, &node);
// test public derivation
// [Chain m/0]
@ -203,7 +191,6 @@ START_TEST(test_bip32_vector_2)
ck_assert_mem_eq(node.chain_code, fromhex("f0909affaa7ee7abe5dd4e100598d4dc53cd709d5a5c2cac40e7412f232f7c9c"), 32);
ck_assert_mem_eq(node.private_key, fromhex("0000000000000000000000000000000000000000000000000000000000000000"), 32);
ck_assert_mem_eq(node.public_key, fromhex("02fc9e5af0ac8d9b3cecfe2a888e2117ba3d089d8585886c9c826b6b22a98d12ea"), 33);
ck_assert_str_eq(node.address, "19EuDJdgfRkwCmRzbzVBHZWQG9QNWhftbZ");
}
END_TEST
@ -211,8 +198,8 @@ START_TEST(test_bip32_compare)
{
HDNode node1, node2, node3;
int i, r;
hdnode_from_seed(0x00, fromhex("301133282ad079cbeb59bc446ad39d333928f74c46997d3609cd3e2801ca69d62788f9f174429946ff4e9be89f67c22fae28cb296a9b37734f75e73d1477af19"), 64, &node1);
hdnode_from_seed(0x00, fromhex("301133282ad079cbeb59bc446ad39d333928f74c46997d3609cd3e2801ca69d62788f9f174429946ff4e9be89f67c22fae28cb296a9b37734f75e73d1477af19"), 64, &node2);
hdnode_from_seed(fromhex("301133282ad079cbeb59bc446ad39d333928f74c46997d3609cd3e2801ca69d62788f9f174429946ff4e9be89f67c22fae28cb296a9b37734f75e73d1477af19"), 64, &node1);
hdnode_from_seed(fromhex("301133282ad079cbeb59bc446ad39d333928f74c46997d3609cd3e2801ca69d62788f9f174429946ff4e9be89f67c22fae28cb296a9b37734f75e73d1477af19"), 64, &node2);
for (i = 0; i < 300; i++) {
memcpy(&node3, &node1, sizeof(HDNode));
r = hdnode_private_ckd(&node1, i); ck_assert_int_eq(r, 1);
@ -230,8 +217,6 @@ START_TEST(test_bip32_compare)
ck_assert_mem_eq(node3.private_key, fromhex("0000000000000000000000000000000000000000000000000000000000000000"), 32);
ck_assert_mem_eq(node1.public_key, node2.public_key, 33);
ck_assert_mem_eq(node1.public_key, node3.public_key, 33);
ck_assert_str_eq(node1.address, node2.address);
ck_assert_str_eq(node1.address, node3.address);
}
}
END_TEST