Browse Source

coding standards, documentation, new exception syntax

cl-refactor
subtly 10 years ago
parent
commit
b50578f1e6
  1. 1
      libdevcore/Exceptions.h
  2. 32
      libdevcrypto/Common.cpp
  3. 4
      libdevcrypto/Common.h
  4. 1
      libwhisper/Message.h
  5. 9
      test/crypto.cpp

1
libdevcore/Exceptions.h

@ -24,7 +24,6 @@
#include <exception>
#include <boost/exception/all.hpp>
#include <boost/throw_exception.hpp>
#include <libdevcrypto/Common.h>
#include "CommonData.h"
#include "FixedHash.h"

32
libdevcrypto/Common.cpp

@ -110,34 +110,36 @@ bool dev::verify(Public _p, Signature _s, h256 _hash)
h256 Nonce::get(bool _commit)
{
// todo: atomic efface bit, periodic save, kdf, rr, rng
static h256 seed;
static string seedFile(getDataDir() + "/seed");
static mutex x;
lock_guard<mutex> l(x);
if (!seed)
// todo: encrypt
static h256 s_seed;
static string s_seedFile(getDataDir() + "/seed");
static mutex s_x;
lock_guard<mutex> l(s_x);
if (!s_seed)
{
static Nonce nonce;
bytes b = contents(seedFile);
static Nonce s_nonce;
bytes b = contents(s_seedFile);
if (b.size() == 32)
memcpy(seed.data(), b.data(), 32);
memcpy(s_seed.data(), b.data(), 32);
else
{
// todo: replace w/entropy from user and system
std::mt19937_64 s_eng(time(0));
std::uniform_int_distribution<uint16_t> d(0, 255);
for (unsigned i = 0; i < 32; ++i)
seed[i] = (byte)d(s_eng);
s_seed[i] = (byte)d(s_eng);
}
if (!seed)
if (!s_seed)
throw InvalidState();
// prevent seed reuse if process terminates abnormally
writeFile(seedFile, bytes());
writeFile(s_seedFile, bytes());
}
h256 prev(seed);
sha3(prev.ref(), seed.ref());
h256 prev(s_seed);
sha3(prev.ref(), s_seed.ref());
if (_commit)
writeFile(seedFile, seed.asBytes());
return seed;
writeFile(s_seedFile, s_seed.asBytes());
return std::move(s_seed);
}
Nonce::~Nonce()

4
libdevcrypto/Common.h

@ -114,8 +114,8 @@ private:
namespace crypto
{
struct InvalidState: virtual Exception {};
struct InvalidState: public dev::Exception {};
/**
* @brief Generator for nonce material
*/

1
libwhisper/Message.h

@ -28,6 +28,7 @@
#include <utility>
#include <libdevcore/RLP.h>
#include <libdevcore/Guards.h>
#include <libdevcrypto/Common.h>
#include <libdevcrypto/SHA3.h>
#include "Common.h"

9
test/crypto.cpp

@ -92,7 +92,7 @@ BOOST_AUTO_TEST_CASE(cryptopp_vs_secp256k1)
}
}
BOOST_AUTO_TEST_CASE(cryptopp_cryptopp_ecdsav)
BOOST_AUTO_TEST_CASE(cryptopp_cryptopp_secp256k1libport)
{
// cryptopp implementation of secp256k1lib sign_compact w/recid parameter and recovery of public key from signature
@ -128,6 +128,12 @@ BOOST_AUTO_TEST_CASE(cryptopp_cryptopp_ecdsav)
Integer s = (kInv * (Integer(secret.asBytes().data(), 32)*r + heInt)) % q;
BOOST_REQUIRE(!!r && !!s);
/*
// For future reference:
// According to maths, this codepath can't be reached, however, it's in secp256k1.
// Commenting this out diverges from codebase implementation.
// To be removed after upstream PR and proof are evaulated.
if (s > params.GetSubgroupOrder())
{
// note: this rarely happens
@ -135,6 +141,7 @@ BOOST_AUTO_TEST_CASE(cryptopp_cryptopp_ecdsav)
if (recid)
recid ^= 1;
}
*/
Signature sig;
r.Encode(sig.data(), 32);

Loading…
Cancel
Save