Skip to content

Commit

Permalink
Adding rapidcheck dependency, adding CKey properties
Browse files Browse the repository at this point in the history
Adding rapidcheck dependency, adding CKey properties

Successfully compiling bitcoin with rapidcheck dependency

Adding new property file for CKey

Serialization symmetry for CKey -> CBitcoinSecret -> CKey

Adding crypto_gen.h - this file is meant to contain all of our crypto related generators

Adding generators for CPrivKey, CPubKey, and uint256

Adding properties to make sure we can Set a CKey using begin() and end(), and property to sign a uint256, then verify with with the pubkey

fixing some issues inside build files and nits introduced in some tests

Fixing issue where I accidentally removed zeromq from packages.mk

Fixing a few more nits

Fixing one last nit
  • Loading branch information
Christewart committed Aug 21, 2016
1 parent 2468292 commit 0a658bb
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 2 deletions.
6 changes: 6 additions & 0 deletions configure.ac
Expand Up @@ -555,6 +555,12 @@ if test x$use_upnp != xno; then
)
fi

dnl check for rapidcheck
AC_CHECK_HEADERS(
[rapidcheck.h],
[AC_CHECK_LIB([rapidcheck], [main],[], [])],
[])

BITCOIN_QT_INIT

dnl sets $bitcoin_enable_qt, $bitcoin_enable_qt_test, $bitcoin_enable_qt_dbus
Expand Down
2 changes: 1 addition & 1 deletion depends/packages/packages.mk
@@ -1,4 +1,4 @@
packages:=boost openssl libevent zeromq
packages:=boost openssl libevent zeromq rapidcheck
native_packages := native_ccache

qt_native_packages = native_protobuf
Expand Down
21 changes: 21 additions & 0 deletions depends/packages/rapidcheck.mk
@@ -0,0 +1,21 @@
package=rapidcheck

$(package)_version=1.0

$(package)_download_path=https://github.com/Christewart/rapidcheck/releases/download/1.0

$(package)_file_name=$(package)-$($(package)_version).tar.gz

$(package)_sha256_hash=c228dc21ec24618bfb6afa31d622d1f4ea71168f04ee499e1ffcfc63cd5833f4

define $(package)_preprocess_cmds
mkdir build
endef

define $(package)_config_cmds
cmake -DCMAKE_INSTALL_PREFIX:PATH=$(build_prefix)/bin ..
endef

define $(package)_build_cmds
$(MAKE)
endef
4 changes: 3 additions & 1 deletion src/Makefile.test.include
Expand Up @@ -54,6 +54,7 @@ BITCOIN_TESTS =\
test/getarg_tests.cpp \
test/hash_tests.cpp \
test/key_tests.cpp \
test/key_properties.cpp \
test/limitedmap_tests.cpp \
test/dbwrapper_tests.cpp \
test/main_tests.cpp \
Expand Down Expand Up @@ -89,7 +90,8 @@ BITCOIN_TESTS =\
test/versionbits_tests.cpp \
test/uint256_tests.cpp \
test/univalue_tests.cpp \
test/util_tests.cpp
test/util_tests.cpp \
test/gen/crypto_gen.h

if ENABLE_WALLET
BITCOIN_TESTS += \
Expand Down
50 changes: 50 additions & 0 deletions src/test/gen/crypto_gen.h
@@ -0,0 +1,50 @@
#include "key.h"
#include "random.h"
#include "uint256.h"
#include <rapidcheck/gen/Arbitrary.h>
#include <rapidcheck/Gen.h>

namespace rc {

/** Generator for a new CKey */
template<>
struct Arbitrary<CKey> {
static Gen<CKey> arbitrary() {
return gen::map<int>([](int x) {
CKey key;
key.MakeNewKey(true);
return key;
});
};
};

/** Generator for a CPrivKey */
template<>
struct Arbitrary<CPrivKey> {
static Gen<CPrivKey> arbitrary() {
return gen::map<CKey>([](CKey key) {
return key.GetPrivKey();
});
};
};

/** Generator for a new CPubKey */
template<>
struct Arbitrary<CPubKey> {
static Gen<CPubKey> arbitrary() {
return gen::map<CKey>([](CKey key) {
return key.GetPubKey();
});
};
};

/** Generates a arbitrary uint256 */
template<>
struct Arbitrary<uint256> {
static Gen<uint256> arbitrary() {
return gen::map<int>([](int x) {
return GetRandHash();
});
};
};
}
56 changes: 56 additions & 0 deletions src/test/key_properties.cpp
@@ -0,0 +1,56 @@
// Copyright (c) 2012-2016 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "key.h"

#include "base58.h"
#include "script/script.h"
#include "uint256.h"
#include "util.h"
#include "utilstrencodings.h"
#include "test/test_bitcoin.h"
#include <string>
#include <vector>

#include <boost/test/unit_test.hpp>
#include <rapidcheck/boost_test.h>
#include <rapidcheck/gen/Arbitrary.h>
#include <rapidcheck/Gen.h>

#include "test/gen/crypto_gen.h"
/** Generator for a new CKey */
BOOST_FIXTURE_TEST_SUITE(key_properties, BasicTestingSetup)

/** Check CKey uniqueness */
RC_BOOST_PROP(key_uniqueness, (CKey key1, CKey key2)) {
RC_ASSERT(!(key1 == key2));
}

/** Verify that a private key generates the correct public key */
RC_BOOST_PROP(key_generates_correct_pubkey, (CKey key)) {
CPubKey pubKey = key.GetPubKey();
RC_ASSERT(key.VerifyPubKey(pubKey));
}

/** Serialization symmetry CKey -> CBitcoinSecret -> CKey */
RC_BOOST_PROP(key_bitcoinsecret_symmetry, (CKey key)) {
CBitcoinSecret secret;
secret.SetKey(key);
RC_ASSERT(secret.GetKey() == key);
}

/** Create a CKey using the 'Set' function must give us the same key */
RC_BOOST_PROP(key_set_symmetry, (CKey key)) {
CKey key1;
key1.Set(key.begin(), key.end(), key.IsCompressed());
RC_ASSERT(key1 == key);
}

/** Create a CKey, sign a piece of data, then verify it with the public key */
RC_BOOST_PROP(key_sign_symmetry, (CKey key, uint256 hash)) {
std::vector<unsigned char> vchSig;
key.Sign(hash,vchSig,0);
CPubKey pubKey = key.GetPubKey();
RC_ASSERT(pubKey.Verify(hash,vchSig));
}
BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 0a658bb

Please sign in to comment.