Browse Source

cryptopp_vs_secp256k1

cl-refactor
subtly 10 years ago
parent
commit
d5937633f5
  1. 14
      libdevcrypto/CryptoPP.cpp
  2. 8
      libdevcrypto/CryptoPP.h
  3. 2
      libdevcrypto/EC.cpp
  4. 33
      test/crypto.cpp

14
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<CryptoPP::ECP> const& _k, Public& _p) {
void pp::PublicFromDL_PublicKey_EC(CryptoPP::DL_PublicKey_EC<CryptoPP::ECP> 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<CryptoPP::ECP> const& _k, Secret& _s) {
void pp::SecretFromDL_PrivateKey_EC(CryptoPP::DL_PrivateKey_EC<CryptoPP::ECP> 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<CryptoPP::ECP> 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);
}

8
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<CryptoPP::ECP> const& _k, Public& _p);
void exportDL_PrivateKey_EC(CryptoPP::DL_PrivateKey_EC<CryptoPP::ECP> const& _k, Secret& _s);
void PublicFromDL_PublicKey_EC(CryptoPP::DL_PublicKey_EC<CryptoPP::ECP> const& _k, Public& _p);
void SecretFromDL_PrivateKey_EC(CryptoPP::DL_PrivateKey_EC<CryptoPP::ECP> 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*);

2
libdevcrypto/EC.cpp

@ -75,7 +75,7 @@ ECKeyPair ECKeyPair::create()
// export public key and set address
ECIES<ECP>::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;

33
test/crypto.cpp

@ -37,6 +37,37 @@ using namespace CryptoPP;
BOOST_AUTO_TEST_SUITE(devcrypto)
BOOST_AUTO_TEST_CASE(cryptopp_vs_secp256k1)
{
ECIES<ECP>::Decryptor d(pp::PRNG(), pp::secp256k1());
ECIES<ECP>::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<ECP>::Decryptor d(pp::PRNG(), pp::secp256k1());
ECIES<ECP>::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<ECP>::Encryptor e(d.GetKey());
Public p;
pp::exportDL_PublicKey_EC(e.GetKey(), p);
pp::PublicFromDL_PublicKey_EC(e.GetKey(), p);
DL_PublicKey_EC<ECP> pub;
pub.Initialize(pp::secp256k1(), pp::PointFromPublic(p));

Loading…
Cancel
Save