├── README ├── NEWS ├── m4 ├── .gitignore ├── ax_check_link_flag.m4 ├── ax_check_preproc_flag.m4 ├── ax_check_compile_flag.m4 └── ax_boost_unit_test_framework.m4 ├── ChangeLog ├── AUTHORS ├── bindings ├── consensus.swg └── java │ └── proxy │ └── org │ └── libbitcoin │ └── consensus │ ├── SWIGTYPE_p_unsigned_char.java │ ├── consensusConstants.java │ ├── consensus.java │ ├── consensusJNI.java │ ├── verify_flags.java │ └── verify_result.java ├── autogen.sh ├── .gitignore ├── src ├── clone │ ├── attributes.h │ ├── crypto │ │ ├── sha1.h │ │ ├── ripemd160.h │ │ ├── sha512.h │ │ ├── hmac_sha512.h │ │ ├── hmac_sha512.cpp │ │ ├── sha256.h │ │ ├── common.h │ │ ├── sha1.cpp │ │ ├── sha512.cpp │ │ └── ripemd160.cpp │ ├── compat │ │ ├── cpuid.h │ │ ├── byteswap.h │ │ └── endian.h │ ├── amount.h │ ├── version.h │ ├── uint256.cpp │ ├── hash.cpp │ ├── util │ │ ├── string.h │ │ └── strencodings.h │ ├── script │ │ └── script_error.h │ ├── primitives │ │ └── transaction.cpp │ ├── uint256.h │ ├── hash.h │ ├── pubkey.h │ ├── span.h │ └── pubkey.cpp └── consensus │ └── consensus.hpp ├── include └── bitcoin │ ├── consensus.hpp │ └── consensus │ ├── version.hpp │ ├── define.hpp │ └── export.hpp ├── test ├── main.cpp ├── test.hpp ├── consensus__verify_flags_to_script_flags.cpp ├── consensus__script_error_to_verify_result.cpp └── consensus__script_verify.cpp ├── libbitcoin-consensus-test_runner.sh ├── bindings.bat ├── bindings.sh ├── libbitcoin-consensus.pc.in ├── README.md └── Makefile.am /README: -------------------------------------------------------------------------------- 1 | See README.md -------------------------------------------------------------------------------- /NEWS: -------------------------------------------------------------------------------- 1 | See https://libbitcoin.org -------------------------------------------------------------------------------- /m4/.gitignore: -------------------------------------------------------------------------------- 1 | /libtool.m4 2 | /lt*.m4 3 | -------------------------------------------------------------------------------- /ChangeLog: -------------------------------------------------------------------------------- 1 | Use command 'git log --oneline --decorate' for latest change log. 2 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | commits libbitcoin developers 2 | -------------------------------------------- 3 | 193 Eric Voskuil (evoskuil) 4 | 29 Phillip Mienk (pmienk) 5 | -------------------------------------------------------------------------------- /bindings/consensus.swg: -------------------------------------------------------------------------------- 1 | %module consensus 2 | %{ 3 | #include 4 | %} 5 | %include "../include/bitcoin/consensus/define.hpp" 6 | %include "../include/bitcoin/consensus/version.hpp" 7 | %include "../include/bitcoin/consensus/export.hpp" 8 | -------------------------------------------------------------------------------- /autogen.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ############################################################################### 3 | # Copyright (c) 2014-2023 libbitcoin-consensus developers (see COPYING). 4 | # 5 | # GENERATED SOURCE CODE, DO NOT EDIT EXCEPT EXPERIMENTALLY 6 | # 7 | ############################################################################### 8 | 9 | autoreconf -i 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Autotools: 2 | *.la 3 | *.lo 4 | *.o 5 | .deps 6 | .libs 7 | /aclocal.m4 8 | /autom4te.cache 9 | /build-aux 10 | /config.* 11 | /configure 12 | /libtool 13 | Makefile 14 | Makefile.in 15 | libbitcoin-consensus.pc 16 | 17 | # IDE's and editors: 18 | *~ 19 | .*.swp 20 | .dirstamp 21 | /*.kdev4 22 | /.cproject 23 | /.project 24 | /.settings 25 | /nbproject 26 | bin 27 | obj 28 | 29 | *.lock 30 | *.vsidx 31 | -------------------------------------------------------------------------------- /src/clone/attributes.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2009-2010 Satoshi Nakamoto 2 | // Copyright (c) 2009-2018 The Bitcoin Core developers 3 | // Distributed under the MIT software license, see the accompanying 4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 | 6 | #ifndef BITCOIN_ATTRIBUTES_H 7 | #define BITCOIN_ATTRIBUTES_H 8 | 9 | #if defined(__has_cpp_attribute) 10 | # if __has_cpp_attribute(nodiscard) 11 | # define NODISCARD [[nodiscard]] 12 | # endif 13 | #endif 14 | #ifndef NODISCARD 15 | # if defined(_MSC_VER) && _MSC_VER >= 1700 16 | # define NODISCARD _Check_return_ 17 | # else 18 | # define NODISCARD __attribute__((warn_unused_result)) 19 | # endif 20 | #endif 21 | 22 | #endif // BITCOIN_ATTRIBUTES_H 23 | -------------------------------------------------------------------------------- /src/clone/crypto/sha1.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2014-2016 The Bitcoin Core developers 2 | // Distributed under the MIT software license, see the accompanying 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 | 5 | #ifndef BITCOIN_CRYPTO_SHA1_H 6 | #define BITCOIN_CRYPTO_SHA1_H 7 | 8 | #include 9 | #include 10 | 11 | /** A hasher class for SHA1. */ 12 | class CSHA1 13 | { 14 | private: 15 | uint32_t s[5]; 16 | unsigned char buf[64]; 17 | uint64_t bytes; 18 | 19 | public: 20 | static const size_t OUTPUT_SIZE = 20; 21 | 22 | CSHA1(); 23 | CSHA1& Write(const unsigned char* data, size_t len); 24 | void Finalize(unsigned char hash[OUTPUT_SIZE]); 25 | CSHA1& Reset(); 26 | }; 27 | 28 | #endif // BITCOIN_CRYPTO_SHA1_H 29 | -------------------------------------------------------------------------------- /src/clone/crypto/ripemd160.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2014-2016 The Bitcoin Core developers 2 | // Distributed under the MIT software license, see the accompanying 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 | 5 | #ifndef BITCOIN_CRYPTO_RIPEMD160_H 6 | #define BITCOIN_CRYPTO_RIPEMD160_H 7 | 8 | #include 9 | #include 10 | 11 | /** A hasher class for RIPEMD-160. */ 12 | class CRIPEMD160 13 | { 14 | private: 15 | uint32_t s[5]; 16 | unsigned char buf[64]; 17 | uint64_t bytes; 18 | 19 | public: 20 | static const size_t OUTPUT_SIZE = 20; 21 | 22 | CRIPEMD160(); 23 | CRIPEMD160& Write(const unsigned char* data, size_t len); 24 | void Finalize(unsigned char hash[OUTPUT_SIZE]); 25 | CRIPEMD160& Reset(); 26 | }; 27 | 28 | #endif // BITCOIN_CRYPTO_RIPEMD160_H 29 | -------------------------------------------------------------------------------- /include/bitcoin/consensus.hpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////////////////////////// 2 | // Copyright (c) 2014-2023 libbitcoin-consensus developers (see COPYING). 3 | // 4 | // GENERATED SOURCE CODE, DO NOT EDIT EXCEPT EXPERIMENTALLY 5 | // 6 | /////////////////////////////////////////////////////////////////////////////// 7 | #ifndef LIBBITCOIN_CONSENSUS_HPP 8 | #define LIBBITCOIN_CONSENSUS_HPP 9 | 10 | /** 11 | * API Users: Include only this header. Direct use of other headers is fragile 12 | * and unsupported as header organization is subject to change. 13 | * 14 | * Maintainers: Do not include this header internal to this library. 15 | */ 16 | 17 | #include 18 | #include 19 | #include 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /include/bitcoin/consensus/version.hpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////////////////////////// 2 | // Copyright (c) 2014-2023 libbitcoin-consensus developers (see COPYING). 3 | // 4 | // GENERATED SOURCE CODE, DO NOT EDIT EXCEPT EXPERIMENTALLY 5 | // 6 | /////////////////////////////////////////////////////////////////////////////// 7 | #ifndef LIBBITCOIN_CONSENSUS_VERSION_HPP 8 | #define LIBBITCOIN_CONSENSUS_VERSION_HPP 9 | 10 | /** 11 | * The semantic version of this repository as: [major].[minor].[patch] 12 | * For interpretation of the versioning scheme see: http://semver.org 13 | */ 14 | 15 | #define LIBBITCOIN_CONSENSUS_VERSION "4.0.0" 16 | #define LIBBITCOIN_CONSENSUS_MAJOR_VERSION 4 17 | #define LIBBITCOIN_CONSENSUS_MINOR_VERSION 0 18 | #define LIBBITCOIN_CONSENSUS_PATCH_VERSION 0 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /src/clone/crypto/sha512.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2014-2019 The Bitcoin Core developers 2 | // Distributed under the MIT software license, see the accompanying 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 | 5 | #ifndef BITCOIN_CRYPTO_SHA512_H 6 | #define BITCOIN_CRYPTO_SHA512_H 7 | 8 | #include 9 | #include 10 | 11 | /** A hasher class for SHA-512. */ 12 | class CSHA512 13 | { 14 | private: 15 | uint64_t s[8]; 16 | unsigned char buf[128]; 17 | uint64_t bytes; 18 | 19 | public: 20 | static constexpr size_t OUTPUT_SIZE = 64; 21 | 22 | CSHA512(); 23 | CSHA512& Write(const unsigned char* data, size_t len); 24 | void Finalize(unsigned char hash[OUTPUT_SIZE]); 25 | CSHA512& Reset(); 26 | uint64_t Size() const { return bytes; } 27 | }; 28 | 29 | #endif // BITCOIN_CRYPTO_SHA512_H 30 | -------------------------------------------------------------------------------- /bindings/java/proxy/org/libbitcoin/consensus/SWIGTYPE_p_unsigned_char.java: -------------------------------------------------------------------------------- 1 | /* ---------------------------------------------------------------------------- 2 | * This file was automatically generated by SWIG (http://www.swig.org). 3 | * Version 3.0.8 4 | * 5 | * Do not make changes to this file unless you know what you are doing--modify 6 | * the SWIG interface file instead. 7 | * ----------------------------------------------------------------------------- */ 8 | 9 | 10 | public class SWIGTYPE_p_unsigned_char { 11 | private transient long swigCPtr; 12 | 13 | protected SWIGTYPE_p_unsigned_char(long cPtr, @SuppressWarnings("unused") boolean futureUse) { 14 | swigCPtr = cPtr; 15 | } 16 | 17 | protected SWIGTYPE_p_unsigned_char() { 18 | swigCPtr = 0; 19 | } 20 | 21 | protected static long getCPtr(SWIGTYPE_p_unsigned_char obj) { 22 | return (obj == null) ? 0 : obj.swigCPtr; 23 | } 24 | } 25 | 26 | -------------------------------------------------------------------------------- /src/clone/compat/cpuid.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2019 The Bitcoin Core developers 2 | // Distributed under the MIT software license, see the accompanying 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 | 5 | #ifndef BITCOIN_COMPAT_CPUID_H 6 | #define BITCOIN_COMPAT_CPUID_H 7 | 8 | #if defined(__x86_64__) || defined(__amd64__) || defined(__i386__) 9 | #define HAVE_GETCPUID 10 | 11 | #include 12 | 13 | // We can't use cpuid.h's __get_cpuid as it does not support subleafs. 14 | void static inline GetCPUID(uint32_t leaf, uint32_t subleaf, uint32_t& a, uint32_t& b, uint32_t& c, uint32_t& d) 15 | { 16 | #ifdef __GNUC__ 17 | __cpuid_count(leaf, subleaf, a, b, c, d); 18 | #else 19 | __asm__ ("cpuid" : "=a"(a), "=b"(b), "=c"(c), "=d"(d) : "0"(leaf), "2"(subleaf)); 20 | #endif 21 | } 22 | 23 | #endif // defined(__x86_64__) || defined(__amd64__) || defined(__i386__) 24 | #endif // BITCOIN_COMPAT_CPUID_H 25 | -------------------------------------------------------------------------------- /src/clone/crypto/hmac_sha512.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2014-2018 The Bitcoin Core developers 2 | // Distributed under the MIT software license, see the accompanying 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 | 5 | #ifndef BITCOIN_CRYPTO_HMAC_SHA512_H 6 | #define BITCOIN_CRYPTO_HMAC_SHA512_H 7 | 8 | #include 9 | 10 | #include 11 | #include 12 | 13 | /** A hasher class for HMAC-SHA-512. */ 14 | class CHMAC_SHA512 15 | { 16 | private: 17 | CSHA512 outer; 18 | CSHA512 inner; 19 | 20 | public: 21 | static const size_t OUTPUT_SIZE = 64; 22 | 23 | CHMAC_SHA512(const unsigned char* key, size_t keylen); 24 | CHMAC_SHA512& Write(const unsigned char* data, size_t len) 25 | { 26 | inner.Write(data, len); 27 | return *this; 28 | } 29 | void Finalize(unsigned char hash[OUTPUT_SIZE]); 30 | }; 31 | 32 | #endif // BITCOIN_CRYPTO_HMAC_SHA512_H 33 | -------------------------------------------------------------------------------- /test/main.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2011-2023 libbitcoin developers (see AUTHORS) 3 | * 4 | * This file is part of libbitcoin. 5 | * 6 | * This program is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public License 17 | * along with this program. If not, see . 18 | */ 19 | #define BOOST_TEST_MODULE libbitcoin_consensus_test 20 | #include 21 | 22 | -------------------------------------------------------------------------------- /bindings/java/proxy/org/libbitcoin/consensus/consensusConstants.java: -------------------------------------------------------------------------------- 1 | /* ---------------------------------------------------------------------------- 2 | * This file was automatically generated by SWIG (http://www.swig.org). 3 | * Version 3.0.8 4 | * 5 | * Do not make changes to this file unless you know what you are doing--modify 6 | * the SWIG interface file instead. 7 | * ----------------------------------------------------------------------------- */ 8 | 9 | 10 | public interface consensusConstants { 11 | public final static String LIBBITCOIN_CONSENSUS_VERSION = consensusJNI.LIBBITCOIN_CONSENSUS_VERSION_get(); 12 | public final static int LIBBITCOIN_CONSENSUS_MAJOR_VERSION = consensusJNI.LIBBITCOIN_CONSENSUS_MAJOR_VERSION_get(); 13 | public final static int LIBBITCOIN_CONSENSUS_MINOR_VERSION = consensusJNI.LIBBITCOIN_CONSENSUS_MINOR_VERSION_get(); 14 | public final static int LIBBITCOIN_CONSENSUS_PATCH_VERSION = consensusJNI.LIBBITCOIN_CONSENSUS_PATCH_VERSION_get(); 15 | } 16 | -------------------------------------------------------------------------------- /bindings/java/proxy/org/libbitcoin/consensus/consensus.java: -------------------------------------------------------------------------------- 1 | /* ---------------------------------------------------------------------------- 2 | * This file was automatically generated by SWIG (http://www.swig.org). 3 | * Version 3.0.8 4 | * 5 | * Do not make changes to this file unless you know what you are doing--modify 6 | * the SWIG interface file instead. 7 | * ----------------------------------------------------------------------------- */ 8 | 9 | 10 | public class consensus implements consensusConstants { 11 | public static verify_result verify_script(SWIGTYPE_p_unsigned_char transaction, long transaction_size, SWIGTYPE_p_unsigned_char prevout_script, long prevout_script_size, java.math.BigInteger prevout_value, long tx_input_index, long flags) { 12 | return verify_result.swigToEnum(consensusJNI.verify_script(SWIGTYPE_p_unsigned_char.getCPtr(transaction), transaction_size, SWIGTYPE_p_unsigned_char.getCPtr(prevout_script), prevout_script_size, prevout_value, tx_input_index, flags)); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /libbitcoin-consensus-test_runner.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ############################################################################### 3 | # Copyright (c) 2014-2023 libbitcoin-consensus developers (see COPYING). 4 | # 5 | # GENERATED SOURCE CODE, DO NOT EDIT EXCEPT EXPERIMENTALLY 6 | # 7 | ############################################################################### 8 | 9 | # Define tests and options. 10 | #============================================================================== 11 | BOOST_UNIT_TEST_OPTIONS=\ 12 | "--run_test=* "\ 13 | "--log_level=warning "\ 14 | "--show_progress=no "\ 15 | "--detect_memory_leak=0 "\ 16 | "--report_level=no "\ 17 | "--build_info=yes" 18 | 19 | 20 | # Run tests. 21 | #============================================================================== 22 | # ALlow CI to send errors to standard output 23 | if [[ $CI == true ]]; then 24 | ./test/libbitcoin-consensus-test ${BOOST_UNIT_TEST_OPTIONS} 25 | else 26 | ./test/libbitcoin-consensus-test ${BOOST_UNIT_TEST_OPTIONS} > test.log 27 | fi 28 | -------------------------------------------------------------------------------- /src/clone/crypto/hmac_sha512.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2014-2018 The Bitcoin Core developers 2 | // Distributed under the MIT software license, see the accompanying 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 | 5 | #include 6 | 7 | #include 8 | 9 | CHMAC_SHA512::CHMAC_SHA512(const unsigned char* key, size_t keylen) 10 | { 11 | unsigned char rkey[128]; 12 | if (keylen <= 128) { 13 | memcpy(rkey, key, keylen); 14 | memset(rkey + keylen, 0, 128 - keylen); 15 | } else { 16 | CSHA512().Write(key, keylen).Finalize(rkey); 17 | memset(rkey + 64, 0, 64); 18 | } 19 | 20 | for (int n = 0; n < 128; n++) 21 | rkey[n] ^= 0x5c; 22 | outer.Write(rkey, 128); 23 | 24 | for (int n = 0; n < 128; n++) 25 | rkey[n] ^= 0x5c ^ 0x36; 26 | inner.Write(rkey, 128); 27 | } 28 | 29 | void CHMAC_SHA512::Finalize(unsigned char hash[OUTPUT_SIZE]) 30 | { 31 | unsigned char temp[64]; 32 | inner.Finalize(temp); 33 | outer.Write(temp, 64).Finalize(hash); 34 | } 35 | -------------------------------------------------------------------------------- /src/clone/amount.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2009-2010 Satoshi Nakamoto 2 | // Copyright (c) 2009-2018 The Bitcoin Core developers 3 | // Distributed under the MIT software license, see the accompanying 4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 | 6 | #ifndef BITCOIN_AMOUNT_H 7 | #define BITCOIN_AMOUNT_H 8 | 9 | #include 10 | 11 | /** Amount in satoshis (Can be negative) */ 12 | typedef int64_t CAmount; 13 | 14 | static const CAmount COIN = 100000000; 15 | 16 | /** No amount larger than this (in satoshi) is valid. 17 | * 18 | * Note that this constant is *not* the total money supply, which in Bitcoin 19 | * currently happens to be less than 21,000,000 BTC for various reasons, but 20 | * rather a sanity check. As this sanity check is used by consensus-critical 21 | * validation code, the exact value of the MAX_MONEY constant is consensus 22 | * critical; in unusual circumstances like a(nother) overflow bug that allowed 23 | * for the creation of coins out of thin air modification could lead to a fork. 24 | * */ 25 | static const CAmount MAX_MONEY = 21000000 * COIN; 26 | inline bool MoneyRange(const CAmount& nValue) { return (nValue >= 0 && nValue <= MAX_MONEY); } 27 | 28 | #endif // BITCOIN_AMOUNT_H 29 | -------------------------------------------------------------------------------- /test/test.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2011-2023 libbitcoin developers (see AUTHORS) 3 | * 4 | * This file is part of libbitcoin. 5 | * 6 | * This program is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public License 17 | * along with this program. If not, see . 18 | */ 19 | #ifndef LIBBITCOIN_CONSENSUS_TEST_TEST_HPP 20 | #define LIBBITCOIN_CONSENSUS_TEST_TEST_HPP 21 | 22 | #include 23 | #include 24 | 25 | typedef std::vector data_chunk; 26 | 27 | bool decode_base16(data_chunk& out, const std::string& in); 28 | 29 | // Set valid to false to establish a parse failure expectation. 30 | data_chunk mnemonic_to_data(const std::string& mnemonic, bool valid=true); 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /src/clone/crypto/sha256.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2014-2018 The Bitcoin Core developers 2 | // Distributed under the MIT software license, see the accompanying 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 | 5 | #ifndef BITCOIN_CRYPTO_SHA256_H 6 | #define BITCOIN_CRYPTO_SHA256_H 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | /** A hasher class for SHA-256. */ 13 | class CSHA256 14 | { 15 | private: 16 | uint32_t s[8]; 17 | unsigned char buf[64]; 18 | uint64_t bytes; 19 | 20 | public: 21 | static const size_t OUTPUT_SIZE = 32; 22 | 23 | CSHA256(); 24 | CSHA256& Write(const unsigned char* data, size_t len); 25 | void Finalize(unsigned char hash[OUTPUT_SIZE]); 26 | CSHA256& Reset(); 27 | }; 28 | 29 | /** Autodetect the best available SHA256 implementation. 30 | * Returns the name of the implementation. 31 | */ 32 | std::string SHA256AutoDetect(); 33 | 34 | /** Compute multiple double-SHA256's of 64-byte blobs. 35 | * output: pointer to a blocks*32 byte output buffer 36 | * input: pointer to a blocks*64 byte input buffer 37 | * blocks: the number of hashes to compute. 38 | */ 39 | void SHA256D64(unsigned char* output, const unsigned char* input, size_t blocks); 40 | 41 | #endif // BITCOIN_CRYPTO_SHA256_H 42 | -------------------------------------------------------------------------------- /bindings.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | REM ########################################################################### 3 | REM # Copyright (c) 2014-2023 libbitcoin-consensus developers (see COPYING). 4 | REM # 5 | REM # GENERATED SOURCE CODE, DO NOT EDIT EXCEPT EXPERIMENTALLY 6 | REM # 7 | REM ########################################################################### 8 | REM Script to build language bindings for consensus c++ public interfaces. 9 | REM This script requires SWIG - "Simplified Wrapper and Interface Generator". 10 | REM SWIG is a simple tool available for most platforms at http://www.swig.org 11 | REM Add the path to swig.exe to the path of this process or your global path. 12 | 13 | echo Generating consensus bindings... 14 | 15 | REM Do everything relative to this file location. 16 | cd %~dp0 17 | 18 | REM Clean and make required directories. 19 | rmdir /s /q "bindings\java\wrap" 2>NUL 20 | rmdir /s /q "bindings\java\proxy\org\libbitcoin\consensus" 2>NUL 21 | rmdir /s /q "bindings\python\wrap" 2>NUL 22 | rmdir /s /q "bindings\python\proxy" 2>NUL 23 | 24 | mkdir "bindings\java\wrap" 25 | mkdir "bindings\java\proxy\org\libbitcoin\consensus" 26 | mkdir "bindings\python\wrap" 27 | mkdir "bindings\python\proxy" 28 | 29 | REM Generate bindings. 30 | swig -c++ -java -outdir "bindings\java\proxy\org\libbitcoin\consensus" -o "bindings\java\wrap\consensus.cpp" "bindings\consensus.swg" 31 | swig -c++ -python -outdir "bindings\python\proxy" -o "bindings\python\wrap\consensus.cpp" "bindings\consensus.swg" 32 | -------------------------------------------------------------------------------- /bindings.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ############################################################################### 3 | # Copyright (c) 2014-2023 libbitcoin-consensus developers (see COPYING). 4 | # 5 | # GENERATED SOURCE CODE, DO NOT EDIT EXCEPT EXPERIMENTALLY 6 | # 7 | ############################################################################### 8 | # Script to build language bindings for consensus c++ public interfaces. 9 | # This script requires SWIG - "Simplified Wrapper and Interface Generator". 10 | # SWIG is a simple tool available for most platforms at http://www.swig.org 11 | # Add the path to swig.exe to the path of this process or your global path. 12 | 13 | # Exit this script on the first error. 14 | set -e 15 | 16 | echo Generating consensus bindings... 17 | 18 | # Do everything relative to this file location. 19 | cd `dirname "$0"` 20 | 21 | # Clean and make required directories. 22 | rm -rf "bindings/java/wrap" 23 | rm -rf "bindings/java/proxy/org/libbitcoin/consensus" 24 | rm -rf "bindings/python/wrap" 25 | rm -rf "bindings/python/proxy" 26 | 27 | mkdir -p "bindings/java/wrap" 28 | mkdir -p "bindings/java/proxy/org/libbitcoin/consensus" 29 | mkdir -p "bindings/python/wrap" 30 | mkdir -p "bindings/python/proxy" 31 | 32 | # Generate bindings. 33 | swig -c++ -java -outdir "bindings/java/proxy/org/libbitcoin/consensus" -o "bindings/java/wrap/consensus.cpp" "bindings/consensus.swg" 34 | swig -c++ -python -outdir "bindings/python/proxy" -o "bindings/python/wrap/consensus.cpp" "bindings/consensus.swg" 35 | -------------------------------------------------------------------------------- /src/consensus/consensus.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2011-2023 libbitcoin developers (see AUTHORS) 3 | * 4 | * This file is part of libbitcoin. 5 | * 6 | * This program is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public License 17 | * along with this program. If not, see . 18 | */ 19 | #ifndef LIBBITCOIN_CONSENSUS_CONSENSUS_HPP 20 | #define LIBBITCOIN_CONSENSUS_CONSENSUS_HPP 21 | 22 | #include 23 | #include 24 | #include 25 | #include "script/script_error.h" 26 | 27 | namespace libbitcoin { 28 | namespace consensus { 29 | 30 | // These are not published in the public header but are exposed here for test. 31 | BCK_API verify_result script_error_to_verify_result(ScriptError_t code) noexcept; 32 | BCK_API unsigned int verify_flags_to_script_flags(uint32_t flags) noexcept; 33 | 34 | } // namespace consensus 35 | } // namespace libbitcoin 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /libbitcoin-consensus.pc.in: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Copyright (c) 2014-2023 libbitcoin-consensus developers (see COPYING). 3 | # 4 | # GENERATED SOURCE CODE, DO NOT EDIT EXCEPT EXPERIMENTALLY 5 | # 6 | ############################################################################### 7 | 8 | # Substitutions 9 | #============================================================================== 10 | prefix=@prefix@ 11 | exec_prefix=@exec_prefix@ 12 | libdir=@libdir@ 13 | includedir=@includedir@ 14 | 15 | 16 | # Metadata 17 | #============================================================================== 18 | Name: libbitcoin-consensus 19 | Description: Bitcoin Consensus Library (optional) 20 | URL: https://github.com/libbitcoin/libbitcoin-consensus 21 | Version: @PACKAGE_VERSION@ 22 | 23 | 24 | # Variables 25 | #============================================================================== 26 | # Dependencies that publish package configuration. 27 | #------------------------------------------------------------------------------ 28 | Requires: libsecp256k1 >= 0.1.0.20 29 | 30 | # Include directory and any other required compiler flags. 31 | #------------------------------------------------------------------------------ 32 | Cflags: -I${includedir} 33 | 34 | # Lib directory, lib and any required that do not publish pkg-config. 35 | #------------------------------------------------------------------------------ 36 | Libs: -L${libdir} -lbitcoin-consensus 37 | 38 | -------------------------------------------------------------------------------- /src/clone/version.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012-2018 The Bitcoin Core developers 2 | // Distributed under the MIT software license, see the accompanying 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 | 5 | #ifndef BITCOIN_VERSION_H 6 | #define BITCOIN_VERSION_H 7 | 8 | /** 9 | * network protocol versioning 10 | */ 11 | 12 | static const int PROTOCOL_VERSION = 70016; 13 | 14 | //! initial proto version, to be increased after version/verack negotiation 15 | static const int INIT_PROTO_VERSION = 209; 16 | 17 | //! disconnect from peers older than this proto version 18 | static const int MIN_PEER_PROTO_VERSION = 31800; 19 | 20 | //! BIP 0031, pong message, is enabled for all versions AFTER this one 21 | static const int BIP0031_VERSION = 60000; 22 | 23 | //! "filter*" commands are disabled without NODE_BLOOM after and including this version 24 | static const int NO_BLOOM_VERSION = 70011; 25 | 26 | //! "sendheaders" command and announcing blocks with headers starts with this version 27 | static const int SENDHEADERS_VERSION = 70012; 28 | 29 | //! "feefilter" tells peers to filter invs to you by fee starts with this version 30 | static const int FEEFILTER_VERSION = 70013; 31 | 32 | //! short-id-based block download starts with this version 33 | static const int SHORT_IDS_BLOCKS_VERSION = 70014; 34 | 35 | //! not banning for invalid compact blocks starts with this version 36 | static const int INVALID_CB_NO_BAN_VERSION = 70015; 37 | 38 | //! "wtxidrelay" command for wtxid-based relay starts with this version 39 | static const int WTXID_RELAY_VERSION = 70016; 40 | 41 | // Make sure that none of the values above collide with 42 | // `SERIALIZE_TRANSACTION_NO_WITNESS` or `ADDRV2_FORMAT`. 43 | 44 | #endif // BITCOIN_VERSION_H 45 | -------------------------------------------------------------------------------- /src/clone/compat/byteswap.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2014-2019 The Bitcoin Core developers 2 | // Distributed under the MIT software license, see the accompanying 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 | 5 | #ifndef BITCOIN_COMPAT_BYTESWAP_H 6 | #define BITCOIN_COMPAT_BYTESWAP_H 7 | 8 | #if defined(HAVE_CONFIG_H) 9 | #include 10 | #endif 11 | 12 | #include 13 | 14 | #if defined(HAVE_BYTESWAP_H) 15 | #include 16 | #endif 17 | 18 | #if defined(MAC_OSX) 19 | 20 | #include 21 | #define bswap_16(x) OSSwapInt16(x) 22 | #define bswap_32(x) OSSwapInt32(x) 23 | #define bswap_64(x) OSSwapInt64(x) 24 | 25 | #else 26 | // Non-MacOS / non-Darwin 27 | 28 | #if HAVE_DECL_BSWAP_16 == 0 29 | inline uint16_t bswap_16(uint16_t x) 30 | { 31 | return (x >> 8) | (x << 8); 32 | } 33 | #endif // HAVE_DECL_BSWAP16 == 0 34 | 35 | #if HAVE_DECL_BSWAP_32 == 0 36 | inline uint32_t bswap_32(uint32_t x) 37 | { 38 | return (((x & 0xff000000U) >> 24) | ((x & 0x00ff0000U) >> 8) | 39 | ((x & 0x0000ff00U) << 8) | ((x & 0x000000ffU) << 24)); 40 | } 41 | #endif // HAVE_DECL_BSWAP32 == 0 42 | 43 | #if HAVE_DECL_BSWAP_64 == 0 44 | inline uint64_t bswap_64(uint64_t x) 45 | { 46 | return (((x & 0xff00000000000000ull) >> 56) 47 | | ((x & 0x00ff000000000000ull) >> 40) 48 | | ((x & 0x0000ff0000000000ull) >> 24) 49 | | ((x & 0x000000ff00000000ull) >> 8) 50 | | ((x & 0x00000000ff000000ull) << 8) 51 | | ((x & 0x0000000000ff0000ull) << 24) 52 | | ((x & 0x000000000000ff00ull) << 40) 53 | | ((x & 0x00000000000000ffull) << 56)); 54 | } 55 | #endif // HAVE_DECL_BSWAP64 == 0 56 | 57 | #endif // defined(MAC_OSX) 58 | 59 | #endif // BITCOIN_COMPAT_BYTESWAP_H 60 | -------------------------------------------------------------------------------- /m4/ax_check_link_flag.m4: -------------------------------------------------------------------------------- 1 | # =========================================================================== 2 | # https://www.gnu.org/software/autoconf-archive/ax_check_link_flag.html 3 | # =========================================================================== 4 | # 5 | # SYNOPSIS 6 | # 7 | # AX_CHECK_LINK_FLAG(FLAG, [ACTION-SUCCESS], [ACTION-FAILURE], [EXTRA-FLAGS], [INPUT]) 8 | # 9 | # DESCRIPTION 10 | # 11 | # Check whether the given FLAG works with the linker or gives an error. 12 | # (Warnings, however, are ignored) 13 | # 14 | # ACTION-SUCCESS/ACTION-FAILURE are shell commands to execute on 15 | # success/failure. 16 | # 17 | # If EXTRA-FLAGS is defined, it is added to the linker's default flags 18 | # when the check is done. The check is thus made with the flags: "LDFLAGS 19 | # EXTRA-FLAGS FLAG". This can for example be used to force the linker to 20 | # issue an error when a bad flag is given. 21 | # 22 | # INPUT gives an alternative input source to AC_LINK_IFELSE. 23 | # 24 | # NOTE: Implementation based on AX_CFLAGS_GCC_OPTION. Please keep this 25 | # macro in sync with AX_CHECK_{PREPROC,COMPILE}_FLAG. 26 | # 27 | # LICENSE 28 | # 29 | # Copyright (c) 2008 Guido U. Draheim 30 | # Copyright (c) 2011 Maarten Bosmans 31 | # 32 | # Copying and distribution of this file, with or without modification, are 33 | # permitted in any medium without royalty provided the copyright notice 34 | # and this notice are preserved. This file is offered as-is, without any 35 | # warranty. 36 | 37 | #serial 6 38 | 39 | AC_DEFUN([AX_CHECK_LINK_FLAG], 40 | [AC_PREREQ(2.64)dnl for _AC_LANG_PREFIX and AS_VAR_IF 41 | AS_VAR_PUSHDEF([CACHEVAR],[ax_cv_check_ldflags_$4_$1])dnl 42 | AC_CACHE_CHECK([whether the linker accepts $1], CACHEVAR, [ 43 | ax_check_save_flags=$LDFLAGS 44 | LDFLAGS="$LDFLAGS $4 $1" 45 | AC_LINK_IFELSE([m4_default([$5],[AC_LANG_PROGRAM()])], 46 | [AS_VAR_SET(CACHEVAR,[yes])], 47 | [AS_VAR_SET(CACHEVAR,[no])]) 48 | LDFLAGS=$ax_check_save_flags]) 49 | AS_VAR_IF(CACHEVAR,yes, 50 | [m4_default([$2], :)], 51 | [m4_default([$3], :)]) 52 | AS_VAR_POPDEF([CACHEVAR])dnl 53 | ])dnl AX_CHECK_LINK_FLAGS 54 | -------------------------------------------------------------------------------- /bindings/java/proxy/org/libbitcoin/consensus/consensusJNI.java: -------------------------------------------------------------------------------- 1 | /* ---------------------------------------------------------------------------- 2 | * This file was automatically generated by SWIG (http://www.swig.org). 3 | * Version 3.0.8 4 | * 5 | * Do not make changes to this file unless you know what you are doing--modify 6 | * the SWIG interface file instead. 7 | * ----------------------------------------------------------------------------- */ 8 | 9 | 10 | public class consensusJNI { 11 | public final static native String LIBBITCOIN_CONSENSUS_VERSION_get(); 12 | public final static native int LIBBITCOIN_CONSENSUS_MAJOR_VERSION_get(); 13 | public final static native int LIBBITCOIN_CONSENSUS_MINOR_VERSION_get(); 14 | public final static native int LIBBITCOIN_CONSENSUS_PATCH_VERSION_get(); 15 | public final static native int verify_result_eval_false_get(); 16 | public final static native int verify_flags_none_get(); 17 | public final static native int verify_flags_p2sh_get(); 18 | public final static native int verify_flags_strictenc_get(); 19 | public final static native int verify_flags_dersig_get(); 20 | public final static native int verify_flags_low_s_get(); 21 | public final static native int verify_flags_nulldummy_get(); 22 | public final static native int verify_flags_sigpushonly_get(); 23 | public final static native int verify_flags_minimaldata_get(); 24 | public final static native int verify_flags_discourage_upgradable_nops_get(); 25 | public final static native int verify_flags_cleanstack_get(); 26 | public final static native int verify_flags_checklocktimeverify_get(); 27 | public final static native int verify_flags_checksequenceverify_get(); 28 | public final static native int verify_flags_witness_get(); 29 | public final static native int verify_flags_discourage_upgradable_witness_program_get(); 30 | public final static native int verify_flags_minimal_if_get(); 31 | public final static native int verify_flags_null_fail_get(); 32 | public final static native int verify_flags_witness_public_key_compressed_get(); 33 | public final static native int verify_script(long jarg1, long jarg2, long jarg3, long jarg4, java.math.BigInteger jarg5, long jarg6, long jarg7); 34 | } 35 | -------------------------------------------------------------------------------- /m4/ax_check_preproc_flag.m4: -------------------------------------------------------------------------------- 1 | # =========================================================================== 2 | # https://www.gnu.org/software/autoconf-archive/ax_check_preproc_flag.html 3 | # =========================================================================== 4 | # 5 | # SYNOPSIS 6 | # 7 | # AX_CHECK_PREPROC_FLAG(FLAG, [ACTION-SUCCESS], [ACTION-FAILURE], [EXTRA-FLAGS], [INPUT]) 8 | # 9 | # DESCRIPTION 10 | # 11 | # Check whether the given FLAG works with the current language's 12 | # preprocessor or gives an error. (Warnings, however, are ignored) 13 | # 14 | # ACTION-SUCCESS/ACTION-FAILURE are shell commands to execute on 15 | # success/failure. 16 | # 17 | # If EXTRA-FLAGS is defined, it is added to the preprocessor's default 18 | # flags when the check is done. The check is thus made with the flags: 19 | # "CPPFLAGS EXTRA-FLAGS FLAG". This can for example be used to force the 20 | # preprocessor to issue an error when a bad flag is given. 21 | # 22 | # INPUT gives an alternative input source to AC_PREPROC_IFELSE. 23 | # 24 | # NOTE: Implementation based on AX_CFLAGS_GCC_OPTION. Please keep this 25 | # macro in sync with AX_CHECK_{COMPILE,LINK}_FLAG. 26 | # 27 | # LICENSE 28 | # 29 | # Copyright (c) 2008 Guido U. Draheim 30 | # Copyright (c) 2011 Maarten Bosmans 31 | # 32 | # Copying and distribution of this file, with or without modification, are 33 | # permitted in any medium without royalty provided the copyright notice 34 | # and this notice are preserved. This file is offered as-is, without any 35 | # warranty. 36 | 37 | #serial 6 38 | 39 | AC_DEFUN([AX_CHECK_PREPROC_FLAG], 40 | [AC_PREREQ(2.64)dnl for _AC_LANG_PREFIX and AS_VAR_IF 41 | AS_VAR_PUSHDEF([CACHEVAR],[ax_cv_check_[]_AC_LANG_ABBREV[]cppflags_$4_$1])dnl 42 | AC_CACHE_CHECK([whether _AC_LANG preprocessor accepts $1], CACHEVAR, [ 43 | ax_check_save_flags=$CPPFLAGS 44 | CPPFLAGS="$CPPFLAGS $4 $1" 45 | AC_PREPROC_IFELSE([m4_default([$5],[AC_LANG_PROGRAM()])], 46 | [AS_VAR_SET(CACHEVAR,[yes])], 47 | [AS_VAR_SET(CACHEVAR,[no])]) 48 | CPPFLAGS=$ax_check_save_flags]) 49 | AS_VAR_IF(CACHEVAR,yes, 50 | [m4_default([$2], :)], 51 | [m4_default([$3], :)]) 52 | AS_VAR_POPDEF([CACHEVAR])dnl 53 | ])dnl AX_CHECK_PREPROC_FLAGS 54 | -------------------------------------------------------------------------------- /m4/ax_check_compile_flag.m4: -------------------------------------------------------------------------------- 1 | # =========================================================================== 2 | # https://www.gnu.org/software/autoconf-archive/ax_check_compile_flag.html 3 | # =========================================================================== 4 | # 5 | # SYNOPSIS 6 | # 7 | # AX_CHECK_COMPILE_FLAG(FLAG, [ACTION-SUCCESS], [ACTION-FAILURE], [EXTRA-FLAGS], [INPUT]) 8 | # 9 | # DESCRIPTION 10 | # 11 | # Check whether the given FLAG works with the current language's compiler 12 | # or gives an error. (Warnings, however, are ignored) 13 | # 14 | # ACTION-SUCCESS/ACTION-FAILURE are shell commands to execute on 15 | # success/failure. 16 | # 17 | # If EXTRA-FLAGS is defined, it is added to the current language's default 18 | # flags (e.g. CFLAGS) when the check is done. The check is thus made with 19 | # the flags: "CFLAGS EXTRA-FLAGS FLAG". This can for example be used to 20 | # force the compiler to issue an error when a bad flag is given. 21 | # 22 | # INPUT gives an alternative input source to AC_COMPILE_IFELSE. 23 | # 24 | # NOTE: Implementation based on AX_CFLAGS_GCC_OPTION. Please keep this 25 | # macro in sync with AX_CHECK_{PREPROC,LINK}_FLAG. 26 | # 27 | # LICENSE 28 | # 29 | # Copyright (c) 2008 Guido U. Draheim 30 | # Copyright (c) 2011 Maarten Bosmans 31 | # 32 | # Copying and distribution of this file, with or without modification, are 33 | # permitted in any medium without royalty provided the copyright notice 34 | # and this notice are preserved. This file is offered as-is, without any 35 | # warranty. 36 | 37 | #serial 6 38 | 39 | AC_DEFUN([AX_CHECK_COMPILE_FLAG], 40 | [AC_PREREQ(2.64)dnl for _AC_LANG_PREFIX and AS_VAR_IF 41 | AS_VAR_PUSHDEF([CACHEVAR],[ax_cv_check_[]_AC_LANG_ABBREV[]flags_$4_$1])dnl 42 | AC_CACHE_CHECK([whether _AC_LANG compiler accepts $1], CACHEVAR, [ 43 | ax_check_save_flags=$[]_AC_LANG_PREFIX[]FLAGS 44 | _AC_LANG_PREFIX[]FLAGS="$[]_AC_LANG_PREFIX[]FLAGS $4 $1" 45 | AC_COMPILE_IFELSE([m4_default([$5],[AC_LANG_PROGRAM()])], 46 | [AS_VAR_SET(CACHEVAR,[yes])], 47 | [AS_VAR_SET(CACHEVAR,[no])]) 48 | _AC_LANG_PREFIX[]FLAGS=$ax_check_save_flags]) 49 | AS_VAR_IF(CACHEVAR,yes, 50 | [m4_default([$2], :)], 51 | [m4_default([$3], :)]) 52 | AS_VAR_POPDEF([CACHEVAR])dnl 53 | ])dnl AX_CHECK_COMPILE_FLAGS 54 | -------------------------------------------------------------------------------- /include/bitcoin/consensus/define.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2011-2023 libbitcoin developers (see AUTHORS) 3 | * 4 | * This file is part of libbitcoin. 5 | * 6 | * This program is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public License 17 | * along with this program. If not, see . 18 | */ 19 | #ifndef LIBBITCOIN_CONSENSUS_DEFINE_HPP 20 | #define LIBBITCOIN_CONSENSUS_DEFINE_HPP 21 | 22 | // See http://gcc.gnu.org/wiki/Visibility 23 | 24 | // Generic helper definitions for shared library support 25 | #if defined _MSC_VER || defined __CYGWIN__ 26 | #define BCK_HELPER_DLL_IMPORT __declspec(dllimport) 27 | #define BCK_HELPER_DLL_EXPORT __declspec(dllexport) 28 | #define BCK_HELPER_DLL_LOCAL 29 | #else 30 | #if __GNUC__ >= 4 31 | #define BCK_HELPER_DLL_IMPORT __attribute__ ((visibility ("default"))) 32 | #define BCK_HELPER_DLL_EXPORT __attribute__ ((visibility ("default"))) 33 | #define BCK_HELPER_DLL_LOCAL __attribute__ ((visibility ("internal"))) 34 | #else 35 | #define BCK_HELPER_DLL_IMPORT 36 | #define BCK_HELPER_DLL_EXPORT 37 | #define BCK_HELPER_DLL_LOCAL 38 | #endif 39 | #endif 40 | 41 | // Now we use the generic helper definitions above to 42 | // define BCK_API and BCK_INTERNAL. 43 | // BCK_API is used for the public API symbols. It either DLL imports or 44 | // DLL exports (or does nothing for static build) 45 | // BCK_INTERNAL is used for non-api symbols. 46 | 47 | #if defined BCK_STATIC 48 | #define BCK_API 49 | #define BCK_INTERNAL 50 | #elif defined BCK_DLL 51 | #define BCK_API BCK_HELPER_DLL_EXPORT 52 | #define BCK_INTERNAL BCK_HELPER_DLL_LOCAL 53 | #else 54 | #define BCK_API BCK_HELPER_DLL_IMPORT 55 | #define BCK_INTERNAL BCK_HELPER_DLL_LOCAL 56 | #endif 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /src/clone/uint256.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2009-2010 Satoshi Nakamoto 2 | // Copyright (c) 2009-2019 The Bitcoin Core developers 3 | // Distributed under the MIT software license, see the accompanying 4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 | 6 | #include 7 | 8 | #include 9 | 10 | #include 11 | 12 | template 13 | base_blob::base_blob(const std::vector& vch) 14 | { 15 | assert(vch.size() == sizeof(m_data)); 16 | memcpy(m_data, vch.data(), sizeof(m_data)); 17 | } 18 | 19 | template 20 | std::string base_blob::GetHex() const 21 | { 22 | uint8_t m_data_rev[WIDTH]; 23 | for (int i = 0; i < WIDTH; ++i) { 24 | m_data_rev[i] = m_data[WIDTH - 1 - i]; 25 | } 26 | return HexStr(m_data_rev); 27 | } 28 | 29 | template 30 | void base_blob::SetHex(const char* psz) 31 | { 32 | memset(m_data, 0, sizeof(m_data)); 33 | 34 | // skip leading spaces 35 | while (IsSpace(*psz)) 36 | psz++; 37 | 38 | // skip 0x 39 | if (psz[0] == '0' && ToLower(psz[1]) == 'x') 40 | psz += 2; 41 | 42 | // hex string to uint 43 | size_t digits = 0; 44 | while (::HexDigit(psz[digits]) != -1) 45 | digits++; 46 | unsigned char* p1 = (unsigned char*)m_data; 47 | unsigned char* pend = p1 + WIDTH; 48 | while (digits > 0 && p1 < pend) { 49 | *p1 = ::HexDigit(psz[--digits]); 50 | if (digits > 0) { 51 | *p1 |= ((unsigned char)::HexDigit(psz[--digits]) << 4); 52 | p1++; 53 | } 54 | } 55 | } 56 | 57 | template 58 | void base_blob::SetHex(const std::string& str) 59 | { 60 | SetHex(str.c_str()); 61 | } 62 | 63 | template 64 | std::string base_blob::ToString() const 65 | { 66 | return (GetHex()); 67 | } 68 | 69 | // Explicit instantiations for base_blob<160> 70 | template base_blob<160>::base_blob(const std::vector&); 71 | template std::string base_blob<160>::GetHex() const; 72 | template std::string base_blob<160>::ToString() const; 73 | template void base_blob<160>::SetHex(const char*); 74 | template void base_blob<160>::SetHex(const std::string&); 75 | 76 | // Explicit instantiations for base_blob<256> 77 | template base_blob<256>::base_blob(const std::vector&); 78 | template std::string base_blob<256>::GetHex() const; 79 | template std::string base_blob<256>::ToString() const; 80 | template void base_blob<256>::SetHex(const char*); 81 | template void base_blob<256>::SetHex(const std::string&); 82 | 83 | const uint256 uint256::ZERO(0); 84 | const uint256 uint256::ONE(1); 85 | -------------------------------------------------------------------------------- /src/clone/hash.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013-2018 The Bitcoin Core developers 2 | // Distributed under the MIT software license, see the accompanying 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include 10 | 11 | inline uint32_t ROTL32(uint32_t x, int8_t r) 12 | { 13 | return (x << r) | (x >> (32 - r)); 14 | } 15 | 16 | unsigned int MurmurHash3(unsigned int nHashSeed, Span vDataToHash) 17 | { 18 | // The following is MurmurHash3 (x86_32), see http://code.google.com/p/smhasher/source/browse/trunk/MurmurHash3.cpp 19 | uint32_t h1 = nHashSeed; 20 | const uint32_t c1 = 0xcc9e2d51; 21 | const uint32_t c2 = 0x1b873593; 22 | 23 | const int nblocks = vDataToHash.size() / 4; 24 | 25 | //---------- 26 | // body 27 | const uint8_t* blocks = vDataToHash.data(); 28 | 29 | for (int i = 0; i < nblocks; ++i) { 30 | uint32_t k1 = ReadLE32(blocks + i*4); 31 | 32 | k1 *= c1; 33 | k1 = ROTL32(k1, 15); 34 | k1 *= c2; 35 | 36 | h1 ^= k1; 37 | h1 = ROTL32(h1, 13); 38 | h1 = h1 * 5 + 0xe6546b64; 39 | } 40 | 41 | //---------- 42 | // tail 43 | const uint8_t* tail = vDataToHash.data() + nblocks * 4; 44 | 45 | uint32_t k1 = 0; 46 | 47 | switch (vDataToHash.size() & 3) { 48 | case 3: 49 | k1 ^= tail[2] << 16; 50 | case 2: 51 | k1 ^= tail[1] << 8; 52 | case 1: 53 | k1 ^= tail[0]; 54 | k1 *= c1; 55 | k1 = ROTL32(k1, 15); 56 | k1 *= c2; 57 | h1 ^= k1; 58 | } 59 | 60 | //---------- 61 | // finalization 62 | h1 ^= vDataToHash.size(); 63 | h1 ^= h1 >> 16; 64 | h1 *= 0x85ebca6b; 65 | h1 ^= h1 >> 13; 66 | h1 *= 0xc2b2ae35; 67 | h1 ^= h1 >> 16; 68 | 69 | return h1; 70 | } 71 | 72 | void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]) 73 | { 74 | unsigned char num[4]; 75 | num[0] = (nChild >> 24) & 0xFF; 76 | num[1] = (nChild >> 16) & 0xFF; 77 | num[2] = (nChild >> 8) & 0xFF; 78 | num[3] = (nChild >> 0) & 0xFF; 79 | CHMAC_SHA512(chainCode.begin(), chainCode.size()).Write(&header, 1).Write(data, 32).Write(num, 4).Finalize(output); 80 | } 81 | 82 | uint256 SHA256Uint256(const uint256& input) 83 | { 84 | uint256 result; 85 | CSHA256().Write(input.begin(), 32).Finalize(result.begin()); 86 | return result; 87 | } 88 | 89 | CHashWriter TaggedHash(const std::string& tag) 90 | { 91 | CHashWriter writer(SER_GETHASH, 0); 92 | uint256 taghash; 93 | CSHA256().Write((const unsigned char*)tag.data(), tag.size()).Finalize(taghash.begin()); 94 | writer << taghash << taghash; 95 | return writer; 96 | } 97 | -------------------------------------------------------------------------------- /src/clone/util/string.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2019-2020 The Bitcoin Core developers 2 | // Distributed under the MIT software license, see the accompanying 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 | 5 | #ifndef BITCOIN_UTIL_STRING_H 6 | #define BITCOIN_UTIL_STRING_H 7 | 8 | #include 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | NODISCARD inline std::string TrimString(const std::string& str, const std::string& pattern = " \f\n\r\t\v") 19 | { 20 | std::string::size_type front = str.find_first_not_of(pattern); 21 | if (front == std::string::npos) { 22 | return std::string(); 23 | } 24 | std::string::size_type end = str.find_last_not_of(pattern); 25 | return str.substr(front, end - front + 1); 26 | } 27 | 28 | /** 29 | * Join a list of items 30 | * 31 | * @param list The list to join 32 | * @param separator The separator 33 | * @param unary_op Apply this operator to each item in the list 34 | */ 35 | template 36 | auto Join(const std::vector& list, const BaseType& separator, UnaryOp unary_op) 37 | -> decltype(unary_op(list.at(0))) 38 | { 39 | decltype(unary_op(list.at(0))) ret; 40 | for (size_t i = 0; i < list.size(); ++i) { 41 | if (i > 0) ret += separator; 42 | ret += unary_op(list.at(i)); 43 | } 44 | return ret; 45 | } 46 | 47 | template 48 | T Join(const std::vector& list, const T& separator) 49 | { 50 | return Join(list, separator, [](const T& i) { return i; }); 51 | } 52 | 53 | // Explicit overload needed for c_str arguments, which would otherwise cause a substitution failure in the template above. 54 | inline std::string Join(const std::vector& list, const std::string& separator) 55 | { 56 | return Join(list, separator); 57 | } 58 | 59 | /** 60 | * Check if a string does not contain any embedded NUL (\0) characters 61 | */ 62 | NODISCARD inline bool ValidAsCString(const std::string& str) noexcept 63 | { 64 | return str.size() == strlen(str.c_str()); 65 | } 66 | 67 | /** 68 | * Locale-independent version of std::to_string 69 | */ 70 | template 71 | std::string ToString(const T& t) 72 | { 73 | std::ostringstream oss; 74 | oss.imbue(std::locale::classic()); 75 | oss << t; 76 | return oss.str(); 77 | } 78 | 79 | /** 80 | * Check whether a container begins with the given prefix. 81 | */ 82 | template 83 | NODISCARD inline bool HasPrefix(const T1& obj, 84 | const std::array& prefix) 85 | { 86 | return obj.size() >= PREFIX_LEN && 87 | std::equal(std::begin(prefix), std::end(prefix), std::begin(obj)); 88 | } 89 | 90 | #endif // BITCOIN_UTIL_STRENCODINGS_H 91 | -------------------------------------------------------------------------------- /src/clone/crypto/common.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2014-2018 The Bitcoin Core developers 2 | // Distributed under the MIT software license, see the accompanying 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 | 5 | #ifndef BITCOIN_CRYPTO_COMMON_H 6 | #define BITCOIN_CRYPTO_COMMON_H 7 | 8 | #if defined(HAVE_CONFIG_H) 9 | #include 10 | #endif 11 | 12 | #include 13 | #include 14 | 15 | #include 16 | 17 | uint16_t static inline ReadLE16(const unsigned char* ptr) 18 | { 19 | uint16_t x; 20 | memcpy((char*)&x, ptr, 2); 21 | return le16toh(x); 22 | } 23 | 24 | uint32_t static inline ReadLE32(const unsigned char* ptr) 25 | { 26 | uint32_t x; 27 | memcpy((char*)&x, ptr, 4); 28 | return le32toh(x); 29 | } 30 | 31 | uint64_t static inline ReadLE64(const unsigned char* ptr) 32 | { 33 | uint64_t x; 34 | memcpy((char*)&x, ptr, 8); 35 | return le64toh(x); 36 | } 37 | 38 | void static inline WriteLE16(unsigned char* ptr, uint16_t x) 39 | { 40 | uint16_t v = htole16(x); 41 | memcpy(ptr, (char*)&v, 2); 42 | } 43 | 44 | void static inline WriteLE32(unsigned char* ptr, uint32_t x) 45 | { 46 | uint32_t v = htole32(x); 47 | memcpy(ptr, (char*)&v, 4); 48 | } 49 | 50 | void static inline WriteLE64(unsigned char* ptr, uint64_t x) 51 | { 52 | uint64_t v = htole64(x); 53 | memcpy(ptr, (char*)&v, 8); 54 | } 55 | 56 | uint16_t static inline ReadBE16(const unsigned char* ptr) 57 | { 58 | uint16_t x; 59 | memcpy((char*)&x, ptr, 2); 60 | return be16toh(x); 61 | } 62 | 63 | uint32_t static inline ReadBE32(const unsigned char* ptr) 64 | { 65 | uint32_t x; 66 | memcpy((char*)&x, ptr, 4); 67 | return be32toh(x); 68 | } 69 | 70 | uint64_t static inline ReadBE64(const unsigned char* ptr) 71 | { 72 | uint64_t x; 73 | memcpy((char*)&x, ptr, 8); 74 | return be64toh(x); 75 | } 76 | 77 | void static inline WriteBE32(unsigned char* ptr, uint32_t x) 78 | { 79 | uint32_t v = htobe32(x); 80 | memcpy(ptr, (char*)&v, 4); 81 | } 82 | 83 | void static inline WriteBE64(unsigned char* ptr, uint64_t x) 84 | { 85 | uint64_t v = htobe64(x); 86 | memcpy(ptr, (char*)&v, 8); 87 | } 88 | 89 | /** Return the smallest number n such that (x >> n) == 0 (or 64 if the highest bit in x is set. */ 90 | uint64_t static inline CountBits(uint64_t x) 91 | { 92 | #if HAVE_BUILTIN_CLZL 93 | if (sizeof(unsigned long) >= sizeof(uint64_t)) { 94 | return x ? 8 * sizeof(unsigned long) - __builtin_clzl(x) : 0; 95 | } 96 | #endif 97 | #if HAVE_BUILTIN_CLZLL 98 | if (sizeof(unsigned long long) >= sizeof(uint64_t)) { 99 | return x ? 8 * sizeof(unsigned long long) - __builtin_clzll(x) : 0; 100 | } 101 | #endif 102 | int ret = 0; 103 | while (x) { 104 | x >>= 1; 105 | ++ret; 106 | } 107 | return ret; 108 | } 109 | 110 | #endif // BITCOIN_CRYPTO_COMMON_H 111 | -------------------------------------------------------------------------------- /src/clone/script/script_error.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2009-2010 Satoshi Nakamoto 2 | // Copyright (c) 2009-2018 The Bitcoin Core developers 3 | // Distributed under the MIT software license, see the accompanying 4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 | 6 | #ifndef BITCOIN_SCRIPT_SCRIPT_ERROR_H 7 | #define BITCOIN_SCRIPT_SCRIPT_ERROR_H 8 | 9 | #include 10 | 11 | typedef enum ScriptError_t 12 | { 13 | SCRIPT_ERR_OK = 0, 14 | SCRIPT_ERR_UNKNOWN_ERROR, 15 | SCRIPT_ERR_EVAL_FALSE, 16 | SCRIPT_ERR_OP_RETURN, 17 | 18 | /* Max sizes */ 19 | SCRIPT_ERR_SCRIPT_SIZE, 20 | SCRIPT_ERR_PUSH_SIZE, 21 | SCRIPT_ERR_OP_COUNT, 22 | SCRIPT_ERR_STACK_SIZE, 23 | SCRIPT_ERR_SIG_COUNT, 24 | SCRIPT_ERR_PUBKEY_COUNT, 25 | 26 | /* Failed verify operations */ 27 | SCRIPT_ERR_VERIFY, 28 | SCRIPT_ERR_EQUALVERIFY, 29 | SCRIPT_ERR_CHECKMULTISIGVERIFY, 30 | SCRIPT_ERR_CHECKSIGVERIFY, 31 | SCRIPT_ERR_NUMEQUALVERIFY, 32 | 33 | /* Logical/Format/Canonical errors */ 34 | SCRIPT_ERR_BAD_OPCODE, 35 | SCRIPT_ERR_DISABLED_OPCODE, 36 | SCRIPT_ERR_INVALID_STACK_OPERATION, 37 | SCRIPT_ERR_INVALID_ALTSTACK_OPERATION, 38 | SCRIPT_ERR_UNBALANCED_CONDITIONAL, 39 | 40 | /* CHECKLOCKTIMEVERIFY and CHECKSEQUENCEVERIFY */ 41 | SCRIPT_ERR_NEGATIVE_LOCKTIME, 42 | SCRIPT_ERR_UNSATISFIED_LOCKTIME, 43 | 44 | /* Malleability */ 45 | SCRIPT_ERR_SIG_HASHTYPE, 46 | SCRIPT_ERR_SIG_DER, 47 | SCRIPT_ERR_MINIMALDATA, 48 | SCRIPT_ERR_SIG_PUSHONLY, 49 | SCRIPT_ERR_SIG_HIGH_S, 50 | SCRIPT_ERR_SIG_NULLDUMMY, 51 | SCRIPT_ERR_PUBKEYTYPE, 52 | SCRIPT_ERR_CLEANSTACK, 53 | SCRIPT_ERR_MINIMALIF, 54 | SCRIPT_ERR_SIG_NULLFAIL, 55 | 56 | /* softfork safeness */ 57 | SCRIPT_ERR_DISCOURAGE_UPGRADABLE_NOPS, 58 | SCRIPT_ERR_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM, 59 | SCRIPT_ERR_DISCOURAGE_UPGRADABLE_TAPROOT_VERSION, 60 | SCRIPT_ERR_DISCOURAGE_OP_SUCCESS, 61 | SCRIPT_ERR_DISCOURAGE_UPGRADABLE_PUBKEYTYPE, 62 | 63 | /* segregated witness */ 64 | SCRIPT_ERR_WITNESS_PROGRAM_WRONG_LENGTH, 65 | SCRIPT_ERR_WITNESS_PROGRAM_WITNESS_EMPTY, 66 | SCRIPT_ERR_WITNESS_PROGRAM_MISMATCH, 67 | SCRIPT_ERR_WITNESS_MALLEATED, 68 | SCRIPT_ERR_WITNESS_MALLEATED_P2SH, 69 | SCRIPT_ERR_WITNESS_UNEXPECTED, 70 | SCRIPT_ERR_WITNESS_PUBKEYTYPE, 71 | 72 | /* Taproot */ 73 | SCRIPT_ERR_SCHNORR_SIG_SIZE, 74 | SCRIPT_ERR_SCHNORR_SIG_HASHTYPE, 75 | SCRIPT_ERR_SCHNORR_SIG, 76 | SCRIPT_ERR_TAPROOT_WRONG_CONTROL_SIZE, 77 | SCRIPT_ERR_TAPSCRIPT_VALIDATION_WEIGHT, 78 | SCRIPT_ERR_TAPSCRIPT_CHECKMULTISIG, 79 | SCRIPT_ERR_TAPSCRIPT_MINIMALIF, 80 | 81 | /* Constant scriptCode */ 82 | SCRIPT_ERR_OP_CODESEPARATOR, 83 | SCRIPT_ERR_SIG_FINDANDDELETE, 84 | 85 | SCRIPT_ERR_ERROR_COUNT 86 | } ScriptError; 87 | 88 | #define SCRIPT_ERR_LAST SCRIPT_ERR_ERROR_COUNT 89 | 90 | std::string ScriptErrorString(const ScriptError error); 91 | 92 | #endif // BITCOIN_SCRIPT_SCRIPT_ERROR_H 93 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/libbitcoin/libbitcoin-consensus.svg?branch=master)](https://travis-ci.org/libbitcoin/libbitcoin-consensus) 2 | 3 | [![Coverage Status](https://coveralls.io/repos/libbitcoin/libbitcoin-consensus/badge.svg)](https://coveralls.io/r/libbitcoin/libbitcoin-consensus) 4 | 5 | # Libbitcoin Consensus 6 | 7 | *Bitcoin consensus library* 8 | 9 | ## Installation 10 | 11 | ```sh 12 | $ ./autogen.sh 13 | $ ./configure 14 | $ make 15 | $ sudo make install 16 | $ sudo ldconfig 17 | ``` 18 | 19 | `libbitcoin-consensus` is now installed in `/usr/local/`. 20 | 21 | ## Dependencies 22 | 23 | This library requires [libsecp256k1](https://github.com/libbitcoin/secp256k1). The test cases have a boost dependency. 24 | 25 | ## Configure Options 26 | 27 | There is a dependency on [boost test](http://www.boost.org/doc/libs/1_57_0/libs/test/doc/html/index.html) for `make check` builds (tests). The `--without-tests` option disables test builds and eliminates the boost check during configure. 28 | 29 | ## Supported Platforms 30 | 31 | **Ubuntu** (gcc and clang) and **OSX** (clang) are regularly tested via a [travis build matrix](https://travis-ci.org/libbitcoin/libbitcoin-consensus). There are also Visual Studio 2017, 2015 and 2013 solutions for **Windows** builds, however the VS2013 build is not currently supported due to a compiler incompatibility introduced in recent versions. 32 | 33 | # About 34 | 35 | This library includes the following 35 files considered to be bitcoin script consensus-critical (in bitcoin core). These files are identical to those used in version 0.19.0 of bitcoin core. 36 | 37 | ``` 38 | amount.h 39 | attributes.h 40 | hash.cpp 41 | hash.h 42 | prevector.h 43 | pubkey.cpp 44 | pubkey.h 45 | serialize.h 46 | span.h 47 | tinyformat.h 48 | uint256.cpp 49 | uint256.h 50 | version.h 51 | compat/byteswap.h 52 | compat/endian.h 53 | crypto/common.h 54 | crypto/hmac_sha512.cpp 55 | crypto/hmac_sha512.h 56 | crypto/ripemd160.cpp 57 | crypto/ripemd160.h 58 | crypto/sha1.cpp 59 | crypto/sha1.h 60 | crypto/sha256.cpp 61 | crypto/sha256.h 62 | crypto/sha512.cpp 63 | crypto/sha512.h 64 | primitives/transaction.cpp 65 | primitives/transaction.h 66 | script/interpreter.cpp 67 | script/interpreter.h 68 | script/script.cpp 69 | script/script.h 70 | script/script_error.h 71 | util/strencodings.cpp 72 | util/strencodings.h 73 | ``` 74 | 75 | # Libbitcoin Integration 76 | 77 | Libbitcoin natively implements consensus checks that are redundant with `libbitcoin-consensus`. Libbitcoin includes a full bitcoin client and server SDK. This includes the full node implementation [libbitcoin-node](https://github.com/libbitcoin/libbitcoin-node), which builds on [libbitcoin](https://github.com/libbitcoin/libbitcoin) and [libbitcoin-blockchain](https://github.com/libbitcoin/libbitcoin-blockchain). 78 | 79 | The `libbitcoin-blockchain` configuration provides the `--with-consensus` option. This allows the developer to select either `libbitcoin` native or `libbitcoin-consensus` checks. The option defaults to `yes` so that by default all `libbitcoin-node` and `libbitcoin-server` builds use the same script consensus checks as a bitcoin core node. 80 | -------------------------------------------------------------------------------- /Makefile.am: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Copyright (c) 2014-2023 libbitcoin-consensus developers (see COPYING). 3 | # 4 | # GENERATED SOURCE CODE, DO NOT EDIT EXCEPT EXPERIMENTALLY 5 | # 6 | ############################################################################### 7 | 8 | # Automake settings. 9 | #============================================================================== 10 | # Look for macros in the m4 subdirectory. 11 | #------------------------------------------------------------------------------ 12 | ACLOCAL_AMFLAGS = -I m4 13 | 14 | 15 | # Distribute, make and install products. 16 | #============================================================================== 17 | # files => ${pkgconfigdir} 18 | #------------------------------------------------------------------------------ 19 | pkgconfig_DATA = \ 20 | libbitcoin-consensus.pc 21 | 22 | # files => ${docdir} 23 | #------------------------------------------------------------------------------ 24 | doc_DATA = \ 25 | AUTHORS \ 26 | COPYING \ 27 | ChangeLog \ 28 | INSTALL \ 29 | NEWS \ 30 | README 31 | 32 | # src/libbitcoin-consensus.la => ${libdir} 33 | #------------------------------------------------------------------------------ 34 | lib_LTLIBRARIES = src/libbitcoin-consensus.la 35 | src_libbitcoin_consensus_la_CPPFLAGS = -I${srcdir}/include -I${srcdir}/src -I${srcdir}/src/clone ${secp256k1_BUILD_CPPFLAGS} 36 | src_libbitcoin_consensus_la_LIBADD = ${secp256k1_LIBS} 37 | src_libbitcoin_consensus_la_SOURCES = \ 38 | src/clone/amount.h \ 39 | src/clone/attributes.h \ 40 | src/clone/hash.cpp \ 41 | src/clone/hash.h \ 42 | src/clone/prevector.h \ 43 | src/clone/pubkey.cpp \ 44 | src/clone/pubkey.h \ 45 | src/clone/serialize.h \ 46 | src/clone/span.h \ 47 | src/clone/tinyformat.h \ 48 | src/clone/uint256.cpp \ 49 | src/clone/uint256.h \ 50 | src/clone/version.h \ 51 | src/clone/compat/byteswap.h \ 52 | src/clone/compat/cpuid.h \ 53 | src/clone/compat/endian.h \ 54 | src/clone/crypto/common.h \ 55 | src/clone/crypto/hmac_sha512.cpp \ 56 | src/clone/crypto/hmac_sha512.h \ 57 | src/clone/crypto/ripemd160.cpp \ 58 | src/clone/crypto/ripemd160.h \ 59 | src/clone/crypto/sha1.cpp \ 60 | src/clone/crypto/sha1.h \ 61 | src/clone/crypto/sha256.cpp \ 62 | src/clone/crypto/sha256.h \ 63 | src/clone/crypto/sha512.cpp \ 64 | src/clone/crypto/sha512.h \ 65 | src/clone/primitives/transaction.cpp \ 66 | src/clone/primitives/transaction.h \ 67 | src/clone/script/interpreter.cpp \ 68 | src/clone/script/interpreter.h \ 69 | src/clone/script/script.cpp \ 70 | src/clone/script/script.h \ 71 | src/clone/script/script_error.h \ 72 | src/clone/util/strencodings.cpp \ 73 | src/clone/util/strencodings.h \ 74 | src/clone/util/string.h \ 75 | src/consensus/consensus.cpp \ 76 | src/consensus/consensus.hpp 77 | 78 | # local: test/libbitcoin-consensus-test 79 | #------------------------------------------------------------------------------ 80 | if WITH_TESTS 81 | 82 | TESTS = libbitcoin-consensus-test_runner.sh 83 | 84 | check_PROGRAMS = test/libbitcoin-consensus-test 85 | test_libbitcoin_consensus_test_CPPFLAGS = -I${srcdir}/include -I${srcdir}/src -I${srcdir}/src/clone ${boost_BUILD_CPPFLAGS} ${secp256k1_BUILD_CPPFLAGS} 86 | test_libbitcoin_consensus_test_LDFLAGS = ${boost_LDFLAGS} 87 | test_libbitcoin_consensus_test_LDADD = src/libbitcoin-consensus.la ${boost_unit_test_framework_LIBS} ${secp256k1_LIBS} 88 | test_libbitcoin_consensus_test_SOURCES = \ 89 | test/consensus__script_error_to_verify_result.cpp \ 90 | test/consensus__script_verify.cpp \ 91 | test/consensus__verify_flags_to_script_flags.cpp \ 92 | test/main.cpp \ 93 | test/script.hpp \ 94 | test/test.cpp \ 95 | test/test.hpp 96 | 97 | endif WITH_TESTS 98 | 99 | # files => ${includedir}/bitcoin 100 | #------------------------------------------------------------------------------ 101 | include_bitcoindir = ${includedir}/bitcoin 102 | include_bitcoin_HEADERS = \ 103 | include/bitcoin/consensus.hpp 104 | 105 | include_bitcoin_consensusdir = ${includedir}/bitcoin/consensus 106 | include_bitcoin_consensus_HEADERS = \ 107 | include/bitcoin/consensus/define.hpp \ 108 | include/bitcoin/consensus/export.hpp \ 109 | include/bitcoin/consensus/version.hpp 110 | 111 | -------------------------------------------------------------------------------- /src/clone/primitives/transaction.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2009-2010 Satoshi Nakamoto 2 | // Copyright (c) 2009-2020 The Bitcoin Core developers 3 | // Distributed under the MIT software license, see the accompanying 4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 | 6 | #include 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | #include 13 | 14 | std::string COutPoint::ToString() const 15 | { 16 | return strprintf("COutPoint(%s, %u)", hash.ToString().substr(0,10), n); 17 | } 18 | 19 | CTxIn::CTxIn(COutPoint prevoutIn, CScript scriptSigIn, uint32_t nSequenceIn) 20 | { 21 | prevout = prevoutIn; 22 | scriptSig = scriptSigIn; 23 | nSequence = nSequenceIn; 24 | } 25 | 26 | CTxIn::CTxIn(uint256 hashPrevTx, uint32_t nOut, CScript scriptSigIn, uint32_t nSequenceIn) 27 | { 28 | prevout = COutPoint(hashPrevTx, nOut); 29 | scriptSig = scriptSigIn; 30 | nSequence = nSequenceIn; 31 | } 32 | 33 | std::string CTxIn::ToString() const 34 | { 35 | std::string str; 36 | str += "CTxIn("; 37 | str += prevout.ToString(); 38 | if (prevout.IsNull()) 39 | str += strprintf(", coinbase %s", HexStr(scriptSig)); 40 | else 41 | str += strprintf(", scriptSig=%s", HexStr(scriptSig).substr(0, 24)); 42 | if (nSequence != SEQUENCE_FINAL) 43 | str += strprintf(", nSequence=%u", nSequence); 44 | str += ")"; 45 | return str; 46 | } 47 | 48 | CTxOut::CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn) 49 | { 50 | nValue = nValueIn; 51 | scriptPubKey = scriptPubKeyIn; 52 | } 53 | 54 | std::string CTxOut::ToString() const 55 | { 56 | return strprintf("CTxOut(nValue=%d.%08d, scriptPubKey=%s)", nValue / COIN, nValue % COIN, HexStr(scriptPubKey).substr(0, 30)); 57 | } 58 | 59 | CMutableTransaction::CMutableTransaction() : nVersion(CTransaction::CURRENT_VERSION), nLockTime(0) {} 60 | CMutableTransaction::CMutableTransaction(const CTransaction& tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime) {} 61 | 62 | uint256 CMutableTransaction::GetHash() const 63 | { 64 | return SerializeHash(*this, SER_GETHASH, SERIALIZE_TRANSACTION_NO_WITNESS); 65 | } 66 | 67 | uint256 CTransaction::ComputeHash() const 68 | { 69 | return SerializeHash(*this, SER_GETHASH, SERIALIZE_TRANSACTION_NO_WITNESS); 70 | } 71 | 72 | uint256 CTransaction::ComputeWitnessHash() const 73 | { 74 | if (!HasWitness()) { 75 | return hash; 76 | } 77 | return SerializeHash(*this, SER_GETHASH, 0); 78 | } 79 | 80 | /* For backward compatibility, the hash is initialized to 0. TODO: remove the need for this default constructor entirely. */ 81 | CTransaction::CTransaction() : vin(), vout(), nVersion(CTransaction::CURRENT_VERSION), nLockTime(0), hash{}, m_witness_hash{} {} 82 | CTransaction::CTransaction(const CMutableTransaction& tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime), hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {} 83 | CTransaction::CTransaction(CMutableTransaction&& tx) : vin(std::move(tx.vin)), vout(std::move(tx.vout)), nVersion(tx.nVersion), nLockTime(tx.nLockTime), hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {} 84 | 85 | CAmount CTransaction::GetValueOut() const 86 | { 87 | CAmount nValueOut = 0; 88 | for (const auto& tx_out : vout) { 89 | if (!MoneyRange(tx_out.nValue) || !MoneyRange(nValueOut + tx_out.nValue)) 90 | throw std::runtime_error(std::string(__func__) + ": value out of range"); 91 | nValueOut += tx_out.nValue; 92 | } 93 | assert(MoneyRange(nValueOut)); 94 | return nValueOut; 95 | } 96 | 97 | unsigned int CTransaction::GetTotalSize() const 98 | { 99 | return ::GetSerializeSize(*this, PROTOCOL_VERSION); 100 | } 101 | 102 | std::string CTransaction::ToString() const 103 | { 104 | std::string str; 105 | str += strprintf("CTransaction(hash=%s, ver=%d, vin.size=%u, vout.size=%u, nLockTime=%u)\n", 106 | GetHash().ToString().substr(0,10), 107 | nVersion, 108 | vin.size(), 109 | vout.size(), 110 | nLockTime); 111 | for (const auto& tx_in : vin) 112 | str += " " + tx_in.ToString() + "\n"; 113 | for (const auto& tx_in : vin) 114 | str += " " + tx_in.scriptWitness.ToString() + "\n"; 115 | for (const auto& tx_out : vout) 116 | str += " " + tx_out.ToString() + "\n"; 117 | return str; 118 | } 119 | -------------------------------------------------------------------------------- /bindings/java/proxy/org/libbitcoin/consensus/verify_flags.java: -------------------------------------------------------------------------------- 1 | /* ---------------------------------------------------------------------------- 2 | * This file was automatically generated by SWIG (http://www.swig.org). 3 | * Version 3.0.8 4 | * 5 | * Do not make changes to this file unless you know what you are doing--modify 6 | * the SWIG interface file instead. 7 | * ----------------------------------------------------------------------------- */ 8 | 9 | 10 | public final class verify_flags { 11 | public final static verify_flags verify_flags_none = new verify_flags("verify_flags_none", consensusJNI.verify_flags_none_get()); 12 | public final static verify_flags verify_flags_p2sh = new verify_flags("verify_flags_p2sh", consensusJNI.verify_flags_p2sh_get()); 13 | public final static verify_flags verify_flags_strictenc = new verify_flags("verify_flags_strictenc", consensusJNI.verify_flags_strictenc_get()); 14 | public final static verify_flags verify_flags_dersig = new verify_flags("verify_flags_dersig", consensusJNI.verify_flags_dersig_get()); 15 | public final static verify_flags verify_flags_low_s = new verify_flags("verify_flags_low_s", consensusJNI.verify_flags_low_s_get()); 16 | public final static verify_flags verify_flags_nulldummy = new verify_flags("verify_flags_nulldummy", consensusJNI.verify_flags_nulldummy_get()); 17 | public final static verify_flags verify_flags_sigpushonly = new verify_flags("verify_flags_sigpushonly", consensusJNI.verify_flags_sigpushonly_get()); 18 | public final static verify_flags verify_flags_minimaldata = new verify_flags("verify_flags_minimaldata", consensusJNI.verify_flags_minimaldata_get()); 19 | public final static verify_flags verify_flags_discourage_upgradable_nops = new verify_flags("verify_flags_discourage_upgradable_nops", consensusJNI.verify_flags_discourage_upgradable_nops_get()); 20 | public final static verify_flags verify_flags_cleanstack = new verify_flags("verify_flags_cleanstack", consensusJNI.verify_flags_cleanstack_get()); 21 | public final static verify_flags verify_flags_checklocktimeverify = new verify_flags("verify_flags_checklocktimeverify", consensusJNI.verify_flags_checklocktimeverify_get()); 22 | public final static verify_flags verify_flags_checksequenceverify = new verify_flags("verify_flags_checksequenceverify", consensusJNI.verify_flags_checksequenceverify_get()); 23 | public final static verify_flags verify_flags_witness = new verify_flags("verify_flags_witness", consensusJNI.verify_flags_witness_get()); 24 | public final static verify_flags verify_flags_discourage_upgradable_witness_program = new verify_flags("verify_flags_discourage_upgradable_witness_program", consensusJNI.verify_flags_discourage_upgradable_witness_program_get()); 25 | public final static verify_flags verify_flags_minimal_if = new verify_flags("verify_flags_minimal_if", consensusJNI.verify_flags_minimal_if_get()); 26 | public final static verify_flags verify_flags_null_fail = new verify_flags("verify_flags_null_fail", consensusJNI.verify_flags_null_fail_get()); 27 | public final static verify_flags verify_flags_witness_public_key_compressed = new verify_flags("verify_flags_witness_public_key_compressed", consensusJNI.verify_flags_witness_public_key_compressed_get()); 28 | 29 | public final int swigValue() { 30 | return swigValue; 31 | } 32 | 33 | public String toString() { 34 | return swigName; 35 | } 36 | 37 | public static verify_flags swigToEnum(int swigValue) { 38 | if (swigValue < swigValues.length && swigValue >= 0 && swigValues[swigValue].swigValue == swigValue) 39 | return swigValues[swigValue]; 40 | for (int i = 0; i < swigValues.length; i++) 41 | if (swigValues[i].swigValue == swigValue) 42 | return swigValues[i]; 43 | throw new IllegalArgumentException("No enum " + verify_flags.class + " with value " + swigValue); 44 | } 45 | 46 | private verify_flags(String swigName) { 47 | this.swigName = swigName; 48 | this.swigValue = swigNext++; 49 | } 50 | 51 | private verify_flags(String swigName, int swigValue) { 52 | this.swigName = swigName; 53 | this.swigValue = swigValue; 54 | swigNext = swigValue+1; 55 | } 56 | 57 | private verify_flags(String swigName, verify_flags swigEnum) { 58 | this.swigName = swigName; 59 | this.swigValue = swigEnum.swigValue; 60 | swigNext = this.swigValue+1; 61 | } 62 | 63 | private static verify_flags[] swigValues = { verify_flags_none, verify_flags_p2sh, verify_flags_strictenc, verify_flags_dersig, verify_flags_low_s, verify_flags_nulldummy, verify_flags_sigpushonly, verify_flags_minimaldata, verify_flags_discourage_upgradable_nops, verify_flags_cleanstack, verify_flags_checklocktimeverify, verify_flags_checksequenceverify, verify_flags_witness, verify_flags_discourage_upgradable_witness_program, verify_flags_minimal_if, verify_flags_null_fail, verify_flags_witness_public_key_compressed }; 64 | private static int swigNext = 0; 65 | private final int swigValue; 66 | private final String swigName; 67 | } 68 | 69 | -------------------------------------------------------------------------------- /src/clone/uint256.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2009-2010 Satoshi Nakamoto 2 | // Copyright (c) 2009-2019 The Bitcoin Core developers 3 | // Distributed under the MIT software license, see the accompanying 4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 | 6 | #ifndef BITCOIN_UINT256_H 7 | #define BITCOIN_UINT256_H 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | /** Template base class for fixed-sized opaque blobs. */ 16 | template 17 | class base_blob 18 | { 19 | protected: 20 | static constexpr int WIDTH = BITS / 8; 21 | uint8_t m_data[WIDTH]; 22 | public: 23 | /* construct 0 value by default */ 24 | constexpr base_blob() : m_data() {} 25 | 26 | /* constructor for constants between 1 and 255 */ 27 | constexpr explicit base_blob(uint8_t v) : m_data{v} {} 28 | 29 | explicit base_blob(const std::vector& vch); 30 | 31 | bool IsNull() const 32 | { 33 | for (int i = 0; i < WIDTH; i++) 34 | if (m_data[i] != 0) 35 | return false; 36 | return true; 37 | } 38 | 39 | void SetNull() 40 | { 41 | memset(m_data, 0, sizeof(m_data)); 42 | } 43 | 44 | inline int Compare(const base_blob& other) const { return memcmp(m_data, other.m_data, sizeof(m_data)); } 45 | 46 | friend inline bool operator==(const base_blob& a, const base_blob& b) { return a.Compare(b) == 0; } 47 | friend inline bool operator!=(const base_blob& a, const base_blob& b) { return a.Compare(b) != 0; } 48 | friend inline bool operator<(const base_blob& a, const base_blob& b) { return a.Compare(b) < 0; } 49 | 50 | std::string GetHex() const; 51 | void SetHex(const char* psz); 52 | void SetHex(const std::string& str); 53 | std::string ToString() const; 54 | 55 | const unsigned char* data() const { return m_data; } 56 | unsigned char* data() { return m_data; } 57 | 58 | unsigned char* begin() 59 | { 60 | return &m_data[0]; 61 | } 62 | 63 | unsigned char* end() 64 | { 65 | return &m_data[WIDTH]; 66 | } 67 | 68 | const unsigned char* begin() const 69 | { 70 | return &m_data[0]; 71 | } 72 | 73 | const unsigned char* end() const 74 | { 75 | return &m_data[WIDTH]; 76 | } 77 | 78 | unsigned int size() const 79 | { 80 | return sizeof(m_data); 81 | } 82 | 83 | uint64_t GetUint64(int pos) const 84 | { 85 | const uint8_t* ptr = m_data + pos * 8; 86 | return ((uint64_t)ptr[0]) | \ 87 | ((uint64_t)ptr[1]) << 8 | \ 88 | ((uint64_t)ptr[2]) << 16 | \ 89 | ((uint64_t)ptr[3]) << 24 | \ 90 | ((uint64_t)ptr[4]) << 32 | \ 91 | ((uint64_t)ptr[5]) << 40 | \ 92 | ((uint64_t)ptr[6]) << 48 | \ 93 | ((uint64_t)ptr[7]) << 56; 94 | } 95 | 96 | template 97 | void Serialize(Stream& s) const 98 | { 99 | s.write((char*)m_data, sizeof(m_data)); 100 | } 101 | 102 | template 103 | void Unserialize(Stream& s) 104 | { 105 | s.read((char*)m_data, sizeof(m_data)); 106 | } 107 | }; 108 | 109 | /** 160-bit opaque blob. 110 | * @note This type is called uint160 for historical reasons only. It is an opaque 111 | * blob of 160 bits and has no integer operations. 112 | */ 113 | class uint160 : public base_blob<160> { 114 | public: 115 | constexpr uint160() {} 116 | explicit uint160(const std::vector& vch) : base_blob<160>(vch) {} 117 | }; 118 | 119 | /** 256-bit opaque blob. 120 | * @note This type is called uint256 for historical reasons only. It is an 121 | * opaque blob of 256 bits and has no integer operations. Use arith_uint256 if 122 | * those are required. 123 | */ 124 | class uint256 : public base_blob<256> { 125 | public: 126 | constexpr uint256() {} 127 | constexpr explicit uint256(uint8_t v) : base_blob<256>(v) {} 128 | explicit uint256(const std::vector& vch) : base_blob<256>(vch) {} 129 | static const uint256 ZERO; 130 | static const uint256 ONE; 131 | }; 132 | 133 | /* uint256 from const char *. 134 | * This is a separate function because the constructor uint256(const char*) can result 135 | * in dangerously catching uint256(0). 136 | */ 137 | inline uint256 uint256S(const char *str) 138 | { 139 | uint256 rv; 140 | rv.SetHex(str); 141 | return rv; 142 | } 143 | /* uint256 from std::string. 144 | * This is a separate function because the constructor uint256(const std::string &str) can result 145 | * in dangerously catching uint256(0) via std::string(const char*). 146 | */ 147 | inline uint256 uint256S(const std::string& str) 148 | { 149 | uint256 rv; 150 | rv.SetHex(str); 151 | return rv; 152 | } 153 | 154 | #endif // BITCOIN_UINT256_H 155 | -------------------------------------------------------------------------------- /src/clone/compat/endian.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2014-2018 The Bitcoin Core developers 2 | // Distributed under the MIT software license, see the accompanying 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 | 5 | #ifndef BITCOIN_COMPAT_ENDIAN_H 6 | #define BITCOIN_COMPAT_ENDIAN_H 7 | 8 | #if defined(HAVE_CONFIG_H) 9 | #include 10 | #endif 11 | 12 | #include 13 | 14 | #include 15 | 16 | #if defined(HAVE_ENDIAN_H) 17 | #include 18 | #elif defined(HAVE_SYS_ENDIAN_H) 19 | #include 20 | #endif 21 | 22 | #ifndef HAVE_CONFIG_H 23 | // While not technically a supported configuration, defaulting to defining these 24 | // DECLs when we were compiled without autotools makes it easier for other build 25 | // systems to build things like libbitcoinconsensus for strange targets. 26 | #ifdef htobe16 27 | #define HAVE_DECL_HTOBE16 1 28 | #endif 29 | #ifdef htole16 30 | #define HAVE_DECL_HTOLE16 1 31 | #endif 32 | #ifdef be16toh 33 | #define HAVE_DECL_BE16TOH 1 34 | #endif 35 | #ifdef le16toh 36 | #define HAVE_DECL_LE16TOH 1 37 | #endif 38 | 39 | #ifdef htobe32 40 | #define HAVE_DECL_HTOBE32 1 41 | #endif 42 | #ifdef htole32 43 | #define HAVE_DECL_HTOLE32 1 44 | #endif 45 | #ifdef be32toh 46 | #define HAVE_DECL_BE32TOH 1 47 | #endif 48 | #ifdef le32toh 49 | #define HAVE_DECL_LE32TOH 1 50 | #endif 51 | 52 | #ifdef htobe64 53 | #define HAVE_DECL_HTOBE64 1 54 | #endif 55 | #ifdef htole64 56 | #define HAVE_DECL_HTOLE64 1 57 | #endif 58 | #ifdef be64toh 59 | #define HAVE_DECL_BE64TOH 1 60 | #endif 61 | #ifdef le64toh 62 | #define HAVE_DECL_LE64TOH 1 63 | #endif 64 | 65 | #endif // HAVE_CONFIG_H 66 | 67 | #if defined(WORDS_BIGENDIAN) 68 | 69 | #if HAVE_DECL_HTOBE16 == 0 70 | inline uint16_t htobe16(uint16_t host_16bits) 71 | { 72 | return host_16bits; 73 | } 74 | #endif // HAVE_DECL_HTOBE16 75 | 76 | #if HAVE_DECL_HTOLE16 == 0 77 | inline uint16_t htole16(uint16_t host_16bits) 78 | { 79 | return bswap_16(host_16bits); 80 | } 81 | #endif // HAVE_DECL_HTOLE16 82 | 83 | #if HAVE_DECL_BE16TOH == 0 84 | inline uint16_t be16toh(uint16_t big_endian_16bits) 85 | { 86 | return big_endian_16bits; 87 | } 88 | #endif // HAVE_DECL_BE16TOH 89 | 90 | #if HAVE_DECL_LE16TOH == 0 91 | inline uint16_t le16toh(uint16_t little_endian_16bits) 92 | { 93 | return bswap_16(little_endian_16bits); 94 | } 95 | #endif // HAVE_DECL_LE16TOH 96 | 97 | #if HAVE_DECL_HTOBE32 == 0 98 | inline uint32_t htobe32(uint32_t host_32bits) 99 | { 100 | return host_32bits; 101 | } 102 | #endif // HAVE_DECL_HTOBE32 103 | 104 | #if HAVE_DECL_HTOLE32 == 0 105 | inline uint32_t htole32(uint32_t host_32bits) 106 | { 107 | return bswap_32(host_32bits); 108 | } 109 | #endif // HAVE_DECL_HTOLE32 110 | 111 | #if HAVE_DECL_BE32TOH == 0 112 | inline uint32_t be32toh(uint32_t big_endian_32bits) 113 | { 114 | return big_endian_32bits; 115 | } 116 | #endif // HAVE_DECL_BE32TOH 117 | 118 | #if HAVE_DECL_LE32TOH == 0 119 | inline uint32_t le32toh(uint32_t little_endian_32bits) 120 | { 121 | return bswap_32(little_endian_32bits); 122 | } 123 | #endif // HAVE_DECL_LE32TOH 124 | 125 | #if HAVE_DECL_HTOBE64 == 0 126 | inline uint64_t htobe64(uint64_t host_64bits) 127 | { 128 | return host_64bits; 129 | } 130 | #endif // HAVE_DECL_HTOBE64 131 | 132 | #if HAVE_DECL_HTOLE64 == 0 133 | inline uint64_t htole64(uint64_t host_64bits) 134 | { 135 | return bswap_64(host_64bits); 136 | } 137 | #endif // HAVE_DECL_HTOLE64 138 | 139 | #if HAVE_DECL_BE64TOH == 0 140 | inline uint64_t be64toh(uint64_t big_endian_64bits) 141 | { 142 | return big_endian_64bits; 143 | } 144 | #endif // HAVE_DECL_BE64TOH 145 | 146 | #if HAVE_DECL_LE64TOH == 0 147 | inline uint64_t le64toh(uint64_t little_endian_64bits) 148 | { 149 | return bswap_64(little_endian_64bits); 150 | } 151 | #endif // HAVE_DECL_LE64TOH 152 | 153 | #else // WORDS_BIGENDIAN 154 | 155 | #if HAVE_DECL_HTOBE16 == 0 156 | inline uint16_t htobe16(uint16_t host_16bits) 157 | { 158 | return bswap_16(host_16bits); 159 | } 160 | #endif // HAVE_DECL_HTOBE16 161 | 162 | #if HAVE_DECL_HTOLE16 == 0 163 | inline uint16_t htole16(uint16_t host_16bits) 164 | { 165 | return host_16bits; 166 | } 167 | #endif // HAVE_DECL_HTOLE16 168 | 169 | #if HAVE_DECL_BE16TOH == 0 170 | inline uint16_t be16toh(uint16_t big_endian_16bits) 171 | { 172 | return bswap_16(big_endian_16bits); 173 | } 174 | #endif // HAVE_DECL_BE16TOH 175 | 176 | #if HAVE_DECL_LE16TOH == 0 177 | inline uint16_t le16toh(uint16_t little_endian_16bits) 178 | { 179 | return little_endian_16bits; 180 | } 181 | #endif // HAVE_DECL_LE16TOH 182 | 183 | #if HAVE_DECL_HTOBE32 == 0 184 | inline uint32_t htobe32(uint32_t host_32bits) 185 | { 186 | return bswap_32(host_32bits); 187 | } 188 | #endif // HAVE_DECL_HTOBE32 189 | 190 | #if HAVE_DECL_HTOLE32 == 0 191 | inline uint32_t htole32(uint32_t host_32bits) 192 | { 193 | return host_32bits; 194 | } 195 | #endif // HAVE_DECL_HTOLE32 196 | 197 | #if HAVE_DECL_BE32TOH == 0 198 | inline uint32_t be32toh(uint32_t big_endian_32bits) 199 | { 200 | return bswap_32(big_endian_32bits); 201 | } 202 | #endif // HAVE_DECL_BE32TOH 203 | 204 | #if HAVE_DECL_LE32TOH == 0 205 | inline uint32_t le32toh(uint32_t little_endian_32bits) 206 | { 207 | return little_endian_32bits; 208 | } 209 | #endif // HAVE_DECL_LE32TOH 210 | 211 | #if HAVE_DECL_HTOBE64 == 0 212 | inline uint64_t htobe64(uint64_t host_64bits) 213 | { 214 | return bswap_64(host_64bits); 215 | } 216 | #endif // HAVE_DECL_HTOBE64 217 | 218 | #if HAVE_DECL_HTOLE64 == 0 219 | inline uint64_t htole64(uint64_t host_64bits) 220 | { 221 | return host_64bits; 222 | } 223 | #endif // HAVE_DECL_HTOLE64 224 | 225 | #if HAVE_DECL_BE64TOH == 0 226 | inline uint64_t be64toh(uint64_t big_endian_64bits) 227 | { 228 | return bswap_64(big_endian_64bits); 229 | } 230 | #endif // HAVE_DECL_BE64TOH 231 | 232 | #if HAVE_DECL_LE64TOH == 0 233 | inline uint64_t le64toh(uint64_t little_endian_64bits) 234 | { 235 | return little_endian_64bits; 236 | } 237 | #endif // HAVE_DECL_LE64TOH 238 | 239 | #endif // WORDS_BIGENDIAN 240 | 241 | #endif // BITCOIN_COMPAT_ENDIAN_H 242 | -------------------------------------------------------------------------------- /m4/ax_boost_unit_test_framework.m4: -------------------------------------------------------------------------------- 1 | # ================================================================================= 2 | # https://www.gnu.org/software/autoconf-archive/ax_boost_unit_test_framework.html 3 | # ================================================================================= 4 | # 5 | # SYNOPSIS 6 | # 7 | # AX_BOOST_UNIT_TEST_FRAMEWORK 8 | # 9 | # DESCRIPTION 10 | # 11 | # Test for Unit_Test_Framework library from the Boost C++ libraries. The 12 | # macro requires a preceding call to AX_BOOST_BASE. Further documentation 13 | # is available at . 14 | # 15 | # This macro calls: 16 | # 17 | # AC_SUBST(BOOST_UNIT_TEST_FRAMEWORK_LIB) 18 | # 19 | # And sets: 20 | # 21 | # HAVE_BOOST_UNIT_TEST_FRAMEWORK 22 | # 23 | # LICENSE 24 | # 25 | # Copyright (c) 2008 Thomas Porschberg 26 | # 27 | # Copying and distribution of this file, with or without modification, are 28 | # permitted in any medium without royalty provided the copyright notice 29 | # and this notice are preserved. This file is offered as-is, without any 30 | # warranty. 31 | 32 | #serial 22 33 | 34 | AC_DEFUN([AX_BOOST_UNIT_TEST_FRAMEWORK], 35 | [ 36 | AC_ARG_WITH([boost-unit-test-framework], 37 | AS_HELP_STRING([--with-boost-unit-test-framework@<:@=special-lib@:>@], 38 | [use the Unit_Test_Framework library from boost - it is possible to specify a certain library for the linker 39 | e.g. --with-boost-unit-test-framework=boost_unit_test_framework-gcc ]), 40 | [ 41 | if test "$withval" = "no"; then 42 | want_boost="no" 43 | elif test "$withval" = "yes"; then 44 | want_boost="yes" 45 | ax_boost_user_unit_test_framework_lib="" 46 | else 47 | want_boost="yes" 48 | ax_boost_user_unit_test_framework_lib="$withval" 49 | fi 50 | ], 51 | [want_boost="yes"] 52 | ) 53 | 54 | if test "x$want_boost" = "xyes"; then 55 | AC_REQUIRE([AC_PROG_CC]) 56 | CPPFLAGS_SAVED="$CPPFLAGS" 57 | CPPFLAGS="$CPPFLAGS $BOOST_CPPFLAGS" 58 | export CPPFLAGS 59 | 60 | LDFLAGS_SAVED="$LDFLAGS" 61 | LDFLAGS="$LDFLAGS $BOOST_LDFLAGS" 62 | export LDFLAGS 63 | 64 | AC_CACHE_CHECK(whether the Boost::Unit_Test_Framework library is available, 65 | ax_cv_boost_unit_test_framework, 66 | [AC_LANG_PUSH([C++]) 67 | AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[@%:@include ]], 68 | [[using boost::unit_test::test_suite; 69 | test_suite* test= BOOST_TEST_SUITE( "Unit test example 1" ); if (test == NULL) { return 1; } else { return 0; }]])], 70 | ax_cv_boost_unit_test_framework=yes, ax_cv_boost_unit_test_framework=no) 71 | AC_LANG_POP([C++]) 72 | ]) 73 | if test "x$ax_cv_boost_unit_test_framework" = "xyes"; then 74 | AC_DEFINE(HAVE_BOOST_UNIT_TEST_FRAMEWORK,,[define if the Boost::Unit_Test_Framework library is available]) 75 | BOOSTLIBDIR=`echo $BOOST_LDFLAGS | sed -e 's/@<:@^\/@:>@*//'` 76 | 77 | if test "x$ax_boost_user_unit_test_framework_lib" = "x"; then 78 | saved_ldflags="${LDFLAGS}" 79 | for monitor_library in `ls $BOOSTLIBDIR/libboost_unit_test_framework*.so* $BOOSTLIBDIR/libboost_unit_test_framework*.dylib* $BOOSTLIBDIR/libboost_unit_test_framework*.a* 2>/dev/null` ; do 80 | if test -r $monitor_library ; then 81 | libextension=`echo $monitor_library | sed 's,.*/,,' | sed -e 's;^lib\(boost_unit_test_framework.*\)\.so.*$;\1;' -e 's;^lib\(boost_unit_test_framework.*\)\.dylib.*$;\1;' -e 's;^lib\(boost_unit_test_framework.*\)\.a.*$;\1;'` 82 | ax_lib=${libextension} 83 | link_unit_test_framework="yes" 84 | else 85 | link_unit_test_framework="no" 86 | fi 87 | 88 | if test "x$link_unit_test_framework" = "xyes"; then 89 | BOOST_UNIT_TEST_FRAMEWORK_LIB="-l$ax_lib" 90 | AC_SUBST(BOOST_UNIT_TEST_FRAMEWORK_LIB) 91 | break 92 | fi 93 | done 94 | if test "x$link_unit_test_framework" != "xyes"; then 95 | for libextension in `ls $BOOSTLIBDIR/boost_unit_test_framework*.dll* $BOOSTLIBDIR/boost_unit_test_framework*.a* 2>/dev/null | sed 's,.*/,,' | sed -e 's;^\(boost_unit_test_framework.*\)\.dll.*$;\1;' -e 's;^\(boost_unit_test_framework.*\)\.a.*$;\1;'` ; do 96 | ax_lib=${libextension} 97 | AC_CHECK_LIB($ax_lib, exit, 98 | [BOOST_UNIT_TEST_FRAMEWORK_LIB="-l$ax_lib"; AC_SUBST(BOOST_UNIT_TEST_FRAMEWORK_LIB) link_unit_test_framework="yes"; break], 99 | [link_unit_test_framework="no"]) 100 | done 101 | fi 102 | else 103 | link_unit_test_framework="no" 104 | saved_ldflags="${LDFLAGS}" 105 | for ax_lib in boost_unit_test_framework-$ax_boost_user_unit_test_framework_lib $ax_boost_user_unit_test_framework_lib ; do 106 | if test "x$link_unit_test_framework" = "xyes"; then 107 | break; 108 | fi 109 | for unittest_library in `ls $BOOSTLIBDIR/lib${ax_lib}.so* $BOOSTLIBDIR/lib${ax_lib}.a* 2>/dev/null` ; do 110 | if test -r $unittest_library ; then 111 | libextension=`echo $unittest_library | sed 's,.*/,,' | sed -e 's;^lib\(boost_unit_test_framework.*\)\.so.*$;\1;' -e 's;^lib\(boost_unit_test_framework.*\)\.a*$;\1;'` 112 | ax_lib=${libextension} 113 | link_unit_test_framework="yes" 114 | else 115 | link_unit_test_framework="no" 116 | fi 117 | 118 | if test "x$link_unit_test_framework" = "xyes"; then 119 | BOOST_UNIT_TEST_FRAMEWORK_LIB="-l$ax_lib" 120 | AC_SUBST(BOOST_UNIT_TEST_FRAMEWORK_LIB) 121 | break 122 | fi 123 | done 124 | done 125 | fi 126 | if test "x$ax_lib" = "x"; then 127 | AC_MSG_ERROR(Could not find a version of the Boost::Unit_Test_Framework library!) 128 | fi 129 | if test "x$link_unit_test_framework" != "xyes"; then 130 | AC_MSG_ERROR(Could not link against $ax_lib !) 131 | fi 132 | fi 133 | 134 | CPPFLAGS="$CPPFLAGS_SAVED" 135 | LDFLAGS="$LDFLAGS_SAVED" 136 | fi 137 | ]) 138 | -------------------------------------------------------------------------------- /src/clone/hash.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2009-2010 Satoshi Nakamoto 2 | // Copyright (c) 2009-2018 The Bitcoin Core developers 3 | // Distributed under the MIT software license, see the accompanying 4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 | 6 | #ifndef BITCOIN_HASH_H 7 | #define BITCOIN_HASH_H 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | #include 19 | #include 20 | 21 | typedef uint256 ChainCode; 22 | 23 | /** A hasher class for Bitcoin's 256-bit hash (double SHA-256). */ 24 | class CHash256 { 25 | private: 26 | CSHA256 sha; 27 | public: 28 | static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE; 29 | 30 | void Finalize(Span output) { 31 | assert(output.size() == OUTPUT_SIZE); 32 | unsigned char buf[CSHA256::OUTPUT_SIZE]; 33 | sha.Finalize(buf); 34 | sha.Reset().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(output.data()); 35 | } 36 | 37 | CHash256& Write(Span input) { 38 | sha.Write(input.data(), input.size()); 39 | return *this; 40 | } 41 | 42 | CHash256& Reset() { 43 | sha.Reset(); 44 | return *this; 45 | } 46 | }; 47 | 48 | /** A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160). */ 49 | class CHash160 { 50 | private: 51 | CSHA256 sha; 52 | public: 53 | static const size_t OUTPUT_SIZE = CRIPEMD160::OUTPUT_SIZE; 54 | 55 | void Finalize(Span output) { 56 | assert(output.size() == OUTPUT_SIZE); 57 | unsigned char buf[CSHA256::OUTPUT_SIZE]; 58 | sha.Finalize(buf); 59 | CRIPEMD160().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(output.data()); 60 | } 61 | 62 | CHash160& Write(Span input) { 63 | sha.Write(input.data(), input.size()); 64 | return *this; 65 | } 66 | 67 | CHash160& Reset() { 68 | sha.Reset(); 69 | return *this; 70 | } 71 | }; 72 | 73 | /** Compute the 256-bit hash of an object. */ 74 | template 75 | inline uint256 Hash(const T& in1) 76 | { 77 | uint256 result; 78 | CHash256().Write(MakeUCharSpan(in1)).Finalize(result); 79 | return result; 80 | } 81 | 82 | /** Compute the 256-bit hash of the concatenation of two objects. */ 83 | template 84 | inline uint256 Hash(const T1& in1, const T2& in2) { 85 | uint256 result; 86 | CHash256().Write(MakeUCharSpan(in1)).Write(MakeUCharSpan(in2)).Finalize(result); 87 | return result; 88 | } 89 | 90 | /** Compute the 160-bit hash an object. */ 91 | template 92 | inline uint160 Hash160(const T1& in1) 93 | { 94 | uint160 result; 95 | CHash160().Write(MakeUCharSpan(in1)).Finalize(result); 96 | return result; 97 | } 98 | 99 | /** A writer stream (for serialization) that computes a 256-bit hash. */ 100 | class CHashWriter 101 | { 102 | private: 103 | CSHA256 ctx; 104 | 105 | const int nType; 106 | const int nVersion; 107 | public: 108 | 109 | CHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {} 110 | 111 | int GetType() const { return nType; } 112 | int GetVersion() const { return nVersion; } 113 | 114 | void write(const char *pch, size_t size) { 115 | ctx.Write((const unsigned char*)pch, size); 116 | } 117 | 118 | /** Compute the double-SHA256 hash of all data written to this object. 119 | * 120 | * Invalidates this object. 121 | */ 122 | uint256 GetHash() { 123 | uint256 result; 124 | ctx.Finalize(result.begin()); 125 | ctx.Reset().Write(result.begin(), CSHA256::OUTPUT_SIZE).Finalize(result.begin()); 126 | return result; 127 | } 128 | 129 | /** Compute the SHA256 hash of all data written to this object. 130 | * 131 | * Invalidates this object. 132 | */ 133 | uint256 GetSHA256() { 134 | uint256 result; 135 | ctx.Finalize(result.begin()); 136 | return result; 137 | } 138 | 139 | /** 140 | * Returns the first 64 bits from the resulting hash. 141 | */ 142 | inline uint64_t GetCheapHash() { 143 | uint256 result = GetHash(); 144 | return ReadLE64(result.begin()); 145 | } 146 | 147 | template 148 | CHashWriter& operator<<(const T& obj) { 149 | // Serialize to this stream 150 | ::Serialize(*this, obj); 151 | return (*this); 152 | } 153 | }; 154 | 155 | /** Reads data from an underlying stream, while hashing the read data. */ 156 | template 157 | class CHashVerifier : public CHashWriter 158 | { 159 | private: 160 | Source* source; 161 | 162 | public: 163 | explicit CHashVerifier(Source* source_) : CHashWriter(source_->GetType(), source_->GetVersion()), source(source_) {} 164 | 165 | void read(char* pch, size_t nSize) 166 | { 167 | source->read(pch, nSize); 168 | this->write(pch, nSize); 169 | } 170 | 171 | void ignore(size_t nSize) 172 | { 173 | char data[1024]; 174 | while (nSize > 0) { 175 | size_t now = std::min(nSize, 1024); 176 | read(data, now); 177 | nSize -= now; 178 | } 179 | } 180 | 181 | template 182 | CHashVerifier& operator>>(T&& obj) 183 | { 184 | // Unserialize from this stream 185 | ::Unserialize(*this, obj); 186 | return (*this); 187 | } 188 | }; 189 | 190 | /** Compute the 256-bit hash of an object's serialization. */ 191 | template 192 | uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION) 193 | { 194 | CHashWriter ss(nType, nVersion); 195 | ss << obj; 196 | return ss.GetHash(); 197 | } 198 | 199 | /** Single-SHA256 a 32-byte input (represented as uint256). */ 200 | NODISCARD uint256 SHA256Uint256(const uint256& input); 201 | 202 | unsigned int MurmurHash3(unsigned int nHashSeed, Span vDataToHash); 203 | 204 | void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]); 205 | 206 | /** Return a CHashWriter primed for tagged hashes (as specified in BIP 340). 207 | * 208 | * The returned object will have SHA256(tag) written to it twice (= 64 bytes). 209 | * A tagged hash can be computed by feeding the message into this object, and 210 | * then calling CHashWriter::GetSHA256(). 211 | */ 212 | CHashWriter TaggedHash(const std::string& tag); 213 | 214 | #endif // BITCOIN_HASH_H 215 | -------------------------------------------------------------------------------- /test/consensus__verify_flags_to_script_flags.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2011-2023 libbitcoin developers (see AUTHORS) 3 | * 4 | * This file is part of libbitcoin. 5 | * 6 | * This program is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public License 17 | * along with this program. If not, see . 18 | */ 19 | #include 20 | #include 21 | #include 22 | 23 | // These give us test accesss to unpublished symbols. 24 | #include "consensus/consensus.hpp" 25 | #include "script/interpreter.h" 26 | 27 | using namespace libbitcoin::consensus; 28 | 29 | BOOST_AUTO_TEST_SUITE(consensus__verify_flags_to_script_flags) 30 | 31 | // Unnamed enum values require cast for boost comparison macros. 32 | 33 | BOOST_AUTO_TEST_CASE(consensus__verify_flags_to_script_flags__none__NONE) 34 | { 35 | BOOST_REQUIRE_EQUAL(verify_flags_to_script_flags(verify_flags_none), (uint32_t)SCRIPT_VERIFY_NONE); 36 | } 37 | 38 | BOOST_AUTO_TEST_CASE(consensus__verify_flags_to_script_flags__p2sh__P2SH) 39 | { 40 | BOOST_REQUIRE_EQUAL(verify_flags_to_script_flags(verify_flags_p2sh), (uint32_t)SCRIPT_VERIFY_P2SH); 41 | } 42 | 43 | BOOST_AUTO_TEST_CASE(consensus__verify_flags_to_script_flags__strictenc__STRICTENC) 44 | { 45 | BOOST_REQUIRE_EQUAL(verify_flags_to_script_flags(verify_flags_strictenc), (uint32_t)SCRIPT_VERIFY_STRICTENC); 46 | } 47 | 48 | BOOST_AUTO_TEST_CASE(consensus__verify_flags_to_script_flags__dersig__DERSIG) 49 | { 50 | BOOST_REQUIRE_EQUAL(verify_flags_to_script_flags(verify_flags_dersig), (uint32_t)SCRIPT_VERIFY_DERSIG); 51 | } 52 | 53 | BOOST_AUTO_TEST_CASE(consensus__verify_flags_to_script_flags__low_s__LOW_S) 54 | { 55 | BOOST_REQUIRE_EQUAL(verify_flags_to_script_flags(verify_flags_low_s), (uint32_t)SCRIPT_VERIFY_LOW_S); 56 | } 57 | 58 | BOOST_AUTO_TEST_CASE(consensus__verify_flags_to_script_flags__nulldummy__NULLDUMMY) 59 | { 60 | BOOST_REQUIRE_EQUAL(verify_flags_to_script_flags(verify_flags_nulldummy), (uint32_t)SCRIPT_VERIFY_NULLDUMMY); 61 | } 62 | 63 | BOOST_AUTO_TEST_CASE(consensus__verify_flags_to_script_flags__sigpushonly__SIGPUSHONLY) 64 | { 65 | BOOST_REQUIRE_EQUAL(verify_flags_to_script_flags(verify_flags_sigpushonly), (uint32_t)SCRIPT_VERIFY_SIGPUSHONLY); 66 | } 67 | 68 | BOOST_AUTO_TEST_CASE(consensus__verify_flags_to_script_flags__minimaldata__MINIMALDATA) 69 | { 70 | BOOST_REQUIRE_EQUAL(verify_flags_to_script_flags(verify_flags_minimaldata), (uint32_t)SCRIPT_VERIFY_MINIMALDATA); 71 | } 72 | 73 | BOOST_AUTO_TEST_CASE(consensus__verify_flags_to_script_flags__discourage_upgradable_nops__DISCOURAGE_UPGRADABLE_NOPS) 74 | { 75 | BOOST_REQUIRE_EQUAL(verify_flags_to_script_flags(verify_flags_discourage_upgradable_nops), (uint32_t)SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS); 76 | } 77 | 78 | BOOST_AUTO_TEST_CASE(consensus__verify_flags_to_script_flags__cleanstack__CLEANSTACK) 79 | { 80 | BOOST_REQUIRE_EQUAL(verify_flags_to_script_flags(verify_flags_cleanstack), (uint32_t)SCRIPT_VERIFY_CLEANSTACK); 81 | } 82 | 83 | BOOST_AUTO_TEST_CASE(consensus__verify_flags_to_script_flags__checklocktimeverify__CHECKLOCKTIMEVERIFY) 84 | { 85 | BOOST_REQUIRE_EQUAL(verify_flags_to_script_flags(verify_flags_checklocktimeverify), (uint32_t)SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY); 86 | } 87 | 88 | BOOST_AUTO_TEST_CASE(consensus__verify_flags_to_script_flags__checksequenceverify__CHECKSEQUENCEVERIFY) 89 | { 90 | BOOST_REQUIRE_EQUAL(verify_flags_to_script_flags(verify_flags_checksequenceverify), (uint32_t)SCRIPT_VERIFY_CHECKSEQUENCEVERIFY); 91 | } 92 | 93 | BOOST_AUTO_TEST_CASE(consensus__verify_flags_to_script_flags__witness__WITNESS) 94 | { 95 | BOOST_REQUIRE_EQUAL(verify_flags_to_script_flags(verify_flags_witness), (uint32_t)SCRIPT_VERIFY_WITNESS); 96 | } 97 | 98 | BOOST_AUTO_TEST_CASE(consensus__verify_flags_to_script_flags__discourage_upgradable_witness_program__DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM) 99 | { 100 | BOOST_REQUIRE_EQUAL(verify_flags_to_script_flags(verify_flags_discourage_upgradable_witness_program), (uint32_t)SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM); 101 | } 102 | 103 | BOOST_AUTO_TEST_CASE(consensus__verify_flags_to_script_flags__minimal_if__MINIMALIF) 104 | { 105 | BOOST_REQUIRE_EQUAL(verify_flags_to_script_flags(verify_flags_minimal_if), (uint32_t)SCRIPT_VERIFY_MINIMALIF); 106 | } 107 | 108 | BOOST_AUTO_TEST_CASE(consensus__verify_flags_to_script_flags__null_fail__NULLFAIL) 109 | { 110 | BOOST_REQUIRE_EQUAL(verify_flags_to_script_flags(verify_flags_null_fail), (uint32_t)SCRIPT_VERIFY_NULLFAIL); 111 | } 112 | 113 | BOOST_AUTO_TEST_CASE(consensus__verify_flags_to_script_flags__witness_public_key_compressed__PUBKEYTYPE) 114 | { 115 | BOOST_REQUIRE_EQUAL(verify_flags_to_script_flags(verify_flags_witness_public_key_compressed), (uint32_t)SCRIPT_VERIFY_WITNESS_PUBKEYTYPE); 116 | } 117 | 118 | BOOST_AUTO_TEST_CASE(consensus__verify_flags_to_script_flags__all__all) 119 | { 120 | const uint32_t all_verify_flags = 121 | verify_flags_none | 122 | verify_flags_p2sh | 123 | verify_flags_strictenc | 124 | verify_flags_dersig | 125 | verify_flags_low_s | 126 | verify_flags_nulldummy | 127 | verify_flags_sigpushonly | 128 | verify_flags_minimaldata | 129 | verify_flags_discourage_upgradable_nops | 130 | verify_flags_cleanstack | 131 | verify_flags_checklocktimeverify | 132 | verify_flags_checksequenceverify | 133 | verify_flags_witness | 134 | verify_flags_discourage_upgradable_witness_program | 135 | verify_flags_minimal_if | 136 | verify_flags_null_fail | 137 | verify_flags_witness_public_key_compressed; 138 | 139 | const uint32_t all_script_flags = 140 | SCRIPT_VERIFY_NONE | 141 | SCRIPT_VERIFY_P2SH | 142 | SCRIPT_VERIFY_STRICTENC | 143 | SCRIPT_VERIFY_DERSIG | 144 | SCRIPT_VERIFY_LOW_S | 145 | SCRIPT_VERIFY_NULLDUMMY | 146 | SCRIPT_VERIFY_SIGPUSHONLY | 147 | SCRIPT_VERIFY_MINIMALDATA | 148 | SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS | 149 | SCRIPT_VERIFY_CLEANSTACK | 150 | SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY | 151 | SCRIPT_VERIFY_CHECKSEQUENCEVERIFY | 152 | SCRIPT_VERIFY_WITNESS | 153 | SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM | 154 | SCRIPT_VERIFY_MINIMALIF | 155 | SCRIPT_VERIFY_NULLFAIL | 156 | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE; 157 | 158 | BOOST_REQUIRE_EQUAL(verify_flags_to_script_flags(all_verify_flags), all_script_flags); 159 | } 160 | 161 | BOOST_AUTO_TEST_SUITE_END() 162 | -------------------------------------------------------------------------------- /bindings/java/proxy/org/libbitcoin/consensus/verify_result.java: -------------------------------------------------------------------------------- 1 | /* ---------------------------------------------------------------------------- 2 | * This file was automatically generated by SWIG (http://www.swig.org). 3 | * Version 3.0.8 4 | * 5 | * Do not make changes to this file unless you know what you are doing--modify 6 | * the SWIG interface file instead. 7 | * ----------------------------------------------------------------------------- */ 8 | 9 | 10 | public final class verify_result { 11 | public final static verify_result verify_result_eval_false = new verify_result("verify_result_eval_false", consensusJNI.verify_result_eval_false_get()); 12 | public final static verify_result verify_result_eval_true = new verify_result("verify_result_eval_true"); 13 | public final static verify_result verify_result_script_size = new verify_result("verify_result_script_size"); 14 | public final static verify_result verify_result_push_size = new verify_result("verify_result_push_size"); 15 | public final static verify_result verify_result_op_count = new verify_result("verify_result_op_count"); 16 | public final static verify_result verify_result_stack_size = new verify_result("verify_result_stack_size"); 17 | public final static verify_result verify_result_sig_count = new verify_result("verify_result_sig_count"); 18 | public final static verify_result verify_result_pubkey_count = new verify_result("verify_result_pubkey_count"); 19 | public final static verify_result verify_result_verify = new verify_result("verify_result_verify"); 20 | public final static verify_result verify_result_equalverify = new verify_result("verify_result_equalverify"); 21 | public final static verify_result verify_result_checkmultisigverify = new verify_result("verify_result_checkmultisigverify"); 22 | public final static verify_result verify_result_checksigverify = new verify_result("verify_result_checksigverify"); 23 | public final static verify_result verify_result_numequalverify = new verify_result("verify_result_numequalverify"); 24 | public final static verify_result verify_result_bad_opcode = new verify_result("verify_result_bad_opcode"); 25 | public final static verify_result verify_result_disabled_opcode = new verify_result("verify_result_disabled_opcode"); 26 | public final static verify_result verify_result_invalid_stack_operation = new verify_result("verify_result_invalid_stack_operation"); 27 | public final static verify_result verify_result_invalid_altstack_operation = new verify_result("verify_result_invalid_altstack_operation"); 28 | public final static verify_result verify_result_unbalanced_conditional = new verify_result("verify_result_unbalanced_conditional"); 29 | public final static verify_result verify_result_sig_hashtype = new verify_result("verify_result_sig_hashtype"); 30 | public final static verify_result verify_result_sig_der = new verify_result("verify_result_sig_der"); 31 | public final static verify_result verify_result_minimaldata = new verify_result("verify_result_minimaldata"); 32 | public final static verify_result verify_result_sig_pushonly = new verify_result("verify_result_sig_pushonly"); 33 | public final static verify_result verify_result_sig_high_s = new verify_result("verify_result_sig_high_s"); 34 | public final static verify_result verify_result_sig_nulldummy = new verify_result("verify_result_sig_nulldummy"); 35 | public final static verify_result verify_result_pubkeytype = new verify_result("verify_result_pubkeytype"); 36 | public final static verify_result verify_result_cleanstack = new verify_result("verify_result_cleanstack"); 37 | public final static verify_result verify_result_minimalif = new verify_result("verify_result_minimalif"); 38 | public final static verify_result verify_result_sig_nullfail = new verify_result("verify_result_sig_nullfail"); 39 | public final static verify_result verify_result_discourage_upgradable_nops = new verify_result("verify_result_discourage_upgradable_nops"); 40 | public final static verify_result verify_result_discourage_upgradable_witness_program = new verify_result("verify_result_discourage_upgradable_witness_program"); 41 | public final static verify_result verify_result_op_return = new verify_result("verify_result_op_return"); 42 | public final static verify_result verify_result_unknown_error = new verify_result("verify_result_unknown_error"); 43 | public final static verify_result verify_result_witness_program_wrong_length = new verify_result("verify_result_witness_program_wrong_length"); 44 | public final static verify_result verify_result_witness_program_empty_witness = new verify_result("verify_result_witness_program_empty_witness"); 45 | public final static verify_result verify_result_witness_program_mismatch = new verify_result("verify_result_witness_program_mismatch"); 46 | public final static verify_result verify_result_witness_malleated = new verify_result("verify_result_witness_malleated"); 47 | public final static verify_result verify_result_witness_malleated_p2sh = new verify_result("verify_result_witness_malleated_p2sh"); 48 | public final static verify_result verify_result_witness_unexpected = new verify_result("verify_result_witness_unexpected"); 49 | public final static verify_result verify_result_witness_pubkeytype = new verify_result("verify_result_witness_pubkeytype"); 50 | public final static verify_result verify_result_tx_invalid = new verify_result("verify_result_tx_invalid"); 51 | public final static verify_result verify_result_tx_size_invalid = new verify_result("verify_result_tx_size_invalid"); 52 | public final static verify_result verify_result_tx_input_invalid = new verify_result("verify_result_tx_input_invalid"); 53 | public final static verify_result verify_result_negative_locktime = new verify_result("verify_result_negative_locktime"); 54 | public final static verify_result verify_result_unsatisfied_locktime = new verify_result("verify_result_unsatisfied_locktime"); 55 | 56 | public final int swigValue() { 57 | return swigValue; 58 | } 59 | 60 | public String toString() { 61 | return swigName; 62 | } 63 | 64 | public static verify_result swigToEnum(int swigValue) { 65 | if (swigValue < swigValues.length && swigValue >= 0 && swigValues[swigValue].swigValue == swigValue) 66 | return swigValues[swigValue]; 67 | for (int i = 0; i < swigValues.length; i++) 68 | if (swigValues[i].swigValue == swigValue) 69 | return swigValues[i]; 70 | throw new IllegalArgumentException("No enum " + verify_result.class + " with value " + swigValue); 71 | } 72 | 73 | private verify_result(String swigName) { 74 | this.swigName = swigName; 75 | this.swigValue = swigNext++; 76 | } 77 | 78 | private verify_result(String swigName, int swigValue) { 79 | this.swigName = swigName; 80 | this.swigValue = swigValue; 81 | swigNext = swigValue+1; 82 | } 83 | 84 | private verify_result(String swigName, verify_result swigEnum) { 85 | this.swigName = swigName; 86 | this.swigValue = swigEnum.swigValue; 87 | swigNext = this.swigValue+1; 88 | } 89 | 90 | private static verify_result[] swigValues = { verify_result_eval_false, verify_result_eval_true, verify_result_script_size, verify_result_push_size, verify_result_op_count, verify_result_stack_size, verify_result_sig_count, verify_result_pubkey_count, verify_result_verify, verify_result_equalverify, verify_result_checkmultisigverify, verify_result_checksigverify, verify_result_numequalverify, verify_result_bad_opcode, verify_result_disabled_opcode, verify_result_invalid_stack_operation, verify_result_invalid_altstack_operation, verify_result_unbalanced_conditional, verify_result_sig_hashtype, verify_result_sig_der, verify_result_minimaldata, verify_result_sig_pushonly, verify_result_sig_high_s, verify_result_sig_nulldummy, verify_result_pubkeytype, verify_result_cleanstack, verify_result_minimalif, verify_result_sig_nullfail, verify_result_discourage_upgradable_nops, verify_result_discourage_upgradable_witness_program, verify_result_op_return, verify_result_unknown_error, verify_result_witness_program_wrong_length, verify_result_witness_program_empty_witness, verify_result_witness_program_mismatch, verify_result_witness_malleated, verify_result_witness_malleated_p2sh, verify_result_witness_unexpected, verify_result_witness_pubkeytype, verify_result_tx_invalid, verify_result_tx_size_invalid, verify_result_tx_input_invalid, verify_result_negative_locktime, verify_result_unsatisfied_locktime }; 91 | private static int swigNext = 0; 92 | private final int swigValue; 93 | private final String swigName; 94 | } 95 | 96 | -------------------------------------------------------------------------------- /src/clone/pubkey.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2009-2010 Satoshi Nakamoto 2 | // Copyright (c) 2009-2019 The Bitcoin Core developers 3 | // Copyright (c) 2017 The Zcash developers 4 | // Distributed under the MIT software license, see the accompanying 5 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 6 | 7 | #ifndef BITCOIN_PUBKEY_H 8 | #define BITCOIN_PUBKEY_H 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include 16 | #include 17 | 18 | const unsigned int BIP32_EXTKEY_SIZE = 74; 19 | 20 | /** A reference to a CKey: the Hash160 of its serialized public key */ 21 | class CKeyID : public uint160 22 | { 23 | public: 24 | CKeyID() : uint160() {} 25 | explicit CKeyID(const uint160& in) : uint160(in) {} 26 | }; 27 | 28 | typedef uint256 ChainCode; 29 | 30 | /** An encapsulated public key. */ 31 | class CPubKey 32 | { 33 | public: 34 | /** 35 | * secp256k1: 36 | */ 37 | static constexpr unsigned int SIZE = 65; 38 | static constexpr unsigned int COMPRESSED_SIZE = 33; 39 | static constexpr unsigned int SIGNATURE_SIZE = 72; 40 | static constexpr unsigned int COMPACT_SIGNATURE_SIZE = 65; 41 | /** 42 | * see www.keylength.com 43 | * script supports up to 75 for single byte push 44 | */ 45 | static_assert( 46 | SIZE >= COMPRESSED_SIZE, 47 | "COMPRESSED_SIZE is larger than SIZE"); 48 | 49 | private: 50 | 51 | /** 52 | * Just store the serialized data. 53 | * Its length can very cheaply be computed from the first byte. 54 | */ 55 | unsigned char vch[SIZE]; 56 | 57 | //! Compute the length of a pubkey with a given first byte. 58 | unsigned int static GetLen(unsigned char chHeader) 59 | { 60 | if (chHeader == 2 || chHeader == 3) 61 | return COMPRESSED_SIZE; 62 | if (chHeader == 4 || chHeader == 6 || chHeader == 7) 63 | return SIZE; 64 | return 0; 65 | } 66 | 67 | //! Set this key data to be invalid 68 | void Invalidate() 69 | { 70 | vch[0] = 0xFF; 71 | } 72 | 73 | public: 74 | 75 | bool static ValidSize(const std::vector &vch) { 76 | return vch.size() > 0 && GetLen(vch[0]) == vch.size(); 77 | } 78 | 79 | //! Construct an invalid public key. 80 | CPubKey() 81 | { 82 | Invalidate(); 83 | } 84 | 85 | //! Initialize a public key using begin/end iterators to byte data. 86 | template 87 | void Set(const T pbegin, const T pend) 88 | { 89 | int len = pend == pbegin ? 0 : GetLen(pbegin[0]); 90 | if (len && len == (pend - pbegin)) 91 | memcpy(vch, (unsigned char*)&pbegin[0], len); 92 | else 93 | Invalidate(); 94 | } 95 | 96 | //! Construct a public key using begin/end iterators to byte data. 97 | template 98 | CPubKey(const T pbegin, const T pend) 99 | { 100 | Set(pbegin, pend); 101 | } 102 | 103 | //! Construct a public key from a byte vector. 104 | explicit CPubKey(const std::vector& _vch) 105 | { 106 | Set(_vch.begin(), _vch.end()); 107 | } 108 | 109 | //! Simple read-only vector-like interface to the pubkey data. 110 | unsigned int size() const { return GetLen(vch[0]); } 111 | const unsigned char* data() const { return vch; } 112 | const unsigned char* begin() const { return vch; } 113 | const unsigned char* end() const { return vch + size(); } 114 | const unsigned char& operator[](unsigned int pos) const { return vch[pos]; } 115 | 116 | //! Comparator implementation. 117 | friend bool operator==(const CPubKey& a, const CPubKey& b) 118 | { 119 | return a.vch[0] == b.vch[0] && 120 | memcmp(a.vch, b.vch, a.size()) == 0; 121 | } 122 | friend bool operator!=(const CPubKey& a, const CPubKey& b) 123 | { 124 | return !(a == b); 125 | } 126 | friend bool operator<(const CPubKey& a, const CPubKey& b) 127 | { 128 | return a.vch[0] < b.vch[0] || 129 | (a.vch[0] == b.vch[0] && memcmp(a.vch, b.vch, a.size()) < 0); 130 | } 131 | 132 | //! Implement serialization, as if this was a byte vector. 133 | template 134 | void Serialize(Stream& s) const 135 | { 136 | unsigned int len = size(); 137 | ::WriteCompactSize(s, len); 138 | s.write((char*)vch, len); 139 | } 140 | template 141 | void Unserialize(Stream& s) 142 | { 143 | unsigned int len = ::ReadCompactSize(s); 144 | if (len <= SIZE) { 145 | s.read((char*)vch, len); 146 | if (len != size()) { 147 | Invalidate(); 148 | } 149 | } else { 150 | // invalid pubkey, skip available data 151 | char dummy; 152 | while (len--) 153 | s.read(&dummy, 1); 154 | Invalidate(); 155 | } 156 | } 157 | 158 | //! Get the KeyID of this public key (hash of its serialization) 159 | CKeyID GetID() const 160 | { 161 | return CKeyID(Hash160(MakeSpan(vch).first(size()))); 162 | } 163 | 164 | //! Get the 256-bit hash of this public key. 165 | uint256 GetHash() const 166 | { 167 | return Hash(MakeSpan(vch).first(size())); 168 | } 169 | 170 | /* 171 | * Check syntactic correctness. 172 | * 173 | * Note that this is consensus critical as CheckECDSASignature() calls it! 174 | */ 175 | bool IsValid() const 176 | { 177 | return size() > 0; 178 | } 179 | 180 | //! fully validate whether this is a valid public key (more expensive than IsValid()) 181 | bool IsFullyValid() const; 182 | 183 | //! Check whether this is a compressed public key. 184 | bool IsCompressed() const 185 | { 186 | return size() == COMPRESSED_SIZE; 187 | } 188 | 189 | /** 190 | * Verify a DER signature (~72 bytes). 191 | * If this public key is not fully valid, the return value will be false. 192 | */ 193 | bool Verify(const uint256& hash, const std::vector& vchSig) const; 194 | 195 | /** 196 | * Check whether a signature is normalized (lower-S). 197 | */ 198 | static bool CheckLowS(const std::vector& vchSig); 199 | 200 | //! Recover a public key from a compact signature. 201 | bool RecoverCompact(const uint256& hash, const std::vector& vchSig); 202 | 203 | //! Turn this public key into an uncompressed public key. 204 | bool Decompress(); 205 | 206 | //! Derive BIP32 child pubkey. 207 | bool Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const; 208 | }; 209 | 210 | class XOnlyPubKey 211 | { 212 | private: 213 | uint256 m_keydata; 214 | 215 | public: 216 | /** Construct an x-only pubkey from exactly 32 bytes. */ 217 | XOnlyPubKey(Span bytes); 218 | 219 | /** Verify a Schnorr signature against this public key. 220 | * 221 | * sigbytes must be exactly 64 bytes. 222 | */ 223 | bool VerifySchnorr(const uint256& msg, Span sigbytes) const; 224 | bool CheckPayToContract(const XOnlyPubKey& base, const uint256& hash, bool parity) const; 225 | 226 | const unsigned char& operator[](int pos) const { return *(m_keydata.begin() + pos); } 227 | const unsigned char* data() const { return m_keydata.begin(); } 228 | size_t size() const { return m_keydata.size(); } 229 | }; 230 | 231 | struct CExtPubKey { 232 | unsigned char nDepth; 233 | unsigned char vchFingerprint[4]; 234 | unsigned int nChild; 235 | ChainCode chaincode; 236 | CPubKey pubkey; 237 | 238 | friend bool operator==(const CExtPubKey &a, const CExtPubKey &b) 239 | { 240 | return a.nDepth == b.nDepth && 241 | memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], sizeof(vchFingerprint)) == 0 && 242 | a.nChild == b.nChild && 243 | a.chaincode == b.chaincode && 244 | a.pubkey == b.pubkey; 245 | } 246 | 247 | friend bool operator!=(const CExtPubKey &a, const CExtPubKey &b) 248 | { 249 | return !(a == b); 250 | } 251 | 252 | void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const; 253 | void Decode(const unsigned char code[BIP32_EXTKEY_SIZE]); 254 | bool Derive(CExtPubKey& out, unsigned int nChild) const; 255 | }; 256 | 257 | /** Users of this module must hold an ECCVerifyHandle. The constructor and 258 | * destructor of these are not allowed to run in parallel, though. */ 259 | class ECCVerifyHandle 260 | { 261 | static int refcount; 262 | 263 | public: 264 | ECCVerifyHandle(); 265 | ~ECCVerifyHandle(); 266 | }; 267 | 268 | #endif // BITCOIN_PUBKEY_H 269 | -------------------------------------------------------------------------------- /src/clone/crypto/sha1.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2014-2019 The Bitcoin Core developers 2 | // Distributed under the MIT software license, see the accompanying 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 | 5 | #include 6 | 7 | #include 8 | 9 | #include 10 | 11 | // Internal implementation code. 12 | namespace 13 | { 14 | /// Internal SHA-1 implementation. 15 | namespace sha1 16 | { 17 | /** One round of SHA-1. */ 18 | void inline Round(uint32_t a, uint32_t& b, uint32_t c, uint32_t d, uint32_t& e, uint32_t f, uint32_t k, uint32_t w) 19 | { 20 | e += ((a << 5) | (a >> 27)) + f + k + w; 21 | b = (b << 30) | (b >> 2); 22 | } 23 | 24 | uint32_t inline f1(uint32_t b, uint32_t c, uint32_t d) { return d ^ (b & (c ^ d)); } 25 | uint32_t inline f2(uint32_t b, uint32_t c, uint32_t d) { return b ^ c ^ d; } 26 | uint32_t inline f3(uint32_t b, uint32_t c, uint32_t d) { return (b & c) | (d & (b | c)); } 27 | 28 | uint32_t inline left(uint32_t x) { return (x << 1) | (x >> 31); } 29 | 30 | /** Initialize SHA-1 state. */ 31 | void inline Initialize(uint32_t* s) 32 | { 33 | s[0] = 0x67452301ul; 34 | s[1] = 0xEFCDAB89ul; 35 | s[2] = 0x98BADCFEul; 36 | s[3] = 0x10325476ul; 37 | s[4] = 0xC3D2E1F0ul; 38 | } 39 | 40 | const uint32_t k1 = 0x5A827999ul; 41 | const uint32_t k2 = 0x6ED9EBA1ul; 42 | const uint32_t k3 = 0x8F1BBCDCul; 43 | const uint32_t k4 = 0xCA62C1D6ul; 44 | 45 | /** Perform a SHA-1 transformation, processing a 64-byte chunk. */ 46 | void Transform(uint32_t* s, const unsigned char* chunk) 47 | { 48 | uint32_t a = s[0], b = s[1], c = s[2], d = s[3], e = s[4]; 49 | uint32_t w0, w1, w2, w3, w4, w5, w6, w7, w8, w9, w10, w11, w12, w13, w14, w15; 50 | 51 | Round(a, b, c, d, e, f1(b, c, d), k1, w0 = ReadBE32(chunk + 0)); 52 | Round(e, a, b, c, d, f1(a, b, c), k1, w1 = ReadBE32(chunk + 4)); 53 | Round(d, e, a, b, c, f1(e, a, b), k1, w2 = ReadBE32(chunk + 8)); 54 | Round(c, d, e, a, b, f1(d, e, a), k1, w3 = ReadBE32(chunk + 12)); 55 | Round(b, c, d, e, a, f1(c, d, e), k1, w4 = ReadBE32(chunk + 16)); 56 | Round(a, b, c, d, e, f1(b, c, d), k1, w5 = ReadBE32(chunk + 20)); 57 | Round(e, a, b, c, d, f1(a, b, c), k1, w6 = ReadBE32(chunk + 24)); 58 | Round(d, e, a, b, c, f1(e, a, b), k1, w7 = ReadBE32(chunk + 28)); 59 | Round(c, d, e, a, b, f1(d, e, a), k1, w8 = ReadBE32(chunk + 32)); 60 | Round(b, c, d, e, a, f1(c, d, e), k1, w9 = ReadBE32(chunk + 36)); 61 | Round(a, b, c, d, e, f1(b, c, d), k1, w10 = ReadBE32(chunk + 40)); 62 | Round(e, a, b, c, d, f1(a, b, c), k1, w11 = ReadBE32(chunk + 44)); 63 | Round(d, e, a, b, c, f1(e, a, b), k1, w12 = ReadBE32(chunk + 48)); 64 | Round(c, d, e, a, b, f1(d, e, a), k1, w13 = ReadBE32(chunk + 52)); 65 | Round(b, c, d, e, a, f1(c, d, e), k1, w14 = ReadBE32(chunk + 56)); 66 | Round(a, b, c, d, e, f1(b, c, d), k1, w15 = ReadBE32(chunk + 60)); 67 | 68 | Round(e, a, b, c, d, f1(a, b, c), k1, w0 = left(w0 ^ w13 ^ w8 ^ w2)); 69 | Round(d, e, a, b, c, f1(e, a, b), k1, w1 = left(w1 ^ w14 ^ w9 ^ w3)); 70 | Round(c, d, e, a, b, f1(d, e, a), k1, w2 = left(w2 ^ w15 ^ w10 ^ w4)); 71 | Round(b, c, d, e, a, f1(c, d, e), k1, w3 = left(w3 ^ w0 ^ w11 ^ w5)); 72 | Round(a, b, c, d, e, f2(b, c, d), k2, w4 = left(w4 ^ w1 ^ w12 ^ w6)); 73 | Round(e, a, b, c, d, f2(a, b, c), k2, w5 = left(w5 ^ w2 ^ w13 ^ w7)); 74 | Round(d, e, a, b, c, f2(e, a, b), k2, w6 = left(w6 ^ w3 ^ w14 ^ w8)); 75 | Round(c, d, e, a, b, f2(d, e, a), k2, w7 = left(w7 ^ w4 ^ w15 ^ w9)); 76 | Round(b, c, d, e, a, f2(c, d, e), k2, w8 = left(w8 ^ w5 ^ w0 ^ w10)); 77 | Round(a, b, c, d, e, f2(b, c, d), k2, w9 = left(w9 ^ w6 ^ w1 ^ w11)); 78 | Round(e, a, b, c, d, f2(a, b, c), k2, w10 = left(w10 ^ w7 ^ w2 ^ w12)); 79 | Round(d, e, a, b, c, f2(e, a, b), k2, w11 = left(w11 ^ w8 ^ w3 ^ w13)); 80 | Round(c, d, e, a, b, f2(d, e, a), k2, w12 = left(w12 ^ w9 ^ w4 ^ w14)); 81 | Round(b, c, d, e, a, f2(c, d, e), k2, w13 = left(w13 ^ w10 ^ w5 ^ w15)); 82 | Round(a, b, c, d, e, f2(b, c, d), k2, w14 = left(w14 ^ w11 ^ w6 ^ w0)); 83 | Round(e, a, b, c, d, f2(a, b, c), k2, w15 = left(w15 ^ w12 ^ w7 ^ w1)); 84 | 85 | Round(d, e, a, b, c, f2(e, a, b), k2, w0 = left(w0 ^ w13 ^ w8 ^ w2)); 86 | Round(c, d, e, a, b, f2(d, e, a), k2, w1 = left(w1 ^ w14 ^ w9 ^ w3)); 87 | Round(b, c, d, e, a, f2(c, d, e), k2, w2 = left(w2 ^ w15 ^ w10 ^ w4)); 88 | Round(a, b, c, d, e, f2(b, c, d), k2, w3 = left(w3 ^ w0 ^ w11 ^ w5)); 89 | Round(e, a, b, c, d, f2(a, b, c), k2, w4 = left(w4 ^ w1 ^ w12 ^ w6)); 90 | Round(d, e, a, b, c, f2(e, a, b), k2, w5 = left(w5 ^ w2 ^ w13 ^ w7)); 91 | Round(c, d, e, a, b, f2(d, e, a), k2, w6 = left(w6 ^ w3 ^ w14 ^ w8)); 92 | Round(b, c, d, e, a, f2(c, d, e), k2, w7 = left(w7 ^ w4 ^ w15 ^ w9)); 93 | Round(a, b, c, d, e, f3(b, c, d), k3, w8 = left(w8 ^ w5 ^ w0 ^ w10)); 94 | Round(e, a, b, c, d, f3(a, b, c), k3, w9 = left(w9 ^ w6 ^ w1 ^ w11)); 95 | Round(d, e, a, b, c, f3(e, a, b), k3, w10 = left(w10 ^ w7 ^ w2 ^ w12)); 96 | Round(c, d, e, a, b, f3(d, e, a), k3, w11 = left(w11 ^ w8 ^ w3 ^ w13)); 97 | Round(b, c, d, e, a, f3(c, d, e), k3, w12 = left(w12 ^ w9 ^ w4 ^ w14)); 98 | Round(a, b, c, d, e, f3(b, c, d), k3, w13 = left(w13 ^ w10 ^ w5 ^ w15)); 99 | Round(e, a, b, c, d, f3(a, b, c), k3, w14 = left(w14 ^ w11 ^ w6 ^ w0)); 100 | Round(d, e, a, b, c, f3(e, a, b), k3, w15 = left(w15 ^ w12 ^ w7 ^ w1)); 101 | 102 | Round(c, d, e, a, b, f3(d, e, a), k3, w0 = left(w0 ^ w13 ^ w8 ^ w2)); 103 | Round(b, c, d, e, a, f3(c, d, e), k3, w1 = left(w1 ^ w14 ^ w9 ^ w3)); 104 | Round(a, b, c, d, e, f3(b, c, d), k3, w2 = left(w2 ^ w15 ^ w10 ^ w4)); 105 | Round(e, a, b, c, d, f3(a, b, c), k3, w3 = left(w3 ^ w0 ^ w11 ^ w5)); 106 | Round(d, e, a, b, c, f3(e, a, b), k3, w4 = left(w4 ^ w1 ^ w12 ^ w6)); 107 | Round(c, d, e, a, b, f3(d, e, a), k3, w5 = left(w5 ^ w2 ^ w13 ^ w7)); 108 | Round(b, c, d, e, a, f3(c, d, e), k3, w6 = left(w6 ^ w3 ^ w14 ^ w8)); 109 | Round(a, b, c, d, e, f3(b, c, d), k3, w7 = left(w7 ^ w4 ^ w15 ^ w9)); 110 | Round(e, a, b, c, d, f3(a, b, c), k3, w8 = left(w8 ^ w5 ^ w0 ^ w10)); 111 | Round(d, e, a, b, c, f3(e, a, b), k3, w9 = left(w9 ^ w6 ^ w1 ^ w11)); 112 | Round(c, d, e, a, b, f3(d, e, a), k3, w10 = left(w10 ^ w7 ^ w2 ^ w12)); 113 | Round(b, c, d, e, a, f3(c, d, e), k3, w11 = left(w11 ^ w8 ^ w3 ^ w13)); 114 | Round(a, b, c, d, e, f2(b, c, d), k4, w12 = left(w12 ^ w9 ^ w4 ^ w14)); 115 | Round(e, a, b, c, d, f2(a, b, c), k4, w13 = left(w13 ^ w10 ^ w5 ^ w15)); 116 | Round(d, e, a, b, c, f2(e, a, b), k4, w14 = left(w14 ^ w11 ^ w6 ^ w0)); 117 | Round(c, d, e, a, b, f2(d, e, a), k4, w15 = left(w15 ^ w12 ^ w7 ^ w1)); 118 | 119 | Round(b, c, d, e, a, f2(c, d, e), k4, w0 = left(w0 ^ w13 ^ w8 ^ w2)); 120 | Round(a, b, c, d, e, f2(b, c, d), k4, w1 = left(w1 ^ w14 ^ w9 ^ w3)); 121 | Round(e, a, b, c, d, f2(a, b, c), k4, w2 = left(w2 ^ w15 ^ w10 ^ w4)); 122 | Round(d, e, a, b, c, f2(e, a, b), k4, w3 = left(w3 ^ w0 ^ w11 ^ w5)); 123 | Round(c, d, e, a, b, f2(d, e, a), k4, w4 = left(w4 ^ w1 ^ w12 ^ w6)); 124 | Round(b, c, d, e, a, f2(c, d, e), k4, w5 = left(w5 ^ w2 ^ w13 ^ w7)); 125 | Round(a, b, c, d, e, f2(b, c, d), k4, w6 = left(w6 ^ w3 ^ w14 ^ w8)); 126 | Round(e, a, b, c, d, f2(a, b, c), k4, w7 = left(w7 ^ w4 ^ w15 ^ w9)); 127 | Round(d, e, a, b, c, f2(e, a, b), k4, w8 = left(w8 ^ w5 ^ w0 ^ w10)); 128 | Round(c, d, e, a, b, f2(d, e, a), k4, w9 = left(w9 ^ w6 ^ w1 ^ w11)); 129 | Round(b, c, d, e, a, f2(c, d, e), k4, w10 = left(w10 ^ w7 ^ w2 ^ w12)); 130 | Round(a, b, c, d, e, f2(b, c, d), k4, w11 = left(w11 ^ w8 ^ w3 ^ w13)); 131 | Round(e, a, b, c, d, f2(a, b, c), k4, w12 = left(w12 ^ w9 ^ w4 ^ w14)); 132 | Round(d, e, a, b, c, f2(e, a, b), k4, left(w13 ^ w10 ^ w5 ^ w15)); 133 | Round(c, d, e, a, b, f2(d, e, a), k4, left(w14 ^ w11 ^ w6 ^ w0)); 134 | Round(b, c, d, e, a, f2(c, d, e), k4, left(w15 ^ w12 ^ w7 ^ w1)); 135 | 136 | s[0] += a; 137 | s[1] += b; 138 | s[2] += c; 139 | s[3] += d; 140 | s[4] += e; 141 | } 142 | 143 | } // namespace sha1 144 | 145 | } // namespace 146 | 147 | ////// SHA1 148 | 149 | CSHA1::CSHA1() : bytes(0) 150 | { 151 | sha1::Initialize(s); 152 | } 153 | 154 | CSHA1& CSHA1::Write(const unsigned char* data, size_t len) 155 | { 156 | const unsigned char* end = data + len; 157 | size_t bufsize = bytes % 64; 158 | if (bufsize && bufsize + len >= 64) { 159 | // Fill the buffer, and process it. 160 | memcpy(buf + bufsize, data, 64 - bufsize); 161 | bytes += 64 - bufsize; 162 | data += 64 - bufsize; 163 | sha1::Transform(s, buf); 164 | bufsize = 0; 165 | } 166 | while (end - data >= 64) { 167 | // Process full chunks directly from the source. 168 | sha1::Transform(s, data); 169 | bytes += 64; 170 | data += 64; 171 | } 172 | if (end > data) { 173 | // Fill the buffer with what remains. 174 | memcpy(buf + bufsize, data, end - data); 175 | bytes += end - data; 176 | } 177 | return *this; 178 | } 179 | 180 | void CSHA1::Finalize(unsigned char hash[OUTPUT_SIZE]) 181 | { 182 | static const unsigned char pad[64] = {0x80}; 183 | unsigned char sizedesc[8]; 184 | WriteBE64(sizedesc, bytes << 3); 185 | Write(pad, 1 + ((119 - (bytes % 64)) % 64)); 186 | Write(sizedesc, 8); 187 | WriteBE32(hash, s[0]); 188 | WriteBE32(hash + 4, s[1]); 189 | WriteBE32(hash + 8, s[2]); 190 | WriteBE32(hash + 12, s[3]); 191 | WriteBE32(hash + 16, s[4]); 192 | } 193 | 194 | CSHA1& CSHA1::Reset() 195 | { 196 | bytes = 0; 197 | sha1::Initialize(s); 198 | return *this; 199 | } 200 | -------------------------------------------------------------------------------- /include/bitcoin/consensus/export.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2011-2023 libbitcoin developers (see AUTHORS) 3 | * 4 | * This file is part of libbitcoin. 5 | * 6 | * This program is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public License 17 | * along with this program. If not, see . 18 | */ 19 | #ifndef LIBBITCOIN_CONSENSUS_EXPORT_HPP 20 | #define LIBBITCOIN_CONSENSUS_EXPORT_HPP 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | namespace libbitcoin { 29 | namespace consensus { 30 | 31 | /** 32 | * Result values from calling verify_script. 33 | */ 34 | typedef enum verify_result 35 | { 36 | // Logical result 37 | verify_result_eval_false = 0, 38 | verify_result_eval_true, 39 | 40 | // Max size errors 41 | verify_result_script_size, 42 | verify_result_push_size, 43 | verify_result_op_count, 44 | verify_result_stack_size, 45 | verify_result_sig_count, 46 | verify_result_pubkey_count, 47 | 48 | // Failed verify operations 49 | verify_result_verify, 50 | verify_result_equalverify, 51 | verify_result_checkmultisigverify, 52 | verify_result_checksigverify, 53 | verify_result_numequalverify, 54 | 55 | // Logical/Format/Canonical errors 56 | verify_result_bad_opcode, 57 | verify_result_disabled_opcode, 58 | verify_result_invalid_stack_operation, 59 | verify_result_invalid_altstack_operation, 60 | verify_result_unbalanced_conditional, 61 | 62 | // BIP62 errors 63 | verify_result_sig_hashtype, 64 | verify_result_sig_der, 65 | verify_result_minimaldata, 66 | verify_result_sig_pushonly, 67 | verify_result_sig_high_s, 68 | verify_result_sig_nulldummy, 69 | verify_result_pubkeytype, 70 | verify_result_cleanstack, 71 | verify_result_minimalif, 72 | verify_result_sig_nullfail, 73 | 74 | // Softfork safeness 75 | verify_result_discourage_upgradable_nops, 76 | verify_result_discourage_upgradable_witness_program, 77 | 78 | // Other 79 | verify_result_op_return, 80 | verify_result_unknown_error, 81 | 82 | // Segregated witness 83 | verify_result_witness_program_wrong_length, 84 | verify_result_witness_program_empty_witness, 85 | verify_result_witness_program_mismatch, 86 | verify_result_witness_malleated, 87 | verify_result_witness_malleated_p2sh, 88 | verify_result_witness_unexpected, 89 | verify_result_witness_pubkeytype, 90 | 91 | // augmention codes for tx deserialization 92 | verify_result_tx_invalid, 93 | verify_result_tx_size_invalid, 94 | verify_result_tx_input_invalid, 95 | 96 | // BIP65/BIP112 (shared codes) 97 | verify_result_negative_locktime, 98 | verify_result_unsatisfied_locktime, 99 | 100 | // Deserialization errors 101 | verify_value_overflow, 102 | verify_evaluation_throws 103 | } verify_result; 104 | 105 | /** 106 | * Flags to use when calling verify_script. 107 | */ 108 | typedef enum : uint32_t 109 | { 110 | /** 111 | * Set no flags. 112 | */ 113 | verify_flags_none = 0, 114 | 115 | /** 116 | * Evaluate P2SH subscripts (softfork safe, BIP16). 117 | */ 118 | verify_flags_p2sh = (1U << 0), 119 | 120 | /** 121 | * Passing a non-strict-DER signature or one with undefined hashtype to a 122 | * checksig operation causes script failure. Evaluating a pubkey that is 123 | * not (0x04 + 64 bytes) or (0x02 or 0x03 + 32 bytes) by checksig causes 124 | * script failure. (softfork safe, but not used or intended as a consensus 125 | * rule). 126 | */ 127 | verify_flags_strictenc = (1U << 1), 128 | 129 | /** 130 | * Passing a non-strict-DER signature to a checksig operation causes script 131 | * failure (softfork safe, BIP62 rule 1, BIP66). 132 | */ 133 | verify_flags_dersig = (1U << 2), 134 | 135 | /** 136 | * Passing a non-strict-DER signature or one with S > order/2 to a checksig 137 | * operation causes script failure 138 | * (softfork safe, BIP62 rule 5). 139 | */ 140 | verify_flags_low_s = (1U << 3), 141 | 142 | /** 143 | * verify dummy stack item consumed by CHECKMULTISIG is of zero-length 144 | * (softfork safe, BIP62 rule 7, BIP147). 145 | */ 146 | verify_flags_nulldummy = (1U << 4), 147 | 148 | /** 149 | * Using a non-push operator in the scriptSig causes script failure 150 | * (softfork safe, BIP62 rule 2). 151 | */ 152 | verify_flags_sigpushonly = (1U << 5), 153 | 154 | /** 155 | * Require minimal encodings for all push operations (OP_0... OP_16, 156 | * OP_1NEGATE where possible, direct pushes up to 75 bytes, OP_PUSHDATA 157 | * up to 255 bytes, OP_PUSHDATA2 for anything larger). Evaluating any other 158 | * push causes the script to fail (BIP62 rule 3). In addition, whenever a 159 | * stack element is interpreted as a number, it must be of minimal length 160 | * (BIP62 rule 4).(softfork safe) 161 | */ 162 | verify_flags_minimaldata = (1U << 6), 163 | 164 | /** 165 | * Discourage use of NOPs reserved for upgrades (NOP1,3-10) 166 | * Provided so that nodes can avoid accepting or mining transactions 167 | * containing executed NOP's whose meaning may change after a soft-fork, 168 | * thus rendering the script invalid; with this flag set executing 169 | * discouraged NOPs fails the script. This verification flag will never be 170 | * a mandatory flag applied to scripts in a block. NOPs that are not 171 | * executed, e.g. within an unexecuted IF ENDIF block, are *not* rejected. 172 | */ 173 | verify_flags_discourage_upgradable_nops = (1U << 7), 174 | 175 | /** 176 | * Require that only a single stack element remains after evaluation. This 177 | * changes the success criterion from "At least one stack element must 178 | * remain, and when interpreted as a boolean, it must be true" to "Exactly 179 | * one stack element must remain, and when interpreted as a boolean, it 180 | * must be true". (softfork safe, BIP62 rule 6) 181 | * Note: verify_flags_cleanstack must be used with verify_flags_p2sh. 182 | */ 183 | verify_flags_cleanstack = (1U << 8), 184 | 185 | /** 186 | * Verify CHECKLOCKTIMEVERIFY, see BIP65 for details. 187 | */ 188 | verify_flags_checklocktimeverify = (1U << 9), 189 | 190 | /** 191 | * Verify CHECKSEQUENCEVERIFY, see BIP112 for details. 192 | */ 193 | verify_flags_checksequenceverify = (1U << 10), 194 | 195 | /** 196 | * SCRIPT_VERIFY_WITNESS (bip141). 197 | */ 198 | verify_flags_witness = (1U << 11), 199 | 200 | /** 201 | * SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM (bip141 policy). 202 | */ 203 | verify_flags_discourage_upgradable_witness_program = (1U << 12), 204 | 205 | /** 206 | * SCRIPT_VERIFY_MINIMALIF (bip141 p2wsh policy). 207 | */ 208 | verify_flags_minimal_if = (1U << 13), 209 | 210 | /** 211 | * SCRIPT_VERIFY_NULLFAIL (bip141 global policy, bip146 soft fork). 212 | */ 213 | verify_flags_null_fail = (1U << 14), 214 | 215 | /** 216 | * SCRIPT_VERIFY_WITNESS_PUBKEYTYPE (bip141/bip143 p2wsh/p2wpkh policy). 217 | */ 218 | verify_flags_witness_public_key_compressed = (1U << 15), 219 | 220 | /** 221 | * Set all flags. 222 | */ 223 | verify_flags_all = 0x7fff 224 | } verify_flags; 225 | 226 | typedef std::vector chunk; 227 | typedef std::vector stack; 228 | typedef struct output 229 | { 230 | chunk script; 231 | uint64_t value; 232 | } output; 233 | typedef std::vector outputs; 234 | 235 | // TODO: this is ready for test. 236 | #ifdef UNTESTED 237 | /** 238 | * Verify that all transaction inputs correctly spend the corresponding 239 | * previous outputs, considering any additional constraints specified by flags. 240 | * This avoids repeated deserialization of the transaction for each input. 241 | * @param[in] transaction The transaction with the input scripts to verify. 242 | * @param[in] prevouts The public key scripts to verify against (in order). 243 | * @param[in] flags Verification constraint flags. 244 | * @returns A script verification result code. 245 | */ 246 | BCK_API verify_result verify_script(const chunk& transaction, 247 | const outputs& prevouts, uint32_t flags) noexcept; 248 | #endif 249 | 250 | /** 251 | * Verify that the transaction input correctly spends the previous output, 252 | * considering any additional constraints specified by flags. 253 | * @param[in] transaction The transaction with the input script to verify. 254 | * @param[in] prevout The public key script to verify against. 255 | * @param[in] input_index The zero-based index of the transaction input. 256 | * @param[in] flags Verification constraint flags. 257 | * @returns A script verification result code. 258 | */ 259 | BCK_API verify_result verify_script(const chunk& transaction, 260 | const output& prevout, uint32_t input_index, uint32_t flags) noexcept; 261 | 262 | /** 263 | * Verify that the unsigned input correctly spends the previous output, 264 | * considering any additional constraints specified by flags. This is useful 265 | * for evaluating script execution without a transaction (checksig excluded). 266 | * @param[in] prevout The public key script to verify against. 267 | * @param[in] input_script The (unsigned) script sig to verify. 268 | * @param[in] witness The input's witness stack to verify (or empty). 269 | * @param[in] flags Verification constraint flags. 270 | * @returns A script verification result code. 271 | */ 272 | BCK_API verify_result verify_unsigned_script(const output& prevout, 273 | const chunk& input_script, const stack& witness, uint32_t flags) noexcept; 274 | 275 | } // namespace consensus 276 | } // namespace libbitcoin 277 | 278 | #endif 279 | -------------------------------------------------------------------------------- /src/clone/util/strencodings.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2009-2010 Satoshi Nakamoto 2 | // Copyright (c) 2009-2020 The Bitcoin Core developers 3 | // Distributed under the MIT software license, see the accompanying 4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 | 6 | /** 7 | * Utilities for converting data from/to strings. 8 | */ 9 | #ifndef BITCOIN_UTIL_STRENCODINGS_H 10 | #define BITCOIN_UTIL_STRENCODINGS_H 11 | 12 | #include 13 | #include 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | #define ARRAYLEN(array) (sizeof(array)/sizeof((array)[0])) 21 | 22 | /** Used by SanitizeString() */ 23 | enum SafeChars 24 | { 25 | SAFE_CHARS_DEFAULT, //!< The full set of allowed chars 26 | SAFE_CHARS_UA_COMMENT, //!< BIP-0014 subset 27 | SAFE_CHARS_FILENAME, //!< Chars allowed in filenames 28 | SAFE_CHARS_URI, //!< Chars allowed in URIs (RFC 3986) 29 | }; 30 | 31 | /** 32 | * Remove unsafe chars. Safe chars chosen to allow simple messages/URLs/email 33 | * addresses, but avoid anything even possibly remotely dangerous like & or > 34 | * @param[in] str The string to sanitize 35 | * @param[in] rule The set of safe chars to choose (default: least restrictive) 36 | * @return A new string without unsafe chars 37 | */ 38 | std::string SanitizeString(const std::string& str, int rule = SAFE_CHARS_DEFAULT); 39 | std::vector ParseHex(const char* psz); 40 | std::vector ParseHex(const std::string& str); 41 | signed char HexDigit(char c); 42 | /* Returns true if each character in str is a hex character, and has an even 43 | * number of hex digits.*/ 44 | bool IsHex(const std::string& str); 45 | /** 46 | * Return true if the string is a hex number, optionally prefixed with "0x" 47 | */ 48 | bool IsHexNumber(const std::string& str); 49 | std::vector DecodeBase64(const char* p, bool* pf_invalid = nullptr); 50 | std::string DecodeBase64(const std::string& str, bool* pf_invalid = nullptr); 51 | std::string EncodeBase64(Span input); 52 | std::string EncodeBase64(const std::string& str); 53 | std::vector DecodeBase32(const char* p, bool* pf_invalid = nullptr); 54 | std::string DecodeBase32(const std::string& str, bool* pf_invalid = nullptr); 55 | 56 | /** 57 | * Base32 encode. 58 | * If `pad` is true, then the output will be padded with '=' so that its length 59 | * is a multiple of 8. 60 | */ 61 | std::string EncodeBase32(Span input, bool pad = true); 62 | 63 | /** 64 | * Base32 encode. 65 | * If `pad` is true, then the output will be padded with '=' so that its length 66 | * is a multiple of 8. 67 | */ 68 | std::string EncodeBase32(const std::string& str, bool pad = true); 69 | 70 | void SplitHostPort(std::string in, int& portOut, std::string& hostOut); 71 | int64_t atoi64(const std::string& str); 72 | int atoi(const std::string& str); 73 | 74 | /** 75 | * Tests if the given character is a decimal digit. 76 | * @param[in] c character to test 77 | * @return true if the argument is a decimal digit; otherwise false. 78 | */ 79 | constexpr bool IsDigit(char c) 80 | { 81 | return c >= '0' && c <= '9'; 82 | } 83 | 84 | /** 85 | * Tests if the given character is a whitespace character. The whitespace characters 86 | * are: space, form-feed ('\f'), newline ('\n'), carriage return ('\r'), horizontal 87 | * tab ('\t'), and vertical tab ('\v'). 88 | * 89 | * This function is locale independent. Under the C locale this function gives the 90 | * same result as std::isspace. 91 | * 92 | * @param[in] c character to test 93 | * @return true if the argument is a whitespace character; otherwise false 94 | */ 95 | constexpr inline bool IsSpace(char c) noexcept { 96 | return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v'; 97 | } 98 | 99 | /** 100 | * Convert string to signed 32-bit integer with strict parse error feedback. 101 | * @returns true if the entire string could be parsed as valid integer, 102 | * false if not the entire string could be parsed or when overflow or underflow occurred. 103 | */ 104 | NODISCARD bool ParseInt32(const std::string& str, int32_t *out); 105 | 106 | /** 107 | * Convert string to signed 64-bit integer with strict parse error feedback. 108 | * @returns true if the entire string could be parsed as valid integer, 109 | * false if not the entire string could be parsed or when overflow or underflow occurred. 110 | */ 111 | NODISCARD bool ParseInt64(const std::string& str, int64_t *out); 112 | 113 | /** 114 | * Convert decimal string to unsigned 8-bit integer with strict parse error feedback. 115 | * @returns true if the entire string could be parsed as valid integer, 116 | * false if not the entire string could be parsed or when overflow or underflow occurred. 117 | */ 118 | NODISCARD bool ParseUInt8(const std::string& str, uint8_t *out); 119 | 120 | /** 121 | * Convert decimal string to unsigned 32-bit integer with strict parse error feedback. 122 | * @returns true if the entire string could be parsed as valid integer, 123 | * false if not the entire string could be parsed or when overflow or underflow occurred. 124 | */ 125 | NODISCARD bool ParseUInt32(const std::string& str, uint32_t *out); 126 | 127 | /** 128 | * Convert decimal string to unsigned 64-bit integer with strict parse error feedback. 129 | * @returns true if the entire string could be parsed as valid integer, 130 | * false if not the entire string could be parsed or when overflow or underflow occurred. 131 | */ 132 | NODISCARD bool ParseUInt64(const std::string& str, uint64_t *out); 133 | 134 | /** 135 | * Convert string to double with strict parse error feedback. 136 | * @returns true if the entire string could be parsed as valid double, 137 | * false if not the entire string could be parsed or when overflow or underflow occurred. 138 | */ 139 | NODISCARD bool ParseDouble(const std::string& str, double *out); 140 | 141 | /** 142 | * Convert a span of bytes to a lower-case hexadecimal string. 143 | */ 144 | std::string HexStr(const Span s); 145 | inline std::string HexStr(const Span s) { return HexStr(MakeUCharSpan(s)); } 146 | 147 | /** 148 | * Format a paragraph of text to a fixed width, adding spaces for 149 | * indentation to any added line. 150 | */ 151 | std::string FormatParagraph(const std::string& in, size_t width = 79, size_t indent = 0); 152 | 153 | /** 154 | * Timing-attack-resistant comparison. 155 | * Takes time proportional to length 156 | * of first argument. 157 | */ 158 | template 159 | bool TimingResistantEqual(const T& a, const T& b) 160 | { 161 | if (b.size() == 0) return a.size() == 0; 162 | size_t accumulator = a.size() ^ b.size(); 163 | for (size_t i = 0; i < a.size(); i++) 164 | accumulator |= a[i] ^ b[i%b.size()]; 165 | return accumulator == 0; 166 | } 167 | 168 | /** Parse number as fixed point according to JSON number syntax. 169 | * See http://json.org/number.gif 170 | * @returns true on success, false on error. 171 | * @note The result must be in the range (-10^18,10^18), otherwise an overflow error will trigger. 172 | */ 173 | NODISCARD bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out); 174 | 175 | /** Convert from one power-of-2 number base to another. */ 176 | template 177 | bool ConvertBits(const O& outfn, I it, I end) { 178 | size_t acc = 0; 179 | size_t bits = 0; 180 | constexpr size_t maxv = (1 << tobits) - 1; 181 | constexpr size_t max_acc = (1 << (frombits + tobits - 1)) - 1; 182 | while (it != end) { 183 | acc = ((acc << frombits) | *it) & max_acc; 184 | bits += frombits; 185 | while (bits >= tobits) { 186 | bits -= tobits; 187 | outfn((acc >> bits) & maxv); 188 | } 189 | ++it; 190 | } 191 | if (pad) { 192 | if (bits) outfn((acc << (tobits - bits)) & maxv); 193 | } else if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) { 194 | return false; 195 | } 196 | return true; 197 | } 198 | 199 | /** 200 | * Converts the given character to its lowercase equivalent. 201 | * This function is locale independent. It only converts uppercase 202 | * characters in the standard 7-bit ASCII range. 203 | * This is a feature, not a limitation. 204 | * 205 | * @param[in] c the character to convert to lowercase. 206 | * @return the lowercase equivalent of c; or the argument 207 | * if no conversion is possible. 208 | */ 209 | constexpr char ToLower(char c) 210 | { 211 | return (c >= 'A' && c <= 'Z' ? (c - 'A') + 'a' : c); 212 | } 213 | 214 | /** 215 | * Returns the lowercase equivalent of the given string. 216 | * This function is locale independent. It only converts uppercase 217 | * characters in the standard 7-bit ASCII range. 218 | * This is a feature, not a limitation. 219 | * 220 | * @param[in] str the string to convert to lowercase. 221 | * @returns lowercased equivalent of str 222 | */ 223 | std::string ToLower(const std::string& str); 224 | 225 | /** 226 | * Converts the given character to its uppercase equivalent. 227 | * This function is locale independent. It only converts lowercase 228 | * characters in the standard 7-bit ASCII range. 229 | * This is a feature, not a limitation. 230 | * 231 | * @param[in] c the character to convert to uppercase. 232 | * @return the uppercase equivalent of c; or the argument 233 | * if no conversion is possible. 234 | */ 235 | constexpr char ToUpper(char c) 236 | { 237 | return (c >= 'a' && c <= 'z' ? (c - 'a') + 'A' : c); 238 | } 239 | 240 | /** 241 | * Returns the uppercase equivalent of the given string. 242 | * This function is locale independent. It only converts lowercase 243 | * characters in the standard 7-bit ASCII range. 244 | * This is a feature, not a limitation. 245 | * 246 | * @param[in] str the string to convert to uppercase. 247 | * @returns UPPERCASED EQUIVALENT OF str 248 | */ 249 | std::string ToUpper(const std::string& str); 250 | 251 | /** 252 | * Capitalizes the first character of the given string. 253 | * This function is locale independent. It only converts lowercase 254 | * characters in the standard 7-bit ASCII range. 255 | * This is a feature, not a limitation. 256 | * 257 | * @param[in] str the string to capitalize. 258 | * @returns string with the first letter capitalized. 259 | */ 260 | std::string Capitalize(std::string str); 261 | 262 | #endif // BITCOIN_UTIL_STRENCODINGS_H 263 | -------------------------------------------------------------------------------- /src/clone/span.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018-2020 The Bitcoin Core developers 2 | // Distributed under the MIT software license, see the accompanying 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 | 5 | #ifndef BITCOIN_SPAN_H 6 | #define BITCOIN_SPAN_H 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #ifdef DEBUG 14 | #define CONSTEXPR_IF_NOT_DEBUG 15 | #define ASSERT_IF_DEBUG(x) assert((x)) 16 | #else 17 | #define CONSTEXPR_IF_NOT_DEBUG constexpr 18 | #define ASSERT_IF_DEBUG(x) 19 | #endif 20 | 21 | /** A Span is an object that can refer to a contiguous sequence of objects. 22 | * 23 | * It implements a subset of C++20's std::span. 24 | * 25 | * Things to be aware of when writing code that deals with Spans: 26 | * 27 | * - Similar to references themselves, Spans are subject to reference lifetime 28 | * issues. The user is responsible for making sure the objects pointed to by 29 | * a Span live as long as the Span is used. For example: 30 | * 31 | * std::vector vec{1,2,3,4}; 32 | * Span sp(vec); 33 | * vec.push_back(5); 34 | * printf("%i\n", sp.front()); // UB! 35 | * 36 | * may exhibit undefined behavior, as increasing the size of a vector may 37 | * invalidate references. 38 | * 39 | * - One particular pitfall is that Spans can be constructed from temporaries, 40 | * but this is unsafe when the Span is stored in a variable, outliving the 41 | * temporary. For example, this will compile, but exhibits undefined behavior: 42 | * 43 | * Span sp(std::vector{1, 2, 3}); 44 | * printf("%i\n", sp.front()); // UB! 45 | * 46 | * The lifetime of the vector ends when the statement it is created in ends. 47 | * Thus the Span is left with a dangling reference, and using it is undefined. 48 | * 49 | * - Due to Span's automatic creation from range-like objects (arrays, and data 50 | * types that expose a data() and size() member function), functions that 51 | * accept a Span as input parameter can be called with any compatible 52 | * range-like object. For example, this works: 53 | * 54 | * void Foo(Span arg); 55 | * 56 | * Foo(std::vector{1, 2, 3}); // Works 57 | * 58 | * This is very useful in cases where a function truly does not care about the 59 | * container, and only about having exactly a range of elements. However it 60 | * may also be surprising to see automatic conversions in this case. 61 | * 62 | * When a function accepts a Span with a mutable element type, it will not 63 | * accept temporaries; only variables or other references. For example: 64 | * 65 | * void FooMut(Span arg); 66 | * 67 | * FooMut(std::vector{1, 2, 3}); // Does not compile 68 | * std::vector baz{1, 2, 3}; 69 | * FooMut(baz); // Works 70 | * 71 | * This is similar to how functions that take (non-const) lvalue references 72 | * as input cannot accept temporaries. This does not work either: 73 | * 74 | * void FooVec(std::vector& arg); 75 | * FooVec(std::vector{1, 2, 3}); // Does not compile 76 | * 77 | * The idea is that if a function accepts a mutable reference, a meaningful 78 | * result will be present in that variable after the call. Passing a temporary 79 | * is useless in that context. 80 | */ 81 | template 82 | class Span 83 | { 84 | C* m_data; 85 | std::size_t m_size; 86 | 87 | public: 88 | constexpr Span() noexcept : m_data(nullptr), m_size(0) {} 89 | 90 | /** Construct a span from a begin pointer and a size. 91 | * 92 | * This implements a subset of the iterator-based std::span constructor in C++20, 93 | * which is hard to implement without std::address_of. 94 | */ 95 | template ::value, int>::type = 0> 96 | constexpr Span(T* begin, std::size_t size) noexcept : m_data(begin), m_size(size) {} 97 | 98 | /** Construct a span from a begin and end pointer. 99 | * 100 | * This implements a subset of the iterator-based std::span constructor in C++20, 101 | * which is hard to implement without std::address_of. 102 | */ 103 | template ::value, int>::type = 0> 104 | CONSTEXPR_IF_NOT_DEBUG Span(T* begin, T* end) noexcept : m_data(begin), m_size(end - begin) 105 | { 106 | ASSERT_IF_DEBUG(end >= begin); 107 | } 108 | 109 | /** Implicit conversion of spans between compatible types. 110 | * 111 | * Specifically, if a pointer to an array of type O can be implicitly converted to a pointer to an array of type 112 | * C, then permit implicit conversion of Span to Span. This matches the behavior of the corresponding 113 | * C++20 std::span constructor. 114 | * 115 | * For example this means that a Span can be converted into a Span. 116 | */ 117 | template ::value, int>::type = 0> 118 | constexpr Span(const Span& other) noexcept : m_data(other.m_data), m_size(other.m_size) {} 119 | 120 | /** Default copy constructor. */ 121 | constexpr Span(const Span&) noexcept = default; 122 | 123 | /** Default assignment operator. */ 124 | Span& operator=(const Span& other) noexcept = default; 125 | 126 | /** Construct a Span from an array. This matches the corresponding C++20 std::span constructor. */ 127 | template 128 | constexpr Span(C (&a)[N]) noexcept : m_data(a), m_size(N) {} 129 | 130 | /** Construct a Span for objects with .data() and .size() (std::string, std::array, std::vector, ...). 131 | * 132 | * This implements a subset of the functionality provided by the C++20 std::span range-based constructor. 133 | * 134 | * To prevent surprises, only Spans for constant value types are supported when passing in temporaries. 135 | * Note that this restriction does not exist when converting arrays or other Spans (see above). 136 | */ 137 | template ::value || std::is_lvalue_reference::value) && std::is_convertible().data())>::type (*)[], C (*)[]>::value && std::is_convertible().size()), std::size_t>::value, int>::type = 0> 138 | constexpr Span(V&& v) noexcept : m_data(v.data()), m_size(v.size()) {} 139 | 140 | constexpr C* data() const noexcept { return m_data; } 141 | constexpr C* begin() const noexcept { return m_data; } 142 | constexpr C* end() const noexcept { return m_data + m_size; } 143 | CONSTEXPR_IF_NOT_DEBUG C& front() const noexcept 144 | { 145 | ASSERT_IF_DEBUG(size() > 0); 146 | return m_data[0]; 147 | } 148 | CONSTEXPR_IF_NOT_DEBUG C& back() const noexcept 149 | { 150 | ASSERT_IF_DEBUG(size() > 0); 151 | return m_data[m_size - 1]; 152 | } 153 | constexpr std::size_t size() const noexcept { return m_size; } 154 | constexpr bool empty() const noexcept { return size() == 0; } 155 | CONSTEXPR_IF_NOT_DEBUG C& operator[](std::size_t pos) const noexcept 156 | { 157 | ASSERT_IF_DEBUG(size() > pos); 158 | return m_data[pos]; 159 | } 160 | CONSTEXPR_IF_NOT_DEBUG Span subspan(std::size_t offset) const noexcept 161 | { 162 | ASSERT_IF_DEBUG(size() >= offset); 163 | return Span(m_data + offset, m_size - offset); 164 | } 165 | CONSTEXPR_IF_NOT_DEBUG Span subspan(std::size_t offset, std::size_t count) const noexcept 166 | { 167 | ASSERT_IF_DEBUG(size() >= offset + count); 168 | return Span(m_data + offset, count); 169 | } 170 | CONSTEXPR_IF_NOT_DEBUG Span first(std::size_t count) const noexcept 171 | { 172 | ASSERT_IF_DEBUG(size() >= count); 173 | return Span(m_data, count); 174 | } 175 | CONSTEXPR_IF_NOT_DEBUG Span last(std::size_t count) const noexcept 176 | { 177 | ASSERT_IF_DEBUG(size() >= count); 178 | return Span(m_data + m_size - count, count); 179 | } 180 | 181 | friend constexpr bool operator==(const Span& a, const Span& b) noexcept { return a.size() == b.size() && std::equal(a.begin(), a.end(), b.begin()); } 182 | friend constexpr bool operator!=(const Span& a, const Span& b) noexcept { return !(a == b); } 183 | friend constexpr bool operator<(const Span& a, const Span& b) noexcept { return std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end()); } 184 | friend constexpr bool operator<=(const Span& a, const Span& b) noexcept { return !(b < a); } 185 | friend constexpr bool operator>(const Span& a, const Span& b) noexcept { return (b < a); } 186 | friend constexpr bool operator>=(const Span& a, const Span& b) noexcept { return !(a < b); } 187 | 188 | template friend class Span; 189 | }; 190 | 191 | // MakeSpan helps constructing a Span of the right type automatically. 192 | /** MakeSpan for arrays: */ 193 | template Span constexpr MakeSpan(A (&a)[N]) { return Span(a, N); } 194 | /** MakeSpan for temporaries / rvalue references, only supporting const output. */ 195 | template constexpr auto MakeSpan(V&& v) -> typename std::enable_if::value, Span::type>>::type { return std::forward(v); } 196 | /** MakeSpan for (lvalue) references, supporting mutable output. */ 197 | template constexpr auto MakeSpan(V& v) -> Span::type> { return v; } 198 | 199 | /** Pop the last element off a span, and return a reference to that element. */ 200 | template 201 | T& SpanPopBack(Span& span) 202 | { 203 | size_t size = span.size(); 204 | ASSERT_IF_DEBUG(size > 0); 205 | T& back = span[size - 1]; 206 | span = Span(span.data(), size - 1); 207 | return back; 208 | } 209 | 210 | // Helper functions to safely cast to unsigned char pointers. 211 | inline unsigned char* UCharCast(char* c) { return (unsigned char*)c; } 212 | inline unsigned char* UCharCast(unsigned char* c) { return c; } 213 | inline const unsigned char* UCharCast(const char* c) { return (unsigned char*)c; } 214 | inline const unsigned char* UCharCast(const unsigned char* c) { return c; } 215 | 216 | // Helper function to safely convert a Span to a Span<[const] unsigned char>. 217 | template constexpr auto UCharSpanCast(Span s) -> Span::type> { return {UCharCast(s.data()), s.size()}; } 218 | 219 | /** Like MakeSpan, but for (const) unsigned char member types only. Only works for (un)signed char containers. */ 220 | template constexpr auto MakeUCharSpan(V&& v) -> decltype(UCharSpanCast(MakeSpan(std::forward(v)))) { return UCharSpanCast(MakeSpan(std::forward(v))); } 221 | 222 | #endif 223 | -------------------------------------------------------------------------------- /test/consensus__script_error_to_verify_result.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2011-2023 libbitcoin developers (see AUTHORS) 3 | * 4 | * This file is part of libbitcoin. 5 | * 6 | * This program is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public License 17 | * along with this program. If not, see . 18 | */ 19 | #include 20 | #include 21 | #include 22 | 23 | // These give us test accesss to unpublished symbols. 24 | #include "consensus/consensus.hpp" 25 | #include "script/script_error.h" 26 | 27 | using namespace libbitcoin::consensus; 28 | 29 | BOOST_AUTO_TEST_SUITE(consensus__script_error_to_verify_result) 30 | 31 | // Logical result 32 | 33 | BOOST_AUTO_TEST_CASE(consensus__script_error_to_verify_result__OK__eval_true) 34 | { 35 | BOOST_REQUIRE_EQUAL(script_error_to_verify_result(SCRIPT_ERR_OK), verify_result_eval_true); 36 | } 37 | 38 | BOOST_AUTO_TEST_CASE(consensus__script_error_to_verify_result__EVAL_FALSE__eval_false) 39 | { 40 | BOOST_REQUIRE_EQUAL(script_error_to_verify_result(SCRIPT_ERR_EVAL_FALSE), verify_result_eval_false); 41 | } 42 | 43 | // Max size errors. 44 | 45 | BOOST_AUTO_TEST_CASE(consensus__script_error_to_verify_result__SCRIPT_SIZE__script_size) 46 | { 47 | BOOST_REQUIRE_EQUAL(script_error_to_verify_result(SCRIPT_ERR_SCRIPT_SIZE), verify_result_script_size); 48 | } 49 | 50 | BOOST_AUTO_TEST_CASE(consensus__script_error_to_verify_result__PUSH_SIZE__push_size) 51 | { 52 | BOOST_REQUIRE_EQUAL(script_error_to_verify_result(SCRIPT_ERR_PUSH_SIZE), verify_result_push_size); 53 | } 54 | 55 | BOOST_AUTO_TEST_CASE(consensus__script_error_to_verify_result__OP_COUNT__op_count) 56 | { 57 | BOOST_REQUIRE_EQUAL(script_error_to_verify_result(SCRIPT_ERR_OP_COUNT), verify_result_op_count); 58 | } 59 | 60 | BOOST_AUTO_TEST_CASE(consensus__script_error_to_verify_result__STACK_SIZE__stack_size) 61 | { 62 | BOOST_REQUIRE_EQUAL(script_error_to_verify_result(SCRIPT_ERR_STACK_SIZE), verify_result_stack_size); 63 | } 64 | 65 | BOOST_AUTO_TEST_CASE(consensus__script_error_to_verify_result__SIG_COUNT__sig_count) 66 | { 67 | BOOST_REQUIRE_EQUAL(script_error_to_verify_result(SCRIPT_ERR_SIG_COUNT), verify_result_sig_count); 68 | } 69 | 70 | BOOST_AUTO_TEST_CASE(consensus__script_error_to_verify_result__PUBKEY_COUNT__pubkey_count) 71 | { 72 | BOOST_REQUIRE_EQUAL(script_error_to_verify_result(SCRIPT_ERR_PUBKEY_COUNT), verify_result_pubkey_count); 73 | } 74 | 75 | // Failed verify operations 76 | 77 | BOOST_AUTO_TEST_CASE(consensus__script_error_to_verify_result__VERIFY__verify) 78 | { 79 | BOOST_REQUIRE_EQUAL(script_error_to_verify_result(SCRIPT_ERR_VERIFY), verify_result_verify); 80 | } 81 | 82 | BOOST_AUTO_TEST_CASE(consensus__script_error_to_verify_result__EQUALVERIFY__equalverify) 83 | { 84 | BOOST_REQUIRE_EQUAL(script_error_to_verify_result(SCRIPT_ERR_EQUALVERIFY), verify_result_equalverify); 85 | } 86 | 87 | BOOST_AUTO_TEST_CASE(consensus__script_error_to_verify_result__CHECKMULTISIGVERIFY__checkmultisigverify) 88 | { 89 | BOOST_REQUIRE_EQUAL(script_error_to_verify_result(SCRIPT_ERR_CHECKMULTISIGVERIFY), verify_result_checkmultisigverify); 90 | } 91 | 92 | BOOST_AUTO_TEST_CASE(consensus__script_error_to_verify_result__CHECKSIGVERIFY__checksigverify) 93 | { 94 | BOOST_REQUIRE_EQUAL(script_error_to_verify_result(SCRIPT_ERR_CHECKSIGVERIFY), verify_result_checksigverify); 95 | } 96 | 97 | BOOST_AUTO_TEST_CASE(consensus__script_error_to_verify_result__NUMEQUALVERIFY__numequalverify) 98 | { 99 | BOOST_REQUIRE_EQUAL(script_error_to_verify_result(SCRIPT_ERR_NUMEQUALVERIFY), verify_result_numequalverify); 100 | } 101 | 102 | // Logical/Format/Canonical errors 103 | 104 | BOOST_AUTO_TEST_CASE(consensus__script_error_to_verify_result__BAD_OPCODE__bad_opcode) 105 | { 106 | BOOST_REQUIRE_EQUAL(script_error_to_verify_result(SCRIPT_ERR_BAD_OPCODE), verify_result_bad_opcode); 107 | } 108 | 109 | BOOST_AUTO_TEST_CASE(consensus__script_error_to_verify_result__DISABLED_OPCODE__disabled_opcode) 110 | { 111 | BOOST_REQUIRE_EQUAL(script_error_to_verify_result(SCRIPT_ERR_DISABLED_OPCODE), verify_result_disabled_opcode); 112 | } 113 | 114 | BOOST_AUTO_TEST_CASE(consensus__script_error_to_verify_result__INVALID_STACK_OPERATION__invalid_stack_operation) 115 | { 116 | BOOST_REQUIRE_EQUAL(script_error_to_verify_result(SCRIPT_ERR_INVALID_STACK_OPERATION), verify_result_invalid_stack_operation); 117 | } 118 | 119 | BOOST_AUTO_TEST_CASE(consensus__script_error_to_verify_result__INVALID_ALTSTACK_OPERATION__invalid_altstack_operation) 120 | { 121 | BOOST_REQUIRE_EQUAL(script_error_to_verify_result(SCRIPT_ERR_INVALID_ALTSTACK_OPERATION), verify_result_invalid_altstack_operation); 122 | } 123 | 124 | BOOST_AUTO_TEST_CASE(consensus__script_error_to_verify_result__UNBALANCED_CONDITIONAL__unbalanced_conditional) 125 | { 126 | BOOST_REQUIRE_EQUAL(script_error_to_verify_result(SCRIPT_ERR_UNBALANCED_CONDITIONAL), verify_result_unbalanced_conditional); 127 | } 128 | 129 | // BIP65 130 | 131 | BOOST_AUTO_TEST_CASE(consensus__script_error_to_verify_result__NEGATIVE_LOCKTIME__sig_hashtype) 132 | { 133 | BOOST_REQUIRE_EQUAL(script_error_to_verify_result(SCRIPT_ERR_NEGATIVE_LOCKTIME), verify_result_negative_locktime); 134 | } 135 | 136 | BOOST_AUTO_TEST_CASE(consensus__script_error_to_verify_result__ERR_UNSATISFIED_LOCKTIME__err_sig_der) 137 | { 138 | BOOST_REQUIRE_EQUAL(script_error_to_verify_result(SCRIPT_ERR_UNSATISFIED_LOCKTIME), verify_result_unsatisfied_locktime); 139 | } 140 | 141 | // BIP62 142 | 143 | BOOST_AUTO_TEST_CASE(consensus__script_error_to_verify_result__SIG_HASHTYPE__sig_hashtype) 144 | { 145 | BOOST_REQUIRE_EQUAL(script_error_to_verify_result(SCRIPT_ERR_SIG_HASHTYPE), verify_result_sig_hashtype); 146 | } 147 | 148 | BOOST_AUTO_TEST_CASE(consensus__script_error_to_verify_result__ERR_SIG_DER__err_sig_der) 149 | { 150 | BOOST_REQUIRE_EQUAL(script_error_to_verify_result(SCRIPT_ERR_SIG_DER), verify_result_sig_der); 151 | } 152 | 153 | BOOST_AUTO_TEST_CASE(consensus__script_error_to_verify_result__ERR_MINIMALDATA__err_minimaldata) 154 | { 155 | BOOST_REQUIRE_EQUAL(script_error_to_verify_result(SCRIPT_ERR_MINIMALDATA), verify_result_minimaldata); 156 | } 157 | 158 | BOOST_AUTO_TEST_CASE(consensus__script_error_to_verify_result__SIG_PUSHONLY__sig_pushonly) 159 | { 160 | BOOST_REQUIRE_EQUAL(script_error_to_verify_result(SCRIPT_ERR_SIG_PUSHONLY), verify_result_sig_pushonly); 161 | } 162 | 163 | BOOST_AUTO_TEST_CASE(consensus__script_error_to_verify_result__SIG_HIGH_S__sig_high_s) 164 | { 165 | BOOST_REQUIRE_EQUAL(script_error_to_verify_result(SCRIPT_ERR_SIG_HIGH_S), verify_result_sig_high_s); 166 | } 167 | 168 | BOOST_AUTO_TEST_CASE(consensus__script_error_to_verify_result__SIG_NULLDUMMY__sig_nulldummy) 169 | { 170 | BOOST_REQUIRE_EQUAL(script_error_to_verify_result(SCRIPT_ERR_SIG_NULLDUMMY), verify_result_sig_nulldummy); 171 | } 172 | 173 | BOOST_AUTO_TEST_CASE(consensus__script_error_to_verify_result__PUBKEYTYPE__pubkeytype) 174 | { 175 | BOOST_REQUIRE_EQUAL(script_error_to_verify_result(SCRIPT_ERR_PUBKEYTYPE), verify_result_pubkeytype); 176 | } 177 | 178 | BOOST_AUTO_TEST_CASE(consensus__script_error_to_verify_result__CLEANSTACK__cleanstack) 179 | { 180 | BOOST_REQUIRE_EQUAL(script_error_to_verify_result(SCRIPT_ERR_CLEANSTACK), verify_result_cleanstack); 181 | } 182 | 183 | BOOST_AUTO_TEST_CASE(consensus__script_error_to_verify_result__MINIMALIF__minimalif) 184 | { 185 | BOOST_REQUIRE_EQUAL(script_error_to_verify_result(SCRIPT_ERR_MINIMALIF), verify_result_minimalif); 186 | } 187 | 188 | BOOST_AUTO_TEST_CASE(consensus__script_error_to_verify_result__SIG_NULLFAIL__nullfail) 189 | { 190 | BOOST_REQUIRE_EQUAL(script_error_to_verify_result(SCRIPT_ERR_SIG_NULLFAIL), verify_result_sig_nullfail); 191 | } 192 | 193 | // Softfork safeness 194 | 195 | BOOST_AUTO_TEST_CASE(consensus__script_error_to_verify_result___DISCOURAGE_UPGRADABLE_NOPS___discourage_upgradable_nops) 196 | { 197 | BOOST_REQUIRE_EQUAL(script_error_to_verify_result(SCRIPT_ERR_DISCOURAGE_UPGRADABLE_NOPS), verify_result_discourage_upgradable_nops); 198 | } 199 | 200 | BOOST_AUTO_TEST_CASE(consensus__script_error_to_verify_result___DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM___discourage_upgradable_witness_program) 201 | { 202 | BOOST_REQUIRE_EQUAL(script_error_to_verify_result(SCRIPT_ERR_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM), verify_result_discourage_upgradable_witness_program); 203 | } 204 | 205 | // Segregated witness 206 | 207 | BOOST_AUTO_TEST_CASE(consensus__script_error_to_verify_result___WITNESS_PROGRAM_WRONG_LENGTH___witness_program_wrong_length) 208 | { 209 | BOOST_REQUIRE_EQUAL(script_error_to_verify_result(SCRIPT_ERR_WITNESS_PROGRAM_WRONG_LENGTH), verify_result_witness_program_wrong_length); 210 | } 211 | 212 | BOOST_AUTO_TEST_CASE(consensus__script_error_to_verify_result___WITNESS_PROGRAM_WITNESS_EMPTY___witness_program_empty_witness) 213 | { 214 | BOOST_REQUIRE_EQUAL(script_error_to_verify_result(SCRIPT_ERR_WITNESS_PROGRAM_WITNESS_EMPTY), verify_result_witness_program_empty_witness); 215 | } 216 | 217 | BOOST_AUTO_TEST_CASE(consensus__script_error_to_verify_result___WITNESS_PROGRAM_MISMATCH___witness_program_mismatch) 218 | { 219 | BOOST_REQUIRE_EQUAL(script_error_to_verify_result(SCRIPT_ERR_WITNESS_PROGRAM_MISMATCH), verify_result_witness_program_mismatch); 220 | } 221 | 222 | BOOST_AUTO_TEST_CASE(consensus__script_error_to_verify_result___WITNESS_MALLEATED___witness_malleated) 223 | { 224 | BOOST_REQUIRE_EQUAL(script_error_to_verify_result(SCRIPT_ERR_WITNESS_MALLEATED), verify_result_witness_malleated); 225 | } 226 | 227 | BOOST_AUTO_TEST_CASE(consensus__script_error_to_verify_result___WITNESS_MALLEATED_P2SH___witness_malleated_p2sh) 228 | { 229 | BOOST_REQUIRE_EQUAL(script_error_to_verify_result(SCRIPT_ERR_WITNESS_MALLEATED_P2SH), verify_result_witness_malleated_p2sh); 230 | } 231 | 232 | BOOST_AUTO_TEST_CASE(consensus__script_error_to_verify_result___WITNESS_UNEXPECTED___witness_unexpected) 233 | { 234 | BOOST_REQUIRE_EQUAL(script_error_to_verify_result(SCRIPT_ERR_WITNESS_UNEXPECTED), verify_result_witness_unexpected); 235 | } 236 | 237 | BOOST_AUTO_TEST_CASE(consensus__script_error_to_verify_result___WITNESS_PUBKEYTYPE___witness_pubkeytype) 238 | { 239 | BOOST_REQUIRE_EQUAL(script_error_to_verify_result(SCRIPT_ERR_WITNESS_PUBKEYTYPE), verify_result_witness_pubkeytype); 240 | } 241 | 242 | // Other 243 | 244 | BOOST_AUTO_TEST_CASE(consensus__script_error_to_verify_result__OP_RETURN__op_return) 245 | { 246 | BOOST_REQUIRE_EQUAL(script_error_to_verify_result(SCRIPT_ERR_OP_RETURN), verify_result_op_return); 247 | } 248 | 249 | BOOST_AUTO_TEST_CASE(consensus__script_error_to_verify_result__UNKNOWN_ERROR__unknown_error) 250 | { 251 | BOOST_REQUIRE_EQUAL(script_error_to_verify_result(SCRIPT_ERR_UNKNOWN_ERROR), verify_result_unknown_error); 252 | } 253 | 254 | BOOST_AUTO_TEST_CASE(consensus__script_error_to_verify_result__ERROR_COUNT__unknown_error) 255 | { 256 | BOOST_REQUIRE_EQUAL(script_error_to_verify_result(SCRIPT_ERR_ERROR_COUNT), verify_result_unknown_error); 257 | } 258 | 259 | BOOST_AUTO_TEST_SUITE_END() 260 | -------------------------------------------------------------------------------- /test/consensus__script_verify.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2011-2023 libbitcoin developers (see AUTHORS) 3 | * 4 | * This file is part of libbitcoin. 5 | * 6 | * This program is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Affero General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Affero General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Affero General Public License 17 | * along with this program. If not, see . 18 | */ 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | #include "script.hpp" 32 | #include "test.hpp" 33 | 34 | BOOST_AUTO_TEST_SUITE(consensus__script_verify) 35 | 36 | using namespace libbitcoin::consensus; 37 | 38 | // Test case derived from: 39 | // github.com/libbitcoin/libbitcoin-explorer/wiki/How-to-Spend-Bitcoin 40 | #define CONSENSUS_SCRIPT_VERIFY_TX \ 41 | "01000000017d01943c40b7f3d8a00a2d62fa1d560bf739a2368c180615b0a7937c0e883e7c000000006b4830450221008f66d188c664a8088893ea4ddd9689024ea5593877753ecc1e9051ed58c15168022037109f0d06e6068b7447966f751de8474641ad2b15ec37f4a9d159b02af68174012103e208f5403383c77d5832a268c9f71480f6e7bfbdfa44904becacfad66163ea31ffffffff01c8af0000000000001976a91458b7a60f11a904feef35a639b6048de8dd4d9f1c88ac00000000" 42 | #define CONSENSUS_SCRIPT_VERIFY_PREVOUT_SCRIPT \ 43 | "76a914c564c740c6900b93afc9f1bdaef0a9d466adf6ee88ac" 44 | 45 | // Test case derived from first witness tx: 46 | #define CONSENSUS_SCRIPT_VERIFY_WITNESS_TX \ 47 | "010000000001015836964079411659db5a4cfddd70e3f0de0261268f86c998a69a143f47c6c83800000000171600149445e8b825f1a17d5e091948545c90654096db68ffffffff02d8be04000000000017a91422c17a06117b40516f9826804800003562e834c98700000000000000004d6a4b424950313431205c6f2f2048656c6c6f20536567576974203a2d29206b656570206974207374726f6e6721204c4c415020426974636f696e20747769747465722e636f6d2f6b6873396e6502483045022100aaa281e0611ba0b5a2cd055f77e5594709d611ad1233e7096394f64ffe16f5b202207e2dcc9ef3a54c24471799ab99f6615847b21be2a6b4e0285918fd025597c5740121021ec0613f21c4e81c4b300426e5e5d30fa651f41e9993223adbe74dbe603c74fb00000000" 48 | #define CONSENSUS_SCRIPT_VERIFY_WITNESS_PREVOUT_SCRIPT \ 49 | "a914642bda298792901eb1b48f654dd7225d99e5e68c87" 50 | 51 | // test helper 52 | static verify_result test_verify(const std::string& transaction, 53 | const std::string& prevout_script, uint64_t value=0, 54 | uint32_t input_index=0, const uint32_t flags=verify_flags_p2sh, 55 | bool tx_size_hack=false) 56 | { 57 | data_chunk tx, prevout; 58 | BOOST_REQUIRE(decode_base16(tx, transaction)); 59 | BOOST_REQUIRE(decode_base16(prevout, prevout_script)); 60 | 61 | if (tx_size_hack) 62 | tx.push_back(0x42); 63 | 64 | return verify_script(tx, { prevout, value }, input_index, flags); 65 | } 66 | 67 | // test helper 68 | static verify_result test_verify_unsigned(const std::string& input_script, 69 | const std::string& prevout_script, const uint32_t flags) 70 | { 71 | // Implement once we have vectors. 72 | static const stack witness; 73 | 74 | // Convert scripts from mnemonic to bytes. 75 | const auto input = mnemonic_to_data(input_script); 76 | const auto prevout = mnemonic_to_data(prevout_script); 77 | 78 | return verify_unsigned_script({ prevout, 0 }, input, witness, flags); 79 | } 80 | 81 | BOOST_AUTO_TEST_CASE(consensus__script_verify__value_overflow__verify_prevout_value_overflow) 82 | { 83 | data_chunk tx{ 0x42 }, prevout; 84 | BOOST_REQUIRE(decode_base16(prevout, CONSENSUS_SCRIPT_VERIFY_PREVOUT_SCRIPT)); 85 | BOOST_REQUIRE(verify_script(tx, { prevout, 0xffffffffffffffff }, 0, 0) == verify_value_overflow); 86 | } 87 | 88 | BOOST_AUTO_TEST_CASE(consensus__script_verify__invalid_tx__tx_invalid) 89 | { 90 | const verify_result result = test_verify("42", "42"); 91 | BOOST_REQUIRE_EQUAL(result, verify_result_tx_invalid); 92 | } 93 | 94 | BOOST_AUTO_TEST_CASE(consensus__script_verify__invalid_input__tx_input_invalid) 95 | { 96 | const verify_result result = test_verify(CONSENSUS_SCRIPT_VERIFY_TX, CONSENSUS_SCRIPT_VERIFY_PREVOUT_SCRIPT, 0, 1); 97 | BOOST_REQUIRE_EQUAL(result, verify_result_tx_input_invalid); 98 | } 99 | 100 | BOOST_AUTO_TEST_CASE(consensus__script_verify__oversized_tx__tx_size_invalid) 101 | { 102 | #ifndef NDEBUG 103 | const verify_result result = test_verify(CONSENSUS_SCRIPT_VERIFY_TX, CONSENSUS_SCRIPT_VERIFY_PREVOUT_SCRIPT, 0, 0, verify_flags_p2sh, +1); 104 | BOOST_REQUIRE_EQUAL(result, verify_result_tx_size_invalid); 105 | #endif // NDEBUG 106 | } 107 | 108 | BOOST_AUTO_TEST_CASE(consensus__script_verify__incorrect_pubkey_hash__equalverify) 109 | { 110 | const verify_result result = test_verify(CONSENSUS_SCRIPT_VERIFY_TX, "76a914c564c740c6900b93afc9f1bdaef0a9d466adf6ef88ac"); 111 | BOOST_REQUIRE_EQUAL(result, verify_result_equalverify); 112 | } 113 | 114 | BOOST_AUTO_TEST_CASE(consensus__script_verify__valid__true) 115 | { 116 | const verify_result result = test_verify(CONSENSUS_SCRIPT_VERIFY_TX, CONSENSUS_SCRIPT_VERIFY_PREVOUT_SCRIPT); 117 | BOOST_REQUIRE_EQUAL(result, verify_result_eval_true); 118 | } 119 | 120 | BOOST_AUTO_TEST_CASE(consensus__script_verify__valid_nested_p2wpkh__true) 121 | { 122 | static const auto index = 0u; 123 | static const auto value = 500000u; 124 | static const uint32_t flags = 125 | verify_flags_p2sh | 126 | verify_flags_dersig | 127 | verify_flags_nulldummy | 128 | verify_flags_checklocktimeverify | 129 | verify_flags_checksequenceverify | 130 | verify_flags_witness; 131 | 132 | const verify_result result = test_verify(CONSENSUS_SCRIPT_VERIFY_WITNESS_TX, CONSENSUS_SCRIPT_VERIFY_WITNESS_PREVOUT_SCRIPT, value, index, flags); 133 | BOOST_REQUIRE_EQUAL(result, verify_result_eval_true); 134 | } 135 | 136 | BOOST_AUTO_TEST_CASE(consensus__script_verify__bip16__valid) 137 | { 138 | for (const auto& test: valid_bip16_scripts) 139 | { 140 | BOOST_CHECK_MESSAGE(test_verify_unsigned(test.input, test.output, verify_flags_none) == verify_result_eval_true, test.description); 141 | BOOST_CHECK_MESSAGE(test_verify_unsigned(test.input, test.output, verify_flags_p2sh) == verify_result_eval_true, test.description); 142 | } 143 | } 144 | 145 | BOOST_AUTO_TEST_CASE(consensus__script_verify__bip16__invalidated) 146 | { 147 | for (const auto& test: invalidated_bip16_scripts) 148 | { 149 | BOOST_CHECK_MESSAGE(test_verify_unsigned(test.input, test.output, verify_flags_none) == verify_result_eval_true, test.description); 150 | BOOST_CHECK_MESSAGE(test_verify_unsigned(test.input, test.output, verify_flags_p2sh) != verify_result_eval_true, test.description); 151 | } 152 | } 153 | 154 | ////// Requires tx test for locktime values (set on vectors, see libbitcoin tests). 155 | ////BOOST_AUTO_TEST_CASE(consensus__script_verify__bip65__valid) 156 | ////{ 157 | //// for (const auto& test: valid_bip65_scripts) 158 | //// { 159 | //// BOOST_CHECK_MESSAGE(test_verify_unsigned(test.input, test.output, verify_flags_none) == verify_result_eval_true, test.description); 160 | //// BOOST_CHECK_MESSAGE(test_verify_unsigned(test.input, test.output, verify_flags_checklocktimeverify) == verify_result_eval_true, test.description); 161 | //// } 162 | ////} 163 | //// 164 | ////// Requires tx test for locktime values (set on vectors, see libbitcoin tests). 165 | ////BOOST_AUTO_TEST_CASE(consensus__script_verify__bip65__invalid) 166 | ////{ 167 | //// for (const auto& test: invalid_bip65_scripts) 168 | //// { 169 | //// BOOST_CHECK_MESSAGE(test_verify_unsigned(test.input, test.output, verify_flags_none) != verify_result_eval_true, test.description); 170 | //// BOOST_CHECK_MESSAGE(test_verify_unsigned(test.input, test.output, verify_flags_checklocktimeverify) != verify_result_eval_true, test.description); 171 | //// } 172 | ////} 173 | //// 174 | ////// Requires tx test for locktime values (set on vectors, see libbitcoin tests). 175 | ////BOOST_AUTO_TEST_CASE(consensus__script_verify__bip65__invalidated) 176 | ////{ 177 | //// for (const auto& test: invalidated_bip65_scripts) 178 | //// { 179 | //// BOOST_CHECK_MESSAGE(test_verify_unsigned(test.input, test.output, verify_flags_none) != verify_result_eval_true, test.description); 180 | //// BOOST_CHECK_MESSAGE(test_verify_unsigned(test.input, test.output, verify_flags_checklocktimeverify) != verify_result_eval_true, test.description); 181 | //// } 182 | ////} 183 | 184 | BOOST_AUTO_TEST_CASE(consensus__script_verify__multisig__valid) 185 | { 186 | for (const auto& test: valid_multisig_scripts) 187 | { 188 | BOOST_CHECK_MESSAGE(test_verify_unsigned(test.input, test.output, verify_flags_none) == verify_result_eval_true, test.description); 189 | BOOST_CHECK_MESSAGE(test_verify_unsigned(test.input, test.output, verify_flags_dersig) == verify_result_eval_true, test.description); 190 | } 191 | } 192 | 193 | BOOST_AUTO_TEST_CASE(consensus__script_verify__multisig__invalid) 194 | { 195 | for (const auto& test: invalid_multisig_scripts) 196 | { 197 | BOOST_CHECK_MESSAGE(test_verify_unsigned(test.input, test.output, verify_flags_none) != verify_result_eval_true, test.description); 198 | BOOST_CHECK_MESSAGE(test_verify_unsigned(test.input, test.output, verify_flags_dersig) != verify_result_eval_true, test.description); 199 | } 200 | } 201 | 202 | BOOST_AUTO_TEST_CASE(consensus__script_verify__context_free__valid) 203 | { 204 | for (const auto& test: valid_context_free_scripts) 205 | { 206 | BOOST_CHECK_MESSAGE(test_verify_unsigned(test.input, test.output, verify_flags_none) == verify_result_eval_true, test.description); 207 | } 208 | } 209 | 210 | BOOST_AUTO_TEST_CASE(consensus__script_verify__context_free__invalid) 211 | { 212 | for (const auto& test: invalid_context_free_scripts) 213 | { 214 | BOOST_CHECK_MESSAGE(test_verify_unsigned(test.input, test.output, verify_flags_none) != verify_result_eval_true, test.description); 215 | } 216 | } 217 | 218 | BOOST_AUTO_TEST_CASE(script__parse__not_invalid) 219 | { 220 | for (const auto& test: not_invalid_parse_scripts) 221 | { 222 | mnemonic_to_data(test.input, true); 223 | mnemonic_to_data(test.output, true); 224 | } 225 | } 226 | 227 | BOOST_AUTO_TEST_CASE(script__parse_syntax__invalid_input) 228 | { 229 | for (const auto& test: invalid_syntax_scripts) 230 | { 231 | mnemonic_to_data(test.input, false); 232 | mnemonic_to_data(test.output, true); 233 | } 234 | } 235 | 236 | BOOST_AUTO_TEST_CASE(script__parse_push_not_overflow__valid) 237 | { 238 | for (const auto& test: valid_push_data_scripts) 239 | { 240 | mnemonic_to_data(test.input, true); 241 | mnemonic_to_data(test.output, true); 242 | } 243 | } 244 | 245 | BOOST_AUTO_TEST_CASE(script__parse_push_overflow__invalid_input) 246 | { 247 | for (const auto& test: invalid_overflowed_push_data_scripts) 248 | { 249 | mnemonic_to_data(test.input, false); 250 | mnemonic_to_data(test.output, true); 251 | } 252 | } 253 | 254 | BOOST_AUTO_TEST_SUITE_END() 255 | -------------------------------------------------------------------------------- /src/clone/crypto/sha512.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2014-2019 The Bitcoin Core developers 2 | // Distributed under the MIT software license, see the accompanying 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 | 5 | #include 6 | 7 | #include 8 | 9 | #include 10 | 11 | // Internal implementation code. 12 | namespace 13 | { 14 | /// Internal SHA-512 implementation. 15 | namespace sha512 16 | { 17 | uint64_t inline Ch(uint64_t x, uint64_t y, uint64_t z) { return z ^ (x & (y ^ z)); } 18 | uint64_t inline Maj(uint64_t x, uint64_t y, uint64_t z) { return (x & y) | (z & (x | y)); } 19 | uint64_t inline Sigma0(uint64_t x) { return (x >> 28 | x << 36) ^ (x >> 34 | x << 30) ^ (x >> 39 | x << 25); } 20 | uint64_t inline Sigma1(uint64_t x) { return (x >> 14 | x << 50) ^ (x >> 18 | x << 46) ^ (x >> 41 | x << 23); } 21 | uint64_t inline sigma0(uint64_t x) { return (x >> 1 | x << 63) ^ (x >> 8 | x << 56) ^ (x >> 7); } 22 | uint64_t inline sigma1(uint64_t x) { return (x >> 19 | x << 45) ^ (x >> 61 | x << 3) ^ (x >> 6); } 23 | 24 | /** One round of SHA-512. */ 25 | void inline Round(uint64_t a, uint64_t b, uint64_t c, uint64_t& d, uint64_t e, uint64_t f, uint64_t g, uint64_t& h, uint64_t k, uint64_t w) 26 | { 27 | uint64_t t1 = h + Sigma1(e) + Ch(e, f, g) + k + w; 28 | uint64_t t2 = Sigma0(a) + Maj(a, b, c); 29 | d += t1; 30 | h = t1 + t2; 31 | } 32 | 33 | /** Initialize SHA-256 state. */ 34 | void inline Initialize(uint64_t* s) 35 | { 36 | s[0] = 0x6a09e667f3bcc908ull; 37 | s[1] = 0xbb67ae8584caa73bull; 38 | s[2] = 0x3c6ef372fe94f82bull; 39 | s[3] = 0xa54ff53a5f1d36f1ull; 40 | s[4] = 0x510e527fade682d1ull; 41 | s[5] = 0x9b05688c2b3e6c1full; 42 | s[6] = 0x1f83d9abfb41bd6bull; 43 | s[7] = 0x5be0cd19137e2179ull; 44 | } 45 | 46 | /** Perform one SHA-512 transformation, processing a 128-byte chunk. */ 47 | void Transform(uint64_t* s, const unsigned char* chunk) 48 | { 49 | uint64_t a = s[0], b = s[1], c = s[2], d = s[3], e = s[4], f = s[5], g = s[6], h = s[7]; 50 | uint64_t w0, w1, w2, w3, w4, w5, w6, w7, w8, w9, w10, w11, w12, w13, w14, w15; 51 | 52 | Round(a, b, c, d, e, f, g, h, 0x428a2f98d728ae22ull, w0 = ReadBE64(chunk + 0)); 53 | Round(h, a, b, c, d, e, f, g, 0x7137449123ef65cdull, w1 = ReadBE64(chunk + 8)); 54 | Round(g, h, a, b, c, d, e, f, 0xb5c0fbcfec4d3b2full, w2 = ReadBE64(chunk + 16)); 55 | Round(f, g, h, a, b, c, d, e, 0xe9b5dba58189dbbcull, w3 = ReadBE64(chunk + 24)); 56 | Round(e, f, g, h, a, b, c, d, 0x3956c25bf348b538ull, w4 = ReadBE64(chunk + 32)); 57 | Round(d, e, f, g, h, a, b, c, 0x59f111f1b605d019ull, w5 = ReadBE64(chunk + 40)); 58 | Round(c, d, e, f, g, h, a, b, 0x923f82a4af194f9bull, w6 = ReadBE64(chunk + 48)); 59 | Round(b, c, d, e, f, g, h, a, 0xab1c5ed5da6d8118ull, w7 = ReadBE64(chunk + 56)); 60 | Round(a, b, c, d, e, f, g, h, 0xd807aa98a3030242ull, w8 = ReadBE64(chunk + 64)); 61 | Round(h, a, b, c, d, e, f, g, 0x12835b0145706fbeull, w9 = ReadBE64(chunk + 72)); 62 | Round(g, h, a, b, c, d, e, f, 0x243185be4ee4b28cull, w10 = ReadBE64(chunk + 80)); 63 | Round(f, g, h, a, b, c, d, e, 0x550c7dc3d5ffb4e2ull, w11 = ReadBE64(chunk + 88)); 64 | Round(e, f, g, h, a, b, c, d, 0x72be5d74f27b896full, w12 = ReadBE64(chunk + 96)); 65 | Round(d, e, f, g, h, a, b, c, 0x80deb1fe3b1696b1ull, w13 = ReadBE64(chunk + 104)); 66 | Round(c, d, e, f, g, h, a, b, 0x9bdc06a725c71235ull, w14 = ReadBE64(chunk + 112)); 67 | Round(b, c, d, e, f, g, h, a, 0xc19bf174cf692694ull, w15 = ReadBE64(chunk + 120)); 68 | 69 | Round(a, b, c, d, e, f, g, h, 0xe49b69c19ef14ad2ull, w0 += sigma1(w14) + w9 + sigma0(w1)); 70 | Round(h, a, b, c, d, e, f, g, 0xefbe4786384f25e3ull, w1 += sigma1(w15) + w10 + sigma0(w2)); 71 | Round(g, h, a, b, c, d, e, f, 0x0fc19dc68b8cd5b5ull, w2 += sigma1(w0) + w11 + sigma0(w3)); 72 | Round(f, g, h, a, b, c, d, e, 0x240ca1cc77ac9c65ull, w3 += sigma1(w1) + w12 + sigma0(w4)); 73 | Round(e, f, g, h, a, b, c, d, 0x2de92c6f592b0275ull, w4 += sigma1(w2) + w13 + sigma0(w5)); 74 | Round(d, e, f, g, h, a, b, c, 0x4a7484aa6ea6e483ull, w5 += sigma1(w3) + w14 + sigma0(w6)); 75 | Round(c, d, e, f, g, h, a, b, 0x5cb0a9dcbd41fbd4ull, w6 += sigma1(w4) + w15 + sigma0(w7)); 76 | Round(b, c, d, e, f, g, h, a, 0x76f988da831153b5ull, w7 += sigma1(w5) + w0 + sigma0(w8)); 77 | Round(a, b, c, d, e, f, g, h, 0x983e5152ee66dfabull, w8 += sigma1(w6) + w1 + sigma0(w9)); 78 | Round(h, a, b, c, d, e, f, g, 0xa831c66d2db43210ull, w9 += sigma1(w7) + w2 + sigma0(w10)); 79 | Round(g, h, a, b, c, d, e, f, 0xb00327c898fb213full, w10 += sigma1(w8) + w3 + sigma0(w11)); 80 | Round(f, g, h, a, b, c, d, e, 0xbf597fc7beef0ee4ull, w11 += sigma1(w9) + w4 + sigma0(w12)); 81 | Round(e, f, g, h, a, b, c, d, 0xc6e00bf33da88fc2ull, w12 += sigma1(w10) + w5 + sigma0(w13)); 82 | Round(d, e, f, g, h, a, b, c, 0xd5a79147930aa725ull, w13 += sigma1(w11) + w6 + sigma0(w14)); 83 | Round(c, d, e, f, g, h, a, b, 0x06ca6351e003826full, w14 += sigma1(w12) + w7 + sigma0(w15)); 84 | Round(b, c, d, e, f, g, h, a, 0x142929670a0e6e70ull, w15 += sigma1(w13) + w8 + sigma0(w0)); 85 | 86 | Round(a, b, c, d, e, f, g, h, 0x27b70a8546d22ffcull, w0 += sigma1(w14) + w9 + sigma0(w1)); 87 | Round(h, a, b, c, d, e, f, g, 0x2e1b21385c26c926ull, w1 += sigma1(w15) + w10 + sigma0(w2)); 88 | Round(g, h, a, b, c, d, e, f, 0x4d2c6dfc5ac42aedull, w2 += sigma1(w0) + w11 + sigma0(w3)); 89 | Round(f, g, h, a, b, c, d, e, 0x53380d139d95b3dfull, w3 += sigma1(w1) + w12 + sigma0(w4)); 90 | Round(e, f, g, h, a, b, c, d, 0x650a73548baf63deull, w4 += sigma1(w2) + w13 + sigma0(w5)); 91 | Round(d, e, f, g, h, a, b, c, 0x766a0abb3c77b2a8ull, w5 += sigma1(w3) + w14 + sigma0(w6)); 92 | Round(c, d, e, f, g, h, a, b, 0x81c2c92e47edaee6ull, w6 += sigma1(w4) + w15 + sigma0(w7)); 93 | Round(b, c, d, e, f, g, h, a, 0x92722c851482353bull, w7 += sigma1(w5) + w0 + sigma0(w8)); 94 | Round(a, b, c, d, e, f, g, h, 0xa2bfe8a14cf10364ull, w8 += sigma1(w6) + w1 + sigma0(w9)); 95 | Round(h, a, b, c, d, e, f, g, 0xa81a664bbc423001ull, w9 += sigma1(w7) + w2 + sigma0(w10)); 96 | Round(g, h, a, b, c, d, e, f, 0xc24b8b70d0f89791ull, w10 += sigma1(w8) + w3 + sigma0(w11)); 97 | Round(f, g, h, a, b, c, d, e, 0xc76c51a30654be30ull, w11 += sigma1(w9) + w4 + sigma0(w12)); 98 | Round(e, f, g, h, a, b, c, d, 0xd192e819d6ef5218ull, w12 += sigma1(w10) + w5 + sigma0(w13)); 99 | Round(d, e, f, g, h, a, b, c, 0xd69906245565a910ull, w13 += sigma1(w11) + w6 + sigma0(w14)); 100 | Round(c, d, e, f, g, h, a, b, 0xf40e35855771202aull, w14 += sigma1(w12) + w7 + sigma0(w15)); 101 | Round(b, c, d, e, f, g, h, a, 0x106aa07032bbd1b8ull, w15 += sigma1(w13) + w8 + sigma0(w0)); 102 | 103 | Round(a, b, c, d, e, f, g, h, 0x19a4c116b8d2d0c8ull, w0 += sigma1(w14) + w9 + sigma0(w1)); 104 | Round(h, a, b, c, d, e, f, g, 0x1e376c085141ab53ull, w1 += sigma1(w15) + w10 + sigma0(w2)); 105 | Round(g, h, a, b, c, d, e, f, 0x2748774cdf8eeb99ull, w2 += sigma1(w0) + w11 + sigma0(w3)); 106 | Round(f, g, h, a, b, c, d, e, 0x34b0bcb5e19b48a8ull, w3 += sigma1(w1) + w12 + sigma0(w4)); 107 | Round(e, f, g, h, a, b, c, d, 0x391c0cb3c5c95a63ull, w4 += sigma1(w2) + w13 + sigma0(w5)); 108 | Round(d, e, f, g, h, a, b, c, 0x4ed8aa4ae3418acbull, w5 += sigma1(w3) + w14 + sigma0(w6)); 109 | Round(c, d, e, f, g, h, a, b, 0x5b9cca4f7763e373ull, w6 += sigma1(w4) + w15 + sigma0(w7)); 110 | Round(b, c, d, e, f, g, h, a, 0x682e6ff3d6b2b8a3ull, w7 += sigma1(w5) + w0 + sigma0(w8)); 111 | Round(a, b, c, d, e, f, g, h, 0x748f82ee5defb2fcull, w8 += sigma1(w6) + w1 + sigma0(w9)); 112 | Round(h, a, b, c, d, e, f, g, 0x78a5636f43172f60ull, w9 += sigma1(w7) + w2 + sigma0(w10)); 113 | Round(g, h, a, b, c, d, e, f, 0x84c87814a1f0ab72ull, w10 += sigma1(w8) + w3 + sigma0(w11)); 114 | Round(f, g, h, a, b, c, d, e, 0x8cc702081a6439ecull, w11 += sigma1(w9) + w4 + sigma0(w12)); 115 | Round(e, f, g, h, a, b, c, d, 0x90befffa23631e28ull, w12 += sigma1(w10) + w5 + sigma0(w13)); 116 | Round(d, e, f, g, h, a, b, c, 0xa4506cebde82bde9ull, w13 += sigma1(w11) + w6 + sigma0(w14)); 117 | Round(c, d, e, f, g, h, a, b, 0xbef9a3f7b2c67915ull, w14 += sigma1(w12) + w7 + sigma0(w15)); 118 | Round(b, c, d, e, f, g, h, a, 0xc67178f2e372532bull, w15 += sigma1(w13) + w8 + sigma0(w0)); 119 | 120 | Round(a, b, c, d, e, f, g, h, 0xca273eceea26619cull, w0 += sigma1(w14) + w9 + sigma0(w1)); 121 | Round(h, a, b, c, d, e, f, g, 0xd186b8c721c0c207ull, w1 += sigma1(w15) + w10 + sigma0(w2)); 122 | Round(g, h, a, b, c, d, e, f, 0xeada7dd6cde0eb1eull, w2 += sigma1(w0) + w11 + sigma0(w3)); 123 | Round(f, g, h, a, b, c, d, e, 0xf57d4f7fee6ed178ull, w3 += sigma1(w1) + w12 + sigma0(w4)); 124 | Round(e, f, g, h, a, b, c, d, 0x06f067aa72176fbaull, w4 += sigma1(w2) + w13 + sigma0(w5)); 125 | Round(d, e, f, g, h, a, b, c, 0x0a637dc5a2c898a6ull, w5 += sigma1(w3) + w14 + sigma0(w6)); 126 | Round(c, d, e, f, g, h, a, b, 0x113f9804bef90daeull, w6 += sigma1(w4) + w15 + sigma0(w7)); 127 | Round(b, c, d, e, f, g, h, a, 0x1b710b35131c471bull, w7 += sigma1(w5) + w0 + sigma0(w8)); 128 | Round(a, b, c, d, e, f, g, h, 0x28db77f523047d84ull, w8 += sigma1(w6) + w1 + sigma0(w9)); 129 | Round(h, a, b, c, d, e, f, g, 0x32caab7b40c72493ull, w9 += sigma1(w7) + w2 + sigma0(w10)); 130 | Round(g, h, a, b, c, d, e, f, 0x3c9ebe0a15c9bebcull, w10 += sigma1(w8) + w3 + sigma0(w11)); 131 | Round(f, g, h, a, b, c, d, e, 0x431d67c49c100d4cull, w11 += sigma1(w9) + w4 + sigma0(w12)); 132 | Round(e, f, g, h, a, b, c, d, 0x4cc5d4becb3e42b6ull, w12 += sigma1(w10) + w5 + sigma0(w13)); 133 | Round(d, e, f, g, h, a, b, c, 0x597f299cfc657e2aull, w13 += sigma1(w11) + w6 + sigma0(w14)); 134 | Round(c, d, e, f, g, h, a, b, 0x5fcb6fab3ad6faecull, w14 + sigma1(w12) + w7 + sigma0(w15)); 135 | Round(b, c, d, e, f, g, h, a, 0x6c44198c4a475817ull, w15 + sigma1(w13) + w8 + sigma0(w0)); 136 | 137 | s[0] += a; 138 | s[1] += b; 139 | s[2] += c; 140 | s[3] += d; 141 | s[4] += e; 142 | s[5] += f; 143 | s[6] += g; 144 | s[7] += h; 145 | } 146 | 147 | } // namespace sha512 148 | 149 | } // namespace 150 | 151 | 152 | ////// SHA-512 153 | 154 | CSHA512::CSHA512() : bytes(0) 155 | { 156 | sha512::Initialize(s); 157 | } 158 | 159 | CSHA512& CSHA512::Write(const unsigned char* data, size_t len) 160 | { 161 | const unsigned char* end = data + len; 162 | size_t bufsize = bytes % 128; 163 | if (bufsize && bufsize + len >= 128) { 164 | // Fill the buffer, and process it. 165 | memcpy(buf + bufsize, data, 128 - bufsize); 166 | bytes += 128 - bufsize; 167 | data += 128 - bufsize; 168 | sha512::Transform(s, buf); 169 | bufsize = 0; 170 | } 171 | while (end - data >= 128) { 172 | // Process full chunks directly from the source. 173 | sha512::Transform(s, data); 174 | data += 128; 175 | bytes += 128; 176 | } 177 | if (end > data) { 178 | // Fill the buffer with what remains. 179 | memcpy(buf + bufsize, data, end - data); 180 | bytes += end - data; 181 | } 182 | return *this; 183 | } 184 | 185 | void CSHA512::Finalize(unsigned char hash[OUTPUT_SIZE]) 186 | { 187 | static const unsigned char pad[128] = {0x80}; 188 | unsigned char sizedesc[16] = {0x00}; 189 | WriteBE64(sizedesc + 8, bytes << 3); 190 | Write(pad, 1 + ((239 - (bytes % 128)) % 128)); 191 | Write(sizedesc, 16); 192 | WriteBE64(hash, s[0]); 193 | WriteBE64(hash + 8, s[1]); 194 | WriteBE64(hash + 16, s[2]); 195 | WriteBE64(hash + 24, s[3]); 196 | WriteBE64(hash + 32, s[4]); 197 | WriteBE64(hash + 40, s[5]); 198 | WriteBE64(hash + 48, s[6]); 199 | WriteBE64(hash + 56, s[7]); 200 | } 201 | 202 | CSHA512& CSHA512::Reset() 203 | { 204 | bytes = 0; 205 | sha512::Initialize(s); 206 | return *this; 207 | } 208 | -------------------------------------------------------------------------------- /src/clone/crypto/ripemd160.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2014-2019 The Bitcoin Core developers 2 | // Distributed under the MIT software license, see the accompanying 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 | 5 | #include 6 | 7 | #include 8 | 9 | #include 10 | 11 | // Internal implementation code. 12 | namespace 13 | { 14 | /// Internal RIPEMD-160 implementation. 15 | namespace ripemd160 16 | { 17 | uint32_t inline f1(uint32_t x, uint32_t y, uint32_t z) { return x ^ y ^ z; } 18 | uint32_t inline f2(uint32_t x, uint32_t y, uint32_t z) { return (x & y) | (~x & z); } 19 | uint32_t inline f3(uint32_t x, uint32_t y, uint32_t z) { return (x | ~y) ^ z; } 20 | uint32_t inline f4(uint32_t x, uint32_t y, uint32_t z) { return (x & z) | (y & ~z); } 21 | uint32_t inline f5(uint32_t x, uint32_t y, uint32_t z) { return x ^ (y | ~z); } 22 | 23 | /** Initialize RIPEMD-160 state. */ 24 | void inline Initialize(uint32_t* s) 25 | { 26 | s[0] = 0x67452301ul; 27 | s[1] = 0xEFCDAB89ul; 28 | s[2] = 0x98BADCFEul; 29 | s[3] = 0x10325476ul; 30 | s[4] = 0xC3D2E1F0ul; 31 | } 32 | 33 | uint32_t inline rol(uint32_t x, int i) { return (x << i) | (x >> (32 - i)); } 34 | 35 | void inline Round(uint32_t& a, uint32_t b, uint32_t& c, uint32_t d, uint32_t e, uint32_t f, uint32_t x, uint32_t k, int r) 36 | { 37 | a = rol(a + f + x + k, r) + e; 38 | c = rol(c, 10); 39 | } 40 | 41 | void inline R11(uint32_t& a, uint32_t b, uint32_t& c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f1(b, c, d), x, 0, r); } 42 | void inline R21(uint32_t& a, uint32_t b, uint32_t& c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f2(b, c, d), x, 0x5A827999ul, r); } 43 | void inline R31(uint32_t& a, uint32_t b, uint32_t& c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f3(b, c, d), x, 0x6ED9EBA1ul, r); } 44 | void inline R41(uint32_t& a, uint32_t b, uint32_t& c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f4(b, c, d), x, 0x8F1BBCDCul, r); } 45 | void inline R51(uint32_t& a, uint32_t b, uint32_t& c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f5(b, c, d), x, 0xA953FD4Eul, r); } 46 | 47 | void inline R12(uint32_t& a, uint32_t b, uint32_t& c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f5(b, c, d), x, 0x50A28BE6ul, r); } 48 | void inline R22(uint32_t& a, uint32_t b, uint32_t& c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f4(b, c, d), x, 0x5C4DD124ul, r); } 49 | void inline R32(uint32_t& a, uint32_t b, uint32_t& c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f3(b, c, d), x, 0x6D703EF3ul, r); } 50 | void inline R42(uint32_t& a, uint32_t b, uint32_t& c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f2(b, c, d), x, 0x7A6D76E9ul, r); } 51 | void inline R52(uint32_t& a, uint32_t b, uint32_t& c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f1(b, c, d), x, 0, r); } 52 | 53 | /** Perform a RIPEMD-160 transformation, processing a 64-byte chunk. */ 54 | void Transform(uint32_t* s, const unsigned char* chunk) 55 | { 56 | uint32_t a1 = s[0], b1 = s[1], c1 = s[2], d1 = s[3], e1 = s[4]; 57 | uint32_t a2 = a1, b2 = b1, c2 = c1, d2 = d1, e2 = e1; 58 | uint32_t w0 = ReadLE32(chunk + 0), w1 = ReadLE32(chunk + 4), w2 = ReadLE32(chunk + 8), w3 = ReadLE32(chunk + 12); 59 | uint32_t w4 = ReadLE32(chunk + 16), w5 = ReadLE32(chunk + 20), w6 = ReadLE32(chunk + 24), w7 = ReadLE32(chunk + 28); 60 | uint32_t w8 = ReadLE32(chunk + 32), w9 = ReadLE32(chunk + 36), w10 = ReadLE32(chunk + 40), w11 = ReadLE32(chunk + 44); 61 | uint32_t w12 = ReadLE32(chunk + 48), w13 = ReadLE32(chunk + 52), w14 = ReadLE32(chunk + 56), w15 = ReadLE32(chunk + 60); 62 | 63 | R11(a1, b1, c1, d1, e1, w0, 11); 64 | R12(a2, b2, c2, d2, e2, w5, 8); 65 | R11(e1, a1, b1, c1, d1, w1, 14); 66 | R12(e2, a2, b2, c2, d2, w14, 9); 67 | R11(d1, e1, a1, b1, c1, w2, 15); 68 | R12(d2, e2, a2, b2, c2, w7, 9); 69 | R11(c1, d1, e1, a1, b1, w3, 12); 70 | R12(c2, d2, e2, a2, b2, w0, 11); 71 | R11(b1, c1, d1, e1, a1, w4, 5); 72 | R12(b2, c2, d2, e2, a2, w9, 13); 73 | R11(a1, b1, c1, d1, e1, w5, 8); 74 | R12(a2, b2, c2, d2, e2, w2, 15); 75 | R11(e1, a1, b1, c1, d1, w6, 7); 76 | R12(e2, a2, b2, c2, d2, w11, 15); 77 | R11(d1, e1, a1, b1, c1, w7, 9); 78 | R12(d2, e2, a2, b2, c2, w4, 5); 79 | R11(c1, d1, e1, a1, b1, w8, 11); 80 | R12(c2, d2, e2, a2, b2, w13, 7); 81 | R11(b1, c1, d1, e1, a1, w9, 13); 82 | R12(b2, c2, d2, e2, a2, w6, 7); 83 | R11(a1, b1, c1, d1, e1, w10, 14); 84 | R12(a2, b2, c2, d2, e2, w15, 8); 85 | R11(e1, a1, b1, c1, d1, w11, 15); 86 | R12(e2, a2, b2, c2, d2, w8, 11); 87 | R11(d1, e1, a1, b1, c1, w12, 6); 88 | R12(d2, e2, a2, b2, c2, w1, 14); 89 | R11(c1, d1, e1, a1, b1, w13, 7); 90 | R12(c2, d2, e2, a2, b2, w10, 14); 91 | R11(b1, c1, d1, e1, a1, w14, 9); 92 | R12(b2, c2, d2, e2, a2, w3, 12); 93 | R11(a1, b1, c1, d1, e1, w15, 8); 94 | R12(a2, b2, c2, d2, e2, w12, 6); 95 | 96 | R21(e1, a1, b1, c1, d1, w7, 7); 97 | R22(e2, a2, b2, c2, d2, w6, 9); 98 | R21(d1, e1, a1, b1, c1, w4, 6); 99 | R22(d2, e2, a2, b2, c2, w11, 13); 100 | R21(c1, d1, e1, a1, b1, w13, 8); 101 | R22(c2, d2, e2, a2, b2, w3, 15); 102 | R21(b1, c1, d1, e1, a1, w1, 13); 103 | R22(b2, c2, d2, e2, a2, w7, 7); 104 | R21(a1, b1, c1, d1, e1, w10, 11); 105 | R22(a2, b2, c2, d2, e2, w0, 12); 106 | R21(e1, a1, b1, c1, d1, w6, 9); 107 | R22(e2, a2, b2, c2, d2, w13, 8); 108 | R21(d1, e1, a1, b1, c1, w15, 7); 109 | R22(d2, e2, a2, b2, c2, w5, 9); 110 | R21(c1, d1, e1, a1, b1, w3, 15); 111 | R22(c2, d2, e2, a2, b2, w10, 11); 112 | R21(b1, c1, d1, e1, a1, w12, 7); 113 | R22(b2, c2, d2, e2, a2, w14, 7); 114 | R21(a1, b1, c1, d1, e1, w0, 12); 115 | R22(a2, b2, c2, d2, e2, w15, 7); 116 | R21(e1, a1, b1, c1, d1, w9, 15); 117 | R22(e2, a2, b2, c2, d2, w8, 12); 118 | R21(d1, e1, a1, b1, c1, w5, 9); 119 | R22(d2, e2, a2, b2, c2, w12, 7); 120 | R21(c1, d1, e1, a1, b1, w2, 11); 121 | R22(c2, d2, e2, a2, b2, w4, 6); 122 | R21(b1, c1, d1, e1, a1, w14, 7); 123 | R22(b2, c2, d2, e2, a2, w9, 15); 124 | R21(a1, b1, c1, d1, e1, w11, 13); 125 | R22(a2, b2, c2, d2, e2, w1, 13); 126 | R21(e1, a1, b1, c1, d1, w8, 12); 127 | R22(e2, a2, b2, c2, d2, w2, 11); 128 | 129 | R31(d1, e1, a1, b1, c1, w3, 11); 130 | R32(d2, e2, a2, b2, c2, w15, 9); 131 | R31(c1, d1, e1, a1, b1, w10, 13); 132 | R32(c2, d2, e2, a2, b2, w5, 7); 133 | R31(b1, c1, d1, e1, a1, w14, 6); 134 | R32(b2, c2, d2, e2, a2, w1, 15); 135 | R31(a1, b1, c1, d1, e1, w4, 7); 136 | R32(a2, b2, c2, d2, e2, w3, 11); 137 | R31(e1, a1, b1, c1, d1, w9, 14); 138 | R32(e2, a2, b2, c2, d2, w7, 8); 139 | R31(d1, e1, a1, b1, c1, w15, 9); 140 | R32(d2, e2, a2, b2, c2, w14, 6); 141 | R31(c1, d1, e1, a1, b1, w8, 13); 142 | R32(c2, d2, e2, a2, b2, w6, 6); 143 | R31(b1, c1, d1, e1, a1, w1, 15); 144 | R32(b2, c2, d2, e2, a2, w9, 14); 145 | R31(a1, b1, c1, d1, e1, w2, 14); 146 | R32(a2, b2, c2, d2, e2, w11, 12); 147 | R31(e1, a1, b1, c1, d1, w7, 8); 148 | R32(e2, a2, b2, c2, d2, w8, 13); 149 | R31(d1, e1, a1, b1, c1, w0, 13); 150 | R32(d2, e2, a2, b2, c2, w12, 5); 151 | R31(c1, d1, e1, a1, b1, w6, 6); 152 | R32(c2, d2, e2, a2, b2, w2, 14); 153 | R31(b1, c1, d1, e1, a1, w13, 5); 154 | R32(b2, c2, d2, e2, a2, w10, 13); 155 | R31(a1, b1, c1, d1, e1, w11, 12); 156 | R32(a2, b2, c2, d2, e2, w0, 13); 157 | R31(e1, a1, b1, c1, d1, w5, 7); 158 | R32(e2, a2, b2, c2, d2, w4, 7); 159 | R31(d1, e1, a1, b1, c1, w12, 5); 160 | R32(d2, e2, a2, b2, c2, w13, 5); 161 | 162 | R41(c1, d1, e1, a1, b1, w1, 11); 163 | R42(c2, d2, e2, a2, b2, w8, 15); 164 | R41(b1, c1, d1, e1, a1, w9, 12); 165 | R42(b2, c2, d2, e2, a2, w6, 5); 166 | R41(a1, b1, c1, d1, e1, w11, 14); 167 | R42(a2, b2, c2, d2, e2, w4, 8); 168 | R41(e1, a1, b1, c1, d1, w10, 15); 169 | R42(e2, a2, b2, c2, d2, w1, 11); 170 | R41(d1, e1, a1, b1, c1, w0, 14); 171 | R42(d2, e2, a2, b2, c2, w3, 14); 172 | R41(c1, d1, e1, a1, b1, w8, 15); 173 | R42(c2, d2, e2, a2, b2, w11, 14); 174 | R41(b1, c1, d1, e1, a1, w12, 9); 175 | R42(b2, c2, d2, e2, a2, w15, 6); 176 | R41(a1, b1, c1, d1, e1, w4, 8); 177 | R42(a2, b2, c2, d2, e2, w0, 14); 178 | R41(e1, a1, b1, c1, d1, w13, 9); 179 | R42(e2, a2, b2, c2, d2, w5, 6); 180 | R41(d1, e1, a1, b1, c1, w3, 14); 181 | R42(d2, e2, a2, b2, c2, w12, 9); 182 | R41(c1, d1, e1, a1, b1, w7, 5); 183 | R42(c2, d2, e2, a2, b2, w2, 12); 184 | R41(b1, c1, d1, e1, a1, w15, 6); 185 | R42(b2, c2, d2, e2, a2, w13, 9); 186 | R41(a1, b1, c1, d1, e1, w14, 8); 187 | R42(a2, b2, c2, d2, e2, w9, 12); 188 | R41(e1, a1, b1, c1, d1, w5, 6); 189 | R42(e2, a2, b2, c2, d2, w7, 5); 190 | R41(d1, e1, a1, b1, c1, w6, 5); 191 | R42(d2, e2, a2, b2, c2, w10, 15); 192 | R41(c1, d1, e1, a1, b1, w2, 12); 193 | R42(c2, d2, e2, a2, b2, w14, 8); 194 | 195 | R51(b1, c1, d1, e1, a1, w4, 9); 196 | R52(b2, c2, d2, e2, a2, w12, 8); 197 | R51(a1, b1, c1, d1, e1, w0, 15); 198 | R52(a2, b2, c2, d2, e2, w15, 5); 199 | R51(e1, a1, b1, c1, d1, w5, 5); 200 | R52(e2, a2, b2, c2, d2, w10, 12); 201 | R51(d1, e1, a1, b1, c1, w9, 11); 202 | R52(d2, e2, a2, b2, c2, w4, 9); 203 | R51(c1, d1, e1, a1, b1, w7, 6); 204 | R52(c2, d2, e2, a2, b2, w1, 12); 205 | R51(b1, c1, d1, e1, a1, w12, 8); 206 | R52(b2, c2, d2, e2, a2, w5, 5); 207 | R51(a1, b1, c1, d1, e1, w2, 13); 208 | R52(a2, b2, c2, d2, e2, w8, 14); 209 | R51(e1, a1, b1, c1, d1, w10, 12); 210 | R52(e2, a2, b2, c2, d2, w7, 6); 211 | R51(d1, e1, a1, b1, c1, w14, 5); 212 | R52(d2, e2, a2, b2, c2, w6, 8); 213 | R51(c1, d1, e1, a1, b1, w1, 12); 214 | R52(c2, d2, e2, a2, b2, w2, 13); 215 | R51(b1, c1, d1, e1, a1, w3, 13); 216 | R52(b2, c2, d2, e2, a2, w13, 6); 217 | R51(a1, b1, c1, d1, e1, w8, 14); 218 | R52(a2, b2, c2, d2, e2, w14, 5); 219 | R51(e1, a1, b1, c1, d1, w11, 11); 220 | R52(e2, a2, b2, c2, d2, w0, 15); 221 | R51(d1, e1, a1, b1, c1, w6, 8); 222 | R52(d2, e2, a2, b2, c2, w3, 13); 223 | R51(c1, d1, e1, a1, b1, w15, 5); 224 | R52(c2, d2, e2, a2, b2, w9, 11); 225 | R51(b1, c1, d1, e1, a1, w13, 6); 226 | R52(b2, c2, d2, e2, a2, w11, 11); 227 | 228 | uint32_t t = s[0]; 229 | s[0] = s[1] + c1 + d2; 230 | s[1] = s[2] + d1 + e2; 231 | s[2] = s[3] + e1 + a2; 232 | s[3] = s[4] + a1 + b2; 233 | s[4] = t + b1 + c2; 234 | } 235 | 236 | } // namespace ripemd160 237 | 238 | } // namespace 239 | 240 | ////// RIPEMD160 241 | 242 | CRIPEMD160::CRIPEMD160() : bytes(0) 243 | { 244 | ripemd160::Initialize(s); 245 | } 246 | 247 | CRIPEMD160& CRIPEMD160::Write(const unsigned char* data, size_t len) 248 | { 249 | const unsigned char* end = data + len; 250 | size_t bufsize = bytes % 64; 251 | if (bufsize && bufsize + len >= 64) { 252 | // Fill the buffer, and process it. 253 | memcpy(buf + bufsize, data, 64 - bufsize); 254 | bytes += 64 - bufsize; 255 | data += 64 - bufsize; 256 | ripemd160::Transform(s, buf); 257 | bufsize = 0; 258 | } 259 | while (end - data >= 64) { 260 | // Process full chunks directly from the source. 261 | ripemd160::Transform(s, data); 262 | bytes += 64; 263 | data += 64; 264 | } 265 | if (end > data) { 266 | // Fill the buffer with what remains. 267 | memcpy(buf + bufsize, data, end - data); 268 | bytes += end - data; 269 | } 270 | return *this; 271 | } 272 | 273 | void CRIPEMD160::Finalize(unsigned char hash[OUTPUT_SIZE]) 274 | { 275 | static const unsigned char pad[64] = {0x80}; 276 | unsigned char sizedesc[8]; 277 | WriteLE64(sizedesc, bytes << 3); 278 | Write(pad, 1 + ((119 - (bytes % 64)) % 64)); 279 | Write(sizedesc, 8); 280 | WriteLE32(hash, s[0]); 281 | WriteLE32(hash + 4, s[1]); 282 | WriteLE32(hash + 8, s[2]); 283 | WriteLE32(hash + 12, s[3]); 284 | WriteLE32(hash + 16, s[4]); 285 | } 286 | 287 | CRIPEMD160& CRIPEMD160::Reset() 288 | { 289 | bytes = 0; 290 | ripemd160::Initialize(s); 291 | return *this; 292 | } 293 | -------------------------------------------------------------------------------- /src/clone/pubkey.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2009-2019 The Bitcoin Core developers 2 | // Copyright (c) 2017 The Zcash developers 3 | // Distributed under the MIT software license, see the accompanying 4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 | 6 | #include 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | namespace 13 | { 14 | /* Global secp256k1_context object used for verification. */ 15 | secp256k1_context* secp256k1_context_verify = nullptr; 16 | } // namespace 17 | 18 | /** This function is taken from the libsecp256k1 distribution and implements 19 | * DER parsing for ECDSA signatures, while supporting an arbitrary subset of 20 | * format violations. 21 | * 22 | * Supported violations include negative integers, excessive padding, garbage 23 | * at the end, and overly long length descriptors. This is safe to use in 24 | * Bitcoin because since the activation of BIP66, signatures are verified to be 25 | * strict DER before being passed to this module, and we know it supports all 26 | * violations present in the blockchain before that point. 27 | */ 28 | int ecdsa_signature_parse_der_lax(const secp256k1_context* ctx, secp256k1_ecdsa_signature* sig, const unsigned char *input, size_t inputlen) { 29 | size_t rpos, rlen, spos, slen; 30 | size_t pos = 0; 31 | size_t lenbyte; 32 | unsigned char tmpsig[64] = {0}; 33 | int overflow = 0; 34 | 35 | /* Hack to initialize sig with a correctly-parsed but invalid signature. */ 36 | secp256k1_ecdsa_signature_parse_compact(ctx, sig, tmpsig); 37 | 38 | /* Sequence tag byte */ 39 | if (pos == inputlen || input[pos] != 0x30) { 40 | return 0; 41 | } 42 | pos++; 43 | 44 | /* Sequence length bytes */ 45 | if (pos == inputlen) { 46 | return 0; 47 | } 48 | lenbyte = input[pos++]; 49 | if (lenbyte & 0x80) { 50 | lenbyte -= 0x80; 51 | if (lenbyte > inputlen - pos) { 52 | return 0; 53 | } 54 | pos += lenbyte; 55 | } 56 | 57 | /* Integer tag byte for R */ 58 | if (pos == inputlen || input[pos] != 0x02) { 59 | return 0; 60 | } 61 | pos++; 62 | 63 | /* Integer length for R */ 64 | if (pos == inputlen) { 65 | return 0; 66 | } 67 | lenbyte = input[pos++]; 68 | if (lenbyte & 0x80) { 69 | lenbyte -= 0x80; 70 | if (lenbyte > inputlen - pos) { 71 | return 0; 72 | } 73 | while (lenbyte > 0 && input[pos] == 0) { 74 | pos++; 75 | lenbyte--; 76 | } 77 | static_assert(sizeof(size_t) >= 4, "size_t too small"); 78 | if (lenbyte >= 4) { 79 | return 0; 80 | } 81 | rlen = 0; 82 | while (lenbyte > 0) { 83 | rlen = (rlen << 8) + input[pos]; 84 | pos++; 85 | lenbyte--; 86 | } 87 | } else { 88 | rlen = lenbyte; 89 | } 90 | if (rlen > inputlen - pos) { 91 | return 0; 92 | } 93 | rpos = pos; 94 | pos += rlen; 95 | 96 | /* Integer tag byte for S */ 97 | if (pos == inputlen || input[pos] != 0x02) { 98 | return 0; 99 | } 100 | pos++; 101 | 102 | /* Integer length for S */ 103 | if (pos == inputlen) { 104 | return 0; 105 | } 106 | lenbyte = input[pos++]; 107 | if (lenbyte & 0x80) { 108 | lenbyte -= 0x80; 109 | if (lenbyte > inputlen - pos) { 110 | return 0; 111 | } 112 | while (lenbyte > 0 && input[pos] == 0) { 113 | pos++; 114 | lenbyte--; 115 | } 116 | static_assert(sizeof(size_t) >= 4, "size_t too small"); 117 | if (lenbyte >= 4) { 118 | return 0; 119 | } 120 | slen = 0; 121 | while (lenbyte > 0) { 122 | slen = (slen << 8) + input[pos]; 123 | pos++; 124 | lenbyte--; 125 | } 126 | } else { 127 | slen = lenbyte; 128 | } 129 | if (slen > inputlen - pos) { 130 | return 0; 131 | } 132 | spos = pos; 133 | 134 | /* Ignore leading zeroes in R */ 135 | while (rlen > 0 && input[rpos] == 0) { 136 | rlen--; 137 | rpos++; 138 | } 139 | /* Copy R value */ 140 | if (rlen > 32) { 141 | overflow = 1; 142 | } else { 143 | memcpy(tmpsig + 32 - rlen, input + rpos, rlen); 144 | } 145 | 146 | /* Ignore leading zeroes in S */ 147 | while (slen > 0 && input[spos] == 0) { 148 | slen--; 149 | spos++; 150 | } 151 | /* Copy S value */ 152 | if (slen > 32) { 153 | overflow = 1; 154 | } else { 155 | memcpy(tmpsig + 64 - slen, input + spos, slen); 156 | } 157 | 158 | if (!overflow) { 159 | overflow = !secp256k1_ecdsa_signature_parse_compact(ctx, sig, tmpsig); 160 | } 161 | if (overflow) { 162 | /* Overwrite the result again with a correctly-parsed but invalid 163 | signature if parsing failed. */ 164 | memset(tmpsig, 0, 64); 165 | secp256k1_ecdsa_signature_parse_compact(ctx, sig, tmpsig); 166 | } 167 | return 1; 168 | } 169 | 170 | XOnlyPubKey::XOnlyPubKey(Span bytes) 171 | { 172 | assert(bytes.size() == 32); 173 | std::copy(bytes.begin(), bytes.end(), m_keydata.begin()); 174 | } 175 | 176 | bool XOnlyPubKey::VerifySchnorr(const uint256& msg, Span sigbytes) const 177 | { 178 | assert(sigbytes.size() == 64); 179 | secp256k1_xonly_pubkey pubkey; 180 | if (!secp256k1_xonly_pubkey_parse(secp256k1_context_verify, &pubkey, m_keydata.data())) return false; 181 | return secp256k1_schnorrsig_verify(secp256k1_context_verify, sigbytes.data(), msg.begin(), sizeof(msg), &pubkey); 182 | } 183 | 184 | bool XOnlyPubKey::CheckPayToContract(const XOnlyPubKey& base, const uint256& hash, bool parity) const 185 | { 186 | secp256k1_xonly_pubkey base_point; 187 | if (!secp256k1_xonly_pubkey_parse(secp256k1_context_verify, &base_point, base.data())) return false; 188 | return secp256k1_xonly_pubkey_tweak_add_check(secp256k1_context_verify, m_keydata.begin(), parity, &base_point, hash.begin()); 189 | } 190 | 191 | bool CPubKey::Verify(const uint256 &hash, const std::vector& vchSig) const { 192 | if (!IsValid()) 193 | return false; 194 | secp256k1_pubkey pubkey; 195 | secp256k1_ecdsa_signature sig; 196 | assert(secp256k1_context_verify && "secp256k1_context_verify must be initialized to use CPubKey."); 197 | if (!secp256k1_ec_pubkey_parse(secp256k1_context_verify, &pubkey, vch, size())) { 198 | return false; 199 | } 200 | if (!ecdsa_signature_parse_der_lax(secp256k1_context_verify, &sig, vchSig.data(), vchSig.size())) { 201 | return false; 202 | } 203 | /* libsecp256k1's ECDSA verification requires lower-S signatures, which have 204 | * not historically been enforced in Bitcoin, so normalize them first. */ 205 | secp256k1_ecdsa_signature_normalize(secp256k1_context_verify, &sig, &sig); 206 | return secp256k1_ecdsa_verify(secp256k1_context_verify, &sig, hash.begin(), &pubkey); 207 | } 208 | 209 | bool CPubKey::RecoverCompact(const uint256 &hash, const std::vector& vchSig) { 210 | if (vchSig.size() != COMPACT_SIGNATURE_SIZE) 211 | return false; 212 | int recid = (vchSig[0] - 27) & 3; 213 | bool fComp = ((vchSig[0] - 27) & 4) != 0; 214 | secp256k1_pubkey pubkey; 215 | secp256k1_ecdsa_recoverable_signature sig; 216 | assert(secp256k1_context_verify && "secp256k1_context_verify must be initialized to use CPubKey."); 217 | if (!secp256k1_ecdsa_recoverable_signature_parse_compact(secp256k1_context_verify, &sig, &vchSig[1], recid)) { 218 | return false; 219 | } 220 | if (!secp256k1_ecdsa_recover(secp256k1_context_verify, &pubkey, &sig, hash.begin())) { 221 | return false; 222 | } 223 | unsigned char pub[SIZE]; 224 | size_t publen = SIZE; 225 | secp256k1_ec_pubkey_serialize(secp256k1_context_verify, pub, &publen, &pubkey, fComp ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED); 226 | Set(pub, pub + publen); 227 | return true; 228 | } 229 | 230 | bool CPubKey::IsFullyValid() const { 231 | if (!IsValid()) 232 | return false; 233 | secp256k1_pubkey pubkey; 234 | assert(secp256k1_context_verify && "secp256k1_context_verify must be initialized to use CPubKey."); 235 | return secp256k1_ec_pubkey_parse(secp256k1_context_verify, &pubkey, vch, size()); 236 | } 237 | 238 | bool CPubKey::Decompress() { 239 | if (!IsValid()) 240 | return false; 241 | secp256k1_pubkey pubkey; 242 | assert(secp256k1_context_verify && "secp256k1_context_verify must be initialized to use CPubKey."); 243 | if (!secp256k1_ec_pubkey_parse(secp256k1_context_verify, &pubkey, vch, size())) { 244 | return false; 245 | } 246 | unsigned char pub[SIZE]; 247 | size_t publen = SIZE; 248 | secp256k1_ec_pubkey_serialize(secp256k1_context_verify, pub, &publen, &pubkey, SECP256K1_EC_UNCOMPRESSED); 249 | Set(pub, pub + publen); 250 | return true; 251 | } 252 | 253 | bool CPubKey::Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const { 254 | assert(IsValid()); 255 | assert((nChild >> 31) == 0); 256 | assert(size() == COMPRESSED_SIZE); 257 | unsigned char out[64]; 258 | BIP32Hash(cc, nChild, *begin(), begin()+1, out); 259 | memcpy(ccChild.begin(), out+32, 32); 260 | secp256k1_pubkey pubkey; 261 | assert(secp256k1_context_verify && "secp256k1_context_verify must be initialized to use CPubKey."); 262 | if (!secp256k1_ec_pubkey_parse(secp256k1_context_verify, &pubkey, vch, size())) { 263 | return false; 264 | } 265 | if (!secp256k1_ec_pubkey_tweak_add(secp256k1_context_verify, &pubkey, out)) { 266 | return false; 267 | } 268 | unsigned char pub[COMPRESSED_SIZE]; 269 | size_t publen = COMPRESSED_SIZE; 270 | secp256k1_ec_pubkey_serialize(secp256k1_context_verify, pub, &publen, &pubkey, SECP256K1_EC_COMPRESSED); 271 | pubkeyChild.Set(pub, pub + publen); 272 | return true; 273 | } 274 | 275 | void CExtPubKey::Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const { 276 | code[0] = nDepth; 277 | memcpy(code+1, vchFingerprint, 4); 278 | code[5] = (nChild >> 24) & 0xFF; code[6] = (nChild >> 16) & 0xFF; 279 | code[7] = (nChild >> 8) & 0xFF; code[8] = (nChild >> 0) & 0xFF; 280 | memcpy(code+9, chaincode.begin(), 32); 281 | assert(pubkey.size() == CPubKey::COMPRESSED_SIZE); 282 | memcpy(code+41, pubkey.begin(), CPubKey::COMPRESSED_SIZE); 283 | } 284 | 285 | void CExtPubKey::Decode(const unsigned char code[BIP32_EXTKEY_SIZE]) { 286 | nDepth = code[0]; 287 | memcpy(vchFingerprint, code+1, 4); 288 | nChild = (code[5] << 24) | (code[6] << 16) | (code[7] << 8) | code[8]; 289 | memcpy(chaincode.begin(), code+9, 32); 290 | pubkey.Set(code+41, code+BIP32_EXTKEY_SIZE); 291 | } 292 | 293 | bool CExtPubKey::Derive(CExtPubKey &out, unsigned int _nChild) const { 294 | out.nDepth = nDepth + 1; 295 | CKeyID id = pubkey.GetID(); 296 | memcpy(&out.vchFingerprint[0], &id, 4); 297 | out.nChild = _nChild; 298 | return pubkey.Derive(out.pubkey, out.chaincode, _nChild, chaincode); 299 | } 300 | 301 | /* static */ bool CPubKey::CheckLowS(const std::vector& vchSig) { 302 | secp256k1_ecdsa_signature sig; 303 | assert(secp256k1_context_verify && "secp256k1_context_verify must be initialized to use CPubKey."); 304 | if (!ecdsa_signature_parse_der_lax(secp256k1_context_verify, &sig, vchSig.data(), vchSig.size())) { 305 | return false; 306 | } 307 | return (!secp256k1_ecdsa_signature_normalize(secp256k1_context_verify, nullptr, &sig)); 308 | } 309 | 310 | /* static */ int ECCVerifyHandle::refcount = 0; 311 | 312 | ECCVerifyHandle::ECCVerifyHandle() 313 | { 314 | if (refcount == 0) { 315 | assert(secp256k1_context_verify == nullptr); 316 | secp256k1_context_verify = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY); 317 | assert(secp256k1_context_verify != nullptr); 318 | } 319 | refcount++; 320 | } 321 | 322 | ECCVerifyHandle::~ECCVerifyHandle() 323 | { 324 | refcount--; 325 | if (refcount == 0) { 326 | assert(secp256k1_context_verify != nullptr); 327 | secp256k1_context_destroy(secp256k1_context_verify); 328 | secp256k1_context_verify = nullptr; 329 | } 330 | } 331 | --------------------------------------------------------------------------------