Changeset View
Changeset View
Standalone View
Standalone View
src/test/key_tests.cpp
Show First 20 Lines • Show All 168 Lines • ▼ Show 20 Lines | BOOST_CHECK(DecodeDestination(addr2, chainParams) == | ||||
CTxDestination(PKHash(pubkey2))); | CTxDestination(PKHash(pubkey2))); | ||||
BOOST_CHECK(DecodeDestination(addr1C, chainParams) == | BOOST_CHECK(DecodeDestination(addr1C, chainParams) == | ||||
CTxDestination(PKHash(pubkey1C))); | CTxDestination(PKHash(pubkey1C))); | ||||
BOOST_CHECK(DecodeDestination(addr2C, chainParams) == | BOOST_CHECK(DecodeDestination(addr2C, chainParams) == | ||||
CTxDestination(PKHash(pubkey2C))); | CTxDestination(PKHash(pubkey2C))); | ||||
for (int n = 0; n < 16; n++) { | for (int n = 0; n < 16; n++) { | ||||
std::string strMsg = strprintf("Very secret message %i: 11", n); | std::string strMsg = strprintf("Very secret message %i: 11", n); | ||||
uint256 hashMsg = Hash(strMsg.begin(), strMsg.end()); | uint256 hashMsg = Hash(strMsg); | ||||
// normal ECDSA signatures | // normal ECDSA signatures | ||||
std::vector<uint8_t> sign1, sign2, sign1C, sign2C; | std::vector<uint8_t> sign1, sign2, sign1C, sign2C; | ||||
BOOST_CHECK(key1.SignECDSA(hashMsg, sign1)); | BOOST_CHECK(key1.SignECDSA(hashMsg, sign1)); | ||||
BOOST_CHECK(key2.SignECDSA(hashMsg, sign2)); | BOOST_CHECK(key2.SignECDSA(hashMsg, sign2)); | ||||
BOOST_CHECK(key1C.SignECDSA(hashMsg, sign1C)); | BOOST_CHECK(key1C.SignECDSA(hashMsg, sign1C)); | ||||
▲ Show 20 Lines • Show All 91 Lines • ▼ Show 20 Lines | for (int n = 0; n < 16; n++) { | ||||
BOOST_CHECK(rS2.size() == 32); | BOOST_CHECK(rS2.size() == 32); | ||||
BOOST_CHECK(rE2 != rS2); | BOOST_CHECK(rE2 != rS2); | ||||
} | } | ||||
// test deterministic signing expected values | // test deterministic signing expected values | ||||
std::vector<uint8_t> detsig, detsigc; | std::vector<uint8_t> detsig, detsigc; | ||||
std::string strMsg = "Very deterministic message"; | std::string strMsg = "Very deterministic message"; | ||||
uint256 hashMsg = Hash(strMsg.begin(), strMsg.end()); | uint256 hashMsg = Hash(strMsg); | ||||
// ECDSA | // ECDSA | ||||
BOOST_CHECK(key1.SignECDSA(hashMsg, detsig)); | BOOST_CHECK(key1.SignECDSA(hashMsg, detsig)); | ||||
BOOST_CHECK(key1C.SignECDSA(hashMsg, detsigc)); | BOOST_CHECK(key1C.SignECDSA(hashMsg, detsigc)); | ||||
BOOST_CHECK(detsig == detsigc); | BOOST_CHECK(detsig == detsigc); | ||||
BOOST_CHECK(detsig == | BOOST_CHECK(detsig == | ||||
ParseHex("304402200c648ad9936cae4006f0b0d7bcbacdcdf5a14260eb550" | ParseHex("304402200c648ad9936cae4006f0b0d7bcbacdcdf5a14260eb550" | ||||
"c31ddb1eb1a13b1b58602201b868673bb5926d1610a07cd03692d" | "c31ddb1eb1a13b1b58602201b868673bb5926d1610a07cd03692d" | ||||
"fdcb98ed059314f66b457a794f2c4b8e79")); | "fdcb98ed059314f66b457a794f2c4b8e79")); | ||||
▲ Show 20 Lines • Show All 42 Lines • ▼ Show 20 Lines | BOOST_CHECK(detsig == | ||||
"6b4b1573c84da49a38405d")); | "6b4b1573c84da49a38405d")); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(key_signature_tests) { | BOOST_AUTO_TEST_CASE(key_signature_tests) { | ||||
// When entropy is specified, we should see at least one high R signature | // When entropy is specified, we should see at least one high R signature | ||||
// within 20 signatures | // within 20 signatures | ||||
CKey key = DecodeSecret(strSecret1); | CKey key = DecodeSecret(strSecret1); | ||||
std::string msg = "A message to be signed"; | std::string msg = "A message to be signed"; | ||||
uint256 msg_hash = Hash(msg.begin(), msg.end()); | uint256 msg_hash = Hash(msg); | ||||
std::vector<uint8_t> sig; | std::vector<uint8_t> sig; | ||||
bool found = false; | bool found = false; | ||||
for (int i = 1; i <= 20; ++i) { | for (int i = 1; i <= 20; ++i) { | ||||
sig.clear(); | sig.clear(); | ||||
BOOST_CHECK(key.SignECDSA(msg_hash, sig, false, i)); | BOOST_CHECK(key.SignECDSA(msg_hash, sig, false, i)); | ||||
found = sig[3] == 0x21 && sig[4] == 0x00; | found = sig[3] == 0x21 && sig[4] == 0x00; | ||||
if (found) { | if (found) { | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
BOOST_CHECK(found); | BOOST_CHECK(found); | ||||
// When entropy is not specified, we should always see low R signatures that | // When entropy is not specified, we should always see low R signatures that | ||||
// are less than 70 bytes in 256 tries We should see at least one signature | // are less than 70 bytes in 256 tries We should see at least one signature | ||||
// that is less than 70 bytes. | // that is less than 70 bytes. | ||||
found = true; | found = true; | ||||
bool found_small = false; | bool found_small = false; | ||||
for (int i = 0; i < 256; ++i) { | for (int i = 0; i < 256; ++i) { | ||||
sig.clear(); | sig.clear(); | ||||
msg = "A message to be signed" + ToString(i); | msg = "A message to be signed" + ToString(i); | ||||
msg_hash = Hash(msg.begin(), msg.end()); | msg_hash = Hash(msg); | ||||
BOOST_CHECK(key.SignECDSA(msg_hash, sig)); | BOOST_CHECK(key.SignECDSA(msg_hash, sig)); | ||||
found = sig[3] == 0x20; | found = sig[3] == 0x20; | ||||
BOOST_CHECK(sig.size() <= 70); | BOOST_CHECK(sig.size() <= 70); | ||||
found_small |= sig.size() < 70; | found_small |= sig.size() < 70; | ||||
} | } | ||||
BOOST_CHECK(found); | BOOST_CHECK(found); | ||||
BOOST_CHECK(found_small); | BOOST_CHECK(found_small); | ||||
} | } | ||||
Show All 33 Lines |