diff options
| author | Bitflash-sh <btf01@proton.me> | 2026-07-24 11:18:11 -0300 |
|---|---|---|
| committer | Bitflash-sh <btf01@proton.me> | 2026-07-24 11:18:11 -0300 |
| commit | 51d617e70f90a972185917ea0096c1ecef2bfa8b (patch) | |
| tree | 9fbbcd867de9de9d0776afdd02f594ba312c46eb /src/sha.h | |
Bitflash: fair-launch release
Satoshi's bitcoin-0.1.0 reborn with RandomX CPU proof of work, anonymous .btf
addressing over Nostr with CGNAT rendezvous, automatic peer discovery, a
headless Linux node, and Windows GUI wallet. Fair launch, no premine, 21M cap.
Diffstat (limited to 'src/sha.h')
| -rw-r--r-- | src/sha.h | 177 |
1 files changed, 177 insertions, 0 deletions
diff --git a/src/sha.h b/src/sha.h new file mode 100644 index 0000000..e3af7b4 --- /dev/null +++ b/src/sha.h | |||
| @@ -0,0 +1,177 @@ | |||
| 1 | // This file is public domain | ||
| 2 | // SHA routines extracted as a standalone file from: | ||
| 3 | // Crypto++: a C++ Class Library of Cryptographic Schemes | ||
| 4 | // Version 5.5.2 (9/24/2007) | ||
| 5 | // http://www.cryptopp.com | ||
| 6 | #ifndef CRYPTOPP_SHA_H | ||
| 7 | #define CRYPTOPP_SHA_H | ||
| 8 | #include <stdlib.h> | ||
| 9 | |||
| 10 | namespace CryptoPP | ||
| 11 | { | ||
| 12 | |||
| 13 | // | ||
| 14 | // Dependencies | ||
| 15 | // | ||
| 16 | |||
| 17 | typedef unsigned char byte; | ||
| 18 | typedef unsigned short word16; | ||
| 19 | typedef unsigned int word32; | ||
| 20 | #if defined(_MSC_VER) || defined(__BORLANDC__) | ||
| 21 | typedef unsigned __int64 word64; | ||
| 22 | #else | ||
| 23 | typedef unsigned long long word64; | ||
| 24 | #endif | ||
| 25 | |||
| 26 | template <class T> inline T rotlFixed(T x, unsigned int y) | ||
| 27 | { | ||
| 28 | assert(y < sizeof(T)*8); | ||
| 29 | return T((x<<y) | (x>>(sizeof(T)*8-y))); | ||
| 30 | } | ||
| 31 | |||
| 32 | template <class T> inline T rotrFixed(T x, unsigned int y) | ||
| 33 | { | ||
| 34 | assert(y < sizeof(T)*8); | ||
| 35 | return T((x>>y) | (x<<(sizeof(T)*8-y))); | ||
| 36 | } | ||
| 37 | |||
| 38 | // ************** endian reversal *************** | ||
| 39 | |||
| 40 | #ifdef _MSC_VER | ||
| 41 | #if _MSC_VER >= 1400 | ||
| 42 | #define CRYPTOPP_FAST_ROTATE(x) 1 | ||
| 43 | #elif _MSC_VER >= 1300 | ||
| 44 | #define CRYPTOPP_FAST_ROTATE(x) ((x) == 32 | (x) == 64) | ||
| 45 | #else | ||
| 46 | #define CRYPTOPP_FAST_ROTATE(x) ((x) == 32) | ||
| 47 | #endif | ||
| 48 | #elif (defined(__MWERKS__) && TARGET_CPU_PPC) || \ | ||
| 49 | (defined(__GNUC__) && (defined(_ARCH_PWR2) || defined(_ARCH_PWR) || defined(_ARCH_PPC) || defined(_ARCH_PPC64) || defined(_ARCH_COM))) | ||
| 50 | #define CRYPTOPP_FAST_ROTATE(x) ((x) == 32) | ||
| 51 | #elif defined(__GNUC__) && (CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X86) // depend on GCC's peephole optimization to generate rotate instructions | ||
| 52 | #define CRYPTOPP_FAST_ROTATE(x) 1 | ||
| 53 | #else | ||
| 54 | #define CRYPTOPP_FAST_ROTATE(x) 0 | ||
| 55 | #endif | ||
| 56 | |||
| 57 | inline byte ByteReverse(byte value) | ||
| 58 | { | ||
| 59 | return value; | ||
| 60 | } | ||
| 61 | |||
| 62 | inline word16 ByteReverse(word16 value) | ||
| 63 | { | ||
| 64 | #ifdef CRYPTOPP_BYTESWAP_AVAILABLE | ||
| 65 | return bswap_16(value); | ||
| 66 | #elif defined(_MSC_VER) && _MSC_VER >= 1300 | ||
| 67 | return _byteswap_ushort(value); | ||
| 68 | #else | ||
| 69 | return rotlFixed(value, 8U); | ||
| 70 | #endif | ||
| 71 | } | ||
| 72 | |||
| 73 | inline word32 ByteReverse(word32 value) | ||
| 74 | { | ||
| 75 | #if defined(__GNUC__) | ||
| 76 | __asm__ ("bswap %0" : "=r" (value) : "0" (value)); | ||
| 77 | return value; | ||
| 78 | #elif defined(CRYPTOPP_BYTESWAP_AVAILABLE) | ||
| 79 | return bswap_32(value); | ||
| 80 | #elif defined(__MWERKS__) && TARGET_CPU_PPC | ||
| 81 | return (word32)__lwbrx(&value,0); | ||
| 82 | #elif _MSC_VER >= 1400 || (_MSC_VER >= 1300 && !defined(_DLL)) | ||
| 83 | return _byteswap_ulong(value); | ||
| 84 | #elif CRYPTOPP_FAST_ROTATE(32) | ||
| 85 | // 5 instructions with rotate instruction, 9 without | ||
| 86 | return (rotrFixed(value, 8U) & 0xff00ff00) | (rotlFixed(value, 8U) & 0x00ff00ff); | ||
| 87 | #else | ||
| 88 | // 6 instructions with rotate instruction, 8 without | ||
| 89 | value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8); | ||
| 90 | return rotlFixed(value, 16U); | ||
| 91 | #endif | ||
| 92 | } | ||
| 93 | |||
| 94 | #ifdef WORD64_AVAILABLE | ||
| 95 | inline word64 ByteReverse(word64 value) | ||
| 96 | { | ||
| 97 | #if defined(__GNUC__) && defined(__x86_64__) | ||
| 98 | __asm__ ("bswap %0" : "=r" (value) : "0" (value)); | ||
| 99 | return value; | ||
| 100 | #elif defined(CRYPTOPP_BYTESWAP_AVAILABLE) | ||
| 101 | return bswap_64(value); | ||
| 102 | #elif defined(_MSC_VER) && _MSC_VER >= 1300 | ||
| 103 | return _byteswap_uint64(value); | ||
| 104 | #elif defined(CRYPTOPP_SLOW_WORD64) | ||
| 105 | return (word64(ByteReverse(word32(value))) << 32) | ByteReverse(word32(value>>32)); | ||
| 106 | #else | ||
| 107 | value = ((value & W64LIT(0xFF00FF00FF00FF00)) >> 8) | ((value & W64LIT(0x00FF00FF00FF00FF)) << 8); | ||
| 108 | value = ((value & W64LIT(0xFFFF0000FFFF0000)) >> 16) | ((value & W64LIT(0x0000FFFF0000FFFF)) << 16); | ||
| 109 | return rotlFixed(value, 32U); | ||
| 110 | #endif | ||
| 111 | } | ||
| 112 | #endif | ||
| 113 | |||
| 114 | |||
| 115 | // | ||
| 116 | // SHA | ||
| 117 | // | ||
| 118 | |||
| 119 | // http://www.weidai.com/scan-mirror/md.html#SHA-1 | ||
| 120 | class SHA1 | ||
| 121 | { | ||
| 122 | public: | ||
| 123 | typedef word32 HashWordType; | ||
| 124 | static void InitState(word32 *state); | ||
| 125 | static void Transform(word32 *digest, const word32 *data); | ||
| 126 | static const char * StaticAlgorithmName() {return "SHA-1";} | ||
| 127 | }; | ||
| 128 | |||
| 129 | typedef SHA1 SHA; // for backwards compatibility | ||
| 130 | |||
| 131 | // implements the SHA-256 standard | ||
| 132 | class SHA256 | ||
| 133 | { | ||
| 134 | public: | ||
| 135 | typedef word32 HashWordType; | ||
| 136 | static void InitState(word32 *state); | ||
| 137 | static void Transform(word32 *digest, const word32 *data); | ||
| 138 | static const char * StaticAlgorithmName() {return "SHA-256";} | ||
| 139 | }; | ||
| 140 | |||
| 141 | // implements the SHA-224 standard | ||
| 142 | class SHA224 | ||
| 143 | { | ||
| 144 | public: | ||
| 145 | typedef word32 HashWordType; | ||
| 146 | static void InitState(word32 *state); | ||
| 147 | static void Transform(word32 *digest, const word32 *data) {SHA256::Transform(digest, data);} | ||
| 148 | static const char * StaticAlgorithmName() {return "SHA-224";} | ||
| 149 | }; | ||
| 150 | |||
| 151 | #ifdef WORD64_AVAILABLE | ||
| 152 | |||
| 153 | // implements the SHA-512 standard | ||
| 154 | class SHA512 | ||
| 155 | { | ||
| 156 | public: | ||
| 157 | typedef word64 HashWordType; | ||
| 158 | static void InitState(word64 *state); | ||
| 159 | static void Transform(word64 *digest, const word64 *data); | ||
| 160 | static const char * StaticAlgorithmName() {return "SHA-512";} | ||
| 161 | }; | ||
| 162 | |||
| 163 | // implements the SHA-384 standard | ||
| 164 | class SHA384 | ||
| 165 | { | ||
| 166 | public: | ||
| 167 | typedef word64 HashWordType; | ||
| 168 | static void InitState(word64 *state); | ||
| 169 | static void Transform(word64 *digest, const word64 *data) {SHA512::Transform(digest, data);} | ||
| 170 | static const char * StaticAlgorithmName() {return "SHA-384";} | ||
| 171 | }; | ||
| 172 | |||
| 173 | #endif | ||
| 174 | |||
| 175 | } | ||
| 176 | |||
| 177 | #endif | ||