From 9eb90ebbd9c9498e23392b232e1cb9e9d86adaba Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Sun, 5 Jan 2014 18:47:12 +0000 Subject: [PATCH] Signatures working. --- main.cpp | 65 ++++++++++++++++++++++++++------------------------------ 1 file changed, 30 insertions(+), 35 deletions(-) diff --git a/main.cpp b/main.cpp index 006814368..537843c3e 100644 --- a/main.cpp +++ b/main.cpp @@ -7,35 +7,15 @@ using namespace std; using namespace eth; -std::string randomWord() -{ - static std::mt19937_64 s_eng(0); - std::string ret(uniform_int_distribution(4, 10)(s_eng), ' '); - char const n[] = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890"; - uniform_int_distribution d(0, sizeof(n) - 2); - for (char& c: ret) - c = n[d(s_eng)]; - return ret; -} - int main() { - secp256k1_start(); - - bytes pubkey(65); - int pubkeylen = 65; - { - cout << "SEC: " << asHex(sha256("123", false)) << endl; - int ret = secp256k1_ecdsa_pubkey_create(pubkey.data(), &pubkeylen, (byte const*)sha256("123", false).data(), 1); - pubkey.resize(pubkeylen); - cout << "PUB: " << ret << " " << pubkeylen << " " << asHex(pubkey) << endl; - } - bytes tx = fromUserHex("88005401010101010101010101010101010101010101011f0de0b6b3a76400001ce8d4a5100080181c373130a009ba1f10285d4e659568bfcfec85067855c5a3c150100815dad4ef98fd37cf0593828c89db94bd6c64e210a32ef8956eaa81ea9307194996a3b879441f5d"); cout << "TX: " << RLP(tx) << endl; Transaction t(tx); - std::string sig64 = toBigEndianString(t.vrs.r) + toBigEndianString(t.vrs.s); + cout << "SENDER: " << hex << t.sender() << endl; + + bytes sig64 = toBigEndian(t.vrs.r) + toBigEndian(t.vrs.s); cout << "SIG: " << sig64.size() << " " << asHex(sig64) << " " << t.vrs.v << endl; auto msg = t.rlp(false); @@ -44,30 +24,45 @@ int main() std::string hmsg = sha256(t.rlpString(false), false); cout << "SHA256(RLP(TX w/o SIG)): 0x" << asHex(hmsg) << endl; + bytes privkey = sha256Bytes("123"); + + secp256k1_start(); + + { + bytes pubkey(65); + int pubkeylen = 65; + + int ret = secp256k1_ecdsa_seckey_verify(privkey.data()); + cout << "SEC: " << dec << ret << " " << asHex(privkey) << endl; + + ret = secp256k1_ecdsa_pubkey_create(pubkey.data(), &pubkeylen, privkey.data(), 1); + pubkey.resize(pubkeylen); + int good = secp256k1_ecdsa_pubkey_verify(pubkey.data(), pubkey.size()); + cout << "PUB: " << dec << ret << " " << pubkeylen << " " << asHex(pubkey) << (good ? " GOOD" : " BAD") << endl; + } + + // Test roundtrip... { bytes sig(64); u256 nonce = 0; int v = 0; - int ret = secp256k1_ecdsa_sign_compact((byte const*)hmsg.data(), hmsg.size(), sig.data(), (byte const*)sha256("123", false).data(), (byte const*)&nonce, &v); + int ret = secp256k1_ecdsa_sign_compact((byte const*)hmsg.data(), hmsg.size(), sig.data(), privkey.data(), (byte const*)&nonce, &v); cout << "MYSIG: " << dec << ret << " " << sig.size() << " " << asHex(sig) << " " << v << endl; - ret = secp256k1_ecdsa_recover_compact((byte const*)hmsg.data(), hmsg.size(), (byte const*)sig.data(), pubkey.data(), &pubkeylen, 1, (int)t.vrs.v); + bytes pubkey(65); + int pubkeylen = 65; + ret = secp256k1_ecdsa_recover_compact((byte const*)hmsg.data(), hmsg.size(), (byte const*)sig.data(), pubkey.data(), &pubkeylen, 0, v); pubkey.resize(pubkeylen); cout << "MYREC: " << dec << ret << " " << pubkeylen << " " << asHex(pubkey) << endl; } { - pubkey.resize(65); - int ret = secp256k1_ecdsa_recover_compact((byte const*)hmsg.data(), hmsg.size(), (byte const*)sig64.data(), pubkey.data(), &pubkeylen, 1, (int)t.vrs.v); + bytes pubkey(65); + int pubkeylen = 65; + int ret = secp256k1_ecdsa_recover_compact((byte const*)hmsg.data(), hmsg.size(), (byte const*)sig64.data(), pubkey.data(), &pubkeylen, 0, (int)t.vrs.v - 27); pubkey.resize(pubkeylen); - cout << "REC: " << dec << ret << " " << pubkeylen << " " << asHex(pubkey) << endl; - cout << hex << sha256(pubkey) << endl; - - pubkey.resize(65); - ret = secp256k1_ecdsa_recover_compact((byte const*)hmsg.data(), hmsg.size(), (byte const*)sig64.data(), pubkey.data(), &pubkeylen, 0, (int)t.vrs.v); - pubkey.resize(pubkeylen); - cout << "REC+: " << dec << ret << " " << pubkeylen << " " << asHex(pubkey) << endl; - cout << hex << sha256(pubkey) << endl; + cout << "RECPUB: " << dec << ret << " " << pubkeylen << " " << asHex(pubkey) << endl; + cout << "SENDER: " << hex << low160(eth::sha256(bytesConstRef(&pubkey).cropped(1))) << endl; } {