From d5937633f56f00785022e0587d16be3f58ba422e Mon Sep 17 00:00:00 2001 From: subtly Date: Thu, 23 Oct 2014 05:04:25 +0200 Subject: [PATCH] cryptopp_vs_secp256k1 --- libdevcrypto/CryptoPP.cpp | 14 +++----------- libdevcrypto/CryptoPP.h | 8 ++++++-- libdevcrypto/EC.cpp | 2 +- test/crypto.cpp | 33 ++++++++++++++++++++++++++++++++- 4 files changed, 42 insertions(+), 15 deletions(-) diff --git a/libdevcrypto/CryptoPP.cpp b/libdevcrypto/CryptoPP.cpp index 7295ae78e..611036e72 100644 --- a/libdevcrypto/CryptoPP.cpp +++ b/libdevcrypto/CryptoPP.cpp @@ -28,13 +28,13 @@ using namespace dev::crypto; using namespace pp; using namespace CryptoPP; -void pp::exportDL_PublicKey_EC(CryptoPP::DL_PublicKey_EC const& _k, Public& _p) { +void pp::PublicFromDL_PublicKey_EC(CryptoPP::DL_PublicKey_EC const& _k, Public& _p) { bytes prefixedKey(65); _k.GetGroupParameters().GetCurve().EncodePoint(prefixedKey.data(), _k.GetPublicElement(), false); memcpy(_p.data(), &prefixedKey[1], 64); } -void pp::exportDL_PrivateKey_EC(CryptoPP::DL_PrivateKey_EC const& _k, Secret& _s) { +void pp::SecretFromDL_PrivateKey_EC(CryptoPP::DL_PrivateKey_EC const& _k, Secret& _s) { _k.GetPrivateExponent().Encode(_s.data(), 32); } @@ -48,14 +48,6 @@ ECP::Point pp::PointFromPublic(Public const& _p) CryptoPP::DL_PublicKey_EC pub; pub.AccessGroupParameters().Initialize(pp::secp256k1()); pub.GetGroupParameters().GetCurve().DecodePoint(p, prefixedKey.data(), 65); - - // Manually: -// Integer x(_p.data(), 32); -// Integer y(&_p.data()[31], 32); // unsure why offset must be 31 -// ECP::Point p(x,y); - -// ECP ecp; -// ecp.DecodePoint(p, _p.data(), _p.size); return std::move(p); } @@ -76,6 +68,6 @@ m_decryptor(pp::PRNG(), pp::secp256k1()) Secret pp::ECKeyPair::secret() { Secret s; - exportDL_PrivateKey_EC(m_decryptor.AccessKey(), s); + SecretFromDL_PrivateKey_EC(m_decryptor.AccessKey(), s); return std::move(s); } \ No newline at end of file diff --git a/libdevcrypto/CryptoPP.h b/libdevcrypto/CryptoPP.h index 645a08dc5..cd41bdf14 100644 --- a/libdevcrypto/CryptoPP.h +++ b/libdevcrypto/CryptoPP.h @@ -40,11 +40,15 @@ inline CryptoPP::AutoSeededRandomPool& PRNG() { static CryptoPP::AutoSeededRando /// EC curve used by CryptoPP inline CryptoPP::OID const& secp256k1() { static CryptoPP::OID curve = CryptoPP::ASN1::secp256k1(); return curve; } -void exportDL_PublicKey_EC(CryptoPP::DL_PublicKey_EC const& _k, Public& _p); -void exportDL_PrivateKey_EC(CryptoPP::DL_PrivateKey_EC const& _k, Secret& _s); +void PublicFromDL_PublicKey_EC(CryptoPP::DL_PublicKey_EC const& _k, Public& _p); + +void SecretFromDL_PrivateKey_EC(CryptoPP::DL_PrivateKey_EC const& _k, Secret& _s); +/// Helper for CryptoPP key CryptoPP::ECP::Point PointFromPublic(Public const& _p); + +/// Helper for CryptoPP key CryptoPP::Integer ExponentFromSecret(Secret const& _s); void ECIESEncrypt(CryptoPP::ECP::Point const& _point, byte*); diff --git a/libdevcrypto/EC.cpp b/libdevcrypto/EC.cpp index 23ca7e647..f532cb74e 100644 --- a/libdevcrypto/EC.cpp +++ b/libdevcrypto/EC.cpp @@ -75,7 +75,7 @@ ECKeyPair ECKeyPair::create() // export public key and set address ECIES::Encryptor e(k.m_decryptor.GetKey()); - pp::exportDL_PublicKey_EC(e.GetKey(), k.m_public); + pp::PublicFromDL_PublicKey_EC(e.GetKey(), k.m_public); k.m_address = dev::right160(dev::sha3(k.m_public.ref())); return k; diff --git a/test/crypto.cpp b/test/crypto.cpp index bb8db2b65..55ee1e176 100644 --- a/test/crypto.cpp +++ b/test/crypto.cpp @@ -37,6 +37,37 @@ using namespace CryptoPP; BOOST_AUTO_TEST_SUITE(devcrypto) +BOOST_AUTO_TEST_CASE(cryptopp_vs_secp256k1) +{ + ECIES::Decryptor d(pp::PRNG(), pp::secp256k1()); + ECIES::Encryptor e(d.GetKey()); + + Secret s; + pp::SecretFromDL_PrivateKey_EC(d.GetKey(), s); + + Public p; + pp::PublicFromDL_PublicKey_EC(e.GetKey(), p); + + assert(dev::toAddress(s) == right160(dev::sha3(p.ref()))); + + Secret previous = s; + for (auto i = 0; i < 30; i++) + { + ECIES::Decryptor d(pp::PRNG(), pp::secp256k1()); + ECIES::Encryptor e(d.GetKey()); + + Secret s; + pp::SecretFromDL_PrivateKey_EC(d.GetKey(), s); + assert(s!=previous); + + Public p; + pp::PublicFromDL_PublicKey_EC(e.GetKey(), p); + + /// wow, this worked. the first time. + assert(dev::toAddress(s) == right160(dev::sha3(p.ref()))); + } +} + BOOST_AUTO_TEST_CASE(cryptopp_private_secret_import) { ECKeyPair k = ECKeyPair::create(); @@ -50,7 +81,7 @@ BOOST_AUTO_TEST_CASE(cryptopp_public_export_import) ECIES::Encryptor e(d.GetKey()); Public p; - pp::exportDL_PublicKey_EC(e.GetKey(), p); + pp::PublicFromDL_PublicKey_EC(e.GetKey(), p); DL_PublicKey_EC pub; pub.Initialize(pp::secp256k1(), pp::PointFromPublic(p));