From 2df62d4877132d848924a7775983ac05bc66ecf3 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Sat, 21 Sep 2013 17:41:02 +0200 Subject: [PATCH] use unit tests via Check instead of small test programs --- .gitignore | 3 +- Makefile | 14 +--- ecdsa.h | 2 + test-bip32.c | 78 ------------------ test-pubkey.c | 58 -------------- test-rfc6979.c | 51 ------------ tests.c | 212 +++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 219 insertions(+), 199 deletions(-) delete mode 100644 test-bip32.c delete mode 100644 test-pubkey.c delete mode 100644 test-rfc6979.c create mode 100644 tests.c diff --git a/.gitignore b/.gitignore index 25ad0464d..ca4c1dd1f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ *.o -test-bip32 -test-pubkey test-rfc6979 test-speed test-verify +tests diff --git a/Makefile b/Makefile index 4e94b2f69..702bc724f 100644 --- a/Makefile +++ b/Makefile @@ -2,19 +2,13 @@ CC = gcc CFLAGS = -Wall -Os OBJS = bignum.o ecdsa.o secp256k1.o sha2.o rand.o hmac.o bip32.o ripemd160.o -all: test-bip32 test-pubkey test-rfc6979 test-speed test-verify +all: tests test-speed test-verify %.o: %.c %.h $(CC) $(CFLAGS) -o $@ -c $< -test-bip32: test-bip32.o $(OBJS) - gcc test-bip32.o $(OBJS) -o test-bip32 - -test-pubkey: test-pubkey.o $(OBJS) - gcc test-pubkey.o $(OBJS) -o test-pubkey - -test-rfc6979: test-rfc6979.o $(OBJS) - gcc test-rfc6979.o $(OBJS) -o test-rfc6979 +tests: tests.o $(OBJS) + gcc tests.o $(OBJS) -lcheck -o tests test-speed: test-speed.o $(OBJS) gcc test-speed.o $(OBJS) -o test-speed @@ -23,4 +17,4 @@ test-verify: test-verify.o $(OBJS) gcc test-verify.o $(OBJS) -o test-verify -lcrypto clean: - rm -f *.o test-bip32 test-pubkey test-rfc6979 test-speed test-verify + rm -f *.o tests test-speed test-verify diff --git a/ecdsa.h b/ecdsa.h index f58a60dd7..2f5028ebb 100644 --- a/ecdsa.h +++ b/ecdsa.h @@ -35,4 +35,6 @@ void ecdsa_get_public_key_compressed(const uint8_t *priv_key, uint8_t *pub_key); void ecdsa_get_address(const uint8_t *pub_key, char version, char *addr); int ecdsa_verify(const uint8_t *pub_key, const uint8_t *signature, const uint8_t *msg, uint32_t msg_len); +void generate_k_rfc6979(bignum256 *secret, const uint8_t *priv_key, const uint8_t *hash); + #endif diff --git a/test-bip32.c b/test-bip32.c deleted file mode 100644 index 261690b0f..000000000 --- a/test-bip32.c +++ /dev/null @@ -1,78 +0,0 @@ -/** - * Copyright (c) 2013 Pavol Rusnak - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES - * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, - * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR - * OTHER DEALINGS IN THE SOFTWARE. - */ - -#include - -#include "bip32.h" - -// test vectors from https://en.bitcoin.it/wiki/BIP_0032_TestVectors - -void xprv_print(xprv *in) -{ - int i; - - printf("chain : "); for (i = 0; i < 32; i++) printf("%02x", in->chain_code[i]); printf("\n"); - printf("priv : "); for (i = 0; i < 32; i++) printf("%02x", in->private_key[i]); printf("\n"); - printf("pub : "); for (i = 0; i < 33; i++) printf("%02x", in->public_key[i]); printf("\n"); - printf("addr : "); printf("%s\n", in->address); - printf("\n"); -} - -int main() -{ - xprv node; - - xprv_from_seed((uint8_t *)"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f", 16, &node); - - printf("[Chain m] got\n"); - xprv_print(&node); - printf("[Chain m] expected\n"); - printf("chain : 873dff81c02f525623fd1fe5167eac3a55a049de3d314bb42ee227ffed37d508\n"); - printf("priv : e8f32e723decf4051aefac8e2c93c9c5b214313817cdb01a1494b917c8436b35\n"); - printf("pub : 0339a36013301597daef41fbe593a02cc513d0b55527ec2df1050e2e8ff49c85c2\n"); - printf("addr : 15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma\n"); - printf("\n"); - - xprv_descent_prime(&node, 0); - - printf("[Chain m/0'] got\n"); - xprv_print(&node); - printf("[Chain m/0'] expected\n"); - printf("chain : 47fdacbd0f1097043b78c63c20c34ef4ed9a111d980047ad16282c7ae6236141\n"); - printf("priv : edb2e14f9ee77d26dd93b4ecede8d16ed408ce149b6cd80b0715a2d911a0afea\n"); - printf("pub : 035a784662a4a20a65bf6aab9ae98a6c068a81c52e4b032c0fb5400c706cfccc56\n"); - printf("addr : 19Q2WoS5hSS6T8GjhK8KZLMgmWaq4neXrh\n"); - printf("\n"); - - xprv_descent(&node, 1); - - printf("[Chain m/0'/1] got\n"); - xprv_print(&node); - printf("[Chain m/0'/1] expected\n"); - printf("chain : 2a7857631386ba23dacac34180dd1983734e444fdbf774041578e9b6adb37c19\n"); - printf("priv : 3c6cb8d0f6a264c91ea8b5030fadaa8e538b020f0a387421a12de9319dc93368\n"); - printf("pub : 03501e454bf00751f24b1b489aa925215d66af2234e3891c3b21a52bedb3cd711c\n"); - printf("addr : 1JQheacLPdM5ySCkrZkV66G2ApAXe1mqLj\n"); - printf("\n"); - - return 0; -} diff --git a/test-pubkey.c b/test-pubkey.c deleted file mode 100644 index 28797e475..000000000 --- a/test-pubkey.c +++ /dev/null @@ -1,58 +0,0 @@ -/** - * Copyright (c) 2013 Pavol Rusnak - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES - * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, - * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR - * OTHER DEALINGS IN THE SOFTWARE. - */ - -#include -#include "ecdsa.h" - -// vectors from https://en.bitcoin.it/wiki/BIP_0032_TestVectors - -const char *privs[] = { -"\xe8\xf3\x2e\x72\x3d\xec\xf4\x05\x1a\xef\xac\x8e\x2c\x93\xc9\xc5\xb2\x14\x31\x38\x17\xcd\xb0\x1a\x14\x94\xb9\x17\xc8\x43\x6b\x35", -"\xed\xb2\xe1\x4f\x9e\xe7\x7d\x26\xdd\x93\xb4\xec\xed\xe8\xd1\x6e\xd4\x08\xce\x14\x9b\x6c\xd8\x0b\x07\x15\xa2\xd9\x11\xa0\xaf\xea", -"\x3c\x6c\xb8\xd0\xf6\xa2\x64\xc9\x1e\xa8\xb5\x03\x0f\xad\xaa\x8e\x53\x8b\x02\x0f\x0a\x38\x74\x21\xa1\x2d\xe9\x31\x9d\xc9\x33\x68", -"\xcb\xce\x0d\x71\x9e\xcf\x74\x31\xd8\x8e\x6a\x89\xfa\x14\x83\xe0\x2e\x35\x09\x2a\xf6\x0c\x04\x2b\x1d\xf2\xff\x59\xfa\x42\x4d\xca", -"\x0f\x47\x92\x45\xfb\x19\xa3\x8a\x19\x54\xc5\xc7\xc0\xeb\xab\x2f\x9b\xdf\xd9\x6a\x17\x56\x3e\xf2\x8a\x6a\x4b\x1a\x2a\x76\x4e\xf4", -"\x47\x1b\x76\xe3\x89\xe5\x28\xd6\xde\x6d\x81\x68\x57\xe0\x12\xc5\x45\x50\x51\xca\xd6\x66\x08\x50\xe5\x83\x72\xa6\xc3\xe6\xe7\xc8", -}; - -const char *pubs[] = { -"0339a36013301597daef41fbe593a02cc513d0b55527ec2df1050e2e8ff49c85c2", -"035a784662a4a20a65bf6aab9ae98a6c068a81c52e4b032c0fb5400c706cfccc56", -"03501e454bf00751f24b1b489aa925215d66af2234e3891c3b21a52bedb3cd711c", -"0357bfe1e341d01c69fe5654309956cbea516822fba8a601743a012a7896ee8dc2", -"02e8445082a72f29b75ca48748a914df60622a609cacfce8ed0e35804560741d29", -"022a471424da5e657499d1ff51cb43c47481a03b1e77f951fe64cec9f5a48f7011", -}; - -int main() -{ - int i, k; - uint8_t pub[33]; - - for (k = 0; k < 6; k++) { - ecdsa_get_public_key_compressed((uint8_t *)privs[k], pub); - printf("got : "); for (i = 0; i < 33; i++) printf("%02x", pub[i]); printf("\n"); - printf("expected : %s\n", pubs[k]); - } - - return 0; -} diff --git a/test-rfc6979.c b/test-rfc6979.c deleted file mode 100644 index b21519d65..000000000 --- a/test-rfc6979.c +++ /dev/null @@ -1,51 +0,0 @@ -/** - * Copyright (c) 2013 Pavol Rusnak - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES - * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, - * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR - * OTHER DEALINGS IN THE SOFTWARE. - */ - -#include -#include "bignum.h" -#include "ecdsa.h" -#include "sha2.h" - -bignum256 k; -uint8_t kb[32]; -uint8_t priv[32] = {0xcc, 0xa9, 0xfb, 0xcc, 0x1b, 0x41, 0xe5, 0xa9, 0x5d, 0x36, 0x9e, 0xaa, 0x6d, 0xdc, 0xff, 0x73, 0xb6, 0x1a, 0x4e, 0xfa, 0xa2, 0x79, 0xcf, 0xc6, 0x56, 0x7e, 0x8d, 0xaa, 0x39, 0xcb, 0xaf, 0x50}; -uint8_t hash[32]; - -void generate_k_rfc6979(bignum256 *k, const uint8_t *priv_key, const uint8_t *hash); - -int main() -{ - int i; - - SHA256_Raw((uint8_t *)"sample", 6, hash); - printf("hash : "); - for (i = 0; i < 32; i++) printf("%02x", hash[i]); printf("\n"); - generate_k_rfc6979(&k, priv, hash); - bn_write_be(&k, kb); - - printf("expected : 2df40ca70e639d89528a6b670d9d48d9165fdc0febc0974056bdce192b8e16a3\n"); - printf("got : "); - for (i = 0; i < 32; i++) printf("%02x", kb[i]); - printf("\n"); - - return 0; -} diff --git a/tests.c b/tests.c new file mode 100644 index 000000000..943439c77 --- /dev/null +++ b/tests.c @@ -0,0 +1,212 @@ +/** + * Copyright (c) 2013 Pavol Rusnak + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ + +#include +#include + +#include "bignum.h" +#include "bip32.h" +#include "ecdsa.h" +#include "sha2.h" + +uint8_t *fromhex(const char *str) +{ + static uint8_t buf[128]; + uint8_t c; + size_t i; + for (i = 0; i < strlen(str) / 2; i++) { + c = 0; + if (str[i*2] >= '0' && str[i*2] <= '9') c += (str[i*2] - '0') << 4; + if (str[i*2] >= 'a' && str[i*2] <= 'f') c += (10 + str[i*2] - 'a') << 4; + if (str[i*2+1] >= '0' && str[i*2+1] <= '9') c += (str[i*2+1] - '0'); + if (str[i*2+1] >= 'a' && str[i*2+1] <= 'f') c += (10 + str[i*2+1] - 'a'); + buf[i] = c; + } + return buf; +} + +char *tohex(const uint8_t *bin, size_t l) +{ + static char buf[257], digits[] = "0123456789abcdef"; + size_t i; + for (i = 0; i < l; i++) { + buf[i*2 ] = digits[(bin[i] >> 4) & 0xF]; + buf[i*2+1] = digits[bin[i] & 0xF]; + } + return buf; +} + +#define _ck_assert_mem(X, Y, L, OP) do { \ + const void* _ck_x = (X); \ + const void* _ck_y = (Y); \ + size_t _ck_l = (L); \ + ck_assert_msg(0 OP memcmp(_ck_y, _ck_x, _ck_l), \ + "Assertion '"#X#OP#Y"' failed: "#X"==\"%s\"", tohex(_ck_x, _ck_l)); \ +} while (0) +#define ck_assert_mem_eq(X, Y, L) _ck_assert_mem(X, Y, L, ==) +#define ck_assert_mem_ne(X, Y, L) _ck_assert_mem(X, Y, L, !=) + +// test vector 1 from https://en.bitcoin.it/wiki/BIP_0032_TestVectors +START_TEST(test_bip32_vector_1) +{ + xprv node; + + // init m + xprv_from_seed(fromhex("000102030405060708090a0b0c0d0e0f"), 16, &node); + + // [Chain m] + 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'] + xprv_descent_prime(&node, 0); + 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] + xprv_descent(&node, 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'] + xprv_descent_prime(&node, 2); + 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] + xprv_descent(&node, 2); + 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] + xprv_descent(&node, 1000000000); + 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 + +// test vector 2 from https://en.bitcoin.it/wiki/BIP_0032_TestVectors +START_TEST(test_bip32_vector_2) +{ + xprv node; + + // init m + xprv_from_seed(fromhex("fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542"), 64, &node); + + // [Chain m] + 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] + xprv_descent(&node, 0); + 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'] + xprv_descent_prime(&node, 2147483647); + 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] + xprv_descent(&node, 1); + 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'] + xprv_descent_prime(&node, 2147483646); + 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] + xprv_descent(&node, 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"); +} +END_TEST + +START_TEST(test_rfc6979) +{ + bignum256 k; + uint8_t buf[32]; + + SHA256_Raw((uint8_t *)"sample", 6, buf); + generate_k_rfc6979(&k, fromhex("cca9fbcc1b41e5a95d369eaa6ddcff73b61a4efaa279cfc6567e8daa39cbaf50"), buf); + bn_write_be(&k, buf); + + ck_assert_mem_eq(buf, fromhex("2df40ca70e639d89528a6b670d9d48d9165fdc0febc0974056bdce192b8e16a3"), 32); +} +END_TEST + +// define test suite and cases +Suite *test_suite(void) +{ + Suite *s = suite_create("MicroECDSA"); + TCase *tc; + + tc = tcase_create("bip32"); + tcase_add_test(tc, test_bip32_vector_1); + tcase_add_test(tc, test_bip32_vector_2); + suite_add_tcase(s, tc); + + tc = tcase_create("rfc6979"); + tcase_add_test(tc, test_rfc6979); + suite_add_tcase(s, tc); + + return s; +} + +// run suite +int main() +{ + int number_failed; + Suite *s = test_suite(); + SRunner *sr = srunner_create(s); + srunner_run_all(sr, CK_NORMAL); + number_failed = srunner_ntests_failed(sr); + srunner_free(sr); + return number_failed; +}