diff --git a/Makefile.am b/Makefile.am index be0ec4312..ebcd9c48d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -242,7 +242,7 @@ cov: test_bitcoin.coverage/.dirstamp cov-zcash total.coverage/.dirstamp endif -dist_bin_SCRIPTS = #zcutil/fetch-params.sh +dist_bin_SCRIPTS = scripts/fetch-zcash-params.sh dist_noinst_SCRIPTS = autogen.sh EXTRA_DIST = $(DIST_SHARE) test/functional/test_runner.py test/functional test/zcash $(DIST_CONTRIB) $(DIST_DOCS) $(WINDOWS_PACKAGING) $(OSX_PACKAGING) $(BIN_CHECKS) @@ -302,8 +302,6 @@ EXTRA_DIST += \ test/util/rpcauth-test.py CLEANFILES = $(OSX_DMG) $(BITCOIN_WIN_INSTALLER) -install-exec-hook: - mv $(DESTDIR)$(bindir)/fetch-params.sh $(DESTDIR)$(bindir)/zcash-fetch-params .INTERMEDIATE: $(COVERAGE_INFO) diff --git a/configure.ac b/configure.ac index 04808cd8e..622d26507 100644 --- a/configure.ac +++ b/configure.ac @@ -1161,7 +1161,7 @@ AX_CHECK_COMPILE_FLAG([-fwrapv],[CXXFLAGS="$CXXFLAGS -fwrapv"]) AX_CHECK_COMPILE_FLAG([-fno-strict-aliasing],[CXXFLAGS="$CXXFLAGS -fno-strict-aliasing"]) AX_CHECK_COMPILE_FLAG([-Wno-builtin-declaration-mismatch],[CXXFLAGS="$CXXFLAGS -Wno-builtin-declaration-mismatch"],,[[$CXXFLAG_WERROR]]) -LIBZCASH_LIBS="-lgmp -lgmpxx $BOOST_SYSTEM_LIB -lcrypto -lsodium $RUST_LIBS" +LIBZCASH_LIBS="-lgomp -lgmp -lgmpxx $BOOST_SYSTEM_LIB -lcrypto -lsodium $RUST_LIBS" BITCOIN_QT_PATH_PROGS([PROTOC], [protoc],$protoc_bin_path) diff --git a/scripts/build.sh b/scripts/build.sh index 13587a0ad..432b7a15b 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -103,5 +103,5 @@ ld -v HOST="$HOST" BUILD="$BUILD" "$MAKE" "$@" -C ./depends/ V=1 ./autogen.sh -CC="$CC" CXX="$CXX" ./configure --disable-wallet --prefix="${PREFIX}" --host="$HOST" --build="$BUILD" "$HARDENING_ARG" "$LCOV_ARG" "$TEST_ARG" "$MINING_ARG" $CONFIGURE_FLAGS --enable-werror CXXFLAGS='-g' +CC="$CC" CXX="$CXX" ./configure --prefix="${PREFIX}" --host="$HOST" --build="$BUILD" "$HARDENING_ARG" "$LCOV_ARG" "$TEST_ARG" "$MINING_ARG" $CONFIGURE_FLAGS --enable-werror CXXFLAGS='-g' "$MAKE" "$@" V=1 diff --git a/scripts/fetch-zcash-params.sh b/scripts/fetch-zcash-params.sh new file mode 100755 index 000000000..443e6ba0a --- /dev/null +++ b/scripts/fetch-zcash-params.sh @@ -0,0 +1,215 @@ +#!/bin/bash + +set -eu + +if [[ "$OSTYPE" == "darwin"* ]]; then + PARAMS_DIR="$HOME/Library/Application Support/ZcashParams" +else + PARAMS_DIR="$HOME/.zcash-params" +fi + +SPROUT_PKEY_NAME='sprout-proving.key' +SPROUT_VKEY_NAME='sprout-verifying.key' +SPROUT_URL="https://z.cash/downloads" +SPROUT_IPFS="/ipfs/QmZKKx7Xup7LiAtFRhYsE1M7waXcv9ir9eCECyXAFGxhEo" + +SHA256CMD="$(command -v sha256sum || echo shasum)" +SHA256ARGS="$(command -v sha256sum >/dev/null || echo '-a 256')" + +WGETCMD="$(command -v wget || echo '')" +IPFSCMD="$(command -v ipfs || echo '')" +CURLCMD="$(command -v curl || echo '')" + +# fetch methods can be disabled with ZC_DISABLE_SOMETHING=1 +ZC_DISABLE_WGET="${ZC_DISABLE_WGET:-}" +ZC_DISABLE_IPFS="${ZC_DISABLE_IPFS:-}" +ZC_DISABLE_CURL="${ZC_DISABLE_CURL:-}" + +function fetch_wget { + if [ -z "$WGETCMD" ] || ! [ -z "$ZC_DISABLE_WGET" ]; then + return 1 + fi + + local filename="$1" + local dlname="$2" + + cat <&2 <&2 + exit 1 + fi + fi +} + +# Use flock to prevent parallel execution. +function lock() { + local lockfile=/tmp/fetch_params.lock + if [[ "$OSTYPE" == "darwin"* ]]; then + if shlock -f ${lockfile} -p $$; then + return 0 + else + return 1 + fi + else + # create lock file + eval "exec 200>/$lockfile" + # acquire the lock + flock -n 200 \ + && return 0 \ + || return 1 + fi +} + +function exit_locked_error { + echo "Only one instance of fetch-zcash-params.sh can be run at a time." >&2 + exit 1 +} + +function main() { + + lock fetch-zcash-params.sh \ + || exit_locked_error + + cat <> "$README_PATH" <(1,111); - base58Prefixes[SCRIPT_ADDRESS] = std::vector(1,196); - base58Prefixes[SECRET_KEY] = std::vector(1,239); - base58Prefixes[EXT_PUBLIC_KEY] = {0x04, 0x35, 0x87, 0xCF}; - base58Prefixes[EXT_SECRET_KEY] = {0x04, 0x35, 0x83, 0x94}; + base58Prefixes[PUBKEY_ADDRESS] = {0x19,0x57}; + base58Prefixes[SCRIPT_ADDRESS] = {0x19,0xE0}; + base58Prefixes[SECRET_KEY] = {0xEF}; + base58Prefixes[EXT_PUBLIC_KEY] = {0x04, 0x35, 0x87, 0xCF}; + base58Prefixes[EXT_SECRET_KEY] = {0x04, 0x35, 0x83, 0x94}; base58Prefixes[ZCPAYMENT_ADDRRESS] = {0x16,0xC0}; base58Prefixes[ZCSPENDING_KEY] = {0xAC,0x08}; bech32_hrp = "regbtcp"; diff --git a/src/consensus/joinsplit.cpp b/src/consensus/joinsplit.cpp index cf0ca635d..b00d9dd71 100644 --- a/src/consensus/joinsplit.cpp +++ b/src/consensus/joinsplit.cpp @@ -32,7 +32,7 @@ bool CheckTransactionJoinsplits(const CTransaction& tx, CValidationState &state) // Ensure that zk-SNARKs verify auto verifier = libzcash::ProofVerifier::Strict(); for(const JSDescription &joinsplit : tx.vjoinsplit) { - if (!joinsplit.Verify(*pzcashParams, verifier, tx.joinSplitPubKey)) { + if (!joinsplit.Verify(pzcashParams.get(), verifier, tx.joinSplitPubKey)) { return state.DoS(100, error("CheckTransaction(): joinsplit does not verify"), REJECT_INVALID, "bad-txns-joinsplit-verification-failed"); } diff --git a/src/init.cpp b/src/init.cpp index 2d411e277..0b7b2b8b6 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -60,6 +60,8 @@ #include #include +#include + #if ENABLE_ZMQ #include #endif @@ -72,6 +74,8 @@ static const bool DEFAULT_STOPAFTERBLOCKIMPORT = false; std::unique_ptr g_connman; std::unique_ptr peerLogic; +std::unique_ptr pzcashParams; + #if !(ENABLE_WALLET) class DummyWalletInit : public WalletInitInterface { public: @@ -289,6 +293,7 @@ void Shutdown() GetMainSignals().UnregisterWithMempoolSignals(mempool); g_wallet_init_interface.Close(); globalVerifyHandle.reset(); + ECC_Stop(); LogPrintf("%s: done\n", __func__); } @@ -698,6 +703,35 @@ static void ThreadImport(std::vector vImportFiles) g_is_mempool_loaded = !fRequestShutdown; } +static void ZC_LoadParams() +{ + struct timeval tv_start, tv_end; + float elapsed; + + boost::filesystem::path pk_path = ZC_GetParamsDir() / "sprout-proving.key"; + boost::filesystem::path vk_path = ZC_GetParamsDir() / "sprout-verifying.key"; + + if (!(boost::filesystem::exists(pk_path) && boost::filesystem::exists(vk_path))) { + uiInterface.ThreadSafeMessageBox(strprintf( + _("Cannot find the Zcash ceremony parameters for BTCP in the following directory:\n" + "%s\n" + "Please run 'scripts/fetch-zcash-params.sh' and then restart."), + ZC_GetParamsDir()), + "", CClientUIInterface::MSG_ERROR); + StartShutdown(); + return; + } + + LogPrintf("Loading verifying key from %s\n", vk_path.string().c_str()); + gettimeofday(&tv_start, 0); + + pzcashParams = std::unique_ptr(ZCJoinSplit::Prepared(vk_path.string(), pk_path.string())); + + gettimeofday(&tv_end, 0); + elapsed = float(tv_end.tv_sec-tv_start.tv_sec) + (tv_end.tv_usec-tv_start.tv_usec)/float(1000000); + LogPrintf("Loaded verifying key in %fs seconds.\n", elapsed); +} + /** Sanity checks * Ensure that Bitcoin is running in a usable environment with all * necessary library support. @@ -1267,6 +1301,14 @@ bool AppInitMain() threadGroup.create_thread(&ThreadScriptCheck); } + // These must be disabled for now, they are buggy and we probably don't + // want any of libsnark's profiling in production anyway. + libsnark::inhibit_profiling_info = true; + libsnark::inhibit_profiling_counters = true; + + // Initialize Zcash circuit parameters + ZC_LoadParams(); + // Start the lightweight task scheduler thread CScheduler::Function serviceLoop = boost::bind(&CScheduler::serviceQueue, &scheduler); threadGroup.create_thread(boost::bind(&TraceThread, "scheduler", serviceLoop)); diff --git a/src/init.h b/src/init.h index 5423a042a..0d299d586 100644 --- a/src/init.h +++ b/src/init.h @@ -10,6 +10,8 @@ #include #include +#include + class CScheduler; class CWallet; @@ -21,6 +23,8 @@ namespace boost class thread_group; } // namespace boost +extern std::unique_ptr pzcashParams; + void StartShutdown(); bool ShutdownRequested(); /** Interrupt threads */ diff --git a/src/logging.cpp b/src/logging.cpp index ded48e8eb..af6628025 100644 --- a/src/logging.cpp +++ b/src/logging.cpp @@ -121,6 +121,8 @@ const CLogCategoryDesc LogCategories[] = {BCLog::LEVELDB, "leveldb"}, {BCLog::EQUIHASH, "equihash"}, {BCLog::PAYMENTDISCLOSURE, "paymentdisclosure"}, + {BCLog::ZRPC, "zrpc"}, + {BCLog::ZRPCUNSAFE, "zrpcunsafe"}, {BCLog::ALL, "1"}, {BCLog::ALL, "all"}, }; diff --git a/src/logging.h b/src/logging.h index 71eb03da3..5f06b5c92 100644 --- a/src/logging.h +++ b/src/logging.h @@ -55,6 +55,8 @@ namespace BCLog { LEVELDB = (1 << 20), EQUIHASH = (1 << 21), PAYMENTDISCLOSURE = (1 << 22), + ZRPC = (1 << 23), + ZRPCUNSAFE = (1 << 24), ALL = ~(uint32_t)0, }; diff --git a/src/paymentdisclosure.h b/src/paymentdisclosure.h index b81bd1ce1..18898924c 100644 --- a/src/paymentdisclosure.h +++ b/src/paymentdisclosure.h @@ -74,7 +74,7 @@ struct PaymentDisclosurePayload { uint8_t version; // 0 = experimental, 1 = first production version, etc. uint256 esk; // zcash/NoteEncryption.cpp uint256 txid; // primitives/transaction.h - size_t js; // Index into CTransaction.vjoinsplit + uint64_t js; // Index into CTransaction.vjoinsplit uint8_t n; // Index into JSDescription fields of length ZC_NUM_JS_OUTPUTS libzcash::PaymentAddress zaddr; // zcash/Address.hpp std::string message; // parameter to RPC call diff --git a/src/primitives/joinsplit.cpp b/src/primitives/joinsplit.cpp index 366010255..3fb423b5c 100644 --- a/src/primitives/joinsplit.cpp +++ b/src/primitives/joinsplit.cpp @@ -48,8 +48,8 @@ JSDescription JSDescription::Randomized( const uint256& anchor, std::array& inputs, std::array& outputs, - std::array& inputMap, - std::array& outputMap, + std::array& inputMap, + std::array& outputMap, CAmount vpub_old, CAmount vpub_new, bool computeProof, diff --git a/src/primitives/joinsplit.h b/src/primitives/joinsplit.h index 12cd2e16a..adf3475de 100644 --- a/src/primitives/joinsplit.h +++ b/src/primitives/joinsplit.h @@ -19,7 +19,7 @@ #include #include -#include +#include class JSDescription { @@ -88,8 +88,8 @@ public: const uint256& rt, std::array& inputs, std::array& outputs, - std::array& inputMap, - std::array& outputMap, + std::array& inputMap, + std::array& outputMap, CAmount vpub_old, CAmount vpub_new, bool computeProof = true, // Set to false in some tests diff --git a/src/wallet/asyncrpcoperation_mergetoaddress.cpp b/src/wallet/asyncrpcoperation_mergetoaddress.cpp index e54ca590a..6ccad2a2c 100644 --- a/src/wallet/asyncrpcoperation_mergetoaddress.cpp +++ b/src/wallet/asyncrpcoperation_mergetoaddress.cpp @@ -12,8 +12,10 @@ #include #include #include +#include #include #include +#include #include