Post a working solution to the problem below, along with your BTC address, and I'll send you 0.3 BTC. First come, first served. For proof that I do pay bounties instantly upon a working solution, please
see this thread.
Basically, I need to do a bunch of the math calculations in C++ (using Qt), and am stuck with deriving a BIP32 child key. I do have a fair bit done though. I can input a BIP32 private key, pick it apart, get the public key using ECDSA (compressed or uncompressed), I can encode the address from the key, and so on.
However, I'm stuck on deriving a child key. I can do the math just fine with PHP's gmp library, but I can't get the results from the PHP and C++ code to match up. They should match up perfectly, since it's just math. I've tried both, QCA (
http://delta.affinix.com/docs/qca/) and just using BIGNUM data type to no avail. So here's the problem:
Compressed Public Key: 023E4740D0BA639E28963F3476157B7CF2FB7C6FDF4254F97099CF8670B505EA59
Private Key: 5c22f8937210130ad1bbc50678a7c0a119a483d47928c323bf0baa3a57fa547d
Chain Code: 180c998615636cd875aa70c71cfa6b7bf570187a56d8c6d054e60b644d13e9d3
Key Index: 35 (00000023)
Here's a small chunk of code I currently have.
QByteArray pubkey = "023E4740D0BA639E28963F3476157B7CF2FB7C6FDF4254F97099CF8670B505EA5900000023";
QByteArray privkey = "5c22f8937210130ad1bbc50678a7c0a119a483d47928c323bf0baa3a57fa547d";
QByteArray chain_code = "180c998615636cd875aa70c71cfa6b7bf570187a56d8c6d054e60b644d13e9d3";
// HMAC SHA512 Hash
QCA::MessageAuthenticationCode hmac("hmac(sha512)", QCA::SecureArray());
QCA::SecureArray karr(chain_code);
QCA::SymmetricKey sk(karr);
hmac.setup(sk);
hmac.update(QCA::hexToArray(pubkey));
// Parse resulting hash
QByteArray result = hmac.final().toByteArray();
QCA::SecureArray arr_left(result.left(32));
QCA::SecureArray arr_key(privkey);
// Get big integers
QCA::BigInteger bnl(arr_left);
QCA::BigInteger bnk(arr_key);
QCA::BigInteger bno("115792089237316195423570985008687907852837564279074904382605163141518161494337");
// Do the math
bnl += bnk;
bnl %= bno;
// Return
QByteArray new_key = bnl.toArray().toByteArray().toHex();
return new_key;
The HMAC SHA512 hash is fine, and I get the correct result from both, PHP and C++, which is: fa634546bbddad6f51e8a7ff05976e0d8813aafb656bf0c7cacb53a200e9e3e624e199e9e83fa03
eedd5056ac2de8482be17fcb6d85ebf15faa1976e5df957aa
However, that's where it ends. Basically, I need the remainder of ((bnl + bnk) / bno). Problem seems to start just after the addition, as the results I get are just slightly different:
PHP: 56863dda2dedc07a23a46d057e3f2eaea1b82ecfde94b3eb89d6fddc58e43863
C++ 156863dda2dedc07a23a46d057e3f2eaea1b82ecfde94b3eb89d6fddc58e43863
For whatever reason, the C++ variant is throwing an extra '1' on the beginning. If I try to convert any of the integers into strings, they're completely off from what PHP gives me as strings for the numbers. Anyway, here's the current results I get for the new private key:
PHP: 56863dda2dedc07a23a46d057e3f2eafe70951e92f4c13afca049f4f88adf722
C++: 56863dda2dedc07a23a46d057e3f2eaea1b82ecfde94b3eb89d6fddc58e43863
I need a solution in C++ (using QCA, BIGNUM, whatever -- although I would prefer QCA) that does the proper math, and gives me the same result I'm getting in PHP. If you know how, please post your solution here along with your BTC address, and I'll send 0.3 BTC right away. Thanks!