repo_id
stringlengths
0
42
file_path
stringlengths
15
97
content
stringlengths
2
2.41M
__index_level_0__
int64
0
0
bitcoin/src
bitcoin/src/bench/ellswift.cpp
// Copyright (c) 2022-2023 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include <bench/bench.h> #include <key.h> #include <random.h> static void EllSwiftCreate(benchmark::Bench& bench) { ECC_Start(); CKey key; key.MakeNewKey(true); uint256 entropy = GetRandHash(); bench.batch(1).unit("pubkey").run([&] { auto ret = key.EllSwiftCreate(MakeByteSpan(entropy)); /* Use the first 32 bytes of the ellswift encoded public key as next private key. */ key.Set(UCharCast(ret.data()), UCharCast(ret.data()) + 32, true); assert(key.IsValid()); /* Use the last 32 bytes of the ellswift encoded public key as next entropy. */ std::copy(ret.begin() + 32, ret.begin() + 64, MakeWritableByteSpan(entropy).begin()); }); ECC_Stop(); } BENCHMARK(EllSwiftCreate, benchmark::PriorityLevel::HIGH);
0
bitcoin/src
bitcoin/src/bench/strencodings.cpp
// Copyright (c) 2022 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include <bench/bench.h> #include <bench/data.h> #include <util/strencodings.h> static void HexStrBench(benchmark::Bench& bench) { auto const& data = benchmark::data::block413567; bench.batch(data.size()).unit("byte").run([&] { auto hex = HexStr(data); ankerl::nanobench::doNotOptimizeAway(hex); }); } BENCHMARK(HexStrBench, benchmark::PriorityLevel::HIGH);
0
bitcoin/src
bitcoin/src/bench/examples.cpp
// Copyright (c) 2015-2022 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include <bench/bench.h> // Extremely fast-running benchmark: #include <math.h> volatile double sum = 0.0; // volatile, global so not optimized away static void Trig(benchmark::Bench& bench) { double d = 0.01; bench.run([&] { sum = sum + sin(d); d += 0.000001; }); } BENCHMARK(Trig, benchmark::PriorityLevel::HIGH);
0
bitcoin/src
bitcoin/src/bench/bech32.cpp
// Copyright (c) 2018-2022 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include <bench/bench.h> #include <bech32.h> #include <util/strencodings.h> #include <string> #include <vector> static void Bech32Encode(benchmark::Bench& bench) { std::vector<uint8_t> v = ParseHex("c97f5a67ec381b760aeaf67573bc164845ff39a3bb26a1cee401ac67243b48db"); std::vector<unsigned char> tmp = {0}; tmp.reserve(1 + 32 * 8 / 5); ConvertBits<8, 5, true>([&](unsigned char c) { tmp.push_back(c); }, v.begin(), v.end()); bench.batch(v.size()).unit("byte").run([&] { bech32::Encode(bech32::Encoding::BECH32, "bc", tmp); }); } static void Bech32Decode(benchmark::Bench& bench) { std::string addr = "bc1qkallence7tjawwvy0dwt4twc62qjgaw8f4vlhyd006d99f09"; bench.batch(addr.size()).unit("byte").run([&] { bech32::Decode(addr); }); } BENCHMARK(Bech32Encode, benchmark::PriorityLevel::HIGH); BENCHMARK(Bech32Decode, benchmark::PriorityLevel::HIGH);
0
bitcoin/src
bitcoin/src/bench/xor.cpp
// Copyright (c) The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or https://opensource.org/license/mit/. #include <bench/bench.h> #include <random.h> #include <streams.h> #include <cstddef> #include <vector> static void Xor(benchmark::Bench& bench) { FastRandomContext frc{/*fDeterministic=*/true}; auto data{frc.randbytes<std::byte>(1024)}; auto key{frc.randbytes<std::byte>(31)}; bench.batch(data.size()).unit("byte").run([&] { util::Xor(data, key); }); } BENCHMARK(Xor, benchmark::PriorityLevel::HIGH);
0
bitcoin/src
bitcoin/src/bench/chacha20.cpp
// Copyright (c) 2019-2022 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include <bench/bench.h> #include <crypto/chacha20.h> #include <crypto/chacha20poly1305.h> /* Number of bytes to process per iteration */ static const uint64_t BUFFER_SIZE_TINY = 64; static const uint64_t BUFFER_SIZE_SMALL = 256; static const uint64_t BUFFER_SIZE_LARGE = 1024*1024; static void CHACHA20(benchmark::Bench& bench, size_t buffersize) { std::vector<std::byte> key(32, {}); ChaCha20 ctx(key); ctx.Seek({0, 0}, 0); std::vector<std::byte> in(buffersize, {}); std::vector<std::byte> out(buffersize, {}); bench.batch(in.size()).unit("byte").run([&] { ctx.Crypt(in, out); }); } static void FSCHACHA20POLY1305(benchmark::Bench& bench, size_t buffersize) { std::vector<std::byte> key(32); FSChaCha20Poly1305 ctx(key, 224); std::vector<std::byte> in(buffersize); std::vector<std::byte> aad; std::vector<std::byte> out(buffersize + FSChaCha20Poly1305::EXPANSION); bench.batch(in.size()).unit("byte").run([&] { ctx.Encrypt(in, aad, out); }); } static void CHACHA20_64BYTES(benchmark::Bench& bench) { CHACHA20(bench, BUFFER_SIZE_TINY); } static void CHACHA20_256BYTES(benchmark::Bench& bench) { CHACHA20(bench, BUFFER_SIZE_SMALL); } static void CHACHA20_1MB(benchmark::Bench& bench) { CHACHA20(bench, BUFFER_SIZE_LARGE); } static void FSCHACHA20POLY1305_64BYTES(benchmark::Bench& bench) { FSCHACHA20POLY1305(bench, BUFFER_SIZE_TINY); } static void FSCHACHA20POLY1305_256BYTES(benchmark::Bench& bench) { FSCHACHA20POLY1305(bench, BUFFER_SIZE_SMALL); } static void FSCHACHA20POLY1305_1MB(benchmark::Bench& bench) { FSCHACHA20POLY1305(bench, BUFFER_SIZE_LARGE); } BENCHMARK(CHACHA20_64BYTES, benchmark::PriorityLevel::HIGH); BENCHMARK(CHACHA20_256BYTES, benchmark::PriorityLevel::HIGH); BENCHMARK(CHACHA20_1MB, benchmark::PriorityLevel::HIGH); BENCHMARK(FSCHACHA20POLY1305_64BYTES, benchmark::PriorityLevel::HIGH); BENCHMARK(FSCHACHA20POLY1305_256BYTES, benchmark::PriorityLevel::HIGH); BENCHMARK(FSCHACHA20POLY1305_1MB, benchmark::PriorityLevel::HIGH);
0
bitcoin/src
bitcoin/src/bench/wallet_create.cpp
// Copyright (c) 2023-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or https://www.opensource.org/licenses/mit-license.php. #include <bench/bench.h> #include <node/context.h> #include <random.h> #include <test/util/setup_common.h> #include <wallet/context.h> #include <wallet/wallet.h> namespace wallet { static void WalletCreate(benchmark::Bench& bench, bool encrypted) { auto test_setup = MakeNoLogFileContext<TestingSetup>(); FastRandomContext random; WalletContext context; context.args = &test_setup->m_args; context.chain = test_setup->m_node.chain.get(); DatabaseOptions options; options.require_format = DatabaseFormat::SQLITE; options.require_create = true; options.create_flags = WALLET_FLAG_DESCRIPTORS; if (encrypted) { options.create_passphrase = random.rand256().ToString(); } DatabaseStatus status; bilingual_str error_string; std::vector<bilingual_str> warnings; fs::path wallet_path = test_setup->m_path_root / strprintf("test_wallet_%d", random.rand32()).c_str(); bench.run([&] { auto wallet = CreateWallet(context, wallet_path.utf8string(), /*load_on_start=*/std::nullopt, options, status, error_string, warnings); assert(status == DatabaseStatus::SUCCESS); assert(wallet != nullptr); // Cleanup wallet.reset(); fs::remove_all(wallet_path); }); } static void WalletCreatePlain(benchmark::Bench& bench) { WalletCreate(bench, /*encrypted=*/false); } static void WalletCreateEncrypted(benchmark::Bench& bench) { WalletCreate(bench, /*encrypted=*/true); } #ifdef USE_SQLITE BENCHMARK(WalletCreatePlain, benchmark::PriorityLevel::LOW); BENCHMARK(WalletCreateEncrypted, benchmark::PriorityLevel::LOW); #endif } // namespace wallet
0
bitcoin/src
bitcoin/src/bench/nanobench.cpp
// Copyright (c) 2019-2020 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #define ANKERL_NANOBENCH_IMPLEMENT #include <bench/nanobench.h>
0
bitcoin/src
bitcoin/src/bench/pool.cpp
// Copyright (c) 2022 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include <bench/bench.h> #include <support/allocators/pool.h> #include <unordered_map> template <typename Map> void BenchFillClearMap(benchmark::Bench& bench, Map& map) { size_t batch_size = 5000; // make sure each iteration of the benchmark contains exactly 5000 inserts and one clear. // do this at least 10 times so we get reasonable accurate results bench.batch(batch_size).minEpochIterations(10).run([&] { auto rng = ankerl::nanobench::Rng(1234); for (size_t i = 0; i < batch_size; ++i) { map[rng()]; } map.clear(); }); } static void PoolAllocator_StdUnorderedMap(benchmark::Bench& bench) { auto map = std::unordered_map<uint64_t, uint64_t>(); BenchFillClearMap(bench, map); } static void PoolAllocator_StdUnorderedMapWithPoolResource(benchmark::Bench& bench) { using Map = std::unordered_map<uint64_t, uint64_t, std::hash<uint64_t>, std::equal_to<uint64_t>, PoolAllocator<std::pair<const uint64_t, uint64_t>, sizeof(std::pair<const uint64_t, uint64_t>) + 4 * sizeof(void*)>>; // make sure the resource supports large enough pools to hold the node. We do this by adding the size of a few pointers to it. auto pool_resource = Map::allocator_type::ResourceType(); auto map = Map{0, std::hash<uint64_t>{}, std::equal_to<uint64_t>{}, &pool_resource}; BenchFillClearMap(bench, map); } BENCHMARK(PoolAllocator_StdUnorderedMap, benchmark::PriorityLevel::HIGH); BENCHMARK(PoolAllocator_StdUnorderedMapWithPoolResource, benchmark::PriorityLevel::HIGH);
0
bitcoin/src
bitcoin/src/bench/rpc_blockchain.cpp
// Copyright (c) 2016-2022 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include <bench/bench.h> #include <bench/data.h> #include <rpc/blockchain.h> #include <streams.h> #include <test/util/setup_common.h> #include <util/chaintype.h> #include <validation.h> #include <univalue.h> namespace { struct TestBlockAndIndex { const std::unique_ptr<const TestingSetup> testing_setup{MakeNoLogFileContext<const TestingSetup>(ChainType::MAIN)}; CBlock block{}; uint256 blockHash{}; CBlockIndex blockindex{}; TestBlockAndIndex() { DataStream stream{benchmark::data::block413567}; std::byte a{0}; stream.write({&a, 1}); // Prevent compaction stream >> TX_WITH_WITNESS(block); blockHash = block.GetHash(); blockindex.phashBlock = &blockHash; blockindex.nBits = 403014710; } }; } // namespace static void BlockToJsonVerbose(benchmark::Bench& bench) { TestBlockAndIndex data; bench.run([&] { auto univalue = blockToJSON(data.testing_setup->m_node.chainman->m_blockman, data.block, data.blockindex, data.blockindex, TxVerbosity::SHOW_DETAILS_AND_PREVOUT); ankerl::nanobench::doNotOptimizeAway(univalue); }); } BENCHMARK(BlockToJsonVerbose, benchmark::PriorityLevel::HIGH); static void BlockToJsonVerboseWrite(benchmark::Bench& bench) { TestBlockAndIndex data; auto univalue = blockToJSON(data.testing_setup->m_node.chainman->m_blockman, data.block, data.blockindex, data.blockindex, TxVerbosity::SHOW_DETAILS_AND_PREVOUT); bench.run([&] { auto str = univalue.write(); ankerl::nanobench::doNotOptimizeAway(str); }); } BENCHMARK(BlockToJsonVerboseWrite, benchmark::PriorityLevel::HIGH);
0
bitcoin/src
bitcoin/src/bench/mempool_eviction.cpp
// Copyright (c) 2011-2022 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include <bench/bench.h> #include <kernel/mempool_entry.h> #include <policy/policy.h> #include <test/util/setup_common.h> #include <txmempool.h> static void AddTx(const CTransactionRef& tx, const CAmount& nFee, CTxMemPool& pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, pool.cs) { int64_t nTime = 0; unsigned int nHeight = 1; uint64_t sequence = 0; bool spendsCoinbase = false; unsigned int sigOpCost = 4; LockPoints lp; pool.addUnchecked(CTxMemPoolEntry( tx, nFee, nTime, nHeight, sequence, spendsCoinbase, sigOpCost, lp)); } // Right now this is only testing eviction performance in an extremely small // mempool. Code needs to be written to generate a much wider variety of // unique transactions for a more meaningful performance measurement. static void MempoolEviction(benchmark::Bench& bench) { const auto testing_setup = MakeNoLogFileContext<const TestingSetup>(); CMutableTransaction tx1 = CMutableTransaction(); tx1.vin.resize(1); tx1.vin[0].scriptSig = CScript() << OP_1; tx1.vin[0].scriptWitness.stack.push_back({1}); tx1.vout.resize(1); tx1.vout[0].scriptPubKey = CScript() << OP_1 << OP_EQUAL; tx1.vout[0].nValue = 10 * COIN; CMutableTransaction tx2 = CMutableTransaction(); tx2.vin.resize(1); tx2.vin[0].scriptSig = CScript() << OP_2; tx2.vin[0].scriptWitness.stack.push_back({2}); tx2.vout.resize(1); tx2.vout[0].scriptPubKey = CScript() << OP_2 << OP_EQUAL; tx2.vout[0].nValue = 10 * COIN; CMutableTransaction tx3 = CMutableTransaction(); tx3.vin.resize(1); tx3.vin[0].prevout = COutPoint(tx2.GetHash(), 0); tx3.vin[0].scriptSig = CScript() << OP_2; tx3.vin[0].scriptWitness.stack.push_back({3}); tx3.vout.resize(1); tx3.vout[0].scriptPubKey = CScript() << OP_3 << OP_EQUAL; tx3.vout[0].nValue = 10 * COIN; CMutableTransaction tx4 = CMutableTransaction(); tx4.vin.resize(2); tx4.vin[0].prevout.SetNull(); tx4.vin[0].scriptSig = CScript() << OP_4; tx4.vin[0].scriptWitness.stack.push_back({4}); tx4.vin[1].prevout.SetNull(); tx4.vin[1].scriptSig = CScript() << OP_4; tx4.vin[1].scriptWitness.stack.push_back({4}); tx4.vout.resize(2); tx4.vout[0].scriptPubKey = CScript() << OP_4 << OP_EQUAL; tx4.vout[0].nValue = 10 * COIN; tx4.vout[1].scriptPubKey = CScript() << OP_4 << OP_EQUAL; tx4.vout[1].nValue = 10 * COIN; CMutableTransaction tx5 = CMutableTransaction(); tx5.vin.resize(2); tx5.vin[0].prevout = COutPoint(tx4.GetHash(), 0); tx5.vin[0].scriptSig = CScript() << OP_4; tx5.vin[0].scriptWitness.stack.push_back({4}); tx5.vin[1].prevout.SetNull(); tx5.vin[1].scriptSig = CScript() << OP_5; tx5.vin[1].scriptWitness.stack.push_back({5}); tx5.vout.resize(2); tx5.vout[0].scriptPubKey = CScript() << OP_5 << OP_EQUAL; tx5.vout[0].nValue = 10 * COIN; tx5.vout[1].scriptPubKey = CScript() << OP_5 << OP_EQUAL; tx5.vout[1].nValue = 10 * COIN; CMutableTransaction tx6 = CMutableTransaction(); tx6.vin.resize(2); tx6.vin[0].prevout = COutPoint(tx4.GetHash(), 1); tx6.vin[0].scriptSig = CScript() << OP_4; tx6.vin[0].scriptWitness.stack.push_back({4}); tx6.vin[1].prevout.SetNull(); tx6.vin[1].scriptSig = CScript() << OP_6; tx6.vin[1].scriptWitness.stack.push_back({6}); tx6.vout.resize(2); tx6.vout[0].scriptPubKey = CScript() << OP_6 << OP_EQUAL; tx6.vout[0].nValue = 10 * COIN; tx6.vout[1].scriptPubKey = CScript() << OP_6 << OP_EQUAL; tx6.vout[1].nValue = 10 * COIN; CMutableTransaction tx7 = CMutableTransaction(); tx7.vin.resize(2); tx7.vin[0].prevout = COutPoint(tx5.GetHash(), 0); tx7.vin[0].scriptSig = CScript() << OP_5; tx7.vin[0].scriptWitness.stack.push_back({5}); tx7.vin[1].prevout = COutPoint(tx6.GetHash(), 0); tx7.vin[1].scriptSig = CScript() << OP_6; tx7.vin[1].scriptWitness.stack.push_back({6}); tx7.vout.resize(2); tx7.vout[0].scriptPubKey = CScript() << OP_7 << OP_EQUAL; tx7.vout[0].nValue = 10 * COIN; tx7.vout[1].scriptPubKey = CScript() << OP_7 << OP_EQUAL; tx7.vout[1].nValue = 10 * COIN; CTxMemPool& pool = *Assert(testing_setup->m_node.mempool); LOCK2(cs_main, pool.cs); // Create transaction references outside the "hot loop" const CTransactionRef tx1_r{MakeTransactionRef(tx1)}; const CTransactionRef tx2_r{MakeTransactionRef(tx2)}; const CTransactionRef tx3_r{MakeTransactionRef(tx3)}; const CTransactionRef tx4_r{MakeTransactionRef(tx4)}; const CTransactionRef tx5_r{MakeTransactionRef(tx5)}; const CTransactionRef tx6_r{MakeTransactionRef(tx6)}; const CTransactionRef tx7_r{MakeTransactionRef(tx7)}; bench.run([&]() NO_THREAD_SAFETY_ANALYSIS { AddTx(tx1_r, 10000LL, pool); AddTx(tx2_r, 5000LL, pool); AddTx(tx3_r, 20000LL, pool); AddTx(tx4_r, 7000LL, pool); AddTx(tx5_r, 1000LL, pool); AddTx(tx6_r, 1100LL, pool); AddTx(tx7_r, 9000LL, pool); pool.TrimToSize(pool.DynamicMemoryUsage() * 3 / 4); pool.TrimToSize(GetVirtualTransactionSize(*tx1_r)); }); } BENCHMARK(MempoolEviction, benchmark::PriorityLevel::HIGH);
0
bitcoin/src
bitcoin/src/bench/bench_bitcoin.cpp
// Copyright (c) 2015-2022 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include <bench/bench.h> #include <clientversion.h> #include <common/args.h> #include <crypto/sha256.h> #include <util/fs.h> #include <util/strencodings.h> #include <chrono> #include <cstdint> #include <iostream> #include <sstream> #include <vector> static const char* DEFAULT_BENCH_FILTER = ".*"; static constexpr int64_t DEFAULT_MIN_TIME_MS{10}; /** Priority level default value, run "all" priority levels */ static const std::string DEFAULT_PRIORITY{"all"}; static void SetupBenchArgs(ArgsManager& argsman) { SetupHelpOptions(argsman); argsman.AddArg("-asymptote=<n1,n2,n3,...>", "Test asymptotic growth of the runtime of an algorithm, if supported by the benchmark", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); argsman.AddArg("-filter=<regex>", strprintf("Regular expression filter to select benchmark by name (default: %s)", DEFAULT_BENCH_FILTER), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); argsman.AddArg("-list", "List benchmarks without executing them", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); argsman.AddArg("-min-time=<milliseconds>", strprintf("Minimum runtime per benchmark, in milliseconds (default: %d)", DEFAULT_MIN_TIME_MS), ArgsManager::ALLOW_ANY | ArgsManager::DISALLOW_NEGATION, OptionsCategory::OPTIONS); argsman.AddArg("-output-csv=<output.csv>", "Generate CSV file with the most important benchmark results", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); argsman.AddArg("-output-json=<output.json>", "Generate JSON file with all benchmark results", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); argsman.AddArg("-sanity-check", "Run benchmarks for only one iteration with no output", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); argsman.AddArg("-priority-level=<l1,l2,l3>", strprintf("Run benchmarks of one or multiple priority level(s) (%s), default: '%s'", benchmark::ListPriorities(), DEFAULT_PRIORITY), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); } // parses a comma separated list like "10,20,30,50" static std::vector<double> parseAsymptote(const std::string& str) { std::stringstream ss(str); std::vector<double> numbers; double d; char c; while (ss >> d) { numbers.push_back(d); ss >> c; } return numbers; } static uint8_t parsePriorityLevel(const std::string& str) { uint8_t levels{0}; for (const auto& level: SplitString(str, ',')) { levels |= benchmark::StringToPriority(level); } return levels; } int main(int argc, char** argv) { ArgsManager argsman; SetupBenchArgs(argsman); SHA256AutoDetect(); std::string error; if (!argsman.ParseParameters(argc, argv, error)) { tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error); return EXIT_FAILURE; } if (HelpRequested(argsman)) { std::cout << "Usage: bench_bitcoin [options]\n" "\n" << argsman.GetHelpMessage() << "Description:\n" "\n" " bench_bitcoin executes microbenchmarks. The quality of the benchmark results\n" " highly depend on the stability of the machine. It can sometimes be difficult\n" " to get stable, repeatable results, so here are a few tips:\n" "\n" " * Use pyperf [1] to disable frequency scaling, turbo boost etc. For best\n" " results, use CPU pinning and CPU isolation (see [2]).\n" "\n" " * Each call of run() should do exactly the same work. E.g. inserting into\n" " a std::vector doesn't do that as it will reallocate on certain calls. Make\n" " sure each run has exactly the same preconditions.\n" "\n" " * If results are still not reliable, increase runtime with e.g.\n" " -min-time=5000 to let a benchmark run for at least 5 seconds.\n" "\n" " * bench_bitcoin uses nanobench [3] for which there is extensive\n" " documentation available online.\n" "\n" "Environment Variables:\n" "\n" " To attach a profiler you can run a benchmark in endless mode. This can be\n" " done with the environment variable NANOBENCH_ENDLESS. E.g. like so:\n" "\n" " NANOBENCH_ENDLESS=MuHash ./bench_bitcoin -filter=MuHash\n" "\n" " In rare cases it can be useful to suppress stability warnings. This can be\n" " done with the environment variable NANOBENCH_SUPPRESS_WARNINGS, e.g:\n" "\n" " NANOBENCH_SUPPRESS_WARNINGS=1 ./bench_bitcoin\n" "\n" "Notes:\n" "\n" " 1. pyperf\n" " https://github.com/psf/pyperf\n" "\n" " 2. CPU pinning & isolation\n" " https://pyperf.readthedocs.io/en/latest/system.html\n" "\n" " 3. nanobench\n" " https://github.com/martinus/nanobench\n" "\n"; return EXIT_SUCCESS; } try { benchmark::Args args; args.asymptote = parseAsymptote(argsman.GetArg("-asymptote", "")); args.is_list_only = argsman.GetBoolArg("-list", false); args.min_time = std::chrono::milliseconds(argsman.GetIntArg("-min-time", DEFAULT_MIN_TIME_MS)); args.output_csv = argsman.GetPathArg("-output-csv"); args.output_json = argsman.GetPathArg("-output-json"); args.regex_filter = argsman.GetArg("-filter", DEFAULT_BENCH_FILTER); args.sanity_check = argsman.GetBoolArg("-sanity-check", false); args.priority = parsePriorityLevel(argsman.GetArg("-priority-level", DEFAULT_PRIORITY)); benchmark::BenchRunner::RunAll(args); return EXIT_SUCCESS; } catch (const std::exception& e) { tfm::format(std::cerr, "Error: %s\n", e.what()); return EXIT_FAILURE; } }
0
bitcoin/src
bitcoin/src/bench/streams_findbyte.cpp
// Copyright (c) 2023 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include <bench/bench.h> #include <streams.h> #include <util/fs.h> #include <cstddef> #include <cstdint> #include <cstdio> static void FindByte(benchmark::Bench& bench) { // Setup AutoFile file{fsbridge::fopen("streams_tmp", "w+b")}; const size_t file_size = 200; uint8_t data[file_size] = {0}; data[file_size-1] = 1; file << data; std::rewind(file.Get()); BufferedFile bf{file, /*nBufSize=*/file_size + 1, /*nRewindIn=*/file_size}; bench.run([&] { bf.SetPos(0); bf.FindByte(std::byte(1)); }); // Cleanup file.fclose(); fs::remove("streams_tmp"); } BENCHMARK(FindByte, benchmark::PriorityLevel::HIGH);
0
bitcoin/src
bitcoin/src/bench/bench.cpp
// Copyright (c) 2015-2022 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include <bench/bench.h> #include <test/util/setup_common.h> #include <util/fs.h> #include <util/string.h> #include <chrono> #include <fstream> #include <functional> #include <iostream> #include <map> #include <regex> #include <string> #include <vector> using namespace std::chrono_literals; const std::function<void(const std::string&)> G_TEST_LOG_FUN{}; const std::function<std::vector<const char*>()> G_TEST_COMMAND_LINE_ARGUMENTS{}; namespace { void GenerateTemplateResults(const std::vector<ankerl::nanobench::Result>& benchmarkResults, const fs::path& file, const char* tpl) { if (benchmarkResults.empty() || file.empty()) { // nothing to write, bail out return; } std::ofstream fout{file}; if (fout.is_open()) { ankerl::nanobench::render(tpl, benchmarkResults, fout); std::cout << "Created " << file << std::endl; } else { std::cout << "Could not write to file " << file << std::endl; } } } // namespace namespace benchmark { // map a label to one or multiple priority levels std::map<std::string, uint8_t> map_label_priority = { {"high", PriorityLevel::HIGH}, {"low", PriorityLevel::LOW}, {"all", 0xff} }; std::string ListPriorities() { using item_t = std::pair<std::string, uint8_t>; auto sort_by_priority = [](item_t a, item_t b){ return a.second < b.second; }; std::set<item_t, decltype(sort_by_priority)> sorted_priorities(map_label_priority.begin(), map_label_priority.end(), sort_by_priority); return Join(sorted_priorities, ',', [](const auto& entry){ return entry.first; }); } uint8_t StringToPriority(const std::string& str) { auto it = map_label_priority.find(str); if (it == map_label_priority.end()) throw std::runtime_error(strprintf("Unknown priority level %s", str)); return it->second; } BenchRunner::BenchmarkMap& BenchRunner::benchmarks() { static BenchmarkMap benchmarks_map; return benchmarks_map; } BenchRunner::BenchRunner(std::string name, BenchFunction func, PriorityLevel level) { benchmarks().insert(std::make_pair(name, std::make_pair(func, level))); } void BenchRunner::RunAll(const Args& args) { std::regex reFilter(args.regex_filter); std::smatch baseMatch; if (args.sanity_check) { std::cout << "Running with -sanity-check option, output is being suppressed as benchmark results will be useless." << std::endl; } std::vector<ankerl::nanobench::Result> benchmarkResults; for (const auto& [name, bench_func] : benchmarks()) { const auto& [func, priority_level] = bench_func; if (!(priority_level & args.priority)) { continue; } if (!std::regex_match(name, baseMatch, reFilter)) { continue; } if (args.is_list_only) { std::cout << name << std::endl; continue; } Bench bench; if (args.sanity_check) { bench.epochs(1).epochIterations(1); bench.output(nullptr); } bench.name(name); if (args.min_time > 0ms) { // convert to nanos before dividing to reduce rounding errors std::chrono::nanoseconds min_time_ns = args.min_time; bench.minEpochTime(min_time_ns / bench.epochs()); } if (args.asymptote.empty()) { func(bench); } else { for (auto n : args.asymptote) { bench.complexityN(n); func(bench); } std::cout << bench.complexityBigO() << std::endl; } if (!bench.results().empty()) { benchmarkResults.push_back(bench.results().back()); } } GenerateTemplateResults(benchmarkResults, args.output_csv, "# Benchmark, evals, iterations, total, min, max, median\n" "{{#result}}{{name}}, {{epochs}}, {{average(iterations)}}, {{sumProduct(iterations, elapsed)}}, {{minimum(elapsed)}}, {{maximum(elapsed)}}, {{median(elapsed)}}\n" "{{/result}}"); GenerateTemplateResults(benchmarkResults, args.output_json, ankerl::nanobench::templates::json()); } } // namespace benchmark
0
bitcoin/src
bitcoin/src/bench/peer_eviction.cpp
// Copyright (c) 2021-2022 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include <bench/bench.h> #include <net.h> #include <netaddress.h> #include <random.h> #include <test/util/net.h> #include <test/util/setup_common.h> #include <algorithm> #include <functional> #include <vector> static void EvictionProtectionCommon( benchmark::Bench& bench, int num_candidates, std::function<void(NodeEvictionCandidate&)> candidate_setup_fn) { using Candidates = std::vector<NodeEvictionCandidate>; FastRandomContext random_context{true}; Candidates candidates{GetRandomNodeEvictionCandidates(num_candidates, random_context)}; for (auto& c : candidates) { candidate_setup_fn(c); } bench.run([&] { // creating a copy has an overhead of about 3%, so it does not influence the benchmark results much. auto copy = candidates; ProtectEvictionCandidatesByRatio(copy); }); } /* Benchmarks */ static void EvictionProtection0Networks250Candidates(benchmark::Bench& bench) { EvictionProtectionCommon( bench, /*num_candidates=*/250, [](NodeEvictionCandidate& c) { c.m_connected = std::chrono::seconds{c.id}; c.m_network = NET_IPV4; }); } static void EvictionProtection1Networks250Candidates(benchmark::Bench& bench) { EvictionProtectionCommon( bench, /*num_candidates=*/250, [](NodeEvictionCandidate& c) { c.m_connected = std::chrono::seconds{c.id}; c.m_is_local = false; if (c.id >= 130 && c.id < 240) { // 110 Tor c.m_network = NET_ONION; } else { c.m_network = NET_IPV4; } }); } static void EvictionProtection2Networks250Candidates(benchmark::Bench& bench) { EvictionProtectionCommon( bench, /*num_candidates=*/250, [](NodeEvictionCandidate& c) { c.m_connected = std::chrono::seconds{c.id}; c.m_is_local = false; if (c.id >= 90 && c.id < 160) { // 70 Tor c.m_network = NET_ONION; } else if (c.id >= 170 && c.id < 250) { // 80 I2P c.m_network = NET_I2P; } else { c.m_network = NET_IPV4; } }); } static void EvictionProtection3Networks050Candidates(benchmark::Bench& bench) { EvictionProtectionCommon( bench, /*num_candidates=*/50, [](NodeEvictionCandidate& c) { c.m_connected = std::chrono::seconds{c.id}; c.m_is_local = (c.id == 28 || c.id == 47); // 2 localhost if (c.id >= 30 && c.id < 47) { // 17 I2P c.m_network = NET_I2P; } else if (c.id >= 24 && c.id < 28) { // 4 Tor c.m_network = NET_ONION; } else { c.m_network = NET_IPV4; } }); } static void EvictionProtection3Networks100Candidates(benchmark::Bench& bench) { EvictionProtectionCommon( bench, /*num_candidates=*/100, [](NodeEvictionCandidate& c) { c.m_connected = std::chrono::seconds{c.id}; c.m_is_local = (c.id >= 55 && c.id < 60); // 5 localhost if (c.id >= 70 && c.id < 80) { // 10 I2P c.m_network = NET_I2P; } else if (c.id >= 80 && c.id < 96) { // 16 Tor c.m_network = NET_ONION; } else { c.m_network = NET_IPV4; } }); } static void EvictionProtection3Networks250Candidates(benchmark::Bench& bench) { EvictionProtectionCommon( bench, /*num_candidates=*/250, [](NodeEvictionCandidate& c) { c.m_connected = std::chrono::seconds{c.id}; c.m_is_local = (c.id >= 140 && c.id < 160); // 20 localhost if (c.id >= 170 && c.id < 180) { // 10 I2P c.m_network = NET_I2P; } else if (c.id >= 190 && c.id < 240) { // 50 Tor c.m_network = NET_ONION; } else { c.m_network = NET_IPV4; } }); } // Candidate numbers used for the benchmarks: // - 50 candidates simulates a possible use of -maxconnections // - 100 candidates approximates an average node with default settings // - 250 candidates is the number of peers reported by operators of busy nodes // No disadvantaged networks, with 250 eviction candidates. BENCHMARK(EvictionProtection0Networks250Candidates, benchmark::PriorityLevel::HIGH); // 1 disadvantaged network (Tor) with 250 eviction candidates. BENCHMARK(EvictionProtection1Networks250Candidates, benchmark::PriorityLevel::HIGH); // 2 disadvantaged networks (I2P, Tor) with 250 eviction candidates. BENCHMARK(EvictionProtection2Networks250Candidates, benchmark::PriorityLevel::HIGH); // 3 disadvantaged networks (I2P/localhost/Tor) with 50/100/250 eviction candidates. BENCHMARK(EvictionProtection3Networks050Candidates, benchmark::PriorityLevel::HIGH); BENCHMARK(EvictionProtection3Networks100Candidates, benchmark::PriorityLevel::HIGH); BENCHMARK(EvictionProtection3Networks250Candidates, benchmark::PriorityLevel::HIGH);
0
bitcoin/src
bitcoin/src/bench/gcs_filter.cpp
// Copyright (c) 2018-2022 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include <bench/bench.h> #include <blockfilter.h> static GCSFilter::ElementSet GenerateGCSTestElements() { GCSFilter::ElementSet elements; // Testing the benchmarks with different number of elements show that a filter // with at least 100,000 elements results in benchmarks that have the same // ns/op. This makes it easy to reason about how long (in nanoseconds) a single // filter element takes to process. for (int i = 0; i < 100000; ++i) { GCSFilter::Element element(32); element[0] = static_cast<unsigned char>(i); element[1] = static_cast<unsigned char>(i >> 8); elements.insert(std::move(element)); } return elements; } static void GCSBlockFilterGetHash(benchmark::Bench& bench) { auto elements = GenerateGCSTestElements(); GCSFilter filter({0, 0, BASIC_FILTER_P, BASIC_FILTER_M}, elements); BlockFilter block_filter(BlockFilterType::BASIC, {}, filter.GetEncoded(), /*skip_decode_check=*/false); bench.run([&] { block_filter.GetHash(); }); } static void GCSFilterConstruct(benchmark::Bench& bench) { auto elements = GenerateGCSTestElements(); uint64_t siphash_k0 = 0; bench.run([&]{ GCSFilter filter({siphash_k0, 0, BASIC_FILTER_P, BASIC_FILTER_M}, elements); siphash_k0++; }); } static void GCSFilterDecode(benchmark::Bench& bench) { auto elements = GenerateGCSTestElements(); GCSFilter filter({0, 0, BASIC_FILTER_P, BASIC_FILTER_M}, elements); auto encoded = filter.GetEncoded(); bench.run([&] { GCSFilter filter({0, 0, BASIC_FILTER_P, BASIC_FILTER_M}, encoded, /*skip_decode_check=*/false); }); } static void GCSFilterDecodeSkipCheck(benchmark::Bench& bench) { auto elements = GenerateGCSTestElements(); GCSFilter filter({0, 0, BASIC_FILTER_P, BASIC_FILTER_M}, elements); auto encoded = filter.GetEncoded(); bench.run([&] { GCSFilter filter({0, 0, BASIC_FILTER_P, BASIC_FILTER_M}, encoded, /*skip_decode_check=*/true); }); } static void GCSFilterMatch(benchmark::Bench& bench) { auto elements = GenerateGCSTestElements(); GCSFilter filter({0, 0, BASIC_FILTER_P, BASIC_FILTER_M}, elements); bench.run([&] { filter.Match(GCSFilter::Element()); }); } BENCHMARK(GCSBlockFilterGetHash, benchmark::PriorityLevel::HIGH); BENCHMARK(GCSFilterConstruct, benchmark::PriorityLevel::HIGH); BENCHMARK(GCSFilterDecode, benchmark::PriorityLevel::HIGH); BENCHMARK(GCSFilterDecodeSkipCheck, benchmark::PriorityLevel::HIGH); BENCHMARK(GCSFilterMatch, benchmark::PriorityLevel::HIGH);
0
bitcoin/src
bitcoin/src/bench/addrman.cpp
// Copyright (c) 2020-2022 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include <addrman.h> #include <bench/bench.h> #include <netbase.h> #include <netgroup.h> #include <random.h> #include <util/check.h> #include <util/time.h> #include <optional> #include <vector> /* A "source" is a source address from which we have received a bunch of other addresses. */ static constexpr size_t NUM_SOURCES = 64; static constexpr size_t NUM_ADDRESSES_PER_SOURCE = 256; static NetGroupManager EMPTY_NETGROUPMAN{std::vector<bool>()}; static constexpr uint32_t ADDRMAN_CONSISTENCY_CHECK_RATIO{0}; static std::vector<CAddress> g_sources; static std::vector<std::vector<CAddress>> g_addresses; static void CreateAddresses() { if (g_sources.size() > 0) { // already created return; } FastRandomContext rng(uint256(std::vector<unsigned char>(32, 123))); auto randAddr = [&rng]() { in6_addr addr; memcpy(&addr, rng.randbytes(sizeof(addr)).data(), sizeof(addr)); uint16_t port; memcpy(&port, rng.randbytes(sizeof(port)).data(), sizeof(port)); if (port == 0) { port = 1; } CAddress ret(CService(addr, port), NODE_NETWORK); ret.nTime = Now<NodeSeconds>(); return ret; }; for (size_t source_i = 0; source_i < NUM_SOURCES; ++source_i) { g_sources.emplace_back(randAddr()); g_addresses.emplace_back(); for (size_t addr_i = 0; addr_i < NUM_ADDRESSES_PER_SOURCE; ++addr_i) { g_addresses[source_i].emplace_back(randAddr()); } } } static void AddAddressesToAddrMan(AddrMan& addrman) { for (size_t source_i = 0; source_i < NUM_SOURCES; ++source_i) { addrman.Add(g_addresses[source_i], g_sources[source_i]); } } static void FillAddrMan(AddrMan& addrman) { CreateAddresses(); AddAddressesToAddrMan(addrman); } /* Benchmarks */ static void AddrManAdd(benchmark::Bench& bench) { CreateAddresses(); bench.run([&] { AddrMan addrman{EMPTY_NETGROUPMAN, /*deterministic=*/false, ADDRMAN_CONSISTENCY_CHECK_RATIO}; AddAddressesToAddrMan(addrman); }); } static void AddrManSelect(benchmark::Bench& bench) { AddrMan addrman{EMPTY_NETGROUPMAN, /*deterministic=*/false, ADDRMAN_CONSISTENCY_CHECK_RATIO}; FillAddrMan(addrman); bench.run([&] { const auto& address = addrman.Select(); assert(address.first.GetPort() > 0); }); } // The worst case performance of the Select() function is when there is only // one address on the table, because it linearly searches every position of // several buckets before identifying the correct bucket static void AddrManSelectFromAlmostEmpty(benchmark::Bench& bench) { AddrMan addrman{EMPTY_NETGROUPMAN, /*deterministic=*/false, ADDRMAN_CONSISTENCY_CHECK_RATIO}; // Add one address to the new table CService addr = Lookup("250.3.1.1", 8333, false).value(); addrman.Add({CAddress(addr, NODE_NONE)}, addr); bench.run([&] { (void)addrman.Select(); }); } static void AddrManSelectByNetwork(benchmark::Bench& bench) { AddrMan addrman{EMPTY_NETGROUPMAN, /*deterministic=*/false, ADDRMAN_CONSISTENCY_CHECK_RATIO}; // add single I2P address to new table CService i2p_service; i2p_service.SetSpecial("udhdrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jna.b32.i2p"); CAddress i2p_address(i2p_service, NODE_NONE); i2p_address.nTime = Now<NodeSeconds>(); const CNetAddr source{LookupHost("252.2.2.2", false).value()}; addrman.Add({i2p_address}, source); FillAddrMan(addrman); bench.run([&] { (void)addrman.Select(/*new_only=*/false, NET_I2P); }); } static void AddrManGetAddr(benchmark::Bench& bench) { AddrMan addrman{EMPTY_NETGROUPMAN, /*deterministic=*/false, ADDRMAN_CONSISTENCY_CHECK_RATIO}; FillAddrMan(addrman); bench.run([&] { const auto& addresses = addrman.GetAddr(/*max_addresses=*/2500, /*max_pct=*/23, /*network=*/std::nullopt); assert(addresses.size() > 0); }); } static void AddrManAddThenGood(benchmark::Bench& bench) { auto markSomeAsGood = [](AddrMan& addrman) { for (size_t source_i = 0; source_i < NUM_SOURCES; ++source_i) { for (size_t addr_i = 0; addr_i < NUM_ADDRESSES_PER_SOURCE; ++addr_i) { addrman.Good(g_addresses[source_i][addr_i]); } } }; CreateAddresses(); bench.run([&] { // To make the benchmark independent of the number of evaluations, we always prepare a new addrman. // This is necessary because AddrMan::Good() method modifies the object, affecting the timing of subsequent calls // to the same method and we want to do the same amount of work in every loop iteration. // // This has some overhead (exactly the result of AddrManAdd benchmark), but that overhead is constant so improvements in // AddrMan::Good() will still be noticeable. AddrMan addrman{EMPTY_NETGROUPMAN, /*deterministic=*/false, ADDRMAN_CONSISTENCY_CHECK_RATIO}; AddAddressesToAddrMan(addrman); markSomeAsGood(addrman); }); } BENCHMARK(AddrManAdd, benchmark::PriorityLevel::HIGH); BENCHMARK(AddrManSelect, benchmark::PriorityLevel::HIGH); BENCHMARK(AddrManSelectFromAlmostEmpty, benchmark::PriorityLevel::HIGH); BENCHMARK(AddrManSelectByNetwork, benchmark::PriorityLevel::HIGH); BENCHMARK(AddrManGetAddr, benchmark::PriorityLevel::HIGH); BENCHMARK(AddrManAddThenGood, benchmark::PriorityLevel::HIGH);
0
bitcoin/src
bitcoin/src/bench/ccoins_caching.cpp
// Copyright (c) 2016-2022 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include <bench/bench.h> #include <coins.h> #include <policy/policy.h> #include <script/signingprovider.h> #include <test/util/transaction_utils.h> #include <vector> // Microbenchmark for simple accesses to a CCoinsViewCache database. Note from // laanwj, "replicating the actual usage patterns of the client is hard though, // many times micro-benchmarks of the database showed completely different // characteristics than e.g. reindex timings. But that's not a requirement of // every benchmark." // (https://github.com/bitcoin/bitcoin/issues/7883#issuecomment-224807484) static void CCoinsCaching(benchmark::Bench& bench) { ECC_Start(); FillableSigningProvider keystore; CCoinsView coinsDummy; CCoinsViewCache coins(&coinsDummy); std::vector<CMutableTransaction> dummyTransactions = SetupDummyInputs(keystore, coins, {11 * COIN, 50 * COIN, 21 * COIN, 22 * COIN}); CMutableTransaction t1; t1.vin.resize(3); t1.vin[0].prevout.hash = dummyTransactions[0].GetHash(); t1.vin[0].prevout.n = 1; t1.vin[0].scriptSig << std::vector<unsigned char>(65, 0); t1.vin[1].prevout.hash = dummyTransactions[1].GetHash(); t1.vin[1].prevout.n = 0; t1.vin[1].scriptSig << std::vector<unsigned char>(65, 0) << std::vector<unsigned char>(33, 4); t1.vin[2].prevout.hash = dummyTransactions[1].GetHash(); t1.vin[2].prevout.n = 1; t1.vin[2].scriptSig << std::vector<unsigned char>(65, 0) << std::vector<unsigned char>(33, 4); t1.vout.resize(2); t1.vout[0].nValue = 90 * COIN; t1.vout[0].scriptPubKey << OP_1; // Benchmark. const CTransaction tx_1(t1); bench.run([&] { bool success{AreInputsStandard(tx_1, coins)}; assert(success); }); ECC_Stop(); } BENCHMARK(CCoinsCaching, benchmark::PriorityLevel::HIGH);
0
bitcoin/src
bitcoin/src/bench/checkqueue.cpp
// Copyright (c) 2015-2022 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include <bench/bench.h> #include <checkqueue.h> #include <common/system.h> #include <key.h> #include <prevector.h> #include <pubkey.h> #include <random.h> #include <vector> static const size_t BATCHES = 101; static const size_t BATCH_SIZE = 30; static const int PREVECTOR_SIZE = 28; static const unsigned int QUEUE_BATCH_SIZE = 128; // This Benchmark tests the CheckQueue with a slightly realistic workload, // where checks all contain a prevector that is indirect 50% of the time // and there is a little bit of work done between calls to Add. static void CCheckQueueSpeedPrevectorJob(benchmark::Bench& bench) { // We shouldn't ever be running with the checkqueue on a single core machine. if (GetNumCores() <= 1) return; ECC_Start(); struct PrevectorJob { prevector<PREVECTOR_SIZE, uint8_t> p; explicit PrevectorJob(FastRandomContext& insecure_rand){ p.resize(insecure_rand.randrange(PREVECTOR_SIZE*2)); } bool operator()() { return true; } }; // The main thread should be counted to prevent thread oversubscription, and // to decrease the variance of benchmark results. int worker_threads_num{GetNumCores() - 1}; CCheckQueue<PrevectorJob> queue{QUEUE_BATCH_SIZE, worker_threads_num}; // create all the data once, then submit copies in the benchmark. FastRandomContext insecure_rand(true); std::vector<std::vector<PrevectorJob>> vBatches(BATCHES); for (auto& vChecks : vBatches) { vChecks.reserve(BATCH_SIZE); for (size_t x = 0; x < BATCH_SIZE; ++x) vChecks.emplace_back(insecure_rand); } bench.minEpochIterations(10).batch(BATCH_SIZE * BATCHES).unit("job").run([&] { // Make insecure_rand here so that each iteration is identical. CCheckQueueControl<PrevectorJob> control(&queue); for (auto vChecks : vBatches) { control.Add(std::move(vChecks)); } // control waits for completion by RAII, but // it is done explicitly here for clarity control.Wait(); }); ECC_Stop(); } BENCHMARK(CCheckQueueSpeedPrevectorJob, benchmark::PriorityLevel::HIGH);
0
bitcoin/src
bitcoin/src/crypto/ripemd160.h
// Copyright (c) 2014-2022 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_CRYPTO_RIPEMD160_H #define BITCOIN_CRYPTO_RIPEMD160_H #include <cstdlib> #include <stdint.h> /** A hasher class for RIPEMD-160. */ class CRIPEMD160 { private: uint32_t s[5]; unsigned char buf[64]; uint64_t bytes{0}; public: static const size_t OUTPUT_SIZE = 20; CRIPEMD160(); CRIPEMD160& Write(const unsigned char* data, size_t len); void Finalize(unsigned char hash[OUTPUT_SIZE]); CRIPEMD160& Reset(); }; #endif // BITCOIN_CRYPTO_RIPEMD160_H
0
bitcoin/src
bitcoin/src/crypto/muhash.h
// Copyright (c) 2017-2021 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_CRYPTO_MUHASH_H #define BITCOIN_CRYPTO_MUHASH_H #if defined(HAVE_CONFIG_H) #include <config/bitcoin-config.h> #endif #include <serialize.h> #include <uint256.h> #include <stdint.h> class Num3072 { private: void FullReduce(); bool IsOverflow() const; Num3072 GetInverse() const; public: static constexpr size_t BYTE_SIZE = 384; #ifdef __SIZEOF_INT128__ typedef unsigned __int128 double_limb_t; typedef uint64_t limb_t; static constexpr int LIMBS = 48; static constexpr int LIMB_SIZE = 64; #else typedef uint64_t double_limb_t; typedef uint32_t limb_t; static constexpr int LIMBS = 96; static constexpr int LIMB_SIZE = 32; #endif limb_t limbs[LIMBS]; // Sanity check for Num3072 constants static_assert(LIMB_SIZE * LIMBS == 3072, "Num3072 isn't 3072 bits"); static_assert(sizeof(double_limb_t) == sizeof(limb_t) * 2, "bad size for double_limb_t"); static_assert(sizeof(limb_t) * 8 == LIMB_SIZE, "LIMB_SIZE is incorrect"); // Hard coded values in MuHash3072 constructor and Finalize static_assert(sizeof(limb_t) == 4 || sizeof(limb_t) == 8, "bad size for limb_t"); void Multiply(const Num3072& a); void Divide(const Num3072& a); void SetToOne(); void Square(); void ToBytes(unsigned char (&out)[BYTE_SIZE]); Num3072() { this->SetToOne(); }; Num3072(const unsigned char (&data)[BYTE_SIZE]); SERIALIZE_METHODS(Num3072, obj) { for (auto& limb : obj.limbs) { READWRITE(limb); } } }; /** A class representing MuHash sets * * MuHash is a hashing algorithm that supports adding set elements in any * order but also deleting in any order. As a result, it can maintain a * running sum for a set of data as a whole, and add/remove when data * is added to or removed from it. A downside of MuHash is that computing * an inverse is relatively expensive. This is solved by representing * the running value as a fraction, and multiplying added elements into * the numerator and removed elements into the denominator. Only when the * final hash is desired, a single modular inverse and multiplication is * needed to combine the two. The combination is also run on serialization * to allow for space-efficient storage on disk. * * As the update operations are also associative, H(a)+H(b)+H(c)+H(d) can * in fact be computed as (H(a)+H(b)) + (H(c)+H(d)). This implies that * all of this is perfectly parallellizable: each thread can process an * arbitrary subset of the update operations, allowing them to be * efficiently combined later. * * MuHash does not support checking if an element is already part of the * set. That is why this class does not enforce the use of a set as the * data it represents because there is no efficient way to do so. * It is possible to add elements more than once and also to remove * elements that have not been added before. However, this implementation * is intended to represent a set of elements. * * See also https://cseweb.ucsd.edu/~mihir/papers/inchash.pdf and * https://lists.linuxfoundation.org/pipermail/bitcoin-dev/2017-May/014337.html. */ class MuHash3072 { private: Num3072 m_numerator; Num3072 m_denominator; Num3072 ToNum3072(Span<const unsigned char> in); public: /* The empty set. */ MuHash3072() noexcept {}; /* A singleton with variable sized data in it. */ explicit MuHash3072(Span<const unsigned char> in) noexcept; /* Insert a single piece of data into the set. */ MuHash3072& Insert(Span<const unsigned char> in) noexcept; /* Remove a single piece of data from the set. */ MuHash3072& Remove(Span<const unsigned char> in) noexcept; /* Multiply (resulting in a hash for the union of the sets) */ MuHash3072& operator*=(const MuHash3072& mul) noexcept; /* Divide (resulting in a hash for the difference of the sets) */ MuHash3072& operator/=(const MuHash3072& div) noexcept; /* Finalize into a 32-byte hash. Does not change this object's value. */ void Finalize(uint256& out) noexcept; SERIALIZE_METHODS(MuHash3072, obj) { READWRITE(obj.m_numerator); READWRITE(obj.m_denominator); } }; #endif // BITCOIN_CRYPTO_MUHASH_H
0
bitcoin/src
bitcoin/src/crypto/sha256_avx2.cpp
// Copyright (c) 2017-2019 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifdef ENABLE_AVX2 #include <stdint.h> #include <immintrin.h> #include <attributes.h> #include <crypto/common.h> namespace sha256d64_avx2 { namespace { __m256i inline K(uint32_t x) { return _mm256_set1_epi32(x); } __m256i inline Add(__m256i x, __m256i y) { return _mm256_add_epi32(x, y); } __m256i inline Add(__m256i x, __m256i y, __m256i z) { return Add(Add(x, y), z); } __m256i inline Add(__m256i x, __m256i y, __m256i z, __m256i w) { return Add(Add(x, y), Add(z, w)); } __m256i inline Add(__m256i x, __m256i y, __m256i z, __m256i w, __m256i v) { return Add(Add(x, y, z), Add(w, v)); } __m256i inline Inc(__m256i& x, __m256i y) { x = Add(x, y); return x; } __m256i inline Inc(__m256i& x, __m256i y, __m256i z) { x = Add(x, y, z); return x; } __m256i inline Inc(__m256i& x, __m256i y, __m256i z, __m256i w) { x = Add(x, y, z, w); return x; } __m256i inline Xor(__m256i x, __m256i y) { return _mm256_xor_si256(x, y); } __m256i inline Xor(__m256i x, __m256i y, __m256i z) { return Xor(Xor(x, y), z); } __m256i inline Or(__m256i x, __m256i y) { return _mm256_or_si256(x, y); } __m256i inline And(__m256i x, __m256i y) { return _mm256_and_si256(x, y); } __m256i inline ShR(__m256i x, int n) { return _mm256_srli_epi32(x, n); } __m256i inline ShL(__m256i x, int n) { return _mm256_slli_epi32(x, n); } __m256i inline Ch(__m256i x, __m256i y, __m256i z) { return Xor(z, And(x, Xor(y, z))); } __m256i inline Maj(__m256i x, __m256i y, __m256i z) { return Or(And(x, y), And(z, Or(x, y))); } __m256i inline Sigma0(__m256i x) { return Xor(Or(ShR(x, 2), ShL(x, 30)), Or(ShR(x, 13), ShL(x, 19)), Or(ShR(x, 22), ShL(x, 10))); } __m256i inline Sigma1(__m256i x) { return Xor(Or(ShR(x, 6), ShL(x, 26)), Or(ShR(x, 11), ShL(x, 21)), Or(ShR(x, 25), ShL(x, 7))); } __m256i inline sigma0(__m256i x) { return Xor(Or(ShR(x, 7), ShL(x, 25)), Or(ShR(x, 18), ShL(x, 14)), ShR(x, 3)); } __m256i inline sigma1(__m256i x) { return Xor(Or(ShR(x, 17), ShL(x, 15)), Or(ShR(x, 19), ShL(x, 13)), ShR(x, 10)); } /** One round of SHA-256. */ void ALWAYS_INLINE Round(__m256i a, __m256i b, __m256i c, __m256i& d, __m256i e, __m256i f, __m256i g, __m256i& h, __m256i k) { __m256i t1 = Add(h, Sigma1(e), Ch(e, f, g), k); __m256i t2 = Add(Sigma0(a), Maj(a, b, c)); d = Add(d, t1); h = Add(t1, t2); } __m256i inline Read8(const unsigned char* chunk, int offset) { __m256i ret = _mm256_set_epi32( ReadLE32(chunk + 0 + offset), ReadLE32(chunk + 64 + offset), ReadLE32(chunk + 128 + offset), ReadLE32(chunk + 192 + offset), ReadLE32(chunk + 256 + offset), ReadLE32(chunk + 320 + offset), ReadLE32(chunk + 384 + offset), ReadLE32(chunk + 448 + offset) ); return _mm256_shuffle_epi8(ret, _mm256_set_epi32(0x0C0D0E0FUL, 0x08090A0BUL, 0x04050607UL, 0x00010203UL, 0x0C0D0E0FUL, 0x08090A0BUL, 0x04050607UL, 0x00010203UL)); } void inline Write8(unsigned char* out, int offset, __m256i v) { v = _mm256_shuffle_epi8(v, _mm256_set_epi32(0x0C0D0E0FUL, 0x08090A0BUL, 0x04050607UL, 0x00010203UL, 0x0C0D0E0FUL, 0x08090A0BUL, 0x04050607UL, 0x00010203UL)); WriteLE32(out + 0 + offset, _mm256_extract_epi32(v, 7)); WriteLE32(out + 32 + offset, _mm256_extract_epi32(v, 6)); WriteLE32(out + 64 + offset, _mm256_extract_epi32(v, 5)); WriteLE32(out + 96 + offset, _mm256_extract_epi32(v, 4)); WriteLE32(out + 128 + offset, _mm256_extract_epi32(v, 3)); WriteLE32(out + 160 + offset, _mm256_extract_epi32(v, 2)); WriteLE32(out + 192 + offset, _mm256_extract_epi32(v, 1)); WriteLE32(out + 224 + offset, _mm256_extract_epi32(v, 0)); } } void Transform_8way(unsigned char* out, const unsigned char* in) { // Transform 1 __m256i a = K(0x6a09e667ul); __m256i b = K(0xbb67ae85ul); __m256i c = K(0x3c6ef372ul); __m256i d = K(0xa54ff53aul); __m256i e = K(0x510e527ful); __m256i f = K(0x9b05688cul); __m256i g = K(0x1f83d9abul); __m256i h = K(0x5be0cd19ul); __m256i w0, w1, w2, w3, w4, w5, w6, w7, w8, w9, w10, w11, w12, w13, w14, w15; Round(a, b, c, d, e, f, g, h, Add(K(0x428a2f98ul), w0 = Read8(in, 0))); Round(h, a, b, c, d, e, f, g, Add(K(0x71374491ul), w1 = Read8(in, 4))); Round(g, h, a, b, c, d, e, f, Add(K(0xb5c0fbcful), w2 = Read8(in, 8))); Round(f, g, h, a, b, c, d, e, Add(K(0xe9b5dba5ul), w3 = Read8(in, 12))); Round(e, f, g, h, a, b, c, d, Add(K(0x3956c25bul), w4 = Read8(in, 16))); Round(d, e, f, g, h, a, b, c, Add(K(0x59f111f1ul), w5 = Read8(in, 20))); Round(c, d, e, f, g, h, a, b, Add(K(0x923f82a4ul), w6 = Read8(in, 24))); Round(b, c, d, e, f, g, h, a, Add(K(0xab1c5ed5ul), w7 = Read8(in, 28))); Round(a, b, c, d, e, f, g, h, Add(K(0xd807aa98ul), w8 = Read8(in, 32))); Round(h, a, b, c, d, e, f, g, Add(K(0x12835b01ul), w9 = Read8(in, 36))); Round(g, h, a, b, c, d, e, f, Add(K(0x243185beul), w10 = Read8(in, 40))); Round(f, g, h, a, b, c, d, e, Add(K(0x550c7dc3ul), w11 = Read8(in, 44))); Round(e, f, g, h, a, b, c, d, Add(K(0x72be5d74ul), w12 = Read8(in, 48))); Round(d, e, f, g, h, a, b, c, Add(K(0x80deb1feul), w13 = Read8(in, 52))); Round(c, d, e, f, g, h, a, b, Add(K(0x9bdc06a7ul), w14 = Read8(in, 56))); Round(b, c, d, e, f, g, h, a, Add(K(0xc19bf174ul), w15 = Read8(in, 60))); Round(a, b, c, d, e, f, g, h, Add(K(0xe49b69c1ul), Inc(w0, sigma1(w14), w9, sigma0(w1)))); Round(h, a, b, c, d, e, f, g, Add(K(0xefbe4786ul), Inc(w1, sigma1(w15), w10, sigma0(w2)))); Round(g, h, a, b, c, d, e, f, Add(K(0x0fc19dc6ul), Inc(w2, sigma1(w0), w11, sigma0(w3)))); Round(f, g, h, a, b, c, d, e, Add(K(0x240ca1ccul), Inc(w3, sigma1(w1), w12, sigma0(w4)))); Round(e, f, g, h, a, b, c, d, Add(K(0x2de92c6ful), Inc(w4, sigma1(w2), w13, sigma0(w5)))); Round(d, e, f, g, h, a, b, c, Add(K(0x4a7484aaul), Inc(w5, sigma1(w3), w14, sigma0(w6)))); Round(c, d, e, f, g, h, a, b, Add(K(0x5cb0a9dcul), Inc(w6, sigma1(w4), w15, sigma0(w7)))); Round(b, c, d, e, f, g, h, a, Add(K(0x76f988daul), Inc(w7, sigma1(w5), w0, sigma0(w8)))); Round(a, b, c, d, e, f, g, h, Add(K(0x983e5152ul), Inc(w8, sigma1(w6), w1, sigma0(w9)))); Round(h, a, b, c, d, e, f, g, Add(K(0xa831c66dul), Inc(w9, sigma1(w7), w2, sigma0(w10)))); Round(g, h, a, b, c, d, e, f, Add(K(0xb00327c8ul), Inc(w10, sigma1(w8), w3, sigma0(w11)))); Round(f, g, h, a, b, c, d, e, Add(K(0xbf597fc7ul), Inc(w11, sigma1(w9), w4, sigma0(w12)))); Round(e, f, g, h, a, b, c, d, Add(K(0xc6e00bf3ul), Inc(w12, sigma1(w10), w5, sigma0(w13)))); Round(d, e, f, g, h, a, b, c, Add(K(0xd5a79147ul), Inc(w13, sigma1(w11), w6, sigma0(w14)))); Round(c, d, e, f, g, h, a, b, Add(K(0x06ca6351ul), Inc(w14, sigma1(w12), w7, sigma0(w15)))); Round(b, c, d, e, f, g, h, a, Add(K(0x14292967ul), Inc(w15, sigma1(w13), w8, sigma0(w0)))); Round(a, b, c, d, e, f, g, h, Add(K(0x27b70a85ul), Inc(w0, sigma1(w14), w9, sigma0(w1)))); Round(h, a, b, c, d, e, f, g, Add(K(0x2e1b2138ul), Inc(w1, sigma1(w15), w10, sigma0(w2)))); Round(g, h, a, b, c, d, e, f, Add(K(0x4d2c6dfcul), Inc(w2, sigma1(w0), w11, sigma0(w3)))); Round(f, g, h, a, b, c, d, e, Add(K(0x53380d13ul), Inc(w3, sigma1(w1), w12, sigma0(w4)))); Round(e, f, g, h, a, b, c, d, Add(K(0x650a7354ul), Inc(w4, sigma1(w2), w13, sigma0(w5)))); Round(d, e, f, g, h, a, b, c, Add(K(0x766a0abbul), Inc(w5, sigma1(w3), w14, sigma0(w6)))); Round(c, d, e, f, g, h, a, b, Add(K(0x81c2c92eul), Inc(w6, sigma1(w4), w15, sigma0(w7)))); Round(b, c, d, e, f, g, h, a, Add(K(0x92722c85ul), Inc(w7, sigma1(w5), w0, sigma0(w8)))); Round(a, b, c, d, e, f, g, h, Add(K(0xa2bfe8a1ul), Inc(w8, sigma1(w6), w1, sigma0(w9)))); Round(h, a, b, c, d, e, f, g, Add(K(0xa81a664bul), Inc(w9, sigma1(w7), w2, sigma0(w10)))); Round(g, h, a, b, c, d, e, f, Add(K(0xc24b8b70ul), Inc(w10, sigma1(w8), w3, sigma0(w11)))); Round(f, g, h, a, b, c, d, e, Add(K(0xc76c51a3ul), Inc(w11, sigma1(w9), w4, sigma0(w12)))); Round(e, f, g, h, a, b, c, d, Add(K(0xd192e819ul), Inc(w12, sigma1(w10), w5, sigma0(w13)))); Round(d, e, f, g, h, a, b, c, Add(K(0xd6990624ul), Inc(w13, sigma1(w11), w6, sigma0(w14)))); Round(c, d, e, f, g, h, a, b, Add(K(0xf40e3585ul), Inc(w14, sigma1(w12), w7, sigma0(w15)))); Round(b, c, d, e, f, g, h, a, Add(K(0x106aa070ul), Inc(w15, sigma1(w13), w8, sigma0(w0)))); Round(a, b, c, d, e, f, g, h, Add(K(0x19a4c116ul), Inc(w0, sigma1(w14), w9, sigma0(w1)))); Round(h, a, b, c, d, e, f, g, Add(K(0x1e376c08ul), Inc(w1, sigma1(w15), w10, sigma0(w2)))); Round(g, h, a, b, c, d, e, f, Add(K(0x2748774cul), Inc(w2, sigma1(w0), w11, sigma0(w3)))); Round(f, g, h, a, b, c, d, e, Add(K(0x34b0bcb5ul), Inc(w3, sigma1(w1), w12, sigma0(w4)))); Round(e, f, g, h, a, b, c, d, Add(K(0x391c0cb3ul), Inc(w4, sigma1(w2), w13, sigma0(w5)))); Round(d, e, f, g, h, a, b, c, Add(K(0x4ed8aa4aul), Inc(w5, sigma1(w3), w14, sigma0(w6)))); Round(c, d, e, f, g, h, a, b, Add(K(0x5b9cca4ful), Inc(w6, sigma1(w4), w15, sigma0(w7)))); Round(b, c, d, e, f, g, h, a, Add(K(0x682e6ff3ul), Inc(w7, sigma1(w5), w0, sigma0(w8)))); Round(a, b, c, d, e, f, g, h, Add(K(0x748f82eeul), Inc(w8, sigma1(w6), w1, sigma0(w9)))); Round(h, a, b, c, d, e, f, g, Add(K(0x78a5636ful), Inc(w9, sigma1(w7), w2, sigma0(w10)))); Round(g, h, a, b, c, d, e, f, Add(K(0x84c87814ul), Inc(w10, sigma1(w8), w3, sigma0(w11)))); Round(f, g, h, a, b, c, d, e, Add(K(0x8cc70208ul), Inc(w11, sigma1(w9), w4, sigma0(w12)))); Round(e, f, g, h, a, b, c, d, Add(K(0x90befffaul), Inc(w12, sigma1(w10), w5, sigma0(w13)))); Round(d, e, f, g, h, a, b, c, Add(K(0xa4506cebul), Inc(w13, sigma1(w11), w6, sigma0(w14)))); Round(c, d, e, f, g, h, a, b, Add(K(0xbef9a3f7ul), Inc(w14, sigma1(w12), w7, sigma0(w15)))); Round(b, c, d, e, f, g, h, a, Add(K(0xc67178f2ul), Inc(w15, sigma1(w13), w8, sigma0(w0)))); a = Add(a, K(0x6a09e667ul)); b = Add(b, K(0xbb67ae85ul)); c = Add(c, K(0x3c6ef372ul)); d = Add(d, K(0xa54ff53aul)); e = Add(e, K(0x510e527ful)); f = Add(f, K(0x9b05688cul)); g = Add(g, K(0x1f83d9abul)); h = Add(h, K(0x5be0cd19ul)); __m256i t0 = a, t1 = b, t2 = c, t3 = d, t4 = e, t5 = f, t6 = g, t7 = h; // Transform 2 Round(a, b, c, d, e, f, g, h, K(0xc28a2f98ul)); Round(h, a, b, c, d, e, f, g, K(0x71374491ul)); Round(g, h, a, b, c, d, e, f, K(0xb5c0fbcful)); Round(f, g, h, a, b, c, d, e, K(0xe9b5dba5ul)); Round(e, f, g, h, a, b, c, d, K(0x3956c25bul)); Round(d, e, f, g, h, a, b, c, K(0x59f111f1ul)); Round(c, d, e, f, g, h, a, b, K(0x923f82a4ul)); Round(b, c, d, e, f, g, h, a, K(0xab1c5ed5ul)); Round(a, b, c, d, e, f, g, h, K(0xd807aa98ul)); Round(h, a, b, c, d, e, f, g, K(0x12835b01ul)); Round(g, h, a, b, c, d, e, f, K(0x243185beul)); Round(f, g, h, a, b, c, d, e, K(0x550c7dc3ul)); Round(e, f, g, h, a, b, c, d, K(0x72be5d74ul)); Round(d, e, f, g, h, a, b, c, K(0x80deb1feul)); Round(c, d, e, f, g, h, a, b, K(0x9bdc06a7ul)); Round(b, c, d, e, f, g, h, a, K(0xc19bf374ul)); Round(a, b, c, d, e, f, g, h, K(0x649b69c1ul)); Round(h, a, b, c, d, e, f, g, K(0xf0fe4786ul)); Round(g, h, a, b, c, d, e, f, K(0x0fe1edc6ul)); Round(f, g, h, a, b, c, d, e, K(0x240cf254ul)); Round(e, f, g, h, a, b, c, d, K(0x4fe9346ful)); Round(d, e, f, g, h, a, b, c, K(0x6cc984beul)); Round(c, d, e, f, g, h, a, b, K(0x61b9411eul)); Round(b, c, d, e, f, g, h, a, K(0x16f988faul)); Round(a, b, c, d, e, f, g, h, K(0xf2c65152ul)); Round(h, a, b, c, d, e, f, g, K(0xa88e5a6dul)); Round(g, h, a, b, c, d, e, f, K(0xb019fc65ul)); Round(f, g, h, a, b, c, d, e, K(0xb9d99ec7ul)); Round(e, f, g, h, a, b, c, d, K(0x9a1231c3ul)); Round(d, e, f, g, h, a, b, c, K(0xe70eeaa0ul)); Round(c, d, e, f, g, h, a, b, K(0xfdb1232bul)); Round(b, c, d, e, f, g, h, a, K(0xc7353eb0ul)); Round(a, b, c, d, e, f, g, h, K(0x3069bad5ul)); Round(h, a, b, c, d, e, f, g, K(0xcb976d5ful)); Round(g, h, a, b, c, d, e, f, K(0x5a0f118ful)); Round(f, g, h, a, b, c, d, e, K(0xdc1eeefdul)); Round(e, f, g, h, a, b, c, d, K(0x0a35b689ul)); Round(d, e, f, g, h, a, b, c, K(0xde0b7a04ul)); Round(c, d, e, f, g, h, a, b, K(0x58f4ca9dul)); Round(b, c, d, e, f, g, h, a, K(0xe15d5b16ul)); Round(a, b, c, d, e, f, g, h, K(0x007f3e86ul)); Round(h, a, b, c, d, e, f, g, K(0x37088980ul)); Round(g, h, a, b, c, d, e, f, K(0xa507ea32ul)); Round(f, g, h, a, b, c, d, e, K(0x6fab9537ul)); Round(e, f, g, h, a, b, c, d, K(0x17406110ul)); Round(d, e, f, g, h, a, b, c, K(0x0d8cd6f1ul)); Round(c, d, e, f, g, h, a, b, K(0xcdaa3b6dul)); Round(b, c, d, e, f, g, h, a, K(0xc0bbbe37ul)); Round(a, b, c, d, e, f, g, h, K(0x83613bdaul)); Round(h, a, b, c, d, e, f, g, K(0xdb48a363ul)); Round(g, h, a, b, c, d, e, f, K(0x0b02e931ul)); Round(f, g, h, a, b, c, d, e, K(0x6fd15ca7ul)); Round(e, f, g, h, a, b, c, d, K(0x521afacaul)); Round(d, e, f, g, h, a, b, c, K(0x31338431ul)); Round(c, d, e, f, g, h, a, b, K(0x6ed41a95ul)); Round(b, c, d, e, f, g, h, a, K(0x6d437890ul)); Round(a, b, c, d, e, f, g, h, K(0xc39c91f2ul)); Round(h, a, b, c, d, e, f, g, K(0x9eccabbdul)); Round(g, h, a, b, c, d, e, f, K(0xb5c9a0e6ul)); Round(f, g, h, a, b, c, d, e, K(0x532fb63cul)); Round(e, f, g, h, a, b, c, d, K(0xd2c741c6ul)); Round(d, e, f, g, h, a, b, c, K(0x07237ea3ul)); Round(c, d, e, f, g, h, a, b, K(0xa4954b68ul)); Round(b, c, d, e, f, g, h, a, K(0x4c191d76ul)); w0 = Add(t0, a); w1 = Add(t1, b); w2 = Add(t2, c); w3 = Add(t3, d); w4 = Add(t4, e); w5 = Add(t5, f); w6 = Add(t6, g); w7 = Add(t7, h); // Transform 3 a = K(0x6a09e667ul); b = K(0xbb67ae85ul); c = K(0x3c6ef372ul); d = K(0xa54ff53aul); e = K(0x510e527ful); f = K(0x9b05688cul); g = K(0x1f83d9abul); h = K(0x5be0cd19ul); Round(a, b, c, d, e, f, g, h, Add(K(0x428a2f98ul), w0)); Round(h, a, b, c, d, e, f, g, Add(K(0x71374491ul), w1)); Round(g, h, a, b, c, d, e, f, Add(K(0xb5c0fbcful), w2)); Round(f, g, h, a, b, c, d, e, Add(K(0xe9b5dba5ul), w3)); Round(e, f, g, h, a, b, c, d, Add(K(0x3956c25bul), w4)); Round(d, e, f, g, h, a, b, c, Add(K(0x59f111f1ul), w5)); Round(c, d, e, f, g, h, a, b, Add(K(0x923f82a4ul), w6)); Round(b, c, d, e, f, g, h, a, Add(K(0xab1c5ed5ul), w7)); Round(a, b, c, d, e, f, g, h, K(0x5807aa98ul)); Round(h, a, b, c, d, e, f, g, K(0x12835b01ul)); Round(g, h, a, b, c, d, e, f, K(0x243185beul)); Round(f, g, h, a, b, c, d, e, K(0x550c7dc3ul)); Round(e, f, g, h, a, b, c, d, K(0x72be5d74ul)); Round(d, e, f, g, h, a, b, c, K(0x80deb1feul)); Round(c, d, e, f, g, h, a, b, K(0x9bdc06a7ul)); Round(b, c, d, e, f, g, h, a, K(0xc19bf274ul)); Round(a, b, c, d, e, f, g, h, Add(K(0xe49b69c1ul), Inc(w0, sigma0(w1)))); Round(h, a, b, c, d, e, f, g, Add(K(0xefbe4786ul), Inc(w1, K(0xa00000ul), sigma0(w2)))); Round(g, h, a, b, c, d, e, f, Add(K(0x0fc19dc6ul), Inc(w2, sigma1(w0), sigma0(w3)))); Round(f, g, h, a, b, c, d, e, Add(K(0x240ca1ccul), Inc(w3, sigma1(w1), sigma0(w4)))); Round(e, f, g, h, a, b, c, d, Add(K(0x2de92c6ful), Inc(w4, sigma1(w2), sigma0(w5)))); Round(d, e, f, g, h, a, b, c, Add(K(0x4a7484aaul), Inc(w5, sigma1(w3), sigma0(w6)))); Round(c, d, e, f, g, h, a, b, Add(K(0x5cb0a9dcul), Inc(w6, sigma1(w4), K(0x100ul), sigma0(w7)))); Round(b, c, d, e, f, g, h, a, Add(K(0x76f988daul), Inc(w7, sigma1(w5), w0, K(0x11002000ul)))); Round(a, b, c, d, e, f, g, h, Add(K(0x983e5152ul), w8 = Add(K(0x80000000ul), sigma1(w6), w1))); Round(h, a, b, c, d, e, f, g, Add(K(0xa831c66dul), w9 = Add(sigma1(w7), w2))); Round(g, h, a, b, c, d, e, f, Add(K(0xb00327c8ul), w10 = Add(sigma1(w8), w3))); Round(f, g, h, a, b, c, d, e, Add(K(0xbf597fc7ul), w11 = Add(sigma1(w9), w4))); Round(e, f, g, h, a, b, c, d, Add(K(0xc6e00bf3ul), w12 = Add(sigma1(w10), w5))); Round(d, e, f, g, h, a, b, c, Add(K(0xd5a79147ul), w13 = Add(sigma1(w11), w6))); Round(c, d, e, f, g, h, a, b, Add(K(0x06ca6351ul), w14 = Add(sigma1(w12), w7, K(0x400022ul)))); Round(b, c, d, e, f, g, h, a, Add(K(0x14292967ul), w15 = Add(K(0x100ul), sigma1(w13), w8, sigma0(w0)))); Round(a, b, c, d, e, f, g, h, Add(K(0x27b70a85ul), Inc(w0, sigma1(w14), w9, sigma0(w1)))); Round(h, a, b, c, d, e, f, g, Add(K(0x2e1b2138ul), Inc(w1, sigma1(w15), w10, sigma0(w2)))); Round(g, h, a, b, c, d, e, f, Add(K(0x4d2c6dfcul), Inc(w2, sigma1(w0), w11, sigma0(w3)))); Round(f, g, h, a, b, c, d, e, Add(K(0x53380d13ul), Inc(w3, sigma1(w1), w12, sigma0(w4)))); Round(e, f, g, h, a, b, c, d, Add(K(0x650a7354ul), Inc(w4, sigma1(w2), w13, sigma0(w5)))); Round(d, e, f, g, h, a, b, c, Add(K(0x766a0abbul), Inc(w5, sigma1(w3), w14, sigma0(w6)))); Round(c, d, e, f, g, h, a, b, Add(K(0x81c2c92eul), Inc(w6, sigma1(w4), w15, sigma0(w7)))); Round(b, c, d, e, f, g, h, a, Add(K(0x92722c85ul), Inc(w7, sigma1(w5), w0, sigma0(w8)))); Round(a, b, c, d, e, f, g, h, Add(K(0xa2bfe8a1ul), Inc(w8, sigma1(w6), w1, sigma0(w9)))); Round(h, a, b, c, d, e, f, g, Add(K(0xa81a664bul), Inc(w9, sigma1(w7), w2, sigma0(w10)))); Round(g, h, a, b, c, d, e, f, Add(K(0xc24b8b70ul), Inc(w10, sigma1(w8), w3, sigma0(w11)))); Round(f, g, h, a, b, c, d, e, Add(K(0xc76c51a3ul), Inc(w11, sigma1(w9), w4, sigma0(w12)))); Round(e, f, g, h, a, b, c, d, Add(K(0xd192e819ul), Inc(w12, sigma1(w10), w5, sigma0(w13)))); Round(d, e, f, g, h, a, b, c, Add(K(0xd6990624ul), Inc(w13, sigma1(w11), w6, sigma0(w14)))); Round(c, d, e, f, g, h, a, b, Add(K(0xf40e3585ul), Inc(w14, sigma1(w12), w7, sigma0(w15)))); Round(b, c, d, e, f, g, h, a, Add(K(0x106aa070ul), Inc(w15, sigma1(w13), w8, sigma0(w0)))); Round(a, b, c, d, e, f, g, h, Add(K(0x19a4c116ul), Inc(w0, sigma1(w14), w9, sigma0(w1)))); Round(h, a, b, c, d, e, f, g, Add(K(0x1e376c08ul), Inc(w1, sigma1(w15), w10, sigma0(w2)))); Round(g, h, a, b, c, d, e, f, Add(K(0x2748774cul), Inc(w2, sigma1(w0), w11, sigma0(w3)))); Round(f, g, h, a, b, c, d, e, Add(K(0x34b0bcb5ul), Inc(w3, sigma1(w1), w12, sigma0(w4)))); Round(e, f, g, h, a, b, c, d, Add(K(0x391c0cb3ul), Inc(w4, sigma1(w2), w13, sigma0(w5)))); Round(d, e, f, g, h, a, b, c, Add(K(0x4ed8aa4aul), Inc(w5, sigma1(w3), w14, sigma0(w6)))); Round(c, d, e, f, g, h, a, b, Add(K(0x5b9cca4ful), Inc(w6, sigma1(w4), w15, sigma0(w7)))); Round(b, c, d, e, f, g, h, a, Add(K(0x682e6ff3ul), Inc(w7, sigma1(w5), w0, sigma0(w8)))); Round(a, b, c, d, e, f, g, h, Add(K(0x748f82eeul), Inc(w8, sigma1(w6), w1, sigma0(w9)))); Round(h, a, b, c, d, e, f, g, Add(K(0x78a5636ful), Inc(w9, sigma1(w7), w2, sigma0(w10)))); Round(g, h, a, b, c, d, e, f, Add(K(0x84c87814ul), Inc(w10, sigma1(w8), w3, sigma0(w11)))); Round(f, g, h, a, b, c, d, e, Add(K(0x8cc70208ul), Inc(w11, sigma1(w9), w4, sigma0(w12)))); Round(e, f, g, h, a, b, c, d, Add(K(0x90befffaul), Inc(w12, sigma1(w10), w5, sigma0(w13)))); Round(d, e, f, g, h, a, b, c, Add(K(0xa4506cebul), Inc(w13, sigma1(w11), w6, sigma0(w14)))); Round(c, d, e, f, g, h, a, b, Add(K(0xbef9a3f7ul), w14, sigma1(w12), w7, sigma0(w15))); Round(b, c, d, e, f, g, h, a, Add(K(0xc67178f2ul), w15, sigma1(w13), w8, sigma0(w0))); // Output Write8(out, 0, Add(a, K(0x6a09e667ul))); Write8(out, 4, Add(b, K(0xbb67ae85ul))); Write8(out, 8, Add(c, K(0x3c6ef372ul))); Write8(out, 12, Add(d, K(0xa54ff53aul))); Write8(out, 16, Add(e, K(0x510e527ful))); Write8(out, 20, Add(f, K(0x9b05688cul))); Write8(out, 24, Add(g, K(0x1f83d9abul))); Write8(out, 28, Add(h, K(0x5be0cd19ul))); } } #endif
0
bitcoin/src
bitcoin/src/crypto/siphash.cpp
// Copyright (c) 2016-2020 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include <crypto/siphash.h> #define ROTL(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b)))) #define SIPROUND do { \ v0 += v1; v1 = ROTL(v1, 13); v1 ^= v0; \ v0 = ROTL(v0, 32); \ v2 += v3; v3 = ROTL(v3, 16); v3 ^= v2; \ v0 += v3; v3 = ROTL(v3, 21); v3 ^= v0; \ v2 += v1; v1 = ROTL(v1, 17); v1 ^= v2; \ v2 = ROTL(v2, 32); \ } while (0) CSipHasher::CSipHasher(uint64_t k0, uint64_t k1) { v[0] = 0x736f6d6570736575ULL ^ k0; v[1] = 0x646f72616e646f6dULL ^ k1; v[2] = 0x6c7967656e657261ULL ^ k0; v[3] = 0x7465646279746573ULL ^ k1; count = 0; tmp = 0; } CSipHasher& CSipHasher::Write(uint64_t data) { uint64_t v0 = v[0], v1 = v[1], v2 = v[2], v3 = v[3]; assert(count % 8 == 0); v3 ^= data; SIPROUND; SIPROUND; v0 ^= data; v[0] = v0; v[1] = v1; v[2] = v2; v[3] = v3; count += 8; return *this; } CSipHasher& CSipHasher::Write(Span<const unsigned char> data) { uint64_t v0 = v[0], v1 = v[1], v2 = v[2], v3 = v[3]; uint64_t t = tmp; uint8_t c = count; while (data.size() > 0) { t |= uint64_t{data.front()} << (8 * (c % 8)); c++; if ((c & 7) == 0) { v3 ^= t; SIPROUND; SIPROUND; v0 ^= t; t = 0; } data = data.subspan(1); } v[0] = v0; v[1] = v1; v[2] = v2; v[3] = v3; count = c; tmp = t; return *this; } uint64_t CSipHasher::Finalize() const { uint64_t v0 = v[0], v1 = v[1], v2 = v[2], v3 = v[3]; uint64_t t = tmp | (((uint64_t)count) << 56); v3 ^= t; SIPROUND; SIPROUND; v0 ^= t; v2 ^= 0xFF; SIPROUND; SIPROUND; SIPROUND; SIPROUND; return v0 ^ v1 ^ v2 ^ v3; } uint64_t SipHashUint256(uint64_t k0, uint64_t k1, const uint256& val) { /* Specialized implementation for efficiency */ uint64_t d = val.GetUint64(0); uint64_t v0 = 0x736f6d6570736575ULL ^ k0; uint64_t v1 = 0x646f72616e646f6dULL ^ k1; uint64_t v2 = 0x6c7967656e657261ULL ^ k0; uint64_t v3 = 0x7465646279746573ULL ^ k1 ^ d; SIPROUND; SIPROUND; v0 ^= d; d = val.GetUint64(1); v3 ^= d; SIPROUND; SIPROUND; v0 ^= d; d = val.GetUint64(2); v3 ^= d; SIPROUND; SIPROUND; v0 ^= d; d = val.GetUint64(3); v3 ^= d; SIPROUND; SIPROUND; v0 ^= d; v3 ^= (uint64_t{4}) << 59; SIPROUND; SIPROUND; v0 ^= (uint64_t{4}) << 59; v2 ^= 0xFF; SIPROUND; SIPROUND; SIPROUND; SIPROUND; return v0 ^ v1 ^ v2 ^ v3; } uint64_t SipHashUint256Extra(uint64_t k0, uint64_t k1, const uint256& val, uint32_t extra) { /* Specialized implementation for efficiency */ uint64_t d = val.GetUint64(0); uint64_t v0 = 0x736f6d6570736575ULL ^ k0; uint64_t v1 = 0x646f72616e646f6dULL ^ k1; uint64_t v2 = 0x6c7967656e657261ULL ^ k0; uint64_t v3 = 0x7465646279746573ULL ^ k1 ^ d; SIPROUND; SIPROUND; v0 ^= d; d = val.GetUint64(1); v3 ^= d; SIPROUND; SIPROUND; v0 ^= d; d = val.GetUint64(2); v3 ^= d; SIPROUND; SIPROUND; v0 ^= d; d = val.GetUint64(3); v3 ^= d; SIPROUND; SIPROUND; v0 ^= d; d = ((uint64_t{36}) << 56) | extra; v3 ^= d; SIPROUND; SIPROUND; v0 ^= d; v2 ^= 0xFF; SIPROUND; SIPROUND; SIPROUND; SIPROUND; return v0 ^ v1 ^ v2 ^ v3; }
0
bitcoin/src
bitcoin/src/crypto/poly1305.h
// Copyright (c) 2019-2022 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_CRYPTO_POLY1305_H #define BITCOIN_CRYPTO_POLY1305_H #include <span.h> #include <cassert> #include <cstdlib> #include <stdint.h> #define POLY1305_BLOCK_SIZE 16 namespace poly1305_donna { // Based on the public domain implementation by Andrew Moon // poly1305-donna-32.h from https://github.com/floodyberry/poly1305-donna typedef struct { uint32_t r[5]; uint32_t h[5]; uint32_t pad[4]; size_t leftover; unsigned char buffer[POLY1305_BLOCK_SIZE]; unsigned char final; } poly1305_context; void poly1305_init(poly1305_context *st, const unsigned char key[32]) noexcept; void poly1305_update(poly1305_context *st, const unsigned char *m, size_t bytes) noexcept; void poly1305_finish(poly1305_context *st, unsigned char mac[16]) noexcept; } // namespace poly1305_donna /** C++ wrapper with std::byte Span interface around poly1305_donna code. */ class Poly1305 { poly1305_donna::poly1305_context m_ctx; public: /** Length of the output produced by Finalize(). */ static constexpr unsigned TAGLEN{16}; /** Length of the keys expected by the constructor. */ static constexpr unsigned KEYLEN{32}; /** Construct a Poly1305 object with a given 32-byte key. */ Poly1305(Span<const std::byte> key) noexcept { assert(key.size() == KEYLEN); poly1305_donna::poly1305_init(&m_ctx, UCharCast(key.data())); } /** Process message bytes. */ Poly1305& Update(Span<const std::byte> msg) noexcept { poly1305_donna::poly1305_update(&m_ctx, UCharCast(msg.data()), msg.size()); return *this; } /** Write authentication tag to 16-byte out. */ void Finalize(Span<std::byte> out) noexcept { assert(out.size() == TAGLEN); poly1305_donna::poly1305_finish(&m_ctx, UCharCast(out.data())); } }; #endif // BITCOIN_CRYPTO_POLY1305_H
0
bitcoin/src
bitcoin/src/crypto/sha3.h
// Copyright (c) 2020-2022 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_CRYPTO_SHA3_H #define BITCOIN_CRYPTO_SHA3_H #include <span.h> #include <cstdlib> #include <stdint.h> //! The Keccak-f[1600] transform. void KeccakF(uint64_t (&st)[25]); class SHA3_256 { private: uint64_t m_state[25] = {0}; unsigned char m_buffer[8]; unsigned m_bufsize = 0; unsigned m_pos = 0; //! Sponge rate in bits. static constexpr unsigned RATE_BITS = 1088; //! Sponge rate expressed as a multiple of the buffer size. static constexpr unsigned RATE_BUFFERS = RATE_BITS / (8 * sizeof(m_buffer)); static_assert(RATE_BITS % (8 * sizeof(m_buffer)) == 0, "Rate must be a multiple of 8 bytes"); public: static constexpr size_t OUTPUT_SIZE = 32; SHA3_256() {} SHA3_256& Write(Span<const unsigned char> data); SHA3_256& Finalize(Span<unsigned char> output); SHA3_256& Reset(); }; #endif // BITCOIN_CRYPTO_SHA3_H
0
bitcoin/src
bitcoin/src/crypto/siphash.h
// Copyright (c) 2016-2020 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_CRYPTO_SIPHASH_H #define BITCOIN_CRYPTO_SIPHASH_H #include <stdint.h> #include <span.h> #include <uint256.h> /** SipHash-2-4 */ class CSipHasher { private: uint64_t v[4]; uint64_t tmp; uint8_t count; // Only the low 8 bits of the input size matter. public: /** Construct a SipHash calculator initialized with 128-bit key (k0, k1) */ CSipHasher(uint64_t k0, uint64_t k1); /** Hash a 64-bit integer worth of data * It is treated as if this was the little-endian interpretation of 8 bytes. * This function can only be used when a multiple of 8 bytes have been written so far. */ CSipHasher& Write(uint64_t data); /** Hash arbitrary bytes. */ CSipHasher& Write(Span<const unsigned char> data); /** Compute the 64-bit SipHash-2-4 of the data written so far. The object remains untouched. */ uint64_t Finalize() const; }; /** Optimized SipHash-2-4 implementation for uint256. * * It is identical to: * SipHasher(k0, k1) * .Write(val.GetUint64(0)) * .Write(val.GetUint64(1)) * .Write(val.GetUint64(2)) * .Write(val.GetUint64(3)) * .Finalize() */ uint64_t SipHashUint256(uint64_t k0, uint64_t k1, const uint256& val); uint64_t SipHashUint256Extra(uint64_t k0, uint64_t k1, const uint256& val, uint32_t extra); #endif // BITCOIN_CRYPTO_SIPHASH_H
0
bitcoin/src
bitcoin/src/crypto/hmac_sha256.cpp
// Copyright (c) 2014-2018 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include <crypto/hmac_sha256.h> #include <string.h> CHMAC_SHA256::CHMAC_SHA256(const unsigned char* key, size_t keylen) { unsigned char rkey[64]; if (keylen <= 64) { memcpy(rkey, key, keylen); memset(rkey + keylen, 0, 64 - keylen); } else { CSHA256().Write(key, keylen).Finalize(rkey); memset(rkey + 32, 0, 32); } for (int n = 0; n < 64; n++) rkey[n] ^= 0x5c; outer.Write(rkey, 64); for (int n = 0; n < 64; n++) rkey[n] ^= 0x5c ^ 0x36; inner.Write(rkey, 64); } void CHMAC_SHA256::Finalize(unsigned char hash[OUTPUT_SIZE]) { unsigned char temp[32]; inner.Finalize(temp); outer.Write(temp, 32).Finalize(hash); }
0
bitcoin/src
bitcoin/src/crypto/hmac_sha512.h
// Copyright (c) 2014-2022 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_CRYPTO_HMAC_SHA512_H #define BITCOIN_CRYPTO_HMAC_SHA512_H #include <crypto/sha512.h> #include <cstdlib> #include <stdint.h> /** A hasher class for HMAC-SHA-512. */ class CHMAC_SHA512 { private: CSHA512 outer; CSHA512 inner; public: static const size_t OUTPUT_SIZE = 64; CHMAC_SHA512(const unsigned char* key, size_t keylen); CHMAC_SHA512& Write(const unsigned char* data, size_t len) { inner.Write(data, len); return *this; } void Finalize(unsigned char hash[OUTPUT_SIZE]); }; #endif // BITCOIN_CRYPTO_HMAC_SHA512_H
0
bitcoin/src
bitcoin/src/crypto/sha256.h
// Copyright (c) 2014-2022 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_CRYPTO_SHA256_H #define BITCOIN_CRYPTO_SHA256_H #include <cstdlib> #include <stdint.h> #include <string> /** A hasher class for SHA-256. */ class CSHA256 { private: uint32_t s[8]; unsigned char buf[64]; uint64_t bytes{0}; public: static const size_t OUTPUT_SIZE = 32; CSHA256(); CSHA256& Write(const unsigned char* data, size_t len); void Finalize(unsigned char hash[OUTPUT_SIZE]); CSHA256& Reset(); }; namespace sha256_implementation { enum UseImplementation : uint8_t { STANDARD = 0, USE_SSE4 = 1 << 0, USE_AVX2 = 1 << 1, USE_SHANI = 1 << 2, USE_SSE4_AND_AVX2 = USE_SSE4 | USE_AVX2, USE_SSE4_AND_SHANI = USE_SSE4 | USE_SHANI, USE_ALL = USE_SSE4 | USE_AVX2 | USE_SHANI, }; } /** Autodetect the best available SHA256 implementation. * Returns the name of the implementation. */ std::string SHA256AutoDetect(sha256_implementation::UseImplementation use_implementation = sha256_implementation::USE_ALL); /** Compute multiple double-SHA256's of 64-byte blobs. * output: pointer to a blocks*32 byte output buffer * input: pointer to a blocks*64 byte input buffer * blocks: the number of hashes to compute. */ void SHA256D64(unsigned char* output, const unsigned char* input, size_t blocks); #endif // BITCOIN_CRYPTO_SHA256_H
0
bitcoin/src
bitcoin/src/crypto/hkdf_sha256_32.cpp
// Copyright (c) 2018-2019 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include <crypto/hkdf_sha256_32.h> #include <assert.h> #include <string.h> CHKDF_HMAC_SHA256_L32::CHKDF_HMAC_SHA256_L32(const unsigned char* ikm, size_t ikmlen, const std::string& salt) { CHMAC_SHA256((const unsigned char*)salt.data(), salt.size()).Write(ikm, ikmlen).Finalize(m_prk); } void CHKDF_HMAC_SHA256_L32::Expand32(const std::string& info, unsigned char hash[OUTPUT_SIZE]) { // expand a 32byte key (single round) assert(info.size() <= 128); static const unsigned char one[1] = {1}; CHMAC_SHA256(m_prk, 32).Write((const unsigned char*)info.data(), info.size()).Write(one, 1).Finalize(hash); }
0
bitcoin/src
bitcoin/src/crypto/sha1.cpp
// Copyright (c) 2014-2019 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include <crypto/sha1.h> #include <crypto/common.h> #include <string.h> // Internal implementation code. namespace { /// Internal SHA-1 implementation. namespace sha1 { /** One round of SHA-1. */ 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) { e += ((a << 5) | (a >> 27)) + f + k + w; b = (b << 30) | (b >> 2); } uint32_t inline f1(uint32_t b, uint32_t c, uint32_t d) { return d ^ (b & (c ^ d)); } uint32_t inline f2(uint32_t b, uint32_t c, uint32_t d) { return b ^ c ^ d; } uint32_t inline f3(uint32_t b, uint32_t c, uint32_t d) { return (b & c) | (d & (b | c)); } uint32_t inline left(uint32_t x) { return (x << 1) | (x >> 31); } /** Initialize SHA-1 state. */ void inline Initialize(uint32_t* s) { s[0] = 0x67452301ul; s[1] = 0xEFCDAB89ul; s[2] = 0x98BADCFEul; s[3] = 0x10325476ul; s[4] = 0xC3D2E1F0ul; } const uint32_t k1 = 0x5A827999ul; const uint32_t k2 = 0x6ED9EBA1ul; const uint32_t k3 = 0x8F1BBCDCul; const uint32_t k4 = 0xCA62C1D6ul; /** Perform a SHA-1 transformation, processing a 64-byte chunk. */ void Transform(uint32_t* s, const unsigned char* chunk) { uint32_t a = s[0], b = s[1], c = s[2], d = s[3], e = s[4]; uint32_t w0, w1, w2, w3, w4, w5, w6, w7, w8, w9, w10, w11, w12, w13, w14, w15; Round(a, b, c, d, e, f1(b, c, d), k1, w0 = ReadBE32(chunk + 0)); Round(e, a, b, c, d, f1(a, b, c), k1, w1 = ReadBE32(chunk + 4)); Round(d, e, a, b, c, f1(e, a, b), k1, w2 = ReadBE32(chunk + 8)); Round(c, d, e, a, b, f1(d, e, a), k1, w3 = ReadBE32(chunk + 12)); Round(b, c, d, e, a, f1(c, d, e), k1, w4 = ReadBE32(chunk + 16)); Round(a, b, c, d, e, f1(b, c, d), k1, w5 = ReadBE32(chunk + 20)); Round(e, a, b, c, d, f1(a, b, c), k1, w6 = ReadBE32(chunk + 24)); Round(d, e, a, b, c, f1(e, a, b), k1, w7 = ReadBE32(chunk + 28)); Round(c, d, e, a, b, f1(d, e, a), k1, w8 = ReadBE32(chunk + 32)); Round(b, c, d, e, a, f1(c, d, e), k1, w9 = ReadBE32(chunk + 36)); Round(a, b, c, d, e, f1(b, c, d), k1, w10 = ReadBE32(chunk + 40)); Round(e, a, b, c, d, f1(a, b, c), k1, w11 = ReadBE32(chunk + 44)); Round(d, e, a, b, c, f1(e, a, b), k1, w12 = ReadBE32(chunk + 48)); Round(c, d, e, a, b, f1(d, e, a), k1, w13 = ReadBE32(chunk + 52)); Round(b, c, d, e, a, f1(c, d, e), k1, w14 = ReadBE32(chunk + 56)); Round(a, b, c, d, e, f1(b, c, d), k1, w15 = ReadBE32(chunk + 60)); Round(e, a, b, c, d, f1(a, b, c), k1, w0 = left(w0 ^ w13 ^ w8 ^ w2)); Round(d, e, a, b, c, f1(e, a, b), k1, w1 = left(w1 ^ w14 ^ w9 ^ w3)); Round(c, d, e, a, b, f1(d, e, a), k1, w2 = left(w2 ^ w15 ^ w10 ^ w4)); Round(b, c, d, e, a, f1(c, d, e), k1, w3 = left(w3 ^ w0 ^ w11 ^ w5)); Round(a, b, c, d, e, f2(b, c, d), k2, w4 = left(w4 ^ w1 ^ w12 ^ w6)); Round(e, a, b, c, d, f2(a, b, c), k2, w5 = left(w5 ^ w2 ^ w13 ^ w7)); Round(d, e, a, b, c, f2(e, a, b), k2, w6 = left(w6 ^ w3 ^ w14 ^ w8)); Round(c, d, e, a, b, f2(d, e, a), k2, w7 = left(w7 ^ w4 ^ w15 ^ w9)); Round(b, c, d, e, a, f2(c, d, e), k2, w8 = left(w8 ^ w5 ^ w0 ^ w10)); Round(a, b, c, d, e, f2(b, c, d), k2, w9 = left(w9 ^ w6 ^ w1 ^ w11)); Round(e, a, b, c, d, f2(a, b, c), k2, w10 = left(w10 ^ w7 ^ w2 ^ w12)); Round(d, e, a, b, c, f2(e, a, b), k2, w11 = left(w11 ^ w8 ^ w3 ^ w13)); Round(c, d, e, a, b, f2(d, e, a), k2, w12 = left(w12 ^ w9 ^ w4 ^ w14)); Round(b, c, d, e, a, f2(c, d, e), k2, w13 = left(w13 ^ w10 ^ w5 ^ w15)); Round(a, b, c, d, e, f2(b, c, d), k2, w14 = left(w14 ^ w11 ^ w6 ^ w0)); Round(e, a, b, c, d, f2(a, b, c), k2, w15 = left(w15 ^ w12 ^ w7 ^ w1)); Round(d, e, a, b, c, f2(e, a, b), k2, w0 = left(w0 ^ w13 ^ w8 ^ w2)); Round(c, d, e, a, b, f2(d, e, a), k2, w1 = left(w1 ^ w14 ^ w9 ^ w3)); Round(b, c, d, e, a, f2(c, d, e), k2, w2 = left(w2 ^ w15 ^ w10 ^ w4)); Round(a, b, c, d, e, f2(b, c, d), k2, w3 = left(w3 ^ w0 ^ w11 ^ w5)); Round(e, a, b, c, d, f2(a, b, c), k2, w4 = left(w4 ^ w1 ^ w12 ^ w6)); Round(d, e, a, b, c, f2(e, a, b), k2, w5 = left(w5 ^ w2 ^ w13 ^ w7)); Round(c, d, e, a, b, f2(d, e, a), k2, w6 = left(w6 ^ w3 ^ w14 ^ w8)); Round(b, c, d, e, a, f2(c, d, e), k2, w7 = left(w7 ^ w4 ^ w15 ^ w9)); Round(a, b, c, d, e, f3(b, c, d), k3, w8 = left(w8 ^ w5 ^ w0 ^ w10)); Round(e, a, b, c, d, f3(a, b, c), k3, w9 = left(w9 ^ w6 ^ w1 ^ w11)); Round(d, e, a, b, c, f3(e, a, b), k3, w10 = left(w10 ^ w7 ^ w2 ^ w12)); Round(c, d, e, a, b, f3(d, e, a), k3, w11 = left(w11 ^ w8 ^ w3 ^ w13)); Round(b, c, d, e, a, f3(c, d, e), k3, w12 = left(w12 ^ w9 ^ w4 ^ w14)); Round(a, b, c, d, e, f3(b, c, d), k3, w13 = left(w13 ^ w10 ^ w5 ^ w15)); Round(e, a, b, c, d, f3(a, b, c), k3, w14 = left(w14 ^ w11 ^ w6 ^ w0)); Round(d, e, a, b, c, f3(e, a, b), k3, w15 = left(w15 ^ w12 ^ w7 ^ w1)); Round(c, d, e, a, b, f3(d, e, a), k3, w0 = left(w0 ^ w13 ^ w8 ^ w2)); Round(b, c, d, e, a, f3(c, d, e), k3, w1 = left(w1 ^ w14 ^ w9 ^ w3)); Round(a, b, c, d, e, f3(b, c, d), k3, w2 = left(w2 ^ w15 ^ w10 ^ w4)); Round(e, a, b, c, d, f3(a, b, c), k3, w3 = left(w3 ^ w0 ^ w11 ^ w5)); Round(d, e, a, b, c, f3(e, a, b), k3, w4 = left(w4 ^ w1 ^ w12 ^ w6)); Round(c, d, e, a, b, f3(d, e, a), k3, w5 = left(w5 ^ w2 ^ w13 ^ w7)); Round(b, c, d, e, a, f3(c, d, e), k3, w6 = left(w6 ^ w3 ^ w14 ^ w8)); Round(a, b, c, d, e, f3(b, c, d), k3, w7 = left(w7 ^ w4 ^ w15 ^ w9)); Round(e, a, b, c, d, f3(a, b, c), k3, w8 = left(w8 ^ w5 ^ w0 ^ w10)); Round(d, e, a, b, c, f3(e, a, b), k3, w9 = left(w9 ^ w6 ^ w1 ^ w11)); Round(c, d, e, a, b, f3(d, e, a), k3, w10 = left(w10 ^ w7 ^ w2 ^ w12)); Round(b, c, d, e, a, f3(c, d, e), k3, w11 = left(w11 ^ w8 ^ w3 ^ w13)); Round(a, b, c, d, e, f2(b, c, d), k4, w12 = left(w12 ^ w9 ^ w4 ^ w14)); Round(e, a, b, c, d, f2(a, b, c), k4, w13 = left(w13 ^ w10 ^ w5 ^ w15)); Round(d, e, a, b, c, f2(e, a, b), k4, w14 = left(w14 ^ w11 ^ w6 ^ w0)); Round(c, d, e, a, b, f2(d, e, a), k4, w15 = left(w15 ^ w12 ^ w7 ^ w1)); Round(b, c, d, e, a, f2(c, d, e), k4, w0 = left(w0 ^ w13 ^ w8 ^ w2)); Round(a, b, c, d, e, f2(b, c, d), k4, w1 = left(w1 ^ w14 ^ w9 ^ w3)); Round(e, a, b, c, d, f2(a, b, c), k4, w2 = left(w2 ^ w15 ^ w10 ^ w4)); Round(d, e, a, b, c, f2(e, a, b), k4, w3 = left(w3 ^ w0 ^ w11 ^ w5)); Round(c, d, e, a, b, f2(d, e, a), k4, w4 = left(w4 ^ w1 ^ w12 ^ w6)); Round(b, c, d, e, a, f2(c, d, e), k4, w5 = left(w5 ^ w2 ^ w13 ^ w7)); Round(a, b, c, d, e, f2(b, c, d), k4, w6 = left(w6 ^ w3 ^ w14 ^ w8)); Round(e, a, b, c, d, f2(a, b, c), k4, w7 = left(w7 ^ w4 ^ w15 ^ w9)); Round(d, e, a, b, c, f2(e, a, b), k4, w8 = left(w8 ^ w5 ^ w0 ^ w10)); Round(c, d, e, a, b, f2(d, e, a), k4, w9 = left(w9 ^ w6 ^ w1 ^ w11)); Round(b, c, d, e, a, f2(c, d, e), k4, w10 = left(w10 ^ w7 ^ w2 ^ w12)); Round(a, b, c, d, e, f2(b, c, d), k4, w11 = left(w11 ^ w8 ^ w3 ^ w13)); Round(e, a, b, c, d, f2(a, b, c), k4, w12 = left(w12 ^ w9 ^ w4 ^ w14)); Round(d, e, a, b, c, f2(e, a, b), k4, left(w13 ^ w10 ^ w5 ^ w15)); Round(c, d, e, a, b, f2(d, e, a), k4, left(w14 ^ w11 ^ w6 ^ w0)); Round(b, c, d, e, a, f2(c, d, e), k4, left(w15 ^ w12 ^ w7 ^ w1)); s[0] += a; s[1] += b; s[2] += c; s[3] += d; s[4] += e; } } // namespace sha1 } // namespace ////// SHA1 CSHA1::CSHA1() { sha1::Initialize(s); } CSHA1& CSHA1::Write(const unsigned char* data, size_t len) { const unsigned char* end = data + len; size_t bufsize = bytes % 64; if (bufsize && bufsize + len >= 64) { // Fill the buffer, and process it. memcpy(buf + bufsize, data, 64 - bufsize); bytes += 64 - bufsize; data += 64 - bufsize; sha1::Transform(s, buf); bufsize = 0; } while (end - data >= 64) { // Process full chunks directly from the source. sha1::Transform(s, data); bytes += 64; data += 64; } if (end > data) { // Fill the buffer with what remains. memcpy(buf + bufsize, data, end - data); bytes += end - data; } return *this; } void CSHA1::Finalize(unsigned char hash[OUTPUT_SIZE]) { static const unsigned char pad[64] = {0x80}; unsigned char sizedesc[8]; WriteBE64(sizedesc, bytes << 3); Write(pad, 1 + ((119 - (bytes % 64)) % 64)); Write(sizedesc, 8); WriteBE32(hash, s[0]); WriteBE32(hash + 4, s[1]); WriteBE32(hash + 8, s[2]); WriteBE32(hash + 12, s[3]); WriteBE32(hash + 16, s[4]); } CSHA1& CSHA1::Reset() { bytes = 0; sha1::Initialize(s); return *this; }
0
bitcoin/src
bitcoin/src/crypto/chacha20poly1305.cpp
// Copyright (c) 2023 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include <crypto/chacha20poly1305.h> #include <crypto/common.h> #include <crypto/chacha20.h> #include <crypto/poly1305.h> #include <span.h> #include <support/cleanse.h> #include <assert.h> #include <cstddef> AEADChaCha20Poly1305::AEADChaCha20Poly1305(Span<const std::byte> key) noexcept : m_chacha20(key) { assert(key.size() == KEYLEN); } void AEADChaCha20Poly1305::SetKey(Span<const std::byte> key) noexcept { assert(key.size() == KEYLEN); m_chacha20.SetKey(key); } namespace { #ifndef HAVE_TIMINGSAFE_BCMP #define HAVE_TIMINGSAFE_BCMP int timingsafe_bcmp(const unsigned char* b1, const unsigned char* b2, size_t n) noexcept { const unsigned char *p1 = b1, *p2 = b2; int ret = 0; for (; n > 0; n--) ret |= *p1++ ^ *p2++; return (ret != 0); } #endif /** Compute poly1305 tag. chacha20 must be set to the right nonce, block 0. Will be at block 1 after. */ void ComputeTag(ChaCha20& chacha20, Span<const std::byte> aad, Span<const std::byte> cipher, Span<std::byte> tag) noexcept { static const std::byte PADDING[16] = {{}}; // Get block of keystream (use a full 64 byte buffer to avoid the need for chacha20's own buffering). std::byte first_block[ChaCha20Aligned::BLOCKLEN]; chacha20.Keystream(first_block); // Use the first 32 bytes of the first keystream block as poly1305 key. Poly1305 poly1305{Span{first_block}.first(Poly1305::KEYLEN)}; // Compute tag: // - Process the padded AAD with Poly1305. const unsigned aad_padding_length = (16 - (aad.size() % 16)) % 16; poly1305.Update(aad).Update(Span{PADDING}.first(aad_padding_length)); // - Process the padded ciphertext with Poly1305. const unsigned cipher_padding_length = (16 - (cipher.size() % 16)) % 16; poly1305.Update(cipher).Update(Span{PADDING}.first(cipher_padding_length)); // - Process the AAD and plaintext length with Poly1305. std::byte length_desc[Poly1305::TAGLEN]; WriteLE64(UCharCast(length_desc), aad.size()); WriteLE64(UCharCast(length_desc + 8), cipher.size()); poly1305.Update(length_desc); // Output tag. poly1305.Finalize(tag); } } // namespace void AEADChaCha20Poly1305::Encrypt(Span<const std::byte> plain1, Span<const std::byte> plain2, Span<const std::byte> aad, Nonce96 nonce, Span<std::byte> cipher) noexcept { assert(cipher.size() == plain1.size() + plain2.size() + EXPANSION); // Encrypt using ChaCha20 (starting at block 1). m_chacha20.Seek(nonce, 1); m_chacha20.Crypt(plain1, cipher.first(plain1.size())); m_chacha20.Crypt(plain2, cipher.subspan(plain1.size()).first(plain2.size())); // Seek to block 0, and compute tag using key drawn from there. m_chacha20.Seek(nonce, 0); ComputeTag(m_chacha20, aad, cipher.first(cipher.size() - EXPANSION), cipher.last(EXPANSION)); } bool AEADChaCha20Poly1305::Decrypt(Span<const std::byte> cipher, Span<const std::byte> aad, Nonce96 nonce, Span<std::byte> plain1, Span<std::byte> plain2) noexcept { assert(cipher.size() == plain1.size() + plain2.size() + EXPANSION); // Verify tag (using key drawn from block 0). m_chacha20.Seek(nonce, 0); std::byte expected_tag[EXPANSION]; ComputeTag(m_chacha20, aad, cipher.first(cipher.size() - EXPANSION), expected_tag); if (timingsafe_bcmp(UCharCast(expected_tag), UCharCast(cipher.last(EXPANSION).data()), EXPANSION)) return false; // Decrypt (starting at block 1). m_chacha20.Crypt(cipher.first(plain1.size()), plain1); m_chacha20.Crypt(cipher.subspan(plain1.size()).first(plain2.size()), plain2); return true; } void AEADChaCha20Poly1305::Keystream(Nonce96 nonce, Span<std::byte> keystream) noexcept { // Skip the first output block, as it's used for generating the poly1305 key. m_chacha20.Seek(nonce, 1); m_chacha20.Keystream(keystream); } void FSChaCha20Poly1305::NextPacket() noexcept { if (++m_packet_counter == m_rekey_interval) { // Generate a full block of keystream, to avoid needing the ChaCha20 buffer, even though // we only need KEYLEN (32) bytes. std::byte one_block[ChaCha20Aligned::BLOCKLEN]; m_aead.Keystream({0xFFFFFFFF, m_rekey_counter}, one_block); // Switch keys. m_aead.SetKey(Span{one_block}.first(KEYLEN)); // Wipe the generated keystream (a copy remains inside m_aead, which will be cleaned up // once it cycles again, or is destroyed). memory_cleanse(one_block, sizeof(one_block)); // Update counters. m_packet_counter = 0; ++m_rekey_counter; } } void FSChaCha20Poly1305::Encrypt(Span<const std::byte> plain1, Span<const std::byte> plain2, Span<const std::byte> aad, Span<std::byte> cipher) noexcept { m_aead.Encrypt(plain1, plain2, aad, {m_packet_counter, m_rekey_counter}, cipher); NextPacket(); } bool FSChaCha20Poly1305::Decrypt(Span<const std::byte> cipher, Span<const std::byte> aad, Span<std::byte> plain1, Span<std::byte> plain2) noexcept { bool ret = m_aead.Decrypt(cipher, aad, {m_packet_counter, m_rekey_counter}, plain1, plain2); NextPacket(); return ret; }
0
bitcoin/src
bitcoin/src/crypto/sha512.cpp
// Copyright (c) 2014-2022 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include <crypto/sha512.h> #include <crypto/common.h> #include <string.h> // Internal implementation code. namespace { /// Internal SHA-512 implementation. namespace sha512 { uint64_t inline Ch(uint64_t x, uint64_t y, uint64_t z) { return z ^ (x & (y ^ z)); } uint64_t inline Maj(uint64_t x, uint64_t y, uint64_t z) { return (x & y) | (z & (x | y)); } uint64_t inline Sigma0(uint64_t x) { return (x >> 28 | x << 36) ^ (x >> 34 | x << 30) ^ (x >> 39 | x << 25); } uint64_t inline Sigma1(uint64_t x) { return (x >> 14 | x << 50) ^ (x >> 18 | x << 46) ^ (x >> 41 | x << 23); } uint64_t inline sigma0(uint64_t x) { return (x >> 1 | x << 63) ^ (x >> 8 | x << 56) ^ (x >> 7); } uint64_t inline sigma1(uint64_t x) { return (x >> 19 | x << 45) ^ (x >> 61 | x << 3) ^ (x >> 6); } /** One round of SHA-512. */ 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) { uint64_t t1 = h + Sigma1(e) + Ch(e, f, g) + k + w; uint64_t t2 = Sigma0(a) + Maj(a, b, c); d += t1; h = t1 + t2; } /** Initialize SHA-512 state. */ void inline Initialize(uint64_t* s) { s[0] = 0x6a09e667f3bcc908ull; s[1] = 0xbb67ae8584caa73bull; s[2] = 0x3c6ef372fe94f82bull; s[3] = 0xa54ff53a5f1d36f1ull; s[4] = 0x510e527fade682d1ull; s[5] = 0x9b05688c2b3e6c1full; s[6] = 0x1f83d9abfb41bd6bull; s[7] = 0x5be0cd19137e2179ull; } /** Perform one SHA-512 transformation, processing a 128-byte chunk. */ void Transform(uint64_t* s, const unsigned char* chunk) { 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]; uint64_t w0, w1, w2, w3, w4, w5, w6, w7, w8, w9, w10, w11, w12, w13, w14, w15; Round(a, b, c, d, e, f, g, h, 0x428a2f98d728ae22ull, w0 = ReadBE64(chunk + 0)); Round(h, a, b, c, d, e, f, g, 0x7137449123ef65cdull, w1 = ReadBE64(chunk + 8)); Round(g, h, a, b, c, d, e, f, 0xb5c0fbcfec4d3b2full, w2 = ReadBE64(chunk + 16)); Round(f, g, h, a, b, c, d, e, 0xe9b5dba58189dbbcull, w3 = ReadBE64(chunk + 24)); Round(e, f, g, h, a, b, c, d, 0x3956c25bf348b538ull, w4 = ReadBE64(chunk + 32)); Round(d, e, f, g, h, a, b, c, 0x59f111f1b605d019ull, w5 = ReadBE64(chunk + 40)); Round(c, d, e, f, g, h, a, b, 0x923f82a4af194f9bull, w6 = ReadBE64(chunk + 48)); Round(b, c, d, e, f, g, h, a, 0xab1c5ed5da6d8118ull, w7 = ReadBE64(chunk + 56)); Round(a, b, c, d, e, f, g, h, 0xd807aa98a3030242ull, w8 = ReadBE64(chunk + 64)); Round(h, a, b, c, d, e, f, g, 0x12835b0145706fbeull, w9 = ReadBE64(chunk + 72)); Round(g, h, a, b, c, d, e, f, 0x243185be4ee4b28cull, w10 = ReadBE64(chunk + 80)); Round(f, g, h, a, b, c, d, e, 0x550c7dc3d5ffb4e2ull, w11 = ReadBE64(chunk + 88)); Round(e, f, g, h, a, b, c, d, 0x72be5d74f27b896full, w12 = ReadBE64(chunk + 96)); Round(d, e, f, g, h, a, b, c, 0x80deb1fe3b1696b1ull, w13 = ReadBE64(chunk + 104)); Round(c, d, e, f, g, h, a, b, 0x9bdc06a725c71235ull, w14 = ReadBE64(chunk + 112)); Round(b, c, d, e, f, g, h, a, 0xc19bf174cf692694ull, w15 = ReadBE64(chunk + 120)); Round(a, b, c, d, e, f, g, h, 0xe49b69c19ef14ad2ull, w0 += sigma1(w14) + w9 + sigma0(w1)); Round(h, a, b, c, d, e, f, g, 0xefbe4786384f25e3ull, w1 += sigma1(w15) + w10 + sigma0(w2)); Round(g, h, a, b, c, d, e, f, 0x0fc19dc68b8cd5b5ull, w2 += sigma1(w0) + w11 + sigma0(w3)); Round(f, g, h, a, b, c, d, e, 0x240ca1cc77ac9c65ull, w3 += sigma1(w1) + w12 + sigma0(w4)); Round(e, f, g, h, a, b, c, d, 0x2de92c6f592b0275ull, w4 += sigma1(w2) + w13 + sigma0(w5)); Round(d, e, f, g, h, a, b, c, 0x4a7484aa6ea6e483ull, w5 += sigma1(w3) + w14 + sigma0(w6)); Round(c, d, e, f, g, h, a, b, 0x5cb0a9dcbd41fbd4ull, w6 += sigma1(w4) + w15 + sigma0(w7)); Round(b, c, d, e, f, g, h, a, 0x76f988da831153b5ull, w7 += sigma1(w5) + w0 + sigma0(w8)); Round(a, b, c, d, e, f, g, h, 0x983e5152ee66dfabull, w8 += sigma1(w6) + w1 + sigma0(w9)); Round(h, a, b, c, d, e, f, g, 0xa831c66d2db43210ull, w9 += sigma1(w7) + w2 + sigma0(w10)); Round(g, h, a, b, c, d, e, f, 0xb00327c898fb213full, w10 += sigma1(w8) + w3 + sigma0(w11)); Round(f, g, h, a, b, c, d, e, 0xbf597fc7beef0ee4ull, w11 += sigma1(w9) + w4 + sigma0(w12)); Round(e, f, g, h, a, b, c, d, 0xc6e00bf33da88fc2ull, w12 += sigma1(w10) + w5 + sigma0(w13)); Round(d, e, f, g, h, a, b, c, 0xd5a79147930aa725ull, w13 += sigma1(w11) + w6 + sigma0(w14)); Round(c, d, e, f, g, h, a, b, 0x06ca6351e003826full, w14 += sigma1(w12) + w7 + sigma0(w15)); Round(b, c, d, e, f, g, h, a, 0x142929670a0e6e70ull, w15 += sigma1(w13) + w8 + sigma0(w0)); Round(a, b, c, d, e, f, g, h, 0x27b70a8546d22ffcull, w0 += sigma1(w14) + w9 + sigma0(w1)); Round(h, a, b, c, d, e, f, g, 0x2e1b21385c26c926ull, w1 += sigma1(w15) + w10 + sigma0(w2)); Round(g, h, a, b, c, d, e, f, 0x4d2c6dfc5ac42aedull, w2 += sigma1(w0) + w11 + sigma0(w3)); Round(f, g, h, a, b, c, d, e, 0x53380d139d95b3dfull, w3 += sigma1(w1) + w12 + sigma0(w4)); Round(e, f, g, h, a, b, c, d, 0x650a73548baf63deull, w4 += sigma1(w2) + w13 + sigma0(w5)); Round(d, e, f, g, h, a, b, c, 0x766a0abb3c77b2a8ull, w5 += sigma1(w3) + w14 + sigma0(w6)); Round(c, d, e, f, g, h, a, b, 0x81c2c92e47edaee6ull, w6 += sigma1(w4) + w15 + sigma0(w7)); Round(b, c, d, e, f, g, h, a, 0x92722c851482353bull, w7 += sigma1(w5) + w0 + sigma0(w8)); Round(a, b, c, d, e, f, g, h, 0xa2bfe8a14cf10364ull, w8 += sigma1(w6) + w1 + sigma0(w9)); Round(h, a, b, c, d, e, f, g, 0xa81a664bbc423001ull, w9 += sigma1(w7) + w2 + sigma0(w10)); Round(g, h, a, b, c, d, e, f, 0xc24b8b70d0f89791ull, w10 += sigma1(w8) + w3 + sigma0(w11)); Round(f, g, h, a, b, c, d, e, 0xc76c51a30654be30ull, w11 += sigma1(w9) + w4 + sigma0(w12)); Round(e, f, g, h, a, b, c, d, 0xd192e819d6ef5218ull, w12 += sigma1(w10) + w5 + sigma0(w13)); Round(d, e, f, g, h, a, b, c, 0xd69906245565a910ull, w13 += sigma1(w11) + w6 + sigma0(w14)); Round(c, d, e, f, g, h, a, b, 0xf40e35855771202aull, w14 += sigma1(w12) + w7 + sigma0(w15)); Round(b, c, d, e, f, g, h, a, 0x106aa07032bbd1b8ull, w15 += sigma1(w13) + w8 + sigma0(w0)); Round(a, b, c, d, e, f, g, h, 0x19a4c116b8d2d0c8ull, w0 += sigma1(w14) + w9 + sigma0(w1)); Round(h, a, b, c, d, e, f, g, 0x1e376c085141ab53ull, w1 += sigma1(w15) + w10 + sigma0(w2)); Round(g, h, a, b, c, d, e, f, 0x2748774cdf8eeb99ull, w2 += sigma1(w0) + w11 + sigma0(w3)); Round(f, g, h, a, b, c, d, e, 0x34b0bcb5e19b48a8ull, w3 += sigma1(w1) + w12 + sigma0(w4)); Round(e, f, g, h, a, b, c, d, 0x391c0cb3c5c95a63ull, w4 += sigma1(w2) + w13 + sigma0(w5)); Round(d, e, f, g, h, a, b, c, 0x4ed8aa4ae3418acbull, w5 += sigma1(w3) + w14 + sigma0(w6)); Round(c, d, e, f, g, h, a, b, 0x5b9cca4f7763e373ull, w6 += sigma1(w4) + w15 + sigma0(w7)); Round(b, c, d, e, f, g, h, a, 0x682e6ff3d6b2b8a3ull, w7 += sigma1(w5) + w0 + sigma0(w8)); Round(a, b, c, d, e, f, g, h, 0x748f82ee5defb2fcull, w8 += sigma1(w6) + w1 + sigma0(w9)); Round(h, a, b, c, d, e, f, g, 0x78a5636f43172f60ull, w9 += sigma1(w7) + w2 + sigma0(w10)); Round(g, h, a, b, c, d, e, f, 0x84c87814a1f0ab72ull, w10 += sigma1(w8) + w3 + sigma0(w11)); Round(f, g, h, a, b, c, d, e, 0x8cc702081a6439ecull, w11 += sigma1(w9) + w4 + sigma0(w12)); Round(e, f, g, h, a, b, c, d, 0x90befffa23631e28ull, w12 += sigma1(w10) + w5 + sigma0(w13)); Round(d, e, f, g, h, a, b, c, 0xa4506cebde82bde9ull, w13 += sigma1(w11) + w6 + sigma0(w14)); Round(c, d, e, f, g, h, a, b, 0xbef9a3f7b2c67915ull, w14 += sigma1(w12) + w7 + sigma0(w15)); Round(b, c, d, e, f, g, h, a, 0xc67178f2e372532bull, w15 += sigma1(w13) + w8 + sigma0(w0)); Round(a, b, c, d, e, f, g, h, 0xca273eceea26619cull, w0 += sigma1(w14) + w9 + sigma0(w1)); Round(h, a, b, c, d, e, f, g, 0xd186b8c721c0c207ull, w1 += sigma1(w15) + w10 + sigma0(w2)); Round(g, h, a, b, c, d, e, f, 0xeada7dd6cde0eb1eull, w2 += sigma1(w0) + w11 + sigma0(w3)); Round(f, g, h, a, b, c, d, e, 0xf57d4f7fee6ed178ull, w3 += sigma1(w1) + w12 + sigma0(w4)); Round(e, f, g, h, a, b, c, d, 0x06f067aa72176fbaull, w4 += sigma1(w2) + w13 + sigma0(w5)); Round(d, e, f, g, h, a, b, c, 0x0a637dc5a2c898a6ull, w5 += sigma1(w3) + w14 + sigma0(w6)); Round(c, d, e, f, g, h, a, b, 0x113f9804bef90daeull, w6 += sigma1(w4) + w15 + sigma0(w7)); Round(b, c, d, e, f, g, h, a, 0x1b710b35131c471bull, w7 += sigma1(w5) + w0 + sigma0(w8)); Round(a, b, c, d, e, f, g, h, 0x28db77f523047d84ull, w8 += sigma1(w6) + w1 + sigma0(w9)); Round(h, a, b, c, d, e, f, g, 0x32caab7b40c72493ull, w9 += sigma1(w7) + w2 + sigma0(w10)); Round(g, h, a, b, c, d, e, f, 0x3c9ebe0a15c9bebcull, w10 += sigma1(w8) + w3 + sigma0(w11)); Round(f, g, h, a, b, c, d, e, 0x431d67c49c100d4cull, w11 += sigma1(w9) + w4 + sigma0(w12)); Round(e, f, g, h, a, b, c, d, 0x4cc5d4becb3e42b6ull, w12 += sigma1(w10) + w5 + sigma0(w13)); Round(d, e, f, g, h, a, b, c, 0x597f299cfc657e2aull, w13 += sigma1(w11) + w6 + sigma0(w14)); Round(c, d, e, f, g, h, a, b, 0x5fcb6fab3ad6faecull, w14 + sigma1(w12) + w7 + sigma0(w15)); Round(b, c, d, e, f, g, h, a, 0x6c44198c4a475817ull, w15 + sigma1(w13) + w8 + sigma0(w0)); s[0] += a; s[1] += b; s[2] += c; s[3] += d; s[4] += e; s[5] += f; s[6] += g; s[7] += h; } } // namespace sha512 } // namespace ////// SHA-512 CSHA512::CSHA512() { sha512::Initialize(s); } CSHA512& CSHA512::Write(const unsigned char* data, size_t len) { const unsigned char* end = data + len; size_t bufsize = bytes % 128; if (bufsize && bufsize + len >= 128) { // Fill the buffer, and process it. memcpy(buf + bufsize, data, 128 - bufsize); bytes += 128 - bufsize; data += 128 - bufsize; sha512::Transform(s, buf); bufsize = 0; } while (end - data >= 128) { // Process full chunks directly from the source. sha512::Transform(s, data); data += 128; bytes += 128; } if (end > data) { // Fill the buffer with what remains. memcpy(buf + bufsize, data, end - data); bytes += end - data; } return *this; } void CSHA512::Finalize(unsigned char hash[OUTPUT_SIZE]) { static const unsigned char pad[128] = {0x80}; unsigned char sizedesc[16] = {0x00}; WriteBE64(sizedesc + 8, bytes << 3); Write(pad, 1 + ((239 - (bytes % 128)) % 128)); Write(sizedesc, 16); WriteBE64(hash, s[0]); WriteBE64(hash + 8, s[1]); WriteBE64(hash + 16, s[2]); WriteBE64(hash + 24, s[3]); WriteBE64(hash + 32, s[4]); WriteBE64(hash + 40, s[5]); WriteBE64(hash + 48, s[6]); WriteBE64(hash + 56, s[7]); } CSHA512& CSHA512::Reset() { bytes = 0; sha512::Initialize(s); return *this; }
0
bitcoin/src
bitcoin/src/crypto/chacha20poly1305.h
// Copyright (c) 2023 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_CRYPTO_CHACHA20POLY1305_H #define BITCOIN_CRYPTO_CHACHA20POLY1305_H #include <cstddef> #include <stdint.h> #include <crypto/chacha20.h> #include <crypto/poly1305.h> #include <span.h> /** The AEAD_CHACHA20_POLY1305 authenticated encryption algorithm from RFC8439 section 2.8. */ class AEADChaCha20Poly1305 { /** Internal stream cipher. */ ChaCha20 m_chacha20; public: /** Expected size of key argument in constructor. */ static constexpr unsigned KEYLEN = 32; /** Expansion when encrypting. */ static constexpr unsigned EXPANSION = Poly1305::TAGLEN; /** Initialize an AEAD instance with a specified 32-byte key. */ AEADChaCha20Poly1305(Span<const std::byte> key) noexcept; /** Switch to another 32-byte key. */ void SetKey(Span<const std::byte> key) noexcept; /** 96-bit nonce type. */ using Nonce96 = ChaCha20::Nonce96; /** Encrypt a message with a specified 96-bit nonce and aad. * * Requires cipher.size() = plain.size() + EXPANSION. */ void Encrypt(Span<const std::byte> plain, Span<const std::byte> aad, Nonce96 nonce, Span<std::byte> cipher) noexcept { Encrypt(plain, {}, aad, nonce, cipher); } /** Encrypt a message (given split into plain1 + plain2) with a specified 96-bit nonce and aad. * * Requires cipher.size() = plain1.size() + plain2.size() + EXPANSION. */ void Encrypt(Span<const std::byte> plain1, Span<const std::byte> plain2, Span<const std::byte> aad, Nonce96 nonce, Span<std::byte> cipher) noexcept; /** Decrypt a message with a specified 96-bit nonce and aad. Returns true if valid. * * Requires cipher.size() = plain.size() + EXPANSION. */ bool Decrypt(Span<const std::byte> cipher, Span<const std::byte> aad, Nonce96 nonce, Span<std::byte> plain) noexcept { return Decrypt(cipher, aad, nonce, plain, {}); } /** Decrypt a message with a specified 96-bit nonce and aad and split the result. Returns true if valid. * * Requires cipher.size() = plain1.size() + plain2.size() + EXPANSION. */ bool Decrypt(Span<const std::byte> cipher, Span<const std::byte> aad, Nonce96 nonce, Span<std::byte> plain1, Span<std::byte> plain2) noexcept; /** Get a number of keystream bytes from the underlying stream cipher. * * This is equivalent to Encrypt() with plain set to that many zero bytes, and dropping the * last EXPANSION bytes off the result. */ void Keystream(Nonce96 nonce, Span<std::byte> keystream) noexcept; }; /** Forward-secure wrapper around AEADChaCha20Poly1305. * * This implements an AEAD which automatically increments the nonce on every encryption or * decryption, and cycles keys after a predetermined number of encryptions or decryptions. * * See BIP324 for details. */ class FSChaCha20Poly1305 { private: /** Internal AEAD. */ AEADChaCha20Poly1305 m_aead; /** Every how many iterations this cipher rekeys. */ const uint32_t m_rekey_interval; /** The number of encryptions/decryptions since the last rekey. */ uint32_t m_packet_counter{0}; /** The number of rekeys performed so far. */ uint64_t m_rekey_counter{0}; /** Update counters (and if necessary, key) to transition to the next message. */ void NextPacket() noexcept; public: /** Length of keys expected by the constructor. */ static constexpr auto KEYLEN = AEADChaCha20Poly1305::KEYLEN; /** Expansion when encrypting. */ static constexpr auto EXPANSION = AEADChaCha20Poly1305::EXPANSION; // No copy or move to protect the secret. FSChaCha20Poly1305(const FSChaCha20Poly1305&) = delete; FSChaCha20Poly1305(FSChaCha20Poly1305&&) = delete; FSChaCha20Poly1305& operator=(const FSChaCha20Poly1305&) = delete; FSChaCha20Poly1305& operator=(FSChaCha20Poly1305&&) = delete; /** Construct an FSChaCha20Poly1305 cipher that rekeys every rekey_interval operations. */ FSChaCha20Poly1305(Span<const std::byte> key, uint32_t rekey_interval) noexcept : m_aead(key), m_rekey_interval(rekey_interval) {} /** Encrypt a message with a specified aad. * * Requires cipher.size() = plain.size() + EXPANSION. */ void Encrypt(Span<const std::byte> plain, Span<const std::byte> aad, Span<std::byte> cipher) noexcept { Encrypt(plain, {}, aad, cipher); } /** Encrypt a message (given split into plain1 + plain2) with a specified aad. * * Requires cipher.size() = plain.size() + EXPANSION. */ void Encrypt(Span<const std::byte> plain1, Span<const std::byte> plain2, Span<const std::byte> aad, Span<std::byte> cipher) noexcept; /** Decrypt a message with a specified aad. Returns true if valid. * * Requires cipher.size() = plain.size() + EXPANSION. */ bool Decrypt(Span<const std::byte> cipher, Span<const std::byte> aad, Span<std::byte> plain) noexcept { return Decrypt(cipher, aad, plain, {}); } /** Decrypt a message with a specified aad and split the result. Returns true if valid. * * Requires cipher.size() = plain1.size() + plain2.size() + EXPANSION. */ bool Decrypt(Span<const std::byte> cipher, Span<const std::byte> aad, Span<std::byte> plain1, Span<std::byte> plain2) noexcept; }; #endif // BITCOIN_CRYPTO_CHACHA20POLY1305_H
0
bitcoin/src
bitcoin/src/crypto/hmac_sha256.h
// Copyright (c) 2014-2022 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_CRYPTO_HMAC_SHA256_H #define BITCOIN_CRYPTO_HMAC_SHA256_H #include <crypto/sha256.h> #include <cstdlib> #include <stdint.h> /** A hasher class for HMAC-SHA-256. */ class CHMAC_SHA256 { private: CSHA256 outer; CSHA256 inner; public: static const size_t OUTPUT_SIZE = 32; CHMAC_SHA256(const unsigned char* key, size_t keylen); CHMAC_SHA256& Write(const unsigned char* data, size_t len) { inner.Write(data, len); return *this; } void Finalize(unsigned char hash[OUTPUT_SIZE]); }; #endif // BITCOIN_CRYPTO_HMAC_SHA256_H
0
bitcoin/src
bitcoin/src/crypto/sha3.cpp
// Copyright (c) 2020 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. // Based on https://github.com/mjosaarinen/tiny_sha3/blob/master/sha3.c // by Markku-Juhani O. Saarinen <[email protected]> #include <crypto/sha3.h> #include <crypto/common.h> #include <span.h> #include <algorithm> #include <array> // For std::begin and std::end. #include <stdint.h> // Internal implementation code. namespace { uint64_t Rotl(uint64_t x, int n) { return (x << n) | (x >> (64 - n)); } } // namespace void KeccakF(uint64_t (&st)[25]) { static constexpr uint64_t RNDC[24] = { 0x0000000000000001, 0x0000000000008082, 0x800000000000808a, 0x8000000080008000, 0x000000000000808b, 0x0000000080000001, 0x8000000080008081, 0x8000000000008009, 0x000000000000008a, 0x0000000000000088, 0x0000000080008009, 0x000000008000000a, 0x000000008000808b, 0x800000000000008b, 0x8000000000008089, 0x8000000000008003, 0x8000000000008002, 0x8000000000000080, 0x000000000000800a, 0x800000008000000a, 0x8000000080008081, 0x8000000000008080, 0x0000000080000001, 0x8000000080008008 }; static constexpr int ROUNDS = 24; for (int round = 0; round < ROUNDS; ++round) { uint64_t bc0, bc1, bc2, bc3, bc4, t; // Theta bc0 = st[0] ^ st[5] ^ st[10] ^ st[15] ^ st[20]; bc1 = st[1] ^ st[6] ^ st[11] ^ st[16] ^ st[21]; bc2 = st[2] ^ st[7] ^ st[12] ^ st[17] ^ st[22]; bc3 = st[3] ^ st[8] ^ st[13] ^ st[18] ^ st[23]; bc4 = st[4] ^ st[9] ^ st[14] ^ st[19] ^ st[24]; t = bc4 ^ Rotl(bc1, 1); st[0] ^= t; st[5] ^= t; st[10] ^= t; st[15] ^= t; st[20] ^= t; t = bc0 ^ Rotl(bc2, 1); st[1] ^= t; st[6] ^= t; st[11] ^= t; st[16] ^= t; st[21] ^= t; t = bc1 ^ Rotl(bc3, 1); st[2] ^= t; st[7] ^= t; st[12] ^= t; st[17] ^= t; st[22] ^= t; t = bc2 ^ Rotl(bc4, 1); st[3] ^= t; st[8] ^= t; st[13] ^= t; st[18] ^= t; st[23] ^= t; t = bc3 ^ Rotl(bc0, 1); st[4] ^= t; st[9] ^= t; st[14] ^= t; st[19] ^= t; st[24] ^= t; // Rho Pi t = st[1]; bc0 = st[10]; st[10] = Rotl(t, 1); t = bc0; bc0 = st[7]; st[7] = Rotl(t, 3); t = bc0; bc0 = st[11]; st[11] = Rotl(t, 6); t = bc0; bc0 = st[17]; st[17] = Rotl(t, 10); t = bc0; bc0 = st[18]; st[18] = Rotl(t, 15); t = bc0; bc0 = st[3]; st[3] = Rotl(t, 21); t = bc0; bc0 = st[5]; st[5] = Rotl(t, 28); t = bc0; bc0 = st[16]; st[16] = Rotl(t, 36); t = bc0; bc0 = st[8]; st[8] = Rotl(t, 45); t = bc0; bc0 = st[21]; st[21] = Rotl(t, 55); t = bc0; bc0 = st[24]; st[24] = Rotl(t, 2); t = bc0; bc0 = st[4]; st[4] = Rotl(t, 14); t = bc0; bc0 = st[15]; st[15] = Rotl(t, 27); t = bc0; bc0 = st[23]; st[23] = Rotl(t, 41); t = bc0; bc0 = st[19]; st[19] = Rotl(t, 56); t = bc0; bc0 = st[13]; st[13] = Rotl(t, 8); t = bc0; bc0 = st[12]; st[12] = Rotl(t, 25); t = bc0; bc0 = st[2]; st[2] = Rotl(t, 43); t = bc0; bc0 = st[20]; st[20] = Rotl(t, 62); t = bc0; bc0 = st[14]; st[14] = Rotl(t, 18); t = bc0; bc0 = st[22]; st[22] = Rotl(t, 39); t = bc0; bc0 = st[9]; st[9] = Rotl(t, 61); t = bc0; bc0 = st[6]; st[6] = Rotl(t, 20); t = bc0; st[1] = Rotl(t, 44); // Chi Iota bc0 = st[0]; bc1 = st[1]; bc2 = st[2]; bc3 = st[3]; bc4 = st[4]; st[0] = bc0 ^ (~bc1 & bc2) ^ RNDC[round]; st[1] = bc1 ^ (~bc2 & bc3); st[2] = bc2 ^ (~bc3 & bc4); st[3] = bc3 ^ (~bc4 & bc0); st[4] = bc4 ^ (~bc0 & bc1); bc0 = st[5]; bc1 = st[6]; bc2 = st[7]; bc3 = st[8]; bc4 = st[9]; st[5] = bc0 ^ (~bc1 & bc2); st[6] = bc1 ^ (~bc2 & bc3); st[7] = bc2 ^ (~bc3 & bc4); st[8] = bc3 ^ (~bc4 & bc0); st[9] = bc4 ^ (~bc0 & bc1); bc0 = st[10]; bc1 = st[11]; bc2 = st[12]; bc3 = st[13]; bc4 = st[14]; st[10] = bc0 ^ (~bc1 & bc2); st[11] = bc1 ^ (~bc2 & bc3); st[12] = bc2 ^ (~bc3 & bc4); st[13] = bc3 ^ (~bc4 & bc0); st[14] = bc4 ^ (~bc0 & bc1); bc0 = st[15]; bc1 = st[16]; bc2 = st[17]; bc3 = st[18]; bc4 = st[19]; st[15] = bc0 ^ (~bc1 & bc2); st[16] = bc1 ^ (~bc2 & bc3); st[17] = bc2 ^ (~bc3 & bc4); st[18] = bc3 ^ (~bc4 & bc0); st[19] = bc4 ^ (~bc0 & bc1); bc0 = st[20]; bc1 = st[21]; bc2 = st[22]; bc3 = st[23]; bc4 = st[24]; st[20] = bc0 ^ (~bc1 & bc2); st[21] = bc1 ^ (~bc2 & bc3); st[22] = bc2 ^ (~bc3 & bc4); st[23] = bc3 ^ (~bc4 & bc0); st[24] = bc4 ^ (~bc0 & bc1); } } SHA3_256& SHA3_256::Write(Span<const unsigned char> data) { if (m_bufsize && m_bufsize + data.size() >= sizeof(m_buffer)) { // Fill the buffer and process it. std::copy(data.begin(), data.begin() + sizeof(m_buffer) - m_bufsize, m_buffer + m_bufsize); data = data.subspan(sizeof(m_buffer) - m_bufsize); m_state[m_pos++] ^= ReadLE64(m_buffer); m_bufsize = 0; if (m_pos == RATE_BUFFERS) { KeccakF(m_state); m_pos = 0; } } while (data.size() >= sizeof(m_buffer)) { // Process chunks directly from the buffer. m_state[m_pos++] ^= ReadLE64(data.data()); data = data.subspan(8); if (m_pos == RATE_BUFFERS) { KeccakF(m_state); m_pos = 0; } } if (data.size()) { // Keep the remainder in the buffer. std::copy(data.begin(), data.end(), m_buffer + m_bufsize); m_bufsize += data.size(); } return *this; } SHA3_256& SHA3_256::Finalize(Span<unsigned char> output) { assert(output.size() == OUTPUT_SIZE); std::fill(m_buffer + m_bufsize, m_buffer + sizeof(m_buffer), 0); m_buffer[m_bufsize] ^= 0x06; m_state[m_pos] ^= ReadLE64(m_buffer); m_state[RATE_BUFFERS - 1] ^= 0x8000000000000000; KeccakF(m_state); for (unsigned i = 0; i < 4; ++i) { WriteLE64(output.data() + 8 * i, m_state[i]); } return *this; } SHA3_256& SHA3_256::Reset() { m_bufsize = 0; m_pos = 0; std::fill(std::begin(m_state), std::end(m_state), 0); return *this; }
0
bitcoin/src
bitcoin/src/crypto/hkdf_sha256_32.h
// Copyright (c) 2018-2022 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_CRYPTO_HKDF_SHA256_32_H #define BITCOIN_CRYPTO_HKDF_SHA256_32_H #include <crypto/hmac_sha256.h> #include <cstdlib> #include <stdint.h> /** A rfc5869 HKDF implementation with HMAC_SHA256 and fixed key output length of 32 bytes (L=32) */ class CHKDF_HMAC_SHA256_L32 { private: unsigned char m_prk[32]; static const size_t OUTPUT_SIZE = 32; public: CHKDF_HMAC_SHA256_L32(const unsigned char* ikm, size_t ikmlen, const std::string& salt); void Expand32(const std::string& info, unsigned char hash[OUTPUT_SIZE]); }; #endif // BITCOIN_CRYPTO_HKDF_SHA256_32_H
0
bitcoin/src
bitcoin/src/crypto/sha512.h
// Copyright (c) 2014-2022 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_CRYPTO_SHA512_H #define BITCOIN_CRYPTO_SHA512_H #include <cstdlib> #include <stdint.h> /** A hasher class for SHA-512. */ class CSHA512 { private: uint64_t s[8]; unsigned char buf[128]; uint64_t bytes{0}; public: static constexpr size_t OUTPUT_SIZE = 64; CSHA512(); CSHA512& Write(const unsigned char* data, size_t len); void Finalize(unsigned char hash[OUTPUT_SIZE]); CSHA512& Reset(); uint64_t Size() const { return bytes; } }; #endif // BITCOIN_CRYPTO_SHA512_H
0
bitcoin/src
bitcoin/src/crypto/poly1305.cpp
// Copyright (c) 2019 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include <crypto/common.h> #include <crypto/poly1305.h> #include <string.h> namespace poly1305_donna { // Based on the public domain implementation by Andrew Moon // poly1305-donna-32.h from https://github.com/floodyberry/poly1305-donna void poly1305_init(poly1305_context *st, const unsigned char key[32]) noexcept { /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */ st->r[0] = (ReadLE32(&key[ 0]) ) & 0x3ffffff; st->r[1] = (ReadLE32(&key[ 3]) >> 2) & 0x3ffff03; st->r[2] = (ReadLE32(&key[ 6]) >> 4) & 0x3ffc0ff; st->r[3] = (ReadLE32(&key[ 9]) >> 6) & 0x3f03fff; st->r[4] = (ReadLE32(&key[12]) >> 8) & 0x00fffff; /* h = 0 */ st->h[0] = 0; st->h[1] = 0; st->h[2] = 0; st->h[3] = 0; st->h[4] = 0; /* save pad for later */ st->pad[0] = ReadLE32(&key[16]); st->pad[1] = ReadLE32(&key[20]); st->pad[2] = ReadLE32(&key[24]); st->pad[3] = ReadLE32(&key[28]); st->leftover = 0; st->final = 0; } static void poly1305_blocks(poly1305_context *st, const unsigned char *m, size_t bytes) noexcept { const uint32_t hibit = (st->final) ? 0 : (1UL << 24); /* 1 << 128 */ uint32_t r0,r1,r2,r3,r4; uint32_t s1,s2,s3,s4; uint32_t h0,h1,h2,h3,h4; uint64_t d0,d1,d2,d3,d4; uint32_t c; r0 = st->r[0]; r1 = st->r[1]; r2 = st->r[2]; r3 = st->r[3]; r4 = st->r[4]; s1 = r1 * 5; s2 = r2 * 5; s3 = r3 * 5; s4 = r4 * 5; h0 = st->h[0]; h1 = st->h[1]; h2 = st->h[2]; h3 = st->h[3]; h4 = st->h[4]; while (bytes >= POLY1305_BLOCK_SIZE) { /* h += m[i] */ h0 += (ReadLE32(m+ 0) ) & 0x3ffffff; h1 += (ReadLE32(m+ 3) >> 2) & 0x3ffffff; h2 += (ReadLE32(m+ 6) >> 4) & 0x3ffffff; h3 += (ReadLE32(m+ 9) >> 6) & 0x3ffffff; h4 += (ReadLE32(m+12) >> 8) | hibit; /* h *= r */ d0 = ((uint64_t)h0 * r0) + ((uint64_t)h1 * s4) + ((uint64_t)h2 * s3) + ((uint64_t)h3 * s2) + ((uint64_t)h4 * s1); d1 = ((uint64_t)h0 * r1) + ((uint64_t)h1 * r0) + ((uint64_t)h2 * s4) + ((uint64_t)h3 * s3) + ((uint64_t)h4 * s2); d2 = ((uint64_t)h0 * r2) + ((uint64_t)h1 * r1) + ((uint64_t)h2 * r0) + ((uint64_t)h3 * s4) + ((uint64_t)h4 * s3); d3 = ((uint64_t)h0 * r3) + ((uint64_t)h1 * r2) + ((uint64_t)h2 * r1) + ((uint64_t)h3 * r0) + ((uint64_t)h4 * s4); d4 = ((uint64_t)h0 * r4) + ((uint64_t)h1 * r3) + ((uint64_t)h2 * r2) + ((uint64_t)h3 * r1) + ((uint64_t)h4 * r0); /* (partial) h %= p */ c = (uint32_t)(d0 >> 26); h0 = (uint32_t)d0 & 0x3ffffff; d1 += c; c = (uint32_t)(d1 >> 26); h1 = (uint32_t)d1 & 0x3ffffff; d2 += c; c = (uint32_t)(d2 >> 26); h2 = (uint32_t)d2 & 0x3ffffff; d3 += c; c = (uint32_t)(d3 >> 26); h3 = (uint32_t)d3 & 0x3ffffff; d4 += c; c = (uint32_t)(d4 >> 26); h4 = (uint32_t)d4 & 0x3ffffff; h0 += c * 5; c = (h0 >> 26); h0 = h0 & 0x3ffffff; h1 += c; m += POLY1305_BLOCK_SIZE; bytes -= POLY1305_BLOCK_SIZE; } st->h[0] = h0; st->h[1] = h1; st->h[2] = h2; st->h[3] = h3; st->h[4] = h4; } void poly1305_finish(poly1305_context *st, unsigned char mac[16]) noexcept { uint32_t h0,h1,h2,h3,h4,c; uint32_t g0,g1,g2,g3,g4; uint64_t f; uint32_t mask; /* process the remaining block */ if (st->leftover) { size_t i = st->leftover; st->buffer[i++] = 1; for (; i < POLY1305_BLOCK_SIZE; i++) { st->buffer[i] = 0; } st->final = 1; poly1305_blocks(st, st->buffer, POLY1305_BLOCK_SIZE); } /* fully carry h */ h0 = st->h[0]; h1 = st->h[1]; h2 = st->h[2]; h3 = st->h[3]; h4 = st->h[4]; c = h1 >> 26; h1 = h1 & 0x3ffffff; h2 += c; c = h2 >> 26; h2 = h2 & 0x3ffffff; h3 += c; c = h3 >> 26; h3 = h3 & 0x3ffffff; h4 += c; c = h4 >> 26; h4 = h4 & 0x3ffffff; h0 += c * 5; c = h0 >> 26; h0 = h0 & 0x3ffffff; h1 += c; /* compute h + -p */ g0 = h0 + 5; c = g0 >> 26; g0 &= 0x3ffffff; g1 = h1 + c; c = g1 >> 26; g1 &= 0x3ffffff; g2 = h2 + c; c = g2 >> 26; g2 &= 0x3ffffff; g3 = h3 + c; c = g3 >> 26; g3 &= 0x3ffffff; g4 = h4 + c - (1UL << 26); /* select h if h < p, or h + -p if h >= p */ mask = (g4 >> ((sizeof(uint32_t) * 8) - 1)) - 1; g0 &= mask; g1 &= mask; g2 &= mask; g3 &= mask; g4 &= mask; mask = ~mask; h0 = (h0 & mask) | g0; h1 = (h1 & mask) | g1; h2 = (h2 & mask) | g2; h3 = (h3 & mask) | g3; h4 = (h4 & mask) | g4; /* h = h % (2^128) */ h0 = ((h0 ) | (h1 << 26)) & 0xffffffff; h1 = ((h1 >> 6) | (h2 << 20)) & 0xffffffff; h2 = ((h2 >> 12) | (h3 << 14)) & 0xffffffff; h3 = ((h3 >> 18) | (h4 << 8)) & 0xffffffff; /* mac = (h + pad) % (2^128) */ f = (uint64_t)h0 + st->pad[0] ; h0 = (uint32_t)f; f = (uint64_t)h1 + st->pad[1] + (f >> 32); h1 = (uint32_t)f; f = (uint64_t)h2 + st->pad[2] + (f >> 32); h2 = (uint32_t)f; f = (uint64_t)h3 + st->pad[3] + (f >> 32); h3 = (uint32_t)f; WriteLE32(mac + 0, h0); WriteLE32(mac + 4, h1); WriteLE32(mac + 8, h2); WriteLE32(mac + 12, h3); /* zero out the state */ st->h[0] = 0; st->h[1] = 0; st->h[2] = 0; st->h[3] = 0; st->h[4] = 0; st->r[0] = 0; st->r[1] = 0; st->r[2] = 0; st->r[3] = 0; st->r[4] = 0; st->pad[0] = 0; st->pad[1] = 0; st->pad[2] = 0; st->pad[3] = 0; } void poly1305_update(poly1305_context *st, const unsigned char *m, size_t bytes) noexcept { size_t i; /* handle leftover */ if (st->leftover) { size_t want = (POLY1305_BLOCK_SIZE - st->leftover); if (want > bytes) { want = bytes; } for (i = 0; i < want; i++) { st->buffer[st->leftover + i] = m[i]; } bytes -= want; m += want; st->leftover += want; if (st->leftover < POLY1305_BLOCK_SIZE) return; poly1305_blocks(st, st->buffer, POLY1305_BLOCK_SIZE); st->leftover = 0; } /* process full blocks */ if (bytes >= POLY1305_BLOCK_SIZE) { size_t want = (bytes & ~(POLY1305_BLOCK_SIZE - 1)); poly1305_blocks(st, m, want); m += want; bytes -= want; } /* store leftover */ if (bytes) { for (i = 0; i < bytes; i++) { st->buffer[st->leftover + i] = m[i]; } st->leftover += bytes; } } } // namespace poly1305_donna
0
bitcoin/src
bitcoin/src/crypto/hmac_sha512.cpp
// Copyright (c) 2014-2018 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include <crypto/hmac_sha512.h> #include <string.h> CHMAC_SHA512::CHMAC_SHA512(const unsigned char* key, size_t keylen) { unsigned char rkey[128]; if (keylen <= 128) { memcpy(rkey, key, keylen); memset(rkey + keylen, 0, 128 - keylen); } else { CSHA512().Write(key, keylen).Finalize(rkey); memset(rkey + 64, 0, 64); } for (int n = 0; n < 128; n++) rkey[n] ^= 0x5c; outer.Write(rkey, 128); for (int n = 0; n < 128; n++) rkey[n] ^= 0x5c ^ 0x36; inner.Write(rkey, 128); } void CHMAC_SHA512::Finalize(unsigned char hash[OUTPUT_SIZE]) { unsigned char temp[64]; inner.Finalize(temp); outer.Write(temp, 64).Finalize(hash); }
0
bitcoin/src
bitcoin/src/crypto/common.h
// Copyright (c) 2014-2020 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_CRYPTO_COMMON_H #define BITCOIN_CRYPTO_COMMON_H #if defined(HAVE_CONFIG_H) #include <config/bitcoin-config.h> #endif #include <stdint.h> #include <string.h> #include <compat/endian.h> uint16_t static inline ReadLE16(const unsigned char* ptr) { uint16_t x; memcpy(&x, ptr, 2); return le16toh(x); } uint32_t static inline ReadLE32(const unsigned char* ptr) { uint32_t x; memcpy(&x, ptr, 4); return le32toh(x); } uint64_t static inline ReadLE64(const unsigned char* ptr) { uint64_t x; memcpy(&x, ptr, 8); return le64toh(x); } void static inline WriteLE16(unsigned char* ptr, uint16_t x) { uint16_t v = htole16(x); memcpy(ptr, &v, 2); } void static inline WriteLE32(unsigned char* ptr, uint32_t x) { uint32_t v = htole32(x); memcpy(ptr, &v, 4); } void static inline WriteLE64(unsigned char* ptr, uint64_t x) { uint64_t v = htole64(x); memcpy(ptr, &v, 8); } uint16_t static inline ReadBE16(const unsigned char* ptr) { uint16_t x; memcpy(&x, ptr, 2); return be16toh(x); } uint32_t static inline ReadBE32(const unsigned char* ptr) { uint32_t x; memcpy(&x, ptr, 4); return be32toh(x); } uint64_t static inline ReadBE64(const unsigned char* ptr) { uint64_t x; memcpy(&x, ptr, 8); return be64toh(x); } void static inline WriteBE32(unsigned char* ptr, uint32_t x) { uint32_t v = htobe32(x); memcpy(ptr, &v, 4); } void static inline WriteBE64(unsigned char* ptr, uint64_t x) { uint64_t v = htobe64(x); memcpy(ptr, &v, 8); } /** Return the smallest number n such that (x >> n) == 0 (or 64 if the highest bit in x is set. */ uint64_t static inline CountBits(uint64_t x) { #if HAVE_BUILTIN_CLZL if (sizeof(unsigned long) >= sizeof(uint64_t)) { return x ? 8 * sizeof(unsigned long) - __builtin_clzl(x) : 0; } #endif #if HAVE_BUILTIN_CLZLL if (sizeof(unsigned long long) >= sizeof(uint64_t)) { return x ? 8 * sizeof(unsigned long long) - __builtin_clzll(x) : 0; } #endif int ret = 0; while (x) { x >>= 1; ++ret; } return ret; } #endif // BITCOIN_CRYPTO_COMMON_H
0
bitcoin/src
bitcoin/src/crypto/aes.h
// Copyright (c) 2015-2019 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. // // C++ wrapper around ctaes, a constant-time AES implementation #ifndef BITCOIN_CRYPTO_AES_H #define BITCOIN_CRYPTO_AES_H extern "C" { #include <crypto/ctaes/ctaes.h> } static const int AES_BLOCKSIZE = 16; static const int AES256_KEYSIZE = 32; /** An encryption class for AES-256. */ class AES256Encrypt { private: AES256_ctx ctx; public: explicit AES256Encrypt(const unsigned char key[32]); ~AES256Encrypt(); void Encrypt(unsigned char ciphertext[16], const unsigned char plaintext[16]) const; }; /** A decryption class for AES-256. */ class AES256Decrypt { private: AES256_ctx ctx; public: explicit AES256Decrypt(const unsigned char key[32]); ~AES256Decrypt(); void Decrypt(unsigned char plaintext[16], const unsigned char ciphertext[16]) const; }; class AES256CBCEncrypt { public: AES256CBCEncrypt(const unsigned char key[AES256_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn); ~AES256CBCEncrypt(); int Encrypt(const unsigned char* data, int size, unsigned char* out) const; private: const AES256Encrypt enc; const bool pad; unsigned char iv[AES_BLOCKSIZE]; }; class AES256CBCDecrypt { public: AES256CBCDecrypt(const unsigned char key[AES256_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn); ~AES256CBCDecrypt(); int Decrypt(const unsigned char* data, int size, unsigned char* out) const; private: const AES256Decrypt dec; const bool pad; unsigned char iv[AES_BLOCKSIZE]; }; #endif // BITCOIN_CRYPTO_AES_H
0
bitcoin/src
bitcoin/src/crypto/sha256_arm_shani.cpp
// Copyright (c) 2022 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. // // Based on https://github.com/noloader/SHA-Intrinsics/blob/master/sha256-arm.c, // Written and placed in public domain by Jeffrey Walton. // Based on code from ARM, and by Johannes Schneiders, Skip Hovsmith and // Barry O'Rourke for the mbedTLS project. // Variant specialized for 64-byte inputs added by Pieter Wuille. #ifdef ENABLE_ARM_SHANI #include <array> #include <cstdint> #include <cstddef> #include <arm_acle.h> #include <arm_neon.h> namespace { alignas(uint32x4_t) static constexpr std::array<uint32_t, 64> K = { 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5, 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5, 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3, 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174, 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC, 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA, 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7, 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967, 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13, 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85, 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3, 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070, 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5, 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3, 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208, 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2, }; } namespace sha256_arm_shani { void Transform(uint32_t* s, const unsigned char* chunk, size_t blocks) { uint32x4_t STATE0, STATE1, ABEF_SAVE, CDGH_SAVE; uint32x4_t MSG0, MSG1, MSG2, MSG3; uint32x4_t TMP0, TMP2; // Load state STATE0 = vld1q_u32(&s[0]); STATE1 = vld1q_u32(&s[4]); while (blocks--) { // Save state ABEF_SAVE = STATE0; CDGH_SAVE = STATE1; // Load and convert input chunk to Big Endian MSG0 = vreinterpretq_u32_u8(vrev32q_u8(vld1q_u8(chunk + 0))); MSG1 = vreinterpretq_u32_u8(vrev32q_u8(vld1q_u8(chunk + 16))); MSG2 = vreinterpretq_u32_u8(vrev32q_u8(vld1q_u8(chunk + 32))); MSG3 = vreinterpretq_u32_u8(vrev32q_u8(vld1q_u8(chunk + 48))); chunk += 64; // Original implementation preloaded message and constant addition which was 1-3% slower. // Now included as first step in quad round code saving one Q Neon register // "TMP0 = vaddq_u32(MSG0, vld1q_u32(&K[0]));" // Rounds 1-4 TMP0 = vaddq_u32(MSG0, vld1q_u32(&K[0])); TMP2 = STATE0; MSG0 = vsha256su0q_u32(MSG0, MSG1); STATE0 = vsha256hq_u32(STATE0, STATE1, TMP0); STATE1 = vsha256h2q_u32(STATE1, TMP2, TMP0); MSG0 = vsha256su1q_u32(MSG0, MSG2, MSG3); // Rounds 5-8 TMP0 = vaddq_u32(MSG1, vld1q_u32(&K[4])); TMP2 = STATE0; MSG1 = vsha256su0q_u32(MSG1, MSG2); STATE0 = vsha256hq_u32(STATE0, STATE1, TMP0); STATE1 = vsha256h2q_u32(STATE1, TMP2, TMP0); MSG1 = vsha256su1q_u32(MSG1, MSG3, MSG0); // Rounds 9-12 TMP0 = vaddq_u32(MSG2, vld1q_u32(&K[8])); TMP2 = STATE0; MSG2 = vsha256su0q_u32(MSG2, MSG3); STATE0 = vsha256hq_u32(STATE0, STATE1, TMP0); STATE1 = vsha256h2q_u32(STATE1, TMP2, TMP0); MSG2 = vsha256su1q_u32(MSG2, MSG0, MSG1); // Rounds 13-16 TMP0 = vaddq_u32(MSG3, vld1q_u32(&K[12])); TMP2 = STATE0; MSG3 = vsha256su0q_u32(MSG3, MSG0); STATE0 = vsha256hq_u32(STATE0, STATE1, TMP0); STATE1 = vsha256h2q_u32(STATE1, TMP2, TMP0); MSG3 = vsha256su1q_u32(MSG3, MSG1, MSG2); // Rounds 17-20 TMP0 = vaddq_u32(MSG0, vld1q_u32(&K[16])); TMP2 = STATE0; MSG0 = vsha256su0q_u32(MSG0, MSG1); STATE0 = vsha256hq_u32(STATE0, STATE1, TMP0); STATE1 = vsha256h2q_u32(STATE1, TMP2, TMP0); MSG0 = vsha256su1q_u32(MSG0, MSG2, MSG3); // Rounds 21-24 TMP0 = vaddq_u32(MSG1, vld1q_u32(&K[20])); TMP2 = STATE0; MSG1 = vsha256su0q_u32(MSG1, MSG2); STATE0 = vsha256hq_u32(STATE0, STATE1, TMP0); STATE1 = vsha256h2q_u32(STATE1, TMP2, TMP0); MSG1 = vsha256su1q_u32(MSG1, MSG3, MSG0); // Rounds 25-28 TMP0 = vaddq_u32(MSG2, vld1q_u32(&K[24])); TMP2 = STATE0; MSG2 = vsha256su0q_u32(MSG2, MSG3); STATE0 = vsha256hq_u32(STATE0, STATE1, TMP0); STATE1 = vsha256h2q_u32(STATE1, TMP2, TMP0); MSG2 = vsha256su1q_u32(MSG2, MSG0, MSG1); // Rounds 29-32 TMP0 = vaddq_u32(MSG3, vld1q_u32(&K[28])); TMP2 = STATE0; MSG3 = vsha256su0q_u32(MSG3, MSG0); STATE0 = vsha256hq_u32(STATE0, STATE1, TMP0); STATE1 = vsha256h2q_u32(STATE1, TMP2, TMP0); MSG3 = vsha256su1q_u32(MSG3, MSG1, MSG2); // Rounds 33-36 TMP0 = vaddq_u32(MSG0, vld1q_u32(&K[32])); TMP2 = STATE0; MSG0 = vsha256su0q_u32(MSG0, MSG1); STATE0 = vsha256hq_u32(STATE0, STATE1, TMP0); STATE1 = vsha256h2q_u32(STATE1, TMP2, TMP0); MSG0 = vsha256su1q_u32(MSG0, MSG2, MSG3); // Rounds 37-40 TMP0 = vaddq_u32(MSG1, vld1q_u32(&K[36])); TMP2 = STATE0; MSG1 = vsha256su0q_u32(MSG1, MSG2); STATE0 = vsha256hq_u32(STATE0, STATE1, TMP0); STATE1 = vsha256h2q_u32(STATE1, TMP2, TMP0); MSG1 = vsha256su1q_u32(MSG1, MSG3, MSG0); // Rounds 41-44 TMP0 = vaddq_u32(MSG2, vld1q_u32(&K[40])); TMP2 = STATE0; MSG2 = vsha256su0q_u32(MSG2, MSG3); STATE0 = vsha256hq_u32(STATE0, STATE1, TMP0); STATE1 = vsha256h2q_u32(STATE1, TMP2, TMP0); MSG2 = vsha256su1q_u32(MSG2, MSG0, MSG1); // Rounds 45-48 TMP0 = vaddq_u32(MSG3, vld1q_u32(&K[44])); TMP2 = STATE0; MSG3 = vsha256su0q_u32(MSG3, MSG0); STATE0 = vsha256hq_u32(STATE0, STATE1, TMP0); STATE1 = vsha256h2q_u32(STATE1, TMP2, TMP0); MSG3 = vsha256su1q_u32(MSG3, MSG1, MSG2); // Rounds 49-52 TMP0 = vaddq_u32(MSG0, vld1q_u32(&K[48])); TMP2 = STATE0; STATE0 = vsha256hq_u32(STATE0, STATE1, TMP0); STATE1 = vsha256h2q_u32(STATE1, TMP2, TMP0); // Rounds 53-56 TMP0 = vaddq_u32(MSG1, vld1q_u32(&K[52])); TMP2 = STATE0; STATE0 = vsha256hq_u32(STATE0, STATE1, TMP0); STATE1 = vsha256h2q_u32(STATE1, TMP2, TMP0); // Rounds 57-60 TMP0 = vaddq_u32(MSG2, vld1q_u32(&K[56])); TMP2 = STATE0; STATE0 = vsha256hq_u32(STATE0, STATE1, TMP0); STATE1 = vsha256h2q_u32(STATE1, TMP2, TMP0); // Rounds 61-64 TMP0 = vaddq_u32(MSG3, vld1q_u32(&K[60])); TMP2 = STATE0; STATE0 = vsha256hq_u32(STATE0, STATE1, TMP0); STATE1 = vsha256h2q_u32(STATE1, TMP2, TMP0); // Update state STATE0 = vaddq_u32(STATE0, ABEF_SAVE); STATE1 = vaddq_u32(STATE1, CDGH_SAVE); } // Save final state vst1q_u32(&s[0], STATE0); vst1q_u32(&s[4], STATE1); } } namespace sha256d64_arm_shani { void Transform_2way(unsigned char* output, const unsigned char* input) { /* Initial state. */ alignas(uint32x4_t) static constexpr std::array<uint32_t, 8> INIT = { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 }; /* Precomputed message schedule for the 2nd transform. */ alignas(uint32x4_t) static constexpr std::array<uint32_t, 64> MIDS = { 0xc28a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf374, 0x649b69c1, 0xf0fe4786, 0x0fe1edc6, 0x240cf254, 0x4fe9346f, 0x6cc984be, 0x61b9411e, 0x16f988fa, 0xf2c65152, 0xa88e5a6d, 0xb019fc65, 0xb9d99ec7, 0x9a1231c3, 0xe70eeaa0, 0xfdb1232b, 0xc7353eb0, 0x3069bad5, 0xcb976d5f, 0x5a0f118f, 0xdc1eeefd, 0x0a35b689, 0xde0b7a04, 0x58f4ca9d, 0xe15d5b16, 0x007f3e86, 0x37088980, 0xa507ea32, 0x6fab9537, 0x17406110, 0x0d8cd6f1, 0xcdaa3b6d, 0xc0bbbe37, 0x83613bda, 0xdb48a363, 0x0b02e931, 0x6fd15ca7, 0x521afaca, 0x31338431, 0x6ed41a95, 0x6d437890, 0xc39c91f2, 0x9eccabbd, 0xb5c9a0e6, 0x532fb63c, 0xd2c741c6, 0x07237ea3, 0xa4954b68, 0x4c191d76 }; /* A few precomputed message schedule values for the 3rd transform. */ alignas(uint32x4_t) static constexpr std::array<uint32_t, 12> FINS = { 0x5807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x80000000, 0x00000000, 0x00000000, 0x00000000, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf274 }; /* Padding processed in the 3rd transform (byteswapped). */ alignas(uint32x4_t) static constexpr std::array<uint32_t, 8> FINAL = {0x80000000, 0, 0, 0, 0, 0, 0, 0x100}; uint32x4_t STATE0A, STATE0B, STATE1A, STATE1B, ABEF_SAVEA, ABEF_SAVEB, CDGH_SAVEA, CDGH_SAVEB; uint32x4_t MSG0A, MSG0B, MSG1A, MSG1B, MSG2A, MSG2B, MSG3A, MSG3B; uint32x4_t TMP0A, TMP0B, TMP2A, TMP2B, TMP; // Transform 1: Load state STATE0A = vld1q_u32(&INIT[0]); STATE0B = STATE0A; STATE1A = vld1q_u32(&INIT[4]); STATE1B = STATE1A; // Transform 1: Load and convert input chunk to Big Endian MSG0A = vreinterpretq_u32_u8(vrev32q_u8(vld1q_u8(input + 0))); MSG1A = vreinterpretq_u32_u8(vrev32q_u8(vld1q_u8(input + 16))); MSG2A = vreinterpretq_u32_u8(vrev32q_u8(vld1q_u8(input + 32))); MSG3A = vreinterpretq_u32_u8(vrev32q_u8(vld1q_u8(input + 48))); MSG0B = vreinterpretq_u32_u8(vrev32q_u8(vld1q_u8(input + 64))); MSG1B = vreinterpretq_u32_u8(vrev32q_u8(vld1q_u8(input + 80))); MSG2B = vreinterpretq_u32_u8(vrev32q_u8(vld1q_u8(input + 96))); MSG3B = vreinterpretq_u32_u8(vrev32q_u8(vld1q_u8(input + 112))); // Transform 1: Rounds 1-4 TMP = vld1q_u32(&K[0]); TMP0A = vaddq_u32(MSG0A, TMP); TMP0B = vaddq_u32(MSG0B, TMP); TMP2A = STATE0A; TMP2B = STATE0B; MSG0A = vsha256su0q_u32(MSG0A, MSG1A); MSG0B = vsha256su0q_u32(MSG0B, MSG1B); STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP0A); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP0B); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP0A); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP0B); MSG0A = vsha256su1q_u32(MSG0A, MSG2A, MSG3A); MSG0B = vsha256su1q_u32(MSG0B, MSG2B, MSG3B); // Transform 1: Rounds 5-8 TMP = vld1q_u32(&K[4]); TMP0A = vaddq_u32(MSG1A, TMP); TMP0B = vaddq_u32(MSG1B, TMP); TMP2A = STATE0A; TMP2B = STATE0B; MSG1A = vsha256su0q_u32(MSG1A, MSG2A); MSG1B = vsha256su0q_u32(MSG1B, MSG2B); STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP0A); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP0B); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP0A); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP0B); MSG1A = vsha256su1q_u32(MSG1A, MSG3A, MSG0A); MSG1B = vsha256su1q_u32(MSG1B, MSG3B, MSG0B); // Transform 1: Rounds 9-12 TMP = vld1q_u32(&K[8]); TMP0A = vaddq_u32(MSG2A, TMP); TMP0B = vaddq_u32(MSG2B, TMP); TMP2A = STATE0A; TMP2B = STATE0B; MSG2A = vsha256su0q_u32(MSG2A, MSG3A); MSG2B = vsha256su0q_u32(MSG2B, MSG3B); STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP0A); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP0B); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP0A); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP0B); MSG2A = vsha256su1q_u32(MSG2A, MSG0A, MSG1A); MSG2B = vsha256su1q_u32(MSG2B, MSG0B, MSG1B); // Transform 1: Rounds 13-16 TMP = vld1q_u32(&K[12]); TMP0A = vaddq_u32(MSG3A, TMP); TMP0B = vaddq_u32(MSG3B, TMP); TMP2A = STATE0A; TMP2B = STATE0B; MSG3A = vsha256su0q_u32(MSG3A, MSG0A); MSG3B = vsha256su0q_u32(MSG3B, MSG0B); STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP0A); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP0B); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP0A); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP0B); MSG3A = vsha256su1q_u32(MSG3A, MSG1A, MSG2A); MSG3B = vsha256su1q_u32(MSG3B, MSG1B, MSG2B); // Transform 1: Rounds 17-20 TMP = vld1q_u32(&K[16]); TMP0A = vaddq_u32(MSG0A, TMP); TMP0B = vaddq_u32(MSG0B, TMP); TMP2A = STATE0A; TMP2B = STATE0B; MSG0A = vsha256su0q_u32(MSG0A, MSG1A); MSG0B = vsha256su0q_u32(MSG0B, MSG1B); STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP0A); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP0B); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP0A); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP0B); MSG0A = vsha256su1q_u32(MSG0A, MSG2A, MSG3A); MSG0B = vsha256su1q_u32(MSG0B, MSG2B, MSG3B); // Transform 1: Rounds 21-24 TMP = vld1q_u32(&K[20]); TMP0A = vaddq_u32(MSG1A, TMP); TMP0B = vaddq_u32(MSG1B, TMP); TMP2A = STATE0A; TMP2B = STATE0B; MSG1A = vsha256su0q_u32(MSG1A, MSG2A); MSG1B = vsha256su0q_u32(MSG1B, MSG2B); STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP0A); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP0B); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP0A); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP0B); MSG1A = vsha256su1q_u32(MSG1A, MSG3A, MSG0A); MSG1B = vsha256su1q_u32(MSG1B, MSG3B, MSG0B); // Transform 1: Rounds 25-28 TMP = vld1q_u32(&K[24]); TMP0A = vaddq_u32(MSG2A, TMP); TMP0B = vaddq_u32(MSG2B, TMP); TMP2A = STATE0A; TMP2B = STATE0B; MSG2A = vsha256su0q_u32(MSG2A, MSG3A); MSG2B = vsha256su0q_u32(MSG2B, MSG3B); STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP0A); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP0B); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP0A); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP0B); MSG2A = vsha256su1q_u32(MSG2A, MSG0A, MSG1A); MSG2B = vsha256su1q_u32(MSG2B, MSG0B, MSG1B); // Transform 1: Rounds 29-32 TMP = vld1q_u32(&K[28]); TMP0A = vaddq_u32(MSG3A, TMP); TMP0B = vaddq_u32(MSG3B, TMP); TMP2A = STATE0A; TMP2B = STATE0B; MSG3A = vsha256su0q_u32(MSG3A, MSG0A); MSG3B = vsha256su0q_u32(MSG3B, MSG0B); STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP0A); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP0B); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP0A); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP0B); MSG3A = vsha256su1q_u32(MSG3A, MSG1A, MSG2A); MSG3B = vsha256su1q_u32(MSG3B, MSG1B, MSG2B); // Transform 1: Rounds 33-36 TMP = vld1q_u32(&K[32]); TMP0A = vaddq_u32(MSG0A, TMP); TMP0B = vaddq_u32(MSG0B, TMP); TMP2A = STATE0A; TMP2B = STATE0B; MSG0A = vsha256su0q_u32(MSG0A, MSG1A); MSG0B = vsha256su0q_u32(MSG0B, MSG1B); STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP0A); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP0B); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP0A); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP0B); MSG0A = vsha256su1q_u32(MSG0A, MSG2A, MSG3A); MSG0B = vsha256su1q_u32(MSG0B, MSG2B, MSG3B); // Transform 1: Rounds 37-40 TMP = vld1q_u32(&K[36]); TMP0A = vaddq_u32(MSG1A, TMP); TMP0B = vaddq_u32(MSG1B, TMP); TMP2A = STATE0A; TMP2B = STATE0B; MSG1A = vsha256su0q_u32(MSG1A, MSG2A); MSG1B = vsha256su0q_u32(MSG1B, MSG2B); STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP0A); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP0B); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP0A); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP0B); MSG1A = vsha256su1q_u32(MSG1A, MSG3A, MSG0A); MSG1B = vsha256su1q_u32(MSG1B, MSG3B, MSG0B); // Transform 1: Rounds 41-44 TMP = vld1q_u32(&K[40]); TMP0A = vaddq_u32(MSG2A, TMP); TMP0B = vaddq_u32(MSG2B, TMP); TMP2A = STATE0A; TMP2B = STATE0B; MSG2A = vsha256su0q_u32(MSG2A, MSG3A); MSG2B = vsha256su0q_u32(MSG2B, MSG3B); STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP0A); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP0B); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP0A); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP0B); MSG2A = vsha256su1q_u32(MSG2A, MSG0A, MSG1A); MSG2B = vsha256su1q_u32(MSG2B, MSG0B, MSG1B); // Transform 1: Rounds 45-48 TMP = vld1q_u32(&K[44]); TMP0A = vaddq_u32(MSG3A, TMP); TMP0B = vaddq_u32(MSG3B, TMP); TMP2A = STATE0A; TMP2B = STATE0B; MSG3A = vsha256su0q_u32(MSG3A, MSG0A); MSG3B = vsha256su0q_u32(MSG3B, MSG0B); STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP0A); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP0B); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP0A); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP0B); MSG3A = vsha256su1q_u32(MSG3A, MSG1A, MSG2A); MSG3B = vsha256su1q_u32(MSG3B, MSG1B, MSG2B); // Transform 1: Rounds 49-52 TMP = vld1q_u32(&K[48]); TMP0A = vaddq_u32(MSG0A, TMP); TMP0B = vaddq_u32(MSG0B, TMP); TMP2A = STATE0A; TMP2B = STATE0B; STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP0A); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP0B); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP0A); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP0B); // Transform 1: Rounds 53-56 TMP = vld1q_u32(&K[52]); TMP0A = vaddq_u32(MSG1A, TMP); TMP0B = vaddq_u32(MSG1B, TMP); TMP2A = STATE0A; TMP2B = STATE0B; STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP0A); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP0B); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP0A); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP0B); // Transform 1: Rounds 57-60 TMP = vld1q_u32(&K[56]); TMP0A = vaddq_u32(MSG2A, TMP); TMP0B = vaddq_u32(MSG2B, TMP); TMP2A = STATE0A; TMP2B = STATE0B; STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP0A); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP0B); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP0A); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP0B); // Transform 1: Rounds 61-64 TMP = vld1q_u32(&K[60]); TMP0A = vaddq_u32(MSG3A, TMP); TMP0B = vaddq_u32(MSG3B, TMP); TMP2A = STATE0A; TMP2B = STATE0B; STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP0A); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP0B); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP0A); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP0B); // Transform 1: Update state TMP = vld1q_u32(&INIT[0]); STATE0A = vaddq_u32(STATE0A, TMP); STATE0B = vaddq_u32(STATE0B, TMP); TMP = vld1q_u32(&INIT[4]); STATE1A = vaddq_u32(STATE1A, TMP); STATE1B = vaddq_u32(STATE1B, TMP); // Transform 2: Save state ABEF_SAVEA = STATE0A; ABEF_SAVEB = STATE0B; CDGH_SAVEA = STATE1A; CDGH_SAVEB = STATE1B; // Transform 2: Rounds 1-4 TMP = vld1q_u32(&MIDS[0]); TMP2A = STATE0A; TMP2B = STATE0B; STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP); // Transform 2: Rounds 5-8 TMP = vld1q_u32(&MIDS[4]); TMP2A = STATE0A; TMP2B = STATE0B; STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP); // Transform 2: Rounds 9-12 TMP = vld1q_u32(&MIDS[8]); TMP2A = STATE0A; TMP2B = STATE0B; STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP); // Transform 2: Rounds 13-16 TMP = vld1q_u32(&MIDS[12]); TMP2A = STATE0A; TMP2B = STATE0B; STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP); // Transform 2: Rounds 17-20 TMP = vld1q_u32(&MIDS[16]); TMP2A = STATE0A; TMP2B = STATE0B; STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP); // Transform 2: Rounds 21-24 TMP = vld1q_u32(&MIDS[20]); TMP2A = STATE0A; TMP2B = STATE0B; STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP); // Transform 2: Rounds 25-28 TMP = vld1q_u32(&MIDS[24]); TMP2A = STATE0A; TMP2B = STATE0B; STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP); // Transform 2: Rounds 29-32 TMP = vld1q_u32(&MIDS[28]); TMP2A = STATE0A; TMP2B = STATE0B; STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP); // Transform 2: Rounds 33-36 TMP = vld1q_u32(&MIDS[32]); TMP2A = STATE0A; TMP2B = STATE0B; STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP); // Transform 2: Rounds 37-40 TMP = vld1q_u32(&MIDS[36]); TMP2A = STATE0A; TMP2B = STATE0B; STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP); // Transform 2: Rounds 41-44 TMP = vld1q_u32(&MIDS[40]); TMP2A = STATE0A; TMP2B = STATE0B; STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP); // Transform 2: Rounds 45-48 TMP = vld1q_u32(&MIDS[44]); TMP2A = STATE0A; TMP2B = STATE0B; STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP); // Transform 2: Rounds 49-52 TMP = vld1q_u32(&MIDS[48]); TMP2A = STATE0A; TMP2B = STATE0B; STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP); // Transform 2: Rounds 53-56 TMP = vld1q_u32(&MIDS[52]); TMP2A = STATE0A; TMP2B = STATE0B; STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP); // Transform 2: Rounds 57-60 TMP = vld1q_u32(&MIDS[56]); TMP2A = STATE0A; TMP2B = STATE0B; STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP); // Transform 2: Rounds 61-64 TMP = vld1q_u32(&MIDS[60]); TMP2A = STATE0A; TMP2B = STATE0B; STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP); // Transform 2: Update state STATE0A = vaddq_u32(STATE0A, ABEF_SAVEA); STATE0B = vaddq_u32(STATE0B, ABEF_SAVEB); STATE1A = vaddq_u32(STATE1A, CDGH_SAVEA); STATE1B = vaddq_u32(STATE1B, CDGH_SAVEB); // Transform 3: Pad previous output MSG0A = STATE0A; MSG0B = STATE0B; MSG1A = STATE1A; MSG1B = STATE1B; MSG2A = vld1q_u32(&FINAL[0]); MSG2B = MSG2A; MSG3A = vld1q_u32(&FINAL[4]); MSG3B = MSG3A; // Transform 3: Load state STATE0A = vld1q_u32(&INIT[0]); STATE0B = STATE0A; STATE1A = vld1q_u32(&INIT[4]); STATE1B = STATE1A; // Transform 3: Rounds 1-4 TMP = vld1q_u32(&K[0]); TMP0A = vaddq_u32(MSG0A, TMP); TMP0B = vaddq_u32(MSG0B, TMP); TMP2A = STATE0A; TMP2B = STATE0B; MSG0A = vsha256su0q_u32(MSG0A, MSG1A); MSG0B = vsha256su0q_u32(MSG0B, MSG1B); STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP0A); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP0B); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP0A); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP0B); MSG0A = vsha256su1q_u32(MSG0A, MSG2A, MSG3A); MSG0B = vsha256su1q_u32(MSG0B, MSG2B, MSG3B); // Transform 3: Rounds 5-8 TMP = vld1q_u32(&K[4]); TMP0A = vaddq_u32(MSG1A, TMP); TMP0B = vaddq_u32(MSG1B, TMP); TMP2A = STATE0A; TMP2B = STATE0B; MSG1A = vsha256su0q_u32(MSG1A, MSG2A); MSG1B = vsha256su0q_u32(MSG1B, MSG2B); STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP0A); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP0B); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP0A); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP0B); MSG1A = vsha256su1q_u32(MSG1A, MSG3A, MSG0A); MSG1B = vsha256su1q_u32(MSG1B, MSG3B, MSG0B); // Transform 3: Rounds 9-12 TMP = vld1q_u32(&FINS[0]); TMP2A = STATE0A; TMP2B = STATE0B; MSG2A = vld1q_u32(&FINS[4]); MSG2B = MSG2A; STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP); MSG2A = vsha256su1q_u32(MSG2A, MSG0A, MSG1A); MSG2B = vsha256su1q_u32(MSG2B, MSG0B, MSG1B); // Transform 3: Rounds 13-16 TMP = vld1q_u32(&FINS[8]); TMP2A = STATE0A; TMP2B = STATE0B; MSG3A = vsha256su0q_u32(MSG3A, MSG0A); MSG3B = vsha256su0q_u32(MSG3B, MSG0B); STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP); MSG3A = vsha256su1q_u32(MSG3A, MSG1A, MSG2A); MSG3B = vsha256su1q_u32(MSG3B, MSG1B, MSG2B); // Transform 3: Rounds 17-20 TMP = vld1q_u32(&K[16]); TMP0A = vaddq_u32(MSG0A, TMP); TMP0B = vaddq_u32(MSG0B, TMP); TMP2A = STATE0A; TMP2B = STATE0B; MSG0A = vsha256su0q_u32(MSG0A, MSG1A); MSG0B = vsha256su0q_u32(MSG0B, MSG1B); STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP0A); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP0B); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP0A); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP0B); MSG0A = vsha256su1q_u32(MSG0A, MSG2A, MSG3A); MSG0B = vsha256su1q_u32(MSG0B, MSG2B, MSG3B); // Transform 3: Rounds 21-24 TMP = vld1q_u32(&K[20]); TMP0A = vaddq_u32(MSG1A, TMP); TMP0B = vaddq_u32(MSG1B, TMP); TMP2A = STATE0A; TMP2B = STATE0B; MSG1A = vsha256su0q_u32(MSG1A, MSG2A); MSG1B = vsha256su0q_u32(MSG1B, MSG2B); STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP0A); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP0B); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP0A); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP0B); MSG1A = vsha256su1q_u32(MSG1A, MSG3A, MSG0A); MSG1B = vsha256su1q_u32(MSG1B, MSG3B, MSG0B); // Transform 3: Rounds 25-28 TMP = vld1q_u32(&K[24]); TMP0A = vaddq_u32(MSG2A, TMP); TMP0B = vaddq_u32(MSG2B, TMP); TMP2A = STATE0A; TMP2B = STATE0B; MSG2A = vsha256su0q_u32(MSG2A, MSG3A); MSG2B = vsha256su0q_u32(MSG2B, MSG3B); STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP0A); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP0B); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP0A); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP0B); MSG2A = vsha256su1q_u32(MSG2A, MSG0A, MSG1A); MSG2B = vsha256su1q_u32(MSG2B, MSG0B, MSG1B); // Transform 3: Rounds 29-32 TMP = vld1q_u32(&K[28]); TMP0A = vaddq_u32(MSG3A, TMP); TMP0B = vaddq_u32(MSG3B, TMP); TMP2A = STATE0A; TMP2B = STATE0B; MSG3A = vsha256su0q_u32(MSG3A, MSG0A); MSG3B = vsha256su0q_u32(MSG3B, MSG0B); STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP0A); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP0B); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP0A); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP0B); MSG3A = vsha256su1q_u32(MSG3A, MSG1A, MSG2A); MSG3B = vsha256su1q_u32(MSG3B, MSG1B, MSG2B); // Transform 3: Rounds 33-36 TMP = vld1q_u32(&K[32]); TMP0A = vaddq_u32(MSG0A, TMP); TMP0B = vaddq_u32(MSG0B, TMP); TMP2A = STATE0A; TMP2B = STATE0B; MSG0A = vsha256su0q_u32(MSG0A, MSG1A); MSG0B = vsha256su0q_u32(MSG0B, MSG1B); STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP0A); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP0B); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP0A); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP0B); MSG0A = vsha256su1q_u32(MSG0A, MSG2A, MSG3A); MSG0B = vsha256su1q_u32(MSG0B, MSG2B, MSG3B); // Transform 3: Rounds 37-40 TMP = vld1q_u32(&K[36]); TMP0A = vaddq_u32(MSG1A, TMP); TMP0B = vaddq_u32(MSG1B, TMP); TMP2A = STATE0A; TMP2B = STATE0B; MSG1A = vsha256su0q_u32(MSG1A, MSG2A); MSG1B = vsha256su0q_u32(MSG1B, MSG2B); STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP0A); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP0B); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP0A); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP0B); MSG1A = vsha256su1q_u32(MSG1A, MSG3A, MSG0A); MSG1B = vsha256su1q_u32(MSG1B, MSG3B, MSG0B); // Transform 3: Rounds 41-44 TMP = vld1q_u32(&K[40]); TMP0A = vaddq_u32(MSG2A, TMP); TMP0B = vaddq_u32(MSG2B, TMP); TMP2A = STATE0A; TMP2B = STATE0B; MSG2A = vsha256su0q_u32(MSG2A, MSG3A); MSG2B = vsha256su0q_u32(MSG2B, MSG3B); STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP0A); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP0B); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP0A); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP0B); MSG2A = vsha256su1q_u32(MSG2A, MSG0A, MSG1A); MSG2B = vsha256su1q_u32(MSG2B, MSG0B, MSG1B); // Transform 3: Rounds 45-48 TMP = vld1q_u32(&K[44]); TMP0A = vaddq_u32(MSG3A, TMP); TMP0B = vaddq_u32(MSG3B, TMP); TMP2A = STATE0A; TMP2B = STATE0B; MSG3A = vsha256su0q_u32(MSG3A, MSG0A); MSG3B = vsha256su0q_u32(MSG3B, MSG0B); STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP0A); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP0B); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP0A); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP0B); MSG3A = vsha256su1q_u32(MSG3A, MSG1A, MSG2A); MSG3B = vsha256su1q_u32(MSG3B, MSG1B, MSG2B); // Transform 3: Rounds 49-52 TMP = vld1q_u32(&K[48]); TMP0A = vaddq_u32(MSG0A, TMP); TMP0B = vaddq_u32(MSG0B, TMP); TMP2A = STATE0A; TMP2B = STATE0B; STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP0A); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP0B); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP0A); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP0B); // Transform 3: Rounds 53-56 TMP = vld1q_u32(&K[52]); TMP0A = vaddq_u32(MSG1A, TMP); TMP0B = vaddq_u32(MSG1B, TMP); TMP2A = STATE0A; TMP2B = STATE0B; STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP0A); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP0B); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP0A); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP0B); // Transform 3: Rounds 57-60 TMP = vld1q_u32(&K[56]); TMP0A = vaddq_u32(MSG2A, TMP); TMP0B = vaddq_u32(MSG2B, TMP); TMP2A = STATE0A; TMP2B = STATE0B; STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP0A); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP0B); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP0A); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP0B); // Transform 3: Rounds 61-64 TMP = vld1q_u32(&K[60]); TMP0A = vaddq_u32(MSG3A, TMP); TMP0B = vaddq_u32(MSG3B, TMP); TMP2A = STATE0A; TMP2B = STATE0B; STATE0A = vsha256hq_u32(STATE0A, STATE1A, TMP0A); STATE0B = vsha256hq_u32(STATE0B, STATE1B, TMP0B); STATE1A = vsha256h2q_u32(STATE1A, TMP2A, TMP0A); STATE1B = vsha256h2q_u32(STATE1B, TMP2B, TMP0B); // Transform 3: Update state TMP = vld1q_u32(&INIT[0]); STATE0A = vaddq_u32(STATE0A, TMP); STATE0B = vaddq_u32(STATE0B, TMP); TMP = vld1q_u32(&INIT[4]); STATE1A = vaddq_u32(STATE1A, TMP); STATE1B = vaddq_u32(STATE1B, TMP); // Store result vst1q_u8(output, vrev32q_u8(vreinterpretq_u8_u32(STATE0A))); vst1q_u8(output + 16, vrev32q_u8(vreinterpretq_u8_u32(STATE1A))); vst1q_u8(output + 32, vrev32q_u8(vreinterpretq_u8_u32(STATE0B))); vst1q_u8(output + 48, vrev32q_u8(vreinterpretq_u8_u32(STATE1B))); } } #endif
0
bitcoin/src
bitcoin/src/crypto/sha256.cpp
// Copyright (c) 2014-2022 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include <crypto/sha256.h> #include <crypto/common.h> #include <assert.h> #include <string.h> #include <compat/cpuid.h> #if defined(__linux__) && defined(ENABLE_ARM_SHANI) && !defined(BUILD_BITCOIN_INTERNAL) #include <sys/auxv.h> #include <asm/hwcap.h> #endif #if defined(MAC_OSX) && defined(ENABLE_ARM_SHANI) && !defined(BUILD_BITCOIN_INTERNAL) #include <sys/types.h> #include <sys/sysctl.h> #endif #if defined(__x86_64__) || defined(__amd64__) || defined(__i386__) #if defined(USE_ASM) namespace sha256_sse4 { void Transform(uint32_t* s, const unsigned char* chunk, size_t blocks); } #endif #endif namespace sha256d64_sse41 { void Transform_4way(unsigned char* out, const unsigned char* in); } namespace sha256d64_avx2 { void Transform_8way(unsigned char* out, const unsigned char* in); } namespace sha256d64_x86_shani { void Transform_2way(unsigned char* out, const unsigned char* in); } namespace sha256_x86_shani { void Transform(uint32_t* s, const unsigned char* chunk, size_t blocks); } namespace sha256_arm_shani { void Transform(uint32_t* s, const unsigned char* chunk, size_t blocks); } namespace sha256d64_arm_shani { void Transform_2way(unsigned char* out, const unsigned char* in); } // Internal implementation code. namespace { /// Internal SHA-256 implementation. namespace sha256 { uint32_t inline Ch(uint32_t x, uint32_t y, uint32_t z) { return z ^ (x & (y ^ z)); } uint32_t inline Maj(uint32_t x, uint32_t y, uint32_t z) { return (x & y) | (z & (x | y)); } uint32_t inline Sigma0(uint32_t x) { return (x >> 2 | x << 30) ^ (x >> 13 | x << 19) ^ (x >> 22 | x << 10); } uint32_t inline Sigma1(uint32_t x) { return (x >> 6 | x << 26) ^ (x >> 11 | x << 21) ^ (x >> 25 | x << 7); } uint32_t inline sigma0(uint32_t x) { return (x >> 7 | x << 25) ^ (x >> 18 | x << 14) ^ (x >> 3); } uint32_t inline sigma1(uint32_t x) { return (x >> 17 | x << 15) ^ (x >> 19 | x << 13) ^ (x >> 10); } /** One round of SHA-256. */ void inline Round(uint32_t a, uint32_t b, uint32_t c, uint32_t& d, uint32_t e, uint32_t f, uint32_t g, uint32_t& h, uint32_t k) { uint32_t t1 = h + Sigma1(e) + Ch(e, f, g) + k; uint32_t t2 = Sigma0(a) + Maj(a, b, c); d += t1; h = t1 + t2; } /** Initialize SHA-256 state. */ void inline Initialize(uint32_t* s) { s[0] = 0x6a09e667ul; s[1] = 0xbb67ae85ul; s[2] = 0x3c6ef372ul; s[3] = 0xa54ff53aul; s[4] = 0x510e527ful; s[5] = 0x9b05688cul; s[6] = 0x1f83d9abul; s[7] = 0x5be0cd19ul; } /** Perform a number of SHA-256 transformations, processing 64-byte chunks. */ void Transform(uint32_t* s, const unsigned char* chunk, size_t blocks) { while (blocks--) { uint32_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]; uint32_t w0, w1, w2, w3, w4, w5, w6, w7, w8, w9, w10, w11, w12, w13, w14, w15; Round(a, b, c, d, e, f, g, h, 0x428a2f98 + (w0 = ReadBE32(chunk + 0))); Round(h, a, b, c, d, e, f, g, 0x71374491 + (w1 = ReadBE32(chunk + 4))); Round(g, h, a, b, c, d, e, f, 0xb5c0fbcf + (w2 = ReadBE32(chunk + 8))); Round(f, g, h, a, b, c, d, e, 0xe9b5dba5 + (w3 = ReadBE32(chunk + 12))); Round(e, f, g, h, a, b, c, d, 0x3956c25b + (w4 = ReadBE32(chunk + 16))); Round(d, e, f, g, h, a, b, c, 0x59f111f1 + (w5 = ReadBE32(chunk + 20))); Round(c, d, e, f, g, h, a, b, 0x923f82a4 + (w6 = ReadBE32(chunk + 24))); Round(b, c, d, e, f, g, h, a, 0xab1c5ed5 + (w7 = ReadBE32(chunk + 28))); Round(a, b, c, d, e, f, g, h, 0xd807aa98 + (w8 = ReadBE32(chunk + 32))); Round(h, a, b, c, d, e, f, g, 0x12835b01 + (w9 = ReadBE32(chunk + 36))); Round(g, h, a, b, c, d, e, f, 0x243185be + (w10 = ReadBE32(chunk + 40))); Round(f, g, h, a, b, c, d, e, 0x550c7dc3 + (w11 = ReadBE32(chunk + 44))); Round(e, f, g, h, a, b, c, d, 0x72be5d74 + (w12 = ReadBE32(chunk + 48))); Round(d, e, f, g, h, a, b, c, 0x80deb1fe + (w13 = ReadBE32(chunk + 52))); Round(c, d, e, f, g, h, a, b, 0x9bdc06a7 + (w14 = ReadBE32(chunk + 56))); Round(b, c, d, e, f, g, h, a, 0xc19bf174 + (w15 = ReadBE32(chunk + 60))); Round(a, b, c, d, e, f, g, h, 0xe49b69c1 + (w0 += sigma1(w14) + w9 + sigma0(w1))); Round(h, a, b, c, d, e, f, g, 0xefbe4786 + (w1 += sigma1(w15) + w10 + sigma0(w2))); Round(g, h, a, b, c, d, e, f, 0x0fc19dc6 + (w2 += sigma1(w0) + w11 + sigma0(w3))); Round(f, g, h, a, b, c, d, e, 0x240ca1cc + (w3 += sigma1(w1) + w12 + sigma0(w4))); Round(e, f, g, h, a, b, c, d, 0x2de92c6f + (w4 += sigma1(w2) + w13 + sigma0(w5))); Round(d, e, f, g, h, a, b, c, 0x4a7484aa + (w5 += sigma1(w3) + w14 + sigma0(w6))); Round(c, d, e, f, g, h, a, b, 0x5cb0a9dc + (w6 += sigma1(w4) + w15 + sigma0(w7))); Round(b, c, d, e, f, g, h, a, 0x76f988da + (w7 += sigma1(w5) + w0 + sigma0(w8))); Round(a, b, c, d, e, f, g, h, 0x983e5152 + (w8 += sigma1(w6) + w1 + sigma0(w9))); Round(h, a, b, c, d, e, f, g, 0xa831c66d + (w9 += sigma1(w7) + w2 + sigma0(w10))); Round(g, h, a, b, c, d, e, f, 0xb00327c8 + (w10 += sigma1(w8) + w3 + sigma0(w11))); Round(f, g, h, a, b, c, d, e, 0xbf597fc7 + (w11 += sigma1(w9) + w4 + sigma0(w12))); Round(e, f, g, h, a, b, c, d, 0xc6e00bf3 + (w12 += sigma1(w10) + w5 + sigma0(w13))); Round(d, e, f, g, h, a, b, c, 0xd5a79147 + (w13 += sigma1(w11) + w6 + sigma0(w14))); Round(c, d, e, f, g, h, a, b, 0x06ca6351 + (w14 += sigma1(w12) + w7 + sigma0(w15))); Round(b, c, d, e, f, g, h, a, 0x14292967 + (w15 += sigma1(w13) + w8 + sigma0(w0))); Round(a, b, c, d, e, f, g, h, 0x27b70a85 + (w0 += sigma1(w14) + w9 + sigma0(w1))); Round(h, a, b, c, d, e, f, g, 0x2e1b2138 + (w1 += sigma1(w15) + w10 + sigma0(w2))); Round(g, h, a, b, c, d, e, f, 0x4d2c6dfc + (w2 += sigma1(w0) + w11 + sigma0(w3))); Round(f, g, h, a, b, c, d, e, 0x53380d13 + (w3 += sigma1(w1) + w12 + sigma0(w4))); Round(e, f, g, h, a, b, c, d, 0x650a7354 + (w4 += sigma1(w2) + w13 + sigma0(w5))); Round(d, e, f, g, h, a, b, c, 0x766a0abb + (w5 += sigma1(w3) + w14 + sigma0(w6))); Round(c, d, e, f, g, h, a, b, 0x81c2c92e + (w6 += sigma1(w4) + w15 + sigma0(w7))); Round(b, c, d, e, f, g, h, a, 0x92722c85 + (w7 += sigma1(w5) + w0 + sigma0(w8))); Round(a, b, c, d, e, f, g, h, 0xa2bfe8a1 + (w8 += sigma1(w6) + w1 + sigma0(w9))); Round(h, a, b, c, d, e, f, g, 0xa81a664b + (w9 += sigma1(w7) + w2 + sigma0(w10))); Round(g, h, a, b, c, d, e, f, 0xc24b8b70 + (w10 += sigma1(w8) + w3 + sigma0(w11))); Round(f, g, h, a, b, c, d, e, 0xc76c51a3 + (w11 += sigma1(w9) + w4 + sigma0(w12))); Round(e, f, g, h, a, b, c, d, 0xd192e819 + (w12 += sigma1(w10) + w5 + sigma0(w13))); Round(d, e, f, g, h, a, b, c, 0xd6990624 + (w13 += sigma1(w11) + w6 + sigma0(w14))); Round(c, d, e, f, g, h, a, b, 0xf40e3585 + (w14 += sigma1(w12) + w7 + sigma0(w15))); Round(b, c, d, e, f, g, h, a, 0x106aa070 + (w15 += sigma1(w13) + w8 + sigma0(w0))); Round(a, b, c, d, e, f, g, h, 0x19a4c116 + (w0 += sigma1(w14) + w9 + sigma0(w1))); Round(h, a, b, c, d, e, f, g, 0x1e376c08 + (w1 += sigma1(w15) + w10 + sigma0(w2))); Round(g, h, a, b, c, d, e, f, 0x2748774c + (w2 += sigma1(w0) + w11 + sigma0(w3))); Round(f, g, h, a, b, c, d, e, 0x34b0bcb5 + (w3 += sigma1(w1) + w12 + sigma0(w4))); Round(e, f, g, h, a, b, c, d, 0x391c0cb3 + (w4 += sigma1(w2) + w13 + sigma0(w5))); Round(d, e, f, g, h, a, b, c, 0x4ed8aa4a + (w5 += sigma1(w3) + w14 + sigma0(w6))); Round(c, d, e, f, g, h, a, b, 0x5b9cca4f + (w6 += sigma1(w4) + w15 + sigma0(w7))); Round(b, c, d, e, f, g, h, a, 0x682e6ff3 + (w7 += sigma1(w5) + w0 + sigma0(w8))); Round(a, b, c, d, e, f, g, h, 0x748f82ee + (w8 += sigma1(w6) + w1 + sigma0(w9))); Round(h, a, b, c, d, e, f, g, 0x78a5636f + (w9 += sigma1(w7) + w2 + sigma0(w10))); Round(g, h, a, b, c, d, e, f, 0x84c87814 + (w10 += sigma1(w8) + w3 + sigma0(w11))); Round(f, g, h, a, b, c, d, e, 0x8cc70208 + (w11 += sigma1(w9) + w4 + sigma0(w12))); Round(e, f, g, h, a, b, c, d, 0x90befffa + (w12 += sigma1(w10) + w5 + sigma0(w13))); Round(d, e, f, g, h, a, b, c, 0xa4506ceb + (w13 += sigma1(w11) + w6 + sigma0(w14))); Round(c, d, e, f, g, h, a, b, 0xbef9a3f7 + (w14 + sigma1(w12) + w7 + sigma0(w15))); Round(b, c, d, e, f, g, h, a, 0xc67178f2 + (w15 + sigma1(w13) + w8 + sigma0(w0))); s[0] += a; s[1] += b; s[2] += c; s[3] += d; s[4] += e; s[5] += f; s[6] += g; s[7] += h; chunk += 64; } } void TransformD64(unsigned char* out, const unsigned char* in) { // Transform 1 uint32_t a = 0x6a09e667ul; uint32_t b = 0xbb67ae85ul; uint32_t c = 0x3c6ef372ul; uint32_t d = 0xa54ff53aul; uint32_t e = 0x510e527ful; uint32_t f = 0x9b05688cul; uint32_t g = 0x1f83d9abul; uint32_t h = 0x5be0cd19ul; uint32_t w0, w1, w2, w3, w4, w5, w6, w7, w8, w9, w10, w11, w12, w13, w14, w15; Round(a, b, c, d, e, f, g, h, 0x428a2f98ul + (w0 = ReadBE32(in + 0))); Round(h, a, b, c, d, e, f, g, 0x71374491ul + (w1 = ReadBE32(in + 4))); Round(g, h, a, b, c, d, e, f, 0xb5c0fbcful + (w2 = ReadBE32(in + 8))); Round(f, g, h, a, b, c, d, e, 0xe9b5dba5ul + (w3 = ReadBE32(in + 12))); Round(e, f, g, h, a, b, c, d, 0x3956c25bul + (w4 = ReadBE32(in + 16))); Round(d, e, f, g, h, a, b, c, 0x59f111f1ul + (w5 = ReadBE32(in + 20))); Round(c, d, e, f, g, h, a, b, 0x923f82a4ul + (w6 = ReadBE32(in + 24))); Round(b, c, d, e, f, g, h, a, 0xab1c5ed5ul + (w7 = ReadBE32(in + 28))); Round(a, b, c, d, e, f, g, h, 0xd807aa98ul + (w8 = ReadBE32(in + 32))); Round(h, a, b, c, d, e, f, g, 0x12835b01ul + (w9 = ReadBE32(in + 36))); Round(g, h, a, b, c, d, e, f, 0x243185beul + (w10 = ReadBE32(in + 40))); Round(f, g, h, a, b, c, d, e, 0x550c7dc3ul + (w11 = ReadBE32(in + 44))); Round(e, f, g, h, a, b, c, d, 0x72be5d74ul + (w12 = ReadBE32(in + 48))); Round(d, e, f, g, h, a, b, c, 0x80deb1feul + (w13 = ReadBE32(in + 52))); Round(c, d, e, f, g, h, a, b, 0x9bdc06a7ul + (w14 = ReadBE32(in + 56))); Round(b, c, d, e, f, g, h, a, 0xc19bf174ul + (w15 = ReadBE32(in + 60))); Round(a, b, c, d, e, f, g, h, 0xe49b69c1ul + (w0 += sigma1(w14) + w9 + sigma0(w1))); Round(h, a, b, c, d, e, f, g, 0xefbe4786ul + (w1 += sigma1(w15) + w10 + sigma0(w2))); Round(g, h, a, b, c, d, e, f, 0x0fc19dc6ul + (w2 += sigma1(w0) + w11 + sigma0(w3))); Round(f, g, h, a, b, c, d, e, 0x240ca1ccul + (w3 += sigma1(w1) + w12 + sigma0(w4))); Round(e, f, g, h, a, b, c, d, 0x2de92c6ful + (w4 += sigma1(w2) + w13 + sigma0(w5))); Round(d, e, f, g, h, a, b, c, 0x4a7484aaul + (w5 += sigma1(w3) + w14 + sigma0(w6))); Round(c, d, e, f, g, h, a, b, 0x5cb0a9dcul + (w6 += sigma1(w4) + w15 + sigma0(w7))); Round(b, c, d, e, f, g, h, a, 0x76f988daul + (w7 += sigma1(w5) + w0 + sigma0(w8))); Round(a, b, c, d, e, f, g, h, 0x983e5152ul + (w8 += sigma1(w6) + w1 + sigma0(w9))); Round(h, a, b, c, d, e, f, g, 0xa831c66dul + (w9 += sigma1(w7) + w2 + sigma0(w10))); Round(g, h, a, b, c, d, e, f, 0xb00327c8ul + (w10 += sigma1(w8) + w3 + sigma0(w11))); Round(f, g, h, a, b, c, d, e, 0xbf597fc7ul + (w11 += sigma1(w9) + w4 + sigma0(w12))); Round(e, f, g, h, a, b, c, d, 0xc6e00bf3ul + (w12 += sigma1(w10) + w5 + sigma0(w13))); Round(d, e, f, g, h, a, b, c, 0xd5a79147ul + (w13 += sigma1(w11) + w6 + sigma0(w14))); Round(c, d, e, f, g, h, a, b, 0x06ca6351ul + (w14 += sigma1(w12) + w7 + sigma0(w15))); Round(b, c, d, e, f, g, h, a, 0x14292967ul + (w15 += sigma1(w13) + w8 + sigma0(w0))); Round(a, b, c, d, e, f, g, h, 0x27b70a85ul + (w0 += sigma1(w14) + w9 + sigma0(w1))); Round(h, a, b, c, d, e, f, g, 0x2e1b2138ul + (w1 += sigma1(w15) + w10 + sigma0(w2))); Round(g, h, a, b, c, d, e, f, 0x4d2c6dfcul + (w2 += sigma1(w0) + w11 + sigma0(w3))); Round(f, g, h, a, b, c, d, e, 0x53380d13ul + (w3 += sigma1(w1) + w12 + sigma0(w4))); Round(e, f, g, h, a, b, c, d, 0x650a7354ul + (w4 += sigma1(w2) + w13 + sigma0(w5))); Round(d, e, f, g, h, a, b, c, 0x766a0abbul + (w5 += sigma1(w3) + w14 + sigma0(w6))); Round(c, d, e, f, g, h, a, b, 0x81c2c92eul + (w6 += sigma1(w4) + w15 + sigma0(w7))); Round(b, c, d, e, f, g, h, a, 0x92722c85ul + (w7 += sigma1(w5) + w0 + sigma0(w8))); Round(a, b, c, d, e, f, g, h, 0xa2bfe8a1ul + (w8 += sigma1(w6) + w1 + sigma0(w9))); Round(h, a, b, c, d, e, f, g, 0xa81a664bul + (w9 += sigma1(w7) + w2 + sigma0(w10))); Round(g, h, a, b, c, d, e, f, 0xc24b8b70ul + (w10 += sigma1(w8) + w3 + sigma0(w11))); Round(f, g, h, a, b, c, d, e, 0xc76c51a3ul + (w11 += sigma1(w9) + w4 + sigma0(w12))); Round(e, f, g, h, a, b, c, d, 0xd192e819ul + (w12 += sigma1(w10) + w5 + sigma0(w13))); Round(d, e, f, g, h, a, b, c, 0xd6990624ul + (w13 += sigma1(w11) + w6 + sigma0(w14))); Round(c, d, e, f, g, h, a, b, 0xf40e3585ul + (w14 += sigma1(w12) + w7 + sigma0(w15))); Round(b, c, d, e, f, g, h, a, 0x106aa070ul + (w15 += sigma1(w13) + w8 + sigma0(w0))); Round(a, b, c, d, e, f, g, h, 0x19a4c116ul + (w0 += sigma1(w14) + w9 + sigma0(w1))); Round(h, a, b, c, d, e, f, g, 0x1e376c08ul + (w1 += sigma1(w15) + w10 + sigma0(w2))); Round(g, h, a, b, c, d, e, f, 0x2748774cul + (w2 += sigma1(w0) + w11 + sigma0(w3))); Round(f, g, h, a, b, c, d, e, 0x34b0bcb5ul + (w3 += sigma1(w1) + w12 + sigma0(w4))); Round(e, f, g, h, a, b, c, d, 0x391c0cb3ul + (w4 += sigma1(w2) + w13 + sigma0(w5))); Round(d, e, f, g, h, a, b, c, 0x4ed8aa4aul + (w5 += sigma1(w3) + w14 + sigma0(w6))); Round(c, d, e, f, g, h, a, b, 0x5b9cca4ful + (w6 += sigma1(w4) + w15 + sigma0(w7))); Round(b, c, d, e, f, g, h, a, 0x682e6ff3ul + (w7 += sigma1(w5) + w0 + sigma0(w8))); Round(a, b, c, d, e, f, g, h, 0x748f82eeul + (w8 += sigma1(w6) + w1 + sigma0(w9))); Round(h, a, b, c, d, e, f, g, 0x78a5636ful + (w9 += sigma1(w7) + w2 + sigma0(w10))); Round(g, h, a, b, c, d, e, f, 0x84c87814ul + (w10 += sigma1(w8) + w3 + sigma0(w11))); Round(f, g, h, a, b, c, d, e, 0x8cc70208ul + (w11 += sigma1(w9) + w4 + sigma0(w12))); Round(e, f, g, h, a, b, c, d, 0x90befffaul + (w12 += sigma1(w10) + w5 + sigma0(w13))); Round(d, e, f, g, h, a, b, c, 0xa4506cebul + (w13 += sigma1(w11) + w6 + sigma0(w14))); Round(c, d, e, f, g, h, a, b, 0xbef9a3f7ul + (w14 + sigma1(w12) + w7 + sigma0(w15))); Round(b, c, d, e, f, g, h, a, 0xc67178f2ul + (w15 + sigma1(w13) + w8 + sigma0(w0))); a += 0x6a09e667ul; b += 0xbb67ae85ul; c += 0x3c6ef372ul; d += 0xa54ff53aul; e += 0x510e527ful; f += 0x9b05688cul; g += 0x1f83d9abul; h += 0x5be0cd19ul; uint32_t t0 = a, t1 = b, t2 = c, t3 = d, t4 = e, t5 = f, t6 = g, t7 = h; // Transform 2 Round(a, b, c, d, e, f, g, h, 0xc28a2f98ul); Round(h, a, b, c, d, e, f, g, 0x71374491ul); Round(g, h, a, b, c, d, e, f, 0xb5c0fbcful); Round(f, g, h, a, b, c, d, e, 0xe9b5dba5ul); Round(e, f, g, h, a, b, c, d, 0x3956c25bul); Round(d, e, f, g, h, a, b, c, 0x59f111f1ul); Round(c, d, e, f, g, h, a, b, 0x923f82a4ul); Round(b, c, d, e, f, g, h, a, 0xab1c5ed5ul); Round(a, b, c, d, e, f, g, h, 0xd807aa98ul); Round(h, a, b, c, d, e, f, g, 0x12835b01ul); Round(g, h, a, b, c, d, e, f, 0x243185beul); Round(f, g, h, a, b, c, d, e, 0x550c7dc3ul); Round(e, f, g, h, a, b, c, d, 0x72be5d74ul); Round(d, e, f, g, h, a, b, c, 0x80deb1feul); Round(c, d, e, f, g, h, a, b, 0x9bdc06a7ul); Round(b, c, d, e, f, g, h, a, 0xc19bf374ul); Round(a, b, c, d, e, f, g, h, 0x649b69c1ul); Round(h, a, b, c, d, e, f, g, 0xf0fe4786ul); Round(g, h, a, b, c, d, e, f, 0x0fe1edc6ul); Round(f, g, h, a, b, c, d, e, 0x240cf254ul); Round(e, f, g, h, a, b, c, d, 0x4fe9346ful); Round(d, e, f, g, h, a, b, c, 0x6cc984beul); Round(c, d, e, f, g, h, a, b, 0x61b9411eul); Round(b, c, d, e, f, g, h, a, 0x16f988faul); Round(a, b, c, d, e, f, g, h, 0xf2c65152ul); Round(h, a, b, c, d, e, f, g, 0xa88e5a6dul); Round(g, h, a, b, c, d, e, f, 0xb019fc65ul); Round(f, g, h, a, b, c, d, e, 0xb9d99ec7ul); Round(e, f, g, h, a, b, c, d, 0x9a1231c3ul); Round(d, e, f, g, h, a, b, c, 0xe70eeaa0ul); Round(c, d, e, f, g, h, a, b, 0xfdb1232bul); Round(b, c, d, e, f, g, h, a, 0xc7353eb0ul); Round(a, b, c, d, e, f, g, h, 0x3069bad5ul); Round(h, a, b, c, d, e, f, g, 0xcb976d5ful); Round(g, h, a, b, c, d, e, f, 0x5a0f118ful); Round(f, g, h, a, b, c, d, e, 0xdc1eeefdul); Round(e, f, g, h, a, b, c, d, 0x0a35b689ul); Round(d, e, f, g, h, a, b, c, 0xde0b7a04ul); Round(c, d, e, f, g, h, a, b, 0x58f4ca9dul); Round(b, c, d, e, f, g, h, a, 0xe15d5b16ul); Round(a, b, c, d, e, f, g, h, 0x007f3e86ul); Round(h, a, b, c, d, e, f, g, 0x37088980ul); Round(g, h, a, b, c, d, e, f, 0xa507ea32ul); Round(f, g, h, a, b, c, d, e, 0x6fab9537ul); Round(e, f, g, h, a, b, c, d, 0x17406110ul); Round(d, e, f, g, h, a, b, c, 0x0d8cd6f1ul); Round(c, d, e, f, g, h, a, b, 0xcdaa3b6dul); Round(b, c, d, e, f, g, h, a, 0xc0bbbe37ul); Round(a, b, c, d, e, f, g, h, 0x83613bdaul); Round(h, a, b, c, d, e, f, g, 0xdb48a363ul); Round(g, h, a, b, c, d, e, f, 0x0b02e931ul); Round(f, g, h, a, b, c, d, e, 0x6fd15ca7ul); Round(e, f, g, h, a, b, c, d, 0x521afacaul); Round(d, e, f, g, h, a, b, c, 0x31338431ul); Round(c, d, e, f, g, h, a, b, 0x6ed41a95ul); Round(b, c, d, e, f, g, h, a, 0x6d437890ul); Round(a, b, c, d, e, f, g, h, 0xc39c91f2ul); Round(h, a, b, c, d, e, f, g, 0x9eccabbdul); Round(g, h, a, b, c, d, e, f, 0xb5c9a0e6ul); Round(f, g, h, a, b, c, d, e, 0x532fb63cul); Round(e, f, g, h, a, b, c, d, 0xd2c741c6ul); Round(d, e, f, g, h, a, b, c, 0x07237ea3ul); Round(c, d, e, f, g, h, a, b, 0xa4954b68ul); Round(b, c, d, e, f, g, h, a, 0x4c191d76ul); w0 = t0 + a; w1 = t1 + b; w2 = t2 + c; w3 = t3 + d; w4 = t4 + e; w5 = t5 + f; w6 = t6 + g; w7 = t7 + h; // Transform 3 a = 0x6a09e667ul; b = 0xbb67ae85ul; c = 0x3c6ef372ul; d = 0xa54ff53aul; e = 0x510e527ful; f = 0x9b05688cul; g = 0x1f83d9abul; h = 0x5be0cd19ul; Round(a, b, c, d, e, f, g, h, 0x428a2f98ul + w0); Round(h, a, b, c, d, e, f, g, 0x71374491ul + w1); Round(g, h, a, b, c, d, e, f, 0xb5c0fbcful + w2); Round(f, g, h, a, b, c, d, e, 0xe9b5dba5ul + w3); Round(e, f, g, h, a, b, c, d, 0x3956c25bul + w4); Round(d, e, f, g, h, a, b, c, 0x59f111f1ul + w5); Round(c, d, e, f, g, h, a, b, 0x923f82a4ul + w6); Round(b, c, d, e, f, g, h, a, 0xab1c5ed5ul + w7); Round(a, b, c, d, e, f, g, h, 0x5807aa98ul); Round(h, a, b, c, d, e, f, g, 0x12835b01ul); Round(g, h, a, b, c, d, e, f, 0x243185beul); Round(f, g, h, a, b, c, d, e, 0x550c7dc3ul); Round(e, f, g, h, a, b, c, d, 0x72be5d74ul); Round(d, e, f, g, h, a, b, c, 0x80deb1feul); Round(c, d, e, f, g, h, a, b, 0x9bdc06a7ul); Round(b, c, d, e, f, g, h, a, 0xc19bf274ul); Round(a, b, c, d, e, f, g, h, 0xe49b69c1ul + (w0 += sigma0(w1))); Round(h, a, b, c, d, e, f, g, 0xefbe4786ul + (w1 += 0xa00000ul + sigma0(w2))); Round(g, h, a, b, c, d, e, f, 0x0fc19dc6ul + (w2 += sigma1(w0) + sigma0(w3))); Round(f, g, h, a, b, c, d, e, 0x240ca1ccul + (w3 += sigma1(w1) + sigma0(w4))); Round(e, f, g, h, a, b, c, d, 0x2de92c6ful + (w4 += sigma1(w2) + sigma0(w5))); Round(d, e, f, g, h, a, b, c, 0x4a7484aaul + (w5 += sigma1(w3) + sigma0(w6))); Round(c, d, e, f, g, h, a, b, 0x5cb0a9dcul + (w6 += sigma1(w4) + 0x100ul + sigma0(w7))); Round(b, c, d, e, f, g, h, a, 0x76f988daul + (w7 += sigma1(w5) + w0 + 0x11002000ul)); Round(a, b, c, d, e, f, g, h, 0x983e5152ul + (w8 = 0x80000000ul + sigma1(w6) + w1)); Round(h, a, b, c, d, e, f, g, 0xa831c66dul + (w9 = sigma1(w7) + w2)); Round(g, h, a, b, c, d, e, f, 0xb00327c8ul + (w10 = sigma1(w8) + w3)); Round(f, g, h, a, b, c, d, e, 0xbf597fc7ul + (w11 = sigma1(w9) + w4)); Round(e, f, g, h, a, b, c, d, 0xc6e00bf3ul + (w12 = sigma1(w10) + w5)); Round(d, e, f, g, h, a, b, c, 0xd5a79147ul + (w13 = sigma1(w11) + w6)); Round(c, d, e, f, g, h, a, b, 0x06ca6351ul + (w14 = sigma1(w12) + w7 + 0x400022ul)); Round(b, c, d, e, f, g, h, a, 0x14292967ul + (w15 = 0x100ul + sigma1(w13) + w8 + sigma0(w0))); Round(a, b, c, d, e, f, g, h, 0x27b70a85ul + (w0 += sigma1(w14) + w9 + sigma0(w1))); Round(h, a, b, c, d, e, f, g, 0x2e1b2138ul + (w1 += sigma1(w15) + w10 + sigma0(w2))); Round(g, h, a, b, c, d, e, f, 0x4d2c6dfcul + (w2 += sigma1(w0) + w11 + sigma0(w3))); Round(f, g, h, a, b, c, d, e, 0x53380d13ul + (w3 += sigma1(w1) + w12 + sigma0(w4))); Round(e, f, g, h, a, b, c, d, 0x650a7354ul + (w4 += sigma1(w2) + w13 + sigma0(w5))); Round(d, e, f, g, h, a, b, c, 0x766a0abbul + (w5 += sigma1(w3) + w14 + sigma0(w6))); Round(c, d, e, f, g, h, a, b, 0x81c2c92eul + (w6 += sigma1(w4) + w15 + sigma0(w7))); Round(b, c, d, e, f, g, h, a, 0x92722c85ul + (w7 += sigma1(w5) + w0 + sigma0(w8))); Round(a, b, c, d, e, f, g, h, 0xa2bfe8a1ul + (w8 += sigma1(w6) + w1 + sigma0(w9))); Round(h, a, b, c, d, e, f, g, 0xa81a664bul + (w9 += sigma1(w7) + w2 + sigma0(w10))); Round(g, h, a, b, c, d, e, f, 0xc24b8b70ul + (w10 += sigma1(w8) + w3 + sigma0(w11))); Round(f, g, h, a, b, c, d, e, 0xc76c51a3ul + (w11 += sigma1(w9) + w4 + sigma0(w12))); Round(e, f, g, h, a, b, c, d, 0xd192e819ul + (w12 += sigma1(w10) + w5 + sigma0(w13))); Round(d, e, f, g, h, a, b, c, 0xd6990624ul + (w13 += sigma1(w11) + w6 + sigma0(w14))); Round(c, d, e, f, g, h, a, b, 0xf40e3585ul + (w14 += sigma1(w12) + w7 + sigma0(w15))); Round(b, c, d, e, f, g, h, a, 0x106aa070ul + (w15 += sigma1(w13) + w8 + sigma0(w0))); Round(a, b, c, d, e, f, g, h, 0x19a4c116ul + (w0 += sigma1(w14) + w9 + sigma0(w1))); Round(h, a, b, c, d, e, f, g, 0x1e376c08ul + (w1 += sigma1(w15) + w10 + sigma0(w2))); Round(g, h, a, b, c, d, e, f, 0x2748774cul + (w2 += sigma1(w0) + w11 + sigma0(w3))); Round(f, g, h, a, b, c, d, e, 0x34b0bcb5ul + (w3 += sigma1(w1) + w12 + sigma0(w4))); Round(e, f, g, h, a, b, c, d, 0x391c0cb3ul + (w4 += sigma1(w2) + w13 + sigma0(w5))); Round(d, e, f, g, h, a, b, c, 0x4ed8aa4aul + (w5 += sigma1(w3) + w14 + sigma0(w6))); Round(c, d, e, f, g, h, a, b, 0x5b9cca4ful + (w6 += sigma1(w4) + w15 + sigma0(w7))); Round(b, c, d, e, f, g, h, a, 0x682e6ff3ul + (w7 += sigma1(w5) + w0 + sigma0(w8))); Round(a, b, c, d, e, f, g, h, 0x748f82eeul + (w8 += sigma1(w6) + w1 + sigma0(w9))); Round(h, a, b, c, d, e, f, g, 0x78a5636ful + (w9 += sigma1(w7) + w2 + sigma0(w10))); Round(g, h, a, b, c, d, e, f, 0x84c87814ul + (w10 += sigma1(w8) + w3 + sigma0(w11))); Round(f, g, h, a, b, c, d, e, 0x8cc70208ul + (w11 += sigma1(w9) + w4 + sigma0(w12))); Round(e, f, g, h, a, b, c, d, 0x90befffaul + (w12 += sigma1(w10) + w5 + sigma0(w13))); Round(d, e, f, g, h, a, b, c, 0xa4506cebul + (w13 += sigma1(w11) + w6 + sigma0(w14))); Round(c, d, e, f, g, h, a, b, 0xbef9a3f7ul + (w14 + sigma1(w12) + w7 + sigma0(w15))); Round(b, c, d, e, f, g, h, a, 0xc67178f2ul + (w15 + sigma1(w13) + w8 + sigma0(w0))); // Output WriteBE32(out + 0, a + 0x6a09e667ul); WriteBE32(out + 4, b + 0xbb67ae85ul); WriteBE32(out + 8, c + 0x3c6ef372ul); WriteBE32(out + 12, d + 0xa54ff53aul); WriteBE32(out + 16, e + 0x510e527ful); WriteBE32(out + 20, f + 0x9b05688cul); WriteBE32(out + 24, g + 0x1f83d9abul); WriteBE32(out + 28, h + 0x5be0cd19ul); } } // namespace sha256 typedef void (*TransformType)(uint32_t*, const unsigned char*, size_t); typedef void (*TransformD64Type)(unsigned char*, const unsigned char*); template<TransformType tr> void TransformD64Wrapper(unsigned char* out, const unsigned char* in) { uint32_t s[8]; static const unsigned char padding1[64] = { 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0 }; unsigned char buffer2[64] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 }; sha256::Initialize(s); tr(s, in, 1); tr(s, padding1, 1); WriteBE32(buffer2 + 0, s[0]); WriteBE32(buffer2 + 4, s[1]); WriteBE32(buffer2 + 8, s[2]); WriteBE32(buffer2 + 12, s[3]); WriteBE32(buffer2 + 16, s[4]); WriteBE32(buffer2 + 20, s[5]); WriteBE32(buffer2 + 24, s[6]); WriteBE32(buffer2 + 28, s[7]); sha256::Initialize(s); tr(s, buffer2, 1); WriteBE32(out + 0, s[0]); WriteBE32(out + 4, s[1]); WriteBE32(out + 8, s[2]); WriteBE32(out + 12, s[3]); WriteBE32(out + 16, s[4]); WriteBE32(out + 20, s[5]); WriteBE32(out + 24, s[6]); WriteBE32(out + 28, s[7]); } TransformType Transform = sha256::Transform; TransformD64Type TransformD64 = sha256::TransformD64; TransformD64Type TransformD64_2way = nullptr; TransformD64Type TransformD64_4way = nullptr; TransformD64Type TransformD64_8way = nullptr; bool SelfTest() { // Input state (equal to the initial SHA256 state) static const uint32_t init[8] = { 0x6a09e667ul, 0xbb67ae85ul, 0x3c6ef372ul, 0xa54ff53aul, 0x510e527ful, 0x9b05688cul, 0x1f83d9abul, 0x5be0cd19ul }; // Some random input data to test with static const unsigned char data[641] = "-" // Intentionally not aligned "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do " "eiusmod tempor incididunt ut labore et dolore magna aliqua. Et m" "olestie ac feugiat sed lectus vestibulum mattis ullamcorper. Mor" "bi blandit cursus risus at ultrices mi tempus imperdiet nulla. N" "unc congue nisi vita suscipit tellus mauris. Imperdiet proin fer" "mentum leo vel orci. Massa tempor nec feugiat nisl pretium fusce" " id velit. Telus in metus vulputate eu scelerisque felis. Mi tem" "pus imperdiet nulla malesuada pellentesque. Tristique magna sit."; // Expected output state for hashing the i*64 first input bytes above (excluding SHA256 padding). static const uint32_t result[9][8] = { {0x6a09e667ul, 0xbb67ae85ul, 0x3c6ef372ul, 0xa54ff53aul, 0x510e527ful, 0x9b05688cul, 0x1f83d9abul, 0x5be0cd19ul}, {0x91f8ec6bul, 0x4da10fe3ul, 0x1c9c292cul, 0x45e18185ul, 0x435cc111ul, 0x3ca26f09ul, 0xeb954caeul, 0x402a7069ul}, {0xcabea5acul, 0x374fb97cul, 0x182ad996ul, 0x7bd69cbful, 0x450ff900ul, 0xc1d2be8aul, 0x6a41d505ul, 0xe6212dc3ul}, {0xbcff09d6ul, 0x3e76f36eul, 0x3ecb2501ul, 0x78866e97ul, 0xe1c1e2fdul, 0x32f4eafful, 0x8aa6c4e5ul, 0xdfc024bcul}, {0xa08c5d94ul, 0x0a862f93ul, 0x6b7f2f40ul, 0x8f9fae76ul, 0x6d40439ful, 0x79dcee0cul, 0x3e39ff3aul, 0xdc3bdbb1ul}, {0x216a0895ul, 0x9f1a3662ul, 0xe99946f9ul, 0x87ba4364ul, 0x0fb5db2cul, 0x12bed3d3ul, 0x6689c0c7ul, 0x292f1b04ul}, {0xca3067f8ul, 0xbc8c2656ul, 0x37cb7e0dul, 0x9b6b8b0ful, 0x46dc380bul, 0xf1287f57ul, 0xc42e4b23ul, 0x3fefe94dul}, {0x3e4c4039ul, 0xbb6fca8cul, 0x6f27d2f7ul, 0x301e44a4ul, 0x8352ba14ul, 0x5769ce37ul, 0x48a1155ful, 0xc0e1c4c6ul}, {0xfe2fa9ddul, 0x69d0862bul, 0x1ae0db23ul, 0x471f9244ul, 0xf55c0145ul, 0xc30f9c3bul, 0x40a84ea0ul, 0x5b8a266cul}, }; // Expected output for each of the individual 8 64-byte messages under full double SHA256 (including padding). static const unsigned char result_d64[256] = { 0x09, 0x3a, 0xc4, 0xd0, 0x0f, 0xf7, 0x57, 0xe1, 0x72, 0x85, 0x79, 0x42, 0xfe, 0xe7, 0xe0, 0xa0, 0xfc, 0x52, 0xd7, 0xdb, 0x07, 0x63, 0x45, 0xfb, 0x53, 0x14, 0x7d, 0x17, 0x22, 0x86, 0xf0, 0x52, 0x48, 0xb6, 0x11, 0x9e, 0x6e, 0x48, 0x81, 0x6d, 0xcc, 0x57, 0x1f, 0xb2, 0x97, 0xa8, 0xd5, 0x25, 0x9b, 0x82, 0xaa, 0x89, 0xe2, 0xfd, 0x2d, 0x56, 0xe8, 0x28, 0x83, 0x0b, 0xe2, 0xfa, 0x53, 0xb7, 0xd6, 0x6b, 0x07, 0x85, 0x83, 0xb0, 0x10, 0xa2, 0xf5, 0x51, 0x3c, 0xf9, 0x60, 0x03, 0xab, 0x45, 0x6c, 0x15, 0x6e, 0xef, 0xb5, 0xac, 0x3e, 0x6c, 0xdf, 0xb4, 0x92, 0x22, 0x2d, 0xce, 0xbf, 0x3e, 0xe9, 0xe5, 0xf6, 0x29, 0x0e, 0x01, 0x4f, 0xd2, 0xd4, 0x45, 0x65, 0xb3, 0xbb, 0xf2, 0x4c, 0x16, 0x37, 0x50, 0x3c, 0x6e, 0x49, 0x8c, 0x5a, 0x89, 0x2b, 0x1b, 0xab, 0xc4, 0x37, 0xd1, 0x46, 0xe9, 0x3d, 0x0e, 0x85, 0xa2, 0x50, 0x73, 0xa1, 0x5e, 0x54, 0x37, 0xd7, 0x94, 0x17, 0x56, 0xc2, 0xd8, 0xe5, 0x9f, 0xed, 0x4e, 0xae, 0x15, 0x42, 0x06, 0x0d, 0x74, 0x74, 0x5e, 0x24, 0x30, 0xce, 0xd1, 0x9e, 0x50, 0xa3, 0x9a, 0xb8, 0xf0, 0x4a, 0x57, 0x69, 0x78, 0x67, 0x12, 0x84, 0x58, 0xbe, 0xc7, 0x36, 0xaa, 0xee, 0x7c, 0x64, 0xa3, 0x76, 0xec, 0xff, 0x55, 0x41, 0x00, 0x2a, 0x44, 0x68, 0x4d, 0xb6, 0x53, 0x9e, 0x1c, 0x95, 0xb7, 0xca, 0xdc, 0x7f, 0x7d, 0x74, 0x27, 0x5c, 0x8e, 0xa6, 0x84, 0xb5, 0xac, 0x87, 0xa9, 0xf3, 0xff, 0x75, 0xf2, 0x34, 0xcd, 0x1a, 0x3b, 0x82, 0x2c, 0x2b, 0x4e, 0x6a, 0x46, 0x30, 0xa6, 0x89, 0x86, 0x23, 0xac, 0xf8, 0xa5, 0x15, 0xe9, 0x0a, 0xaa, 0x1e, 0x9a, 0xd7, 0x93, 0x6b, 0x28, 0xe4, 0x3b, 0xfd, 0x59, 0xc6, 0xed, 0x7c, 0x5f, 0xa5, 0x41, 0xcb, 0x51 }; // Test Transform() for 0 through 8 transformations. for (size_t i = 0; i <= 8; ++i) { uint32_t state[8]; std::copy(init, init + 8, state); Transform(state, data + 1, i); if (!std::equal(state, state + 8, result[i])) return false; } // Test TransformD64 unsigned char out[32]; TransformD64(out, data + 1); if (!std::equal(out, out + 32, result_d64)) return false; // Test TransformD64_2way, if available. if (TransformD64_2way) { unsigned char out[64]; TransformD64_2way(out, data + 1); if (!std::equal(out, out + 64, result_d64)) return false; } // Test TransformD64_4way, if available. if (TransformD64_4way) { unsigned char out[128]; TransformD64_4way(out, data + 1); if (!std::equal(out, out + 128, result_d64)) return false; } // Test TransformD64_8way, if available. if (TransformD64_8way) { unsigned char out[256]; TransformD64_8way(out, data + 1); if (!std::equal(out, out + 256, result_d64)) return false; } return true; } #if defined(USE_ASM) && (defined(__x86_64__) || defined(__amd64__) || defined(__i386__)) /** Check whether the OS has enabled AVX registers. */ bool AVXEnabled() { uint32_t a, d; __asm__("xgetbv" : "=a"(a), "=d"(d) : "c"(0)); return (a & 6) == 6; } #endif } // namespace std::string SHA256AutoDetect(sha256_implementation::UseImplementation use_implementation) { std::string ret = "standard"; Transform = sha256::Transform; TransformD64 = sha256::TransformD64; TransformD64_2way = nullptr; TransformD64_4way = nullptr; TransformD64_8way = nullptr; #if defined(USE_ASM) && defined(HAVE_GETCPUID) bool have_sse4 = false; bool have_xsave = false; bool have_avx = false; [[maybe_unused]] bool have_avx2 = false; [[maybe_unused]] bool have_x86_shani = false; [[maybe_unused]] bool enabled_avx = false; uint32_t eax, ebx, ecx, edx; GetCPUID(1, 0, eax, ebx, ecx, edx); if (use_implementation & sha256_implementation::USE_SSE4) { have_sse4 = (ecx >> 19) & 1; } have_xsave = (ecx >> 27) & 1; have_avx = (ecx >> 28) & 1; if (have_xsave && have_avx) { enabled_avx = AVXEnabled(); } if (have_sse4) { GetCPUID(7, 0, eax, ebx, ecx, edx); if (use_implementation & sha256_implementation::USE_AVX2) { have_avx2 = (ebx >> 5) & 1; } if (use_implementation & sha256_implementation::USE_SHANI) { have_x86_shani = (ebx >> 29) & 1; } } #if defined(ENABLE_X86_SHANI) && !defined(BUILD_BITCOIN_INTERNAL) if (have_x86_shani) { Transform = sha256_x86_shani::Transform; TransformD64 = TransformD64Wrapper<sha256_x86_shani::Transform>; TransformD64_2way = sha256d64_x86_shani::Transform_2way; ret = "x86_shani(1way,2way)"; have_sse4 = false; // Disable SSE4/AVX2; have_avx2 = false; } #endif if (have_sse4) { #if defined(__x86_64__) || defined(__amd64__) Transform = sha256_sse4::Transform; TransformD64 = TransformD64Wrapper<sha256_sse4::Transform>; ret = "sse4(1way)"; #endif #if defined(ENABLE_SSE41) && !defined(BUILD_BITCOIN_INTERNAL) TransformD64_4way = sha256d64_sse41::Transform_4way; ret += ",sse41(4way)"; #endif } #if defined(ENABLE_AVX2) && !defined(BUILD_BITCOIN_INTERNAL) if (have_avx2 && have_avx && enabled_avx) { TransformD64_8way = sha256d64_avx2::Transform_8way; ret += ",avx2(8way)"; } #endif #endif // defined(USE_ASM) && defined(HAVE_GETCPUID) #if defined(ENABLE_ARM_SHANI) && !defined(BUILD_BITCOIN_INTERNAL) bool have_arm_shani = false; if (use_implementation & sha256_implementation::USE_SHANI) { #if defined(__linux__) #if defined(__arm__) // 32-bit if (getauxval(AT_HWCAP2) & HWCAP2_SHA2) { have_arm_shani = true; } #endif #if defined(__aarch64__) // 64-bit if (getauxval(AT_HWCAP) & HWCAP_SHA2) { have_arm_shani = true; } #endif #endif #if defined(MAC_OSX) int val = 0; size_t len = sizeof(val); if (sysctlbyname("hw.optional.arm.FEAT_SHA256", &val, &len, nullptr, 0) == 0) { have_arm_shani = val != 0; } #endif } if (have_arm_shani) { Transform = sha256_arm_shani::Transform; TransformD64 = TransformD64Wrapper<sha256_arm_shani::Transform>; TransformD64_2way = sha256d64_arm_shani::Transform_2way; ret = "arm_shani(1way,2way)"; } #endif assert(SelfTest()); return ret; } ////// SHA-256 CSHA256::CSHA256() { sha256::Initialize(s); } CSHA256& CSHA256::Write(const unsigned char* data, size_t len) { const unsigned char* end = data + len; size_t bufsize = bytes % 64; if (bufsize && bufsize + len >= 64) { // Fill the buffer, and process it. memcpy(buf + bufsize, data, 64 - bufsize); bytes += 64 - bufsize; data += 64 - bufsize; Transform(s, buf, 1); bufsize = 0; } if (end - data >= 64) { size_t blocks = (end - data) / 64; Transform(s, data, blocks); data += 64 * blocks; bytes += 64 * blocks; } if (end > data) { // Fill the buffer with what remains. memcpy(buf + bufsize, data, end - data); bytes += end - data; } return *this; } void CSHA256::Finalize(unsigned char hash[OUTPUT_SIZE]) { static const unsigned char pad[64] = {0x80}; unsigned char sizedesc[8]; WriteBE64(sizedesc, bytes << 3); Write(pad, 1 + ((119 - (bytes % 64)) % 64)); Write(sizedesc, 8); WriteBE32(hash, s[0]); WriteBE32(hash + 4, s[1]); WriteBE32(hash + 8, s[2]); WriteBE32(hash + 12, s[3]); WriteBE32(hash + 16, s[4]); WriteBE32(hash + 20, s[5]); WriteBE32(hash + 24, s[6]); WriteBE32(hash + 28, s[7]); } CSHA256& CSHA256::Reset() { bytes = 0; sha256::Initialize(s); return *this; } void SHA256D64(unsigned char* out, const unsigned char* in, size_t blocks) { if (TransformD64_8way) { while (blocks >= 8) { TransformD64_8way(out, in); out += 256; in += 512; blocks -= 8; } } if (TransformD64_4way) { while (blocks >= 4) { TransformD64_4way(out, in); out += 128; in += 256; blocks -= 4; } } if (TransformD64_2way) { while (blocks >= 2) { TransformD64_2way(out, in); out += 64; in += 128; blocks -= 2; } } while (blocks) { TransformD64(out, in); out += 32; in += 64; --blocks; } }
0
bitcoin/src
bitcoin/src/crypto/sha256_sse4.cpp
// Copyright (c) 2017-2022 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. // // This is a translation to GCC extended asm syntax from YASM code by Intel // (available at the bottom of this file). #include <cstdlib> #include <stdint.h> #if defined(__x86_64__) || defined(__amd64__) namespace sha256_sse4 { void Transform(uint32_t* s, const unsigned char* chunk, size_t blocks) { static const uint32_t K256 alignas(16) [] = { 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2, }; static const uint32_t FLIP_MASK alignas(16) [] = {0x00010203, 0x04050607, 0x08090a0b, 0x0c0d0e0f}; static const uint32_t SHUF_00BA alignas(16) [] = {0x03020100, 0x0b0a0908, 0xffffffff, 0xffffffff}; static const uint32_t SHUF_DC00 alignas(16) [] = {0xffffffff, 0xffffffff, 0x03020100, 0x0b0a0908}; uint32_t a, b, c, d, f, g, h, y0, y1, y2; uint64_t tbl; uint64_t inp_end, inp; uint32_t xfer alignas(16) [4]; __asm__ __volatile__( "shl $0x6,%2;" "je Ldone_hash_%=;" "add %1,%2;" "mov %2,%14;" "mov (%0),%3;" "mov 0x4(%0),%4;" "mov 0x8(%0),%5;" "mov 0xc(%0),%6;" "mov 0x10(%0),%k2;" "mov 0x14(%0),%7;" "mov 0x18(%0),%8;" "mov 0x1c(%0),%9;" "movdqa %18,%%xmm12;" "movdqa %19,%%xmm10;" "movdqa %20,%%xmm11;" "Lloop0_%=:" "lea %17,%13;" "movdqu (%1),%%xmm4;" "pshufb %%xmm12,%%xmm4;" "movdqu 0x10(%1),%%xmm5;" "pshufb %%xmm12,%%xmm5;" "movdqu 0x20(%1),%%xmm6;" "pshufb %%xmm12,%%xmm6;" "movdqu 0x30(%1),%%xmm7;" "pshufb %%xmm12,%%xmm7;" "mov %1,%15;" "mov $3,%1;" "Lloop1_%=:" "movdqa 0x0(%13),%%xmm9;" "paddd %%xmm4,%%xmm9;" "movdqa %%xmm9,%16;" "movdqa %%xmm7,%%xmm0;" "mov %k2,%10;" "ror $0xe,%10;" "mov %3,%11;" "palignr $0x4,%%xmm6,%%xmm0;" "ror $0x9,%11;" "xor %k2,%10;" "mov %7,%12;" "ror $0x5,%10;" "movdqa %%xmm5,%%xmm1;" "xor %3,%11;" "xor %8,%12;" "paddd %%xmm4,%%xmm0;" "xor %k2,%10;" "and %k2,%12;" "ror $0xb,%11;" "palignr $0x4,%%xmm4,%%xmm1;" "xor %3,%11;" "ror $0x6,%10;" "xor %8,%12;" "movdqa %%xmm1,%%xmm2;" "ror $0x2,%11;" "add %10,%12;" "add %16,%12;" "movdqa %%xmm1,%%xmm3;" "mov %3,%10;" "add %12,%9;" "mov %3,%12;" "pslld $0x19,%%xmm1;" "or %5,%10;" "add %9,%6;" "and %5,%12;" "psrld $0x7,%%xmm2;" "and %4,%10;" "add %11,%9;" "por %%xmm2,%%xmm1;" "or %12,%10;" "add %10,%9;" "movdqa %%xmm3,%%xmm2;" "mov %6,%10;" "mov %9,%11;" "movdqa %%xmm3,%%xmm8;" "ror $0xe,%10;" "xor %6,%10;" "mov %k2,%12;" "ror $0x9,%11;" "pslld $0xe,%%xmm3;" "xor %9,%11;" "ror $0x5,%10;" "xor %7,%12;" "psrld $0x12,%%xmm2;" "ror $0xb,%11;" "xor %6,%10;" "and %6,%12;" "ror $0x6,%10;" "pxor %%xmm3,%%xmm1;" "xor %9,%11;" "xor %7,%12;" "psrld $0x3,%%xmm8;" "add %10,%12;" "add 4+%16,%12;" "ror $0x2,%11;" "pxor %%xmm2,%%xmm1;" "mov %9,%10;" "add %12,%8;" "mov %9,%12;" "pxor %%xmm8,%%xmm1;" "or %4,%10;" "add %8,%5;" "and %4,%12;" "pshufd $0xfa,%%xmm7,%%xmm2;" "and %3,%10;" "add %11,%8;" "paddd %%xmm1,%%xmm0;" "or %12,%10;" "add %10,%8;" "movdqa %%xmm2,%%xmm3;" "mov %5,%10;" "mov %8,%11;" "ror $0xe,%10;" "movdqa %%xmm2,%%xmm8;" "xor %5,%10;" "ror $0x9,%11;" "mov %6,%12;" "xor %8,%11;" "ror $0x5,%10;" "psrlq $0x11,%%xmm2;" "xor %k2,%12;" "psrlq $0x13,%%xmm3;" "xor %5,%10;" "and %5,%12;" "psrld $0xa,%%xmm8;" "ror $0xb,%11;" "xor %8,%11;" "xor %k2,%12;" "ror $0x6,%10;" "pxor %%xmm3,%%xmm2;" "add %10,%12;" "ror $0x2,%11;" "add 8+%16,%12;" "pxor %%xmm2,%%xmm8;" "mov %8,%10;" "add %12,%7;" "mov %8,%12;" "pshufb %%xmm10,%%xmm8;" "or %3,%10;" "add %7,%4;" "and %3,%12;" "paddd %%xmm8,%%xmm0;" "and %9,%10;" "add %11,%7;" "pshufd $0x50,%%xmm0,%%xmm2;" "or %12,%10;" "add %10,%7;" "movdqa %%xmm2,%%xmm3;" "mov %4,%10;" "ror $0xe,%10;" "mov %7,%11;" "movdqa %%xmm2,%%xmm4;" "ror $0x9,%11;" "xor %4,%10;" "mov %5,%12;" "ror $0x5,%10;" "psrlq $0x11,%%xmm2;" "xor %7,%11;" "xor %6,%12;" "psrlq $0x13,%%xmm3;" "xor %4,%10;" "and %4,%12;" "ror $0xb,%11;" "psrld $0xa,%%xmm4;" "xor %7,%11;" "ror $0x6,%10;" "xor %6,%12;" "pxor %%xmm3,%%xmm2;" "ror $0x2,%11;" "add %10,%12;" "add 12+%16,%12;" "pxor %%xmm2,%%xmm4;" "mov %7,%10;" "add %12,%k2;" "mov %7,%12;" "pshufb %%xmm11,%%xmm4;" "or %9,%10;" "add %k2,%3;" "and %9,%12;" "paddd %%xmm0,%%xmm4;" "and %8,%10;" "add %11,%k2;" "or %12,%10;" "add %10,%k2;" "movdqa 0x10(%13),%%xmm9;" "paddd %%xmm5,%%xmm9;" "movdqa %%xmm9,%16;" "movdqa %%xmm4,%%xmm0;" "mov %3,%10;" "ror $0xe,%10;" "mov %k2,%11;" "palignr $0x4,%%xmm7,%%xmm0;" "ror $0x9,%11;" "xor %3,%10;" "mov %4,%12;" "ror $0x5,%10;" "movdqa %%xmm6,%%xmm1;" "xor %k2,%11;" "xor %5,%12;" "paddd %%xmm5,%%xmm0;" "xor %3,%10;" "and %3,%12;" "ror $0xb,%11;" "palignr $0x4,%%xmm5,%%xmm1;" "xor %k2,%11;" "ror $0x6,%10;" "xor %5,%12;" "movdqa %%xmm1,%%xmm2;" "ror $0x2,%11;" "add %10,%12;" "add %16,%12;" "movdqa %%xmm1,%%xmm3;" "mov %k2,%10;" "add %12,%6;" "mov %k2,%12;" "pslld $0x19,%%xmm1;" "or %8,%10;" "add %6,%9;" "and %8,%12;" "psrld $0x7,%%xmm2;" "and %7,%10;" "add %11,%6;" "por %%xmm2,%%xmm1;" "or %12,%10;" "add %10,%6;" "movdqa %%xmm3,%%xmm2;" "mov %9,%10;" "mov %6,%11;" "movdqa %%xmm3,%%xmm8;" "ror $0xe,%10;" "xor %9,%10;" "mov %3,%12;" "ror $0x9,%11;" "pslld $0xe,%%xmm3;" "xor %6,%11;" "ror $0x5,%10;" "xor %4,%12;" "psrld $0x12,%%xmm2;" "ror $0xb,%11;" "xor %9,%10;" "and %9,%12;" "ror $0x6,%10;" "pxor %%xmm3,%%xmm1;" "xor %6,%11;" "xor %4,%12;" "psrld $0x3,%%xmm8;" "add %10,%12;" "add 4+%16,%12;" "ror $0x2,%11;" "pxor %%xmm2,%%xmm1;" "mov %6,%10;" "add %12,%5;" "mov %6,%12;" "pxor %%xmm8,%%xmm1;" "or %7,%10;" "add %5,%8;" "and %7,%12;" "pshufd $0xfa,%%xmm4,%%xmm2;" "and %k2,%10;" "add %11,%5;" "paddd %%xmm1,%%xmm0;" "or %12,%10;" "add %10,%5;" "movdqa %%xmm2,%%xmm3;" "mov %8,%10;" "mov %5,%11;" "ror $0xe,%10;" "movdqa %%xmm2,%%xmm8;" "xor %8,%10;" "ror $0x9,%11;" "mov %9,%12;" "xor %5,%11;" "ror $0x5,%10;" "psrlq $0x11,%%xmm2;" "xor %3,%12;" "psrlq $0x13,%%xmm3;" "xor %8,%10;" "and %8,%12;" "psrld $0xa,%%xmm8;" "ror $0xb,%11;" "xor %5,%11;" "xor %3,%12;" "ror $0x6,%10;" "pxor %%xmm3,%%xmm2;" "add %10,%12;" "ror $0x2,%11;" "add 8+%16,%12;" "pxor %%xmm2,%%xmm8;" "mov %5,%10;" "add %12,%4;" "mov %5,%12;" "pshufb %%xmm10,%%xmm8;" "or %k2,%10;" "add %4,%7;" "and %k2,%12;" "paddd %%xmm8,%%xmm0;" "and %6,%10;" "add %11,%4;" "pshufd $0x50,%%xmm0,%%xmm2;" "or %12,%10;" "add %10,%4;" "movdqa %%xmm2,%%xmm3;" "mov %7,%10;" "ror $0xe,%10;" "mov %4,%11;" "movdqa %%xmm2,%%xmm5;" "ror $0x9,%11;" "xor %7,%10;" "mov %8,%12;" "ror $0x5,%10;" "psrlq $0x11,%%xmm2;" "xor %4,%11;" "xor %9,%12;" "psrlq $0x13,%%xmm3;" "xor %7,%10;" "and %7,%12;" "ror $0xb,%11;" "psrld $0xa,%%xmm5;" "xor %4,%11;" "ror $0x6,%10;" "xor %9,%12;" "pxor %%xmm3,%%xmm2;" "ror $0x2,%11;" "add %10,%12;" "add 12+%16,%12;" "pxor %%xmm2,%%xmm5;" "mov %4,%10;" "add %12,%3;" "mov %4,%12;" "pshufb %%xmm11,%%xmm5;" "or %6,%10;" "add %3,%k2;" "and %6,%12;" "paddd %%xmm0,%%xmm5;" "and %5,%10;" "add %11,%3;" "or %12,%10;" "add %10,%3;" "movdqa 0x20(%13),%%xmm9;" "paddd %%xmm6,%%xmm9;" "movdqa %%xmm9,%16;" "movdqa %%xmm5,%%xmm0;" "mov %k2,%10;" "ror $0xe,%10;" "mov %3,%11;" "palignr $0x4,%%xmm4,%%xmm0;" "ror $0x9,%11;" "xor %k2,%10;" "mov %7,%12;" "ror $0x5,%10;" "movdqa %%xmm7,%%xmm1;" "xor %3,%11;" "xor %8,%12;" "paddd %%xmm6,%%xmm0;" "xor %k2,%10;" "and %k2,%12;" "ror $0xb,%11;" "palignr $0x4,%%xmm6,%%xmm1;" "xor %3,%11;" "ror $0x6,%10;" "xor %8,%12;" "movdqa %%xmm1,%%xmm2;" "ror $0x2,%11;" "add %10,%12;" "add %16,%12;" "movdqa %%xmm1,%%xmm3;" "mov %3,%10;" "add %12,%9;" "mov %3,%12;" "pslld $0x19,%%xmm1;" "or %5,%10;" "add %9,%6;" "and %5,%12;" "psrld $0x7,%%xmm2;" "and %4,%10;" "add %11,%9;" "por %%xmm2,%%xmm1;" "or %12,%10;" "add %10,%9;" "movdqa %%xmm3,%%xmm2;" "mov %6,%10;" "mov %9,%11;" "movdqa %%xmm3,%%xmm8;" "ror $0xe,%10;" "xor %6,%10;" "mov %k2,%12;" "ror $0x9,%11;" "pslld $0xe,%%xmm3;" "xor %9,%11;" "ror $0x5,%10;" "xor %7,%12;" "psrld $0x12,%%xmm2;" "ror $0xb,%11;" "xor %6,%10;" "and %6,%12;" "ror $0x6,%10;" "pxor %%xmm3,%%xmm1;" "xor %9,%11;" "xor %7,%12;" "psrld $0x3,%%xmm8;" "add %10,%12;" "add 4+%16,%12;" "ror $0x2,%11;" "pxor %%xmm2,%%xmm1;" "mov %9,%10;" "add %12,%8;" "mov %9,%12;" "pxor %%xmm8,%%xmm1;" "or %4,%10;" "add %8,%5;" "and %4,%12;" "pshufd $0xfa,%%xmm5,%%xmm2;" "and %3,%10;" "add %11,%8;" "paddd %%xmm1,%%xmm0;" "or %12,%10;" "add %10,%8;" "movdqa %%xmm2,%%xmm3;" "mov %5,%10;" "mov %8,%11;" "ror $0xe,%10;" "movdqa %%xmm2,%%xmm8;" "xor %5,%10;" "ror $0x9,%11;" "mov %6,%12;" "xor %8,%11;" "ror $0x5,%10;" "psrlq $0x11,%%xmm2;" "xor %k2,%12;" "psrlq $0x13,%%xmm3;" "xor %5,%10;" "and %5,%12;" "psrld $0xa,%%xmm8;" "ror $0xb,%11;" "xor %8,%11;" "xor %k2,%12;" "ror $0x6,%10;" "pxor %%xmm3,%%xmm2;" "add %10,%12;" "ror $0x2,%11;" "add 8+%16,%12;" "pxor %%xmm2,%%xmm8;" "mov %8,%10;" "add %12,%7;" "mov %8,%12;" "pshufb %%xmm10,%%xmm8;" "or %3,%10;" "add %7,%4;" "and %3,%12;" "paddd %%xmm8,%%xmm0;" "and %9,%10;" "add %11,%7;" "pshufd $0x50,%%xmm0,%%xmm2;" "or %12,%10;" "add %10,%7;" "movdqa %%xmm2,%%xmm3;" "mov %4,%10;" "ror $0xe,%10;" "mov %7,%11;" "movdqa %%xmm2,%%xmm6;" "ror $0x9,%11;" "xor %4,%10;" "mov %5,%12;" "ror $0x5,%10;" "psrlq $0x11,%%xmm2;" "xor %7,%11;" "xor %6,%12;" "psrlq $0x13,%%xmm3;" "xor %4,%10;" "and %4,%12;" "ror $0xb,%11;" "psrld $0xa,%%xmm6;" "xor %7,%11;" "ror $0x6,%10;" "xor %6,%12;" "pxor %%xmm3,%%xmm2;" "ror $0x2,%11;" "add %10,%12;" "add 12+%16,%12;" "pxor %%xmm2,%%xmm6;" "mov %7,%10;" "add %12,%k2;" "mov %7,%12;" "pshufb %%xmm11,%%xmm6;" "or %9,%10;" "add %k2,%3;" "and %9,%12;" "paddd %%xmm0,%%xmm6;" "and %8,%10;" "add %11,%k2;" "or %12,%10;" "add %10,%k2;" "movdqa 0x30(%13),%%xmm9;" "paddd %%xmm7,%%xmm9;" "movdqa %%xmm9,%16;" "add $0x40,%13;" "movdqa %%xmm6,%%xmm0;" "mov %3,%10;" "ror $0xe,%10;" "mov %k2,%11;" "palignr $0x4,%%xmm5,%%xmm0;" "ror $0x9,%11;" "xor %3,%10;" "mov %4,%12;" "ror $0x5,%10;" "movdqa %%xmm4,%%xmm1;" "xor %k2,%11;" "xor %5,%12;" "paddd %%xmm7,%%xmm0;" "xor %3,%10;" "and %3,%12;" "ror $0xb,%11;" "palignr $0x4,%%xmm7,%%xmm1;" "xor %k2,%11;" "ror $0x6,%10;" "xor %5,%12;" "movdqa %%xmm1,%%xmm2;" "ror $0x2,%11;" "add %10,%12;" "add %16,%12;" "movdqa %%xmm1,%%xmm3;" "mov %k2,%10;" "add %12,%6;" "mov %k2,%12;" "pslld $0x19,%%xmm1;" "or %8,%10;" "add %6,%9;" "and %8,%12;" "psrld $0x7,%%xmm2;" "and %7,%10;" "add %11,%6;" "por %%xmm2,%%xmm1;" "or %12,%10;" "add %10,%6;" "movdqa %%xmm3,%%xmm2;" "mov %9,%10;" "mov %6,%11;" "movdqa %%xmm3,%%xmm8;" "ror $0xe,%10;" "xor %9,%10;" "mov %3,%12;" "ror $0x9,%11;" "pslld $0xe,%%xmm3;" "xor %6,%11;" "ror $0x5,%10;" "xor %4,%12;" "psrld $0x12,%%xmm2;" "ror $0xb,%11;" "xor %9,%10;" "and %9,%12;" "ror $0x6,%10;" "pxor %%xmm3,%%xmm1;" "xor %6,%11;" "xor %4,%12;" "psrld $0x3,%%xmm8;" "add %10,%12;" "add 4+%16,%12;" "ror $0x2,%11;" "pxor %%xmm2,%%xmm1;" "mov %6,%10;" "add %12,%5;" "mov %6,%12;" "pxor %%xmm8,%%xmm1;" "or %7,%10;" "add %5,%8;" "and %7,%12;" "pshufd $0xfa,%%xmm6,%%xmm2;" "and %k2,%10;" "add %11,%5;" "paddd %%xmm1,%%xmm0;" "or %12,%10;" "add %10,%5;" "movdqa %%xmm2,%%xmm3;" "mov %8,%10;" "mov %5,%11;" "ror $0xe,%10;" "movdqa %%xmm2,%%xmm8;" "xor %8,%10;" "ror $0x9,%11;" "mov %9,%12;" "xor %5,%11;" "ror $0x5,%10;" "psrlq $0x11,%%xmm2;" "xor %3,%12;" "psrlq $0x13,%%xmm3;" "xor %8,%10;" "and %8,%12;" "psrld $0xa,%%xmm8;" "ror $0xb,%11;" "xor %5,%11;" "xor %3,%12;" "ror $0x6,%10;" "pxor %%xmm3,%%xmm2;" "add %10,%12;" "ror $0x2,%11;" "add 8+%16,%12;" "pxor %%xmm2,%%xmm8;" "mov %5,%10;" "add %12,%4;" "mov %5,%12;" "pshufb %%xmm10,%%xmm8;" "or %k2,%10;" "add %4,%7;" "and %k2,%12;" "paddd %%xmm8,%%xmm0;" "and %6,%10;" "add %11,%4;" "pshufd $0x50,%%xmm0,%%xmm2;" "or %12,%10;" "add %10,%4;" "movdqa %%xmm2,%%xmm3;" "mov %7,%10;" "ror $0xe,%10;" "mov %4,%11;" "movdqa %%xmm2,%%xmm7;" "ror $0x9,%11;" "xor %7,%10;" "mov %8,%12;" "ror $0x5,%10;" "psrlq $0x11,%%xmm2;" "xor %4,%11;" "xor %9,%12;" "psrlq $0x13,%%xmm3;" "xor %7,%10;" "and %7,%12;" "ror $0xb,%11;" "psrld $0xa,%%xmm7;" "xor %4,%11;" "ror $0x6,%10;" "xor %9,%12;" "pxor %%xmm3,%%xmm2;" "ror $0x2,%11;" "add %10,%12;" "add 12+%16,%12;" "pxor %%xmm2,%%xmm7;" "mov %4,%10;" "add %12,%3;" "mov %4,%12;" "pshufb %%xmm11,%%xmm7;" "or %6,%10;" "add %3,%k2;" "and %6,%12;" "paddd %%xmm0,%%xmm7;" "and %5,%10;" "add %11,%3;" "or %12,%10;" "add %10,%3;" "sub $0x1,%1;" "jne Lloop1_%=;" "mov $0x2,%1;" "Lloop2_%=:" "paddd 0x0(%13),%%xmm4;" "movdqa %%xmm4,%16;" "mov %k2,%10;" "ror $0xe,%10;" "mov %3,%11;" "xor %k2,%10;" "ror $0x9,%11;" "mov %7,%12;" "xor %3,%11;" "ror $0x5,%10;" "xor %8,%12;" "xor %k2,%10;" "ror $0xb,%11;" "and %k2,%12;" "xor %3,%11;" "ror $0x6,%10;" "xor %8,%12;" "add %10,%12;" "ror $0x2,%11;" "add %16,%12;" "mov %3,%10;" "add %12,%9;" "mov %3,%12;" "or %5,%10;" "add %9,%6;" "and %5,%12;" "and %4,%10;" "add %11,%9;" "or %12,%10;" "add %10,%9;" "mov %6,%10;" "ror $0xe,%10;" "mov %9,%11;" "xor %6,%10;" "ror $0x9,%11;" "mov %k2,%12;" "xor %9,%11;" "ror $0x5,%10;" "xor %7,%12;" "xor %6,%10;" "ror $0xb,%11;" "and %6,%12;" "xor %9,%11;" "ror $0x6,%10;" "xor %7,%12;" "add %10,%12;" "ror $0x2,%11;" "add 4+%16,%12;" "mov %9,%10;" "add %12,%8;" "mov %9,%12;" "or %4,%10;" "add %8,%5;" "and %4,%12;" "and %3,%10;" "add %11,%8;" "or %12,%10;" "add %10,%8;" "mov %5,%10;" "ror $0xe,%10;" "mov %8,%11;" "xor %5,%10;" "ror $0x9,%11;" "mov %6,%12;" "xor %8,%11;" "ror $0x5,%10;" "xor %k2,%12;" "xor %5,%10;" "ror $0xb,%11;" "and %5,%12;" "xor %8,%11;" "ror $0x6,%10;" "xor %k2,%12;" "add %10,%12;" "ror $0x2,%11;" "add 8+%16,%12;" "mov %8,%10;" "add %12,%7;" "mov %8,%12;" "or %3,%10;" "add %7,%4;" "and %3,%12;" "and %9,%10;" "add %11,%7;" "or %12,%10;" "add %10,%7;" "mov %4,%10;" "ror $0xe,%10;" "mov %7,%11;" "xor %4,%10;" "ror $0x9,%11;" "mov %5,%12;" "xor %7,%11;" "ror $0x5,%10;" "xor %6,%12;" "xor %4,%10;" "ror $0xb,%11;" "and %4,%12;" "xor %7,%11;" "ror $0x6,%10;" "xor %6,%12;" "add %10,%12;" "ror $0x2,%11;" "add 12+%16,%12;" "mov %7,%10;" "add %12,%k2;" "mov %7,%12;" "or %9,%10;" "add %k2,%3;" "and %9,%12;" "and %8,%10;" "add %11,%k2;" "or %12,%10;" "add %10,%k2;" "paddd 0x10(%13),%%xmm5;" "movdqa %%xmm5,%16;" "add $0x20,%13;" "mov %3,%10;" "ror $0xe,%10;" "mov %k2,%11;" "xor %3,%10;" "ror $0x9,%11;" "mov %4,%12;" "xor %k2,%11;" "ror $0x5,%10;" "xor %5,%12;" "xor %3,%10;" "ror $0xb,%11;" "and %3,%12;" "xor %k2,%11;" "ror $0x6,%10;" "xor %5,%12;" "add %10,%12;" "ror $0x2,%11;" "add %16,%12;" "mov %k2,%10;" "add %12,%6;" "mov %k2,%12;" "or %8,%10;" "add %6,%9;" "and %8,%12;" "and %7,%10;" "add %11,%6;" "or %12,%10;" "add %10,%6;" "mov %9,%10;" "ror $0xe,%10;" "mov %6,%11;" "xor %9,%10;" "ror $0x9,%11;" "mov %3,%12;" "xor %6,%11;" "ror $0x5,%10;" "xor %4,%12;" "xor %9,%10;" "ror $0xb,%11;" "and %9,%12;" "xor %6,%11;" "ror $0x6,%10;" "xor %4,%12;" "add %10,%12;" "ror $0x2,%11;" "add 4+%16,%12;" "mov %6,%10;" "add %12,%5;" "mov %6,%12;" "or %7,%10;" "add %5,%8;" "and %7,%12;" "and %k2,%10;" "add %11,%5;" "or %12,%10;" "add %10,%5;" "mov %8,%10;" "ror $0xe,%10;" "mov %5,%11;" "xor %8,%10;" "ror $0x9,%11;" "mov %9,%12;" "xor %5,%11;" "ror $0x5,%10;" "xor %3,%12;" "xor %8,%10;" "ror $0xb,%11;" "and %8,%12;" "xor %5,%11;" "ror $0x6,%10;" "xor %3,%12;" "add %10,%12;" "ror $0x2,%11;" "add 8+%16,%12;" "mov %5,%10;" "add %12,%4;" "mov %5,%12;" "or %k2,%10;" "add %4,%7;" "and %k2,%12;" "and %6,%10;" "add %11,%4;" "or %12,%10;" "add %10,%4;" "mov %7,%10;" "ror $0xe,%10;" "mov %4,%11;" "xor %7,%10;" "ror $0x9,%11;" "mov %8,%12;" "xor %4,%11;" "ror $0x5,%10;" "xor %9,%12;" "xor %7,%10;" "ror $0xb,%11;" "and %7,%12;" "xor %4,%11;" "ror $0x6,%10;" "xor %9,%12;" "add %10,%12;" "ror $0x2,%11;" "add 12+%16,%12;" "mov %4,%10;" "add %12,%3;" "mov %4,%12;" "or %6,%10;" "add %3,%k2;" "and %6,%12;" "and %5,%10;" "add %11,%3;" "or %12,%10;" "add %10,%3;" "movdqa %%xmm6,%%xmm4;" "movdqa %%xmm7,%%xmm5;" "sub $0x1,%1;" "jne Lloop2_%=;" "add (%0),%3;" "mov %3,(%0);" "add 0x4(%0),%4;" "mov %4,0x4(%0);" "add 0x8(%0),%5;" "mov %5,0x8(%0);" "add 0xc(%0),%6;" "mov %6,0xc(%0);" "add 0x10(%0),%k2;" "mov %k2,0x10(%0);" "add 0x14(%0),%7;" "mov %7,0x14(%0);" "add 0x18(%0),%8;" "mov %8,0x18(%0);" "add 0x1c(%0),%9;" "mov %9,0x1c(%0);" "mov %15,%1;" "add $0x40,%1;" "cmp %14,%1;" "jne Lloop0_%=;" "Ldone_hash_%=:" : "+r"(s), "+r"(chunk), "+r"(blocks), "=r"(a), "=r"(b), "=r"(c), "=r"(d), /* e = chunk */ "=r"(f), "=r"(g), "=r"(h), "=r"(y0), "=r"(y1), "=r"(y2), "=r"(tbl), "+m"(inp_end), "+m"(inp), "+m"(xfer) : "m"(K256), "m"(FLIP_MASK), "m"(SHUF_00BA), "m"(SHUF_DC00) : "cc", "memory", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7", "xmm8", "xmm9", "xmm10", "xmm11", "xmm12" ); } } /* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; Copyright (c) 2012, Intel Corporation ; ; All rights reserved. ; ; Redistribution and use in source and binary forms, with or without ; modification, are permitted provided that the following conditions are ; met: ; ; * Redistributions of source code must retain the above copyright ; notice, this list of conditions and the following disclaimer. ; ; * Redistributions in binary form must reproduce the above copyright ; notice, this list of conditions and the following disclaimer in the ; documentation and/or other materials provided with the ; distribution. ; ; * Neither the name of the Intel Corporation nor the names of its ; contributors may be used to endorse or promote products derived from ; this software without specific prior written permission. ; ; ; THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION "AS IS" AND ANY ; EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE ; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR ; PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL CORPORATION OR ; CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, ; EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, ; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR ; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF ; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING ; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS ; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ; Example YASM command lines: ; Windows: yasm -Xvc -f x64 -rnasm -pnasm -o sha256_sse4.obj -g cv8 sha256_sse4.asm ; Linux: yasm -f x64 -f elf64 -X gnu -g dwarf2 -D LINUX -o sha256_sse4.o sha256_sse4.asm ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ; This code is described in an Intel White-Paper: ; "Fast SHA-256 Implementations on Intel Architecture Processors" ; ; To find it, surf to https://www.intel.com/p/en_US/embedded ; and search for that title. ; The paper is expected to be released roughly at the end of April, 2012 ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; This code schedules 1 blocks at a time, with 4 lanes per block ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; %define MOVDQ movdqu ;; assume buffers not aligned ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Define Macros ; addm [mem], reg ; Add reg to mem using reg-mem add and store %macro addm 2 add %2, %1 mov %1, %2 %endm ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; COPY_XMM_AND_BSWAP xmm, [mem], byte_flip_mask ; Load xmm with mem and byte swap each dword %macro COPY_XMM_AND_BSWAP 3 MOVDQ %1, %2 pshufb %1, %3 %endmacro ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; %define X0 xmm4 %define X1 xmm5 %define X2 xmm6 %define X3 xmm7 %define XTMP0 xmm0 %define XTMP1 xmm1 %define XTMP2 xmm2 %define XTMP3 xmm3 %define XTMP4 xmm8 %define XFER xmm9 %define SHUF_00BA xmm10 ; shuffle xBxA -> 00BA %define SHUF_DC00 xmm11 ; shuffle xDxC -> DC00 %define BYTE_FLIP_MASK xmm12 %ifdef LINUX %define NUM_BLKS rdx ; 3rd arg %define CTX rsi ; 2nd arg %define INP rdi ; 1st arg %define SRND rdi ; clobbers INP %define c ecx %define d r8d %define e edx %else %define NUM_BLKS r8 ; 3rd arg %define CTX rdx ; 2nd arg %define INP rcx ; 1st arg %define SRND rcx ; clobbers INP %define c edi %define d esi %define e r8d %endif %define TBL rbp %define a eax %define b ebx %define f r9d %define g r10d %define h r11d %define y0 r13d %define y1 r14d %define y2 r15d _INP_END_SIZE equ 8 _INP_SIZE equ 8 _XFER_SIZE equ 8 %ifdef LINUX _XMM_SAVE_SIZE equ 0 %else _XMM_SAVE_SIZE equ 7*16 %endif ; STACK_SIZE plus pushes must be an odd multiple of 8 _ALIGN_SIZE equ 8 _INP_END equ 0 _INP equ _INP_END + _INP_END_SIZE _XFER equ _INP + _INP_SIZE _XMM_SAVE equ _XFER + _XFER_SIZE + _ALIGN_SIZE STACK_SIZE equ _XMM_SAVE + _XMM_SAVE_SIZE ; rotate_Xs ; Rotate values of symbols X0...X3 %macro rotate_Xs 0 %xdefine X_ X0 %xdefine X0 X1 %xdefine X1 X2 %xdefine X2 X3 %xdefine X3 X_ %endm ; ROTATE_ARGS ; Rotate values of symbols a...h %macro ROTATE_ARGS 0 %xdefine TMP_ h %xdefine h g %xdefine g f %xdefine f e %xdefine e d %xdefine d c %xdefine c b %xdefine b a %xdefine a TMP_ %endm %macro FOUR_ROUNDS_AND_SCHED 0 ;; compute s0 four at a time and s1 two at a time ;; compute W[-16] + W[-7] 4 at a time movdqa XTMP0, X3 mov y0, e ; y0 = e ror y0, (25-11) ; y0 = e >> (25-11) mov y1, a ; y1 = a palignr XTMP0, X2, 4 ; XTMP0 = W[-7] ror y1, (22-13) ; y1 = a >> (22-13) xor y0, e ; y0 = e ^ (e >> (25-11)) mov y2, f ; y2 = f ror y0, (11-6) ; y0 = (e >> (11-6)) ^ (e >> (25-6)) movdqa XTMP1, X1 xor y1, a ; y1 = a ^ (a >> (22-13) xor y2, g ; y2 = f^g paddd XTMP0, X0 ; XTMP0 = W[-7] + W[-16] xor y0, e ; y0 = e ^ (e >> (11-6)) ^ (e >> (25-6)) and y2, e ; y2 = (f^g)&e ror y1, (13-2) ; y1 = (a >> (13-2)) ^ (a >> (22-2)) ;; compute s0 palignr XTMP1, X0, 4 ; XTMP1 = W[-15] xor y1, a ; y1 = a ^ (a >> (13-2)) ^ (a >> (22-2)) ror y0, 6 ; y0 = S1 = (e>>6) & (e>>11) ^ (e>>25) xor y2, g ; y2 = CH = ((f^g)&e)^g movdqa XTMP2, XTMP1 ; XTMP2 = W[-15] ror y1, 2 ; y1 = S0 = (a>>2) ^ (a>>13) ^ (a>>22) add y2, y0 ; y2 = S1 + CH add y2, [rsp + _XFER + 0*4] ; y2 = k + w + S1 + CH movdqa XTMP3, XTMP1 ; XTMP3 = W[-15] mov y0, a ; y0 = a add h, y2 ; h = h + S1 + CH + k + w mov y2, a ; y2 = a pslld XTMP1, (32-7) or y0, c ; y0 = a|c add d, h ; d = d + h + S1 + CH + k + w and y2, c ; y2 = a&c psrld XTMP2, 7 and y0, b ; y0 = (a|c)&b add h, y1 ; h = h + S1 + CH + k + w + S0 por XTMP1, XTMP2 ; XTMP1 = W[-15] ror 7 or y0, y2 ; y0 = MAJ = (a|c)&b)|(a&c) add h, y0 ; h = h + S1 + CH + k + w + S0 + MAJ ROTATE_ARGS movdqa XTMP2, XTMP3 ; XTMP2 = W[-15] mov y0, e ; y0 = e mov y1, a ; y1 = a movdqa XTMP4, XTMP3 ; XTMP4 = W[-15] ror y0, (25-11) ; y0 = e >> (25-11) xor y0, e ; y0 = e ^ (e >> (25-11)) mov y2, f ; y2 = f ror y1, (22-13) ; y1 = a >> (22-13) pslld XTMP3, (32-18) xor y1, a ; y1 = a ^ (a >> (22-13) ror y0, (11-6) ; y0 = (e >> (11-6)) ^ (e >> (25-6)) xor y2, g ; y2 = f^g psrld XTMP2, 18 ror y1, (13-2) ; y1 = (a >> (13-2)) ^ (a >> (22-2)) xor y0, e ; y0 = e ^ (e >> (11-6)) ^ (e >> (25-6)) and y2, e ; y2 = (f^g)&e ror y0, 6 ; y0 = S1 = (e>>6) & (e>>11) ^ (e>>25) pxor XTMP1, XTMP3 xor y1, a ; y1 = a ^ (a >> (13-2)) ^ (a >> (22-2)) xor y2, g ; y2 = CH = ((f^g)&e)^g psrld XTMP4, 3 ; XTMP4 = W[-15] >> 3 add y2, y0 ; y2 = S1 + CH add y2, [rsp + _XFER + 1*4] ; y2 = k + w + S1 + CH ror y1, 2 ; y1 = S0 = (a>>2) ^ (a>>13) ^ (a>>22) pxor XTMP1, XTMP2 ; XTMP1 = W[-15] ror 7 ^ W[-15] ror 18 mov y0, a ; y0 = a add h, y2 ; h = h + S1 + CH + k + w mov y2, a ; y2 = a pxor XTMP1, XTMP4 ; XTMP1 = s0 or y0, c ; y0 = a|c add d, h ; d = d + h + S1 + CH + k + w and y2, c ; y2 = a&c ;; compute low s1 pshufd XTMP2, X3, 11111010b ; XTMP2 = W[-2] {BBAA} and y0, b ; y0 = (a|c)&b add h, y1 ; h = h + S1 + CH + k + w + S0 paddd XTMP0, XTMP1 ; XTMP0 = W[-16] + W[-7] + s0 or y0, y2 ; y0 = MAJ = (a|c)&b)|(a&c) add h, y0 ; h = h + S1 + CH + k + w + S0 + MAJ ROTATE_ARGS movdqa XTMP3, XTMP2 ; XTMP3 = W[-2] {BBAA} mov y0, e ; y0 = e mov y1, a ; y1 = a ror y0, (25-11) ; y0 = e >> (25-11) movdqa XTMP4, XTMP2 ; XTMP4 = W[-2] {BBAA} xor y0, e ; y0 = e ^ (e >> (25-11)) ror y1, (22-13) ; y1 = a >> (22-13) mov y2, f ; y2 = f xor y1, a ; y1 = a ^ (a >> (22-13) ror y0, (11-6) ; y0 = (e >> (11-6)) ^ (e >> (25-6)) psrlq XTMP2, 17 ; XTMP2 = W[-2] ror 17 {xBxA} xor y2, g ; y2 = f^g psrlq XTMP3, 19 ; XTMP3 = W[-2] ror 19 {xBxA} xor y0, e ; y0 = e ^ (e >> (11-6)) ^ (e >> (25-6)) and y2, e ; y2 = (f^g)&e psrld XTMP4, 10 ; XTMP4 = W[-2] >> 10 {BBAA} ror y1, (13-2) ; y1 = (a >> (13-2)) ^ (a >> (22-2)) xor y1, a ; y1 = a ^ (a >> (13-2)) ^ (a >> (22-2)) xor y2, g ; y2 = CH = ((f^g)&e)^g ror y0, 6 ; y0 = S1 = (e>>6) & (e>>11) ^ (e>>25) pxor XTMP2, XTMP3 add y2, y0 ; y2 = S1 + CH ror y1, 2 ; y1 = S0 = (a>>2) ^ (a>>13) ^ (a>>22) add y2, [rsp + _XFER + 2*4] ; y2 = k + w + S1 + CH pxor XTMP4, XTMP2 ; XTMP4 = s1 {xBxA} mov y0, a ; y0 = a add h, y2 ; h = h + S1 + CH + k + w mov y2, a ; y2 = a pshufb XTMP4, SHUF_00BA ; XTMP4 = s1 {00BA} or y0, c ; y0 = a|c add d, h ; d = d + h + S1 + CH + k + w and y2, c ; y2 = a&c paddd XTMP0, XTMP4 ; XTMP0 = {..., ..., W[1], W[0]} and y0, b ; y0 = (a|c)&b add h, y1 ; h = h + S1 + CH + k + w + S0 ;; compute high s1 pshufd XTMP2, XTMP0, 01010000b ; XTMP2 = W[-2] {DDCC} or y0, y2 ; y0 = MAJ = (a|c)&b)|(a&c) add h, y0 ; h = h + S1 + CH + k + w + S0 + MAJ ROTATE_ARGS movdqa XTMP3, XTMP2 ; XTMP3 = W[-2] {DDCC} mov y0, e ; y0 = e ror y0, (25-11) ; y0 = e >> (25-11) mov y1, a ; y1 = a movdqa X0, XTMP2 ; X0 = W[-2] {DDCC} ror y1, (22-13) ; y1 = a >> (22-13) xor y0, e ; y0 = e ^ (e >> (25-11)) mov y2, f ; y2 = f ror y0, (11-6) ; y0 = (e >> (11-6)) ^ (e >> (25-6)) psrlq XTMP2, 17 ; XTMP2 = W[-2] ror 17 {xDxC} xor y1, a ; y1 = a ^ (a >> (22-13) xor y2, g ; y2 = f^g psrlq XTMP3, 19 ; XTMP3 = W[-2] ror 19 {xDxC} xor y0, e ; y0 = e ^ (e >> (11-6)) ^ (e >> (25-6)) and y2, e ; y2 = (f^g)&e ror y1, (13-2) ; y1 = (a >> (13-2)) ^ (a >> (22-2)) psrld X0, 10 ; X0 = W[-2] >> 10 {DDCC} xor y1, a ; y1 = a ^ (a >> (13-2)) ^ (a >> (22-2)) ror y0, 6 ; y0 = S1 = (e>>6) & (e>>11) ^ (e>>25) xor y2, g ; y2 = CH = ((f^g)&e)^g pxor XTMP2, XTMP3 ror y1, 2 ; y1 = S0 = (a>>2) ^ (a>>13) ^ (a>>22) add y2, y0 ; y2 = S1 + CH add y2, [rsp + _XFER + 3*4] ; y2 = k + w + S1 + CH pxor X0, XTMP2 ; X0 = s1 {xDxC} mov y0, a ; y0 = a add h, y2 ; h = h + S1 + CH + k + w mov y2, a ; y2 = a pshufb X0, SHUF_DC00 ; X0 = s1 {DC00} or y0, c ; y0 = a|c add d, h ; d = d + h + S1 + CH + k + w and y2, c ; y2 = a&c paddd X0, XTMP0 ; X0 = {W[3], W[2], W[1], W[0]} and y0, b ; y0 = (a|c)&b add h, y1 ; h = h + S1 + CH + k + w + S0 or y0, y2 ; y0 = MAJ = (a|c)&b)|(a&c) add h, y0 ; h = h + S1 + CH + k + w + S0 + MAJ ROTATE_ARGS rotate_Xs %endm ;; input is [rsp + _XFER + %1 * 4] %macro DO_ROUND 1 mov y0, e ; y0 = e ror y0, (25-11) ; y0 = e >> (25-11) mov y1, a ; y1 = a xor y0, e ; y0 = e ^ (e >> (25-11)) ror y1, (22-13) ; y1 = a >> (22-13) mov y2, f ; y2 = f xor y1, a ; y1 = a ^ (a >> (22-13) ror y0, (11-6) ; y0 = (e >> (11-6)) ^ (e >> (25-6)) xor y2, g ; y2 = f^g xor y0, e ; y0 = e ^ (e >> (11-6)) ^ (e >> (25-6)) ror y1, (13-2) ; y1 = (a >> (13-2)) ^ (a >> (22-2)) and y2, e ; y2 = (f^g)&e xor y1, a ; y1 = a ^ (a >> (13-2)) ^ (a >> (22-2)) ror y0, 6 ; y0 = S1 = (e>>6) & (e>>11) ^ (e>>25) xor y2, g ; y2 = CH = ((f^g)&e)^g add y2, y0 ; y2 = S1 + CH ror y1, 2 ; y1 = S0 = (a>>2) ^ (a>>13) ^ (a>>22) add y2, [rsp + _XFER + %1 * 4] ; y2 = k + w + S1 + CH mov y0, a ; y0 = a add h, y2 ; h = h + S1 + CH + k + w mov y2, a ; y2 = a or y0, c ; y0 = a|c add d, h ; d = d + h + S1 + CH + k + w and y2, c ; y2 = a&c and y0, b ; y0 = (a|c)&b add h, y1 ; h = h + S1 + CH + k + w + S0 or y0, y2 ; y0 = MAJ = (a|c)&b)|(a&c) add h, y0 ; h = h + S1 + CH + k + w + S0 + MAJ ROTATE_ARGS %endm ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; void sha256_sse4(void *input_data, UINT32 digest[8], UINT64 num_blks) ;; arg 1 : pointer to input data ;; arg 2 : pointer to digest ;; arg 3 : Num blocks section .text global sha256_sse4 align 32 sha256_sse4: push rbx %ifndef LINUX push rsi push rdi %endif push rbp push r13 push r14 push r15 sub rsp,STACK_SIZE %ifndef LINUX movdqa [rsp + _XMM_SAVE + 0*16],xmm6 movdqa [rsp + _XMM_SAVE + 1*16],xmm7 movdqa [rsp + _XMM_SAVE + 2*16],xmm8 movdqa [rsp + _XMM_SAVE + 3*16],xmm9 movdqa [rsp + _XMM_SAVE + 4*16],xmm10 movdqa [rsp + _XMM_SAVE + 5*16],xmm11 movdqa [rsp + _XMM_SAVE + 6*16],xmm12 %endif shl NUM_BLKS, 6 ; convert to bytes jz done_hash add NUM_BLKS, INP ; pointer to end of data mov [rsp + _INP_END], NUM_BLKS ;; load initial digest mov a,[4*0 + CTX] mov b,[4*1 + CTX] mov c,[4*2 + CTX] mov d,[4*3 + CTX] mov e,[4*4 + CTX] mov f,[4*5 + CTX] mov g,[4*6 + CTX] mov h,[4*7 + CTX] movdqa BYTE_FLIP_MASK, [PSHUFFLE_BYTE_FLIP_MASK wrt rip] movdqa SHUF_00BA, [_SHUF_00BA wrt rip] movdqa SHUF_DC00, [_SHUF_DC00 wrt rip] loop0: lea TBL,[K256 wrt rip] ;; byte swap first 16 dwords COPY_XMM_AND_BSWAP X0, [INP + 0*16], BYTE_FLIP_MASK COPY_XMM_AND_BSWAP X1, [INP + 1*16], BYTE_FLIP_MASK COPY_XMM_AND_BSWAP X2, [INP + 2*16], BYTE_FLIP_MASK COPY_XMM_AND_BSWAP X3, [INP + 3*16], BYTE_FLIP_MASK mov [rsp + _INP], INP ;; schedule 48 input dwords, by doing 3 rounds of 16 each mov SRND, 3 align 16 loop1: movdqa XFER, [TBL + 0*16] paddd XFER, X0 movdqa [rsp + _XFER], XFER FOUR_ROUNDS_AND_SCHED movdqa XFER, [TBL + 1*16] paddd XFER, X0 movdqa [rsp + _XFER], XFER FOUR_ROUNDS_AND_SCHED movdqa XFER, [TBL + 2*16] paddd XFER, X0 movdqa [rsp + _XFER], XFER FOUR_ROUNDS_AND_SCHED movdqa XFER, [TBL + 3*16] paddd XFER, X0 movdqa [rsp + _XFER], XFER add TBL, 4*16 FOUR_ROUNDS_AND_SCHED sub SRND, 1 jne loop1 mov SRND, 2 loop2: paddd X0, [TBL + 0*16] movdqa [rsp + _XFER], X0 DO_ROUND 0 DO_ROUND 1 DO_ROUND 2 DO_ROUND 3 paddd X1, [TBL + 1*16] movdqa [rsp + _XFER], X1 add TBL, 2*16 DO_ROUND 0 DO_ROUND 1 DO_ROUND 2 DO_ROUND 3 movdqa X0, X2 movdqa X1, X3 sub SRND, 1 jne loop2 addm [4*0 + CTX],a addm [4*1 + CTX],b addm [4*2 + CTX],c addm [4*3 + CTX],d addm [4*4 + CTX],e addm [4*5 + CTX],f addm [4*6 + CTX],g addm [4*7 + CTX],h mov INP, [rsp + _INP] add INP, 64 cmp INP, [rsp + _INP_END] jne loop0 done_hash: %ifndef LINUX movdqa xmm6,[rsp + _XMM_SAVE + 0*16] movdqa xmm7,[rsp + _XMM_SAVE + 1*16] movdqa xmm8,[rsp + _XMM_SAVE + 2*16] movdqa xmm9,[rsp + _XMM_SAVE + 3*16] movdqa xmm10,[rsp + _XMM_SAVE + 4*16] movdqa xmm11,[rsp + _XMM_SAVE + 5*16] movdqa xmm12,[rsp + _XMM_SAVE + 6*16] %endif add rsp, STACK_SIZE pop r15 pop r14 pop r13 pop rbp %ifndef LINUX pop rdi pop rsi %endif pop rbx ret section .data align 64 K256: dd 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5 dd 0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5 dd 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3 dd 0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174 dd 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc dd 0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da dd 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7 dd 0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967 dd 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13 dd 0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85 dd 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3 dd 0xd192e819,0xd6990624,0xf40e3585,0x106aa070 dd 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5 dd 0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3 dd 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208 dd 0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2 PSHUFFLE_BYTE_FLIP_MASK: ddq 0x0c0d0e0f08090a0b0405060700010203 ; shuffle xBxA -> 00BA _SHUF_00BA: ddq 0xFFFFFFFFFFFFFFFF0b0a090803020100 ; shuffle xDxC -> DC00 _SHUF_DC00: ddq 0x0b0a090803020100FFFFFFFFFFFFFFFF */ #endif
0
bitcoin/src
bitcoin/src/crypto/sha256_sse41.cpp
// Copyright (c) 2018-2019 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifdef ENABLE_SSE41 #include <stdint.h> #include <immintrin.h> #include <attributes.h> #include <crypto/common.h> namespace sha256d64_sse41 { namespace { __m128i inline K(uint32_t x) { return _mm_set1_epi32(x); } __m128i inline Add(__m128i x, __m128i y) { return _mm_add_epi32(x, y); } __m128i inline Add(__m128i x, __m128i y, __m128i z) { return Add(Add(x, y), z); } __m128i inline Add(__m128i x, __m128i y, __m128i z, __m128i w) { return Add(Add(x, y), Add(z, w)); } __m128i inline Add(__m128i x, __m128i y, __m128i z, __m128i w, __m128i v) { return Add(Add(x, y, z), Add(w, v)); } __m128i inline Inc(__m128i& x, __m128i y) { x = Add(x, y); return x; } __m128i inline Inc(__m128i& x, __m128i y, __m128i z) { x = Add(x, y, z); return x; } __m128i inline Inc(__m128i& x, __m128i y, __m128i z, __m128i w) { x = Add(x, y, z, w); return x; } __m128i inline Xor(__m128i x, __m128i y) { return _mm_xor_si128(x, y); } __m128i inline Xor(__m128i x, __m128i y, __m128i z) { return Xor(Xor(x, y), z); } __m128i inline Or(__m128i x, __m128i y) { return _mm_or_si128(x, y); } __m128i inline And(__m128i x, __m128i y) { return _mm_and_si128(x, y); } __m128i inline ShR(__m128i x, int n) { return _mm_srli_epi32(x, n); } __m128i inline ShL(__m128i x, int n) { return _mm_slli_epi32(x, n); } __m128i inline Ch(__m128i x, __m128i y, __m128i z) { return Xor(z, And(x, Xor(y, z))); } __m128i inline Maj(__m128i x, __m128i y, __m128i z) { return Or(And(x, y), And(z, Or(x, y))); } __m128i inline Sigma0(__m128i x) { return Xor(Or(ShR(x, 2), ShL(x, 30)), Or(ShR(x, 13), ShL(x, 19)), Or(ShR(x, 22), ShL(x, 10))); } __m128i inline Sigma1(__m128i x) { return Xor(Or(ShR(x, 6), ShL(x, 26)), Or(ShR(x, 11), ShL(x, 21)), Or(ShR(x, 25), ShL(x, 7))); } __m128i inline sigma0(__m128i x) { return Xor(Or(ShR(x, 7), ShL(x, 25)), Or(ShR(x, 18), ShL(x, 14)), ShR(x, 3)); } __m128i inline sigma1(__m128i x) { return Xor(Or(ShR(x, 17), ShL(x, 15)), Or(ShR(x, 19), ShL(x, 13)), ShR(x, 10)); } /** One round of SHA-256. */ void ALWAYS_INLINE Round(__m128i a, __m128i b, __m128i c, __m128i& d, __m128i e, __m128i f, __m128i g, __m128i& h, __m128i k) { __m128i t1 = Add(h, Sigma1(e), Ch(e, f, g), k); __m128i t2 = Add(Sigma0(a), Maj(a, b, c)); d = Add(d, t1); h = Add(t1, t2); } __m128i inline Read4(const unsigned char* chunk, int offset) { __m128i ret = _mm_set_epi32( ReadLE32(chunk + 0 + offset), ReadLE32(chunk + 64 + offset), ReadLE32(chunk + 128 + offset), ReadLE32(chunk + 192 + offset) ); return _mm_shuffle_epi8(ret, _mm_set_epi32(0x0C0D0E0FUL, 0x08090A0BUL, 0x04050607UL, 0x00010203UL)); } void inline Write4(unsigned char* out, int offset, __m128i v) { v = _mm_shuffle_epi8(v, _mm_set_epi32(0x0C0D0E0FUL, 0x08090A0BUL, 0x04050607UL, 0x00010203UL)); WriteLE32(out + 0 + offset, _mm_extract_epi32(v, 3)); WriteLE32(out + 32 + offset, _mm_extract_epi32(v, 2)); WriteLE32(out + 64 + offset, _mm_extract_epi32(v, 1)); WriteLE32(out + 96 + offset, _mm_extract_epi32(v, 0)); } } void Transform_4way(unsigned char* out, const unsigned char* in) { // Transform 1 __m128i a = K(0x6a09e667ul); __m128i b = K(0xbb67ae85ul); __m128i c = K(0x3c6ef372ul); __m128i d = K(0xa54ff53aul); __m128i e = K(0x510e527ful); __m128i f = K(0x9b05688cul); __m128i g = K(0x1f83d9abul); __m128i h = K(0x5be0cd19ul); __m128i w0, w1, w2, w3, w4, w5, w6, w7, w8, w9, w10, w11, w12, w13, w14, w15; Round(a, b, c, d, e, f, g, h, Add(K(0x428a2f98ul), w0 = Read4(in, 0))); Round(h, a, b, c, d, e, f, g, Add(K(0x71374491ul), w1 = Read4(in, 4))); Round(g, h, a, b, c, d, e, f, Add(K(0xb5c0fbcful), w2 = Read4(in, 8))); Round(f, g, h, a, b, c, d, e, Add(K(0xe9b5dba5ul), w3 = Read4(in, 12))); Round(e, f, g, h, a, b, c, d, Add(K(0x3956c25bul), w4 = Read4(in, 16))); Round(d, e, f, g, h, a, b, c, Add(K(0x59f111f1ul), w5 = Read4(in, 20))); Round(c, d, e, f, g, h, a, b, Add(K(0x923f82a4ul), w6 = Read4(in, 24))); Round(b, c, d, e, f, g, h, a, Add(K(0xab1c5ed5ul), w7 = Read4(in, 28))); Round(a, b, c, d, e, f, g, h, Add(K(0xd807aa98ul), w8 = Read4(in, 32))); Round(h, a, b, c, d, e, f, g, Add(K(0x12835b01ul), w9 = Read4(in, 36))); Round(g, h, a, b, c, d, e, f, Add(K(0x243185beul), w10 = Read4(in, 40))); Round(f, g, h, a, b, c, d, e, Add(K(0x550c7dc3ul), w11 = Read4(in, 44))); Round(e, f, g, h, a, b, c, d, Add(K(0x72be5d74ul), w12 = Read4(in, 48))); Round(d, e, f, g, h, a, b, c, Add(K(0x80deb1feul), w13 = Read4(in, 52))); Round(c, d, e, f, g, h, a, b, Add(K(0x9bdc06a7ul), w14 = Read4(in, 56))); Round(b, c, d, e, f, g, h, a, Add(K(0xc19bf174ul), w15 = Read4(in, 60))); Round(a, b, c, d, e, f, g, h, Add(K(0xe49b69c1ul), Inc(w0, sigma1(w14), w9, sigma0(w1)))); Round(h, a, b, c, d, e, f, g, Add(K(0xefbe4786ul), Inc(w1, sigma1(w15), w10, sigma0(w2)))); Round(g, h, a, b, c, d, e, f, Add(K(0x0fc19dc6ul), Inc(w2, sigma1(w0), w11, sigma0(w3)))); Round(f, g, h, a, b, c, d, e, Add(K(0x240ca1ccul), Inc(w3, sigma1(w1), w12, sigma0(w4)))); Round(e, f, g, h, a, b, c, d, Add(K(0x2de92c6ful), Inc(w4, sigma1(w2), w13, sigma0(w5)))); Round(d, e, f, g, h, a, b, c, Add(K(0x4a7484aaul), Inc(w5, sigma1(w3), w14, sigma0(w6)))); Round(c, d, e, f, g, h, a, b, Add(K(0x5cb0a9dcul), Inc(w6, sigma1(w4), w15, sigma0(w7)))); Round(b, c, d, e, f, g, h, a, Add(K(0x76f988daul), Inc(w7, sigma1(w5), w0, sigma0(w8)))); Round(a, b, c, d, e, f, g, h, Add(K(0x983e5152ul), Inc(w8, sigma1(w6), w1, sigma0(w9)))); Round(h, a, b, c, d, e, f, g, Add(K(0xa831c66dul), Inc(w9, sigma1(w7), w2, sigma0(w10)))); Round(g, h, a, b, c, d, e, f, Add(K(0xb00327c8ul), Inc(w10, sigma1(w8), w3, sigma0(w11)))); Round(f, g, h, a, b, c, d, e, Add(K(0xbf597fc7ul), Inc(w11, sigma1(w9), w4, sigma0(w12)))); Round(e, f, g, h, a, b, c, d, Add(K(0xc6e00bf3ul), Inc(w12, sigma1(w10), w5, sigma0(w13)))); Round(d, e, f, g, h, a, b, c, Add(K(0xd5a79147ul), Inc(w13, sigma1(w11), w6, sigma0(w14)))); Round(c, d, e, f, g, h, a, b, Add(K(0x06ca6351ul), Inc(w14, sigma1(w12), w7, sigma0(w15)))); Round(b, c, d, e, f, g, h, a, Add(K(0x14292967ul), Inc(w15, sigma1(w13), w8, sigma0(w0)))); Round(a, b, c, d, e, f, g, h, Add(K(0x27b70a85ul), Inc(w0, sigma1(w14), w9, sigma0(w1)))); Round(h, a, b, c, d, e, f, g, Add(K(0x2e1b2138ul), Inc(w1, sigma1(w15), w10, sigma0(w2)))); Round(g, h, a, b, c, d, e, f, Add(K(0x4d2c6dfcul), Inc(w2, sigma1(w0), w11, sigma0(w3)))); Round(f, g, h, a, b, c, d, e, Add(K(0x53380d13ul), Inc(w3, sigma1(w1), w12, sigma0(w4)))); Round(e, f, g, h, a, b, c, d, Add(K(0x650a7354ul), Inc(w4, sigma1(w2), w13, sigma0(w5)))); Round(d, e, f, g, h, a, b, c, Add(K(0x766a0abbul), Inc(w5, sigma1(w3), w14, sigma0(w6)))); Round(c, d, e, f, g, h, a, b, Add(K(0x81c2c92eul), Inc(w6, sigma1(w4), w15, sigma0(w7)))); Round(b, c, d, e, f, g, h, a, Add(K(0x92722c85ul), Inc(w7, sigma1(w5), w0, sigma0(w8)))); Round(a, b, c, d, e, f, g, h, Add(K(0xa2bfe8a1ul), Inc(w8, sigma1(w6), w1, sigma0(w9)))); Round(h, a, b, c, d, e, f, g, Add(K(0xa81a664bul), Inc(w9, sigma1(w7), w2, sigma0(w10)))); Round(g, h, a, b, c, d, e, f, Add(K(0xc24b8b70ul), Inc(w10, sigma1(w8), w3, sigma0(w11)))); Round(f, g, h, a, b, c, d, e, Add(K(0xc76c51a3ul), Inc(w11, sigma1(w9), w4, sigma0(w12)))); Round(e, f, g, h, a, b, c, d, Add(K(0xd192e819ul), Inc(w12, sigma1(w10), w5, sigma0(w13)))); Round(d, e, f, g, h, a, b, c, Add(K(0xd6990624ul), Inc(w13, sigma1(w11), w6, sigma0(w14)))); Round(c, d, e, f, g, h, a, b, Add(K(0xf40e3585ul), Inc(w14, sigma1(w12), w7, sigma0(w15)))); Round(b, c, d, e, f, g, h, a, Add(K(0x106aa070ul), Inc(w15, sigma1(w13), w8, sigma0(w0)))); Round(a, b, c, d, e, f, g, h, Add(K(0x19a4c116ul), Inc(w0, sigma1(w14), w9, sigma0(w1)))); Round(h, a, b, c, d, e, f, g, Add(K(0x1e376c08ul), Inc(w1, sigma1(w15), w10, sigma0(w2)))); Round(g, h, a, b, c, d, e, f, Add(K(0x2748774cul), Inc(w2, sigma1(w0), w11, sigma0(w3)))); Round(f, g, h, a, b, c, d, e, Add(K(0x34b0bcb5ul), Inc(w3, sigma1(w1), w12, sigma0(w4)))); Round(e, f, g, h, a, b, c, d, Add(K(0x391c0cb3ul), Inc(w4, sigma1(w2), w13, sigma0(w5)))); Round(d, e, f, g, h, a, b, c, Add(K(0x4ed8aa4aul), Inc(w5, sigma1(w3), w14, sigma0(w6)))); Round(c, d, e, f, g, h, a, b, Add(K(0x5b9cca4ful), Inc(w6, sigma1(w4), w15, sigma0(w7)))); Round(b, c, d, e, f, g, h, a, Add(K(0x682e6ff3ul), Inc(w7, sigma1(w5), w0, sigma0(w8)))); Round(a, b, c, d, e, f, g, h, Add(K(0x748f82eeul), Inc(w8, sigma1(w6), w1, sigma0(w9)))); Round(h, a, b, c, d, e, f, g, Add(K(0x78a5636ful), Inc(w9, sigma1(w7), w2, sigma0(w10)))); Round(g, h, a, b, c, d, e, f, Add(K(0x84c87814ul), Inc(w10, sigma1(w8), w3, sigma0(w11)))); Round(f, g, h, a, b, c, d, e, Add(K(0x8cc70208ul), Inc(w11, sigma1(w9), w4, sigma0(w12)))); Round(e, f, g, h, a, b, c, d, Add(K(0x90befffaul), Inc(w12, sigma1(w10), w5, sigma0(w13)))); Round(d, e, f, g, h, a, b, c, Add(K(0xa4506cebul), Inc(w13, sigma1(w11), w6, sigma0(w14)))); Round(c, d, e, f, g, h, a, b, Add(K(0xbef9a3f7ul), Inc(w14, sigma1(w12), w7, sigma0(w15)))); Round(b, c, d, e, f, g, h, a, Add(K(0xc67178f2ul), Inc(w15, sigma1(w13), w8, sigma0(w0)))); a = Add(a, K(0x6a09e667ul)); b = Add(b, K(0xbb67ae85ul)); c = Add(c, K(0x3c6ef372ul)); d = Add(d, K(0xa54ff53aul)); e = Add(e, K(0x510e527ful)); f = Add(f, K(0x9b05688cul)); g = Add(g, K(0x1f83d9abul)); h = Add(h, K(0x5be0cd19ul)); __m128i t0 = a, t1 = b, t2 = c, t3 = d, t4 = e, t5 = f, t6 = g, t7 = h; // Transform 2 Round(a, b, c, d, e, f, g, h, K(0xc28a2f98ul)); Round(h, a, b, c, d, e, f, g, K(0x71374491ul)); Round(g, h, a, b, c, d, e, f, K(0xb5c0fbcful)); Round(f, g, h, a, b, c, d, e, K(0xe9b5dba5ul)); Round(e, f, g, h, a, b, c, d, K(0x3956c25bul)); Round(d, e, f, g, h, a, b, c, K(0x59f111f1ul)); Round(c, d, e, f, g, h, a, b, K(0x923f82a4ul)); Round(b, c, d, e, f, g, h, a, K(0xab1c5ed5ul)); Round(a, b, c, d, e, f, g, h, K(0xd807aa98ul)); Round(h, a, b, c, d, e, f, g, K(0x12835b01ul)); Round(g, h, a, b, c, d, e, f, K(0x243185beul)); Round(f, g, h, a, b, c, d, e, K(0x550c7dc3ul)); Round(e, f, g, h, a, b, c, d, K(0x72be5d74ul)); Round(d, e, f, g, h, a, b, c, K(0x80deb1feul)); Round(c, d, e, f, g, h, a, b, K(0x9bdc06a7ul)); Round(b, c, d, e, f, g, h, a, K(0xc19bf374ul)); Round(a, b, c, d, e, f, g, h, K(0x649b69c1ul)); Round(h, a, b, c, d, e, f, g, K(0xf0fe4786ul)); Round(g, h, a, b, c, d, e, f, K(0x0fe1edc6ul)); Round(f, g, h, a, b, c, d, e, K(0x240cf254ul)); Round(e, f, g, h, a, b, c, d, K(0x4fe9346ful)); Round(d, e, f, g, h, a, b, c, K(0x6cc984beul)); Round(c, d, e, f, g, h, a, b, K(0x61b9411eul)); Round(b, c, d, e, f, g, h, a, K(0x16f988faul)); Round(a, b, c, d, e, f, g, h, K(0xf2c65152ul)); Round(h, a, b, c, d, e, f, g, K(0xa88e5a6dul)); Round(g, h, a, b, c, d, e, f, K(0xb019fc65ul)); Round(f, g, h, a, b, c, d, e, K(0xb9d99ec7ul)); Round(e, f, g, h, a, b, c, d, K(0x9a1231c3ul)); Round(d, e, f, g, h, a, b, c, K(0xe70eeaa0ul)); Round(c, d, e, f, g, h, a, b, K(0xfdb1232bul)); Round(b, c, d, e, f, g, h, a, K(0xc7353eb0ul)); Round(a, b, c, d, e, f, g, h, K(0x3069bad5ul)); Round(h, a, b, c, d, e, f, g, K(0xcb976d5ful)); Round(g, h, a, b, c, d, e, f, K(0x5a0f118ful)); Round(f, g, h, a, b, c, d, e, K(0xdc1eeefdul)); Round(e, f, g, h, a, b, c, d, K(0x0a35b689ul)); Round(d, e, f, g, h, a, b, c, K(0xde0b7a04ul)); Round(c, d, e, f, g, h, a, b, K(0x58f4ca9dul)); Round(b, c, d, e, f, g, h, a, K(0xe15d5b16ul)); Round(a, b, c, d, e, f, g, h, K(0x007f3e86ul)); Round(h, a, b, c, d, e, f, g, K(0x37088980ul)); Round(g, h, a, b, c, d, e, f, K(0xa507ea32ul)); Round(f, g, h, a, b, c, d, e, K(0x6fab9537ul)); Round(e, f, g, h, a, b, c, d, K(0x17406110ul)); Round(d, e, f, g, h, a, b, c, K(0x0d8cd6f1ul)); Round(c, d, e, f, g, h, a, b, K(0xcdaa3b6dul)); Round(b, c, d, e, f, g, h, a, K(0xc0bbbe37ul)); Round(a, b, c, d, e, f, g, h, K(0x83613bdaul)); Round(h, a, b, c, d, e, f, g, K(0xdb48a363ul)); Round(g, h, a, b, c, d, e, f, K(0x0b02e931ul)); Round(f, g, h, a, b, c, d, e, K(0x6fd15ca7ul)); Round(e, f, g, h, a, b, c, d, K(0x521afacaul)); Round(d, e, f, g, h, a, b, c, K(0x31338431ul)); Round(c, d, e, f, g, h, a, b, K(0x6ed41a95ul)); Round(b, c, d, e, f, g, h, a, K(0x6d437890ul)); Round(a, b, c, d, e, f, g, h, K(0xc39c91f2ul)); Round(h, a, b, c, d, e, f, g, K(0x9eccabbdul)); Round(g, h, a, b, c, d, e, f, K(0xb5c9a0e6ul)); Round(f, g, h, a, b, c, d, e, K(0x532fb63cul)); Round(e, f, g, h, a, b, c, d, K(0xd2c741c6ul)); Round(d, e, f, g, h, a, b, c, K(0x07237ea3ul)); Round(c, d, e, f, g, h, a, b, K(0xa4954b68ul)); Round(b, c, d, e, f, g, h, a, K(0x4c191d76ul)); w0 = Add(t0, a); w1 = Add(t1, b); w2 = Add(t2, c); w3 = Add(t3, d); w4 = Add(t4, e); w5 = Add(t5, f); w6 = Add(t6, g); w7 = Add(t7, h); // Transform 3 a = K(0x6a09e667ul); b = K(0xbb67ae85ul); c = K(0x3c6ef372ul); d = K(0xa54ff53aul); e = K(0x510e527ful); f = K(0x9b05688cul); g = K(0x1f83d9abul); h = K(0x5be0cd19ul); Round(a, b, c, d, e, f, g, h, Add(K(0x428a2f98ul), w0)); Round(h, a, b, c, d, e, f, g, Add(K(0x71374491ul), w1)); Round(g, h, a, b, c, d, e, f, Add(K(0xb5c0fbcful), w2)); Round(f, g, h, a, b, c, d, e, Add(K(0xe9b5dba5ul), w3)); Round(e, f, g, h, a, b, c, d, Add(K(0x3956c25bul), w4)); Round(d, e, f, g, h, a, b, c, Add(K(0x59f111f1ul), w5)); Round(c, d, e, f, g, h, a, b, Add(K(0x923f82a4ul), w6)); Round(b, c, d, e, f, g, h, a, Add(K(0xab1c5ed5ul), w7)); Round(a, b, c, d, e, f, g, h, K(0x5807aa98ul)); Round(h, a, b, c, d, e, f, g, K(0x12835b01ul)); Round(g, h, a, b, c, d, e, f, K(0x243185beul)); Round(f, g, h, a, b, c, d, e, K(0x550c7dc3ul)); Round(e, f, g, h, a, b, c, d, K(0x72be5d74ul)); Round(d, e, f, g, h, a, b, c, K(0x80deb1feul)); Round(c, d, e, f, g, h, a, b, K(0x9bdc06a7ul)); Round(b, c, d, e, f, g, h, a, K(0xc19bf274ul)); Round(a, b, c, d, e, f, g, h, Add(K(0xe49b69c1ul), Inc(w0, sigma0(w1)))); Round(h, a, b, c, d, e, f, g, Add(K(0xefbe4786ul), Inc(w1, K(0xa00000ul), sigma0(w2)))); Round(g, h, a, b, c, d, e, f, Add(K(0x0fc19dc6ul), Inc(w2, sigma1(w0), sigma0(w3)))); Round(f, g, h, a, b, c, d, e, Add(K(0x240ca1ccul), Inc(w3, sigma1(w1), sigma0(w4)))); Round(e, f, g, h, a, b, c, d, Add(K(0x2de92c6ful), Inc(w4, sigma1(w2), sigma0(w5)))); Round(d, e, f, g, h, a, b, c, Add(K(0x4a7484aaul), Inc(w5, sigma1(w3), sigma0(w6)))); Round(c, d, e, f, g, h, a, b, Add(K(0x5cb0a9dcul), Inc(w6, sigma1(w4), K(0x100ul), sigma0(w7)))); Round(b, c, d, e, f, g, h, a, Add(K(0x76f988daul), Inc(w7, sigma1(w5), w0, K(0x11002000ul)))); Round(a, b, c, d, e, f, g, h, Add(K(0x983e5152ul), w8 = Add(K(0x80000000ul), sigma1(w6), w1))); Round(h, a, b, c, d, e, f, g, Add(K(0xa831c66dul), w9 = Add(sigma1(w7), w2))); Round(g, h, a, b, c, d, e, f, Add(K(0xb00327c8ul), w10 = Add(sigma1(w8), w3))); Round(f, g, h, a, b, c, d, e, Add(K(0xbf597fc7ul), w11 = Add(sigma1(w9), w4))); Round(e, f, g, h, a, b, c, d, Add(K(0xc6e00bf3ul), w12 = Add(sigma1(w10), w5))); Round(d, e, f, g, h, a, b, c, Add(K(0xd5a79147ul), w13 = Add(sigma1(w11), w6))); Round(c, d, e, f, g, h, a, b, Add(K(0x06ca6351ul), w14 = Add(sigma1(w12), w7, K(0x400022ul)))); Round(b, c, d, e, f, g, h, a, Add(K(0x14292967ul), w15 = Add(K(0x100ul), sigma1(w13), w8, sigma0(w0)))); Round(a, b, c, d, e, f, g, h, Add(K(0x27b70a85ul), Inc(w0, sigma1(w14), w9, sigma0(w1)))); Round(h, a, b, c, d, e, f, g, Add(K(0x2e1b2138ul), Inc(w1, sigma1(w15), w10, sigma0(w2)))); Round(g, h, a, b, c, d, e, f, Add(K(0x4d2c6dfcul), Inc(w2, sigma1(w0), w11, sigma0(w3)))); Round(f, g, h, a, b, c, d, e, Add(K(0x53380d13ul), Inc(w3, sigma1(w1), w12, sigma0(w4)))); Round(e, f, g, h, a, b, c, d, Add(K(0x650a7354ul), Inc(w4, sigma1(w2), w13, sigma0(w5)))); Round(d, e, f, g, h, a, b, c, Add(K(0x766a0abbul), Inc(w5, sigma1(w3), w14, sigma0(w6)))); Round(c, d, e, f, g, h, a, b, Add(K(0x81c2c92eul), Inc(w6, sigma1(w4), w15, sigma0(w7)))); Round(b, c, d, e, f, g, h, a, Add(K(0x92722c85ul), Inc(w7, sigma1(w5), w0, sigma0(w8)))); Round(a, b, c, d, e, f, g, h, Add(K(0xa2bfe8a1ul), Inc(w8, sigma1(w6), w1, sigma0(w9)))); Round(h, a, b, c, d, e, f, g, Add(K(0xa81a664bul), Inc(w9, sigma1(w7), w2, sigma0(w10)))); Round(g, h, a, b, c, d, e, f, Add(K(0xc24b8b70ul), Inc(w10, sigma1(w8), w3, sigma0(w11)))); Round(f, g, h, a, b, c, d, e, Add(K(0xc76c51a3ul), Inc(w11, sigma1(w9), w4, sigma0(w12)))); Round(e, f, g, h, a, b, c, d, Add(K(0xd192e819ul), Inc(w12, sigma1(w10), w5, sigma0(w13)))); Round(d, e, f, g, h, a, b, c, Add(K(0xd6990624ul), Inc(w13, sigma1(w11), w6, sigma0(w14)))); Round(c, d, e, f, g, h, a, b, Add(K(0xf40e3585ul), Inc(w14, sigma1(w12), w7, sigma0(w15)))); Round(b, c, d, e, f, g, h, a, Add(K(0x106aa070ul), Inc(w15, sigma1(w13), w8, sigma0(w0)))); Round(a, b, c, d, e, f, g, h, Add(K(0x19a4c116ul), Inc(w0, sigma1(w14), w9, sigma0(w1)))); Round(h, a, b, c, d, e, f, g, Add(K(0x1e376c08ul), Inc(w1, sigma1(w15), w10, sigma0(w2)))); Round(g, h, a, b, c, d, e, f, Add(K(0x2748774cul), Inc(w2, sigma1(w0), w11, sigma0(w3)))); Round(f, g, h, a, b, c, d, e, Add(K(0x34b0bcb5ul), Inc(w3, sigma1(w1), w12, sigma0(w4)))); Round(e, f, g, h, a, b, c, d, Add(K(0x391c0cb3ul), Inc(w4, sigma1(w2), w13, sigma0(w5)))); Round(d, e, f, g, h, a, b, c, Add(K(0x4ed8aa4aul), Inc(w5, sigma1(w3), w14, sigma0(w6)))); Round(c, d, e, f, g, h, a, b, Add(K(0x5b9cca4ful), Inc(w6, sigma1(w4), w15, sigma0(w7)))); Round(b, c, d, e, f, g, h, a, Add(K(0x682e6ff3ul), Inc(w7, sigma1(w5), w0, sigma0(w8)))); Round(a, b, c, d, e, f, g, h, Add(K(0x748f82eeul), Inc(w8, sigma1(w6), w1, sigma0(w9)))); Round(h, a, b, c, d, e, f, g, Add(K(0x78a5636ful), Inc(w9, sigma1(w7), w2, sigma0(w10)))); Round(g, h, a, b, c, d, e, f, Add(K(0x84c87814ul), Inc(w10, sigma1(w8), w3, sigma0(w11)))); Round(f, g, h, a, b, c, d, e, Add(K(0x8cc70208ul), Inc(w11, sigma1(w9), w4, sigma0(w12)))); Round(e, f, g, h, a, b, c, d, Add(K(0x90befffaul), Inc(w12, sigma1(w10), w5, sigma0(w13)))); Round(d, e, f, g, h, a, b, c, Add(K(0xa4506cebul), Inc(w13, sigma1(w11), w6, sigma0(w14)))); Round(c, d, e, f, g, h, a, b, Add(K(0xbef9a3f7ul), w14, sigma1(w12), w7, sigma0(w15))); Round(b, c, d, e, f, g, h, a, Add(K(0xc67178f2ul), w15, sigma1(w13), w8, sigma0(w0))); // Output Write4(out, 0, Add(a, K(0x6a09e667ul))); Write4(out, 4, Add(b, K(0xbb67ae85ul))); Write4(out, 8, Add(c, K(0x3c6ef372ul))); Write4(out, 12, Add(d, K(0xa54ff53aul))); Write4(out, 16, Add(e, K(0x510e527ful))); Write4(out, 20, Add(f, K(0x9b05688cul))); Write4(out, 24, Add(g, K(0x1f83d9abul))); Write4(out, 28, Add(h, K(0x5be0cd19ul))); } } #endif
0
bitcoin/src
bitcoin/src/crypto/chacha20.h
// Copyright (c) 2017-2022 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_CRYPTO_CHACHA20_H #define BITCOIN_CRYPTO_CHACHA20_H #include <span.h> #include <array> #include <cstddef> #include <cstdlib> #include <stdint.h> #include <utility> // classes for ChaCha20 256-bit stream cipher developed by Daniel J. Bernstein // https://cr.yp.to/chacha/chacha-20080128.pdf. // // The 128-bit input is here implemented as a 96-bit nonce and a 32-bit block // counter, as in RFC8439 Section 2.3. When the 32-bit block counter overflows // the first 32-bit part of the nonce is automatically incremented, making it // conceptually compatible with variants that use a 64/64 split instead. /** ChaCha20 cipher that only operates on multiples of 64 bytes. */ class ChaCha20Aligned { private: uint32_t input[12]; public: /** Expected key length in constructor and SetKey. */ static constexpr unsigned KEYLEN{32}; /** Block size (inputs/outputs to Keystream / Crypt should be multiples of this). */ static constexpr unsigned BLOCKLEN{64}; /** For safety, disallow initialization without key. */ ChaCha20Aligned() noexcept = delete; /** Initialize a cipher with specified 32-byte key. */ ChaCha20Aligned(Span<const std::byte> key) noexcept; /** Destructor to clean up private memory. */ ~ChaCha20Aligned(); /** Set 32-byte key, and seek to nonce 0 and block position 0. */ void SetKey(Span<const std::byte> key) noexcept; /** Type for 96-bit nonces used by the Set function below. * * The first field corresponds to the LE32-encoded first 4 bytes of the nonce, also referred * to as the '32-bit fixed-common part' in Example 2.8.2 of RFC8439. * * The second field corresponds to the LE64-encoded last 8 bytes of the nonce. * */ using Nonce96 = std::pair<uint32_t, uint64_t>; /** Set the 96-bit nonce and 32-bit block counter. * * Block_counter selects a position to seek to (to byte BLOCKLEN*block_counter). After 256 GiB, * the block counter overflows, and nonce.first is incremented. */ void Seek(Nonce96 nonce, uint32_t block_counter) noexcept; /** outputs the keystream into out, whose length must be a multiple of BLOCKLEN. */ void Keystream(Span<std::byte> out) noexcept; /** en/deciphers the message <input> and write the result into <output> * * The size of input and output must be equal, and be a multiple of BLOCKLEN. */ void Crypt(Span<const std::byte> input, Span<std::byte> output) noexcept; }; /** Unrestricted ChaCha20 cipher. */ class ChaCha20 { private: ChaCha20Aligned m_aligned; std::array<std::byte, ChaCha20Aligned::BLOCKLEN> m_buffer; unsigned m_bufleft{0}; public: /** Expected key length in constructor and SetKey. */ static constexpr unsigned KEYLEN = ChaCha20Aligned::KEYLEN; /** For safety, disallow initialization without key. */ ChaCha20() noexcept = delete; /** Initialize a cipher with specified 32-byte key. */ ChaCha20(Span<const std::byte> key) noexcept : m_aligned(key) {} /** Destructor to clean up private memory. */ ~ChaCha20(); /** Set 32-byte key, and seek to nonce 0 and block position 0. */ void SetKey(Span<const std::byte> key) noexcept; /** 96-bit nonce type. */ using Nonce96 = ChaCha20Aligned::Nonce96; /** Set the 96-bit nonce and 32-bit block counter. See ChaCha20Aligned::Seek. */ void Seek(Nonce96 nonce, uint32_t block_counter) noexcept { m_aligned.Seek(nonce, block_counter); m_bufleft = 0; } /** en/deciphers the message <in_bytes> and write the result into <out_bytes> * * The size of in_bytes and out_bytes must be equal. */ void Crypt(Span<const std::byte> in_bytes, Span<std::byte> out_bytes) noexcept; /** outputs the keystream to out. */ void Keystream(Span<std::byte> out) noexcept; }; /** Forward-secure ChaCha20 * * This implements a stream cipher that automatically transitions to a new stream with a new key * and new nonce after a predefined number of encryptions or decryptions. * * See BIP324 for details. */ class FSChaCha20 { private: /** Internal stream cipher. */ ChaCha20 m_chacha20; /** The number of encryptions/decryptions before a rekey happens. */ const uint32_t m_rekey_interval; /** The number of encryptions/decryptions since the last rekey. */ uint32_t m_chunk_counter{0}; /** The number of rekey operations that have happened. */ uint64_t m_rekey_counter{0}; public: /** Length of keys expected by the constructor. */ static constexpr unsigned KEYLEN = 32; // No copy or move to protect the secret. FSChaCha20(const FSChaCha20&) = delete; FSChaCha20(FSChaCha20&&) = delete; FSChaCha20& operator=(const FSChaCha20&) = delete; FSChaCha20& operator=(FSChaCha20&&) = delete; /** Construct an FSChaCha20 cipher that rekeys every rekey_interval Crypt() calls. */ FSChaCha20(Span<const std::byte> key, uint32_t rekey_interval) noexcept; /** Encrypt or decrypt a chunk. */ void Crypt(Span<const std::byte> input, Span<std::byte> output) noexcept; }; #endif // BITCOIN_CRYPTO_CHACHA20_H
0
bitcoin/src
bitcoin/src/crypto/chacha20.cpp
// Copyright (c) 2017-2022 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. // Based on the public domain implementation 'merged' by D. J. Bernstein // See https://cr.yp.to/chacha.html. #include <crypto/common.h> #include <crypto/chacha20.h> #include <support/cleanse.h> #include <span.h> #include <algorithm> #include <string.h> constexpr static inline uint32_t rotl32(uint32_t v, int c) { return (v << c) | (v >> (32 - c)); } #define QUARTERROUND(a,b,c,d) \ a += b; d = rotl32(d ^ a, 16); \ c += d; b = rotl32(b ^ c, 12); \ a += b; d = rotl32(d ^ a, 8); \ c += d; b = rotl32(b ^ c, 7); #define REPEAT10(a) do { {a}; {a}; {a}; {a}; {a}; {a}; {a}; {a}; {a}; {a}; } while(0) void ChaCha20Aligned::SetKey(Span<const std::byte> key) noexcept { assert(key.size() == KEYLEN); input[0] = ReadLE32(UCharCast(key.data() + 0)); input[1] = ReadLE32(UCharCast(key.data() + 4)); input[2] = ReadLE32(UCharCast(key.data() + 8)); input[3] = ReadLE32(UCharCast(key.data() + 12)); input[4] = ReadLE32(UCharCast(key.data() + 16)); input[5] = ReadLE32(UCharCast(key.data() + 20)); input[6] = ReadLE32(UCharCast(key.data() + 24)); input[7] = ReadLE32(UCharCast(key.data() + 28)); input[8] = 0; input[9] = 0; input[10] = 0; input[11] = 0; } ChaCha20Aligned::~ChaCha20Aligned() { memory_cleanse(input, sizeof(input)); } ChaCha20Aligned::ChaCha20Aligned(Span<const std::byte> key) noexcept { SetKey(key); } void ChaCha20Aligned::Seek(Nonce96 nonce, uint32_t block_counter) noexcept { input[8] = block_counter; input[9] = nonce.first; input[10] = nonce.second; input[11] = nonce.second >> 32; } inline void ChaCha20Aligned::Keystream(Span<std::byte> output) noexcept { unsigned char* c = UCharCast(output.data()); size_t blocks = output.size() / BLOCKLEN; assert(blocks * BLOCKLEN == output.size()); uint32_t x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15; uint32_t j4, j5, j6, j7, j8, j9, j10, j11, j12, j13, j14, j15; if (!blocks) return; j4 = input[0]; j5 = input[1]; j6 = input[2]; j7 = input[3]; j8 = input[4]; j9 = input[5]; j10 = input[6]; j11 = input[7]; j12 = input[8]; j13 = input[9]; j14 = input[10]; j15 = input[11]; for (;;) { x0 = 0x61707865; x1 = 0x3320646e; x2 = 0x79622d32; x3 = 0x6b206574; x4 = j4; x5 = j5; x6 = j6; x7 = j7; x8 = j8; x9 = j9; x10 = j10; x11 = j11; x12 = j12; x13 = j13; x14 = j14; x15 = j15; // The 20 inner ChaCha20 rounds are unrolled here for performance. REPEAT10( QUARTERROUND( x0, x4, x8,x12); QUARTERROUND( x1, x5, x9,x13); QUARTERROUND( x2, x6,x10,x14); QUARTERROUND( x3, x7,x11,x15); QUARTERROUND( x0, x5,x10,x15); QUARTERROUND( x1, x6,x11,x12); QUARTERROUND( x2, x7, x8,x13); QUARTERROUND( x3, x4, x9,x14); ); x0 += 0x61707865; x1 += 0x3320646e; x2 += 0x79622d32; x3 += 0x6b206574; x4 += j4; x5 += j5; x6 += j6; x7 += j7; x8 += j8; x9 += j9; x10 += j10; x11 += j11; x12 += j12; x13 += j13; x14 += j14; x15 += j15; ++j12; if (!j12) ++j13; WriteLE32(c + 0, x0); WriteLE32(c + 4, x1); WriteLE32(c + 8, x2); WriteLE32(c + 12, x3); WriteLE32(c + 16, x4); WriteLE32(c + 20, x5); WriteLE32(c + 24, x6); WriteLE32(c + 28, x7); WriteLE32(c + 32, x8); WriteLE32(c + 36, x9); WriteLE32(c + 40, x10); WriteLE32(c + 44, x11); WriteLE32(c + 48, x12); WriteLE32(c + 52, x13); WriteLE32(c + 56, x14); WriteLE32(c + 60, x15); if (blocks == 1) { input[8] = j12; input[9] = j13; return; } blocks -= 1; c += BLOCKLEN; } } inline void ChaCha20Aligned::Crypt(Span<const std::byte> in_bytes, Span<std::byte> out_bytes) noexcept { assert(in_bytes.size() == out_bytes.size()); const unsigned char* m = UCharCast(in_bytes.data()); unsigned char* c = UCharCast(out_bytes.data()); size_t blocks = out_bytes.size() / BLOCKLEN; assert(blocks * BLOCKLEN == out_bytes.size()); uint32_t x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15; uint32_t j4, j5, j6, j7, j8, j9, j10, j11, j12, j13, j14, j15; if (!blocks) return; j4 = input[0]; j5 = input[1]; j6 = input[2]; j7 = input[3]; j8 = input[4]; j9 = input[5]; j10 = input[6]; j11 = input[7]; j12 = input[8]; j13 = input[9]; j14 = input[10]; j15 = input[11]; for (;;) { x0 = 0x61707865; x1 = 0x3320646e; x2 = 0x79622d32; x3 = 0x6b206574; x4 = j4; x5 = j5; x6 = j6; x7 = j7; x8 = j8; x9 = j9; x10 = j10; x11 = j11; x12 = j12; x13 = j13; x14 = j14; x15 = j15; // The 20 inner ChaCha20 rounds are unrolled here for performance. REPEAT10( QUARTERROUND( x0, x4, x8,x12); QUARTERROUND( x1, x5, x9,x13); QUARTERROUND( x2, x6,x10,x14); QUARTERROUND( x3, x7,x11,x15); QUARTERROUND( x0, x5,x10,x15); QUARTERROUND( x1, x6,x11,x12); QUARTERROUND( x2, x7, x8,x13); QUARTERROUND( x3, x4, x9,x14); ); x0 += 0x61707865; x1 += 0x3320646e; x2 += 0x79622d32; x3 += 0x6b206574; x4 += j4; x5 += j5; x6 += j6; x7 += j7; x8 += j8; x9 += j9; x10 += j10; x11 += j11; x12 += j12; x13 += j13; x14 += j14; x15 += j15; x0 ^= ReadLE32(m + 0); x1 ^= ReadLE32(m + 4); x2 ^= ReadLE32(m + 8); x3 ^= ReadLE32(m + 12); x4 ^= ReadLE32(m + 16); x5 ^= ReadLE32(m + 20); x6 ^= ReadLE32(m + 24); x7 ^= ReadLE32(m + 28); x8 ^= ReadLE32(m + 32); x9 ^= ReadLE32(m + 36); x10 ^= ReadLE32(m + 40); x11 ^= ReadLE32(m + 44); x12 ^= ReadLE32(m + 48); x13 ^= ReadLE32(m + 52); x14 ^= ReadLE32(m + 56); x15 ^= ReadLE32(m + 60); ++j12; if (!j12) ++j13; WriteLE32(c + 0, x0); WriteLE32(c + 4, x1); WriteLE32(c + 8, x2); WriteLE32(c + 12, x3); WriteLE32(c + 16, x4); WriteLE32(c + 20, x5); WriteLE32(c + 24, x6); WriteLE32(c + 28, x7); WriteLE32(c + 32, x8); WriteLE32(c + 36, x9); WriteLE32(c + 40, x10); WriteLE32(c + 44, x11); WriteLE32(c + 48, x12); WriteLE32(c + 52, x13); WriteLE32(c + 56, x14); WriteLE32(c + 60, x15); if (blocks == 1) { input[8] = j12; input[9] = j13; return; } blocks -= 1; c += BLOCKLEN; m += BLOCKLEN; } } void ChaCha20::Keystream(Span<std::byte> out) noexcept { if (out.empty()) return; if (m_bufleft) { unsigned reuse = std::min<size_t>(m_bufleft, out.size()); std::copy(m_buffer.end() - m_bufleft, m_buffer.end() - m_bufleft + reuse, out.begin()); m_bufleft -= reuse; out = out.subspan(reuse); } if (out.size() >= m_aligned.BLOCKLEN) { size_t blocks = out.size() / m_aligned.BLOCKLEN; m_aligned.Keystream(out.first(blocks * m_aligned.BLOCKLEN)); out = out.subspan(blocks * m_aligned.BLOCKLEN); } if (!out.empty()) { m_aligned.Keystream(m_buffer); std::copy(m_buffer.begin(), m_buffer.begin() + out.size(), out.begin()); m_bufleft = m_aligned.BLOCKLEN - out.size(); } } void ChaCha20::Crypt(Span<const std::byte> input, Span<std::byte> output) noexcept { assert(input.size() == output.size()); if (!input.size()) return; if (m_bufleft) { unsigned reuse = std::min<size_t>(m_bufleft, input.size()); for (unsigned i = 0; i < reuse; i++) { output[i] = input[i] ^ m_buffer[m_aligned.BLOCKLEN - m_bufleft + i]; } m_bufleft -= reuse; output = output.subspan(reuse); input = input.subspan(reuse); } if (input.size() >= m_aligned.BLOCKLEN) { size_t blocks = input.size() / m_aligned.BLOCKLEN; m_aligned.Crypt(input.first(blocks * m_aligned.BLOCKLEN), output.first(blocks * m_aligned.BLOCKLEN)); output = output.subspan(blocks * m_aligned.BLOCKLEN); input = input.subspan(blocks * m_aligned.BLOCKLEN); } if (!input.empty()) { m_aligned.Keystream(m_buffer); for (unsigned i = 0; i < input.size(); i++) { output[i] = input[i] ^ m_buffer[i]; } m_bufleft = m_aligned.BLOCKLEN - input.size(); } } ChaCha20::~ChaCha20() { memory_cleanse(m_buffer.data(), m_buffer.size()); } void ChaCha20::SetKey(Span<const std::byte> key) noexcept { m_aligned.SetKey(key); m_bufleft = 0; memory_cleanse(m_buffer.data(), m_buffer.size()); } FSChaCha20::FSChaCha20(Span<const std::byte> key, uint32_t rekey_interval) noexcept : m_chacha20(key), m_rekey_interval(rekey_interval) { assert(key.size() == KEYLEN); } void FSChaCha20::Crypt(Span<const std::byte> input, Span<std::byte> output) noexcept { assert(input.size() == output.size()); // Invoke internal stream cipher for actual encryption/decryption. m_chacha20.Crypt(input, output); // Rekey after m_rekey_interval encryptions/decryptions. if (++m_chunk_counter == m_rekey_interval) { // Get new key from the stream cipher. std::byte new_key[KEYLEN]; m_chacha20.Keystream(new_key); // Update its key. m_chacha20.SetKey(new_key); // Wipe the key (a copy remains inside m_chacha20, where it'll be wiped on the next rekey // or on destruction). memory_cleanse(new_key, sizeof(new_key)); // Set the nonce for the new section of output. m_chacha20.Seek({0, ++m_rekey_counter}, 0); // Reset the chunk counter. m_chunk_counter = 0; } }
0
bitcoin/src
bitcoin/src/crypto/sha1.h
// Copyright (c) 2014-2022 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_CRYPTO_SHA1_H #define BITCOIN_CRYPTO_SHA1_H #include <cstdlib> #include <stdint.h> /** A hasher class for SHA1. */ class CSHA1 { private: uint32_t s[5]; unsigned char buf[64]; uint64_t bytes{0}; public: static const size_t OUTPUT_SIZE = 20; CSHA1(); CSHA1& Write(const unsigned char* data, size_t len); void Finalize(unsigned char hash[OUTPUT_SIZE]); CSHA1& Reset(); }; #endif // BITCOIN_CRYPTO_SHA1_H
0
bitcoin/src
bitcoin/src/crypto/ripemd160.cpp
// Copyright (c) 2014-2019 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include <crypto/ripemd160.h> #include <crypto/common.h> #include <string.h> // Internal implementation code. namespace { /// Internal RIPEMD-160 implementation. namespace ripemd160 { uint32_t inline f1(uint32_t x, uint32_t y, uint32_t z) { return x ^ y ^ z; } uint32_t inline f2(uint32_t x, uint32_t y, uint32_t z) { return (x & y) | (~x & z); } uint32_t inline f3(uint32_t x, uint32_t y, uint32_t z) { return (x | ~y) ^ z; } uint32_t inline f4(uint32_t x, uint32_t y, uint32_t z) { return (x & z) | (y & ~z); } uint32_t inline f5(uint32_t x, uint32_t y, uint32_t z) { return x ^ (y | ~z); } /** Initialize RIPEMD-160 state. */ void inline Initialize(uint32_t* s) { s[0] = 0x67452301ul; s[1] = 0xEFCDAB89ul; s[2] = 0x98BADCFEul; s[3] = 0x10325476ul; s[4] = 0xC3D2E1F0ul; } uint32_t inline rol(uint32_t x, int i) { return (x << i) | (x >> (32 - i)); } 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) { a = rol(a + f + x + k, r) + e; c = rol(c, 10); } 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); } 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); } 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); } 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); } 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); } 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); } 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); } 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); } 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); } 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); } /** Perform a RIPEMD-160 transformation, processing a 64-byte chunk. */ void Transform(uint32_t* s, const unsigned char* chunk) { uint32_t a1 = s[0], b1 = s[1], c1 = s[2], d1 = s[3], e1 = s[4]; uint32_t a2 = a1, b2 = b1, c2 = c1, d2 = d1, e2 = e1; uint32_t w0 = ReadLE32(chunk + 0), w1 = ReadLE32(chunk + 4), w2 = ReadLE32(chunk + 8), w3 = ReadLE32(chunk + 12); uint32_t w4 = ReadLE32(chunk + 16), w5 = ReadLE32(chunk + 20), w6 = ReadLE32(chunk + 24), w7 = ReadLE32(chunk + 28); uint32_t w8 = ReadLE32(chunk + 32), w9 = ReadLE32(chunk + 36), w10 = ReadLE32(chunk + 40), w11 = ReadLE32(chunk + 44); uint32_t w12 = ReadLE32(chunk + 48), w13 = ReadLE32(chunk + 52), w14 = ReadLE32(chunk + 56), w15 = ReadLE32(chunk + 60); R11(a1, b1, c1, d1, e1, w0, 11); R12(a2, b2, c2, d2, e2, w5, 8); R11(e1, a1, b1, c1, d1, w1, 14); R12(e2, a2, b2, c2, d2, w14, 9); R11(d1, e1, a1, b1, c1, w2, 15); R12(d2, e2, a2, b2, c2, w7, 9); R11(c1, d1, e1, a1, b1, w3, 12); R12(c2, d2, e2, a2, b2, w0, 11); R11(b1, c1, d1, e1, a1, w4, 5); R12(b2, c2, d2, e2, a2, w9, 13); R11(a1, b1, c1, d1, e1, w5, 8); R12(a2, b2, c2, d2, e2, w2, 15); R11(e1, a1, b1, c1, d1, w6, 7); R12(e2, a2, b2, c2, d2, w11, 15); R11(d1, e1, a1, b1, c1, w7, 9); R12(d2, e2, a2, b2, c2, w4, 5); R11(c1, d1, e1, a1, b1, w8, 11); R12(c2, d2, e2, a2, b2, w13, 7); R11(b1, c1, d1, e1, a1, w9, 13); R12(b2, c2, d2, e2, a2, w6, 7); R11(a1, b1, c1, d1, e1, w10, 14); R12(a2, b2, c2, d2, e2, w15, 8); R11(e1, a1, b1, c1, d1, w11, 15); R12(e2, a2, b2, c2, d2, w8, 11); R11(d1, e1, a1, b1, c1, w12, 6); R12(d2, e2, a2, b2, c2, w1, 14); R11(c1, d1, e1, a1, b1, w13, 7); R12(c2, d2, e2, a2, b2, w10, 14); R11(b1, c1, d1, e1, a1, w14, 9); R12(b2, c2, d2, e2, a2, w3, 12); R11(a1, b1, c1, d1, e1, w15, 8); R12(a2, b2, c2, d2, e2, w12, 6); R21(e1, a1, b1, c1, d1, w7, 7); R22(e2, a2, b2, c2, d2, w6, 9); R21(d1, e1, a1, b1, c1, w4, 6); R22(d2, e2, a2, b2, c2, w11, 13); R21(c1, d1, e1, a1, b1, w13, 8); R22(c2, d2, e2, a2, b2, w3, 15); R21(b1, c1, d1, e1, a1, w1, 13); R22(b2, c2, d2, e2, a2, w7, 7); R21(a1, b1, c1, d1, e1, w10, 11); R22(a2, b2, c2, d2, e2, w0, 12); R21(e1, a1, b1, c1, d1, w6, 9); R22(e2, a2, b2, c2, d2, w13, 8); R21(d1, e1, a1, b1, c1, w15, 7); R22(d2, e2, a2, b2, c2, w5, 9); R21(c1, d1, e1, a1, b1, w3, 15); R22(c2, d2, e2, a2, b2, w10, 11); R21(b1, c1, d1, e1, a1, w12, 7); R22(b2, c2, d2, e2, a2, w14, 7); R21(a1, b1, c1, d1, e1, w0, 12); R22(a2, b2, c2, d2, e2, w15, 7); R21(e1, a1, b1, c1, d1, w9, 15); R22(e2, a2, b2, c2, d2, w8, 12); R21(d1, e1, a1, b1, c1, w5, 9); R22(d2, e2, a2, b2, c2, w12, 7); R21(c1, d1, e1, a1, b1, w2, 11); R22(c2, d2, e2, a2, b2, w4, 6); R21(b1, c1, d1, e1, a1, w14, 7); R22(b2, c2, d2, e2, a2, w9, 15); R21(a1, b1, c1, d1, e1, w11, 13); R22(a2, b2, c2, d2, e2, w1, 13); R21(e1, a1, b1, c1, d1, w8, 12); R22(e2, a2, b2, c2, d2, w2, 11); R31(d1, e1, a1, b1, c1, w3, 11); R32(d2, e2, a2, b2, c2, w15, 9); R31(c1, d1, e1, a1, b1, w10, 13); R32(c2, d2, e2, a2, b2, w5, 7); R31(b1, c1, d1, e1, a1, w14, 6); R32(b2, c2, d2, e2, a2, w1, 15); R31(a1, b1, c1, d1, e1, w4, 7); R32(a2, b2, c2, d2, e2, w3, 11); R31(e1, a1, b1, c1, d1, w9, 14); R32(e2, a2, b2, c2, d2, w7, 8); R31(d1, e1, a1, b1, c1, w15, 9); R32(d2, e2, a2, b2, c2, w14, 6); R31(c1, d1, e1, a1, b1, w8, 13); R32(c2, d2, e2, a2, b2, w6, 6); R31(b1, c1, d1, e1, a1, w1, 15); R32(b2, c2, d2, e2, a2, w9, 14); R31(a1, b1, c1, d1, e1, w2, 14); R32(a2, b2, c2, d2, e2, w11, 12); R31(e1, a1, b1, c1, d1, w7, 8); R32(e2, a2, b2, c2, d2, w8, 13); R31(d1, e1, a1, b1, c1, w0, 13); R32(d2, e2, a2, b2, c2, w12, 5); R31(c1, d1, e1, a1, b1, w6, 6); R32(c2, d2, e2, a2, b2, w2, 14); R31(b1, c1, d1, e1, a1, w13, 5); R32(b2, c2, d2, e2, a2, w10, 13); R31(a1, b1, c1, d1, e1, w11, 12); R32(a2, b2, c2, d2, e2, w0, 13); R31(e1, a1, b1, c1, d1, w5, 7); R32(e2, a2, b2, c2, d2, w4, 7); R31(d1, e1, a1, b1, c1, w12, 5); R32(d2, e2, a2, b2, c2, w13, 5); R41(c1, d1, e1, a1, b1, w1, 11); R42(c2, d2, e2, a2, b2, w8, 15); R41(b1, c1, d1, e1, a1, w9, 12); R42(b2, c2, d2, e2, a2, w6, 5); R41(a1, b1, c1, d1, e1, w11, 14); R42(a2, b2, c2, d2, e2, w4, 8); R41(e1, a1, b1, c1, d1, w10, 15); R42(e2, a2, b2, c2, d2, w1, 11); R41(d1, e1, a1, b1, c1, w0, 14); R42(d2, e2, a2, b2, c2, w3, 14); R41(c1, d1, e1, a1, b1, w8, 15); R42(c2, d2, e2, a2, b2, w11, 14); R41(b1, c1, d1, e1, a1, w12, 9); R42(b2, c2, d2, e2, a2, w15, 6); R41(a1, b1, c1, d1, e1, w4, 8); R42(a2, b2, c2, d2, e2, w0, 14); R41(e1, a1, b1, c1, d1, w13, 9); R42(e2, a2, b2, c2, d2, w5, 6); R41(d1, e1, a1, b1, c1, w3, 14); R42(d2, e2, a2, b2, c2, w12, 9); R41(c1, d1, e1, a1, b1, w7, 5); R42(c2, d2, e2, a2, b2, w2, 12); R41(b1, c1, d1, e1, a1, w15, 6); R42(b2, c2, d2, e2, a2, w13, 9); R41(a1, b1, c1, d1, e1, w14, 8); R42(a2, b2, c2, d2, e2, w9, 12); R41(e1, a1, b1, c1, d1, w5, 6); R42(e2, a2, b2, c2, d2, w7, 5); R41(d1, e1, a1, b1, c1, w6, 5); R42(d2, e2, a2, b2, c2, w10, 15); R41(c1, d1, e1, a1, b1, w2, 12); R42(c2, d2, e2, a2, b2, w14, 8); R51(b1, c1, d1, e1, a1, w4, 9); R52(b2, c2, d2, e2, a2, w12, 8); R51(a1, b1, c1, d1, e1, w0, 15); R52(a2, b2, c2, d2, e2, w15, 5); R51(e1, a1, b1, c1, d1, w5, 5); R52(e2, a2, b2, c2, d2, w10, 12); R51(d1, e1, a1, b1, c1, w9, 11); R52(d2, e2, a2, b2, c2, w4, 9); R51(c1, d1, e1, a1, b1, w7, 6); R52(c2, d2, e2, a2, b2, w1, 12); R51(b1, c1, d1, e1, a1, w12, 8); R52(b2, c2, d2, e2, a2, w5, 5); R51(a1, b1, c1, d1, e1, w2, 13); R52(a2, b2, c2, d2, e2, w8, 14); R51(e1, a1, b1, c1, d1, w10, 12); R52(e2, a2, b2, c2, d2, w7, 6); R51(d1, e1, a1, b1, c1, w14, 5); R52(d2, e2, a2, b2, c2, w6, 8); R51(c1, d1, e1, a1, b1, w1, 12); R52(c2, d2, e2, a2, b2, w2, 13); R51(b1, c1, d1, e1, a1, w3, 13); R52(b2, c2, d2, e2, a2, w13, 6); R51(a1, b1, c1, d1, e1, w8, 14); R52(a2, b2, c2, d2, e2, w14, 5); R51(e1, a1, b1, c1, d1, w11, 11); R52(e2, a2, b2, c2, d2, w0, 15); R51(d1, e1, a1, b1, c1, w6, 8); R52(d2, e2, a2, b2, c2, w3, 13); R51(c1, d1, e1, a1, b1, w15, 5); R52(c2, d2, e2, a2, b2, w9, 11); R51(b1, c1, d1, e1, a1, w13, 6); R52(b2, c2, d2, e2, a2, w11, 11); uint32_t t = s[0]; s[0] = s[1] + c1 + d2; s[1] = s[2] + d1 + e2; s[2] = s[3] + e1 + a2; s[3] = s[4] + a1 + b2; s[4] = t + b1 + c2; } } // namespace ripemd160 } // namespace ////// RIPEMD160 CRIPEMD160::CRIPEMD160() { ripemd160::Initialize(s); } CRIPEMD160& CRIPEMD160::Write(const unsigned char* data, size_t len) { const unsigned char* end = data + len; size_t bufsize = bytes % 64; if (bufsize && bufsize + len >= 64) { // Fill the buffer, and process it. memcpy(buf + bufsize, data, 64 - bufsize); bytes += 64 - bufsize; data += 64 - bufsize; ripemd160::Transform(s, buf); bufsize = 0; } while (end - data >= 64) { // Process full chunks directly from the source. ripemd160::Transform(s, data); bytes += 64; data += 64; } if (end > data) { // Fill the buffer with what remains. memcpy(buf + bufsize, data, end - data); bytes += end - data; } return *this; } void CRIPEMD160::Finalize(unsigned char hash[OUTPUT_SIZE]) { static const unsigned char pad[64] = {0x80}; unsigned char sizedesc[8]; WriteLE64(sizedesc, bytes << 3); Write(pad, 1 + ((119 - (bytes % 64)) % 64)); Write(sizedesc, 8); WriteLE32(hash, s[0]); WriteLE32(hash + 4, s[1]); WriteLE32(hash + 8, s[2]); WriteLE32(hash + 12, s[3]); WriteLE32(hash + 16, s[4]); } CRIPEMD160& CRIPEMD160::Reset() { bytes = 0; ripemd160::Initialize(s); return *this; }
0
bitcoin/src
bitcoin/src/crypto/sha256_x86_shani.cpp
// Copyright (c) 2018-2022 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. // // Based on https://github.com/noloader/SHA-Intrinsics/blob/master/sha256-x86.c, // Written and placed in public domain by Jeffrey Walton. // Based on code from Intel, and by Sean Gulley for the miTLS project. #ifdef ENABLE_X86_SHANI #include <stdint.h> #include <immintrin.h> #include <attributes.h> namespace { alignas(__m128i) const uint8_t MASK[16] = {0x03, 0x02, 0x01, 0x00, 0x07, 0x06, 0x05, 0x04, 0x0b, 0x0a, 0x09, 0x08, 0x0f, 0x0e, 0x0d, 0x0c}; alignas(__m128i) const uint8_t INIT0[16] = {0x8c, 0x68, 0x05, 0x9b, 0x7f, 0x52, 0x0e, 0x51, 0x85, 0xae, 0x67, 0xbb, 0x67, 0xe6, 0x09, 0x6a}; alignas(__m128i) const uint8_t INIT1[16] = {0x19, 0xcd, 0xe0, 0x5b, 0xab, 0xd9, 0x83, 0x1f, 0x3a, 0xf5, 0x4f, 0xa5, 0x72, 0xf3, 0x6e, 0x3c}; void ALWAYS_INLINE QuadRound(__m128i& state0, __m128i& state1, uint64_t k1, uint64_t k0) { const __m128i msg = _mm_set_epi64x(k1, k0); state1 = _mm_sha256rnds2_epu32(state1, state0, msg); state0 = _mm_sha256rnds2_epu32(state0, state1, _mm_shuffle_epi32(msg, 0x0e)); } void ALWAYS_INLINE QuadRound(__m128i& state0, __m128i& state1, __m128i m, uint64_t k1, uint64_t k0) { const __m128i msg = _mm_add_epi32(m, _mm_set_epi64x(k1, k0)); state1 = _mm_sha256rnds2_epu32(state1, state0, msg); state0 = _mm_sha256rnds2_epu32(state0, state1, _mm_shuffle_epi32(msg, 0x0e)); } void ALWAYS_INLINE ShiftMessageA(__m128i& m0, __m128i m1) { m0 = _mm_sha256msg1_epu32(m0, m1); } void ALWAYS_INLINE ShiftMessageC(__m128i& m0, __m128i m1, __m128i& m2) { m2 = _mm_sha256msg2_epu32(_mm_add_epi32(m2, _mm_alignr_epi8(m1, m0, 4)), m1); } void ALWAYS_INLINE ShiftMessageB(__m128i& m0, __m128i m1, __m128i& m2) { ShiftMessageC(m0, m1, m2); ShiftMessageA(m0, m1); } void ALWAYS_INLINE Shuffle(__m128i& s0, __m128i& s1) { const __m128i t1 = _mm_shuffle_epi32(s0, 0xB1); const __m128i t2 = _mm_shuffle_epi32(s1, 0x1B); s0 = _mm_alignr_epi8(t1, t2, 0x08); s1 = _mm_blend_epi16(t2, t1, 0xF0); } void ALWAYS_INLINE Unshuffle(__m128i& s0, __m128i& s1) { const __m128i t1 = _mm_shuffle_epi32(s0, 0x1B); const __m128i t2 = _mm_shuffle_epi32(s1, 0xB1); s0 = _mm_blend_epi16(t1, t2, 0xF0); s1 = _mm_alignr_epi8(t2, t1, 0x08); } __m128i ALWAYS_INLINE Load(const unsigned char* in) { return _mm_shuffle_epi8(_mm_loadu_si128((const __m128i*)in), _mm_load_si128((const __m128i*)MASK)); } void ALWAYS_INLINE Save(unsigned char* out, __m128i s) { _mm_storeu_si128((__m128i*)out, _mm_shuffle_epi8(s, _mm_load_si128((const __m128i*)MASK))); } } namespace sha256_x86_shani { void Transform(uint32_t* s, const unsigned char* chunk, size_t blocks) { __m128i m0, m1, m2, m3, s0, s1, so0, so1; /* Load state */ s0 = _mm_loadu_si128((const __m128i*)s); s1 = _mm_loadu_si128((const __m128i*)(s + 4)); Shuffle(s0, s1); while (blocks--) { /* Remember old state */ so0 = s0; so1 = s1; /* Load data and transform */ m0 = Load(chunk); QuadRound(s0, s1, m0, 0xe9b5dba5b5c0fbcfull, 0x71374491428a2f98ull); m1 = Load(chunk + 16); QuadRound(s0, s1, m1, 0xab1c5ed5923f82a4ull, 0x59f111f13956c25bull); ShiftMessageA(m0, m1); m2 = Load(chunk + 32); QuadRound(s0, s1, m2, 0x550c7dc3243185beull, 0x12835b01d807aa98ull); ShiftMessageA(m1, m2); m3 = Load(chunk + 48); QuadRound(s0, s1, m3, 0xc19bf1749bdc06a7ull, 0x80deb1fe72be5d74ull); ShiftMessageB(m2, m3, m0); QuadRound(s0, s1, m0, 0x240ca1cc0fc19dc6ull, 0xefbe4786E49b69c1ull); ShiftMessageB(m3, m0, m1); QuadRound(s0, s1, m1, 0x76f988da5cb0a9dcull, 0x4a7484aa2de92c6full); ShiftMessageB(m0, m1, m2); QuadRound(s0, s1, m2, 0xbf597fc7b00327c8ull, 0xa831c66d983e5152ull); ShiftMessageB(m1, m2, m3); QuadRound(s0, s1, m3, 0x1429296706ca6351ull, 0xd5a79147c6e00bf3ull); ShiftMessageB(m2, m3, m0); QuadRound(s0, s1, m0, 0x53380d134d2c6dfcull, 0x2e1b213827b70a85ull); ShiftMessageB(m3, m0, m1); QuadRound(s0, s1, m1, 0x92722c8581c2c92eull, 0x766a0abb650a7354ull); ShiftMessageB(m0, m1, m2); QuadRound(s0, s1, m2, 0xc76c51A3c24b8b70ull, 0xa81a664ba2bfe8a1ull); ShiftMessageB(m1, m2, m3); QuadRound(s0, s1, m3, 0x106aa070f40e3585ull, 0xd6990624d192e819ull); ShiftMessageB(m2, m3, m0); QuadRound(s0, s1, m0, 0x34b0bcb52748774cull, 0x1e376c0819a4c116ull); ShiftMessageB(m3, m0, m1); QuadRound(s0, s1, m1, 0x682e6ff35b9cca4full, 0x4ed8aa4a391c0cb3ull); ShiftMessageC(m0, m1, m2); QuadRound(s0, s1, m2, 0x8cc7020884c87814ull, 0x78a5636f748f82eeull); ShiftMessageC(m1, m2, m3); QuadRound(s0, s1, m3, 0xc67178f2bef9A3f7ull, 0xa4506ceb90befffaull); /* Combine with old state */ s0 = _mm_add_epi32(s0, so0); s1 = _mm_add_epi32(s1, so1); /* Advance */ chunk += 64; } Unshuffle(s0, s1); _mm_storeu_si128((__m128i*)s, s0); _mm_storeu_si128((__m128i*)(s + 4), s1); } } namespace sha256d64_x86_shani { void Transform_2way(unsigned char* out, const unsigned char* in) { __m128i am0, am1, am2, am3, as0, as1, aso0, aso1; __m128i bm0, bm1, bm2, bm3, bs0, bs1, bso0, bso1; /* Transform 1 */ bs0 = as0 = _mm_load_si128((const __m128i*)INIT0); bs1 = as1 = _mm_load_si128((const __m128i*)INIT1); am0 = Load(in); bm0 = Load(in + 64); QuadRound(as0, as1, am0, 0xe9b5dba5b5c0fbcfull, 0x71374491428a2f98ull); QuadRound(bs0, bs1, bm0, 0xe9b5dba5b5c0fbcfull, 0x71374491428a2f98ull); am1 = Load(in + 16); bm1 = Load(in + 80); QuadRound(as0, as1, am1, 0xab1c5ed5923f82a4ull, 0x59f111f13956c25bull); QuadRound(bs0, bs1, bm1, 0xab1c5ed5923f82a4ull, 0x59f111f13956c25bull); ShiftMessageA(am0, am1); ShiftMessageA(bm0, bm1); am2 = Load(in + 32); bm2 = Load(in + 96); QuadRound(as0, as1, am2, 0x550c7dc3243185beull, 0x12835b01d807aa98ull); QuadRound(bs0, bs1, bm2, 0x550c7dc3243185beull, 0x12835b01d807aa98ull); ShiftMessageA(am1, am2); ShiftMessageA(bm1, bm2); am3 = Load(in + 48); bm3 = Load(in + 112); QuadRound(as0, as1, am3, 0xc19bf1749bdc06a7ull, 0x80deb1fe72be5d74ull); QuadRound(bs0, bs1, bm3, 0xc19bf1749bdc06a7ull, 0x80deb1fe72be5d74ull); ShiftMessageB(am2, am3, am0); ShiftMessageB(bm2, bm3, bm0); QuadRound(as0, as1, am0, 0x240ca1cc0fc19dc6ull, 0xefbe4786E49b69c1ull); QuadRound(bs0, bs1, bm0, 0x240ca1cc0fc19dc6ull, 0xefbe4786E49b69c1ull); ShiftMessageB(am3, am0, am1); ShiftMessageB(bm3, bm0, bm1); QuadRound(as0, as1, am1, 0x76f988da5cb0a9dcull, 0x4a7484aa2de92c6full); QuadRound(bs0, bs1, bm1, 0x76f988da5cb0a9dcull, 0x4a7484aa2de92c6full); ShiftMessageB(am0, am1, am2); ShiftMessageB(bm0, bm1, bm2); QuadRound(as0, as1, am2, 0xbf597fc7b00327c8ull, 0xa831c66d983e5152ull); QuadRound(bs0, bs1, bm2, 0xbf597fc7b00327c8ull, 0xa831c66d983e5152ull); ShiftMessageB(am1, am2, am3); ShiftMessageB(bm1, bm2, bm3); QuadRound(as0, as1, am3, 0x1429296706ca6351ull, 0xd5a79147c6e00bf3ull); QuadRound(bs0, bs1, bm3, 0x1429296706ca6351ull, 0xd5a79147c6e00bf3ull); ShiftMessageB(am2, am3, am0); ShiftMessageB(bm2, bm3, bm0); QuadRound(as0, as1, am0, 0x53380d134d2c6dfcull, 0x2e1b213827b70a85ull); QuadRound(bs0, bs1, bm0, 0x53380d134d2c6dfcull, 0x2e1b213827b70a85ull); ShiftMessageB(am3, am0, am1); ShiftMessageB(bm3, bm0, bm1); QuadRound(as0, as1, am1, 0x92722c8581c2c92eull, 0x766a0abb650a7354ull); QuadRound(bs0, bs1, bm1, 0x92722c8581c2c92eull, 0x766a0abb650a7354ull); ShiftMessageB(am0, am1, am2); ShiftMessageB(bm0, bm1, bm2); QuadRound(as0, as1, am2, 0xc76c51A3c24b8b70ull, 0xa81a664ba2bfe8a1ull); QuadRound(bs0, bs1, bm2, 0xc76c51A3c24b8b70ull, 0xa81a664ba2bfe8a1ull); ShiftMessageB(am1, am2, am3); ShiftMessageB(bm1, bm2, bm3); QuadRound(as0, as1, am3, 0x106aa070f40e3585ull, 0xd6990624d192e819ull); QuadRound(bs0, bs1, bm3, 0x106aa070f40e3585ull, 0xd6990624d192e819ull); ShiftMessageB(am2, am3, am0); ShiftMessageB(bm2, bm3, bm0); QuadRound(as0, as1, am0, 0x34b0bcb52748774cull, 0x1e376c0819a4c116ull); QuadRound(bs0, bs1, bm0, 0x34b0bcb52748774cull, 0x1e376c0819a4c116ull); ShiftMessageB(am3, am0, am1); ShiftMessageB(bm3, bm0, bm1); QuadRound(as0, as1, am1, 0x682e6ff35b9cca4full, 0x4ed8aa4a391c0cb3ull); QuadRound(bs0, bs1, bm1, 0x682e6ff35b9cca4full, 0x4ed8aa4a391c0cb3ull); ShiftMessageC(am0, am1, am2); ShiftMessageC(bm0, bm1, bm2); QuadRound(as0, as1, am2, 0x8cc7020884c87814ull, 0x78a5636f748f82eeull); QuadRound(bs0, bs1, bm2, 0x8cc7020884c87814ull, 0x78a5636f748f82eeull); ShiftMessageC(am1, am2, am3); ShiftMessageC(bm1, bm2, bm3); QuadRound(as0, as1, am3, 0xc67178f2bef9A3f7ull, 0xa4506ceb90befffaull); QuadRound(bs0, bs1, bm3, 0xc67178f2bef9A3f7ull, 0xa4506ceb90befffaull); as0 = _mm_add_epi32(as0, _mm_load_si128((const __m128i*)INIT0)); bs0 = _mm_add_epi32(bs0, _mm_load_si128((const __m128i*)INIT0)); as1 = _mm_add_epi32(as1, _mm_load_si128((const __m128i*)INIT1)); bs1 = _mm_add_epi32(bs1, _mm_load_si128((const __m128i*)INIT1)); /* Transform 2 */ aso0 = as0; bso0 = bs0; aso1 = as1; bso1 = bs1; QuadRound(as0, as1, 0xe9b5dba5b5c0fbcfull, 0x71374491c28a2f98ull); QuadRound(bs0, bs1, 0xe9b5dba5b5c0fbcfull, 0x71374491c28a2f98ull); QuadRound(as0, as1, 0xab1c5ed5923f82a4ull, 0x59f111f13956c25bull); QuadRound(bs0, bs1, 0xab1c5ed5923f82a4ull, 0x59f111f13956c25bull); QuadRound(as0, as1, 0x550c7dc3243185beull, 0x12835b01d807aa98ull); QuadRound(bs0, bs1, 0x550c7dc3243185beull, 0x12835b01d807aa98ull); QuadRound(as0, as1, 0xc19bf3749bdc06a7ull, 0x80deb1fe72be5d74ull); QuadRound(bs0, bs1, 0xc19bf3749bdc06a7ull, 0x80deb1fe72be5d74ull); QuadRound(as0, as1, 0x240cf2540fe1edc6ull, 0xf0fe4786649b69c1ull); QuadRound(bs0, bs1, 0x240cf2540fe1edc6ull, 0xf0fe4786649b69c1ull); QuadRound(as0, as1, 0x16f988fa61b9411eull, 0x6cc984be4fe9346full); QuadRound(bs0, bs1, 0x16f988fa61b9411eull, 0x6cc984be4fe9346full); QuadRound(as0, as1, 0xb9d99ec7b019fc65ull, 0xa88e5a6df2c65152ull); QuadRound(bs0, bs1, 0xb9d99ec7b019fc65ull, 0xa88e5a6df2c65152ull); QuadRound(as0, as1, 0xc7353eb0fdb1232bull, 0xe70eeaa09a1231c3ull); QuadRound(bs0, bs1, 0xc7353eb0fdb1232bull, 0xe70eeaa09a1231c3ull); QuadRound(as0, as1, 0xdc1eeefd5a0f118full, 0xcb976d5f3069bad5ull); QuadRound(bs0, bs1, 0xdc1eeefd5a0f118full, 0xcb976d5f3069bad5ull); QuadRound(as0, as1, 0xe15d5b1658f4ca9dull, 0xde0b7a040a35b689ull); QuadRound(bs0, bs1, 0xe15d5b1658f4ca9dull, 0xde0b7a040a35b689ull); QuadRound(as0, as1, 0x6fab9537a507ea32ull, 0x37088980007f3e86ull); QuadRound(bs0, bs1, 0x6fab9537a507ea32ull, 0x37088980007f3e86ull); QuadRound(as0, as1, 0xc0bbbe37cdaa3b6dull, 0x0d8cd6f117406110ull); QuadRound(bs0, bs1, 0xc0bbbe37cdaa3b6dull, 0x0d8cd6f117406110ull); QuadRound(as0, as1, 0x6fd15ca70b02e931ull, 0xdb48a36383613bdaull); QuadRound(bs0, bs1, 0x6fd15ca70b02e931ull, 0xdb48a36383613bdaull); QuadRound(as0, as1, 0x6d4378906ed41a95ull, 0x31338431521afacaull); QuadRound(bs0, bs1, 0x6d4378906ed41a95ull, 0x31338431521afacaull); QuadRound(as0, as1, 0x532fb63cb5c9a0e6ull, 0x9eccabbdc39c91f2ull); QuadRound(bs0, bs1, 0x532fb63cb5c9a0e6ull, 0x9eccabbdc39c91f2ull); QuadRound(as0, as1, 0x4c191d76a4954b68ull, 0x07237ea3d2c741c6ull); QuadRound(bs0, bs1, 0x4c191d76a4954b68ull, 0x07237ea3d2c741c6ull); as0 = _mm_add_epi32(as0, aso0); bs0 = _mm_add_epi32(bs0, bso0); as1 = _mm_add_epi32(as1, aso1); bs1 = _mm_add_epi32(bs1, bso1); /* Extract hash */ Unshuffle(as0, as1); Unshuffle(bs0, bs1); am0 = as0; bm0 = bs0; am1 = as1; bm1 = bs1; /* Transform 3 */ bs0 = as0 = _mm_load_si128((const __m128i*)INIT0); bs1 = as1 = _mm_load_si128((const __m128i*)INIT1); QuadRound(as0, as1, am0, 0xe9b5dba5B5c0fbcfull, 0x71374491428a2f98ull); QuadRound(bs0, bs1, bm0, 0xe9b5dba5B5c0fbcfull, 0x71374491428a2f98ull); QuadRound(as0, as1, am1, 0xab1c5ed5923f82a4ull, 0x59f111f13956c25bull); QuadRound(bs0, bs1, bm1, 0xab1c5ed5923f82a4ull, 0x59f111f13956c25bull); ShiftMessageA(am0, am1); ShiftMessageA(bm0, bm1); bm2 = am2 = _mm_set_epi64x(0x0ull, 0x80000000ull); QuadRound(as0, as1, 0x550c7dc3243185beull, 0x12835b015807aa98ull); QuadRound(bs0, bs1, 0x550c7dc3243185beull, 0x12835b015807aa98ull); ShiftMessageA(am1, am2); ShiftMessageA(bm1, bm2); bm3 = am3 = _mm_set_epi64x(0x10000000000ull, 0x0ull); QuadRound(as0, as1, 0xc19bf2749bdc06a7ull, 0x80deb1fe72be5d74ull); QuadRound(bs0, bs1, 0xc19bf2749bdc06a7ull, 0x80deb1fe72be5d74ull); ShiftMessageB(am2, am3, am0); ShiftMessageB(bm2, bm3, bm0); QuadRound(as0, as1, am0, 0x240ca1cc0fc19dc6ull, 0xefbe4786e49b69c1ull); QuadRound(bs0, bs1, bm0, 0x240ca1cc0fc19dc6ull, 0xefbe4786e49b69c1ull); ShiftMessageB(am3, am0, am1); ShiftMessageB(bm3, bm0, bm1); QuadRound(as0, as1, am1, 0x76f988da5cb0a9dcull, 0x4a7484aa2de92c6full); QuadRound(bs0, bs1, bm1, 0x76f988da5cb0a9dcull, 0x4a7484aa2de92c6full); ShiftMessageB(am0, am1, am2); ShiftMessageB(bm0, bm1, bm2); QuadRound(as0, as1, am2, 0xbf597fc7b00327c8ull, 0xa831c66d983e5152ull); QuadRound(bs0, bs1, bm2, 0xbf597fc7b00327c8ull, 0xa831c66d983e5152ull); ShiftMessageB(am1, am2, am3); ShiftMessageB(bm1, bm2, bm3); QuadRound(as0, as1, am3, 0x1429296706ca6351ull, 0xd5a79147c6e00bf3ull); QuadRound(bs0, bs1, bm3, 0x1429296706ca6351ull, 0xd5a79147c6e00bf3ull); ShiftMessageB(am2, am3, am0); ShiftMessageB(bm2, bm3, bm0); QuadRound(as0, as1, am0, 0x53380d134d2c6dfcull, 0x2e1b213827b70a85ull); QuadRound(bs0, bs1, bm0, 0x53380d134d2c6dfcull, 0x2e1b213827b70a85ull); ShiftMessageB(am3, am0, am1); ShiftMessageB(bm3, bm0, bm1); QuadRound(as0, as1, am1, 0x92722c8581c2c92eull, 0x766a0abb650a7354ull); QuadRound(bs0, bs1, bm1, 0x92722c8581c2c92eull, 0x766a0abb650a7354ull); ShiftMessageB(am0, am1, am2); ShiftMessageB(bm0, bm1, bm2); QuadRound(as0, as1, am2, 0xc76c51a3c24b8b70ull, 0xa81a664ba2bfe8A1ull); QuadRound(bs0, bs1, bm2, 0xc76c51a3c24b8b70ull, 0xa81a664ba2bfe8A1ull); ShiftMessageB(am1, am2, am3); ShiftMessageB(bm1, bm2, bm3); QuadRound(as0, as1, am3, 0x106aa070f40e3585ull, 0xd6990624d192e819ull); QuadRound(bs0, bs1, bm3, 0x106aa070f40e3585ull, 0xd6990624d192e819ull); ShiftMessageB(am2, am3, am0); ShiftMessageB(bm2, bm3, bm0); QuadRound(as0, as1, am0, 0x34b0bcb52748774cull, 0x1e376c0819a4c116ull); QuadRound(bs0, bs1, bm0, 0x34b0bcb52748774cull, 0x1e376c0819a4c116ull); ShiftMessageB(am3, am0, am1); ShiftMessageB(bm3, bm0, bm1); QuadRound(as0, as1, am1, 0x682e6ff35b9cca4full, 0x4ed8aa4a391c0cb3ull); QuadRound(bs0, bs1, bm1, 0x682e6ff35b9cca4full, 0x4ed8aa4a391c0cb3ull); ShiftMessageC(am0, am1, am2); ShiftMessageC(bm0, bm1, bm2); QuadRound(as0, as1, am2, 0x8cc7020884c87814ull, 0x78a5636f748f82eeull); QuadRound(bs0, bs1, bm2, 0x8cc7020884c87814ull, 0x78a5636f748f82eeull); ShiftMessageC(am1, am2, am3); ShiftMessageC(bm1, bm2, bm3); QuadRound(as0, as1, am3, 0xc67178f2bef9a3f7ull, 0xa4506ceb90befffaull); QuadRound(bs0, bs1, bm3, 0xc67178f2bef9a3f7ull, 0xa4506ceb90befffaull); as0 = _mm_add_epi32(as0, _mm_load_si128((const __m128i*)INIT0)); bs0 = _mm_add_epi32(bs0, _mm_load_si128((const __m128i*)INIT0)); as1 = _mm_add_epi32(as1, _mm_load_si128((const __m128i*)INIT1)); bs1 = _mm_add_epi32(bs1, _mm_load_si128((const __m128i*)INIT1)); /* Extract hash into out */ Unshuffle(as0, as1); Unshuffle(bs0, bs1); Save(out, as0); Save(out + 16, as1); Save(out + 32, bs0); Save(out + 48, bs1); } } #endif
0
bitcoin/src
bitcoin/src/crypto/aes.cpp
// Copyright (c) 2016-2019 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include <crypto/aes.h> #include <string.h> extern "C" { #include <crypto/ctaes/ctaes.c> } AES256Encrypt::AES256Encrypt(const unsigned char key[32]) { AES256_init(&ctx, key); } AES256Encrypt::~AES256Encrypt() { memset(&ctx, 0, sizeof(ctx)); } void AES256Encrypt::Encrypt(unsigned char ciphertext[16], const unsigned char plaintext[16]) const { AES256_encrypt(&ctx, 1, ciphertext, plaintext); } AES256Decrypt::AES256Decrypt(const unsigned char key[32]) { AES256_init(&ctx, key); } AES256Decrypt::~AES256Decrypt() { memset(&ctx, 0, sizeof(ctx)); } void AES256Decrypt::Decrypt(unsigned char plaintext[16], const unsigned char ciphertext[16]) const { AES256_decrypt(&ctx, 1, plaintext, ciphertext); } template <typename T> static int CBCEncrypt(const T& enc, const unsigned char iv[AES_BLOCKSIZE], const unsigned char* data, int size, bool pad, unsigned char* out) { int written = 0; int padsize = size % AES_BLOCKSIZE; unsigned char mixed[AES_BLOCKSIZE]; if (!data || !size || !out) return 0; if (!pad && padsize != 0) return 0; memcpy(mixed, iv, AES_BLOCKSIZE); // Write all but the last block while (written + AES_BLOCKSIZE <= size) { for (int i = 0; i != AES_BLOCKSIZE; i++) mixed[i] ^= *data++; enc.Encrypt(out + written, mixed); memcpy(mixed, out + written, AES_BLOCKSIZE); written += AES_BLOCKSIZE; } if (pad) { // For all that remains, pad each byte with the value of the remaining // space. If there is none, pad by a full block. for (int i = 0; i != padsize; i++) mixed[i] ^= *data++; for (int i = padsize; i != AES_BLOCKSIZE; i++) mixed[i] ^= AES_BLOCKSIZE - padsize; enc.Encrypt(out + written, mixed); written += AES_BLOCKSIZE; } return written; } template <typename T> static int CBCDecrypt(const T& dec, const unsigned char iv[AES_BLOCKSIZE], const unsigned char* data, int size, bool pad, unsigned char* out) { int written = 0; bool fail = false; const unsigned char* prev = iv; if (!data || !size || !out) return 0; if (size % AES_BLOCKSIZE != 0) return 0; // Decrypt all data. Padding will be checked in the output. while (written != size) { dec.Decrypt(out, data + written); for (int i = 0; i != AES_BLOCKSIZE; i++) *out++ ^= prev[i]; prev = data + written; written += AES_BLOCKSIZE; } // When decrypting padding, attempt to run in constant-time if (pad) { // If used, padding size is the value of the last decrypted byte. For // it to be valid, It must be between 1 and AES_BLOCKSIZE. unsigned char padsize = *--out; fail = !padsize | (padsize > AES_BLOCKSIZE); // If not well-formed, treat it as though there's no padding. padsize *= !fail; // All padding must equal the last byte otherwise it's not well-formed for (int i = AES_BLOCKSIZE; i != 0; i--) fail |= ((i > AES_BLOCKSIZE - padsize) & (*out-- != padsize)); written -= padsize; } return written * !fail; } AES256CBCEncrypt::AES256CBCEncrypt(const unsigned char key[AES256_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn) : enc(key), pad(padIn) { memcpy(iv, ivIn, AES_BLOCKSIZE); } int AES256CBCEncrypt::Encrypt(const unsigned char* data, int size, unsigned char* out) const { return CBCEncrypt(enc, iv, data, size, pad, out); } AES256CBCEncrypt::~AES256CBCEncrypt() { memset(iv, 0, sizeof(iv)); } AES256CBCDecrypt::AES256CBCDecrypt(const unsigned char key[AES256_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn) : dec(key), pad(padIn) { memcpy(iv, ivIn, AES_BLOCKSIZE); } int AES256CBCDecrypt::Decrypt(const unsigned char* data, int size, unsigned char* out) const { return CBCDecrypt(dec, iv, data, size, pad, out); } AES256CBCDecrypt::~AES256CBCDecrypt() { memset(iv, 0, sizeof(iv)); }
0
bitcoin/src
bitcoin/src/crypto/muhash.cpp
// Copyright (c) 2017-2022 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include <crypto/muhash.h> #include <crypto/chacha20.h> #include <crypto/common.h> #include <hash.h> #include <cassert> #include <cstdio> #include <limits> namespace { using limb_t = Num3072::limb_t; using double_limb_t = Num3072::double_limb_t; constexpr int LIMB_SIZE = Num3072::LIMB_SIZE; /** 2^3072 - 1103717, the largest 3072-bit safe prime number, is used as the modulus. */ constexpr limb_t MAX_PRIME_DIFF = 1103717; /** Extract the lowest limb of [c0,c1,c2] into n, and left shift the number by 1 limb. */ inline void extract3(limb_t& c0, limb_t& c1, limb_t& c2, limb_t& n) { n = c0; c0 = c1; c1 = c2; c2 = 0; } /** [c0,c1] = a * b */ inline void mul(limb_t& c0, limb_t& c1, const limb_t& a, const limb_t& b) { double_limb_t t = (double_limb_t)a * b; c1 = t >> LIMB_SIZE; c0 = t; } /* [c0,c1,c2] += n * [d0,d1,d2]. c2 is 0 initially */ inline void mulnadd3(limb_t& c0, limb_t& c1, limb_t& c2, limb_t& d0, limb_t& d1, limb_t& d2, const limb_t& n) { double_limb_t t = (double_limb_t)d0 * n + c0; c0 = t; t >>= LIMB_SIZE; t += (double_limb_t)d1 * n + c1; c1 = t; t >>= LIMB_SIZE; c2 = t + d2 * n; } /* [c0,c1] *= n */ inline void muln2(limb_t& c0, limb_t& c1, const limb_t& n) { double_limb_t t = (double_limb_t)c0 * n; c0 = t; t >>= LIMB_SIZE; t += (double_limb_t)c1 * n; c1 = t; } /** [c0,c1,c2] += a * b */ inline void muladd3(limb_t& c0, limb_t& c1, limb_t& c2, const limb_t& a, const limb_t& b) { double_limb_t t = (double_limb_t)a * b; limb_t th = t >> LIMB_SIZE; limb_t tl = t; c0 += tl; th += (c0 < tl) ? 1 : 0; c1 += th; c2 += (c1 < th) ? 1 : 0; } /** [c0,c1,c2] += 2 * a * b */ inline void muldbladd3(limb_t& c0, limb_t& c1, limb_t& c2, const limb_t& a, const limb_t& b) { double_limb_t t = (double_limb_t)a * b; limb_t th = t >> LIMB_SIZE; limb_t tl = t; c0 += tl; limb_t tt = th + ((c0 < tl) ? 1 : 0); c1 += tt; c2 += (c1 < tt) ? 1 : 0; c0 += tl; th += (c0 < tl) ? 1 : 0; c1 += th; c2 += (c1 < th) ? 1 : 0; } /** * Add limb a to [c0,c1]: [c0,c1] += a. Then extract the lowest * limb of [c0,c1] into n, and left shift the number by 1 limb. * */ inline void addnextract2(limb_t& c0, limb_t& c1, const limb_t& a, limb_t& n) { limb_t c2 = 0; // add c0 += a; if (c0 < a) { c1 += 1; // Handle case when c1 has overflown if (c1 == 0) c2 = 1; } // extract n = c0; c0 = c1; c1 = c2; } /** in_out = in_out^(2^sq) * mul */ inline void square_n_mul(Num3072& in_out, const int sq, const Num3072& mul) { for (int j = 0; j < sq; ++j) in_out.Square(); in_out.Multiply(mul); } } // namespace /** Indicates whether d is larger than the modulus. */ bool Num3072::IsOverflow() const { if (this->limbs[0] <= std::numeric_limits<limb_t>::max() - MAX_PRIME_DIFF) return false; for (int i = 1; i < LIMBS; ++i) { if (this->limbs[i] != std::numeric_limits<limb_t>::max()) return false; } return true; } void Num3072::FullReduce() { limb_t c0 = MAX_PRIME_DIFF; limb_t c1 = 0; for (int i = 0; i < LIMBS; ++i) { addnextract2(c0, c1, this->limbs[i], this->limbs[i]); } } Num3072 Num3072::GetInverse() const { // For fast exponentiation a sliding window exponentiation with repunit // precomputation is utilized. See "Fast Point Decompression for Standard // Elliptic Curves" (Brumley, Järvinen, 2008). Num3072 p[12]; // p[i] = a^(2^(2^i)-1) Num3072 out; p[0] = *this; for (int i = 0; i < 11; ++i) { p[i + 1] = p[i]; for (int j = 0; j < (1 << i); ++j) p[i + 1].Square(); p[i + 1].Multiply(p[i]); } out = p[11]; square_n_mul(out, 512, p[9]); square_n_mul(out, 256, p[8]); square_n_mul(out, 128, p[7]); square_n_mul(out, 64, p[6]); square_n_mul(out, 32, p[5]); square_n_mul(out, 8, p[3]); square_n_mul(out, 2, p[1]); square_n_mul(out, 1, p[0]); square_n_mul(out, 5, p[2]); square_n_mul(out, 3, p[0]); square_n_mul(out, 2, p[0]); square_n_mul(out, 4, p[0]); square_n_mul(out, 4, p[1]); square_n_mul(out, 3, p[0]); return out; } void Num3072::Multiply(const Num3072& a) { limb_t c0 = 0, c1 = 0, c2 = 0; Num3072 tmp; /* Compute limbs 0..N-2 of this*a into tmp, including one reduction. */ for (int j = 0; j < LIMBS - 1; ++j) { limb_t d0 = 0, d1 = 0, d2 = 0; mul(d0, d1, this->limbs[1 + j], a.limbs[LIMBS + j - (1 + j)]); for (int i = 2 + j; i < LIMBS; ++i) muladd3(d0, d1, d2, this->limbs[i], a.limbs[LIMBS + j - i]); mulnadd3(c0, c1, c2, d0, d1, d2, MAX_PRIME_DIFF); for (int i = 0; i < j + 1; ++i) muladd3(c0, c1, c2, this->limbs[i], a.limbs[j - i]); extract3(c0, c1, c2, tmp.limbs[j]); } /* Compute limb N-1 of a*b into tmp. */ assert(c2 == 0); for (int i = 0; i < LIMBS; ++i) muladd3(c0, c1, c2, this->limbs[i], a.limbs[LIMBS - 1 - i]); extract3(c0, c1, c2, tmp.limbs[LIMBS - 1]); /* Perform a second reduction. */ muln2(c0, c1, MAX_PRIME_DIFF); for (int j = 0; j < LIMBS; ++j) { addnextract2(c0, c1, tmp.limbs[j], this->limbs[j]); } assert(c1 == 0); assert(c0 == 0 || c0 == 1); /* Perform up to two more reductions if the internal state has already * overflown the MAX of Num3072 or if it is larger than the modulus or * if both are the case. * */ if (this->IsOverflow()) this->FullReduce(); if (c0) this->FullReduce(); } void Num3072::Square() { limb_t c0 = 0, c1 = 0, c2 = 0; Num3072 tmp; /* Compute limbs 0..N-2 of this*this into tmp, including one reduction. */ for (int j = 0; j < LIMBS - 1; ++j) { limb_t d0 = 0, d1 = 0, d2 = 0; for (int i = 0; i < (LIMBS - 1 - j) / 2; ++i) muldbladd3(d0, d1, d2, this->limbs[i + j + 1], this->limbs[LIMBS - 1 - i]); if ((j + 1) & 1) muladd3(d0, d1, d2, this->limbs[(LIMBS - 1 - j) / 2 + j + 1], this->limbs[LIMBS - 1 - (LIMBS - 1 - j) / 2]); mulnadd3(c0, c1, c2, d0, d1, d2, MAX_PRIME_DIFF); for (int i = 0; i < (j + 1) / 2; ++i) muldbladd3(c0, c1, c2, this->limbs[i], this->limbs[j - i]); if ((j + 1) & 1) muladd3(c0, c1, c2, this->limbs[(j + 1) / 2], this->limbs[j - (j + 1) / 2]); extract3(c0, c1, c2, tmp.limbs[j]); } assert(c2 == 0); for (int i = 0; i < LIMBS / 2; ++i) muldbladd3(c0, c1, c2, this->limbs[i], this->limbs[LIMBS - 1 - i]); extract3(c0, c1, c2, tmp.limbs[LIMBS - 1]); /* Perform a second reduction. */ muln2(c0, c1, MAX_PRIME_DIFF); for (int j = 0; j < LIMBS; ++j) { addnextract2(c0, c1, tmp.limbs[j], this->limbs[j]); } assert(c1 == 0); assert(c0 == 0 || c0 == 1); /* Perform up to two more reductions if the internal state has already * overflown the MAX of Num3072 or if it is larger than the modulus or * if both are the case. * */ if (this->IsOverflow()) this->FullReduce(); if (c0) this->FullReduce(); } void Num3072::SetToOne() { this->limbs[0] = 1; for (int i = 1; i < LIMBS; ++i) this->limbs[i] = 0; } void Num3072::Divide(const Num3072& a) { if (this->IsOverflow()) this->FullReduce(); Num3072 inv{}; if (a.IsOverflow()) { Num3072 b = a; b.FullReduce(); inv = b.GetInverse(); } else { inv = a.GetInverse(); } this->Multiply(inv); if (this->IsOverflow()) this->FullReduce(); } Num3072::Num3072(const unsigned char (&data)[BYTE_SIZE]) { for (int i = 0; i < LIMBS; ++i) { if (sizeof(limb_t) == 4) { this->limbs[i] = ReadLE32(data + 4 * i); } else if (sizeof(limb_t) == 8) { this->limbs[i] = ReadLE64(data + 8 * i); } } } void Num3072::ToBytes(unsigned char (&out)[BYTE_SIZE]) { for (int i = 0; i < LIMBS; ++i) { if (sizeof(limb_t) == 4) { WriteLE32(out + i * 4, this->limbs[i]); } else if (sizeof(limb_t) == 8) { WriteLE64(out + i * 8, this->limbs[i]); } } } Num3072 MuHash3072::ToNum3072(Span<const unsigned char> in) { unsigned char tmp[Num3072::BYTE_SIZE]; uint256 hashed_in{(HashWriter{} << in).GetSHA256()}; static_assert(sizeof(tmp) % ChaCha20Aligned::BLOCKLEN == 0); ChaCha20Aligned{MakeByteSpan(hashed_in)}.Keystream(MakeWritableByteSpan(tmp)); Num3072 out{tmp}; return out; } MuHash3072::MuHash3072(Span<const unsigned char> in) noexcept { m_numerator = ToNum3072(in); } void MuHash3072::Finalize(uint256& out) noexcept { m_numerator.Divide(m_denominator); m_denominator.SetToOne(); // Needed to keep the MuHash object valid unsigned char data[Num3072::BYTE_SIZE]; m_numerator.ToBytes(data); out = (HashWriter{} << data).GetSHA256(); } MuHash3072& MuHash3072::operator*=(const MuHash3072& mul) noexcept { m_numerator.Multiply(mul.m_numerator); m_denominator.Multiply(mul.m_denominator); return *this; } MuHash3072& MuHash3072::operator/=(const MuHash3072& div) noexcept { m_numerator.Multiply(div.m_denominator); m_denominator.Multiply(div.m_numerator); return *this; } MuHash3072& MuHash3072::Insert(Span<const unsigned char> in) noexcept { m_numerator.Multiply(ToNum3072(in)); return *this; } MuHash3072& MuHash3072::Remove(Span<const unsigned char> in) noexcept { m_denominator.Multiply(ToNum3072(in)); return *this; }
0
bitcoin/src/crypto
bitcoin/src/crypto/ctaes/ctaes.c
/********************************************************************* * Copyright (c) 2016 Pieter Wuille * * Distributed under the MIT software license, see the accompanying * * file COPYING or http://www.opensource.org/licenses/mit-license.php.* **********************************************************************/ /* Constant time, unoptimized, concise, plain C, AES implementation * Based On: * Emilia Kasper and Peter Schwabe, Faster and Timing-Attack Resistant AES-GCM * http://www.iacr.org/archive/ches2009/57470001/57470001.pdf * But using 8 16-bit integers representing a single AES state rather than 8 128-bit * integers representing 8 AES states. */ #include "ctaes.h" /* Slice variable slice_i contains the i'th bit of the 16 state variables in this order: * 0 1 2 3 * 4 5 6 7 * 8 9 10 11 * 12 13 14 15 */ /** Convert a byte to sliced form, storing it corresponding to given row and column in s */ static void LoadByte(AES_state* s, unsigned char byte, int r, int c) { int i; for (i = 0; i < 8; i++) { s->slice[i] |= (byte & 1) << (r * 4 + c); byte >>= 1; } } /** Load 16 bytes of data into 8 sliced integers */ static void LoadBytes(AES_state *s, const unsigned char* data16) { int c; for (c = 0; c < 4; c++) { int r; for (r = 0; r < 4; r++) { LoadByte(s, *(data16++), r, c); } } } /** Convert 8 sliced integers into 16 bytes of data */ static void SaveBytes(unsigned char* data16, const AES_state *s) { int c; for (c = 0; c < 4; c++) { int r; for (r = 0; r < 4; r++) { int b; uint8_t v = 0; for (b = 0; b < 8; b++) { v |= ((s->slice[b] >> (r * 4 + c)) & 1) << b; } *(data16++) = v; } } } /* S-box implementation based on the gate logic from: * Joan Boyar and Rene Peralta, A depth-16 circuit for the AES S-box. * https://eprint.iacr.org/2011/332.pdf */ static void SubBytes(AES_state *s, int inv) { /* Load the bit slices */ uint16_t U0 = s->slice[7], U1 = s->slice[6], U2 = s->slice[5], U3 = s->slice[4]; uint16_t U4 = s->slice[3], U5 = s->slice[2], U6 = s->slice[1], U7 = s->slice[0]; uint16_t T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16; uint16_t T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, D; uint16_t M1, M6, M11, M13, M15, M20, M21, M22, M23, M25, M37, M38, M39, M40; uint16_t M41, M42, M43, M44, M45, M46, M47, M48, M49, M50, M51, M52, M53, M54; uint16_t M55, M56, M57, M58, M59, M60, M61, M62, M63; if (inv) { uint16_t R5, R13, R17, R18, R19; /* Undo linear postprocessing */ T23 = U0 ^ U3; T22 = ~(U1 ^ U3); T2 = ~(U0 ^ U1); T1 = U3 ^ U4; T24 = ~(U4 ^ U7); R5 = U6 ^ U7; T8 = ~(U1 ^ T23); T19 = T22 ^ R5; T9 = ~(U7 ^ T1); T10 = T2 ^ T24; T13 = T2 ^ R5; T3 = T1 ^ R5; T25 = ~(U2 ^ T1); R13 = U1 ^ U6; T17 = ~(U2 ^ T19); T20 = T24 ^ R13; T4 = U4 ^ T8; R17 = ~(U2 ^ U5); R18 = ~(U5 ^ U6); R19 = ~(U2 ^ U4); D = U0 ^ R17; T6 = T22 ^ R17; T16 = R13 ^ R19; T27 = T1 ^ R18; T15 = T10 ^ T27; T14 = T10 ^ R18; T26 = T3 ^ T16; } else { /* Linear preprocessing. */ T1 = U0 ^ U3; T2 = U0 ^ U5; T3 = U0 ^ U6; T4 = U3 ^ U5; T5 = U4 ^ U6; T6 = T1 ^ T5; T7 = U1 ^ U2; T8 = U7 ^ T6; T9 = U7 ^ T7; T10 = T6 ^ T7; T11 = U1 ^ U5; T12 = U2 ^ U5; T13 = T3 ^ T4; T14 = T6 ^ T11; T15 = T5 ^ T11; T16 = T5 ^ T12; T17 = T9 ^ T16; T18 = U3 ^ U7; T19 = T7 ^ T18; T20 = T1 ^ T19; T21 = U6 ^ U7; T22 = T7 ^ T21; T23 = T2 ^ T22; T24 = T2 ^ T10; T25 = T20 ^ T17; T26 = T3 ^ T16; T27 = T1 ^ T12; D = U7; } /* Non-linear transformation (shared between the forward and backward case) */ M1 = T13 & T6; M6 = T3 & T16; M11 = T1 & T15; M13 = (T4 & T27) ^ M11; M15 = (T2 & T10) ^ M11; M20 = T14 ^ M1 ^ (T23 & T8) ^ M13; M21 = (T19 & D) ^ M1 ^ T24 ^ M15; M22 = T26 ^ M6 ^ (T22 & T9) ^ M13; M23 = (T20 & T17) ^ M6 ^ M15 ^ T25; M25 = M22 & M20; M37 = M21 ^ ((M20 ^ M21) & (M23 ^ M25)); M38 = M20 ^ M25 ^ (M21 | (M20 & M23)); M39 = M23 ^ ((M22 ^ M23) & (M21 ^ M25)); M40 = M22 ^ M25 ^ (M23 | (M21 & M22)); M41 = M38 ^ M40; M42 = M37 ^ M39; M43 = M37 ^ M38; M44 = M39 ^ M40; M45 = M42 ^ M41; M46 = M44 & T6; M47 = M40 & T8; M48 = M39 & D; M49 = M43 & T16; M50 = M38 & T9; M51 = M37 & T17; M52 = M42 & T15; M53 = M45 & T27; M54 = M41 & T10; M55 = M44 & T13; M56 = M40 & T23; M57 = M39 & T19; M58 = M43 & T3; M59 = M38 & T22; M60 = M37 & T20; M61 = M42 & T1; M62 = M45 & T4; M63 = M41 & T2; if (inv){ /* Undo linear preprocessing */ uint16_t P0 = M52 ^ M61; uint16_t P1 = M58 ^ M59; uint16_t P2 = M54 ^ M62; uint16_t P3 = M47 ^ M50; uint16_t P4 = M48 ^ M56; uint16_t P5 = M46 ^ M51; uint16_t P6 = M49 ^ M60; uint16_t P7 = P0 ^ P1; uint16_t P8 = M50 ^ M53; uint16_t P9 = M55 ^ M63; uint16_t P10 = M57 ^ P4; uint16_t P11 = P0 ^ P3; uint16_t P12 = M46 ^ M48; uint16_t P13 = M49 ^ M51; uint16_t P14 = M49 ^ M62; uint16_t P15 = M54 ^ M59; uint16_t P16 = M57 ^ M61; uint16_t P17 = M58 ^ P2; uint16_t P18 = M63 ^ P5; uint16_t P19 = P2 ^ P3; uint16_t P20 = P4 ^ P6; uint16_t P22 = P2 ^ P7; uint16_t P23 = P7 ^ P8; uint16_t P24 = P5 ^ P7; uint16_t P25 = P6 ^ P10; uint16_t P26 = P9 ^ P11; uint16_t P27 = P10 ^ P18; uint16_t P28 = P11 ^ P25; uint16_t P29 = P15 ^ P20; s->slice[7] = P13 ^ P22; s->slice[6] = P26 ^ P29; s->slice[5] = P17 ^ P28; s->slice[4] = P12 ^ P22; s->slice[3] = P23 ^ P27; s->slice[2] = P19 ^ P24; s->slice[1] = P14 ^ P23; s->slice[0] = P9 ^ P16; } else { /* Linear postprocessing */ uint16_t L0 = M61 ^ M62; uint16_t L1 = M50 ^ M56; uint16_t L2 = M46 ^ M48; uint16_t L3 = M47 ^ M55; uint16_t L4 = M54 ^ M58; uint16_t L5 = M49 ^ M61; uint16_t L6 = M62 ^ L5; uint16_t L7 = M46 ^ L3; uint16_t L8 = M51 ^ M59; uint16_t L9 = M52 ^ M53; uint16_t L10 = M53 ^ L4; uint16_t L11 = M60 ^ L2; uint16_t L12 = M48 ^ M51; uint16_t L13 = M50 ^ L0; uint16_t L14 = M52 ^ M61; uint16_t L15 = M55 ^ L1; uint16_t L16 = M56 ^ L0; uint16_t L17 = M57 ^ L1; uint16_t L18 = M58 ^ L8; uint16_t L19 = M63 ^ L4; uint16_t L20 = L0 ^ L1; uint16_t L21 = L1 ^ L7; uint16_t L22 = L3 ^ L12; uint16_t L23 = L18 ^ L2; uint16_t L24 = L15 ^ L9; uint16_t L25 = L6 ^ L10; uint16_t L26 = L7 ^ L9; uint16_t L27 = L8 ^ L10; uint16_t L28 = L11 ^ L14; uint16_t L29 = L11 ^ L17; s->slice[7] = L6 ^ L24; s->slice[6] = ~(L16 ^ L26); s->slice[5] = ~(L19 ^ L28); s->slice[4] = L6 ^ L21; s->slice[3] = L20 ^ L22; s->slice[2] = L25 ^ L29; s->slice[1] = ~(L13 ^ L27); s->slice[0] = ~(L6 ^ L23); } } #define BIT_RANGE(from,to) (((1 << ((to) - (from))) - 1) << (from)) #define BIT_RANGE_LEFT(x,from,to,shift) (((x) & BIT_RANGE((from), (to))) << (shift)) #define BIT_RANGE_RIGHT(x,from,to,shift) (((x) & BIT_RANGE((from), (to))) >> (shift)) static void ShiftRows(AES_state* s) { int i; for (i = 0; i < 8; i++) { uint16_t v = s->slice[i]; s->slice[i] = (v & BIT_RANGE(0, 4)) | BIT_RANGE_LEFT(v, 4, 5, 3) | BIT_RANGE_RIGHT(v, 5, 8, 1) | BIT_RANGE_LEFT(v, 8, 10, 2) | BIT_RANGE_RIGHT(v, 10, 12, 2) | BIT_RANGE_LEFT(v, 12, 15, 1) | BIT_RANGE_RIGHT(v, 15, 16, 3); } } static void InvShiftRows(AES_state* s) { int i; for (i = 0; i < 8; i++) { uint16_t v = s->slice[i]; s->slice[i] = (v & BIT_RANGE(0, 4)) | BIT_RANGE_LEFT(v, 4, 7, 1) | BIT_RANGE_RIGHT(v, 7, 8, 3) | BIT_RANGE_LEFT(v, 8, 10, 2) | BIT_RANGE_RIGHT(v, 10, 12, 2) | BIT_RANGE_LEFT(v, 12, 13, 3) | BIT_RANGE_RIGHT(v, 13, 16, 1); } } #define ROT(x,b) (((x) >> ((b) * 4)) | ((x) << ((4-(b)) * 4))) static void MixColumns(AES_state* s, int inv) { /* The MixColumns transform treats the bytes of the columns of the state as * coefficients of a 3rd degree polynomial over GF(2^8) and multiplies them * by the fixed polynomial a(x) = {03}x^3 + {01}x^2 + {01}x + {02}, modulo * x^4 + {01}. * * In the inverse transform, we multiply by the inverse of a(x), * a^-1(x) = {0b}x^3 + {0d}x^2 + {09}x + {0e}. This is equal to * a(x) * ({04}x^2 + {05}), so we can reuse the forward transform's code * (found in OpenSSL's bsaes-x86_64.pl, attributed to Jussi Kivilinna) * * In the bitsliced representation, a multiplication of every column by x * mod x^4 + 1 is simply a right rotation. */ /* Shared for both directions is a multiplication by a(x), which can be * rewritten as (x^3 + x^2 + x) + {02}*(x^3 + {01}). * * First compute s into the s? variables, (x^3 + {01}) * s into the s?_01 * variables and (x^3 + x^2 + x)*s into the s?_123 variables. */ uint16_t s0 = s->slice[0], s1 = s->slice[1], s2 = s->slice[2], s3 = s->slice[3]; uint16_t s4 = s->slice[4], s5 = s->slice[5], s6 = s->slice[6], s7 = s->slice[7]; uint16_t s0_01 = s0 ^ ROT(s0, 1), s0_123 = ROT(s0_01, 1) ^ ROT(s0, 3); uint16_t s1_01 = s1 ^ ROT(s1, 1), s1_123 = ROT(s1_01, 1) ^ ROT(s1, 3); uint16_t s2_01 = s2 ^ ROT(s2, 1), s2_123 = ROT(s2_01, 1) ^ ROT(s2, 3); uint16_t s3_01 = s3 ^ ROT(s3, 1), s3_123 = ROT(s3_01, 1) ^ ROT(s3, 3); uint16_t s4_01 = s4 ^ ROT(s4, 1), s4_123 = ROT(s4_01, 1) ^ ROT(s4, 3); uint16_t s5_01 = s5 ^ ROT(s5, 1), s5_123 = ROT(s5_01, 1) ^ ROT(s5, 3); uint16_t s6_01 = s6 ^ ROT(s6, 1), s6_123 = ROT(s6_01, 1) ^ ROT(s6, 3); uint16_t s7_01 = s7 ^ ROT(s7, 1), s7_123 = ROT(s7_01, 1) ^ ROT(s7, 3); /* Now compute s = s?_123 + {02} * s?_01. */ s->slice[0] = s7_01 ^ s0_123; s->slice[1] = s7_01 ^ s0_01 ^ s1_123; s->slice[2] = s1_01 ^ s2_123; s->slice[3] = s7_01 ^ s2_01 ^ s3_123; s->slice[4] = s7_01 ^ s3_01 ^ s4_123; s->slice[5] = s4_01 ^ s5_123; s->slice[6] = s5_01 ^ s6_123; s->slice[7] = s6_01 ^ s7_123; if (inv) { /* In the reverse direction, we further need to multiply by * {04}x^2 + {05}, which can be written as {04} * (x^2 + {01}) + {01}. * * First compute (x^2 + {01}) * s into the t?_02 variables: */ uint16_t t0_02 = s->slice[0] ^ ROT(s->slice[0], 2); uint16_t t1_02 = s->slice[1] ^ ROT(s->slice[1], 2); uint16_t t2_02 = s->slice[2] ^ ROT(s->slice[2], 2); uint16_t t3_02 = s->slice[3] ^ ROT(s->slice[3], 2); uint16_t t4_02 = s->slice[4] ^ ROT(s->slice[4], 2); uint16_t t5_02 = s->slice[5] ^ ROT(s->slice[5], 2); uint16_t t6_02 = s->slice[6] ^ ROT(s->slice[6], 2); uint16_t t7_02 = s->slice[7] ^ ROT(s->slice[7], 2); /* And then update s += {04} * t?_02 */ s->slice[0] ^= t6_02; s->slice[1] ^= t6_02 ^ t7_02; s->slice[2] ^= t0_02 ^ t7_02; s->slice[3] ^= t1_02 ^ t6_02; s->slice[4] ^= t2_02 ^ t6_02 ^ t7_02; s->slice[5] ^= t3_02 ^ t7_02; s->slice[6] ^= t4_02; s->slice[7] ^= t5_02; } } static void AddRoundKey(AES_state* s, const AES_state* round) { int b; for (b = 0; b < 8; b++) { s->slice[b] ^= round->slice[b]; } } /** column_0(s) = column_c(a) */ static void GetOneColumn(AES_state* s, const AES_state* a, int c) { int b; for (b = 0; b < 8; b++) { s->slice[b] = (a->slice[b] >> c) & 0x1111; } } /** column_c1(r) |= (column_0(s) ^= column_c2(a)) */ static void KeySetupColumnMix(AES_state* s, AES_state* r, const AES_state* a, int c1, int c2) { int b; for (b = 0; b < 8; b++) { r->slice[b] |= ((s->slice[b] ^= ((a->slice[b] >> c2) & 0x1111)) & 0x1111) << c1; } } /** Rotate the rows in s one position upwards, and xor in r */ static void KeySetupTransform(AES_state* s, const AES_state* r) { int b; for (b = 0; b < 8; b++) { s->slice[b] = ((s->slice[b] >> 4) | (s->slice[b] << 12)) ^ r->slice[b]; } } /* Multiply the cells in s by x, as polynomials over GF(2) mod x^8 + x^4 + x^3 + x + 1 */ static void MultX(AES_state* s) { uint16_t top = s->slice[7]; s->slice[7] = s->slice[6]; s->slice[6] = s->slice[5]; s->slice[5] = s->slice[4]; s->slice[4] = s->slice[3] ^ top; s->slice[3] = s->slice[2] ^ top; s->slice[2] = s->slice[1]; s->slice[1] = s->slice[0] ^ top; s->slice[0] = top; } /** Expand the cipher key into the key schedule. * * state must be a pointer to an array of size nrounds + 1. * key must be a pointer to 4 * nkeywords bytes. * * AES128 uses nkeywords = 4, nrounds = 10 * AES192 uses nkeywords = 6, nrounds = 12 * AES256 uses nkeywords = 8, nrounds = 14 */ static void AES_setup(AES_state* rounds, const uint8_t* key, int nkeywords, int nrounds) { int i; /* The one-byte round constant */ AES_state rcon = {{1,0,0,0,0,0,0,0}}; /* The number of the word being generated, modulo nkeywords */ int pos = 0; /* The column representing the word currently being processed */ AES_state column; for (i = 0; i < nrounds + 1; i++) { int b; for (b = 0; b < 8; b++) { rounds[i].slice[b] = 0; } } /* The first nkeywords round columns are just taken from the key directly. */ for (i = 0; i < nkeywords; i++) { int r; for (r = 0; r < 4; r++) { LoadByte(&rounds[i >> 2], *(key++), r, i & 3); } } GetOneColumn(&column, &rounds[(nkeywords - 1) >> 2], (nkeywords - 1) & 3); for (i = nkeywords; i < 4 * (nrounds + 1); i++) { /* Transform column */ if (pos == 0) { SubBytes(&column, 0); KeySetupTransform(&column, &rcon); MultX(&rcon); } else if (nkeywords > 6 && pos == 4) { SubBytes(&column, 0); } if (++pos == nkeywords) pos = 0; KeySetupColumnMix(&column, &rounds[i >> 2], &rounds[(i - nkeywords) >> 2], i & 3, (i - nkeywords) & 3); } } static void AES_encrypt(const AES_state* rounds, int nrounds, unsigned char* cipher16, const unsigned char* plain16) { AES_state s = {{0}}; int round; LoadBytes(&s, plain16); AddRoundKey(&s, rounds++); for (round = 1; round < nrounds; round++) { SubBytes(&s, 0); ShiftRows(&s); MixColumns(&s, 0); AddRoundKey(&s, rounds++); } SubBytes(&s, 0); ShiftRows(&s); AddRoundKey(&s, rounds); SaveBytes(cipher16, &s); } static void AES_decrypt(const AES_state* rounds, int nrounds, unsigned char* plain16, const unsigned char* cipher16) { /* Most AES decryption implementations use the alternate scheme * (the Equivalent Inverse Cipher), which allows for more code reuse between * the encryption and decryption code, but requires separate setup for both. */ AES_state s = {{0}}; int round; rounds += nrounds; LoadBytes(&s, cipher16); AddRoundKey(&s, rounds--); for (round = 1; round < nrounds; round++) { InvShiftRows(&s); SubBytes(&s, 1); AddRoundKey(&s, rounds--); MixColumns(&s, 1); } InvShiftRows(&s); SubBytes(&s, 1); AddRoundKey(&s, rounds); SaveBytes(plain16, &s); } void AES128_init(AES128_ctx* ctx, const unsigned char* key16) { AES_setup(ctx->rk, key16, 4, 10); } void AES128_encrypt(const AES128_ctx* ctx, size_t blocks, unsigned char* cipher16, const unsigned char* plain16) { while (blocks--) { AES_encrypt(ctx->rk, 10, cipher16, plain16); cipher16 += 16; plain16 += 16; } } void AES128_decrypt(const AES128_ctx* ctx, size_t blocks, unsigned char* plain16, const unsigned char* cipher16) { while (blocks--) { AES_decrypt(ctx->rk, 10, plain16, cipher16); cipher16 += 16; plain16 += 16; } } void AES192_init(AES192_ctx* ctx, const unsigned char* key24) { AES_setup(ctx->rk, key24, 6, 12); } void AES192_encrypt(const AES192_ctx* ctx, size_t blocks, unsigned char* cipher16, const unsigned char* plain16) { while (blocks--) { AES_encrypt(ctx->rk, 12, cipher16, plain16); cipher16 += 16; plain16 += 16; } } void AES192_decrypt(const AES192_ctx* ctx, size_t blocks, unsigned char* plain16, const unsigned char* cipher16) { while (blocks--) { AES_decrypt(ctx->rk, 12, plain16, cipher16); cipher16 += 16; plain16 += 16; } } void AES256_init(AES256_ctx* ctx, const unsigned char* key32) { AES_setup(ctx->rk, key32, 8, 14); } void AES256_encrypt(const AES256_ctx* ctx, size_t blocks, unsigned char* cipher16, const unsigned char* plain16) { while (blocks--) { AES_encrypt(ctx->rk, 14, cipher16, plain16); cipher16 += 16; plain16 += 16; } } void AES256_decrypt(const AES256_ctx* ctx, size_t blocks, unsigned char* plain16, const unsigned char* cipher16) { while (blocks--) { AES_decrypt(ctx->rk, 14, plain16, cipher16); cipher16 += 16; plain16 += 16; } }
0
bitcoin/src/crypto
bitcoin/src/crypto/ctaes/README.md
ctaes ===== Simple C module for constant-time AES encryption and decryption. Features: * Simple, pure C code without any dependencies. * No tables or data-dependent branches whatsoever, but using bit sliced approach from https://eprint.iacr.org/2009/129.pdf. * Very small object code: slightly over 4k of executable code when compiled with -Os. * Slower than implementations based on precomputed tables or specialized instructions, but can do ~15 MB/s on modern CPUs. Performance ----------- Compiled with GCC 5.3.1 with -O3, on an Intel(R) Core(TM) i7-4800MQ CPU, numbers in CPU cycles: | Algorithm | Key schedule | Encryption per byte | Decryption per byte | | --------- | ------------:| -------------------:| -------------------:| | AES-128 | 2.8k | 154 | 161 | | AES-192 | 3.1k | 169 | 181 | | AES-256 | 4.0k | 191 | 203 | Build steps ----------- Object code: $ gcc -O3 ctaes.c -c -o ctaes.o Tests: $ gcc -O3 ctaes.c test.c -o test Benchmark: $ gcc -O3 ctaes.c bench.c -o bench Review ------ Results of a formal review of the code can be found in http://bitcoin.sipa.be/ctaes/review.zip
0
bitcoin/src/crypto
bitcoin/src/crypto/ctaes/COPYING
The MIT License (MIT) Copyright (c) 2016 Pieter Wuille Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0
bitcoin/src/crypto
bitcoin/src/crypto/ctaes/test.c
/********************************************************************* * Copyright (c) 2016 Pieter Wuille * * Distributed under the MIT software license, see the accompanying * * file COPYING or http://www.opensource.org/licenses/mit-license.php.* **********************************************************************/ #include "ctaes.h" #include <stdio.h> #include <string.h> #include <assert.h> typedef struct { int keysize; const char* key; const char* plain; const char* cipher; } ctaes_test; static const ctaes_test ctaes_tests[] = { /* AES test vectors from FIPS 197. */ {128, "000102030405060708090a0b0c0d0e0f", "00112233445566778899aabbccddeeff", "69c4e0d86a7b0430d8cdb78070b4c55a"}, {192, "000102030405060708090a0b0c0d0e0f1011121314151617", "00112233445566778899aabbccddeeff", "dda97ca4864cdfe06eaf70a0ec0d7191"}, {256, "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", "00112233445566778899aabbccddeeff", "8ea2b7ca516745bfeafc49904b496089"}, /* AES-ECB test vectors from NIST sp800-38a. */ {128, "2b7e151628aed2a6abf7158809cf4f3c", "6bc1bee22e409f96e93d7e117393172a", "3ad77bb40d7a3660a89ecaf32466ef97"}, {128, "2b7e151628aed2a6abf7158809cf4f3c", "ae2d8a571e03ac9c9eb76fac45af8e51", "f5d3d58503b9699de785895a96fdbaaf"}, {128, "2b7e151628aed2a6abf7158809cf4f3c", "30c81c46a35ce411e5fbc1191a0a52ef", "43b1cd7f598ece23881b00e3ed030688"}, {128, "2b7e151628aed2a6abf7158809cf4f3c", "f69f2445df4f9b17ad2b417be66c3710", "7b0c785e27e8ad3f8223207104725dd4"}, {192, "8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b", "6bc1bee22e409f96e93d7e117393172a", "bd334f1d6e45f25ff712a214571fa5cc"}, {192, "8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b", "ae2d8a571e03ac9c9eb76fac45af8e51", "974104846d0ad3ad7734ecb3ecee4eef"}, {192, "8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b", "30c81c46a35ce411e5fbc1191a0a52ef", "ef7afd2270e2e60adce0ba2face6444e"}, {192, "8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b", "f69f2445df4f9b17ad2b417be66c3710", "9a4b41ba738d6c72fb16691603c18e0e"}, {256, "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", "6bc1bee22e409f96e93d7e117393172a", "f3eed1bdb5d2a03c064b5a7e3db181f8"}, {256, "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", "ae2d8a571e03ac9c9eb76fac45af8e51", "591ccb10d410ed26dc5ba74a31362870"}, {256, "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", "30c81c46a35ce411e5fbc1191a0a52ef", "b6ed21b99ca6f4f9f153e7b1beafed1d"}, {256, "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", "f69f2445df4f9b17ad2b417be66c3710", "23304b7a39f9f3ff067d8d8f9e24ecc7"} }; static void from_hex(unsigned char* data, int len, const char* hex) { int p; for (p = 0; p < len; p++) { int v = 0; int n; for (n = 0; n < 2; n++) { assert((*hex >= '0' && *hex <= '9') || (*hex >= 'a' && *hex <= 'f')); if (*hex >= '0' && *hex <= '9') { v |= (*hex - '0') << (4 * (1 - n)); } else { v |= (*hex - 'a' + 10) << (4 * (1 - n)); } hex++; } *(data++) = v; } assert(*hex == 0); } int main(void) { int i; int fail = 0; for (i = 0; i < sizeof(ctaes_tests) / sizeof(ctaes_tests[0]); i++) { unsigned char key[32], plain[16], cipher[16], ciphered[16], deciphered[16]; const ctaes_test* test = &ctaes_tests[i]; assert(test->keysize == 128 || test->keysize == 192 || test->keysize == 256); from_hex(plain, 16, test->plain); from_hex(cipher, 16, test->cipher); switch (test->keysize) { case 128: { AES128_ctx ctx; from_hex(key, 16, test->key); AES128_init(&ctx, key); AES128_encrypt(&ctx, 1, ciphered, plain); AES128_decrypt(&ctx, 1, deciphered, cipher); break; } case 192: { AES192_ctx ctx; from_hex(key, 24, test->key); AES192_init(&ctx, key); AES192_encrypt(&ctx, 1, ciphered, plain); AES192_decrypt(&ctx, 1, deciphered, cipher); break; } case 256: { AES256_ctx ctx; from_hex(key, 32, test->key); AES256_init(&ctx, key); AES256_encrypt(&ctx, 1, ciphered, plain); AES256_decrypt(&ctx, 1, deciphered, cipher); break; } } if (memcmp(cipher, ciphered, 16)) { fprintf(stderr, "E(key=\"%s\", plain=\"%s\") != \"%s\"\n", test->key, test->plain, test->cipher); fail++; } if (memcmp(plain, deciphered, 16)) { fprintf(stderr, "D(key=\"%s\", cipher=\"%s\") != \"%s\"\n", test->key, test->cipher, test->plain); fail++; } } if (fail == 0) { fprintf(stderr, "All tests successful\n"); } else { fprintf(stderr, "%i tests failed\n", fail); } return (fail != 0); }
0
bitcoin/src/crypto
bitcoin/src/crypto/ctaes/ctaes.h
/********************************************************************* * Copyright (c) 2016 Pieter Wuille * * Distributed under the MIT software license, see the accompanying * * file COPYING or http://www.opensource.org/licenses/mit-license.php.* **********************************************************************/ #ifndef _CTAES_H_ #define _CTAES_H_ 1 #include <stdint.h> #include <stdlib.h> typedef struct { uint16_t slice[8]; } AES_state; typedef struct { AES_state rk[11]; } AES128_ctx; typedef struct { AES_state rk[13]; } AES192_ctx; typedef struct { AES_state rk[15]; } AES256_ctx; void AES128_init(AES128_ctx* ctx, const unsigned char* key16); void AES128_encrypt(const AES128_ctx* ctx, size_t blocks, unsigned char* cipher16, const unsigned char* plain16); void AES128_decrypt(const AES128_ctx* ctx, size_t blocks, unsigned char* plain16, const unsigned char* cipher16); void AES192_init(AES192_ctx* ctx, const unsigned char* key24); void AES192_encrypt(const AES192_ctx* ctx, size_t blocks, unsigned char* cipher16, const unsigned char* plain16); void AES192_decrypt(const AES192_ctx* ctx, size_t blocks, unsigned char* plain16, const unsigned char* cipher16); void AES256_init(AES256_ctx* ctx, const unsigned char* key32); void AES256_encrypt(const AES256_ctx* ctx, size_t blocks, unsigned char* cipher16, const unsigned char* plain16); void AES256_decrypt(const AES256_ctx* ctx, size_t blocks, unsigned char* plain16, const unsigned char* cipher16); #endif
0
bitcoin/src/crypto
bitcoin/src/crypto/ctaes/bench.c
#include <stdio.h> #include <math.h> #include "sys/time.h" #include "ctaes.h" static double gettimedouble(void) { struct timeval tv; gettimeofday(&tv, NULL); return tv.tv_usec * 0.000001 + tv.tv_sec; } static void print_number(double x) { double y = x; int c = 0; if (y < 0.0) { y = -y; } while (y < 100.0) { y *= 10.0; c++; } printf("%.*f", c, x); } static void run_benchmark(char *name, void (*benchmark)(void*), void (*setup)(void*), void (*teardown)(void*), void* data, int count, int iter) { int i; double min = HUGE_VAL; double sum = 0.0; double max = 0.0; for (i = 0; i < count; i++) { double begin, total; if (setup != NULL) { setup(data); } begin = gettimedouble(); benchmark(data); total = gettimedouble() - begin; if (teardown != NULL) { teardown(data); } if (total < min) { min = total; } if (total > max) { max = total; } sum += total; } printf("%s: min ", name); print_number(min * 1000000000.0 / iter); printf("ns / avg "); print_number((sum / count) * 1000000000.0 / iter); printf("ns / max "); print_number(max * 1000000000.0 / iter); printf("ns\n"); } static void bench_AES128_init(void* data) { AES128_ctx* ctx = (AES128_ctx*)data; int i; for (i = 0; i < 50000; i++) { AES128_init(ctx, (unsigned char*)ctx); } } static void bench_AES128_encrypt_setup(void* data) { AES128_ctx* ctx = (AES128_ctx*)data; static const unsigned char key[16] = {0}; AES128_init(ctx, key); } static void bench_AES128_encrypt(void* data) { const AES128_ctx* ctx = (const AES128_ctx*)data; unsigned char scratch[16] = {0}; int i; for (i = 0; i < 4000000 / 16; i++) { AES128_encrypt(ctx, 1, scratch, scratch); } } static void bench_AES128_decrypt(void* data) { const AES128_ctx* ctx = (const AES128_ctx*)data; unsigned char scratch[16] = {0}; int i; for (i = 0; i < 4000000 / 16; i++) { AES128_decrypt(ctx, 1, scratch, scratch); } } static void bench_AES192_init(void* data) { AES192_ctx* ctx = (AES192_ctx*)data; int i; for (i = 0; i < 50000; i++) { AES192_init(ctx, (unsigned char*)ctx); } } static void bench_AES192_encrypt_setup(void* data) { AES192_ctx* ctx = (AES192_ctx*)data; static const unsigned char key[16] = {0}; AES192_init(ctx, key); } static void bench_AES192_encrypt(void* data) { const AES192_ctx* ctx = (const AES192_ctx*)data; unsigned char scratch[16] = {0}; int i; for (i = 0; i < 4000000 / 16; i++) { AES192_encrypt(ctx, 1, scratch, scratch); } } static void bench_AES192_decrypt(void* data) { const AES192_ctx* ctx = (const AES192_ctx*)data; unsigned char scratch[16] = {0}; int i; for (i = 0; i < 4000000 / 16; i++) { AES192_decrypt(ctx, 1, scratch, scratch); } } static void bench_AES256_init(void* data) { AES256_ctx* ctx = (AES256_ctx*)data; int i; for (i = 0; i < 50000; i++) { AES256_init(ctx, (unsigned char*)ctx); } } static void bench_AES256_encrypt_setup(void* data) { AES256_ctx* ctx = (AES256_ctx*)data; static const unsigned char key[16] = {0}; AES256_init(ctx, key); } static void bench_AES256_encrypt(void* data) { const AES256_ctx* ctx = (const AES256_ctx*)data; unsigned char scratch[16] = {0}; int i; for (i = 0; i < 4000000 / 16; i++) { AES256_encrypt(ctx, 1, scratch, scratch); } } static void bench_AES256_decrypt(void* data) { const AES256_ctx* ctx = (const AES256_ctx*)data; unsigned char scratch[16] = {0}; int i; for (i = 0; i < 4000000 / 16; i++) { AES256_decrypt(ctx, 1, scratch, scratch); } } int main(void) { AES128_ctx ctx128; AES192_ctx ctx192; AES256_ctx ctx256; run_benchmark("aes128_init", bench_AES128_init, NULL, NULL, &ctx128, 20, 50000); run_benchmark("aes128_encrypt_byte", bench_AES128_encrypt, bench_AES128_encrypt_setup, NULL, &ctx128, 20, 4000000); run_benchmark("aes128_decrypt_byte", bench_AES128_decrypt, bench_AES128_encrypt_setup, NULL, &ctx128, 20, 4000000); run_benchmark("aes192_init", bench_AES192_init, NULL, NULL, &ctx192, 20, 50000); run_benchmark("aes192_encrypt_byte", bench_AES192_encrypt, bench_AES192_encrypt_setup, NULL, &ctx192, 20, 4000000); run_benchmark("aes192_decrypt_byte", bench_AES192_decrypt, bench_AES192_encrypt_setup, NULL, &ctx192, 20, 4000000); run_benchmark("aes256_init", bench_AES256_init, NULL, NULL, &ctx256, 20, 50000); run_benchmark("aes256_encrypt_byte", bench_AES256_encrypt, bench_AES256_encrypt_setup, NULL, &ctx256, 20, 4000000); run_benchmark("aes256_decrypt_byte", bench_AES256_decrypt, bench_AES256_encrypt_setup, NULL, &ctx256, 20, 4000000); return 0; }
0
bitcoin/src
bitcoin/src/crc32c/CMakeLists.txt
# Copyright 2017 The CRC32C Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. See the AUTHORS file for names of contributors. cmake_minimum_required(VERSION 3.1) project(Crc32c VERSION 1.1.0 LANGUAGES C CXX) # C standard can be overridden when this is used as a sub-project. if(NOT CMAKE_C_STANDARD) # This project can use C11, but will gracefully decay down to C89. set(CMAKE_C_STANDARD 11) set(CMAKE_C_STANDARD_REQUIRED OFF) set(CMAKE_C_EXTENSIONS OFF) endif(NOT CMAKE_C_STANDARD) # C++ standard can be overridden when this is used as a sub-project. if(NOT CMAKE_CXX_STANDARD) # This project requires C++11. set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) endif(NOT CMAKE_CXX_STANDARD) # https://github.com/izenecloud/cmake/blob/master/SetCompilerWarningAll.cmake if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") # Use the highest warning level for Visual Studio. set(CMAKE_CXX_WARNING_LEVEL 4) if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]") string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") else(CMAKE_CXX_FLAGS MATCHES "/W[0-4]") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4") endif(CMAKE_CXX_FLAGS MATCHES "/W[0-4]") # Disable C++ exceptions. string(REGEX REPLACE "/EH[a-z]+" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHs-c-") add_definitions(-D_HAS_EXCEPTIONS=0) # Disable RTTI. string(REGEX REPLACE "/GR" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /GR-") else(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") # Use -Wall for clang and gcc. if(NOT CMAKE_CXX_FLAGS MATCHES "-Wall") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall") endif(NOT CMAKE_CXX_FLAGS MATCHES "-Wall") # Use -Wextra for clang and gcc. if(NOT CMAKE_CXX_FLAGS MATCHES "-Wextra") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wextra") endif(NOT CMAKE_CXX_FLAGS MATCHES "-Wextra") # Use -Werror for clang and gcc. if(NOT CMAKE_CXX_FLAGS MATCHES "-Werror") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror") endif(NOT CMAKE_CXX_FLAGS MATCHES "-Werror") # Disable C++ exceptions. string(REGEX REPLACE "-fexceptions" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions") # Disable RTTI. string(REGEX REPLACE "-frtti" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti") endif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") option(CRC32C_BUILD_TESTS "Build CRC32C's unit tests" ON) option(CRC32C_BUILD_BENCHMARKS "Build CRC32C's benchmarks" ON) option(CRC32C_USE_GLOG "Build CRC32C's tests with Google Logging" ON) option(CRC32C_INSTALL "Install CRC32C's header and library" ON) include(TestBigEndian) test_big_endian(BYTE_ORDER_BIG_ENDIAN) include(CheckCXXCompilerFlag) # Used by glog. check_cxx_compiler_flag(-Wno-deprecated CRC32C_HAVE_NO_DEPRECATED) # Used by glog. check_cxx_compiler_flag(-Wno-sign-compare CRC32C_HAVE_NO_SIGN_COMPARE) # Used by glog. check_cxx_compiler_flag(-Wno-unused-parameter CRC32C_HAVE_NO_UNUSED_PARAMETER) # Used by googletest. check_cxx_compiler_flag(-Wno-missing-field-initializers CRC32C_HAVE_NO_MISSING_FIELD_INITIALIZERS) # Check for __builtin_prefetch support in the compiler. include(CheckCXXSourceCompiles) check_cxx_source_compiles(" int main() { char data = 0; const char* address = &data; __builtin_prefetch(address, 0, 0); return 0; } " HAVE_BUILTIN_PREFETCH) # Check for _mm_prefetch support in the compiler. include(CheckCXXSourceCompiles) check_cxx_source_compiles(" #if defined(_MSC_VER) #include <intrin.h> #else // !defined(_MSC_VER) #include <xmmintrin.h> #endif // defined(_MSC_VER) int main() { char data = 0; const char* address = &data; _mm_prefetch(address, _MM_HINT_NTA); return 0; } " HAVE_MM_PREFETCH) # Check for SSE4.2 support in the compiler. set(OLD_CMAKE_REQURED_FLAGS ${CMAKE_REQUIRED_FLAGS}) if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} /arch:AVX") else(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -msse4.2") endif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") check_cxx_source_compiles(" #if defined(_MSC_VER) #include <intrin.h> #else // !defined(_MSC_VER) #include <cpuid.h> #include <nmmintrin.h> #endif // defined(_MSC_VER) int main() { _mm_crc32_u8(0, 0); _mm_crc32_u32(0, 0); #if defined(_M_X64) || defined(__x86_64__) _mm_crc32_u64(0, 0); #endif // defined(_M_X64) || defined(__x86_64__) return 0; } " HAVE_SSE42) set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQURED_FLAGS}) # Check for ARMv8 w/ CRC and CRYPTO extensions support in the compiler. set(OLD_CMAKE_REQURED_FLAGS ${CMAKE_REQUIRED_FLAGS}) if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") # TODO(pwnall): Insert correct flag when VS gets ARM CRC32C support. set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} /arch:NOTYET") else(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -march=armv8-a+crc+crypto") endif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") check_cxx_source_compiles(" #include <arm_acle.h> #include <arm_neon.h> int main() { __crc32cb(0, 0); __crc32ch(0, 0); __crc32cw(0, 0); __crc32cd(0, 0); vmull_p64(0, 0); return 0; } " HAVE_ARM64_CRC32C) set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQURED_FLAGS}) # Check for strong getauxval() support in the system headers. check_cxx_source_compiles(" #include <arm_acle.h> #include <arm_neon.h> #include <sys/auxv.h> int main() { getauxval(AT_HWCAP); return 0; } " HAVE_STRONG_GETAUXVAL) # Check for weak getauxval() support in the compiler. check_cxx_source_compiles(" unsigned long getauxval(unsigned long type) __attribute__((weak)); #define AT_HWCAP 16 int main() { getauxval(AT_HWCAP); return 0; } " HAVE_WEAK_GETAUXVAL) if(CRC32C_USE_GLOG) # glog requires this setting to avoid using dynamic_cast. set(DISABLE_RTTI ON CACHE BOOL "" FORCE) # glog's test targets trigger deprecation warnings, and compiling them burns # CPU cycles on the CI. set(BUILD_TESTING_SAVED "${BUILD_TESTING}") set(BUILD_TESTING OFF CACHE BOOL "" FORCE) add_subdirectory("third_party/glog" EXCLUDE_FROM_ALL) set(BUILD_TESTING "${BUILD_TESTING_SAVED}" CACHE BOOL "" FORCE) # glog triggers deprecation warnings on OSX. # https://github.com/google/glog/issues/185 if(CRC32C_HAVE_NO_DEPRECATED) set_property(TARGET glog APPEND PROPERTY COMPILE_OPTIONS -Wno-deprecated) endif(CRC32C_HAVE_NO_DEPRECATED) # glog triggers sign comparison warnings on gcc. if(CRC32C_HAVE_NO_SIGN_COMPARE) set_property(TARGET glog APPEND PROPERTY COMPILE_OPTIONS -Wno-sign-compare) endif(CRC32C_HAVE_NO_SIGN_COMPARE) # glog triggers unused parameter warnings on clang. if(CRC32C_HAVE_NO_UNUSED_PARAMETER) set_property(TARGET glog APPEND PROPERTY COMPILE_OPTIONS -Wno-unused-parameter) endif(CRC32C_HAVE_NO_UNUSED_PARAMETER) set(CRC32C_TESTS_BUILT_WITH_GLOG 1) endif(CRC32C_USE_GLOG) configure_file( "src/crc32c_config.h.in" "${PROJECT_BINARY_DIR}/include/crc32c/crc32c_config.h" ) include_directories("${PROJECT_BINARY_DIR}/include") # ARM64 CRC32C code is built separately, so we don't accidentally compile # unsupported instructions into code that gets run without ARM32 support. add_library(crc32c_arm64 OBJECT "") target_sources(crc32c_arm64 PRIVATE "${PROJECT_BINARY_DIR}/include/crc32c/crc32c_config.h" "src/crc32c_arm64.cc" "src/crc32c_arm64.h" ) if(HAVE_ARM64_CRC32C) if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") # TODO(pwnall): Insert correct flag when VS gets ARM64 CRC32C support. target_compile_options(crc32c_arm64 PRIVATE "/arch:NOTYET") else(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") target_compile_options(crc32c_arm64 PRIVATE "-march=armv8-a+crc+crypto") endif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") endif(HAVE_ARM64_CRC32C) # CMake only enables PIC by default in SHARED and MODULE targets. if(BUILD_SHARED_LIBS) set_property(TARGET crc32c_arm64 PROPERTY POSITION_INDEPENDENT_CODE TRUE) endif(BUILD_SHARED_LIBS) # SSE4.2 code is built separately, so we don't accidentally compile unsupported # instructions into code that gets run without SSE4.2 support. add_library(crc32c_sse42 OBJECT "") target_sources(crc32c_sse42 PRIVATE "${PROJECT_BINARY_DIR}/include/crc32c/crc32c_config.h" "src/crc32c_sse42.cc" "src/crc32c_sse42.h" ) if(HAVE_SSE42) if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") target_compile_options(crc32c_sse42 PRIVATE "/arch:AVX") else(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") target_compile_options(crc32c_sse42 PRIVATE "-msse4.2") endif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") endif(HAVE_SSE42) # CMake only enables PIC by default in SHARED and MODULE targets. if(BUILD_SHARED_LIBS) set_property(TARGET crc32c_sse42 PROPERTY POSITION_INDEPENDENT_CODE TRUE) endif(BUILD_SHARED_LIBS) # Must be included before CMAKE_INSTALL_INCLUDEDIR is used. include(GNUInstallDirs) add_library(crc32c "" # TODO(pwnall): Move the TARGET_OBJECTS generator expressions to the PRIVATE # section of target_sources when cmake_minimum_required becomes 3.9 or above. $<TARGET_OBJECTS:crc32c_arm64> $<TARGET_OBJECTS:crc32c_sse42> ) target_sources(crc32c PRIVATE "${PROJECT_BINARY_DIR}/include/crc32c/crc32c_config.h" "src/crc32c_arm64.h" "src/crc32c_arm64_check.h" "src/crc32c_internal.h" "src/crc32c_portable.cc" "src/crc32c_prefetch.h" "src/crc32c_read_le.h" "src/crc32c_round_up.h" "src/crc32c_sse42.h" "src/crc32c_sse42_check.h" "src/crc32c.cc" # Only CMake 3.3+ supports PUBLIC sources in targets exported by "install". $<$<VERSION_GREATER:CMAKE_VERSION,3.2>:PUBLIC> "include/crc32c/crc32c.h" ) target_include_directories(crc32c PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include> $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}> ) set_property( TARGET crc32c_arm64 crc32c_sse42 crc32c APPEND PROPERTY COMPILE_DEFINITIONS CRC32C_HAVE_CONFIG_H ) set_target_properties(crc32c PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR}) # Warnings as errors in Visual Studio for this project's targets. if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") set_property(TARGET crc32c APPEND PROPERTY COMPILE_OPTIONS "/WX") set_property(TARGET crc32c_arm64 APPEND PROPERTY COMPILE_OPTIONS "/WX") set_property(TARGET crc32c_sse42 APPEND PROPERTY COMPILE_OPTIONS "/WX") endif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") if(CRC32C_BUILD_TESTS) enable_testing() # Prevent overriding the parent project's compiler/linker settings on Windows. set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) set(install_gtest OFF) set(install_gmock OFF) # This project is tested using GoogleTest. add_subdirectory("third_party/googletest") # GoogleTest triggers a missing field initializers warning. if(CRC32C_HAVE_NO_MISSING_FIELD_INITIALIZERS) set_property(TARGET gtest APPEND PROPERTY COMPILE_OPTIONS -Wno-missing-field-initializers) set_property(TARGET gmock APPEND PROPERTY COMPILE_OPTIONS -Wno-missing-field-initializers) endif(CRC32C_HAVE_NO_MISSING_FIELD_INITIALIZERS) add_executable(crc32c_tests "") target_sources(crc32c_tests PRIVATE "${PROJECT_BINARY_DIR}/include/crc32c/crc32c_config.h" "src/crc32c_arm64_unittest.cc" "src/crc32c_extend_unittests.h" "src/crc32c_portable_unittest.cc" "src/crc32c_prefetch_unittest.cc" "src/crc32c_read_le_unittest.cc" "src/crc32c_round_up_unittest.cc" "src/crc32c_sse42_unittest.cc" "src/crc32c_unittest.cc" "src/crc32c_test_main.cc" ) target_link_libraries(crc32c_tests crc32c gtest) # Warnings as errors in Visual Studio for this project's targets. if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") set_property(TARGET crc32c_tests APPEND PROPERTY COMPILE_OPTIONS "/WX") endif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") if(CRC32C_USE_GLOG) target_link_libraries(crc32c_tests glog) endif(CRC32C_USE_GLOG) add_test(NAME crc32c_tests COMMAND crc32c_tests) add_executable(crc32c_capi_tests "") target_sources(crc32c_capi_tests PRIVATE "src/crc32c_capi_unittest.c" ) target_link_libraries(crc32c_capi_tests crc32c) # Warnings as errors in Visual Studio for this project's targets. if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") set_property(TARGET crc32c_capi_tests APPEND PROPERTY COMPILE_OPTIONS "/WX") endif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") add_test(NAME crc32c_capi_tests COMMAND crc32c_capi_tests) endif(CRC32C_BUILD_TESTS) if(CRC32C_BUILD_BENCHMARKS) add_executable(crc32c_bench "") target_sources(crc32c_bench PRIVATE "${PROJECT_BINARY_DIR}/include/crc32c/crc32c_config.h" "src/crc32c_benchmark.cc" ) target_link_libraries(crc32c_bench crc32c) # This project uses Google benchmark for benchmarking. set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "" FORCE) set(BENCHMARK_ENABLE_EXCEPTIONS OFF CACHE BOOL "" FORCE) add_subdirectory("third_party/benchmark") target_link_libraries(crc32c_bench benchmark) if(CRC32C_USE_GLOG) target_link_libraries(crc32c_bench glog) endif(CRC32C_USE_GLOG) # Warnings as errors in Visual Studio for this project's targets. if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") set_property(TARGET crc32c_bench APPEND PROPERTY COMPILE_OPTIONS "/WX") endif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") endif(CRC32C_BUILD_BENCHMARKS) if(CRC32C_INSTALL) install(TARGETS crc32c EXPORT Crc32cTargets RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} ) install( FILES "include/crc32c/crc32c.h" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/crc32c" ) include(CMakePackageConfigHelpers) configure_package_config_file( "${PROJECT_NAME}Config.cmake.in" "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" ) write_basic_package_version_file( "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" COMPATIBILITY SameMajorVersion ) install( EXPORT Crc32cTargets NAMESPACE Crc32c:: DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" ) install( FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" ) endif(CRC32C_INSTALL)
0
bitcoin/src
bitcoin/src/crc32c/LICENSE
Copyright 2017, The CRC32C Authors. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Google Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0
bitcoin/src
bitcoin/src/crc32c/AUTHORS
# This is the list of CRC32C authors for copyright purposes. # # This does not necessarily list everyone who has contributed code, since in # some cases, their employer may be the copyright holder. To see the full list # of contributors, see the revision history in source control. Google Inc. Fangming Fang <[email protected]> Vadim Skipin <[email protected]> Rodrigo Tobar <[email protected]> Harry Mallon <[email protected]>
0
bitcoin/src
bitcoin/src/crc32c/.appveyor.yml
# Build matrix / environment variables are explained on: # https://www.appveyor.com/docs/appveyor-yml/ # This file can be validated on: https://ci.appveyor.com/tools/validate-yaml version: "{build}" environment: matrix: # AppVeyor currently has no custom job name feature. # http://help.appveyor.com/discussions/questions/1623-can-i-provide-a-friendly-name-for-jobs - JOB: Visual Studio 2019 APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2019 CMAKE_GENERATOR: Visual Studio 16 2019 platform: - x86 - x64 configuration: - RelWithDebInfo - Debug build_script: - git submodule update --init --recursive - mkdir build - cd build - if "%platform%"=="x86" (set CMAKE_GENERATOR_PLATFORM="Win32") else (set CMAKE_GENERATOR_PLATFORM="%platform%") - cmake --version - cmake .. -G "%CMAKE_GENERATOR%" -A "%CMAKE_GENERATOR_PLATFORM%" -DCMAKE_CONFIGURATION_TYPES="%CONFIGURATION%" -DCRC32C_USE_GLOG=0 - cmake --build . --config "%CONFIGURATION%" - cd .. test_script: - build\%CONFIGURATION%\crc32c_tests.exe - build\%CONFIGURATION%\crc32c_capi_tests.exe - build\%CONFIGURATION%\crc32c_bench.exe
0
bitcoin/src
bitcoin/src/crc32c/README.md
# CRC32C [![Build Status](https://travis-ci.org/google/crc32c.svg?branch=master)](https://travis-ci.org/google/crc32c) [![Build Status](https://ci.appveyor.com/api/projects/status/moiq7331pett4xuj/branch/master?svg=true)](https://ci.appveyor.com/project/pwnall/crc32c) New file format authors should consider [HighwayHash](https://github.com/google/highwayhash). The initial version of this code was extracted from [LevelDB](https://github.com/google/leveldb), which is a stable key-value store that is widely used at Google. This project collects a few CRC32C implementations under an umbrella that dispatches to a suitable implementation based on the host computer's hardware capabilities. CRC32C is specified as the CRC that uses the iSCSI polynomial in [RFC 3720](https://tools.ietf.org/html/rfc3720#section-12.1). The polynomial was introduced by G. Castagnoli, S. Braeuer and M. Herrmann. CRC32C is used in software such as Btrfs, ext4, Ceph and leveldb. ## Usage ```cpp #include "crc32c/crc32c.h" int main() { const std::uint8_t buffer[] = {0, 0, 0, 0}; std::uint32_t result; // Process a raw buffer. result = crc32c::Crc32c(buffer, 4); // Process a std::string. std::string string; string.resize(4); result = crc32c::Crc32c(string); // If you have C++17 support, process a std::string_view. std::string_view string_view(string); result = crc32c::Crc32c(string_view); return 0; } ``` ## Prerequisites This project uses [CMake](https://cmake.org/) for building and testing. CMake is available in all popular Linux distributions, as well as in [Homebrew](https://brew.sh/). This project uses submodules for dependency management. ```bash git submodule update --init --recursive ``` If you're using [Atom](https://atom.io/), the following packages can help. ```bash apm install autocomplete-clang build build-cmake clang-format language-cmake \ linter linter-clang ``` If you don't mind more setup in return for more speed, replace `autocomplete-clang` and `linter-clang` with `you-complete-me`. This requires [setting up ycmd](https://github.com/ycm-core/ycmd#building). ```bash apm install autocomplete-plus build build-cmake clang-format language-cmake \ linter you-complete-me ``` ## Building The following commands build and install the project. ```bash mkdir build cd build cmake -DCRC32C_BUILD_TESTS=0 -DCRC32C_BUILD_BENCHMARKS=0 .. && make all install ``` ## Development The following command (when executed from `build/`) (re)builds the project and runs the tests. ```bash cmake .. && cmake --build . && ctest --output-on-failure ``` ### Android testing The following command builds the project against the Android NDK, which is useful for benchmarking against ARM processors. ```bash cmake .. -DCMAKE_SYSTEM_NAME=Android -DCMAKE_ANDROID_ARCH_ABI=arm64-v8a \ -DCMAKE_ANDROID_NDK=$HOME/Library/Android/sdk/ndk-bundle \ -DCMAKE_ANDROID_NDK_TOOLCHAIN_VERSION=clang \ -DCMAKE_ANDROID_STL_TYPE=c++_static -DCRC32C_USE_GLOG=0 \ -DCMAKE_BUILD_TYPE=Release && cmake --build . ``` The following commands install and run the benchmarks. ```bash adb push crc32c_bench /data/local/tmp adb shell chmod +x /data/local/tmp/crc32c_bench adb shell 'cd /data/local/tmp && ./crc32c_bench' adb shell rm /data/local/tmp/crc32c_bench ``` The following commands install and run the tests. ```bash adb push crc32c_tests /data/local/tmp adb shell chmod +x /data/local/tmp/crc32c_tests adb shell 'cd /data/local/tmp && ./crc32c_tests' adb shell rm /data/local/tmp/crc32c_tests ```
0
bitcoin/src
bitcoin/src/crc32c/CONTRIBUTING.md
# How to Contribute We'd love to accept your patches and contributions to this project. There are just a few small guidelines you need to follow. ## Contributor License Agreement Contributions to this project must be accompanied by a Contributor License Agreement. You (or your employer) retain the copyright to your contribution, this simply gives us permission to use and redistribute your contributions as part of the project. Head over to <https://cla.developers.google.com/> to see your current agreements on file or to sign a new one. You generally only need to submit a CLA once, so if you've already submitted one (even if it was for a different project), you probably don't need to do it again. ## Code reviews All submissions, including submissions by project members, require review. We use GitHub pull requests for this purpose. Consult [GitHub Help](https://help.github.com/articles/about-pull-requests/) for more information on using pull requests.
0
bitcoin/src
bitcoin/src/crc32c/.clang-format
--- Language: Cpp BasedOnStyle: Google
0
bitcoin/src
bitcoin/src/crc32c/.ycm_extra_conf.py
# Copyright 2017 The CRC32C Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. """YouCompleteMe configuration that interprets a .clang_complete file. This module implementes the YouCompleteMe configuration API documented at: https://github.com/ycm-core/ycmd#ycm_extra_confpy-specification The implementation loads and processes a .clang_complete file, documented at: https://github.com/xavierd/clang_complete/blob/master/README.md """ import os # Flags added to the list in .clang_complete. BASE_FLAGS = [ '-Werror', # Unlike clang_complete, YCM can also be used as a linter. '-DUSE_CLANG_COMPLETER', # YCM needs this. '-xc++', # YCM needs this to avoid compiling headers as C code. ] # Clang flags that take in paths. # See https://clang.llvm.org/docs/ClangCommandLineReference.html PATH_FLAGS = [ '-isystem', '-I', '-iquote', '--sysroot=' ] def DirectoryOfThisScript(): """Returns the absolute path to the directory containing this script.""" return os.path.dirname(os.path.abspath(__file__)) def MakeRelativePathsInFlagsAbsolute(flags, build_root): """Expands relative paths in a list of Clang command-line flags. Args: flags: The list of flags passed to Clang. build_root: The current directory when running the Clang compiler. Should be an absolute path. Returns: A list of flags with relative paths replaced by absolute paths. """ new_flags = [] make_next_absolute = False for flag in flags: new_flag = flag if make_next_absolute: make_next_absolute = False if not flag.startswith('/'): new_flag = os.path.join(build_root, flag) for path_flag in PATH_FLAGS: if flag == path_flag: make_next_absolute = True break if flag.startswith(path_flag): path = flag[len(path_flag):] new_flag = path_flag + os.path.join(build_root, path) break if new_flag: new_flags.append(new_flag) return new_flags def FindNearest(target, path, build_root): """Looks for a file with a specific name closest to a project path. This is similar to the logic used by a version-control system (like git) to find its configuration directory (.git) based on the current directory when a command is invoked. Args: target: The file name to search for. path: The directory where the search starts. The search will explore the given directory's ascendants using the parent relationship. Should be an absolute path. build_root: A directory that acts as a fence for the search. If the search reaches this directory, it will not advance to its parent. Should be an absolute path. Returns: The path to a file with the desired name. None if the search failed. """ candidate = os.path.join(path, target) if os.path.isfile(candidate): return candidate if path == build_root: return None parent = os.path.dirname(path) if parent == path: return None return FindNearest(target, parent, build_root) def FlagsForClangComplete(file_path, build_root): """Reads the .clang_complete flags for a source file. Args: file_path: The path to the source file. Should be inside the project. Used to locate the relevant .clang_complete file. build_root: The current directory when running the Clang compiler for this file. Should be an absolute path. Returns: A list of strings, where each element is a Clang command-line flag. """ clang_complete_path = FindNearest('.clang_complete', file_path, build_root) if clang_complete_path is None: return None clang_complete_flags = open(clang_complete_path, 'r').read().splitlines() return clang_complete_flags def FlagsForFile(filename, **kwargs): """Implements the YouCompleteMe API.""" # kwargs can be used to pass 'client_data' to the YCM configuration. This # configuration script does not need any extra information, so # pylint: disable=unused-argument build_root = DirectoryOfThisScript() file_path = os.path.realpath(filename) flags = BASE_FLAGS clang_flags = FlagsForClangComplete(file_path, build_root) if clang_flags: flags += clang_flags final_flags = MakeRelativePathsInFlagsAbsolute(flags, build_root) return {'flags': final_flags}
0
bitcoin/src
bitcoin/src/crc32c/Crc32cConfig.cmake.in
# Copyright 2017 The CRC32C Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. See the AUTHORS file for names of contributors. @PACKAGE_INIT@ include("${CMAKE_CURRENT_LIST_DIR}/Crc32cTargets.cmake") check_required_components(Crc32c)
0
bitcoin/src
bitcoin/src/crc32c/.clang_complete
-Ibuild/include/ -Ibuild/third_party/glog/ -Iinclude/ -Ithird_party/benchmark/include/ -Ithird_party/googletest/googletest/include/ -Ithird_party/googletest/googlemock/include/ -Ithird_party/glog/src/ -std=c++11
0
bitcoin/src
bitcoin/src/crc32c/.travis.yml
# Build matrix / environment variables are explained on: # http://about.travis-ci.org/docs/user/build-configuration/ # This file can be validated on: http://lint.travis-ci.org/ language: cpp dist: bionic osx_image: xcode12.5 compiler: - gcc - clang os: - linux - osx env: - GLOG=1 SHARED_LIB=0 BUILD_TYPE=Debug - GLOG=1 SHARED_LIB=0 BUILD_TYPE=RelWithDebInfo - GLOG=0 SHARED_LIB=0 BUILD_TYPE=Debug - GLOG=0 SHARED_LIB=0 BUILD_TYPE=RelWithDebInfo - GLOG=0 SHARED_LIB=1 BUILD_TYPE=Debug - GLOG=0 SHARED_LIB=1 BUILD_TYPE=RelWithDebInfo addons: apt: sources: - sourceline: 'deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-12 main' key_url: 'https://apt.llvm.org/llvm-snapshot.gpg.key' - sourceline: 'ppa:ubuntu-toolchain-r/test' packages: - clang-12 - cmake - gcc-11 - g++-11 - ninja-build homebrew: packages: - cmake - gcc@11 - llvm@12 - ninja update: true install: # The following Homebrew packages aren't linked by default, and need to be # prepended to the path explicitly. - if [ "$TRAVIS_OS_NAME" = "osx" ]; then export PATH="$(brew --prefix llvm)/bin:$PATH"; fi # /usr/bin/gcc points to an older compiler on both Linux and macOS. - if [ "$CXX" = "g++" ]; then export CXX="g++-11" CC="gcc-11"; fi # /usr/bin/clang points to an older compiler on both Linux and macOS. # # Homebrew's llvm package doesn't ship a versioned clang++ binary, so the values # below don't work on macOS. Fortunately, the path change above makes the # default values (clang and clang++) resolve to the correct compiler on macOS. - if [ "$TRAVIS_OS_NAME" = "linux" ]; then if [ "$CXX" = "clang++" ]; then export CXX="clang++-12" CC="clang-12"; fi; fi - echo ${CC} - echo ${CXX} - ${CXX} --version - cmake --version before_script: - mkdir -p build && cd build - cmake .. -G Ninja -DCRC32C_USE_GLOG=$GLOG -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DBUILD_SHARED_LIBS=$SHARED_LIB -DCMAKE_INSTALL_PREFIX=$HOME/.local - cmake --build . - cd .. script: - build/crc32c_tests - build/crc32c_capi_tests - build/crc32c_bench - cd build && cmake --build . --target install
0
bitcoin/src/crc32c/include
bitcoin/src/crc32c/include/crc32c/crc32c.h
/* Copyright 2017 The CRC32C Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. See the AUTHORS file for names of contributors. */ #ifndef CRC32C_CRC32C_H_ #define CRC32C_CRC32C_H_ /* The API exported by the CRC32C project. */ #if defined(__cplusplus) #include <cstddef> #include <cstdint> #include <string> #else /* !defined(__cplusplus) */ #include <stddef.h> #include <stdint.h> #endif /* !defined(__cplusplus) */ /* The C API. */ #if defined(__cplusplus) extern "C" { #endif /* defined(__cplusplus) */ /* Extends "crc" with the CRC32C of "count" bytes in the buffer pointed by "data" */ uint32_t crc32c_extend(uint32_t crc, const uint8_t* data, size_t count); /* Computes the CRC32C of "count" bytes in the buffer pointed by "data". */ uint32_t crc32c_value(const uint8_t* data, size_t count); #ifdef __cplusplus } /* end extern "C" */ #endif /* defined(__cplusplus) */ /* The C++ API. */ #if defined(__cplusplus) namespace crc32c { // Extends "crc" with the CRC32C of "count" bytes in the buffer pointed by // "data". uint32_t Extend(uint32_t crc, const uint8_t* data, size_t count); // Computes the CRC32C of "count" bytes in the buffer pointed by "data". inline uint32_t Crc32c(const uint8_t* data, size_t count) { return Extend(0, data, count); } // Computes the CRC32C of "count" bytes in the buffer pointed by "data". inline uint32_t Crc32c(const char* data, size_t count) { return Extend(0, reinterpret_cast<const uint8_t*>(data), count); } // Computes the CRC32C of the string's content. inline uint32_t Crc32c(const std::string& string) { return Crc32c(reinterpret_cast<const uint8_t*>(string.data()), string.size()); } } // namespace crc32c #if __cplusplus > 201402L #if __has_include(<string_view>) #include <string_view> namespace crc32c { // Computes the CRC32C of the bytes in the string_view. inline uint32_t Crc32c(const std::string_view& string_view) { return Crc32c(reinterpret_cast<const uint8_t*>(string_view.data()), string_view.size()); } } // namespace crc32c #endif // __has_include(<string_view>) #endif // __cplusplus > 201402L #endif /* defined(__cplusplus) */ #endif // CRC32C_CRC32C_H_
0
bitcoin/src/crc32c
bitcoin/src/crc32c/src/crc32c_arm64_check.h
// Copyright 2017 The CRC32C Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. See the AUTHORS file for names of contributors. // ARM-specific code checking for the availability of CRC32C instructions. #ifndef CRC32C_CRC32C_ARM_CHECK_H_ #define CRC32C_CRC32C_ARM_CHECK_H_ #include <cstddef> #include <cstdint> #ifdef CRC32C_HAVE_CONFIG_H #include "crc32c/crc32c_config.h" #endif #if HAVE_ARM64_CRC32C #ifdef __linux__ #if HAVE_STRONG_GETAUXVAL #include <sys/auxv.h> #elif HAVE_WEAK_GETAUXVAL // getauxval() is not available on Android until API level 20. Link it as a weak // symbol. extern "C" unsigned long getauxval(unsigned long type) __attribute__((weak)); #define AT_HWCAP 16 #endif // HAVE_STRONG_GETAUXVAL || HAVE_WEAK_GETAUXVAL #endif // defined (__linux__) #ifdef __APPLE__ #include <sys/types.h> #include <sys/sysctl.h> #endif // defined (__APPLE__) namespace crc32c { inline bool CanUseArm64Crc32() { #if defined (__linux__) && (HAVE_STRONG_GETAUXVAL || HAVE_WEAK_GETAUXVAL) // From 'arch/arm64/include/uapi/asm/hwcap.h' in Linux kernel source code. constexpr unsigned long kHWCAP_PMULL = 1 << 4; constexpr unsigned long kHWCAP_CRC32 = 1 << 7; unsigned long hwcap = #if HAVE_STRONG_GETAUXVAL // Some compilers warn on (&getauxval != nullptr) in the block below. getauxval(AT_HWCAP); #elif HAVE_WEAK_GETAUXVAL (&getauxval != nullptr) ? getauxval(AT_HWCAP) : 0; #else #error This is supposed to be nested inside a check for HAVE_*_GETAUXVAL. #endif // HAVE_STRONG_GETAUXVAL return (hwcap & (kHWCAP_PMULL | kHWCAP_CRC32)) == (kHWCAP_PMULL | kHWCAP_CRC32); #elif defined(__APPLE__) int val = 0; size_t len = sizeof(val); return sysctlbyname("hw.optional.armv8_crc32", &val, &len, nullptr, 0) == 0 && val != 0; #else return false; #endif // HAVE_STRONG_GETAUXVAL || HAVE_WEAK_GETAUXVAL } } // namespace crc32c #endif // HAVE_ARM64_CRC32C #endif // CRC32C_CRC32C_ARM_CHECK_H_
0
bitcoin/src/crc32c
bitcoin/src/crc32c/src/crc32c_test_main.cc
// Copyright 2017 The CRC32C Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifdef CRC32C_HAVE_CONFIG_H #include "crc32c/crc32c_config.h" #endif #include "gtest/gtest.h" #if CRC32C_TESTS_BUILT_WITH_GLOG #include "glog/logging.h" #endif // CRC32C_TESTS_BUILT_WITH_GLOG int main(int argc, char** argv) { #if CRC32C_TESTS_BUILT_WITH_GLOG google::InitGoogleLogging(argv[0]); google::InstallFailureSignalHandler(); #endif // CRC32C_TESTS_BUILT_WITH_GLOG testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }
0
bitcoin/src/crc32c
bitcoin/src/crc32c/src/crc32c_read_le_unittest.cc
// Copyright 2017 The CRC32C Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. See the AUTHORS file for names of contributors. #include "./crc32c_read_le.h" #include <cstddef> #include <cstdint> #include "gtest/gtest.h" #include "./crc32c_round_up.h" namespace crc32c { TEST(Crc32CReadLETest, ReadUint32LE) { // little-endian 0x12345678 alignas(4) uint8_t bytes[] = {0x78, 0x56, 0x34, 0x12}; ASSERT_EQ(RoundUp<4>(bytes), bytes) << "Stack array is not aligned"; EXPECT_EQ(static_cast<uint32_t>(0x12345678), ReadUint32LE(bytes)); } TEST(Crc32CReadLETest, ReadUint64LE) { // little-endian 0x123456789ABCDEF0 alignas(8) uint8_t bytes[] = {0xF0, 0xDE, 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12}; ASSERT_EQ(RoundUp<8>(bytes), bytes) << "Stack array is not aligned"; EXPECT_EQ(static_cast<uint64_t>(0x123456789ABCDEF0), ReadUint64LE(bytes)); } } // namespace crc32c
0
bitcoin/src/crc32c
bitcoin/src/crc32c/src/crc32c_internal.h
// Copyright 2017 The CRC32C Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. See the AUTHORS file for names of contributors. #ifndef CRC32C_CRC32C_INTERNAL_H_ #define CRC32C_CRC32C_INTERNAL_H_ // Internal functions that may change between releases. #include <cstddef> #include <cstdint> namespace crc32c { // Un-accelerated implementation that works on all CPUs. uint32_t ExtendPortable(uint32_t crc, const uint8_t* data, size_t count); // CRCs are pre- and post- conditioned by xoring with all ones. static constexpr const uint32_t kCRC32Xor = static_cast<uint32_t>(0xffffffffU); } // namespace crc32c #endif // CRC32C_CRC32C_INTERNAL_H_
0
bitcoin/src/crc32c
bitcoin/src/crc32c/src/crc32c_prefetch.h
// Copyright 2017 The CRC32C Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. See the AUTHORS file for names of contributors. #ifndef CRC32C_CRC32C_PREFETCH_H_ #define CRC32C_CRC32C_PREFETCH_H_ #include <cstddef> #include <cstdint> #ifdef CRC32C_HAVE_CONFIG_H #include "crc32c/crc32c_config.h" #endif #if HAVE_MM_PREFETCH #if defined(_MSC_VER) #include <intrin.h> #else // !defined(_MSC_VER) #include <xmmintrin.h> #endif // defined(_MSC_VER) #endif // HAVE_MM_PREFETCH namespace crc32c { // Ask the hardware to prefetch the data at the given address into the L1 cache. inline void RequestPrefetch(const uint8_t* address) { #if HAVE_BUILTIN_PREFETCH // Clang and GCC implement the __builtin_prefetch non-standard extension, // which maps to the best instruction on the target architecture. __builtin_prefetch(reinterpret_cast<const char*>(address), 0 /* Read only. */, 0 /* No temporal locality. */); #elif HAVE_MM_PREFETCH // Visual Studio doesn't implement __builtin_prefetch, but exposes the // PREFETCHNTA instruction via the _mm_prefetch intrinsic. _mm_prefetch(reinterpret_cast<const char*>(address), _MM_HINT_NTA); #else // No prefetch support. Silence compiler warnings. (void)address; #endif // HAVE_BUILTIN_PREFETCH } } // namespace crc32c #endif // CRC32C_CRC32C_ROUND_UP_H_
0
bitcoin/src/crc32c
bitcoin/src/crc32c/src/crc32c_arm64_unittest.cc
// Copyright 2017 The CRC32C Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. See the AUTHORS file for names of contributors. #include "gtest/gtest.h" #include "./crc32c_arm64.h" #include "./crc32c_extend_unittests.h" namespace crc32c { #if HAVE_ARM64_CRC32C struct Arm64TestTraits { static uint32_t Extend(uint32_t crc, const uint8_t* data, size_t count) { return ExtendArm64(crc, data, count); } }; INSTANTIATE_TYPED_TEST_SUITE_P(Arm64, ExtendTest, Arm64TestTraits); #endif // HAVE_ARM64_CRC32C } // namespace crc32c
0
bitcoin/src/crc32c
bitcoin/src/crc32c/src/crc32c_sse42_unittest.cc
// Copyright 2017 The CRC32C Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. See the AUTHORS file for names of contributors. #include "gtest/gtest.h" #include "./crc32c_extend_unittests.h" #include "./crc32c_sse42.h" namespace crc32c { #if HAVE_SSE42 && (defined(_M_X64) || defined(__x86_64__)) struct Sse42TestTraits { static uint32_t Extend(uint32_t crc, const uint8_t* data, size_t count) { return ExtendSse42(crc, data, count); } }; INSTANTIATE_TYPED_TEST_SUITE_P(Sse42, ExtendTest, Sse42TestTraits); #endif // HAVE_SSE42 && (defined(_M_X64) || defined(__x86_64__)) } // namespace crc32c
0
bitcoin/src/crc32c
bitcoin/src/crc32c/src/crc32c_unittest.cc
// Copyright 2017 The CRC32C Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. See the AUTHORS file for names of contributors. #include "crc32c/crc32c.h" #include <cstddef> #include <cstdint> #include <cstring> #include "gtest/gtest.h" #include "./crc32c_extend_unittests.h" TEST(Crc32CTest, Crc32c) { // From rfc3720 section B.4. uint8_t buf[32]; std::memset(buf, 0, sizeof(buf)); EXPECT_EQ(static_cast<uint32_t>(0x8a9136aa), crc32c::Crc32c(buf, sizeof(buf))); std::memset(buf, 0xff, sizeof(buf)); EXPECT_EQ(static_cast<uint32_t>(0x62a8ab43), crc32c::Crc32c(buf, sizeof(buf))); for (size_t i = 0; i < 32; ++i) buf[i] = static_cast<uint8_t>(i); EXPECT_EQ(static_cast<uint32_t>(0x46dd794e), crc32c::Crc32c(buf, sizeof(buf))); for (size_t i = 0; i < 32; ++i) buf[i] = static_cast<uint8_t>(31 - i); EXPECT_EQ(static_cast<uint32_t>(0x113fdb5c), crc32c::Crc32c(buf, sizeof(buf))); uint8_t data[48] = { 0x01, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x18, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; EXPECT_EQ(static_cast<uint32_t>(0xd9963a56), crc32c::Crc32c(data, sizeof(data))); } namespace crc32c { struct ApiTestTraits { static uint32_t Extend(uint32_t crc, const uint8_t* data, size_t count) { return ::crc32c::Extend(crc, data, count); } }; INSTANTIATE_TYPED_TEST_SUITE_P(Api, ExtendTest, ApiTestTraits); } // namespace crc32c TEST(CRC32CTest, Crc32cCharPointer) { char buf[32]; std::memset(buf, 0, sizeof(buf)); EXPECT_EQ(static_cast<uint32_t>(0x8a9136aa), crc32c::Crc32c(buf, sizeof(buf))); std::memset(buf, 0xff, sizeof(buf)); EXPECT_EQ(static_cast<uint32_t>(0x62a8ab43), crc32c::Crc32c(buf, sizeof(buf))); for (size_t i = 0; i < 32; ++i) buf[i] = static_cast<char>(i); EXPECT_EQ(static_cast<uint32_t>(0x46dd794e), crc32c::Crc32c(buf, sizeof(buf))); for (size_t i = 0; i < 32; ++i) buf[i] = static_cast<char>(31 - i); EXPECT_EQ(static_cast<uint32_t>(0x113fdb5c), crc32c::Crc32c(buf, sizeof(buf))); } TEST(CRC32CTest, Crc32cStdString) { std::string buf; buf.resize(32); for (size_t i = 0; i < 32; ++i) buf[i] = static_cast<char>(0x00); EXPECT_EQ(static_cast<uint32_t>(0x8a9136aa), crc32c::Crc32c(buf)); for (size_t i = 0; i < 32; ++i) buf[i] = '\xff'; EXPECT_EQ(static_cast<uint32_t>(0x62a8ab43), crc32c::Crc32c(buf)); for (size_t i = 0; i < 32; ++i) buf[i] = static_cast<char>(i); EXPECT_EQ(static_cast<uint32_t>(0x46dd794e), crc32c::Crc32c(buf)); for (size_t i = 0; i < 32; ++i) buf[i] = static_cast<char>(31 - i); EXPECT_EQ(static_cast<uint32_t>(0x113fdb5c), crc32c::Crc32c(buf)); } #if __cplusplus > 201402L #if __has_include(<string_view>) TEST(CRC32CTest, Crc32cStdStringView) { uint8_t buf[32]; std::string_view view(reinterpret_cast<const char*>(buf), sizeof(buf)); std::memset(buf, 0, sizeof(buf)); EXPECT_EQ(static_cast<uint32_t>(0x8a9136aa), crc32c::Crc32c(view)); std::memset(buf, 0xff, sizeof(buf)); EXPECT_EQ(static_cast<uint32_t>(0x62a8ab43), crc32c::Crc32c(view)); for (size_t i = 0; i < 32; ++i) buf[i] = static_cast<uint8_t>(i); EXPECT_EQ(static_cast<uint32_t>(0x46dd794e), crc32c::Crc32c(view)); for (size_t i = 0; i < 32; ++i) buf[i] = static_cast<uint8_t>(31 - i); EXPECT_EQ(static_cast<uint32_t>(0x113fdb5c), crc32c::Crc32c(view)); } #endif // __has_include(<string_view>) #endif // __cplusplus > 201402L #define TESTED_EXTEND Extend #include "./crc32c_extend_unittests.h" #undef TESTED_EXTEND
0
bitcoin/src/crc32c
bitcoin/src/crc32c/src/crc32c_arm64.h
// Copyright 2017 The CRC32C Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. See the AUTHORS file for names of contributors. // ARM-specific code #ifndef CRC32C_CRC32C_ARM_H_ #define CRC32C_CRC32C_ARM_H_ #include <cstddef> #include <cstdint> #ifdef CRC32C_HAVE_CONFIG_H #include "crc32c/crc32c_config.h" #endif #if HAVE_ARM64_CRC32C namespace crc32c { uint32_t ExtendArm64(uint32_t crc, const uint8_t* data, size_t count); } // namespace crc32c #endif // HAVE_ARM64_CRC32C #endif // CRC32C_CRC32C_ARM_H_
0
bitcoin/src/crc32c
bitcoin/src/crc32c/src/crc32c_capi_unittest.c
// Copyright 2017 The CRC32C Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. See the AUTHORS file for names of contributors. #include "crc32c/crc32c.h" #include <stddef.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { /* From rfc3720 section B.4. */ uint8_t buf[32]; memset(buf, 0, sizeof(buf)); if ((uint32_t)0x8a9136aa != crc32c_value(buf, sizeof(buf))) { printf("crc32c_value(zeros) test failed\n"); return 1; } memset(buf, 0xff, sizeof(buf)); if ((uint32_t)0x62a8ab43 != crc32c_value(buf, sizeof(buf))) { printf("crc32c_value(0xff) test failed\n"); return 1; } for (size_t i = 0; i < 32; ++i) buf[i] = (uint8_t)i; if ((uint32_t)0x46dd794e != crc32c_value(buf, sizeof(buf))) { printf("crc32c_value(0..31) test failed\n"); return 1; } for (size_t i = 0; i < 32; ++i) buf[i] = (uint8_t)(31 - i); if ((uint32_t)0x113fdb5c != crc32c_value(buf, sizeof(buf))) { printf("crc32c_value(31..0) test failed\n"); return 1; } uint8_t data[48] = { 0x01, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x18, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; if ((uint32_t)0xd9963a56 != crc32c_value(data, sizeof(data))) { printf("crc32c_value(31..0) test failed\n"); return 1; } const uint8_t* hello_space_world = (const uint8_t*)"hello world"; const uint8_t* hello_space = (const uint8_t*)"hello "; const uint8_t* world = (const uint8_t*)"world"; if (crc32c_value(hello_space_world, 11) != crc32c_extend(crc32c_value(hello_space, 6), world, 5)) { printf("crc32c_extend test failed\n"); return 1; } printf("All tests passed\n"); return 0; }
0
bitcoin/src/crc32c
bitcoin/src/crc32c/src/crc32c_prefetch_unittest.cc
// Copyright 2017 The CRC32C Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. See the AUTHORS file for names of contributors. #include "./crc32c_prefetch.h" // There is no easy way to test cache prefetching. We can only test that the // crc32c_prefetch.h header compiles on its own, so it doesn't have any unstated // dependencies.
0
bitcoin/src/crc32c
bitcoin/src/crc32c/src/crc32c_sse42.h
// Copyright 2017 The CRC32C Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. See the AUTHORS file for names of contributors. #ifndef CRC32C_CRC32C_SSE42_H_ #define CRC32C_CRC32C_SSE42_H_ // X86-specific code. #include <cstddef> #include <cstdint> #ifdef CRC32C_HAVE_CONFIG_H #include "crc32c/crc32c_config.h" #endif // The hardware-accelerated implementation is only enabled for 64-bit builds, // because a straightforward 32-bit implementation actually runs slower than the // portable version. Most X86 machines are 64-bit nowadays, so it doesn't make // much sense to spend time building an optimized hardware-accelerated // implementation. #if HAVE_SSE42 && (defined(_M_X64) || defined(__x86_64__)) namespace crc32c { // SSE4.2-accelerated implementation in crc32c_sse42.cc uint32_t ExtendSse42(uint32_t crc, const uint8_t* data, size_t count); } // namespace crc32c #endif // HAVE_SSE42 && (defined(_M_X64) || defined(__x86_64__)) #endif // CRC32C_CRC32C_SSE42_H_
0
bitcoin/src/crc32c
bitcoin/src/crc32c/src/crc32c_portable_unittest.cc
// Copyright 2017 The CRC32C Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. See the AUTHORS file for names of contributors. #include "gtest/gtest.h" #include "./crc32c_extend_unittests.h" #include "./crc32c_internal.h" namespace crc32c { struct PortableTestTraits { static uint32_t Extend(uint32_t crc, const uint8_t* data, size_t count) { return ExtendPortable(crc, data, count); } }; INSTANTIATE_TYPED_TEST_SUITE_P(Portable, ExtendTest, PortableTestTraits); } // namespace crc32c
0
bitcoin/src/crc32c
bitcoin/src/crc32c/src/crc32c_extend_unittests.h
// Copyright 2017 The CRC32C Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. See the AUTHORS file for names of contributors. #ifndef CRC32C_CRC32C_EXTEND_UNITTESTS_H_ #define CRC32C_CRC32C_EXTEND_UNITTESTS_H_ #include <cstddef> #include <cstdint> #include <cstring> #include "gtest/gtest.h" // Common test cases for all implementations of CRC32C_Extend(). namespace crc32c { template<typename TestTraits> class ExtendTest : public testing::Test {}; TYPED_TEST_SUITE_P(ExtendTest); TYPED_TEST_P(ExtendTest, StandardResults) { // From rfc3720 section B.4. uint8_t buf[32]; std::memset(buf, 0, sizeof(buf)); EXPECT_EQ(static_cast<uint32_t>(0x8a9136aa), TypeParam::Extend(0, buf, sizeof(buf))); std::memset(buf, 0xff, sizeof(buf)); EXPECT_EQ(static_cast<uint32_t>(0x62a8ab43), TypeParam::Extend(0, buf, sizeof(buf))); for (int i = 0; i < 32; ++i) buf[i] = static_cast<uint8_t>(i); EXPECT_EQ(static_cast<uint32_t>(0x46dd794e), TypeParam::Extend(0, buf, sizeof(buf))); for (int i = 0; i < 32; ++i) buf[i] = static_cast<uint8_t>(31 - i); EXPECT_EQ(static_cast<uint32_t>(0x113fdb5c), TypeParam::Extend(0, buf, sizeof(buf))); uint8_t data[48] = { 0x01, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x18, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; EXPECT_EQ(static_cast<uint32_t>(0xd9963a56), TypeParam::Extend(0, data, sizeof(data))); } TYPED_TEST_P(ExtendTest, HelloWorld) { const uint8_t* hello_space_world = reinterpret_cast<const uint8_t*>("hello world"); const uint8_t* hello_space = reinterpret_cast<const uint8_t*>("hello "); const uint8_t* world = reinterpret_cast<const uint8_t*>("world"); EXPECT_EQ(TypeParam::Extend(0, hello_space_world, 11), TypeParam::Extend(TypeParam::Extend(0, hello_space, 6), world, 5)); } TYPED_TEST_P(ExtendTest, BufferSlicing) { uint8_t buffer[48] = { 0x01, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x18, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; for (size_t i = 0; i < 48; ++i) { for (size_t j = i + 1; j <= 48; ++j) { uint32_t crc = 0; if (i > 0) crc = TypeParam::Extend(crc, buffer, i); crc = TypeParam::Extend(crc, buffer + i, j - i); if (j < 48) crc = TypeParam::Extend(crc, buffer + j, 48 - j); EXPECT_EQ(static_cast<uint32_t>(0xd9963a56), crc); } } } TYPED_TEST_P(ExtendTest, LargeBufferSlicing) { uint8_t buffer[2048]; for (size_t i = 0; i < 2048; i++) buffer[i] = static_cast<uint8_t>(3 * i * i + 7 * i + 11); for (size_t i = 0; i < 2048; ++i) { for (size_t j = i + 1; j <= 2048; ++j) { uint32_t crc = 0; if (i > 0) crc = TypeParam::Extend(crc, buffer, i); crc = TypeParam::Extend(crc, buffer + i, j - i); if (j < 2048) crc = TypeParam::Extend(crc, buffer + j, 2048 - j); EXPECT_EQ(static_cast<uint32_t>(0x36dcc753), crc); } } } REGISTER_TYPED_TEST_SUITE_P(ExtendTest, StandardResults, HelloWorld, BufferSlicing, LargeBufferSlicing); } // namespace crc32c #endif // CRC32C_CRC32C_EXTEND_UNITTESTS_H_
0
bitcoin/src/crc32c
bitcoin/src/crc32c/src/crc32c.cc
// Copyright 2017 The CRC32C Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. See the AUTHORS file for names of contributors. #include "crc32c/crc32c.h" #include <cstddef> #include <cstdint> #include "./crc32c_arm64.h" #include "./crc32c_arm64_check.h" #include "./crc32c_internal.h" #include "./crc32c_sse42.h" #include "./crc32c_sse42_check.h" namespace crc32c { uint32_t Extend(uint32_t crc, const uint8_t* data, size_t count) { #if HAVE_SSE42 && (defined(_M_X64) || defined(__x86_64__)) static bool can_use_sse42 = CanUseSse42(); if (can_use_sse42) return ExtendSse42(crc, data, count); #elif HAVE_ARM64_CRC32C static bool can_use_arm64_crc32 = CanUseArm64Crc32(); if (can_use_arm64_crc32) return ExtendArm64(crc, data, count); #endif // HAVE_SSE42 && (defined(_M_X64) || defined(__x86_64__)) return ExtendPortable(crc, data, count); } extern "C" uint32_t crc32c_extend(uint32_t crc, const uint8_t* data, size_t count) { return crc32c::Extend(crc, data, count); } extern "C" uint32_t crc32c_value(const uint8_t* data, size_t count) { return crc32c::Crc32c(data, count); } } // namespace crc32c
0
bitcoin/src/crc32c
bitcoin/src/crc32c/src/crc32c_sse42.cc
// Copyright 2008 The CRC32C Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. See the AUTHORS file for names of contributors. #include "./crc32c_sse42.h" // In a separate source file to allow this accelerated CRC32C function to be // compiled with the appropriate compiler flags to enable SSE4.2 instructions. // This implementation is loosely based on Intel Pub 323405 from April 2011, // "Fast CRC Computation for iSCSI Polynomial Using CRC32 Instruction". #include <cstddef> #include <cstdint> #include "./crc32c_internal.h" #include "./crc32c_prefetch.h" #include "./crc32c_read_le.h" #include "./crc32c_round_up.h" #ifdef CRC32C_HAVE_CONFIG_H #include "crc32c/crc32c_config.h" #endif #if HAVE_SSE42 && (defined(_M_X64) || defined(__x86_64__)) #if defined(_MSC_VER) #include <intrin.h> #else // !defined(_MSC_VER) #include <nmmintrin.h> #endif // defined(_MSC_VER) namespace crc32c { namespace { constexpr const ptrdiff_t kGroups = 3; constexpr const ptrdiff_t kBlock0Size = 16 * 1024 / kGroups / 64 * 64; constexpr const ptrdiff_t kBlock1Size = 4 * 1024 / kGroups / 8 * 8; constexpr const ptrdiff_t kBlock2Size = 1024 / kGroups / 8 * 8; const uint32_t kBlock0SkipTable[8][16] = { {0x00000000, 0xff770459, 0xfb027e43, 0x04757a1a, 0xf3e88a77, 0x0c9f8e2e, 0x08eaf434, 0xf79df06d, 0xe23d621f, 0x1d4a6646, 0x193f1c5c, 0xe6481805, 0x11d5e868, 0xeea2ec31, 0xead7962b, 0x15a09272}, {0x00000000, 0xc196b2cf, 0x86c1136f, 0x4757a1a0, 0x086e502f, 0xc9f8e2e0, 0x8eaf4340, 0x4f39f18f, 0x10dca05e, 0xd14a1291, 0x961db331, 0x578b01fe, 0x18b2f071, 0xd92442be, 0x9e73e31e, 0x5fe551d1}, {0x00000000, 0x21b940bc, 0x43728178, 0x62cbc1c4, 0x86e502f0, 0xa75c424c, 0xc5978388, 0xe42ec334, 0x08267311, 0x299f33ad, 0x4b54f269, 0x6aedb2d5, 0x8ec371e1, 0xaf7a315d, 0xcdb1f099, 0xec08b025}, {0x00000000, 0x104ce622, 0x2099cc44, 0x30d52a66, 0x41339888, 0x517f7eaa, 0x61aa54cc, 0x71e6b2ee, 0x82673110, 0x922bd732, 0xa2fefd54, 0xb2b21b76, 0xc354a998, 0xd3184fba, 0xe3cd65dc, 0xf38183fe}, {0x00000000, 0x012214d1, 0x024429a2, 0x03663d73, 0x04885344, 0x05aa4795, 0x06cc7ae6, 0x07ee6e37, 0x0910a688, 0x0832b259, 0x0b548f2a, 0x0a769bfb, 0x0d98f5cc, 0x0cbae11d, 0x0fdcdc6e, 0x0efec8bf}, {0x00000000, 0x12214d10, 0x24429a20, 0x3663d730, 0x48853440, 0x5aa47950, 0x6cc7ae60, 0x7ee6e370, 0x910a6880, 0x832b2590, 0xb548f2a0, 0xa769bfb0, 0xd98f5cc0, 0xcbae11d0, 0xfdcdc6e0, 0xefec8bf0}, {0x00000000, 0x27f8a7f1, 0x4ff14fe2, 0x6809e813, 0x9fe29fc4, 0xb81a3835, 0xd013d026, 0xf7eb77d7, 0x3a294979, 0x1dd1ee88, 0x75d8069b, 0x5220a16a, 0xa5cbd6bd, 0x8233714c, 0xea3a995f, 0xcdc23eae}, {0x00000000, 0x745292f2, 0xe8a525e4, 0x9cf7b716, 0xd4a63d39, 0xa0f4afcb, 0x3c0318dd, 0x48518a2f, 0xaca00c83, 0xd8f29e71, 0x44052967, 0x3057bb95, 0x780631ba, 0x0c54a348, 0x90a3145e, 0xe4f186ac}, }; const uint32_t kBlock1SkipTable[8][16] = { {0x00000000, 0x79113270, 0xf22264e0, 0x8b335690, 0xe1a8bf31, 0x98b98d41, 0x138adbd1, 0x6a9be9a1, 0xc6bd0893, 0xbfac3ae3, 0x349f6c73, 0x4d8e5e03, 0x2715b7a2, 0x5e0485d2, 0xd537d342, 0xac26e132}, {0x00000000, 0x889667d7, 0x14c0b95f, 0x9c56de88, 0x298172be, 0xa1171569, 0x3d41cbe1, 0xb5d7ac36, 0x5302e57c, 0xdb9482ab, 0x47c25c23, 0xcf543bf4, 0x7a8397c2, 0xf215f015, 0x6e432e9d, 0xe6d5494a}, {0x00000000, 0xa605caf8, 0x49e7e301, 0xefe229f9, 0x93cfc602, 0x35ca0cfa, 0xda282503, 0x7c2deffb, 0x2273faf5, 0x8476300d, 0x6b9419f4, 0xcd91d30c, 0xb1bc3cf7, 0x17b9f60f, 0xf85bdff6, 0x5e5e150e}, {0x00000000, 0x44e7f5ea, 0x89cfebd4, 0xcd281e3e, 0x1673a159, 0x529454b3, 0x9fbc4a8d, 0xdb5bbf67, 0x2ce742b2, 0x6800b758, 0xa528a966, 0xe1cf5c8c, 0x3a94e3eb, 0x7e731601, 0xb35b083f, 0xf7bcfdd5}, {0x00000000, 0x59ce8564, 0xb39d0ac8, 0xea538fac, 0x62d66361, 0x3b18e605, 0xd14b69a9, 0x8885eccd, 0xc5acc6c2, 0x9c6243a6, 0x7631cc0a, 0x2fff496e, 0xa77aa5a3, 0xfeb420c7, 0x14e7af6b, 0x4d292a0f}, {0x00000000, 0x8eb5fb75, 0x1887801b, 0x96327b6e, 0x310f0036, 0xbfbafb43, 0x2988802d, 0xa73d7b58, 0x621e006c, 0xecabfb19, 0x7a998077, 0xf42c7b02, 0x5311005a, 0xdda4fb2f, 0x4b968041, 0xc5237b34}, {0x00000000, 0xc43c00d8, 0x8d947741, 0x49a87799, 0x1ec49873, 0xdaf898ab, 0x9350ef32, 0x576cefea, 0x3d8930e6, 0xf9b5303e, 0xb01d47a7, 0x7421477f, 0x234da895, 0xe771a84d, 0xaed9dfd4, 0x6ae5df0c}, {0x00000000, 0x7b1261cc, 0xf624c398, 0x8d36a254, 0xe9a5f1c1, 0x92b7900d, 0x1f813259, 0x64935395, 0xd6a79573, 0xadb5f4bf, 0x208356eb, 0x5b913727, 0x3f0264b2, 0x4410057e, 0xc926a72a, 0xb234c6e6}, }; const uint32_t kBlock2SkipTable[8][16] = { {0x00000000, 0x8f158014, 0x1bc776d9, 0x94d2f6cd, 0x378eedb2, 0xb89b6da6, 0x2c499b6b, 0xa35c1b7f, 0x6f1ddb64, 0xe0085b70, 0x74daadbd, 0xfbcf2da9, 0x589336d6, 0xd786b6c2, 0x4354400f, 0xcc41c01b}, {0x00000000, 0xde3bb6c8, 0xb99b1b61, 0x67a0ada9, 0x76da4033, 0xa8e1f6fb, 0xcf415b52, 0x117aed9a, 0xedb48066, 0x338f36ae, 0x542f9b07, 0x8a142dcf, 0x9b6ec055, 0x4555769d, 0x22f5db34, 0xfcce6dfc}, {0x00000000, 0xde85763d, 0xb8e69a8b, 0x6663ecb6, 0x742143e7, 0xaaa435da, 0xccc7d96c, 0x1242af51, 0xe84287ce, 0x36c7f1f3, 0x50a41d45, 0x8e216b78, 0x9c63c429, 0x42e6b214, 0x24855ea2, 0xfa00289f}, {0x00000000, 0xd569796d, 0xaf3e842b, 0x7a57fd46, 0x5b917ea7, 0x8ef807ca, 0xf4affa8c, 0x21c683e1, 0xb722fd4e, 0x624b8423, 0x181c7965, 0xcd750008, 0xecb383e9, 0x39dafa84, 0x438d07c2, 0x96e47eaf}, {0x00000000, 0x6ba98c6d, 0xd75318da, 0xbcfa94b7, 0xab4a4745, 0xc0e3cb28, 0x7c195f9f, 0x17b0d3f2, 0x5378f87b, 0x38d17416, 0x842be0a1, 0xef826ccc, 0xf832bf3e, 0x939b3353, 0x2f61a7e4, 0x44c82b89}, {0x00000000, 0xa6f1f0f6, 0x480f971d, 0xeefe67eb, 0x901f2e3a, 0x36eedecc, 0xd810b927, 0x7ee149d1, 0x25d22a85, 0x8323da73, 0x6dddbd98, 0xcb2c4d6e, 0xb5cd04bf, 0x133cf449, 0xfdc293a2, 0x5b336354}, {0x00000000, 0x4ba4550a, 0x9748aa14, 0xdcecff1e, 0x2b7d22d9, 0x60d977d3, 0xbc3588cd, 0xf791ddc7, 0x56fa45b2, 0x1d5e10b8, 0xc1b2efa6, 0x8a16baac, 0x7d87676b, 0x36233261, 0xeacfcd7f, 0xa16b9875}, {0x00000000, 0xadf48b64, 0x5e056039, 0xf3f1eb5d, 0xbc0ac072, 0x11fe4b16, 0xe20fa04b, 0x4ffb2b2f, 0x7df9f615, 0xd00d7d71, 0x23fc962c, 0x8e081d48, 0xc1f33667, 0x6c07bd03, 0x9ff6565e, 0x3202dd3a}, }; constexpr const ptrdiff_t kPrefetchHorizon = 256; } // namespace uint32_t ExtendSse42(uint32_t crc, const uint8_t* data, size_t size) { const uint8_t* p = data; const uint8_t* e = data + size; uint32_t l = crc ^ kCRC32Xor; #define STEP1 \ do { \ l = _mm_crc32_u8(l, *p++); \ } while (0) #define STEP4(crc) \ do { \ crc = _mm_crc32_u32(crc, ReadUint32LE(p)); \ p += 4; \ } while (0) #define STEP8(crc, data) \ do { \ crc = _mm_crc32_u64(crc, ReadUint64LE(data)); \ data += 8; \ } while (0) #define STEP8BY3(crc0, crc1, crc2, p0, p1, p2) \ do { \ STEP8(crc0, p0); \ STEP8(crc1, p1); \ STEP8(crc2, p2); \ } while (0) #define STEP8X3(crc0, crc1, crc2, bs) \ do { \ crc0 = _mm_crc32_u64(crc0, ReadUint64LE(p)); \ crc1 = _mm_crc32_u64(crc1, ReadUint64LE(p + bs)); \ crc2 = _mm_crc32_u64(crc2, ReadUint64LE(p + 2 * bs)); \ p += 8; \ } while (0) #define SKIP_BLOCK(crc, tab) \ do { \ crc = tab[0][crc & 0xf] ^ tab[1][(crc >> 4) & 0xf] ^ \ tab[2][(crc >> 8) & 0xf] ^ tab[3][(crc >> 12) & 0xf] ^ \ tab[4][(crc >> 16) & 0xf] ^ tab[5][(crc >> 20) & 0xf] ^ \ tab[6][(crc >> 24) & 0xf] ^ tab[7][(crc >> 28) & 0xf]; \ } while (0) // Point x at first 8-byte aligned byte in the buffer. This might be past the // end of the buffer. const uint8_t* x = RoundUp<8>(p); if (x <= e) { // Process bytes p is 8-byte aligned. while (p != x) { STEP1; } } // Proccess the data in predetermined block sizes with tables for quickly // combining the checksum. Experimentally it's better to use larger block // sizes where possible so use a hierarchy of decreasing block sizes. uint64_t l64 = l; while ((e - p) >= kGroups * kBlock0Size) { uint64_t l641 = 0; uint64_t l642 = 0; for (int i = 0; i < kBlock0Size; i += 8 * 8) { // Prefetch ahead to hide latency. RequestPrefetch(p + kPrefetchHorizon); RequestPrefetch(p + kBlock0Size + kPrefetchHorizon); RequestPrefetch(p + 2 * kBlock0Size + kPrefetchHorizon); // Process 64 bytes at a time. STEP8X3(l64, l641, l642, kBlock0Size); STEP8X3(l64, l641, l642, kBlock0Size); STEP8X3(l64, l641, l642, kBlock0Size); STEP8X3(l64, l641, l642, kBlock0Size); STEP8X3(l64, l641, l642, kBlock0Size); STEP8X3(l64, l641, l642, kBlock0Size); STEP8X3(l64, l641, l642, kBlock0Size); STEP8X3(l64, l641, l642, kBlock0Size); } // Combine results. SKIP_BLOCK(l64, kBlock0SkipTable); l64 ^= l641; SKIP_BLOCK(l64, kBlock0SkipTable); l64 ^= l642; p += (kGroups - 1) * kBlock0Size; } while ((e - p) >= kGroups * kBlock1Size) { uint64_t l641 = 0; uint64_t l642 = 0; for (int i = 0; i < kBlock1Size; i += 8) { STEP8X3(l64, l641, l642, kBlock1Size); } SKIP_BLOCK(l64, kBlock1SkipTable); l64 ^= l641; SKIP_BLOCK(l64, kBlock1SkipTable); l64 ^= l642; p += (kGroups - 1) * kBlock1Size; } while ((e - p) >= kGroups * kBlock2Size) { uint64_t l641 = 0; uint64_t l642 = 0; for (int i = 0; i < kBlock2Size; i += 8) { STEP8X3(l64, l641, l642, kBlock2Size); } SKIP_BLOCK(l64, kBlock2SkipTable); l64 ^= l641; SKIP_BLOCK(l64, kBlock2SkipTable); l64 ^= l642; p += (kGroups - 1) * kBlock2Size; } // Process bytes 16 at a time while ((e - p) >= 16) { STEP8(l64, p); STEP8(l64, p); } l = static_cast<uint32_t>(l64); // Process the last few bytes. while (p != e) { STEP1; } #undef SKIP_BLOCK #undef STEP8X3 #undef STEP8BY3 #undef STEP8 #undef STEP4 #undef STEP1 return l ^ kCRC32Xor; } } // namespace crc32c #endif // HAVE_SSE42 && (defined(_M_X64) || defined(__x86_64__))
0
bitcoin/src/crc32c
bitcoin/src/crc32c/src/crc32c_benchmark.cc
// Copyright 2017 The CRC32C Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. See the AUTHORS file for names of contributors. #include <cstddef> #include <cstdint> #ifdef CRC32C_HAVE_CONFIG_H #include "crc32c/crc32c_config.h" #endif #include "benchmark/benchmark.h" #if CRC32C_TESTS_BUILT_WITH_GLOG #include "glog/logging.h" #endif // CRC32C_TESTS_BUILT_WITH_GLOG #include "./crc32c_arm64.h" #include "./crc32c_arm64_check.h" #include "./crc32c_internal.h" #include "./crc32c_sse42.h" #include "./crc32c_sse42_check.h" #include "crc32c/crc32c.h" class CRC32CBenchmark : public benchmark::Fixture { public: void SetUp(const benchmark::State& state) override { block_size_ = static_cast<size_t>(state.range(0)); block_data_ = std::string(block_size_, 'x'); block_buffer_ = reinterpret_cast<const uint8_t*>(block_data_.data()); } protected: std::string block_data_; const uint8_t* block_buffer_; size_t block_size_; }; BENCHMARK_DEFINE_F(CRC32CBenchmark, Public)(benchmark::State& state) { uint32_t crc = 0; for (auto _ : state) crc = crc32c::Extend(crc, block_buffer_, block_size_); state.SetBytesProcessed(state.iterations() * block_size_); } BENCHMARK_REGISTER_F(CRC32CBenchmark, Public) ->RangeMultiplier(16) ->Range(256, 16777216); // Block size. BENCHMARK_DEFINE_F(CRC32CBenchmark, Portable)(benchmark::State& state) { uint32_t crc = 0; for (auto _ : state) crc = crc32c::ExtendPortable(crc, block_buffer_, block_size_); state.SetBytesProcessed(state.iterations() * block_size_); } BENCHMARK_REGISTER_F(CRC32CBenchmark, Portable) ->RangeMultiplier(16) ->Range(256, 16777216); // Block size. #if HAVE_ARM64_CRC32C BENCHMARK_DEFINE_F(CRC32CBenchmark, ArmCRC32C)(benchmark::State& state) { if (!crc32c::CanUseArm64Crc32()) { state.SkipWithError("ARM CRC32C instructions not available or not enabled"); return; } uint32_t crc = 0; for (auto _ : state) crc = crc32c::ExtendArm64(crc, block_buffer_, block_size_); state.SetBytesProcessed(state.iterations() * block_size_); } BENCHMARK_REGISTER_F(CRC32CBenchmark, ArmCRC32C) ->RangeMultiplier(16) ->Range(256, 16777216); // Block size. #endif // HAVE_ARM64_CRC32C #if HAVE_SSE42 && (defined(_M_X64) || defined(__x86_64__)) BENCHMARK_DEFINE_F(CRC32CBenchmark, Sse42)(benchmark::State& state) { if (!crc32c::CanUseSse42()) { state.SkipWithError("SSE4.2 instructions not available or not enabled"); return; } uint32_t crc = 0; for (auto _ : state) crc = crc32c::ExtendSse42(crc, block_buffer_, block_size_); state.SetBytesProcessed(state.iterations() * block_size_); } BENCHMARK_REGISTER_F(CRC32CBenchmark, Sse42) ->RangeMultiplier(16) ->Range(256, 16777216); // Block size. #endif // HAVE_SSE42 && (defined(_M_X64) || defined(__x86_64__)) int main(int argc, char** argv) { #if CRC32C_TESTS_BUILT_WITH_GLOG google::InitGoogleLogging(argv[0]); google::InstallFailureSignalHandler(); #endif // CRC32C_TESTS_BUILT_WITH_GLOG benchmark::Initialize(&argc, argv); benchmark::RunSpecifiedBenchmarks(); return 0; }
0
bitcoin/src/crc32c
bitcoin/src/crc32c/src/crc32c_portable.cc
// Copyright 2008 The CRC32C Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. See the AUTHORS file for names of contributors. #include "./crc32c_internal.h" #include <cstddef> #include <cstdint> #include "./crc32c_prefetch.h" #include "./crc32c_read_le.h" #include "./crc32c_round_up.h" namespace { const uint32_t kByteExtensionTable[256] = { 0x00000000, 0xf26b8303, 0xe13b70f7, 0x1350f3f4, 0xc79a971f, 0x35f1141c, 0x26a1e7e8, 0xd4ca64eb, 0x8ad958cf, 0x78b2dbcc, 0x6be22838, 0x9989ab3b, 0x4d43cfd0, 0xbf284cd3, 0xac78bf27, 0x5e133c24, 0x105ec76f, 0xe235446c, 0xf165b798, 0x030e349b, 0xd7c45070, 0x25afd373, 0x36ff2087, 0xc494a384, 0x9a879fa0, 0x68ec1ca3, 0x7bbcef57, 0x89d76c54, 0x5d1d08bf, 0xaf768bbc, 0xbc267848, 0x4e4dfb4b, 0x20bd8ede, 0xd2d60ddd, 0xc186fe29, 0x33ed7d2a, 0xe72719c1, 0x154c9ac2, 0x061c6936, 0xf477ea35, 0xaa64d611, 0x580f5512, 0x4b5fa6e6, 0xb93425e5, 0x6dfe410e, 0x9f95c20d, 0x8cc531f9, 0x7eaeb2fa, 0x30e349b1, 0xc288cab2, 0xd1d83946, 0x23b3ba45, 0xf779deae, 0x05125dad, 0x1642ae59, 0xe4292d5a, 0xba3a117e, 0x4851927d, 0x5b016189, 0xa96ae28a, 0x7da08661, 0x8fcb0562, 0x9c9bf696, 0x6ef07595, 0x417b1dbc, 0xb3109ebf, 0xa0406d4b, 0x522bee48, 0x86e18aa3, 0x748a09a0, 0x67dafa54, 0x95b17957, 0xcba24573, 0x39c9c670, 0x2a993584, 0xd8f2b687, 0x0c38d26c, 0xfe53516f, 0xed03a29b, 0x1f682198, 0x5125dad3, 0xa34e59d0, 0xb01eaa24, 0x42752927, 0x96bf4dcc, 0x64d4cecf, 0x77843d3b, 0x85efbe38, 0xdbfc821c, 0x2997011f, 0x3ac7f2eb, 0xc8ac71e8, 0x1c661503, 0xee0d9600, 0xfd5d65f4, 0x0f36e6f7, 0x61c69362, 0x93ad1061, 0x80fde395, 0x72966096, 0xa65c047d, 0x5437877e, 0x4767748a, 0xb50cf789, 0xeb1fcbad, 0x197448ae, 0x0a24bb5a, 0xf84f3859, 0x2c855cb2, 0xdeeedfb1, 0xcdbe2c45, 0x3fd5af46, 0x7198540d, 0x83f3d70e, 0x90a324fa, 0x62c8a7f9, 0xb602c312, 0x44694011, 0x5739b3e5, 0xa55230e6, 0xfb410cc2, 0x092a8fc1, 0x1a7a7c35, 0xe811ff36, 0x3cdb9bdd, 0xceb018de, 0xdde0eb2a, 0x2f8b6829, 0x82f63b78, 0x709db87b, 0x63cd4b8f, 0x91a6c88c, 0x456cac67, 0xb7072f64, 0xa457dc90, 0x563c5f93, 0x082f63b7, 0xfa44e0b4, 0xe9141340, 0x1b7f9043, 0xcfb5f4a8, 0x3dde77ab, 0x2e8e845f, 0xdce5075c, 0x92a8fc17, 0x60c37f14, 0x73938ce0, 0x81f80fe3, 0x55326b08, 0xa759e80b, 0xb4091bff, 0x466298fc, 0x1871a4d8, 0xea1a27db, 0xf94ad42f, 0x0b21572c, 0xdfeb33c7, 0x2d80b0c4, 0x3ed04330, 0xccbbc033, 0xa24bb5a6, 0x502036a5, 0x4370c551, 0xb11b4652, 0x65d122b9, 0x97baa1ba, 0x84ea524e, 0x7681d14d, 0x2892ed69, 0xdaf96e6a, 0xc9a99d9e, 0x3bc21e9d, 0xef087a76, 0x1d63f975, 0x0e330a81, 0xfc588982, 0xb21572c9, 0x407ef1ca, 0x532e023e, 0xa145813d, 0x758fe5d6, 0x87e466d5, 0x94b49521, 0x66df1622, 0x38cc2a06, 0xcaa7a905, 0xd9f75af1, 0x2b9cd9f2, 0xff56bd19, 0x0d3d3e1a, 0x1e6dcdee, 0xec064eed, 0xc38d26c4, 0x31e6a5c7, 0x22b65633, 0xd0ddd530, 0x0417b1db, 0xf67c32d8, 0xe52cc12c, 0x1747422f, 0x49547e0b, 0xbb3ffd08, 0xa86f0efc, 0x5a048dff, 0x8ecee914, 0x7ca56a17, 0x6ff599e3, 0x9d9e1ae0, 0xd3d3e1ab, 0x21b862a8, 0x32e8915c, 0xc083125f, 0x144976b4, 0xe622f5b7, 0xf5720643, 0x07198540, 0x590ab964, 0xab613a67, 0xb831c993, 0x4a5a4a90, 0x9e902e7b, 0x6cfbad78, 0x7fab5e8c, 0x8dc0dd8f, 0xe330a81a, 0x115b2b19, 0x020bd8ed, 0xf0605bee, 0x24aa3f05, 0xd6c1bc06, 0xc5914ff2, 0x37faccf1, 0x69e9f0d5, 0x9b8273d6, 0x88d28022, 0x7ab90321, 0xae7367ca, 0x5c18e4c9, 0x4f48173d, 0xbd23943e, 0xf36e6f75, 0x0105ec76, 0x12551f82, 0xe03e9c81, 0x34f4f86a, 0xc69f7b69, 0xd5cf889d, 0x27a40b9e, 0x79b737ba, 0x8bdcb4b9, 0x988c474d, 0x6ae7c44e, 0xbe2da0a5, 0x4c4623a6, 0x5f16d052, 0xad7d5351}; const uint32_t kStrideExtensionTable0[256] = { 0x00000000, 0x30d23865, 0x61a470ca, 0x517648af, 0xc348e194, 0xf39ad9f1, 0xa2ec915e, 0x923ea93b, 0x837db5d9, 0xb3af8dbc, 0xe2d9c513, 0xd20bfd76, 0x4035544d, 0x70e76c28, 0x21912487, 0x11431ce2, 0x03171d43, 0x33c52526, 0x62b36d89, 0x526155ec, 0xc05ffcd7, 0xf08dc4b2, 0xa1fb8c1d, 0x9129b478, 0x806aa89a, 0xb0b890ff, 0xe1ced850, 0xd11ce035, 0x4322490e, 0x73f0716b, 0x228639c4, 0x125401a1, 0x062e3a86, 0x36fc02e3, 0x678a4a4c, 0x57587229, 0xc566db12, 0xf5b4e377, 0xa4c2abd8, 0x941093bd, 0x85538f5f, 0xb581b73a, 0xe4f7ff95, 0xd425c7f0, 0x461b6ecb, 0x76c956ae, 0x27bf1e01, 0x176d2664, 0x053927c5, 0x35eb1fa0, 0x649d570f, 0x544f6f6a, 0xc671c651, 0xf6a3fe34, 0xa7d5b69b, 0x97078efe, 0x8644921c, 0xb696aa79, 0xe7e0e2d6, 0xd732dab3, 0x450c7388, 0x75de4bed, 0x24a80342, 0x147a3b27, 0x0c5c750c, 0x3c8e4d69, 0x6df805c6, 0x5d2a3da3, 0xcf149498, 0xffc6acfd, 0xaeb0e452, 0x9e62dc37, 0x8f21c0d5, 0xbff3f8b0, 0xee85b01f, 0xde57887a, 0x4c692141, 0x7cbb1924, 0x2dcd518b, 0x1d1f69ee, 0x0f4b684f, 0x3f99502a, 0x6eef1885, 0x5e3d20e0, 0xcc0389db, 0xfcd1b1be, 0xada7f911, 0x9d75c174, 0x8c36dd96, 0xbce4e5f3, 0xed92ad5c, 0xdd409539, 0x4f7e3c02, 0x7fac0467, 0x2eda4cc8, 0x1e0874ad, 0x0a724f8a, 0x3aa077ef, 0x6bd63f40, 0x5b040725, 0xc93aae1e, 0xf9e8967b, 0xa89eded4, 0x984ce6b1, 0x890ffa53, 0xb9ddc236, 0xe8ab8a99, 0xd879b2fc, 0x4a471bc7, 0x7a9523a2, 0x2be36b0d, 0x1b315368, 0x096552c9, 0x39b76aac, 0x68c12203, 0x58131a66, 0xca2db35d, 0xfaff8b38, 0xab89c397, 0x9b5bfbf2, 0x8a18e710, 0xbacadf75, 0xebbc97da, 0xdb6eafbf, 0x49500684, 0x79823ee1, 0x28f4764e, 0x18264e2b, 0x18b8ea18, 0x286ad27d, 0x791c9ad2, 0x49cea2b7, 0xdbf00b8c, 0xeb2233e9, 0xba547b46, 0x8a864323, 0x9bc55fc1, 0xab1767a4, 0xfa612f0b, 0xcab3176e, 0x588dbe55, 0x685f8630, 0x3929ce9f, 0x09fbf6fa, 0x1baff75b, 0x2b7dcf3e, 0x7a0b8791, 0x4ad9bff4, 0xd8e716cf, 0xe8352eaa, 0xb9436605, 0x89915e60, 0x98d24282, 0xa8007ae7, 0xf9763248, 0xc9a40a2d, 0x5b9aa316, 0x6b489b73, 0x3a3ed3dc, 0x0aecebb9, 0x1e96d09e, 0x2e44e8fb, 0x7f32a054, 0x4fe09831, 0xddde310a, 0xed0c096f, 0xbc7a41c0, 0x8ca879a5, 0x9deb6547, 0xad395d22, 0xfc4f158d, 0xcc9d2de8, 0x5ea384d3, 0x6e71bcb6, 0x3f07f419, 0x0fd5cc7c, 0x1d81cddd, 0x2d53f5b8, 0x7c25bd17, 0x4cf78572, 0xdec92c49, 0xee1b142c, 0xbf6d5c83, 0x8fbf64e6, 0x9efc7804, 0xae2e4061, 0xff5808ce, 0xcf8a30ab, 0x5db49990, 0x6d66a1f5, 0x3c10e95a, 0x0cc2d13f, 0x14e49f14, 0x2436a771, 0x7540efde, 0x4592d7bb, 0xd7ac7e80, 0xe77e46e5, 0xb6080e4a, 0x86da362f, 0x97992acd, 0xa74b12a8, 0xf63d5a07, 0xc6ef6262, 0x54d1cb59, 0x6403f33c, 0x3575bb93, 0x05a783f6, 0x17f38257, 0x2721ba32, 0x7657f29d, 0x4685caf8, 0xd4bb63c3, 0xe4695ba6, 0xb51f1309, 0x85cd2b6c, 0x948e378e, 0xa45c0feb, 0xf52a4744, 0xc5f87f21, 0x57c6d61a, 0x6714ee7f, 0x3662a6d0, 0x06b09eb5, 0x12caa592, 0x22189df7, 0x736ed558, 0x43bced3d, 0xd1824406, 0xe1507c63, 0xb02634cc, 0x80f40ca9, 0x91b7104b, 0xa165282e, 0xf0136081, 0xc0c158e4, 0x52fff1df, 0x622dc9ba, 0x335b8115, 0x0389b970, 0x11ddb8d1, 0x210f80b4, 0x7079c81b, 0x40abf07e, 0xd2955945, 0xe2476120, 0xb331298f, 0x83e311ea, 0x92a00d08, 0xa272356d, 0xf3047dc2, 0xc3d645a7, 0x51e8ec9c, 0x613ad4f9, 0x304c9c56, 0x009ea433}; const uint32_t kStrideExtensionTable1[256] = { 0x00000000, 0x54075546, 0xa80eaa8c, 0xfc09ffca, 0x55f123e9, 0x01f676af, 0xfdff8965, 0xa9f8dc23, 0xabe247d2, 0xffe51294, 0x03eced5e, 0x57ebb818, 0xfe13643b, 0xaa14317d, 0x561dceb7, 0x021a9bf1, 0x5228f955, 0x062fac13, 0xfa2653d9, 0xae21069f, 0x07d9dabc, 0x53de8ffa, 0xafd77030, 0xfbd02576, 0xf9cabe87, 0xadcdebc1, 0x51c4140b, 0x05c3414d, 0xac3b9d6e, 0xf83cc828, 0x043537e2, 0x503262a4, 0xa451f2aa, 0xf056a7ec, 0x0c5f5826, 0x58580d60, 0xf1a0d143, 0xa5a78405, 0x59ae7bcf, 0x0da92e89, 0x0fb3b578, 0x5bb4e03e, 0xa7bd1ff4, 0xf3ba4ab2, 0x5a429691, 0x0e45c3d7, 0xf24c3c1d, 0xa64b695b, 0xf6790bff, 0xa27e5eb9, 0x5e77a173, 0x0a70f435, 0xa3882816, 0xf78f7d50, 0x0b86829a, 0x5f81d7dc, 0x5d9b4c2d, 0x099c196b, 0xf595e6a1, 0xa192b3e7, 0x086a6fc4, 0x5c6d3a82, 0xa064c548, 0xf463900e, 0x4d4f93a5, 0x1948c6e3, 0xe5413929, 0xb1466c6f, 0x18beb04c, 0x4cb9e50a, 0xb0b01ac0, 0xe4b74f86, 0xe6add477, 0xb2aa8131, 0x4ea37efb, 0x1aa42bbd, 0xb35cf79e, 0xe75ba2d8, 0x1b525d12, 0x4f550854, 0x1f676af0, 0x4b603fb6, 0xb769c07c, 0xe36e953a, 0x4a964919, 0x1e911c5f, 0xe298e395, 0xb69fb6d3, 0xb4852d22, 0xe0827864, 0x1c8b87ae, 0x488cd2e8, 0xe1740ecb, 0xb5735b8d, 0x497aa447, 0x1d7df101, 0xe91e610f, 0xbd193449, 0x4110cb83, 0x15179ec5, 0xbcef42e6, 0xe8e817a0, 0x14e1e86a, 0x40e6bd2c, 0x42fc26dd, 0x16fb739b, 0xeaf28c51, 0xbef5d917, 0x170d0534, 0x430a5072, 0xbf03afb8, 0xeb04fafe, 0xbb36985a, 0xef31cd1c, 0x133832d6, 0x473f6790, 0xeec7bbb3, 0xbac0eef5, 0x46c9113f, 0x12ce4479, 0x10d4df88, 0x44d38ace, 0xb8da7504, 0xecdd2042, 0x4525fc61, 0x1122a927, 0xed2b56ed, 0xb92c03ab, 0x9a9f274a, 0xce98720c, 0x32918dc6, 0x6696d880, 0xcf6e04a3, 0x9b6951e5, 0x6760ae2f, 0x3367fb69, 0x317d6098, 0x657a35de, 0x9973ca14, 0xcd749f52, 0x648c4371, 0x308b1637, 0xcc82e9fd, 0x9885bcbb, 0xc8b7de1f, 0x9cb08b59, 0x60b97493, 0x34be21d5, 0x9d46fdf6, 0xc941a8b0, 0x3548577a, 0x614f023c, 0x635599cd, 0x3752cc8b, 0xcb5b3341, 0x9f5c6607, 0x36a4ba24, 0x62a3ef62, 0x9eaa10a8, 0xcaad45ee, 0x3eced5e0, 0x6ac980a6, 0x96c07f6c, 0xc2c72a2a, 0x6b3ff609, 0x3f38a34f, 0xc3315c85, 0x973609c3, 0x952c9232, 0xc12bc774, 0x3d2238be, 0x69256df8, 0xc0ddb1db, 0x94dae49d, 0x68d31b57, 0x3cd44e11, 0x6ce62cb5, 0x38e179f3, 0xc4e88639, 0x90efd37f, 0x39170f5c, 0x6d105a1a, 0x9119a5d0, 0xc51ef096, 0xc7046b67, 0x93033e21, 0x6f0ac1eb, 0x3b0d94ad, 0x92f5488e, 0xc6f21dc8, 0x3afbe202, 0x6efcb744, 0xd7d0b4ef, 0x83d7e1a9, 0x7fde1e63, 0x2bd94b25, 0x82219706, 0xd626c240, 0x2a2f3d8a, 0x7e2868cc, 0x7c32f33d, 0x2835a67b, 0xd43c59b1, 0x803b0cf7, 0x29c3d0d4, 0x7dc48592, 0x81cd7a58, 0xd5ca2f1e, 0x85f84dba, 0xd1ff18fc, 0x2df6e736, 0x79f1b270, 0xd0096e53, 0x840e3b15, 0x7807c4df, 0x2c009199, 0x2e1a0a68, 0x7a1d5f2e, 0x8614a0e4, 0xd213f5a2, 0x7beb2981, 0x2fec7cc7, 0xd3e5830d, 0x87e2d64b, 0x73814645, 0x27861303, 0xdb8fecc9, 0x8f88b98f, 0x267065ac, 0x727730ea, 0x8e7ecf20, 0xda799a66, 0xd8630197, 0x8c6454d1, 0x706dab1b, 0x246afe5d, 0x8d92227e, 0xd9957738, 0x259c88f2, 0x719bddb4, 0x21a9bf10, 0x75aeea56, 0x89a7159c, 0xdda040da, 0x74589cf9, 0x205fc9bf, 0xdc563675, 0x88516333, 0x8a4bf8c2, 0xde4cad84, 0x2245524e, 0x76420708, 0xdfbadb2b, 0x8bbd8e6d, 0x77b471a7, 0x23b324e1}; const uint32_t kStrideExtensionTable2[256] = { 0x00000000, 0x678efd01, 0xcf1dfa02, 0xa8930703, 0x9bd782f5, 0xfc597ff4, 0x54ca78f7, 0x334485f6, 0x3243731b, 0x55cd8e1a, 0xfd5e8919, 0x9ad07418, 0xa994f1ee, 0xce1a0cef, 0x66890bec, 0x0107f6ed, 0x6486e636, 0x03081b37, 0xab9b1c34, 0xcc15e135, 0xff5164c3, 0x98df99c2, 0x304c9ec1, 0x57c263c0, 0x56c5952d, 0x314b682c, 0x99d86f2f, 0xfe56922e, 0xcd1217d8, 0xaa9cead9, 0x020fedda, 0x658110db, 0xc90dcc6c, 0xae83316d, 0x0610366e, 0x619ecb6f, 0x52da4e99, 0x3554b398, 0x9dc7b49b, 0xfa49499a, 0xfb4ebf77, 0x9cc04276, 0x34534575, 0x53ddb874, 0x60993d82, 0x0717c083, 0xaf84c780, 0xc80a3a81, 0xad8b2a5a, 0xca05d75b, 0x6296d058, 0x05182d59, 0x365ca8af, 0x51d255ae, 0xf94152ad, 0x9ecfafac, 0x9fc85941, 0xf846a440, 0x50d5a343, 0x375b5e42, 0x041fdbb4, 0x639126b5, 0xcb0221b6, 0xac8cdcb7, 0x97f7ee29, 0xf0791328, 0x58ea142b, 0x3f64e92a, 0x0c206cdc, 0x6bae91dd, 0xc33d96de, 0xa4b36bdf, 0xa5b49d32, 0xc23a6033, 0x6aa96730, 0x0d279a31, 0x3e631fc7, 0x59ede2c6, 0xf17ee5c5, 0x96f018c4, 0xf371081f, 0x94fff51e, 0x3c6cf21d, 0x5be20f1c, 0x68a68aea, 0x0f2877eb, 0xa7bb70e8, 0xc0358de9, 0xc1327b04, 0xa6bc8605, 0x0e2f8106, 0x69a17c07, 0x5ae5f9f1, 0x3d6b04f0, 0x95f803f3, 0xf276fef2, 0x5efa2245, 0x3974df44, 0x91e7d847, 0xf6692546, 0xc52da0b0, 0xa2a35db1, 0x0a305ab2, 0x6dbea7b3, 0x6cb9515e, 0x0b37ac5f, 0xa3a4ab5c, 0xc42a565d, 0xf76ed3ab, 0x90e02eaa, 0x387329a9, 0x5ffdd4a8, 0x3a7cc473, 0x5df23972, 0xf5613e71, 0x92efc370, 0xa1ab4686, 0xc625bb87, 0x6eb6bc84, 0x09384185, 0x083fb768, 0x6fb14a69, 0xc7224d6a, 0xa0acb06b, 0x93e8359d, 0xf466c89c, 0x5cf5cf9f, 0x3b7b329e, 0x2a03aaa3, 0x4d8d57a2, 0xe51e50a1, 0x8290ada0, 0xb1d42856, 0xd65ad557, 0x7ec9d254, 0x19472f55, 0x1840d9b8, 0x7fce24b9, 0xd75d23ba, 0xb0d3debb, 0x83975b4d, 0xe419a64c, 0x4c8aa14f, 0x2b045c4e, 0x4e854c95, 0x290bb194, 0x8198b697, 0xe6164b96, 0xd552ce60, 0xb2dc3361, 0x1a4f3462, 0x7dc1c963, 0x7cc63f8e, 0x1b48c28f, 0xb3dbc58c, 0xd455388d, 0xe711bd7b, 0x809f407a, 0x280c4779, 0x4f82ba78, 0xe30e66cf, 0x84809bce, 0x2c139ccd, 0x4b9d61cc, 0x78d9e43a, 0x1f57193b, 0xb7c41e38, 0xd04ae339, 0xd14d15d4, 0xb6c3e8d5, 0x1e50efd6, 0x79de12d7, 0x4a9a9721, 0x2d146a20, 0x85876d23, 0xe2099022, 0x878880f9, 0xe0067df8, 0x48957afb, 0x2f1b87fa, 0x1c5f020c, 0x7bd1ff0d, 0xd342f80e, 0xb4cc050f, 0xb5cbf3e2, 0xd2450ee3, 0x7ad609e0, 0x1d58f4e1, 0x2e1c7117, 0x49928c16, 0xe1018b15, 0x868f7614, 0xbdf4448a, 0xda7ab98b, 0x72e9be88, 0x15674389, 0x2623c67f, 0x41ad3b7e, 0xe93e3c7d, 0x8eb0c17c, 0x8fb73791, 0xe839ca90, 0x40aacd93, 0x27243092, 0x1460b564, 0x73ee4865, 0xdb7d4f66, 0xbcf3b267, 0xd972a2bc, 0xbefc5fbd, 0x166f58be, 0x71e1a5bf, 0x42a52049, 0x252bdd48, 0x8db8da4b, 0xea36274a, 0xeb31d1a7, 0x8cbf2ca6, 0x242c2ba5, 0x43a2d6a4, 0x70e65352, 0x1768ae53, 0xbffba950, 0xd8755451, 0x74f988e6, 0x137775e7, 0xbbe472e4, 0xdc6a8fe5, 0xef2e0a13, 0x88a0f712, 0x2033f011, 0x47bd0d10, 0x46bafbfd, 0x213406fc, 0x89a701ff, 0xee29fcfe, 0xdd6d7908, 0xbae38409, 0x1270830a, 0x75fe7e0b, 0x107f6ed0, 0x77f193d1, 0xdf6294d2, 0xb8ec69d3, 0x8ba8ec25, 0xec261124, 0x44b51627, 0x233beb26, 0x223c1dcb, 0x45b2e0ca, 0xed21e7c9, 0x8aaf1ac8, 0xb9eb9f3e, 0xde65623f, 0x76f6653c, 0x1178983d}; const uint32_t kStrideExtensionTable3[256] = { 0x00000000, 0xf20c0dfe, 0xe1f46d0d, 0x13f860f3, 0xc604aceb, 0x3408a115, 0x27f0c1e6, 0xd5fccc18, 0x89e52f27, 0x7be922d9, 0x6811422a, 0x9a1d4fd4, 0x4fe183cc, 0xbded8e32, 0xae15eec1, 0x5c19e33f, 0x162628bf, 0xe42a2541, 0xf7d245b2, 0x05de484c, 0xd0228454, 0x222e89aa, 0x31d6e959, 0xc3dae4a7, 0x9fc30798, 0x6dcf0a66, 0x7e376a95, 0x8c3b676b, 0x59c7ab73, 0xabcba68d, 0xb833c67e, 0x4a3fcb80, 0x2c4c517e, 0xde405c80, 0xcdb83c73, 0x3fb4318d, 0xea48fd95, 0x1844f06b, 0x0bbc9098, 0xf9b09d66, 0xa5a97e59, 0x57a573a7, 0x445d1354, 0xb6511eaa, 0x63add2b2, 0x91a1df4c, 0x8259bfbf, 0x7055b241, 0x3a6a79c1, 0xc866743f, 0xdb9e14cc, 0x29921932, 0xfc6ed52a, 0x0e62d8d4, 0x1d9ab827, 0xef96b5d9, 0xb38f56e6, 0x41835b18, 0x527b3beb, 0xa0773615, 0x758bfa0d, 0x8787f7f3, 0x947f9700, 0x66739afe, 0x5898a2fc, 0xaa94af02, 0xb96ccff1, 0x4b60c20f, 0x9e9c0e17, 0x6c9003e9, 0x7f68631a, 0x8d646ee4, 0xd17d8ddb, 0x23718025, 0x3089e0d6, 0xc285ed28, 0x17792130, 0xe5752cce, 0xf68d4c3d, 0x048141c3, 0x4ebe8a43, 0xbcb287bd, 0xaf4ae74e, 0x5d46eab0, 0x88ba26a8, 0x7ab62b56, 0x694e4ba5, 0x9b42465b, 0xc75ba564, 0x3557a89a, 0x26afc869, 0xd4a3c597, 0x015f098f, 0xf3530471, 0xe0ab6482, 0x12a7697c, 0x74d4f382, 0x86d8fe7c, 0x95209e8f, 0x672c9371, 0xb2d05f69, 0x40dc5297, 0x53243264, 0xa1283f9a, 0xfd31dca5, 0x0f3dd15b, 0x1cc5b1a8, 0xeec9bc56, 0x3b35704e, 0xc9397db0, 0xdac11d43, 0x28cd10bd, 0x62f2db3d, 0x90fed6c3, 0x8306b630, 0x710abbce, 0xa4f677d6, 0x56fa7a28, 0x45021adb, 0xb70e1725, 0xeb17f41a, 0x191bf9e4, 0x0ae39917, 0xf8ef94e9, 0x2d1358f1, 0xdf1f550f, 0xcce735fc, 0x3eeb3802, 0xb13145f8, 0x433d4806, 0x50c528f5, 0xa2c9250b, 0x7735e913, 0x8539e4ed, 0x96c1841e, 0x64cd89e0, 0x38d46adf, 0xcad86721, 0xd92007d2, 0x2b2c0a2c, 0xfed0c634, 0x0cdccbca, 0x1f24ab39, 0xed28a6c7, 0xa7176d47, 0x551b60b9, 0x46e3004a, 0xb4ef0db4, 0x6113c1ac, 0x931fcc52, 0x80e7aca1, 0x72eba15f, 0x2ef24260, 0xdcfe4f9e, 0xcf062f6d, 0x3d0a2293, 0xe8f6ee8b, 0x1afae375, 0x09028386, 0xfb0e8e78, 0x9d7d1486, 0x6f711978, 0x7c89798b, 0x8e857475, 0x5b79b86d, 0xa975b593, 0xba8dd560, 0x4881d89e, 0x14983ba1, 0xe694365f, 0xf56c56ac, 0x07605b52, 0xd29c974a, 0x20909ab4, 0x3368fa47, 0xc164f7b9, 0x8b5b3c39, 0x795731c7, 0x6aaf5134, 0x98a35cca, 0x4d5f90d2, 0xbf539d2c, 0xacabfddf, 0x5ea7f021, 0x02be131e, 0xf0b21ee0, 0xe34a7e13, 0x114673ed, 0xc4babff5, 0x36b6b20b, 0x254ed2f8, 0xd742df06, 0xe9a9e704, 0x1ba5eafa, 0x085d8a09, 0xfa5187f7, 0x2fad4bef, 0xdda14611, 0xce5926e2, 0x3c552b1c, 0x604cc823, 0x9240c5dd, 0x81b8a52e, 0x73b4a8d0, 0xa64864c8, 0x54446936, 0x47bc09c5, 0xb5b0043b, 0xff8fcfbb, 0x0d83c245, 0x1e7ba2b6, 0xec77af48, 0x398b6350, 0xcb876eae, 0xd87f0e5d, 0x2a7303a3, 0x766ae09c, 0x8466ed62, 0x979e8d91, 0x6592806f, 0xb06e4c77, 0x42624189, 0x519a217a, 0xa3962c84, 0xc5e5b67a, 0x37e9bb84, 0x2411db77, 0xd61dd689, 0x03e11a91, 0xf1ed176f, 0xe215779c, 0x10197a62, 0x4c00995d, 0xbe0c94a3, 0xadf4f450, 0x5ff8f9ae, 0x8a0435b6, 0x78083848, 0x6bf058bb, 0x99fc5545, 0xd3c39ec5, 0x21cf933b, 0x3237f3c8, 0xc03bfe36, 0x15c7322e, 0xe7cb3fd0, 0xf4335f23, 0x063f52dd, 0x5a26b1e2, 0xa82abc1c, 0xbbd2dcef, 0x49ded111, 0x9c221d09, 0x6e2e10f7, 0x7dd67004, 0x8fda7dfa}; constexpr const ptrdiff_t kPrefetchHorizon = 256; } // namespace namespace crc32c { uint32_t ExtendPortable(uint32_t crc, const uint8_t* data, size_t size) { const uint8_t* p = data; const uint8_t* e = p + size; uint32_t l = crc ^ kCRC32Xor; // Process one byte at a time. #define STEP1 \ do { \ int c = (l & 0xff) ^ *p++; \ l = kByteExtensionTable[c] ^ (l >> 8); \ } while (0) // Process one of the 4 strides of 4-byte data. #define STEP4(s) \ do { \ crc##s = ReadUint32LE(p + s * 4) ^ kStrideExtensionTable3[crc##s & 0xff] ^ \ kStrideExtensionTable2[(crc##s >> 8) & 0xff] ^ \ kStrideExtensionTable1[(crc##s >> 16) & 0xff] ^ \ kStrideExtensionTable0[crc##s >> 24]; \ } while (0) // Process a 16-byte swath of 4 strides, each of which has 4 bytes of data. #define STEP16 \ do { \ STEP4(0); \ STEP4(1); \ STEP4(2); \ STEP4(3); \ p += 16; \ } while (0) // Process 4 bytes that were already loaded into a word. #define STEP4W(w) \ do { \ w ^= l; \ for (size_t i = 0; i < 4; ++i) { \ w = (w >> 8) ^ kByteExtensionTable[w & 0xff]; \ } \ l = w; \ } while (0) // Point x at first 4-byte aligned byte in the buffer. This might be past the // end of the buffer. const uint8_t* x = RoundUp<4>(p); if (x <= e) { // Process bytes p is 4-byte aligned. while (p != x) { STEP1; } } if ((e - p) >= 16) { // Load a 16-byte swath into the stride partial results. uint32_t crc0 = ReadUint32LE(p + 0 * 4) ^ l; uint32_t crc1 = ReadUint32LE(p + 1 * 4); uint32_t crc2 = ReadUint32LE(p + 2 * 4); uint32_t crc3 = ReadUint32LE(p + 3 * 4); p += 16; while ((e - p) > kPrefetchHorizon) { RequestPrefetch(p + kPrefetchHorizon); // Process 64 bytes at a time. STEP16; STEP16; STEP16; STEP16; } // Process one 16-byte swath at a time. while ((e - p) >= 16) { STEP16; } // Advance one word at a time as far as possible. while ((e - p) >= 4) { STEP4(0); uint32_t tmp = crc0; crc0 = crc1; crc1 = crc2; crc2 = crc3; crc3 = tmp; p += 4; } // Combine the 4 partial stride results. l = 0; STEP4W(crc0); STEP4W(crc1); STEP4W(crc2); STEP4W(crc3); } // Process the last few bytes. while (p != e) { STEP1; } #undef STEP4W #undef STEP16 #undef STEP4 #undef STEP1 return l ^ kCRC32Xor; } } // namespace crc32c
0
bitcoin/src/crc32c
bitcoin/src/crc32c/src/crc32c_sse42_check.h
// Copyright 2017 The CRC32C Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. See the AUTHORS file for names of contributors. #ifndef CRC32C_CRC32C_SSE42_CHECK_H_ #define CRC32C_CRC32C_SSE42_CHECK_H_ // X86-specific code checking the availability of SSE4.2 instructions. #include <cstddef> #include <cstdint> #ifdef CRC32C_HAVE_CONFIG_H #include "crc32c/crc32c_config.h" #endif #if HAVE_SSE42 && (defined(_M_X64) || defined(__x86_64__)) // If the compiler supports SSE4.2, it definitely supports X86. #if defined(_MSC_VER) #include <intrin.h> namespace crc32c { inline bool CanUseSse42() { int cpu_info[4]; __cpuid(cpu_info, 1); return (cpu_info[2] & (1 << 20)) != 0; } } // namespace crc32c #else // !defined(_MSC_VER) #include <cpuid.h> namespace crc32c { inline bool CanUseSse42() { unsigned int eax, ebx, ecx, edx; return __get_cpuid(1, &eax, &ebx, &ecx, &edx) && ((ecx & (1 << 20)) != 0); } } // namespace crc32c #endif // defined(_MSC_VER) #endif // HAVE_SSE42 && (defined(_M_X64) || defined(__x86_64__)) #endif // CRC32C_CRC32C_SSE42_CHECK_H_
0
bitcoin/src/crc32c
bitcoin/src/crc32c/src/crc32c_round_up.h
// Copyright 2017 The CRC32C Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. See the AUTHORS file for names of contributors. #ifndef CRC32C_CRC32C_ROUND_UP_H_ #define CRC32C_CRC32C_ROUND_UP_H_ #include <cstddef> #include <cstdint> namespace crc32c { // Returns the smallest number >= the given number that is evenly divided by N. // // N must be a power of two. template <int N> constexpr inline uintptr_t RoundUp(uintptr_t pointer) { static_assert((N & (N - 1)) == 0, "N must be a power of two"); return (pointer + (N - 1)) & ~(N - 1); } // Returns the smallest address >= the given address that is aligned to N bytes. // // N must be a power of two. template <int N> constexpr inline const uint8_t* RoundUp(const uint8_t* pointer) { static_assert((N & (N - 1)) == 0, "N must be a power of two"); return reinterpret_cast<uint8_t*>( RoundUp<N>(reinterpret_cast<uintptr_t>(pointer))); } } // namespace crc32c #endif // CRC32C_CRC32C_ROUND_UP_H_
0
bitcoin/src/crc32c
bitcoin/src/crc32c/src/crc32c_round_up_unittest.cc
// Copyright 2017 The CRC32C Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. See the AUTHORS file for names of contributors. #include "./crc32c_round_up.h" #include <cstddef> #include <cstdint> #include "gtest/gtest.h" namespace crc32c { TEST(CRC32CRoundUpTest, RoundUpUintptr) { uintptr_t zero = 0; ASSERT_EQ(zero, RoundUp<1>(zero)); ASSERT_EQ(1U, RoundUp<1>(1U)); ASSERT_EQ(2U, RoundUp<1>(2U)); ASSERT_EQ(3U, RoundUp<1>(3U)); ASSERT_EQ(~static_cast<uintptr_t>(0), RoundUp<1>(~static_cast<uintptr_t>(0))); ASSERT_EQ(~static_cast<uintptr_t>(1), RoundUp<1>(~static_cast<uintptr_t>(1))); ASSERT_EQ(~static_cast<uintptr_t>(2), RoundUp<1>(~static_cast<uintptr_t>(2))); ASSERT_EQ(~static_cast<uintptr_t>(3), RoundUp<1>(~static_cast<uintptr_t>(3))); ASSERT_EQ(zero, RoundUp<2>(zero)); ASSERT_EQ(2U, RoundUp<2>(1U)); ASSERT_EQ(2U, RoundUp<2>(2U)); ASSERT_EQ(4U, RoundUp<2>(3U)); ASSERT_EQ(4U, RoundUp<2>(4U)); ASSERT_EQ(6U, RoundUp<2>(5U)); ASSERT_EQ(6U, RoundUp<2>(6U)); ASSERT_EQ(8U, RoundUp<2>(7U)); ASSERT_EQ(8U, RoundUp<2>(8U)); ASSERT_EQ(~static_cast<uintptr_t>(1), RoundUp<2>(~static_cast<uintptr_t>(1))); ASSERT_EQ(~static_cast<uintptr_t>(1), RoundUp<2>(~static_cast<uintptr_t>(2))); ASSERT_EQ(~static_cast<uintptr_t>(3), RoundUp<2>(~static_cast<uintptr_t>(3))); ASSERT_EQ(~static_cast<uintptr_t>(3), RoundUp<2>(~static_cast<uintptr_t>(4))); ASSERT_EQ(zero, RoundUp<4>(zero)); ASSERT_EQ(4U, RoundUp<4>(1U)); ASSERT_EQ(4U, RoundUp<4>(2U)); ASSERT_EQ(4U, RoundUp<4>(3U)); ASSERT_EQ(4U, RoundUp<4>(4U)); ASSERT_EQ(8U, RoundUp<4>(5U)); ASSERT_EQ(8U, RoundUp<4>(6U)); ASSERT_EQ(8U, RoundUp<4>(7U)); ASSERT_EQ(8U, RoundUp<4>(8U)); ASSERT_EQ(~static_cast<uintptr_t>(3), RoundUp<4>(~static_cast<uintptr_t>(3))); ASSERT_EQ(~static_cast<uintptr_t>(3), RoundUp<4>(~static_cast<uintptr_t>(4))); ASSERT_EQ(~static_cast<uintptr_t>(3), RoundUp<4>(~static_cast<uintptr_t>(5))); ASSERT_EQ(~static_cast<uintptr_t>(3), RoundUp<4>(~static_cast<uintptr_t>(6))); ASSERT_EQ(~static_cast<uintptr_t>(7), RoundUp<4>(~static_cast<uintptr_t>(7))); ASSERT_EQ(~static_cast<uintptr_t>(7), RoundUp<4>(~static_cast<uintptr_t>(8))); ASSERT_EQ(~static_cast<uintptr_t>(7), RoundUp<4>(~static_cast<uintptr_t>(9))); } TEST(CRC32CRoundUpTest, RoundUpPointer) { uintptr_t zero = 0, three = 3, four = 4, seven = 7, eight = 8; const uint8_t* zero_ptr = reinterpret_cast<const uint8_t*>(zero); const uint8_t* three_ptr = reinterpret_cast<const uint8_t*>(three); const uint8_t* four_ptr = reinterpret_cast<const uint8_t*>(four); const uint8_t* seven_ptr = reinterpret_cast<const uint8_t*>(seven); const uint8_t* eight_ptr = reinterpret_cast<uint8_t*>(eight); ASSERT_EQ(zero_ptr, RoundUp<1>(zero_ptr)); ASSERT_EQ(zero_ptr, RoundUp<4>(zero_ptr)); ASSERT_EQ(zero_ptr, RoundUp<8>(zero_ptr)); ASSERT_EQ(three_ptr, RoundUp<1>(three_ptr)); ASSERT_EQ(four_ptr, RoundUp<4>(three_ptr)); ASSERT_EQ(eight_ptr, RoundUp<8>(three_ptr)); ASSERT_EQ(four_ptr, RoundUp<1>(four_ptr)); ASSERT_EQ(four_ptr, RoundUp<4>(four_ptr)); ASSERT_EQ(eight_ptr, RoundUp<8>(four_ptr)); ASSERT_EQ(seven_ptr, RoundUp<1>(seven_ptr)); ASSERT_EQ(eight_ptr, RoundUp<4>(seven_ptr)); ASSERT_EQ(eight_ptr, RoundUp<8>(four_ptr)); } } // namespace crc32c
0
bitcoin/src/crc32c
bitcoin/src/crc32c/src/crc32c_read_le.h
// Copyright 2017 The CRC32C Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. See the AUTHORS file for names of contributors. #ifndef CRC32C_CRC32C_READ_LE_H_ #define CRC32C_CRC32C_READ_LE_H_ #include <cstdint> #include <cstring> #ifdef CRC32C_HAVE_CONFIG_H #include "crc32c/crc32c_config.h" #endif namespace crc32c { // Reads a little-endian 32-bit integer from a 32-bit-aligned buffer. inline uint32_t ReadUint32LE(const uint8_t* buffer) { #if BYTE_ORDER_BIG_ENDIAN return ((static_cast<uint32_t>(static_cast<uint8_t>(buffer[0]))) | (static_cast<uint32_t>(static_cast<uint8_t>(buffer[1])) << 8) | (static_cast<uint32_t>(static_cast<uint8_t>(buffer[2])) << 16) | (static_cast<uint32_t>(static_cast<uint8_t>(buffer[3])) << 24)); #else // !BYTE_ORDER_BIG_ENDIAN uint32_t result; // This should be optimized to a single instruction. std::memcpy(&result, buffer, sizeof(result)); return result; #endif // BYTE_ORDER_BIG_ENDIAN } // Reads a little-endian 64-bit integer from a 64-bit-aligned buffer. inline uint64_t ReadUint64LE(const uint8_t* buffer) { #if BYTE_ORDER_BIG_ENDIAN return ((static_cast<uint64_t>(static_cast<uint8_t>(buffer[0]))) | (static_cast<uint64_t>(static_cast<uint8_t>(buffer[1])) << 8) | (static_cast<uint64_t>(static_cast<uint8_t>(buffer[2])) << 16) | (static_cast<uint64_t>(static_cast<uint8_t>(buffer[3])) << 24) | (static_cast<uint64_t>(static_cast<uint8_t>(buffer[4])) << 32) | (static_cast<uint64_t>(static_cast<uint8_t>(buffer[5])) << 40) | (static_cast<uint64_t>(static_cast<uint8_t>(buffer[6])) << 48) | (static_cast<uint64_t>(static_cast<uint8_t>(buffer[7])) << 56)); #else // !BYTE_ORDER_BIG_ENDIAN uint64_t result; // This should be optimized to a single instruction. std::memcpy(&result, buffer, sizeof(result)); return result; #endif // BYTE_ORDER_BIG_ENDIAN } } // namespace crc32c #endif // CRC32C_CRC32C_READ_LE_H_
0
bitcoin/src/crc32c
bitcoin/src/crc32c/src/crc32c_config.h.in
// Copyright 2017 The CRC32C Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. See the AUTHORS file for names of contributors. #ifndef CRC32C_CRC32C_CONFIG_H_ #define CRC32C_CRC32C_CONFIG_H_ // Define to 1 if building for a big-endian platform. #cmakedefine01 BYTE_ORDER_BIG_ENDIAN // Define to 1 if the compiler has the __builtin_prefetch intrinsic. #cmakedefine01 HAVE_BUILTIN_PREFETCH // Define to 1 if targeting X86 and the compiler has the _mm_prefetch intrinsic. #cmakedefine01 HAVE_MM_PREFETCH // Define to 1 if targeting X86 and the compiler has the _mm_crc32_u{8,32,64} // intrinsics. #cmakedefine01 HAVE_SSE42 // Define to 1 if targeting ARM and the compiler has the __crc32c{b,h,w,d} and // the vmull_p64 intrinsics. #cmakedefine01 HAVE_ARM64_CRC32C // Define to 1 if the system libraries have the getauxval function in the // <sys/auxv.h> header. Should be true on Linux and Android API level 20+. #cmakedefine01 HAVE_STRONG_GETAUXVAL // Define to 1 if the compiler supports defining getauxval as a weak symbol. // Should be true for any compiler that supports __attribute__((weak)). #cmakedefine01 HAVE_WEAK_GETAUXVAL // Define to 1 if CRC32C tests have been built with Google Logging. #cmakedefine01 CRC32C_TESTS_BUILT_WITH_GLOG #endif // CRC32C_CRC32C_CONFIG_H_
0
bitcoin/src/crc32c
bitcoin/src/crc32c/src/crc32c_arm64.cc
// Copyright 2017 The CRC32C Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. See the AUTHORS file for names of contributors. #include "./crc32c_arm64.h" // In a separate source file to allow this accelerated CRC32C function to be // compiled with the appropriate compiler flags to enable ARM NEON CRC32C // instructions. // This implementation is based on https://github.com/google/leveldb/pull/490. #include <cstddef> #include <cstdint> #include "./crc32c_internal.h" #ifdef CRC32C_HAVE_CONFIG_H #include "crc32c/crc32c_config.h" #endif #if HAVE_ARM64_CRC32C #include <arm_acle.h> #include <arm_neon.h> #define KBYTES 1032 #define SEGMENTBYTES 256 // compute 8bytes for each segment parallelly #define CRC32C32BYTES(P, IND) \ do { \ crc1 = __crc32cd( \ crc1, *((const uint64_t *)(P) + (SEGMENTBYTES / 8) * 1 + (IND))); \ crc2 = __crc32cd( \ crc2, *((const uint64_t *)(P) + (SEGMENTBYTES / 8) * 2 + (IND))); \ crc3 = __crc32cd( \ crc3, *((const uint64_t *)(P) + (SEGMENTBYTES / 8) * 3 + (IND))); \ crc0 = __crc32cd( \ crc0, *((const uint64_t *)(P) + (SEGMENTBYTES / 8) * 0 + (IND))); \ } while (0); // compute 8*8 bytes for each segment parallelly #define CRC32C256BYTES(P, IND) \ do { \ CRC32C32BYTES((P), (IND)*8 + 0) \ CRC32C32BYTES((P), (IND)*8 + 1) \ CRC32C32BYTES((P), (IND)*8 + 2) \ CRC32C32BYTES((P), (IND)*8 + 3) \ CRC32C32BYTES((P), (IND)*8 + 4) \ CRC32C32BYTES((P), (IND)*8 + 5) \ CRC32C32BYTES((P), (IND)*8 + 6) \ CRC32C32BYTES((P), (IND)*8 + 7) \ } while (0); // compute 4*8*8 bytes for each segment parallelly #define CRC32C1024BYTES(P) \ do { \ CRC32C256BYTES((P), 0) \ CRC32C256BYTES((P), 1) \ CRC32C256BYTES((P), 2) \ CRC32C256BYTES((P), 3) \ (P) += 4 * SEGMENTBYTES; \ } while (0) namespace crc32c { uint32_t ExtendArm64(uint32_t crc, const uint8_t *data, size_t size) { int64_t length = size; uint32_t crc0, crc1, crc2, crc3; uint64_t t0, t1, t2; // k0=CRC(x^(3*SEGMENTBYTES*8)), k1=CRC(x^(2*SEGMENTBYTES*8)), // k2=CRC(x^(SEGMENTBYTES*8)) const poly64_t k0 = 0x8d96551c, k1 = 0xbd6f81f8, k2 = 0xdcb17aa4; crc = crc ^ kCRC32Xor; while (length >= KBYTES) { crc0 = crc; crc1 = 0; crc2 = 0; crc3 = 0; // Process 1024 bytes in parallel. CRC32C1024BYTES(data); // Merge the 4 partial CRC32C values. t2 = (uint64_t)vmull_p64(crc2, k2); t1 = (uint64_t)vmull_p64(crc1, k1); t0 = (uint64_t)vmull_p64(crc0, k0); crc = __crc32cd(crc3, *(uint64_t *)data); data += sizeof(uint64_t); crc ^= __crc32cd(0, t2); crc ^= __crc32cd(0, t1); crc ^= __crc32cd(0, t0); length -= KBYTES; } while (length >= 8) { crc = __crc32cd(crc, *(uint64_t *)data); data += 8; length -= 8; } if (length & 4) { crc = __crc32cw(crc, *(uint32_t *)data); data += 4; } if (length & 2) { crc = __crc32ch(crc, *(uint16_t *)data); data += 2; } if (length & 1) { crc = __crc32cb(crc, *data); } return crc ^ kCRC32Xor; } } // namespace crc32c #endif // HAVE_ARM64_CRC32C
0
bitcoin/src
bitcoin/src/compat/stdin.h
// Copyright (c) 2018 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_COMPAT_STDIN_H #define BITCOIN_COMPAT_STDIN_H struct NoechoInst { NoechoInst(); ~NoechoInst(); }; #define NO_STDIN_ECHO() NoechoInst _no_echo bool StdinTerminal(); bool StdinReady(); #endif // BITCOIN_COMPAT_STDIN_H
0
bitcoin/src
bitcoin/src/compat/cpuid.h
// Copyright (c) 2017-2022 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_COMPAT_CPUID_H #define BITCOIN_COMPAT_CPUID_H #if defined(__x86_64__) || defined(__amd64__) || defined(__i386__) #define HAVE_GETCPUID #include <cpuid.h> #include <cstdint> // We can't use cpuid.h's __get_cpuid as it does not support subleafs. void static inline GetCPUID(uint32_t leaf, uint32_t subleaf, uint32_t& a, uint32_t& b, uint32_t& c, uint32_t& d) { #ifdef __GNUC__ __cpuid_count(leaf, subleaf, a, b, c, d); #else __asm__ ("cpuid" : "=a"(a), "=b"(b), "=c"(c), "=d"(d) : "0"(leaf), "2"(subleaf)); #endif } #endif // defined(__x86_64__) || defined(__amd64__) || defined(__i386__) #endif // BITCOIN_COMPAT_CPUID_H
0
bitcoin/src
bitcoin/src/compat/endian.h
// Copyright (c) 2014-2022 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_COMPAT_ENDIAN_H #define BITCOIN_COMPAT_ENDIAN_H #if defined(HAVE_CONFIG_H) #include <config/bitcoin-config.h> #endif #include <compat/byteswap.h> #include <cstdint> #if defined(HAVE_ENDIAN_H) #include <endian.h> #elif defined(HAVE_SYS_ENDIAN_H) #include <sys/endian.h> #endif #ifndef HAVE_CONFIG_H // While not technically a supported configuration, defaulting to defining these // DECLs when we were compiled without autotools makes it easier for other build // systems to build things like libbitcoinconsensus for strange targets. #ifdef htobe16 #define HAVE_DECL_HTOBE16 1 #endif #ifdef htole16 #define HAVE_DECL_HTOLE16 1 #endif #ifdef be16toh #define HAVE_DECL_BE16TOH 1 #endif #ifdef le16toh #define HAVE_DECL_LE16TOH 1 #endif #ifdef htobe32 #define HAVE_DECL_HTOBE32 1 #endif #ifdef htole32 #define HAVE_DECL_HTOLE32 1 #endif #ifdef be32toh #define HAVE_DECL_BE32TOH 1 #endif #ifdef le32toh #define HAVE_DECL_LE32TOH 1 #endif #ifdef htobe64 #define HAVE_DECL_HTOBE64 1 #endif #ifdef htole64 #define HAVE_DECL_HTOLE64 1 #endif #ifdef be64toh #define HAVE_DECL_BE64TOH 1 #endif #ifdef le64toh #define HAVE_DECL_LE64TOH 1 #endif #endif // HAVE_CONFIG_H #if defined(WORDS_BIGENDIAN) #if HAVE_DECL_HTOBE16 == 0 inline uint16_t htobe16(uint16_t host_16bits) { return host_16bits; } #endif // HAVE_DECL_HTOBE16 #if HAVE_DECL_HTOLE16 == 0 inline uint16_t htole16(uint16_t host_16bits) { return bswap_16(host_16bits); } #endif // HAVE_DECL_HTOLE16 #if HAVE_DECL_BE16TOH == 0 inline uint16_t be16toh(uint16_t big_endian_16bits) { return big_endian_16bits; } #endif // HAVE_DECL_BE16TOH #if HAVE_DECL_LE16TOH == 0 inline uint16_t le16toh(uint16_t little_endian_16bits) { return bswap_16(little_endian_16bits); } #endif // HAVE_DECL_LE16TOH #if HAVE_DECL_HTOBE32 == 0 inline uint32_t htobe32(uint32_t host_32bits) { return host_32bits; } #endif // HAVE_DECL_HTOBE32 #if HAVE_DECL_HTOLE32 == 0 inline uint32_t htole32(uint32_t host_32bits) { return bswap_32(host_32bits); } #endif // HAVE_DECL_HTOLE32 #if HAVE_DECL_BE32TOH == 0 inline uint32_t be32toh(uint32_t big_endian_32bits) { return big_endian_32bits; } #endif // HAVE_DECL_BE32TOH #if HAVE_DECL_LE32TOH == 0 inline uint32_t le32toh(uint32_t little_endian_32bits) { return bswap_32(little_endian_32bits); } #endif // HAVE_DECL_LE32TOH #if HAVE_DECL_HTOBE64 == 0 inline uint64_t htobe64(uint64_t host_64bits) { return host_64bits; } #endif // HAVE_DECL_HTOBE64 #if HAVE_DECL_HTOLE64 == 0 inline uint64_t htole64(uint64_t host_64bits) { return bswap_64(host_64bits); } #endif // HAVE_DECL_HTOLE64 #if HAVE_DECL_BE64TOH == 0 inline uint64_t be64toh(uint64_t big_endian_64bits) { return big_endian_64bits; } #endif // HAVE_DECL_BE64TOH #if HAVE_DECL_LE64TOH == 0 inline uint64_t le64toh(uint64_t little_endian_64bits) { return bswap_64(little_endian_64bits); } #endif // HAVE_DECL_LE64TOH #else // WORDS_BIGENDIAN #if HAVE_DECL_HTOBE16 == 0 inline uint16_t htobe16(uint16_t host_16bits) { return bswap_16(host_16bits); } #endif // HAVE_DECL_HTOBE16 #if HAVE_DECL_HTOLE16 == 0 inline uint16_t htole16(uint16_t host_16bits) { return host_16bits; } #endif // HAVE_DECL_HTOLE16 #if HAVE_DECL_BE16TOH == 0 inline uint16_t be16toh(uint16_t big_endian_16bits) { return bswap_16(big_endian_16bits); } #endif // HAVE_DECL_BE16TOH #if HAVE_DECL_LE16TOH == 0 inline uint16_t le16toh(uint16_t little_endian_16bits) { return little_endian_16bits; } #endif // HAVE_DECL_LE16TOH #if HAVE_DECL_HTOBE32 == 0 inline uint32_t htobe32(uint32_t host_32bits) { return bswap_32(host_32bits); } #endif // HAVE_DECL_HTOBE32 #if HAVE_DECL_HTOLE32 == 0 inline uint32_t htole32(uint32_t host_32bits) { return host_32bits; } #endif // HAVE_DECL_HTOLE32 #if HAVE_DECL_BE32TOH == 0 inline uint32_t be32toh(uint32_t big_endian_32bits) { return bswap_32(big_endian_32bits); } #endif // HAVE_DECL_BE32TOH #if HAVE_DECL_LE32TOH == 0 inline uint32_t le32toh(uint32_t little_endian_32bits) { return little_endian_32bits; } #endif // HAVE_DECL_LE32TOH #if HAVE_DECL_HTOBE64 == 0 inline uint64_t htobe64(uint64_t host_64bits) { return bswap_64(host_64bits); } #endif // HAVE_DECL_HTOBE64 #if HAVE_DECL_HTOLE64 == 0 inline uint64_t htole64(uint64_t host_64bits) { return host_64bits; } #endif // HAVE_DECL_HTOLE64 #if HAVE_DECL_BE64TOH == 0 inline uint64_t be64toh(uint64_t big_endian_64bits) { return bswap_64(big_endian_64bits); } #endif // HAVE_DECL_BE64TOH #if HAVE_DECL_LE64TOH == 0 inline uint64_t le64toh(uint64_t little_endian_64bits) { return little_endian_64bits; } #endif // HAVE_DECL_LE64TOH #endif // WORDS_BIGENDIAN #endif // BITCOIN_COMPAT_ENDIAN_H
0
bitcoin/src
bitcoin/src/compat/stdin.cpp
// Copyright (c) 2018-2022 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include <compat/stdin.h> #include <cstdio> #ifdef WIN32 #include <windows.h> #include <io.h> #else #include <termios.h> #include <unistd.h> #include <poll.h> #endif // https://stackoverflow.com/questions/1413445/reading-a-password-from-stdcin void SetStdinEcho(bool enable) { #ifdef WIN32 HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); DWORD mode; GetConsoleMode(hStdin, &mode); if (!enable) { mode &= ~ENABLE_ECHO_INPUT; } else { mode |= ENABLE_ECHO_INPUT; } SetConsoleMode(hStdin, mode); #else struct termios tty; tcgetattr(STDIN_FILENO, &tty); if (!enable) { tty.c_lflag &= ~ECHO; } else { tty.c_lflag |= ECHO; } (void)tcsetattr(STDIN_FILENO, TCSANOW, &tty); #endif } bool StdinTerminal() { #ifdef WIN32 return _isatty(_fileno(stdin)); #else return isatty(fileno(stdin)); #endif } bool StdinReady() { if (!StdinTerminal()) { return true; } #ifdef WIN32 return false; #else struct pollfd fds; fds.fd = STDIN_FILENO; fds.events = POLLIN; return poll(&fds, 1, 0) == 1; #endif } NoechoInst::NoechoInst() { SetStdinEcho(false); } NoechoInst::~NoechoInst() { SetStdinEcho(true); }
0
bitcoin/src
bitcoin/src/compat/byteswap.h
// Copyright (c) 2014-2022 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_COMPAT_BYTESWAP_H #define BITCOIN_COMPAT_BYTESWAP_H #if defined(HAVE_CONFIG_H) #include <config/bitcoin-config.h> #endif #include <cstdint> #if defined(HAVE_BYTESWAP_H) #include <byteswap.h> #endif #if defined(MAC_OSX) #include <libkern/OSByteOrder.h> #define bswap_16(x) OSSwapInt16(x) #define bswap_32(x) OSSwapInt32(x) #define bswap_64(x) OSSwapInt64(x) #else // Non-MacOS / non-Darwin #if HAVE_DECL_BSWAP_16 == 0 inline uint16_t bswap_16(uint16_t x) { return (x >> 8) | (x << 8); } #endif // HAVE_DECL_BSWAP16 == 0 #if HAVE_DECL_BSWAP_32 == 0 inline uint32_t bswap_32(uint32_t x) { return (((x & 0xff000000U) >> 24) | ((x & 0x00ff0000U) >> 8) | ((x & 0x0000ff00U) << 8) | ((x & 0x000000ffU) << 24)); } #endif // HAVE_DECL_BSWAP32 == 0 #if HAVE_DECL_BSWAP_64 == 0 inline uint64_t bswap_64(uint64_t x) { return (((x & 0xff00000000000000ull) >> 56) | ((x & 0x00ff000000000000ull) >> 40) | ((x & 0x0000ff0000000000ull) >> 24) | ((x & 0x000000ff00000000ull) >> 8) | ((x & 0x00000000ff000000ull) << 8) | ((x & 0x0000000000ff0000ull) << 24) | ((x & 0x000000000000ff00ull) << 40) | ((x & 0x00000000000000ffull) << 56)); } #endif // HAVE_DECL_BSWAP64 == 0 #endif // defined(MAC_OSX) #endif // BITCOIN_COMPAT_BYTESWAP_H
0