Browse Source

two ec curves walked into a bar...

cl-refactor
subtly 10 years ago
parent
commit
0ff0bf4a38
  1. 4
      libdevcrypto/CryptoPP.cpp
  2. 33
      test/crypto.cpp

4
libdevcrypto/CryptoPP.cpp

@ -49,11 +49,11 @@ Integer pp::ExponentFromSecret(Secret const& _s)
void pp::PublicFromExponent(Integer const& _e, Public& _p) { void pp::PublicFromExponent(Integer const& _e, Public& _p) {
CryptoPP::DL_PrivateKey_EC<CryptoPP::ECP> k; CryptoPP::DL_PrivateKey_EC<CryptoPP::ECP> k;
k.AccessGroupParameters().Initialize(ASN1::secp256r1()); k.AccessGroupParameters().Initialize(secp256k1());
k.SetPrivateExponent(_e); k.SetPrivateExponent(_e);
CryptoPP::DL_PublicKey_EC<CryptoPP::ECP> p; CryptoPP::DL_PublicKey_EC<CryptoPP::ECP> p;
p.AccessGroupParameters().Initialize(ASN1::secp256r1()); p.AccessGroupParameters().Initialize(secp256k1());
k.MakePublicKey(p); k.MakePublicKey(p);
pp::PublicFromDL_PublicKey_EC(p, _p); pp::PublicFromDL_PublicKey_EC(p, _p);
} }

33
test/crypto.cpp

@ -67,20 +67,39 @@ BOOST_AUTO_TEST_CASE(cryptopp_vs_secp256k1)
} }
} }
BOOST_AUTO_TEST_CASE(cryptopp_is_bad) BOOST_AUTO_TEST_CASE(cryptopp_keys_cryptor_sipaseckp256k1)
{ {
SecretKeyRef k; SecretKeyRef k;
Secret s = k.sec(); Secret s = k.sec();
/// Convert secret to exponent used by pp // Convert secret to exponent used by pp
Integer e = pp::ExponentFromSecret(k.sec()); Integer e = pp::ExponentFromSecret(s);
// Test that exported DL_EC private is same as exponent from Secret
CryptoPP::DL_PrivateKey_EC<CryptoPP::ECP> privatek;
privatek.AccessGroupParameters().Initialize(pp::secp256k1());
privatek.SetPrivateExponent(e);
assert(e == privatek.GetPrivateExponent());
// Test that exported secret is same as decryptor(privatek) secret
ECIES<ECP>::Decryptor d; ECIES<ECP>::Decryptor d;
// k.AccessGroupParameters().Initialize(ASN1::secp256r1()); d.AccessKey().AccessGroupParameters().Initialize(pp::secp256k1());
// k.SetPrivateExponent(_e); d.AccessKey().SetPrivateExponent(e);
assert(d.AccessKey().GetPrivateExponent() == e);
pp::SecretFromDL_PrivateKey_EC(d.GetKey(), s); // Test that decryptor->encryptor->public == private->makepublic->public
CryptoPP::DL_PublicKey_EC<CryptoPP::ECP> pubk;
pubk.AccessGroupParameters().Initialize(pp::secp256k1());
privatek.MakePublicKey(pubk);
ECIES<ECP>::Encryptor enc(d);
assert(pubk.GetPublicElement() == enc.AccessKey().GetPublicElement());
// Test against sipa/seckp256k1
Public p;
pp::PublicFromExponent(pp::ExponentFromSecret(s), p);
assert(toAddress(s) == dev::right160(dev::sha3(p.ref())));
assert(k.pub() == p);
} }
BOOST_AUTO_TEST_CASE(cryptopp_public_export_import) BOOST_AUTO_TEST_CASE(cryptopp_public_export_import)
@ -102,8 +121,6 @@ BOOST_AUTO_TEST_CASE(cryptopp_public_export_import)
pub.Initialize(pp::secp256k1(), pp::PointFromPublic(p)); pub.Initialize(pp::secp256k1(), pp::PointFromPublic(p));
assert(pub.GetPublicElement() == e.GetKey().GetPublicElement()); assert(pub.GetPublicElement() == e.GetKey().GetPublicElement());
////
SecretKeyRef k; SecretKeyRef k;
Public p2; Public p2;
pp::PublicFromExponent(pp::ExponentFromSecret(k.sec()), p2); pp::PublicFromExponent(pp::ExponentFromSecret(k.sec()), p2);

Loading…
Cancel
Save