├── .gitignore ├── Makefile ├── LICENSE ├── hash ├── sha512.h ├── sha256.h ├── ripemd160.h ├── ripemd160.cpp ├── sha512.cpp ├── ripemd160_sse.cpp ├── sha256.cpp └── sha256_sse.cpp ├── README.md └── minikeyg.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | minikeyg 3 | 4 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | default: 2 | g++ -m64 -mssse3 -Wno-write-strings -O2 -o hash/ripemd160.o -c hash/ripemd160.cpp 3 | g++ -m64 -mssse3 -Wno-write-strings -O2 -o hash/sha256.o -c hash/sha256.cpp 4 | g++ -m64 -mssse3 -Wno-write-strings -O2 -o hash/ripemd160_sse.o -c hash/ripemd160_sse.cpp 5 | g++ -m64 -mssse3 -Wno-write-strings -O2 -o hash/sha256_sse.o -c hash/sha256_sse.cpp 6 | g++ -O3 -o minikeyg minikeyg.cpp hash/ripemd160.o hash/ripemd160_sse.o hash/sha256.o hash/sha256_sse.o -lpthread 7 | 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | “Commons Clause” License Condition v1.0 2 | 3 | The Software is provided to you by the Licensor under the License, as defined below, subject to the following condition. 4 | 5 | Without limiting other conditions in the License, the grant of rights under the License will not include, and the License does not grant to you, the right to Sell the Software. 6 | 7 | For purposes of the foregoing, "Sell" means practicing any or all of the rights granted to you under the License to provide to third parties, for a fee or other consideration (including without limitation fees for hosting or consulting/ support services related to the Software), a product or service whose value derives, entirely or substantially, from the functionality of the Software. Any license notice or attribution required by the License must also include this Commons Clause License Condition notice. 8 | 9 | Software: minikeyg 10 | 11 | License: Apache 2.0 12 | 13 | Licensor: Luis Alberto 14 | -------------------------------------------------------------------------------- /hash/sha512.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the VanitySearch distribution (https://github.com/JeanLucPons/VanitySearch). 3 | * Copyright (c) 2019 Jean Luc PONS. 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, version 3. 8 | * 9 | * This program is distributed in the hope that it will be useful, but 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | * General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | #ifndef SHA512_H 19 | #define SHA512_H 20 | #include 21 | 22 | void sha512(unsigned char *input, int length, unsigned char *digest); 23 | void pbkdf2_hmac_sha512(uint8_t *out, size_t outlen,const uint8_t *passwd, size_t passlen,const uint8_t *salt, size_t saltlen,uint64_t iter); 24 | void hmac_sha512(unsigned char *key, int key_length, unsigned char *message, int message_length, unsigned char *digest); 25 | 26 | std::string sha512_hex(unsigned char *digest); 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /hash/sha256.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the VanitySearch distribution (https://github.com/JeanLucPons/VanitySearch). 3 | * Copyright (c) 2019 Jean Luc PONS. 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, version 3. 8 | * 9 | * This program is distributed in the hope that it will be useful, but 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | * General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | #ifndef SHA256_H 19 | #define SHA256_H 20 | #include 21 | 22 | void sha256(uint8_t *input,size_t length, uint8_t *digest); 23 | void sha256_33(uint8_t *input, uint8_t *digest); 24 | void sha256_65(uint8_t *input, uint8_t *digest); 25 | void sha256_checksum(uint8_t *input, int length, uint8_t *checksum); 26 | void sha256sse_1B(uint32_t *i0, uint32_t *i1, uint32_t *i2, uint32_t *i3, 27 | uint8_t *d0, uint8_t *d1, uint8_t *d2, uint8_t *d3); 28 | void sha256sse_2B(uint32_t *i0, uint32_t *i1, uint32_t *i2, uint32_t *i3, 29 | uint8_t *d0, uint8_t *d1, uint8_t *d2, uint8_t *d3); 30 | void sha256sse_checksum(uint32_t *i0, uint32_t *i1, uint32_t *i2, uint32_t *i3, 31 | uint8_t *d0, uint8_t *d1, uint8_t *d2, uint8_t *d3); 32 | std::string sha256_hex(unsigned char *digest); 33 | void sha256sse_test(); 34 | 35 | #endif -------------------------------------------------------------------------------- /hash/ripemd160.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the VanitySearch distribution (https://github.com/JeanLucPons/VanitySearch). 3 | * Copyright (c) 2019 Jean Luc PONS. 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, version 3. 8 | * 9 | * This program is distributed in the hope that it will be useful, but 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | * General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | #ifndef RIPEMD160_H 19 | #define RIPEMD160_H 20 | 21 | #include 22 | #include 23 | #include 24 | 25 | /** A hasher class for RIPEMD-160. */ 26 | class CRIPEMD160 27 | { 28 | private: 29 | uint32_t s[5]; 30 | unsigned char buf[64]; 31 | uint64_t bytes; 32 | 33 | public: 34 | CRIPEMD160(); 35 | void Write(const unsigned char* data, size_t len); 36 | void Finalize(unsigned char hash[20]); 37 | }; 38 | 39 | void ripemd160(unsigned char *input,int length,unsigned char *digest); 40 | void ripemd160_32(unsigned char *input, unsigned char *digest); 41 | void ripemd160sse_32(uint8_t *i0, uint8_t *i1, uint8_t *i2, uint8_t *i3, 42 | uint8_t *d0, uint8_t *d1, uint8_t *d2, uint8_t *d3); 43 | void ripemd160sse_test(); 44 | std::string ripemd160_hex(unsigned char *digest); 45 | 46 | static inline bool ripemd160_comp_hash(uint8_t *h0, uint8_t *h1) { 47 | uint32_t *h0i = (uint32_t *)h0; 48 | uint32_t *h1i = (uint32_t *)h1; 49 | return (h0i[0] == h1i[0]) && 50 | (h0i[1] == h1i[1]) && 51 | (h0i[2] == h1i[2]) && 52 | (h0i[3] == h1i[3]) && 53 | (h0i[4] == h1i[4]); 54 | } 55 | 56 | #endif // RIPEMD160_H 57 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # minikeyg 2 | 3 | faster minikey generator for Casascius keys 4 | 5 | **Please read all this document before to use the program** 6 | 7 | ## Download 8 | 9 | To clone the repository: 10 | 11 | ``` 12 | git clone https://github.com/albertobsd/minikeyg.git 13 | ``` 14 | 15 | don't forget change to the minikeyg directory 16 | 17 | `cd minikeyg` 18 | 19 | 20 | ## How to build 21 | 22 | Just compile: 23 | 24 | ``` 25 | make 26 | ``` 27 | 28 | # usage 29 | 30 | This program only have a few possibles arguments 31 | 32 | ``` 33 | ./minikeyg -u 34 | ``` 35 | 36 | the `-u` option is for generate keys without the byte verification, `u` stand for `unverified` keys, if you don't specify the keys generated will be always valid with the byte verification. 37 | 38 | ``` 39 | ./minikeyg -s 22 40 | ``` 41 | 42 | the `-s 22` or `-s 30` option is for generate only keys of the specified size, `s` stand for `size`, if you don't specify this option the default value is 22 43 | 44 | ``` 45 | ./minikeyg -m SECUN34uVQKk3UkMBAiTLSoLTUWfwS 46 | ``` 47 | the `-m` option is for specify a star key this string need to fit in 22 or 30 characters `m` stand for `minikey`, if you don't specify this option the star value is chossen randomly 48 | 49 | ``` 50 | ./minikeyg -a 23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz 51 | ``` 52 | 53 | the `-a` option is for specify the alphabet to be use I hear that the Series 2 Coin don't have number 1 in his minikey, if you don't specify this optin the defaul alphabet is `123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz` 54 | 55 | ``` 56 | ./minikeyg -t 2 57 | ``` 58 | 59 | the `-t` option is for specify the thread number, default number of threads is 1. 60 | 61 | ``` 62 | ./minikeyg -r 63 | ``` 64 | 65 | the `-r` option is to generate just random keys without any special order, `r` stand for `random`. This option is OS dependent and only works with `/dev/urandom` 66 | 67 | Of course you can use all the option together to fit you needs. 68 | 69 | ## Just a key generator 70 | 71 | This program is just a generator, this generator is already build in keyhunt 72 | 73 | You need redirect the output og this program to brainflayer please check https://github.com/ryancdotorg/brainflayer 74 | 75 | ## Free Code 76 | 77 | This code is free of charge, see the licence for more details. https://github.com/albertobsd/minikeyg/blob/main/LICENSE 78 | 79 | This is a hobby for me but is still a lot of work. 80 | 81 | ## ¡Beta! 82 | 83 | This version is still a **beta** version, there are a lot of things that can be fail. And absoluly there are some bugs 84 | 85 | # Donations 86 | 87 | - BTC: 1Coffee1jV4gB5gaXfHgSHDz9xx9QSECVW 88 | - ETH: 0x6222978c984C22d21b11b5b6b0Dd839C75821069 89 | - DOGE: DKAG4g2HwVFCLzs7YWdgtcsK6v5jym1ErV 90 | 91 | All the donations will be use only for two things: 92 | 93 | - Buy coffee to develop from dusk till dawn 94 | - Get an affordable desktop computer with decent GPU, not highend, just to start the GPU version of all my programs 95 | -------------------------------------------------------------------------------- /hash/ripemd160.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the VanitySearch distribution (https://github.com/JeanLucPons/VanitySearch). 3 | * Copyright (c) 2019 Jean Luc PONS. 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, version 3. 8 | * 9 | * This program is distributed in the hope that it will be useful, but 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | * General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | #include "ripemd160.h" 19 | #include 20 | 21 | /// Internal RIPEMD-160 implementation. 22 | namespace _ripemd160 { 23 | 24 | /** Initialize RIPEMD-160 state. */ 25 | void inline Initialize(uint32_t* s) 26 | { 27 | s[0] = 0x67452301ul; 28 | s[1] = 0xEFCDAB89ul; 29 | s[2] = 0x98BADCFEul; 30 | s[3] = 0x10325476ul; 31 | s[4] = 0xC3D2E1F0ul; 32 | } 33 | 34 | #ifndef WIN64 35 | inline uint32_t _rotl(uint32_t x, uint8_t r) { 36 | asm("roll %1,%0" : "+r" (x) : "c" (r)); 37 | return x; 38 | } 39 | #endif 40 | 41 | #define ROL(x,n) _rotl(x,n) 42 | 43 | #define f1(x, y, z) (x ^ y ^ z) 44 | #define f2(x, y, z) ((x & y) | (~x & z)) 45 | #define f3(x, y, z) ((x | ~y) ^ z) 46 | #define f4(x, y, z) ((x & z) | (~z & y)) 47 | #define f5(x, y, z) (x ^ (y | ~z)) 48 | 49 | #define Round(a,b,c,d,e,f,x,k,r) \ 50 | a = ROL(a + f + x + k, r) + e; \ 51 | c = ROL(c, 10); 52 | 53 | #define R11(a,b,c,d,e,x,r) Round(a, b, c, d, e, f1(b, c, d), x, 0, r) 54 | #define R21(a,b,c,d,e,x,r) Round(a, b, c, d, e, f2(b, c, d), x, 0x5A827999ul, r) 55 | #define R31(a,b,c,d,e,x,r) Round(a, b, c, d, e, f3(b, c, d), x, 0x6ED9EBA1ul, r) 56 | #define R41(a,b,c,d,e,x,r) Round(a, b, c, d, e, f4(b, c, d), x, 0x8F1BBCDCul, r) 57 | #define R51(a,b,c,d,e,x,r) Round(a, b, c, d, e, f5(b, c, d), x, 0xA953FD4Eul, r) 58 | #define R12(a,b,c,d,e,x,r) Round(a, b, c, d, e, f5(b, c, d), x, 0x50A28BE6ul, r) 59 | #define R22(a,b,c,d,e,x,r) Round(a, b, c, d, e, f4(b, c, d), x, 0x5C4DD124ul, r) 60 | #define R32(a,b,c,d,e,x,r) Round(a, b, c, d, e, f3(b, c, d), x, 0x6D703EF3ul, r) 61 | #define R42(a,b,c,d,e,x,r) Round(a, b, c, d, e, f2(b, c, d), x, 0x7A6D76E9ul, r) 62 | #define R52(a,b,c,d,e,x,r) Round(a, b, c, d, e, f1(b, c, d), x, 0, r) 63 | 64 | /** Perform a RIPEMD-160 transformation, processing a 64-byte chunk. */ 65 | void Transform(uint32_t* s, const unsigned char* chunk) 66 | { 67 | uint32_t a1 = s[0], b1 = s[1], c1 = s[2], d1 = s[3], e1 = s[4]; 68 | uint32_t a2 = a1, b2 = b1, c2 = c1, d2 = d1, e2 = e1; 69 | uint32_t w[16]; 70 | memcpy(w,chunk,16*sizeof(uint32_t)); 71 | 72 | R11(a1, b1, c1, d1, e1, w[0], 11); 73 | R12(a2, b2, c2, d2, e2, w[5], 8); 74 | R11(e1, a1, b1, c1, d1, w[1], 14); 75 | R12(e2, a2, b2, c2, d2, w[14], 9); 76 | R11(d1, e1, a1, b1, c1, w[2], 15); 77 | R12(d2, e2, a2, b2, c2, w[7], 9); 78 | R11(c1, d1, e1, a1, b1, w[3], 12); 79 | R12(c2, d2, e2, a2, b2, w[0], 11); 80 | R11(b1, c1, d1, e1, a1, w[4], 5); 81 | R12(b2, c2, d2, e2, a2, w[9], 13); 82 | R11(a1, b1, c1, d1, e1, w[5], 8); 83 | R12(a2, b2, c2, d2, e2, w[2], 15); 84 | R11(e1, a1, b1, c1, d1, w[6], 7); 85 | R12(e2, a2, b2, c2, d2, w[11], 15); 86 | R11(d1, e1, a1, b1, c1, w[7], 9); 87 | R12(d2, e2, a2, b2, c2, w[4], 5); 88 | R11(c1, d1, e1, a1, b1, w[8], 11); 89 | R12(c2, d2, e2, a2, b2, w[13], 7); 90 | R11(b1, c1, d1, e1, a1, w[9], 13); 91 | R12(b2, c2, d2, e2, a2, w[6], 7); 92 | R11(a1, b1, c1, d1, e1, w[10], 14); 93 | R12(a2, b2, c2, d2, e2, w[15], 8); 94 | R11(e1, a1, b1, c1, d1, w[11], 15); 95 | R12(e2, a2, b2, c2, d2, w[8], 11); 96 | R11(d1, e1, a1, b1, c1, w[12], 6); 97 | R12(d2, e2, a2, b2, c2, w[1], 14); 98 | R11(c1, d1, e1, a1, b1, w[13], 7); 99 | R12(c2, d2, e2, a2, b2, w[10], 14); 100 | R11(b1, c1, d1, e1, a1, w[14], 9); 101 | R12(b2, c2, d2, e2, a2, w[3], 12); 102 | R11(a1, b1, c1, d1, e1, w[15], 8); 103 | R12(a2, b2, c2, d2, e2, w[12], 6); 104 | 105 | R21(e1, a1, b1, c1, d1, w[7], 7); 106 | R22(e2, a2, b2, c2, d2, w[6], 9); 107 | R21(d1, e1, a1, b1, c1, w[4], 6); 108 | R22(d2, e2, a2, b2, c2, w[11], 13); 109 | R21(c1, d1, e1, a1, b1, w[13], 8); 110 | R22(c2, d2, e2, a2, b2, w[3], 15); 111 | R21(b1, c1, d1, e1, a1, w[1], 13); 112 | R22(b2, c2, d2, e2, a2, w[7], 7); 113 | R21(a1, b1, c1, d1, e1, w[10], 11); 114 | R22(a2, b2, c2, d2, e2, w[0], 12); 115 | R21(e1, a1, b1, c1, d1, w[6], 9); 116 | R22(e2, a2, b2, c2, d2, w[13], 8); 117 | R21(d1, e1, a1, b1, c1, w[15], 7); 118 | R22(d2, e2, a2, b2, c2, w[5], 9); 119 | R21(c1, d1, e1, a1, b1, w[3], 15); 120 | R22(c2, d2, e2, a2, b2, w[10], 11); 121 | R21(b1, c1, d1, e1, a1, w[12], 7); 122 | R22(b2, c2, d2, e2, a2, w[14], 7); 123 | R21(a1, b1, c1, d1, e1, w[0], 12); 124 | R22(a2, b2, c2, d2, e2, w[15], 7); 125 | R21(e1, a1, b1, c1, d1, w[9], 15); 126 | R22(e2, a2, b2, c2, d2, w[8], 12); 127 | R21(d1, e1, a1, b1, c1, w[5], 9); 128 | R22(d2, e2, a2, b2, c2, w[12], 7); 129 | R21(c1, d1, e1, a1, b1, w[2], 11); 130 | R22(c2, d2, e2, a2, b2, w[4], 6); 131 | R21(b1, c1, d1, e1, a1, w[14], 7); 132 | R22(b2, c2, d2, e2, a2, w[9], 15); 133 | R21(a1, b1, c1, d1, e1, w[11], 13); 134 | R22(a2, b2, c2, d2, e2, w[1], 13); 135 | R21(e1, a1, b1, c1, d1, w[8], 12); 136 | R22(e2, a2, b2, c2, d2, w[2], 11); 137 | 138 | R31(d1, e1, a1, b1, c1, w[3], 11); 139 | R32(d2, e2, a2, b2, c2, w[15], 9); 140 | R31(c1, d1, e1, a1, b1, w[10], 13); 141 | R32(c2, d2, e2, a2, b2, w[5], 7); 142 | R31(b1, c1, d1, e1, a1, w[14], 6); 143 | R32(b2, c2, d2, e2, a2, w[1], 15); 144 | R31(a1, b1, c1, d1, e1, w[4], 7); 145 | R32(a2, b2, c2, d2, e2, w[3], 11); 146 | R31(e1, a1, b1, c1, d1, w[9], 14); 147 | R32(e2, a2, b2, c2, d2, w[7], 8); 148 | R31(d1, e1, a1, b1, c1, w[15], 9); 149 | R32(d2, e2, a2, b2, c2, w[14], 6); 150 | R31(c1, d1, e1, a1, b1, w[8], 13); 151 | R32(c2, d2, e2, a2, b2, w[6], 6); 152 | R31(b1, c1, d1, e1, a1, w[1], 15); 153 | R32(b2, c2, d2, e2, a2, w[9], 14); 154 | R31(a1, b1, c1, d1, e1, w[2], 14); 155 | R32(a2, b2, c2, d2, e2, w[11], 12); 156 | R31(e1, a1, b1, c1, d1, w[7], 8); 157 | R32(e2, a2, b2, c2, d2, w[8], 13); 158 | R31(d1, e1, a1, b1, c1, w[0], 13); 159 | R32(d2, e2, a2, b2, c2, w[12], 5); 160 | R31(c1, d1, e1, a1, b1, w[6], 6); 161 | R32(c2, d2, e2, a2, b2, w[2], 14); 162 | R31(b1, c1, d1, e1, a1, w[13], 5); 163 | R32(b2, c2, d2, e2, a2, w[10], 13); 164 | R31(a1, b1, c1, d1, e1, w[11], 12); 165 | R32(a2, b2, c2, d2, e2, w[0], 13); 166 | R31(e1, a1, b1, c1, d1, w[5], 7); 167 | R32(e2, a2, b2, c2, d2, w[4], 7); 168 | R31(d1, e1, a1, b1, c1, w[12], 5); 169 | R32(d2, e2, a2, b2, c2, w[13], 5); 170 | 171 | R41(c1, d1, e1, a1, b1, w[1], 11); 172 | R42(c2, d2, e2, a2, b2, w[8], 15); 173 | R41(b1, c1, d1, e1, a1, w[9], 12); 174 | R42(b2, c2, d2, e2, a2, w[6], 5); 175 | R41(a1, b1, c1, d1, e1, w[11], 14); 176 | R42(a2, b2, c2, d2, e2, w[4], 8); 177 | R41(e1, a1, b1, c1, d1, w[10], 15); 178 | R42(e2, a2, b2, c2, d2, w[1], 11); 179 | R41(d1, e1, a1, b1, c1, w[0], 14); 180 | R42(d2, e2, a2, b2, c2, w[3], 14); 181 | R41(c1, d1, e1, a1, b1, w[8], 15); 182 | R42(c2, d2, e2, a2, b2, w[11], 14); 183 | R41(b1, c1, d1, e1, a1, w[12], 9); 184 | R42(b2, c2, d2, e2, a2, w[15], 6); 185 | R41(a1, b1, c1, d1, e1, w[4], 8); 186 | R42(a2, b2, c2, d2, e2, w[0], 14); 187 | R41(e1, a1, b1, c1, d1, w[13], 9); 188 | R42(e2, a2, b2, c2, d2, w[5], 6); 189 | R41(d1, e1, a1, b1, c1, w[3], 14); 190 | R42(d2, e2, a2, b2, c2, w[12], 9); 191 | R41(c1, d1, e1, a1, b1, w[7], 5); 192 | R42(c2, d2, e2, a2, b2, w[2], 12); 193 | R41(b1, c1, d1, e1, a1, w[15], 6); 194 | R42(b2, c2, d2, e2, a2, w[13], 9); 195 | R41(a1, b1, c1, d1, e1, w[14], 8); 196 | R42(a2, b2, c2, d2, e2, w[9], 12); 197 | R41(e1, a1, b1, c1, d1, w[5], 6); 198 | R42(e2, a2, b2, c2, d2, w[7], 5); 199 | R41(d1, e1, a1, b1, c1, w[6], 5); 200 | R42(d2, e2, a2, b2, c2, w[10], 15); 201 | R41(c1, d1, e1, a1, b1, w[2], 12); 202 | R42(c2, d2, e2, a2, b2, w[14], 8); 203 | 204 | R51(b1, c1, d1, e1, a1, w[4], 9); 205 | R52(b2, c2, d2, e2, a2, w[12], 8); 206 | R51(a1, b1, c1, d1, e1, w[0], 15); 207 | R52(a2, b2, c2, d2, e2, w[15], 5); 208 | R51(e1, a1, b1, c1, d1, w[5], 5); 209 | R52(e2, a2, b2, c2, d2, w[10], 12); 210 | R51(d1, e1, a1, b1, c1, w[9], 11); 211 | R52(d2, e2, a2, b2, c2, w[4], 9); 212 | R51(c1, d1, e1, a1, b1, w[7], 6); 213 | R52(c2, d2, e2, a2, b2, w[1], 12); 214 | R51(b1, c1, d1, e1, a1, w[12], 8); 215 | R52(b2, c2, d2, e2, a2, w[5], 5); 216 | R51(a1, b1, c1, d1, e1, w[2], 13); 217 | R52(a2, b2, c2, d2, e2, w[8], 14); 218 | R51(e1, a1, b1, c1, d1, w[10], 12); 219 | R52(e2, a2, b2, c2, d2, w[7], 6); 220 | R51(d1, e1, a1, b1, c1, w[14], 5); 221 | R52(d2, e2, a2, b2, c2, w[6], 8); 222 | R51(c1, d1, e1, a1, b1, w[1], 12); 223 | R52(c2, d2, e2, a2, b2, w[2], 13); 224 | R51(b1, c1, d1, e1, a1, w[3], 13); 225 | R52(b2, c2, d2, e2, a2, w[13], 6); 226 | R51(a1, b1, c1, d1, e1, w[8], 14); 227 | R52(a2, b2, c2, d2, e2, w[14], 5); 228 | R51(e1, a1, b1, c1, d1, w[11], 11); 229 | R52(e2, a2, b2, c2, d2, w[0], 15); 230 | R51(d1, e1, a1, b1, c1, w[6], 8); 231 | R52(d2, e2, a2, b2, c2, w[3], 13); 232 | R51(c1, d1, e1, a1, b1, w[15], 5); 233 | R52(c2, d2, e2, a2, b2, w[9], 11); 234 | R51(b1, c1, d1, e1, a1, w[13], 6); 235 | R52(b2, c2, d2, e2, a2, w[11], 11); 236 | 237 | uint32_t t = s[0]; 238 | s[0] = s[1] + c1 + d2; 239 | s[1] = s[2] + d1 + e2; 240 | s[2] = s[3] + e1 + a2; 241 | s[3] = s[4] + a1 + b2; 242 | s[4] = t + b1 + c2; 243 | } 244 | 245 | } // namespace ripemd160 246 | 247 | CRIPEMD160::CRIPEMD160() : bytes(0) 248 | { 249 | _ripemd160::Initialize(s); 250 | } 251 | 252 | void CRIPEMD160::Write(const unsigned char* data, size_t len) 253 | { 254 | const unsigned char* end = data + len; 255 | size_t bufsize = bytes % 64; 256 | if (bufsize && bufsize + len >= 64) { 257 | // Fill the buffer, and process it. 258 | memcpy(buf + bufsize, data, 64 - bufsize); 259 | bytes += 64 - bufsize; 260 | data += 64 - bufsize; 261 | _ripemd160::Transform(s, buf); 262 | bufsize = 0; 263 | } 264 | while (end >= data + 64) { 265 | // Process full chunks directly from the source. 266 | _ripemd160::Transform(s, data); 267 | bytes += 64; 268 | data += 64; 269 | } 270 | if (end > data) { 271 | // Fill the buffer with what remains. 272 | memcpy(buf + bufsize, data, end - data); 273 | bytes += end - data; 274 | } 275 | } 276 | 277 | void CRIPEMD160::Finalize(unsigned char hash[20]) 278 | { 279 | static const unsigned char pad[64] = {0x80}; 280 | unsigned char sizedesc[8]; 281 | *(uint64_t *)sizedesc = bytes << 3; 282 | Write(pad, 1 + ((119 - (bytes % 64)) % 64)); 283 | Write(sizedesc, 8); 284 | memcpy(hash,s,20); 285 | } 286 | 287 | static const uint64_t sizedesc_32 = 32 << 3; 288 | static const unsigned char pad[64] = { 0x80 }; 289 | 290 | void ripemd160_32(unsigned char *input, unsigned char *digest) { 291 | 292 | uint32_t *s = (uint32_t *)digest; 293 | _ripemd160::Initialize(s); 294 | memcpy(input+32,pad,24); 295 | memcpy(input+56,&sizedesc_32,8); 296 | _ripemd160::Transform(s, input); 297 | 298 | } 299 | 300 | void ripemd160(unsigned char *input,int length,unsigned char *digest) { 301 | 302 | CRIPEMD160 cripe; 303 | cripe.Write(input,length); 304 | cripe.Finalize(digest); 305 | 306 | } 307 | 308 | std::string ripemd160_hex(unsigned char *digest) { 309 | 310 | char buf[2 * 20 + 1]; 311 | buf[2 * 20] = 0; 312 | for (int i = 0; i < 20; i++) 313 | sprintf(buf + i * 2, "%02x", (int)digest[i]); 314 | return std::string(buf); 315 | 316 | } 317 | -------------------------------------------------------------------------------- /hash/sha512.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the VanitySearch distribution (https://github.com/JeanLucPons/VanitySearch). 3 | * Copyright (c) 2019 Jean Luc PONS. 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, version 3. 8 | * 9 | * This program is distributed in the hope that it will be useful, but 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | * General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | #include 19 | #include "sha512.h" 20 | 21 | #define BSWAP 22 | #define SHA512_BLOCK_SIZE 128 23 | #define SHA512_HASH_LENGTH 64 24 | #define MIN(x,y) (xy)?x:y; 26 | 27 | /// Internal SHA-512 implementation. 28 | namespace _sha512 { 29 | 30 | static const uint64_t K[80] = { 31 | 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, 0xb5c0fbcfec4d3b2fULL, 32 | 0xe9b5dba58189dbbcULL, 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, 33 | 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL, 0xd807aa98a3030242ULL, 34 | 0x12835b0145706fbeULL, 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL, 35 | 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, 0x9bdc06a725c71235ULL, 36 | 0xc19bf174cf692694ULL, 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, 37 | 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL, 0x2de92c6f592b0275ULL, 38 | 0x4a7484aa6ea6e483ULL, 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL, 39 | 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, 0xb00327c898fb213fULL, 40 | 0xbf597fc7beef0ee4ULL, 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, 41 | 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL, 0x27b70a8546d22ffcULL, 42 | 0x2e1b21385c26c926ULL, 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL, 43 | 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, 0x81c2c92e47edaee6ULL, 44 | 0x92722c851482353bULL, 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, 45 | 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL, 0xd192e819d6ef5218ULL, 46 | 0xd69906245565a910ULL, 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL, 47 | 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, 0x2748774cdf8eeb99ULL, 48 | 0x34b0bcb5e19b48a8ULL, 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, 49 | 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL, 0x748f82ee5defb2fcULL, 50 | 0x78a5636f43172f60ULL, 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL, 51 | 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, 0xbef9a3f7b2c67915ULL, 52 | 0xc67178f2e372532bULL, 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, 53 | 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL, 0x06f067aa72176fbaULL, 54 | 0x0a637dc5a2c898a6ULL, 0x113f9804bef90daeULL, 0x1b710b35131c471bULL, 55 | 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, 0x3c9ebe0a15c9bebcULL, 56 | 0x431d67c49c100d4cULL, 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, 57 | 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL 58 | }; 59 | 60 | #ifndef WIN64 61 | #define _byteswap_ulong __builtin_bswap32 62 | #define _byteswap_uint64 __builtin_bswap64 63 | inline uint64_t _rotr64(uint64_t x, uint8_t r) { 64 | asm("rorq %1,%0" : "+r" (x) : "c" (r)); 65 | return x; 66 | } 67 | #endif 68 | 69 | #define ROR(x,n) _rotr64(x, n) 70 | #define S0(x) (ROR(x, 28) ^ ROR(x, 34) ^ ROR(x, 39)) 71 | #define S1(x) (ROR(x, 14) ^ ROR(x, 18) ^ ROR(x, 41)) 72 | #define G0(x) (ROR(x, 1) ^ ROR(x, 8) ^ (x >> 7)) 73 | #define G1(x) (ROR(x, 19) ^ ROR(x, 61) ^ (x >> 6)) 74 | 75 | #define ROUND(i, a,b,c,d,e,f,g,h) \ 76 | t = h + S1(e) + (g ^ (e & (f ^ g))) + K[i] + W[i]; \ 77 | d += t; \ 78 | h = t + S0(a) + ( ((a | b) & c) | (a & b) ) 79 | 80 | #ifdef BSWAP 81 | #define READBE64(ptr) _byteswap_uint64(*(uint64_t *)(ptr)); 82 | #define WRITEBE64(ptr,x) *((uint64_t *)(ptr)) = _byteswap_uint64(x); 83 | #define READBE32(i) _byteswap_ulong((uint32_t)(i)); 84 | #else 85 | #define READBE64(ptr) *(uint64_t *)(ptr); 86 | #define WRITEBE64(ptr,x) *(ptr) = x; 87 | #define READBE32(i) (uint32_t)(i); 88 | #endif 89 | 90 | static void Transform(uint64_t state[8], const uint8_t buf[128]) { 91 | 92 | uint64_t W[80], t; 93 | uint64_t a, b, c, d, e, f, g, h; 94 | int i; 95 | 96 | a = state[0]; 97 | b = state[1]; 98 | c = state[2]; 99 | d = state[3]; 100 | e = state[4]; 101 | f = state[5]; 102 | g = state[6]; 103 | h = state[7]; 104 | 105 | W[0] = READBE64(buf + 0x0); 106 | W[1] = READBE64(buf + 0x8); 107 | W[2] = READBE64(buf + 0x10); 108 | W[3] = READBE64(buf + 0x18); 109 | W[4] = READBE64(buf + 0x20); 110 | W[5] = READBE64(buf + 0x28); 111 | W[6] = READBE64(buf + 0x30); 112 | W[7] = READBE64(buf + 0x38); 113 | W[8] = READBE64(buf + 0x40); 114 | W[9] = READBE64(buf + 0x48); 115 | W[10] = READBE64(buf + 0x50); 116 | W[11] = READBE64(buf + 0x58); 117 | W[12] = READBE64(buf + 0x60); 118 | W[13] = READBE64(buf + 0x68); 119 | W[14] = READBE64(buf + 0x70); 120 | W[15] = READBE64(buf + 0x78); 121 | 122 | for(i = 16; i < 80; i++) 123 | W[i] = W[i - 16] + G0(W[i - 15]) + W[i - 7] + G1(W[i - 2]); 124 | 125 | for (i = 0; i < 80; i += 8) { 126 | ROUND(i + 0, a, b, c, d, e, f, g, h); 127 | ROUND(i + 1, h, a, b, c, d, e, f, g); 128 | ROUND(i + 2, g, h, a, b, c, d, e, f); 129 | ROUND(i + 3, f, g, h, a, b, c, d, e); 130 | ROUND(i + 4, e, f, g, h, a, b, c, d); 131 | ROUND(i + 5, d, e, f, g, h, a, b, c); 132 | ROUND(i + 6, c, d, e, f, g, h, a, b); 133 | ROUND(i + 7, b, c, d, e, f, g, h, a); 134 | } 135 | 136 | state[0] += a; 137 | state[1] += b; 138 | state[2] += c; 139 | state[3] += d; 140 | state[4] += e; 141 | state[5] += f; 142 | state[6] += g; 143 | state[7] += h; 144 | 145 | } 146 | 147 | 148 | } 149 | 150 | 151 | class CSHA512 { 152 | 153 | private: 154 | uint64_t s[8]; 155 | unsigned char buf[128]; 156 | size_t buffSize; 157 | size_t count; 158 | 159 | public: 160 | 161 | CSHA512(); 162 | void Initialize(); 163 | void Write(const unsigned char* data, size_t len); 164 | void WriteDirect128(const unsigned char* data); 165 | void WriteDirect64(const unsigned char* data); 166 | void Finalize(unsigned char hash[64]); 167 | 168 | }; 169 | 170 | CSHA512::CSHA512() { 171 | 172 | Initialize(); 173 | 174 | } 175 | 176 | void CSHA512::Initialize() { 177 | 178 | buffSize = 0; 179 | count = 0; 180 | s[0] = 0x6a09e667f3bcc908ULL; 181 | s[1] = 0xbb67ae8584caa73bULL; 182 | s[2] = 0x3c6ef372fe94f82bULL; 183 | s[3] = 0xa54ff53a5f1d36f1ULL; 184 | s[4] = 0x510e527fade682d1ULL; 185 | s[5] = 0x9b05688c2b3e6c1fULL; 186 | s[6] = 0x1f83d9abfb41bd6bULL; 187 | s[7] = 0x5be0cd19137e2179ULL; 188 | 189 | } 190 | 191 | void CSHA512::Write(const unsigned char* data, size_t len) { 192 | 193 | if (buffSize > 0) { 194 | 195 | // Fill internal buffer up and transform 196 | size_t fill = MIN(len,128-buffSize); 197 | memcpy(buf+buffSize,data, fill); 198 | len -= fill; 199 | buffSize += fill; 200 | if(buffSize < 128) 201 | return; 202 | _sha512::Transform(s, buf); 203 | count++; 204 | 205 | } 206 | 207 | // Internal buffer is empty 208 | while (len >= 128) { 209 | _sha512::Transform(s, data); 210 | count++; 211 | data += 128; 212 | len -= 128; 213 | } 214 | 215 | // save rest for next time 216 | memcpy(buf,data,len); 217 | buffSize = len; 218 | 219 | } 220 | 221 | // Write 128 bytes aligned (buffsize must be 0) 222 | void CSHA512::WriteDirect128(const unsigned char* data) { 223 | _sha512::Transform(s, data); 224 | count++; 225 | } 226 | 227 | // Write 64 bytes aligned (buffsize must be 0) 228 | void CSHA512::WriteDirect64(const unsigned char* data) { 229 | memcpy(buf, data, 64); 230 | buffSize = 64; 231 | } 232 | 233 | void CSHA512::Finalize(unsigned char hash[64]) { 234 | 235 | size_t rest; 236 | size_t i; 237 | 238 | rest = buffSize; 239 | 240 | // End code 241 | buf[buffSize++] = 0x80; 242 | 243 | if (buffSize > 112) { 244 | memset(buf+buffSize,0,128-buffSize); 245 | _sha512::Transform(s, buf); 246 | buffSize = 0; 247 | } 248 | memset(buf+buffSize,0,112-buffSize); 249 | 250 | // Write length (128bit big-endian) 251 | WRITEBE64(buf + 112, count >> 54); 252 | WRITEBE64(buf + 120, ((count << 7) | rest) << 3); 253 | _sha512::Transform(s, buf); 254 | 255 | for (i = 0; i < 8; i++) 256 | WRITEBE64(hash + 8 * i, s[i]); 257 | 258 | } 259 | 260 | 261 | /* padding */ 262 | #define IPAD 0x36 263 | #define IPADLL 0x3636363636363636LL 264 | #define OPAD 0x5c 265 | #define OPADLL 0x5c5c5c5c5c5c5c5cLL 266 | 267 | void hmac_sha512_init(CSHA512 &ctx, const uint8_t key[SHA512_BLOCK_SIZE]) { 268 | 269 | uint64_t pad[SHA512_BLOCK_SIZE/8]; 270 | uint64_t *keyPtr = (uint64_t *)key; 271 | int i; 272 | 273 | // Inner padding 274 | for (i = 0; i < SHA512_BLOCK_SIZE/8; i++) 275 | pad[i] = keyPtr[i] ^ IPADLL; 276 | 277 | ctx.Initialize(); 278 | ctx.WriteDirect128((unsigned char *)pad); 279 | 280 | } 281 | 282 | 283 | void hmac_sha512_done(CSHA512 &ctx, const uint8_t key[SHA512_BLOCK_SIZE], uint8_t result[SHA512_HASH_LENGTH]) { 284 | 285 | uint64_t pad[SHA512_BLOCK_SIZE/8]; 286 | uint64_t *keyPtr = (uint64_t *)key; 287 | uint8_t ihash[SHA512_HASH_LENGTH]; 288 | int i; 289 | 290 | // Finalize inner hash 291 | ctx.Finalize(ihash); 292 | 293 | // Construct outer padding 294 | for (i = 0; i < SHA512_BLOCK_SIZE/8; i++) 295 | pad[i] = keyPtr[i] ^ OPADLL; 296 | 297 | // Final hash 298 | CSHA512 c; 299 | c.WriteDirect128((unsigned char *)pad); 300 | c.WriteDirect64(ihash); 301 | c.Finalize(result); 302 | 303 | } 304 | 305 | 306 | void pbkdf2_hmac_sha512(uint8_t *out, size_t outlen, 307 | const uint8_t *passwd, size_t passlen, 308 | const uint8_t *salt, size_t saltlen, 309 | uint64_t iter) { 310 | 311 | CSHA512 hmac, hmac_template; 312 | uint32_t i, be32i; 313 | uint64_t j; 314 | int k; 315 | 316 | uint8_t key[SHA512_BLOCK_SIZE]; 317 | uint8_t F[SHA512_HASH_LENGTH], U[SHA512_HASH_LENGTH]; 318 | uint64_t *Fptr = (uint64_t *)F; 319 | uint64_t *Uptr = (uint64_t *)U; 320 | size_t need; 321 | 322 | if (passlen < SHA512_BLOCK_SIZE) { 323 | memcpy(key, passwd, passlen); 324 | memset(key + passlen, 0, SHA512_BLOCK_SIZE - passlen); 325 | } else { 326 | hmac.Write(passwd, passlen); 327 | hmac.Finalize(key); 328 | memset(key + SHA512_HASH_LENGTH, 0, SHA512_BLOCK_SIZE - SHA512_HASH_LENGTH); 329 | } 330 | 331 | hmac_sha512_init(hmac_template, key); 332 | hmac_template.Write(salt, saltlen); 333 | 334 | for (i = 1; outlen > 0; i++) { 335 | 336 | hmac = hmac_template; 337 | be32i = READBE32(i); 338 | hmac.Write((unsigned char *)&be32i, sizeof(be32i)); 339 | hmac_sha512_done(hmac, key, U); 340 | memcpy(F, U, SHA512_HASH_LENGTH); 341 | 342 | for (j = 2; j <= iter; j++) { 343 | hmac_sha512_init(hmac, key); 344 | hmac.WriteDirect64(U); 345 | hmac_sha512_done(hmac, key, U); 346 | for (k = 0; k < SHA512_HASH_LENGTH/8; k++) 347 | Fptr[k] ^= Uptr[k]; 348 | } 349 | 350 | need = MIN(SHA512_HASH_LENGTH, outlen); 351 | 352 | memcpy(out, F, need); 353 | out += need; 354 | outlen -= need; 355 | 356 | } 357 | 358 | } 359 | 360 | void hmac_sha512(unsigned char *key, int key_length, unsigned char *message, int message_length, unsigned char *digest) { 361 | 362 | uint8_t ipad[SHA512_BLOCK_SIZE]; 363 | uint8_t opad[SHA512_BLOCK_SIZE]; 364 | uint8_t hash[SHA512_HASH_LENGTH]; 365 | int i; 366 | 367 | // TODO Handle key larger than 128 368 | 369 | for (i = 0; i < key_length && i < SHA512_BLOCK_SIZE; i++) { 370 | ipad[i] = key[i] ^ IPAD; 371 | opad[i] = key[i] ^ OPAD; 372 | } 373 | for (; i < SHA512_BLOCK_SIZE; i++) { 374 | ipad[i] = IPAD; 375 | opad[i] = OPAD; 376 | } 377 | 378 | CSHA512 h; 379 | h.WriteDirect128(ipad); 380 | h.Write(message, message_length); 381 | h.Finalize(hash); 382 | 383 | h.Initialize(); 384 | h.WriteDirect128(opad); 385 | h.Write(hash, SHA512_HASH_LENGTH); 386 | h.Finalize(digest); 387 | 388 | } 389 | 390 | void sha512(unsigned char *input, int length, unsigned char *digest) { 391 | 392 | CSHA512 sha; 393 | sha.Write(input, length); 394 | sha.Finalize(digest); 395 | 396 | } 397 | 398 | std::string sha512_hex(unsigned char *digest) { 399 | 400 | char buf[2 * 64 + 1]; 401 | buf[2 * 64] = 0; 402 | for (int i = 0; i < 64; i++) 403 | sprintf(buf + i * 2, "%02x", digest[i]); 404 | return std::string(buf); 405 | 406 | } 407 | -------------------------------------------------------------------------------- /hash/ripemd160_sse.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the VanitySearch distribution (https://github.com/JeanLucPons/VanitySearch). 3 | * Copyright (c) 2019 Jean Luc PONS. 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, version 3. 8 | * 9 | * This program is distributed in the hope that it will be useful, but 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | * General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | #include "ripemd160.h" 19 | #include 20 | #include 21 | 22 | // Internal SSE RIPEMD-160 implementation. 23 | namespace ripemd160sse { 24 | 25 | #ifdef WIN64 26 | static const __declspec(align(16)) uint32_t _init[] = { 27 | #else 28 | static const uint32_t _init[] __attribute__ ((aligned (16))) = { 29 | #endif 30 | 0x67452301ul,0x67452301ul,0x67452301ul,0x67452301ul, 31 | 0xEFCDAB89ul,0xEFCDAB89ul,0xEFCDAB89ul,0xEFCDAB89ul, 32 | 0x98BADCFEul,0x98BADCFEul,0x98BADCFEul,0x98BADCFEul, 33 | 0x10325476ul,0x10325476ul,0x10325476ul,0x10325476ul, 34 | 0xC3D2E1F0ul,0xC3D2E1F0ul,0xC3D2E1F0ul,0xC3D2E1F0ul 35 | }; 36 | 37 | //#define f1(x, y, z) (x ^ y ^ z) 38 | //#define f2(x, y, z) ((x & y) | (~x & z)) 39 | //#define f3(x, y, z) ((x | ~y) ^ z) 40 | //#define f4(x, y, z) ((x & z) | (~z & y)) 41 | //#define f5(x, y, z) (x ^ (y | ~z)) 42 | 43 | #define ROL(x,n) _mm_or_si128( _mm_slli_epi32(x, n) , _mm_srli_epi32(x, 32 - n) ) 44 | 45 | #ifdef WIN64 46 | 47 | #define not(x) _mm_andnot_si128(x, _mm_cmpeq_epi32(_mm_setzero_si128(), _mm_setzero_si128())) 48 | #define f1(x,y,z) _mm_xor_si128(x, _mm_xor_si128(y, z)) 49 | #define f2(x,y,z) _mm_or_si128(_mm_and_si128(x,y),_mm_andnot_si128(x,z)) 50 | #define f3(x,y,z) _mm_xor_si128(_mm_or_si128(x,not(y)),z) 51 | #define f4(x,y,z) _mm_or_si128(_mm_and_si128(x,z),_mm_andnot_si128(z,y)) 52 | #define f5(x,y,z) _mm_xor_si128(x,_mm_or_si128(y,not(z))) 53 | 54 | #else 55 | 56 | #define f1(x,y,z) _mm_xor_si128(x, _mm_xor_si128(y, z)) 57 | #define f2(x,y,z) _mm_or_si128(_mm_and_si128(x,y),_mm_andnot_si128(x,z)) 58 | #define f3(x,y,z) _mm_xor_si128(_mm_or_si128(x,~(y)),z) 59 | #define f4(x,y,z) _mm_or_si128(_mm_and_si128(x,z),_mm_andnot_si128(z,y)) 60 | #define f5(x,y,z) _mm_xor_si128(x,_mm_or_si128(y,~(z))) 61 | 62 | #endif 63 | 64 | 65 | #define add3(x0, x1, x2 ) _mm_add_epi32(_mm_add_epi32(x0, x1), x2) 66 | #define add4(x0, x1, x2, x3) _mm_add_epi32(_mm_add_epi32(x0, x1), _mm_add_epi32(x2, x3)) 67 | 68 | #define Round(a,b,c,d,e,f,x,k,r) \ 69 | u = add4(a,f,x,_mm_set1_epi32(k)); \ 70 | a = _mm_add_epi32(ROL(u, r),e); \ 71 | c = ROL(c, 10); 72 | 73 | #define R11(a,b,c,d,e,x,r) Round(a, b, c, d, e, f1(b, c, d), x, 0, r) 74 | #define R21(a,b,c,d,e,x,r) Round(a, b, c, d, e, f2(b, c, d), x, 0x5A827999ul, r) 75 | #define R31(a,b,c,d,e,x,r) Round(a, b, c, d, e, f3(b, c, d), x, 0x6ED9EBA1ul, r) 76 | #define R41(a,b,c,d,e,x,r) Round(a, b, c, d, e, f4(b, c, d), x, 0x8F1BBCDCul, r) 77 | #define R51(a,b,c,d,e,x,r) Round(a, b, c, d, e, f5(b, c, d), x, 0xA953FD4Eul, r) 78 | #define R12(a,b,c,d,e,x,r) Round(a, b, c, d, e, f5(b, c, d), x, 0x50A28BE6ul, r) 79 | #define R22(a,b,c,d,e,x,r) Round(a, b, c, d, e, f4(b, c, d), x, 0x5C4DD124ul, r) 80 | #define R32(a,b,c,d,e,x,r) Round(a, b, c, d, e, f3(b, c, d), x, 0x6D703EF3ul, r) 81 | #define R42(a,b,c,d,e,x,r) Round(a, b, c, d, e, f2(b, c, d), x, 0x7A6D76E9ul, r) 82 | #define R52(a,b,c,d,e,x,r) Round(a, b, c, d, e, f1(b, c, d), x, 0, r) 83 | 84 | #define LOADW(i) _mm_set_epi32(*((uint32_t *)blk[0]+i),*((uint32_t *)blk[1]+i),*((uint32_t *)blk[2]+i),*((uint32_t *)blk[3]+i)) 85 | 86 | // Initialize RIPEMD-160 state 87 | void Initialize(__m128i *s) { 88 | memcpy(s, _init, sizeof(_init)); 89 | } 90 | 91 | // Perform 4 RIPE in parallel using SSE2 92 | void Transform(__m128i *s, uint8_t *blk[4]) { 93 | 94 | __m128i a1 = _mm_load_si128(s + 0); 95 | __m128i b1 = _mm_load_si128(s + 1); 96 | __m128i c1 = _mm_load_si128(s + 2); 97 | __m128i d1 = _mm_load_si128(s + 3); 98 | __m128i e1 = _mm_load_si128(s + 4); 99 | __m128i a2 = a1; 100 | __m128i b2 = b1; 101 | __m128i c2 = c1; 102 | __m128i d2 = d1; 103 | __m128i e2 = e1; 104 | __m128i u; 105 | __m128i w[16]; 106 | 107 | 108 | w[0] = LOADW(0); 109 | w[1] = LOADW(1); 110 | w[2] = LOADW(2); 111 | w[3] = LOADW(3); 112 | w[4] = LOADW(4); 113 | w[5] = LOADW(5); 114 | w[6] = LOADW(6); 115 | w[7] = LOADW(7); 116 | w[8] = LOADW(8); 117 | w[9] = LOADW(9); 118 | w[10] = LOADW(10); 119 | w[11] = LOADW(11); 120 | w[12] = LOADW(12); 121 | w[13] = LOADW(13); 122 | w[14] = LOADW(14); 123 | w[15] = LOADW(15); 124 | 125 | R11(a1, b1, c1, d1, e1, w[0], 11); 126 | R12(a2, b2, c2, d2, e2, w[5], 8); 127 | R11(e1, a1, b1, c1, d1, w[1], 14); 128 | R12(e2, a2, b2, c2, d2, w[14], 9); 129 | R11(d1, e1, a1, b1, c1, w[2], 15); 130 | R12(d2, e2, a2, b2, c2, w[7], 9); 131 | R11(c1, d1, e1, a1, b1, w[3], 12); 132 | R12(c2, d2, e2, a2, b2, w[0], 11); 133 | R11(b1, c1, d1, e1, a1, w[4], 5); 134 | R12(b2, c2, d2, e2, a2, w[9], 13); 135 | R11(a1, b1, c1, d1, e1, w[5], 8); 136 | R12(a2, b2, c2, d2, e2, w[2], 15); 137 | R11(e1, a1, b1, c1, d1, w[6], 7); 138 | R12(e2, a2, b2, c2, d2, w[11], 15); 139 | R11(d1, e1, a1, b1, c1, w[7], 9); 140 | R12(d2, e2, a2, b2, c2, w[4], 5); 141 | R11(c1, d1, e1, a1, b1, w[8], 11); 142 | R12(c2, d2, e2, a2, b2, w[13], 7); 143 | R11(b1, c1, d1, e1, a1, w[9], 13); 144 | R12(b2, c2, d2, e2, a2, w[6], 7); 145 | R11(a1, b1, c1, d1, e1, w[10], 14); 146 | R12(a2, b2, c2, d2, e2, w[15], 8); 147 | R11(e1, a1, b1, c1, d1, w[11], 15); 148 | R12(e2, a2, b2, c2, d2, w[8], 11); 149 | R11(d1, e1, a1, b1, c1, w[12], 6); 150 | R12(d2, e2, a2, b2, c2, w[1], 14); 151 | R11(c1, d1, e1, a1, b1, w[13], 7); 152 | R12(c2, d2, e2, a2, b2, w[10], 14); 153 | R11(b1, c1, d1, e1, a1, w[14], 9); 154 | R12(b2, c2, d2, e2, a2, w[3], 12); 155 | R11(a1, b1, c1, d1, e1, w[15], 8); 156 | R12(a2, b2, c2, d2, e2, w[12], 6); 157 | 158 | R21(e1, a1, b1, c1, d1, w[7], 7); 159 | R22(e2, a2, b2, c2, d2, w[6], 9); 160 | R21(d1, e1, a1, b1, c1, w[4], 6); 161 | R22(d2, e2, a2, b2, c2, w[11], 13); 162 | R21(c1, d1, e1, a1, b1, w[13], 8); 163 | R22(c2, d2, e2, a2, b2, w[3], 15); 164 | R21(b1, c1, d1, e1, a1, w[1], 13); 165 | R22(b2, c2, d2, e2, a2, w[7], 7); 166 | R21(a1, b1, c1, d1, e1, w[10], 11); 167 | R22(a2, b2, c2, d2, e2, w[0], 12); 168 | R21(e1, a1, b1, c1, d1, w[6], 9); 169 | R22(e2, a2, b2, c2, d2, w[13], 8); 170 | R21(d1, e1, a1, b1, c1, w[15], 7); 171 | R22(d2, e2, a2, b2, c2, w[5], 9); 172 | R21(c1, d1, e1, a1, b1, w[3], 15); 173 | R22(c2, d2, e2, a2, b2, w[10], 11); 174 | R21(b1, c1, d1, e1, a1, w[12], 7); 175 | R22(b2, c2, d2, e2, a2, w[14], 7); 176 | R21(a1, b1, c1, d1, e1, w[0], 12); 177 | R22(a2, b2, c2, d2, e2, w[15], 7); 178 | R21(e1, a1, b1, c1, d1, w[9], 15); 179 | R22(e2, a2, b2, c2, d2, w[8], 12); 180 | R21(d1, e1, a1, b1, c1, w[5], 9); 181 | R22(d2, e2, a2, b2, c2, w[12], 7); 182 | R21(c1, d1, e1, a1, b1, w[2], 11); 183 | R22(c2, d2, e2, a2, b2, w[4], 6); 184 | R21(b1, c1, d1, e1, a1, w[14], 7); 185 | R22(b2, c2, d2, e2, a2, w[9], 15); 186 | R21(a1, b1, c1, d1, e1, w[11], 13); 187 | R22(a2, b2, c2, d2, e2, w[1], 13); 188 | R21(e1, a1, b1, c1, d1, w[8], 12); 189 | R22(e2, a2, b2, c2, d2, w[2], 11); 190 | 191 | R31(d1, e1, a1, b1, c1, w[3], 11); 192 | R32(d2, e2, a2, b2, c2, w[15], 9); 193 | R31(c1, d1, e1, a1, b1, w[10], 13); 194 | R32(c2, d2, e2, a2, b2, w[5], 7); 195 | R31(b1, c1, d1, e1, a1, w[14], 6); 196 | R32(b2, c2, d2, e2, a2, w[1], 15); 197 | R31(a1, b1, c1, d1, e1, w[4], 7); 198 | R32(a2, b2, c2, d2, e2, w[3], 11); 199 | R31(e1, a1, b1, c1, d1, w[9], 14); 200 | R32(e2, a2, b2, c2, d2, w[7], 8); 201 | R31(d1, e1, a1, b1, c1, w[15], 9); 202 | R32(d2, e2, a2, b2, c2, w[14], 6); 203 | R31(c1, d1, e1, a1, b1, w[8], 13); 204 | R32(c2, d2, e2, a2, b2, w[6], 6); 205 | R31(b1, c1, d1, e1, a1, w[1], 15); 206 | R32(b2, c2, d2, e2, a2, w[9], 14); 207 | R31(a1, b1, c1, d1, e1, w[2], 14); 208 | R32(a2, b2, c2, d2, e2, w[11], 12); 209 | R31(e1, a1, b1, c1, d1, w[7], 8); 210 | R32(e2, a2, b2, c2, d2, w[8], 13); 211 | R31(d1, e1, a1, b1, c1, w[0], 13); 212 | R32(d2, e2, a2, b2, c2, w[12], 5); 213 | R31(c1, d1, e1, a1, b1, w[6], 6); 214 | R32(c2, d2, e2, a2, b2, w[2], 14); 215 | R31(b1, c1, d1, e1, a1, w[13], 5); 216 | R32(b2, c2, d2, e2, a2, w[10], 13); 217 | R31(a1, b1, c1, d1, e1, w[11], 12); 218 | R32(a2, b2, c2, d2, e2, w[0], 13); 219 | R31(e1, a1, b1, c1, d1, w[5], 7); 220 | R32(e2, a2, b2, c2, d2, w[4], 7); 221 | R31(d1, e1, a1, b1, c1, w[12], 5); 222 | R32(d2, e2, a2, b2, c2, w[13], 5); 223 | 224 | R41(c1, d1, e1, a1, b1, w[1], 11); 225 | R42(c2, d2, e2, a2, b2, w[8], 15); 226 | R41(b1, c1, d1, e1, a1, w[9], 12); 227 | R42(b2, c2, d2, e2, a2, w[6], 5); 228 | R41(a1, b1, c1, d1, e1, w[11], 14); 229 | R42(a2, b2, c2, d2, e2, w[4], 8); 230 | R41(e1, a1, b1, c1, d1, w[10], 15); 231 | R42(e2, a2, b2, c2, d2, w[1], 11); 232 | R41(d1, e1, a1, b1, c1, w[0], 14); 233 | R42(d2, e2, a2, b2, c2, w[3], 14); 234 | R41(c1, d1, e1, a1, b1, w[8], 15); 235 | R42(c2, d2, e2, a2, b2, w[11], 14); 236 | R41(b1, c1, d1, e1, a1, w[12], 9); 237 | R42(b2, c2, d2, e2, a2, w[15], 6); 238 | R41(a1, b1, c1, d1, e1, w[4], 8); 239 | R42(a2, b2, c2, d2, e2, w[0], 14); 240 | R41(e1, a1, b1, c1, d1, w[13], 9); 241 | R42(e2, a2, b2, c2, d2, w[5], 6); 242 | R41(d1, e1, a1, b1, c1, w[3], 14); 243 | R42(d2, e2, a2, b2, c2, w[12], 9); 244 | R41(c1, d1, e1, a1, b1, w[7], 5); 245 | R42(c2, d2, e2, a2, b2, w[2], 12); 246 | R41(b1, c1, d1, e1, a1, w[15], 6); 247 | R42(b2, c2, d2, e2, a2, w[13], 9); 248 | R41(a1, b1, c1, d1, e1, w[14], 8); 249 | R42(a2, b2, c2, d2, e2, w[9], 12); 250 | R41(e1, a1, b1, c1, d1, w[5], 6); 251 | R42(e2, a2, b2, c2, d2, w[7], 5); 252 | R41(d1, e1, a1, b1, c1, w[6], 5); 253 | R42(d2, e2, a2, b2, c2, w[10], 15); 254 | R41(c1, d1, e1, a1, b1, w[2], 12); 255 | R42(c2, d2, e2, a2, b2, w[14], 8); 256 | 257 | R51(b1, c1, d1, e1, a1, w[4], 9); 258 | R52(b2, c2, d2, e2, a2, w[12], 8); 259 | R51(a1, b1, c1, d1, e1, w[0], 15); 260 | R52(a2, b2, c2, d2, e2, w[15], 5); 261 | R51(e1, a1, b1, c1, d1, w[5], 5); 262 | R52(e2, a2, b2, c2, d2, w[10], 12); 263 | R51(d1, e1, a1, b1, c1, w[9], 11); 264 | R52(d2, e2, a2, b2, c2, w[4], 9); 265 | R51(c1, d1, e1, a1, b1, w[7], 6); 266 | R52(c2, d2, e2, a2, b2, w[1], 12); 267 | R51(b1, c1, d1, e1, a1, w[12], 8); 268 | R52(b2, c2, d2, e2, a2, w[5], 5); 269 | R51(a1, b1, c1, d1, e1, w[2], 13); 270 | R52(a2, b2, c2, d2, e2, w[8], 14); 271 | R51(e1, a1, b1, c1, d1, w[10], 12); 272 | R52(e2, a2, b2, c2, d2, w[7], 6); 273 | R51(d1, e1, a1, b1, c1, w[14], 5); 274 | R52(d2, e2, a2, b2, c2, w[6], 8); 275 | R51(c1, d1, e1, a1, b1, w[1], 12); 276 | R52(c2, d2, e2, a2, b2, w[2], 13); 277 | R51(b1, c1, d1, e1, a1, w[3], 13); 278 | R52(b2, c2, d2, e2, a2, w[13], 6); 279 | R51(a1, b1, c1, d1, e1, w[8], 14); 280 | R52(a2, b2, c2, d2, e2, w[14], 5); 281 | R51(e1, a1, b1, c1, d1, w[11], 11); 282 | R52(e2, a2, b2, c2, d2, w[0], 15); 283 | R51(d1, e1, a1, b1, c1, w[6], 8); 284 | R52(d2, e2, a2, b2, c2, w[3], 13); 285 | R51(c1, d1, e1, a1, b1, w[15], 5); 286 | R52(c2, d2, e2, a2, b2, w[9], 11); 287 | R51(b1, c1, d1, e1, a1, w[13], 6); 288 | R52(b2, c2, d2, e2, a2, w[11], 11); 289 | 290 | __m128i t = s[0]; 291 | s[0] = add3(s[1],c1,d2); 292 | s[1] = add3(s[2],d1,e2); 293 | s[2] = add3(s[3],e1,a2); 294 | s[3] = add3(s[4],a1,b2); 295 | s[4] = add3(t,b1,c2); 296 | } 297 | 298 | } // namespace ripemd160sse 299 | 300 | #ifdef WIN64 301 | 302 | #define DEPACK(d,i) \ 303 | ((uint32_t *)d)[0] = s[0].m128i_u32[i]; \ 304 | ((uint32_t *)d)[1] = s[1].m128i_u32[i]; \ 305 | ((uint32_t *)d)[2] = s[2].m128i_u32[i]; \ 306 | ((uint32_t *)d)[3] = s[3].m128i_u32[i]; \ 307 | ((uint32_t *)d)[4] = s[4].m128i_u32[i]; 308 | 309 | #else 310 | 311 | #define DEPACK(d,i) \ 312 | ((uint32_t *)d)[0] = s0[i]; \ 313 | ((uint32_t *)d)[1] = s1[i]; \ 314 | ((uint32_t *)d)[2] = s2[i]; \ 315 | ((uint32_t *)d)[3] = s3[i]; \ 316 | ((uint32_t *)d)[4] = s4[i]; 317 | 318 | #endif 319 | 320 | static const uint64_t sizedesc_32 = 32 << 3; 321 | static const unsigned char pad[64] = { 0x80 }; 322 | 323 | void ripemd160sse_32( 324 | unsigned char *i0, 325 | unsigned char *i1, 326 | unsigned char *i2, 327 | unsigned char *i3, 328 | unsigned char *d0, 329 | unsigned char *d1, 330 | unsigned char *d2, 331 | unsigned char *d3) { 332 | 333 | __m128i s[5]; 334 | uint8_t *bs[] = { i0,i1,i2,i3 }; 335 | 336 | ripemd160sse::Initialize(s); 337 | memcpy(i0 + 32, pad, 24); 338 | memcpy(i0 + 56, &sizedesc_32, 8); 339 | memcpy(i1 + 32, pad, 24); 340 | memcpy(i1 + 56, &sizedesc_32, 8); 341 | memcpy(i2 + 32, pad, 24); 342 | memcpy(i2 + 56, &sizedesc_32, 8); 343 | memcpy(i3 + 32, pad, 24); 344 | memcpy(i3 + 56, &sizedesc_32, 8); 345 | 346 | ripemd160sse::Transform(s, bs); 347 | 348 | #ifndef WIN64 349 | uint32_t *s0 = (uint32_t *)&s[0]; 350 | uint32_t *s1 = (uint32_t *)&s[1]; 351 | uint32_t *s2 = (uint32_t *)&s[2]; 352 | uint32_t *s3 = (uint32_t *)&s[3]; 353 | uint32_t *s4 = (uint32_t *)&s[4]; 354 | #endif 355 | 356 | DEPACK(d0,3); 357 | DEPACK(d1,2); 358 | DEPACK(d2,1); 359 | DEPACK(d3,0); 360 | 361 | } 362 | 363 | void ripemd160sse_test() { 364 | 365 | unsigned char h0[20]; 366 | unsigned char h1[20]; 367 | unsigned char h2[20]; 368 | unsigned char h3[20]; 369 | unsigned char ch0[20]; 370 | unsigned char ch1[20]; 371 | unsigned char ch2[20]; 372 | unsigned char ch3[20]; 373 | unsigned char m0[64]; 374 | unsigned char m1[64]; 375 | unsigned char m2[64]; 376 | unsigned char m3[64]; 377 | 378 | strcpy((char *)m0, "This is a test message to test01"); 379 | strcpy((char *)m1, "This is a test message to test02"); 380 | strcpy((char *)m2, "This is a test message to test03"); 381 | strcpy((char *)m3, "This is a test message to test04"); 382 | 383 | ripemd160_32(m0, ch0); 384 | ripemd160_32(m1, ch1); 385 | ripemd160_32(m2, ch2); 386 | ripemd160_32(m3, ch3); 387 | 388 | ripemd160sse_32(m0, m1, m2, m3, h0, h1, h2, h3); 389 | 390 | if ((ripemd160_hex(h0) != ripemd160_hex(ch0)) || 391 | (ripemd160_hex(h1) != ripemd160_hex(ch1)) || 392 | (ripemd160_hex(h2) != ripemd160_hex(ch2)) || 393 | (ripemd160_hex(h3) != ripemd160_hex(ch3))) { 394 | 395 | printf("RIPEMD160() Results Wrong !\n"); 396 | printf("RIP: %s\n", ripemd160_hex(ch0).c_str()); 397 | printf("RIP: %s\n", ripemd160_hex(ch1).c_str()); 398 | printf("RIP: %s\n", ripemd160_hex(ch2).c_str()); 399 | printf("RIP: %s\n\n", ripemd160_hex(ch3).c_str()); 400 | printf("SSE: %s\n", ripemd160_hex(h0).c_str()); 401 | printf("SSE: %s\n", ripemd160_hex(h1).c_str()); 402 | printf("SSE: %s\n", ripemd160_hex(h2).c_str()); 403 | printf("SSE: %s\n\n", ripemd160_hex(h3).c_str()); 404 | 405 | } 406 | 407 | printf("RIPE() Results OK !\n"); 408 | 409 | } 410 | -------------------------------------------------------------------------------- /minikeyg.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | "Commons Clause" License Condition v1.0 3 | 4 | The Software is provided to you by the Licensor under the License, as defined below, subject to the following condition. 5 | 6 | Without limiting other conditions in the License, the grant of rights under the License will not include, and the License does not grant to you, the right to Sell the Software. 7 | 8 | For purposes of the foregoing, "Sell" means practicing any or all of the rights granted to you under the License to provide to third parties, for a fee or other consideration (including without limitation fees for hosting or consulting/ support services related to the Software), a product or service whose value derives, entirely or substantially, from the functionality of the Software. Any license notice or attribution required by the License must also include this Commons Clause License Condition notice. 9 | 10 | Software: minikeyg 11 | 12 | License: Apache 2.0 13 | 14 | Licensor: Luis Alberto 15 | 16 | */ 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | #include 28 | #include 29 | 30 | #include "hash/sha256.h" 31 | #include "hash/ripemd160.h" 32 | 33 | const char *Ccoinbuffer_default = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; 34 | 35 | char *Ccoinbuffer; 36 | 37 | int Ccoinbuffer_length,Ccoinbuffer_mod; 38 | 39 | 40 | int THREADS = 1; 41 | 42 | int FLAG_COINBUFFER = 0; 43 | int FLAG_VERIFIED = 1; 44 | int FLAG_RANDOM = 0; 45 | int FLAG_MINIKEY = 0; 46 | int FLAG_SIZE = 0; 47 | int SIZE_VALUE = 0; 48 | int INCREMENT_OFFSET = 0; 49 | 50 | char *user_minikey; 51 | int user_size; 52 | FILE *fd; 53 | 54 | char minikey[40] = {0}; 55 | unsigned char raw_minikey[40]; 56 | 57 | pthread_mutex_t thread_mutex; 58 | 59 | bool increment_minikey_index(char *buffer,unsigned char *rawbuffer,int index); 60 | 61 | void bin2alphabet(unsigned char *buffer,int length); 62 | 63 | void sha256sse_23(uint8_t *src0, uint8_t *src1, uint8_t *src2, uint8_t *src3, uint8_t *dst0, uint8_t *dst1, uint8_t *dst2, uint8_t *dst3); 64 | void sha256sse_31(uint8_t *src0, uint8_t *src1, uint8_t *src2, uint8_t *src3, uint8_t *dst0, uint8_t *dst1, uint8_t *dst2, uint8_t *dst3); 65 | 66 | void *process_verified22(void *vargp); 67 | void *process_verified30(void *vargp); 68 | void *process_unverified(void *vargp); 69 | 70 | void *process_verified22_random(void *vargp); 71 | void *process_verified30_random(void *vargp); 72 | void *process_unverified_random(void *vargp); 73 | 74 | 75 | int main(int argc, char **argv) { 76 | 77 | char *r_str; 78 | int len,i,s; 79 | pthread_t tid ; 80 | char c; 81 | while ((c = getopt(argc, argv, "a:m:s:t:ur")) != -1) { 82 | switch(c){ 83 | case 'r': 84 | FLAG_RANDOM = 1; 85 | break; 86 | case 'u': 87 | FLAG_VERIFIED = 0; 88 | break; 89 | case 'm': 90 | FLAG_MINIKEY = 1; 91 | user_minikey = optarg; 92 | break; 93 | case 'a': 94 | FLAG_COINBUFFER = 1; 95 | Ccoinbuffer = optarg; 96 | Ccoinbuffer_length = strlen(Ccoinbuffer); 97 | Ccoinbuffer_mod = Ccoinbuffer_length -1; 98 | 99 | break; 100 | case 's': 101 | user_size = (int)strtol(optarg,NULL,10); 102 | switch(user_size) { 103 | case 22: 104 | case 30: 105 | FLAG_SIZE = 1; 106 | SIZE_VALUE = user_size; 107 | INCREMENT_OFFSET = SIZE_VALUE -1; 108 | break; 109 | default: 110 | fprintf(stderr,"Invalid size %i\n",user_size); 111 | exit(0); 112 | break; 113 | } 114 | break; 115 | case 't': 116 | THREADS = (int) strtol(optarg,NULL,10); 117 | if(THREADS < 1) { 118 | THREADS = 1; 119 | } 120 | break; 121 | } 122 | } 123 | pthread_mutex_init(&thread_mutex,NULL); 124 | 125 | if(FLAG_SIZE == 0) { 126 | SIZE_VALUE = 22; //default size 127 | INCREMENT_OFFSET = SIZE_VALUE - 1; 128 | } 129 | if(FLAG_COINBUFFER == 0) { 130 | Ccoinbuffer = (char*) Ccoinbuffer_default; 131 | Ccoinbuffer_length = strlen(Ccoinbuffer); 132 | Ccoinbuffer_mod = Ccoinbuffer_length -1; 133 | } 134 | 135 | 136 | if(FLAG_RANDOM) { 137 | fd = fopen("/dev/urandom","r"); 138 | if(fd == NULL) { 139 | fprintf(stderr,"Can't open /dev/urandom\n"); 140 | exit(0); 141 | } 142 | } 143 | else { 144 | if(FLAG_MINIKEY) { 145 | len = strlen(user_minikey); 146 | switch(len) { 147 | case 22: 148 | case 30: 149 | if(FLAG_SIZE && SIZE_VALUE != len) { 150 | fprintf(stderr,"Size mismatch %i != %i (%s)\n",SIZE_VALUE,len,user_minikey); 151 | exit(0); 152 | } 153 | else { 154 | SIZE_VALUE = len; 155 | INCREMENT_OFFSET = SIZE_VALUE -1; 156 | } 157 | strcpy(minikey,user_minikey); 158 | for(i = 0; i< SIZE_VALUE; i++) { 159 | r_str = strchr(Ccoinbuffer,user_minikey[i]); 160 | if(r_str == NULL) { 161 | fprintf(stderr,"Invalid minikey %s have invalid character %c\n",user_minikey,user_minikey[i]); 162 | exit(0); 163 | } 164 | raw_minikey[i] = (int)(r_str - Ccoinbuffer) % Ccoinbuffer_length; 165 | } 166 | break; 167 | default: 168 | fprintf(stderr,"Invalid size %i: %s\n",len,user_minikey); 169 | exit(0); 170 | break; 171 | } 172 | } 173 | else { 174 | fd = fopen("/dev/urandom","r"); 175 | if(fd == NULL) { 176 | fprintf(stderr,"Can't open /dev/urandom\n"); 177 | exit(0); 178 | } 179 | fread(raw_minikey,sizeof(char),SIZE_VALUE,fd); 180 | fclose(fd); 181 | raw_minikey[0] = 25; 182 | minikey[0] = 'S'; 183 | for(i = 1; i < SIZE_VALUE; i++) { 184 | raw_minikey[i] = raw_minikey[i] % Ccoinbuffer_length; 185 | minikey[i] = Ccoinbuffer[raw_minikey[i]]; 186 | } 187 | } 188 | } 189 | for(i= 0;i < THREADS; i++) { 190 | if(FLAG_VERIFIED) { 191 | if(SIZE_VALUE == 22) { 192 | if(FLAG_RANDOM) { 193 | s = pthread_create(&tid,NULL,process_verified22_random,NULL); 194 | } 195 | else { 196 | s = pthread_create(&tid,NULL,process_verified22,NULL); 197 | } 198 | } 199 | else { 200 | if(FLAG_RANDOM) { 201 | s = pthread_create(&tid,NULL,process_verified30_random,NULL); 202 | } 203 | else { 204 | s = pthread_create(&tid,NULL,process_verified30,NULL); 205 | } 206 | } 207 | } 208 | else { 209 | if(FLAG_RANDOM) { 210 | s = pthread_create(&tid,NULL,process_unverified_random,NULL); 211 | } 212 | else { 213 | s = pthread_create(&tid,NULL,process_unverified,NULL); 214 | } 215 | } 216 | } 217 | pthread_join(tid,NULL); 218 | } 219 | 220 | void *process_verified22(void *vargp) { 221 | char current_minikey[4][40]; 222 | uint8_t keyvalue[4][32]; 223 | int i; 224 | for(i = 0; i < 4; i++){ 225 | current_minikey[i][0] = 'S'; 226 | current_minikey[i][SIZE_VALUE] = '?'; 227 | } 228 | do { 229 | pthread_mutex_lock(&thread_mutex); 230 | memcpy(current_minikey[0],minikey,SIZE_VALUE); 231 | increment_minikey_index(minikey,raw_minikey,INCREMENT_OFFSET); 232 | memcpy(current_minikey[1],minikey,SIZE_VALUE); 233 | increment_minikey_index(minikey,raw_minikey,INCREMENT_OFFSET); 234 | memcpy(current_minikey[2],minikey,SIZE_VALUE); 235 | increment_minikey_index(minikey,raw_minikey,INCREMENT_OFFSET); 236 | memcpy(current_minikey[3],minikey,SIZE_VALUE); 237 | increment_minikey_index(minikey,raw_minikey,INCREMENT_OFFSET); 238 | pthread_mutex_unlock(&thread_mutex); 239 | sha256sse_23((uint8_t*)current_minikey[0],(uint8_t*)current_minikey[1],(uint8_t*)current_minikey[2],(uint8_t*)current_minikey[3],keyvalue[0],keyvalue[1],keyvalue[2],keyvalue[3]); 240 | for(i = 0; i < 4; i++){ 241 | if(keyvalue[i][0] == 0x00) { 242 | current_minikey[i][SIZE_VALUE] = '\0'; 243 | fprintf(stdout,"%s\n",current_minikey[i]); 244 | current_minikey[i][SIZE_VALUE] = '?'; 245 | } 246 | } 247 | }while(1); 248 | return NULL; 249 | } 250 | 251 | void *process_verified30(void *vargp) { 252 | char current_minikey[4][40]; 253 | uint8_t keyvalue[4][32]; 254 | int i; 255 | for(i = 0; i < 4; i++){ 256 | current_minikey[i][0] = 'S'; 257 | current_minikey[i][SIZE_VALUE] = '?'; 258 | } 259 | do { 260 | pthread_mutex_lock(&thread_mutex); 261 | memcpy(current_minikey[0],minikey,SIZE_VALUE); 262 | increment_minikey_index(minikey,raw_minikey,INCREMENT_OFFSET); 263 | memcpy(current_minikey[1],minikey,SIZE_VALUE); 264 | increment_minikey_index(minikey,raw_minikey,INCREMENT_OFFSET); 265 | memcpy(current_minikey[2],minikey,SIZE_VALUE); 266 | increment_minikey_index(minikey,raw_minikey,INCREMENT_OFFSET); 267 | memcpy(current_minikey[3],minikey,SIZE_VALUE); 268 | increment_minikey_index(minikey,raw_minikey,INCREMENT_OFFSET); 269 | pthread_mutex_unlock(&thread_mutex); 270 | sha256sse_31((uint8_t*)current_minikey[0],(uint8_t*)current_minikey[1],(uint8_t*)current_minikey[2],(uint8_t*)current_minikey[3],keyvalue[0],keyvalue[1],keyvalue[2],keyvalue[3]); 271 | for(i = 0; i < 4; i++){ 272 | if(keyvalue[i][0] == 0x00) { 273 | current_minikey[i][SIZE_VALUE] = '\0'; 274 | fprintf(stdout,"%s\n",current_minikey[i]); 275 | current_minikey[i][SIZE_VALUE] = '?'; 276 | } 277 | } 278 | }while(1); 279 | return NULL; 280 | } 281 | 282 | void *process_verified22_random(void *vargp) { 283 | char random_buffer[4][2048]; 284 | char current_minikey[4][40]; 285 | uint8_t keyvalue[4][32]; 286 | int i,j,limit; 287 | for(i = 0; i < 4; i++) { 288 | current_minikey[i][0] = 'S'; 289 | current_minikey[i][SIZE_VALUE] = '?'; 290 | } 291 | limit = 2048 -SIZE_VALUE; 292 | do { 293 | pthread_mutex_lock(&thread_mutex); 294 | fread(random_buffer[0],1,2048,fd); 295 | fread(random_buffer[1],1,2048,fd); 296 | fread(random_buffer[2],1,2048,fd); 297 | fread(random_buffer[3],1,2048,fd); 298 | pthread_mutex_unlock(&thread_mutex); 299 | for(i = 0; i < 4; i++){ 300 | bin2alphabet((unsigned char*)random_buffer[i],2048); 301 | } 302 | for(j = 0; j < limit; j++ ) { 303 | memcpy(current_minikey[0]+1,random_buffer[0]+j,INCREMENT_OFFSET); 304 | memcpy(current_minikey[1]+1,random_buffer[1]+j,INCREMENT_OFFSET); 305 | memcpy(current_minikey[2]+1,random_buffer[2]+j,INCREMENT_OFFSET); 306 | memcpy(current_minikey[3]+1,random_buffer[3]+j,INCREMENT_OFFSET); 307 | sha256sse_23((uint8_t*)current_minikey[0],(uint8_t*)current_minikey[1],(uint8_t*)current_minikey[2],(uint8_t*)current_minikey[3],keyvalue[0],keyvalue[1],keyvalue[2],keyvalue[3]); 308 | for(i = 0; i < 4; i++){ 309 | if(keyvalue[i][0] == 0x00) { 310 | current_minikey[i][SIZE_VALUE] = '\0'; 311 | fprintf(stdout,"%s\n",current_minikey[i]); 312 | current_minikey[i][SIZE_VALUE] = '?'; 313 | } 314 | } 315 | } 316 | }while(1); 317 | return NULL; 318 | } 319 | 320 | 321 | void *process_verified30_random(void *vargp) { 322 | char random_buffer[4][2048]; 323 | char current_minikey[4][40]; 324 | uint8_t keyvalue[4][32]; 325 | int i,j,limit; 326 | 327 | for(i = 0; i < 4; i++) { 328 | current_minikey[i][0] = 'S'; 329 | current_minikey[i][SIZE_VALUE] = '?'; 330 | } 331 | limit = 2048 -SIZE_VALUE; 332 | do { 333 | pthread_mutex_lock(&thread_mutex); 334 | fread(random_buffer[0],1,2048,fd); 335 | fread(random_buffer[1],1,2048,fd); 336 | fread(random_buffer[2],1,2048,fd); 337 | fread(random_buffer[3],1,2048,fd); 338 | pthread_mutex_unlock(&thread_mutex); 339 | for(i = 0; i < 4; i++){ 340 | bin2alphabet((unsigned char*)random_buffer[i],2048); 341 | } 342 | for(j = 0; j < limit; j++ ) { 343 | memcpy(current_minikey[0]+1,random_buffer[0]+j,INCREMENT_OFFSET); 344 | memcpy(current_minikey[1]+1,random_buffer[1]+j,INCREMENT_OFFSET); 345 | memcpy(current_minikey[2]+1,random_buffer[2]+j,INCREMENT_OFFSET); 346 | memcpy(current_minikey[3]+1,random_buffer[3]+j,INCREMENT_OFFSET); 347 | sha256sse_31((uint8_t*)current_minikey[0],(uint8_t*)current_minikey[1],(uint8_t*)current_minikey[2],(uint8_t*)current_minikey[3],keyvalue[0],keyvalue[1],keyvalue[2],keyvalue[3]); 348 | for(i = 0; i < 4; i++){ 349 | if(keyvalue[i][0] == 0x00) { 350 | current_minikey[i][SIZE_VALUE] = '\0'; 351 | fprintf(stdout,"%s\n",current_minikey[i]); 352 | current_minikey[i][SIZE_VALUE] = '?'; 353 | } 354 | } 355 | } 356 | }while(1); 357 | return NULL; 358 | } 359 | 360 | void *process_unverified(void *vargp) { 361 | char current_minikey[4][40]; 362 | int i; 363 | for(i = 0; i < 4; i++){ 364 | memset(current_minikey[i],0,40); 365 | } 366 | do { 367 | pthread_mutex_lock(&thread_mutex); 368 | memcpy(current_minikey[0],minikey,SIZE_VALUE); 369 | increment_minikey_index(minikey,raw_minikey,INCREMENT_OFFSET); 370 | memcpy(current_minikey[1],minikey,SIZE_VALUE); 371 | increment_minikey_index(minikey,raw_minikey,INCREMENT_OFFSET); 372 | memcpy(current_minikey[2],minikey,SIZE_VALUE); 373 | increment_minikey_index(minikey,raw_minikey,INCREMENT_OFFSET); 374 | memcpy(current_minikey[3],minikey,SIZE_VALUE); 375 | increment_minikey_index(minikey,raw_minikey,INCREMENT_OFFSET); 376 | pthread_mutex_unlock(&thread_mutex); 377 | for(i = 0; i < 4; i++) { 378 | fprintf(stdout,"%s\n",current_minikey[i]); 379 | } 380 | }while(1); 381 | return NULL; 382 | } 383 | 384 | void *process_unverified_random(void *vargp) { 385 | char random_buffer[4][2048]; 386 | char current_minikey[4][40]; 387 | int i,j,limit; 388 | for(i = 0; i < 4; i++){ 389 | memset(current_minikey[i],0,40); 390 | current_minikey[i][0] = 'S'; 391 | } 392 | limit = 2048 -SIZE_VALUE; 393 | do { 394 | pthread_mutex_lock(&thread_mutex); 395 | fread(random_buffer[0],1,2048,fd); 396 | fread(random_buffer[1],1,2048,fd); 397 | fread(random_buffer[2],1,2048,fd); 398 | fread(random_buffer[3],1,2048,fd); 399 | pthread_mutex_unlock(&thread_mutex); 400 | for(i = 0; i < 4; i++){ 401 | bin2alphabet((unsigned char*)random_buffer[i],2048); 402 | } 403 | for(j = 0; j < limit; j++ ) { 404 | for(i = 0; i < 4; i++) { 405 | memcpy(current_minikey[i]+1,random_buffer[i]+j,INCREMENT_OFFSET); 406 | fprintf(stdout,"%s\n",current_minikey[i]); 407 | } 408 | } 409 | }while(1); 410 | return NULL; 411 | } 412 | 413 | void bin2alphabet(unsigned char *buffer,int length) { 414 | for(int i = 0; i < length; i++) { 415 | buffer[i] = Ccoinbuffer[(int)buffer[i] % Ccoinbuffer_mod]; 416 | } 417 | } 418 | 419 | bool increment_minikey_index(char *buffer,unsigned char *rawbuffer,int index) { 420 | if(rawbuffer[index] < Ccoinbuffer_mod){ 421 | rawbuffer[index]++; 422 | buffer[index] = Ccoinbuffer[rawbuffer[index]]; 423 | } 424 | else { 425 | rawbuffer[index] = 0x00; 426 | buffer[index] = Ccoinbuffer[0]; 427 | if(index>0) { 428 | return increment_minikey_index(buffer,rawbuffer,index-1); 429 | } 430 | else { 431 | return false; 432 | } 433 | } 434 | return true; 435 | } 436 | 437 | #define BUFFMINIKEYCHECK23(buff,src) \ 438 | (buff)[ 0] = (uint32_t)src[ 0] << 24 | (uint32_t)src[ 1] << 16 | (uint32_t)src[ 2] << 8 | (uint32_t)src[ 3]; \ 439 | (buff)[ 1] = (uint32_t)src[ 4] << 24 | (uint32_t)src[ 5] << 16 | (uint32_t)src[ 6] << 8 | (uint32_t)src[ 7]; \ 440 | (buff)[ 2] = (uint32_t)src[ 8] << 24 | (uint32_t)src[ 9] << 16 | (uint32_t)src[10] << 8 | (uint32_t)src[11]; \ 441 | (buff)[ 3] = (uint32_t)src[12] << 24 | (uint32_t)src[13] << 16 | (uint32_t)src[14] << 8 | (uint32_t)src[15]; \ 442 | (buff)[ 4] = (uint32_t)src[16] << 24 | (uint32_t)src[17] << 16 | (uint32_t)src[18] << 8 | (uint32_t)src[19]; \ 443 | (buff)[ 5] = (uint32_t)src[20] << 24 | (uint32_t)src[21] << 16 | (uint32_t)src[22] << 8 | 0x80; \ 444 | (buff)[ 6] = 0; \ 445 | (buff)[ 7] = 0; \ 446 | (buff)[ 8] = 0; \ 447 | (buff)[ 9] = 0; \ 448 | (buff)[10] = 0; \ 449 | (buff)[11] = 0; \ 450 | (buff)[12] = 0; \ 451 | (buff)[13] = 0; \ 452 | (buff)[14] = 0; \ 453 | (buff)[15] = 0xB8; //184 bits => 23 BYTES 454 | 455 | void sha256sse_23(uint8_t *src0, uint8_t *src1, uint8_t *src2, uint8_t *src3, uint8_t *dst0, uint8_t *dst1, uint8_t *dst2, uint8_t *dst3) { 456 | uint32_t b0[16]; 457 | uint32_t b1[16]; 458 | uint32_t b2[16]; 459 | uint32_t b3[16]; 460 | BUFFMINIKEYCHECK23(b0, src0); 461 | BUFFMINIKEYCHECK23(b1, src1); 462 | BUFFMINIKEYCHECK23(b2, src2); 463 | BUFFMINIKEYCHECK23(b3, src3); 464 | sha256sse_1B(b0, b1, b2, b3, dst0, dst1, dst2, dst3); 465 | } 466 | 467 | #define BUFFMINIKEYCHECK31(buff,src) \ 468 | (buff)[ 0] = (uint32_t)src[ 0] << 24 | (uint32_t)src[ 1] << 16 | (uint32_t)src[ 2] << 8 | (uint32_t)src[ 3]; \ 469 | (buff)[ 1] = (uint32_t)src[ 4] << 24 | (uint32_t)src[ 5] << 16 | (uint32_t)src[ 6] << 8 | (uint32_t)src[ 7]; \ 470 | (buff)[ 2] = (uint32_t)src[ 8] << 24 | (uint32_t)src[ 9] << 16 | (uint32_t)src[10] << 8 | (uint32_t)src[11]; \ 471 | (buff)[ 3] = (uint32_t)src[12] << 24 | (uint32_t)src[13] << 16 | (uint32_t)src[14] << 8 | (uint32_t)src[15]; \ 472 | (buff)[ 4] = (uint32_t)src[16] << 24 | (uint32_t)src[17] << 16 | (uint32_t)src[18] << 8 | (uint32_t)src[19]; \ 473 | (buff)[ 5] = (uint32_t)src[20] << 24 | (uint32_t)src[21] << 16 | (uint32_t)src[22] << 8 | (uint32_t)src[23]; \ 474 | (buff)[ 6] = (uint32_t)src[24] << 24 | (uint32_t)src[25] << 16 | (uint32_t)src[26] << 8 | (uint32_t)src[27]; \ 475 | (buff)[ 7] = (uint32_t)src[28] << 24 | (uint32_t)src[29] << 16 | (uint32_t)src[30] << 8 | 0x80; \ 476 | (buff)[ 8] = 0; \ 477 | (buff)[ 9] = 0; \ 478 | (buff)[10] = 0; \ 479 | (buff)[11] = 0; \ 480 | (buff)[12] = 0; \ 481 | (buff)[13] = 0; \ 482 | (buff)[14] = 0; \ 483 | (buff)[15] = 0xF8; //248 bits => 31 BYTES 484 | 485 | void sha256sse_31(uint8_t *src0, uint8_t *src1, uint8_t *src2, uint8_t *src3, uint8_t *dst0, uint8_t *dst1, uint8_t *dst2, uint8_t *dst3) { 486 | uint32_t b0[16]; 487 | uint32_t b1[16]; 488 | uint32_t b2[16]; 489 | uint32_t b3[16]; 490 | BUFFMINIKEYCHECK31(b0, src0); 491 | BUFFMINIKEYCHECK31(b1, src1); 492 | BUFFMINIKEYCHECK31(b2, src2); 493 | BUFFMINIKEYCHECK31(b3, src3); 494 | sha256sse_1B(b0, b1, b2, b3, dst0, dst1, dst2, dst3); 495 | } -------------------------------------------------------------------------------- /hash/sha256.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the VanitySearch distribution (https://github.com/JeanLucPons/VanitySearch). 3 | * Copyright (c) 2019 Jean Luc PONS. 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, version 3. 8 | * 9 | * This program is distributed in the hope that it will be useful, but 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | * General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | #include 19 | #include "sha256.h" 20 | 21 | #define BSWAP 22 | 23 | /// Internal SHA-256 implementation. 24 | namespace _sha256 25 | { 26 | 27 | static const unsigned char pad[64] = { 0x80 }; 28 | 29 | #ifndef WIN64 30 | #define _byteswap_ulong __builtin_bswap32 31 | #define _byteswap_uint64 __builtin_bswap64 32 | inline uint32_t _rotr(uint32_t x, uint8_t r) { 33 | asm("rorl %1,%0" : "+r" (x) : "c" (r)); 34 | return x; 35 | } 36 | #endif 37 | 38 | #define ROR(x,n) _rotr(x, n) 39 | #define S0(x) (ROR(x,2) ^ ROR(x,13) ^ ROR(x,22)) 40 | #define S1(x) (ROR(x,6) ^ ROR(x,11) ^ ROR(x,25)) 41 | #define s0(x) (ROR(x,7) ^ ROR(x,18) ^ (x >> 3)) 42 | #define s1(x) (ROR(x,17) ^ ROR(x,19) ^ (x >> 10)) 43 | 44 | #define Maj(x,y,z) ((x&y)^(x&z)^(y&z)) 45 | //#define Ch(x,y,z) ((x&y)^(~x&z)) 46 | 47 | // The following functions are equivalent to the above 48 | //#define Maj(x,y,z) ((x & y) | (z & (x | y))) 49 | #define Ch(x,y,z) (z ^ (x & (y ^ z))) 50 | 51 | // SHA-256 round 52 | #define Round(a, b, c, d, e, f, g, h, k, w) \ 53 | t1 = h + S1(e) + Ch(e,f,g) + k + (w); \ 54 | t2 = S0(a) + Maj(a,b,c); \ 55 | d += t1; \ 56 | h = t1 + t2; 57 | 58 | #ifdef BSWAP 59 | #define WRITEBE32(ptr,x) *((uint32_t *)(ptr)) = _byteswap_ulong(x) 60 | #define WRITEBE64(ptr,x) *((uint64_t *)(ptr)) = _byteswap_uint64(x) 61 | #define READBE32(ptr) (uint32_t)_byteswap_ulong(*(uint32_t *)(ptr)) 62 | #else 63 | #define WRITEBE32(ptr,x) *(ptr) = x 64 | #define WRITEBE64(ptr,x) *(ptr) = x 65 | #define READBE32(ptr) *(uint32_t *)(ptr) 66 | #endif 67 | 68 | // Initialise state 69 | void Initialize(uint32_t *s) { 70 | 71 | s[0] = 0x6a09e667ul; 72 | s[1] = 0xbb67ae85ul; 73 | s[2] = 0x3c6ef372ul; 74 | s[3] = 0xa54ff53aul; 75 | s[4] = 0x510e527ful; 76 | s[5] = 0x9b05688cul; 77 | s[6] = 0x1f83d9abul; 78 | s[7] = 0x5be0cd19ul; 79 | 80 | } 81 | 82 | 83 | // Perform SHA-256 transformations, process 64-byte chunks 84 | void Transform(uint32_t* s, const unsigned char* chunk) 85 | { 86 | uint32_t t1; 87 | uint32_t t2; 88 | 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]; 89 | uint32_t w0, w1, w2, w3, w4, w5, w6, w7, w8, w9, w10, w11, w12, w13, w14, w15; 90 | 91 | Round(a, b, c, d, e, f, g, h, 0x428a2f98, w0 = READBE32(chunk + 0)); 92 | Round(h, a, b, c, d, e, f, g, 0x71374491, w1 = READBE32(chunk + 4)); 93 | Round(g, h, a, b, c, d, e, f, 0xb5c0fbcf, w2 = READBE32(chunk + 8)); 94 | Round(f, g, h, a, b, c, d, e, 0xe9b5dba5, w3 = READBE32(chunk + 12)); 95 | Round(e, f, g, h, a, b, c, d, 0x3956c25b, w4 = READBE32(chunk + 16)); 96 | Round(d, e, f, g, h, a, b, c, 0x59f111f1, w5 = READBE32(chunk + 20)); 97 | Round(c, d, e, f, g, h, a, b, 0x923f82a4, w6 = READBE32(chunk + 24)); 98 | Round(b, c, d, e, f, g, h, a, 0xab1c5ed5, w7 = READBE32(chunk + 28)); 99 | Round(a, b, c, d, e, f, g, h, 0xd807aa98, w8 = READBE32(chunk + 32)); 100 | Round(h, a, b, c, d, e, f, g, 0x12835b01, w9 = READBE32(chunk + 36)); 101 | Round(g, h, a, b, c, d, e, f, 0x243185be, w10 = READBE32(chunk + 40)); 102 | Round(f, g, h, a, b, c, d, e, 0x550c7dc3, w11 = READBE32(chunk + 44)); 103 | Round(e, f, g, h, a, b, c, d, 0x72be5d74, w12 = READBE32(chunk + 48)); 104 | Round(d, e, f, g, h, a, b, c, 0x80deb1fe, w13 = READBE32(chunk + 52)); 105 | Round(c, d, e, f, g, h, a, b, 0x9bdc06a7, w14 = READBE32(chunk + 56)); 106 | Round(b, c, d, e, f, g, h, a, 0xc19bf174, w15 = READBE32(chunk + 60)); 107 | 108 | Round(a, b, c, d, e, f, g, h, 0xe49b69c1, w0 += s1(w14) + w9 + s0(w1)); 109 | Round(h, a, b, c, d, e, f, g, 0xefbe4786, w1 += s1(w15) + w10 + s0(w2)); 110 | Round(g, h, a, b, c, d, e, f, 0x0fc19dc6, w2 += s1(w0) + w11 + s0(w3)); 111 | Round(f, g, h, a, b, c, d, e, 0x240ca1cc, w3 += s1(w1) + w12 + s0(w4)); 112 | Round(e, f, g, h, a, b, c, d, 0x2de92c6f, w4 += s1(w2) + w13 + s0(w5)); 113 | Round(d, e, f, g, h, a, b, c, 0x4a7484aa, w5 += s1(w3) + w14 + s0(w6)); 114 | Round(c, d, e, f, g, h, a, b, 0x5cb0a9dc, w6 += s1(w4) + w15 + s0(w7)); 115 | Round(b, c, d, e, f, g, h, a, 0x76f988da, w7 += s1(w5) + w0 + s0(w8)); 116 | Round(a, b, c, d, e, f, g, h, 0x983e5152, w8 += s1(w6) + w1 + s0(w9)); 117 | Round(h, a, b, c, d, e, f, g, 0xa831c66d, w9 += s1(w7) + w2 + s0(w10)); 118 | Round(g, h, a, b, c, d, e, f, 0xb00327c8, w10 += s1(w8) + w3 + s0(w11)); 119 | Round(f, g, h, a, b, c, d, e, 0xbf597fc7, w11 += s1(w9) + w4 + s0(w12)); 120 | Round(e, f, g, h, a, b, c, d, 0xc6e00bf3, w12 += s1(w10) + w5 + s0(w13)); 121 | Round(d, e, f, g, h, a, b, c, 0xd5a79147, w13 += s1(w11) + w6 + s0(w14)); 122 | Round(c, d, e, f, g, h, a, b, 0x06ca6351, w14 += s1(w12) + w7 + s0(w15)); 123 | Round(b, c, d, e, f, g, h, a, 0x14292967, w15 += s1(w13) + w8 + s0(w0)); 124 | 125 | Round(a, b, c, d, e, f, g, h, 0x27b70a85, w0 += s1(w14) + w9 + s0(w1)); 126 | Round(h, a, b, c, d, e, f, g, 0x2e1b2138, w1 += s1(w15) + w10 + s0(w2)); 127 | Round(g, h, a, b, c, d, e, f, 0x4d2c6dfc, w2 += s1(w0) + w11 + s0(w3)); 128 | Round(f, g, h, a, b, c, d, e, 0x53380d13, w3 += s1(w1) + w12 + s0(w4)); 129 | Round(e, f, g, h, a, b, c, d, 0x650a7354, w4 += s1(w2) + w13 + s0(w5)); 130 | Round(d, e, f, g, h, a, b, c, 0x766a0abb, w5 += s1(w3) + w14 + s0(w6)); 131 | Round(c, d, e, f, g, h, a, b, 0x81c2c92e, w6 += s1(w4) + w15 + s0(w7)); 132 | Round(b, c, d, e, f, g, h, a, 0x92722c85, w7 += s1(w5) + w0 + s0(w8)); 133 | Round(a, b, c, d, e, f, g, h, 0xa2bfe8a1, w8 += s1(w6) + w1 + s0(w9)); 134 | Round(h, a, b, c, d, e, f, g, 0xa81a664b, w9 += s1(w7) + w2 + s0(w10)); 135 | Round(g, h, a, b, c, d, e, f, 0xc24b8b70, w10 += s1(w8) + w3 + s0(w11)); 136 | Round(f, g, h, a, b, c, d, e, 0xc76c51a3, w11 += s1(w9) + w4 + s0(w12)); 137 | Round(e, f, g, h, a, b, c, d, 0xd192e819, w12 += s1(w10) + w5 + s0(w13)); 138 | Round(d, e, f, g, h, a, b, c, 0xd6990624, w13 += s1(w11) + w6 + s0(w14)); 139 | Round(c, d, e, f, g, h, a, b, 0xf40e3585, w14 += s1(w12) + w7 + s0(w15)); 140 | Round(b, c, d, e, f, g, h, a, 0x106aa070, w15 += s1(w13) + w8 + s0(w0)); 141 | 142 | Round(a, b, c, d, e, f, g, h, 0x19a4c116, w0 += s1(w14) + w9 + s0(w1)); 143 | Round(h, a, b, c, d, e, f, g, 0x1e376c08, w1 += s1(w15) + w10 + s0(w2)); 144 | Round(g, h, a, b, c, d, e, f, 0x2748774c, w2 += s1(w0) + w11 + s0(w3)); 145 | Round(f, g, h, a, b, c, d, e, 0x34b0bcb5, w3 += s1(w1) + w12 + s0(w4)); 146 | Round(e, f, g, h, a, b, c, d, 0x391c0cb3, w4 += s1(w2) + w13 + s0(w5)); 147 | Round(d, e, f, g, h, a, b, c, 0x4ed8aa4a, w5 += s1(w3) + w14 + s0(w6)); 148 | Round(c, d, e, f, g, h, a, b, 0x5b9cca4f, w6 += s1(w4) + w15 + s0(w7)); 149 | Round(b, c, d, e, f, g, h, a, 0x682e6ff3, w7 += s1(w5) + w0 + s0(w8)); 150 | Round(a, b, c, d, e, f, g, h, 0x748f82ee, w8 += s1(w6) + w1 + s0(w9)); 151 | Round(h, a, b, c, d, e, f, g, 0x78a5636f, w9 += s1(w7) + w2 + s0(w10)); 152 | Round(g, h, a, b, c, d, e, f, 0x84c87814, w10 += s1(w8) + w3 + s0(w11)); 153 | Round(f, g, h, a, b, c, d, e, 0x8cc70208, w11 += s1(w9) + w4 + s0(w12)); 154 | Round(e, f, g, h, a, b, c, d, 0x90befffa, w12 += s1(w10) + w5 + s0(w13)); 155 | Round(d, e, f, g, h, a, b, c, 0xa4506ceb, w13 += s1(w11) + w6 + s0(w14)); 156 | Round(c, d, e, f, g, h, a, b, 0xbef9a3f7, w14 + s1(w12) + w7 + s0(w15)); 157 | Round(b, c, d, e, f, g, h, a, 0xc67178f2, w15 + s1(w13) + w8 + s0(w0)); 158 | 159 | s[0] += a; 160 | s[1] += b; 161 | s[2] += c; 162 | s[3] += d; 163 | s[4] += e; 164 | s[5] += f; 165 | s[6] += g; 166 | s[7] += h; 167 | 168 | } 169 | 170 | // Compute SHA256(SHA256(chunk))[0] 171 | void Transform2(uint32_t* s, const unsigned char* chunk) { 172 | 173 | uint32_t t1; 174 | uint32_t t2; 175 | uint32_t w0, w1, w2, w3, w4, w5, w6, w7, w8, w9, w10, w11, w12, w13, w14, w15; 176 | 177 | uint32_t a = 0x6a09e667ul; 178 | uint32_t b = 0xbb67ae85ul; 179 | uint32_t c = 0x3c6ef372ul; 180 | uint32_t d = 0xa54ff53aul; 181 | uint32_t e = 0x510e527ful; 182 | uint32_t f = 0x9b05688cul; 183 | uint32_t g = 0x1f83d9abul; 184 | uint32_t h = 0x5be0cd19ul; 185 | 186 | Round(a, b, c, d, e, f, g, h, 0x428a2f98, w0 = READBE32(chunk + 0)); 187 | Round(h, a, b, c, d, e, f, g, 0x71374491, w1 = READBE32(chunk + 4)); 188 | Round(g, h, a, b, c, d, e, f, 0xb5c0fbcf, w2 = READBE32(chunk + 8)); 189 | Round(f, g, h, a, b, c, d, e, 0xe9b5dba5, w3 = READBE32(chunk + 12)); 190 | Round(e, f, g, h, a, b, c, d, 0x3956c25b, w4 = READBE32(chunk + 16)); 191 | Round(d, e, f, g, h, a, b, c, 0x59f111f1, w5 = READBE32(chunk + 20)); 192 | Round(c, d, e, f, g, h, a, b, 0x923f82a4, w6 = READBE32(chunk + 24)); 193 | Round(b, c, d, e, f, g, h, a, 0xab1c5ed5, w7 = READBE32(chunk + 28)); 194 | Round(a, b, c, d, e, f, g, h, 0xd807aa98, w8 = READBE32(chunk + 32)); 195 | Round(h, a, b, c, d, e, f, g, 0x12835b01, w9 = READBE32(chunk + 36)); 196 | Round(g, h, a, b, c, d, e, f, 0x243185be, w10 = READBE32(chunk + 40)); 197 | Round(f, g, h, a, b, c, d, e, 0x550c7dc3, w11 = READBE32(chunk + 44)); 198 | Round(e, f, g, h, a, b, c, d, 0x72be5d74, w12 = READBE32(chunk + 48)); 199 | Round(d, e, f, g, h, a, b, c, 0x80deb1fe, w13 = READBE32(chunk + 52)); 200 | Round(c, d, e, f, g, h, a, b, 0x9bdc06a7, w14 = READBE32(chunk + 56)); 201 | Round(b, c, d, e, f, g, h, a, 0xc19bf174, w15 = READBE32(chunk + 60)); 202 | 203 | Round(a, b, c, d, e, f, g, h, 0xe49b69c1, w0 += s1(w14) + w9 + s0(w1)); 204 | Round(h, a, b, c, d, e, f, g, 0xefbe4786, w1 += s1(w15) + w10 + s0(w2)); 205 | Round(g, h, a, b, c, d, e, f, 0x0fc19dc6, w2 += s1(w0) + w11 + s0(w3)); 206 | Round(f, g, h, a, b, c, d, e, 0x240ca1cc, w3 += s1(w1) + w12 + s0(w4)); 207 | Round(e, f, g, h, a, b, c, d, 0x2de92c6f, w4 += s1(w2) + w13 + s0(w5)); 208 | Round(d, e, f, g, h, a, b, c, 0x4a7484aa, w5 += s1(w3) + w14 + s0(w6)); 209 | Round(c, d, e, f, g, h, a, b, 0x5cb0a9dc, w6 += s1(w4) + w15 + s0(w7)); 210 | Round(b, c, d, e, f, g, h, a, 0x76f988da, w7 += s1(w5) + w0 + s0(w8)); 211 | Round(a, b, c, d, e, f, g, h, 0x983e5152, w8 += s1(w6) + w1 + s0(w9)); 212 | Round(h, a, b, c, d, e, f, g, 0xa831c66d, w9 += s1(w7) + w2 + s0(w10)); 213 | Round(g, h, a, b, c, d, e, f, 0xb00327c8, w10 += s1(w8) + w3 + s0(w11)); 214 | Round(f, g, h, a, b, c, d, e, 0xbf597fc7, w11 += s1(w9) + w4 + s0(w12)); 215 | Round(e, f, g, h, a, b, c, d, 0xc6e00bf3, w12 += s1(w10) + w5 + s0(w13)); 216 | Round(d, e, f, g, h, a, b, c, 0xd5a79147, w13 += s1(w11) + w6 + s0(w14)); 217 | Round(c, d, e, f, g, h, a, b, 0x06ca6351, w14 += s1(w12) + w7 + s0(w15)); 218 | Round(b, c, d, e, f, g, h, a, 0x14292967, w15 += s1(w13) + w8 + s0(w0)); 219 | 220 | Round(a, b, c, d, e, f, g, h, 0x27b70a85, w0 += s1(w14) + w9 + s0(w1)); 221 | Round(h, a, b, c, d, e, f, g, 0x2e1b2138, w1 += s1(w15) + w10 + s0(w2)); 222 | Round(g, h, a, b, c, d, e, f, 0x4d2c6dfc, w2 += s1(w0) + w11 + s0(w3)); 223 | Round(f, g, h, a, b, c, d, e, 0x53380d13, w3 += s1(w1) + w12 + s0(w4)); 224 | Round(e, f, g, h, a, b, c, d, 0x650a7354, w4 += s1(w2) + w13 + s0(w5)); 225 | Round(d, e, f, g, h, a, b, c, 0x766a0abb, w5 += s1(w3) + w14 + s0(w6)); 226 | Round(c, d, e, f, g, h, a, b, 0x81c2c92e, w6 += s1(w4) + w15 + s0(w7)); 227 | Round(b, c, d, e, f, g, h, a, 0x92722c85, w7 += s1(w5) + w0 + s0(w8)); 228 | Round(a, b, c, d, e, f, g, h, 0xa2bfe8a1, w8 += s1(w6) + w1 + s0(w9)); 229 | Round(h, a, b, c, d, e, f, g, 0xa81a664b, w9 += s1(w7) + w2 + s0(w10)); 230 | Round(g, h, a, b, c, d, e, f, 0xc24b8b70, w10 += s1(w8) + w3 + s0(w11)); 231 | Round(f, g, h, a, b, c, d, e, 0xc76c51a3, w11 += s1(w9) + w4 + s0(w12)); 232 | Round(e, f, g, h, a, b, c, d, 0xd192e819, w12 += s1(w10) + w5 + s0(w13)); 233 | Round(d, e, f, g, h, a, b, c, 0xd6990624, w13 += s1(w11) + w6 + s0(w14)); 234 | Round(c, d, e, f, g, h, a, b, 0xf40e3585, w14 += s1(w12) + w7 + s0(w15)); 235 | Round(b, c, d, e, f, g, h, a, 0x106aa070, w15 += s1(w13) + w8 + s0(w0)); 236 | 237 | Round(a, b, c, d, e, f, g, h, 0x19a4c116, w0 += s1(w14) + w9 + s0(w1)); 238 | Round(h, a, b, c, d, e, f, g, 0x1e376c08, w1 += s1(w15) + w10 + s0(w2)); 239 | Round(g, h, a, b, c, d, e, f, 0x2748774c, w2 += s1(w0) + w11 + s0(w3)); 240 | Round(f, g, h, a, b, c, d, e, 0x34b0bcb5, w3 += s1(w1) + w12 + s0(w4)); 241 | Round(e, f, g, h, a, b, c, d, 0x391c0cb3, w4 += s1(w2) + w13 + s0(w5)); 242 | Round(d, e, f, g, h, a, b, c, 0x4ed8aa4a, w5 += s1(w3) + w14 + s0(w6)); 243 | Round(c, d, e, f, g, h, a, b, 0x5b9cca4f, w6 += s1(w4) + w15 + s0(w7)); 244 | Round(b, c, d, e, f, g, h, a, 0x682e6ff3, w7 += s1(w5) + w0 + s0(w8)); 245 | Round(a, b, c, d, e, f, g, h, 0x748f82ee, w8 += s1(w6) + w1 + s0(w9)); 246 | Round(h, a, b, c, d, e, f, g, 0x78a5636f, w9 += s1(w7) + w2 + s0(w10)); 247 | Round(g, h, a, b, c, d, e, f, 0x84c87814, w10 += s1(w8) + w3 + s0(w11)); 248 | Round(f, g, h, a, b, c, d, e, 0x8cc70208, w11 += s1(w9) + w4 + s0(w12)); 249 | Round(e, f, g, h, a, b, c, d, 0x90befffa, w12 += s1(w10) + w5 + s0(w13)); 250 | Round(d, e, f, g, h, a, b, c, 0xa4506ceb, w13 += s1(w11) + w6 + s0(w14)); 251 | Round(c, d, e, f, g, h, a, b, 0xbef9a3f7, w14 + s1(w12) + w7 + s0(w15)); 252 | Round(b, c, d, e, f, g, h, a, 0xc67178f2, w15 + s1(w13) + w8 + s0(w0)); 253 | 254 | w0 = 0x6a09e667ul + a; 255 | w1 = 0xbb67ae85ul + b; 256 | w2 = 0x3c6ef372ul + c; 257 | w3 = 0xa54ff53aul + d; 258 | w4 = 0x510e527ful + e; 259 | w5 = 0x9b05688cul + f; 260 | w6 = 0x1f83d9abul + g; 261 | w7 = 0x5be0cd19ul + h; 262 | w8 = 0x80000000; 263 | w9 = 0; 264 | w10 = 0; 265 | w11 = 0; 266 | w12 = 0; 267 | w13 = 0; 268 | w14 = 0; 269 | w15 = 0x100; 270 | 271 | a = 0x6a09e667ul; 272 | b = 0xbb67ae85ul; 273 | c = 0x3c6ef372ul; 274 | d = 0xa54ff53aul; 275 | e = 0x510e527ful; 276 | f = 0x9b05688cul; 277 | g = 0x1f83d9abul; 278 | h = 0x5be0cd19ul; 279 | 280 | Round(a, b, c, d, e, f, g, h, 0x428a2f98, w0); 281 | Round(h, a, b, c, d, e, f, g, 0x71374491, w1); 282 | Round(g, h, a, b, c, d, e, f, 0xb5c0fbcf, w2); 283 | Round(f, g, h, a, b, c, d, e, 0xe9b5dba5, w3); 284 | Round(e, f, g, h, a, b, c, d, 0x3956c25b, w4); 285 | Round(d, e, f, g, h, a, b, c, 0x59f111f1, w5); 286 | Round(c, d, e, f, g, h, a, b, 0x923f82a4, w6); 287 | Round(b, c, d, e, f, g, h, a, 0xab1c5ed5, w7); 288 | Round(a, b, c, d, e, f, g, h, 0xd807aa98, w8); 289 | Round(h, a, b, c, d, e, f, g, 0x12835b01, w9); 290 | Round(g, h, a, b, c, d, e, f, 0x243185be, w10); 291 | Round(f, g, h, a, b, c, d, e, 0x550c7dc3, w11); 292 | Round(e, f, g, h, a, b, c, d, 0x72be5d74, w12); 293 | Round(d, e, f, g, h, a, b, c, 0x80deb1fe, w13); 294 | Round(c, d, e, f, g, h, a, b, 0x9bdc06a7, w14); 295 | Round(b, c, d, e, f, g, h, a, 0xc19bf174, w15); 296 | 297 | Round(a, b, c, d, e, f, g, h, 0xe49b69c1, w0 += s1(w14) + w9 + s0(w1)); 298 | Round(h, a, b, c, d, e, f, g, 0xefbe4786, w1 += s1(w15) + w10 + s0(w2)); 299 | Round(g, h, a, b, c, d, e, f, 0x0fc19dc6, w2 += s1(w0) + w11 + s0(w3)); 300 | Round(f, g, h, a, b, c, d, e, 0x240ca1cc, w3 += s1(w1) + w12 + s0(w4)); 301 | Round(e, f, g, h, a, b, c, d, 0x2de92c6f, w4 += s1(w2) + w13 + s0(w5)); 302 | Round(d, e, f, g, h, a, b, c, 0x4a7484aa, w5 += s1(w3) + w14 + s0(w6)); 303 | Round(c, d, e, f, g, h, a, b, 0x5cb0a9dc, w6 += s1(w4) + w15 + s0(w7)); 304 | Round(b, c, d, e, f, g, h, a, 0x76f988da, w7 += s1(w5) + w0 + s0(w8)); 305 | Round(a, b, c, d, e, f, g, h, 0x983e5152, w8 += s1(w6) + w1 + s0(w9)); 306 | Round(h, a, b, c, d, e, f, g, 0xa831c66d, w9 += s1(w7) + w2 + s0(w10)); 307 | Round(g, h, a, b, c, d, e, f, 0xb00327c8, w10 += s1(w8) + w3 + s0(w11)); 308 | Round(f, g, h, a, b, c, d, e, 0xbf597fc7, w11 += s1(w9) + w4 + s0(w12)); 309 | Round(e, f, g, h, a, b, c, d, 0xc6e00bf3, w12 += s1(w10) + w5 + s0(w13)); 310 | Round(d, e, f, g, h, a, b, c, 0xd5a79147, w13 += s1(w11) + w6 + s0(w14)); 311 | Round(c, d, e, f, g, h, a, b, 0x06ca6351, w14 += s1(w12) + w7 + s0(w15)); 312 | Round(b, c, d, e, f, g, h, a, 0x14292967, w15 += s1(w13) + w8 + s0(w0)); 313 | 314 | Round(a, b, c, d, e, f, g, h, 0x27b70a85, w0 += s1(w14) + w9 + s0(w1)); 315 | Round(h, a, b, c, d, e, f, g, 0x2e1b2138, w1 += s1(w15) + w10 + s0(w2)); 316 | Round(g, h, a, b, c, d, e, f, 0x4d2c6dfc, w2 += s1(w0) + w11 + s0(w3)); 317 | Round(f, g, h, a, b, c, d, e, 0x53380d13, w3 += s1(w1) + w12 + s0(w4)); 318 | Round(e, f, g, h, a, b, c, d, 0x650a7354, w4 += s1(w2) + w13 + s0(w5)); 319 | Round(d, e, f, g, h, a, b, c, 0x766a0abb, w5 += s1(w3) + w14 + s0(w6)); 320 | Round(c, d, e, f, g, h, a, b, 0x81c2c92e, w6 += s1(w4) + w15 + s0(w7)); 321 | Round(b, c, d, e, f, g, h, a, 0x92722c85, w7 += s1(w5) + w0 + s0(w8)); 322 | Round(a, b, c, d, e, f, g, h, 0xa2bfe8a1, w8 += s1(w6) + w1 + s0(w9)); 323 | Round(h, a, b, c, d, e, f, g, 0xa81a664b, w9 += s1(w7) + w2 + s0(w10)); 324 | Round(g, h, a, b, c, d, e, f, 0xc24b8b70, w10 += s1(w8) + w3 + s0(w11)); 325 | Round(f, g, h, a, b, c, d, e, 0xc76c51a3, w11 += s1(w9) + w4 + s0(w12)); 326 | Round(e, f, g, h, a, b, c, d, 0xd192e819, w12 += s1(w10) + w5 + s0(w13)); 327 | Round(d, e, f, g, h, a, b, c, 0xd6990624, w13 += s1(w11) + w6 + s0(w14)); 328 | Round(c, d, e, f, g, h, a, b, 0xf40e3585, w14 += s1(w12) + w7 + s0(w15)); 329 | Round(b, c, d, e, f, g, h, a, 0x106aa070, w15 += s1(w13) + w8 + s0(w0)); 330 | 331 | Round(a, b, c, d, e, f, g, h, 0x19a4c116, w0 += s1(w14) + w9 + s0(w1)); 332 | Round(h, a, b, c, d, e, f, g, 0x1e376c08, w1 += s1(w15) + w10 + s0(w2)); 333 | Round(g, h, a, b, c, d, e, f, 0x2748774c, w2 += s1(w0) + w11 + s0(w3)); 334 | Round(f, g, h, a, b, c, d, e, 0x34b0bcb5, w3 += s1(w1) + w12 + s0(w4)); 335 | Round(e, f, g, h, a, b, c, d, 0x391c0cb3, w4 += s1(w2) + w13 + s0(w5)); 336 | Round(d, e, f, g, h, a, b, c, 0x4ed8aa4a, w5 += s1(w3) + w14 + s0(w6)); 337 | Round(c, d, e, f, g, h, a, b, 0x5b9cca4f, w6 += s1(w4) + w15 + s0(w7)); 338 | Round(b, c, d, e, f, g, h, a, 0x682e6ff3, w7 += s1(w5) + w0 + s0(w8)); 339 | Round(a, b, c, d, e, f, g, h, 0x748f82ee, w8 += s1(w6) + w1 + s0(w9)); 340 | Round(h, a, b, c, d, e, f, g, 0x78a5636f, w9 += s1(w7) + w2 + s0(w10)); 341 | Round(g, h, a, b, c, d, e, f, 0x84c87814, w10 += s1(w8) + w3 + s0(w11)); 342 | Round(f, g, h, a, b, c, d, e, 0x8cc70208, w11 += s1(w9) + w4 + s0(w12)); 343 | Round(e, f, g, h, a, b, c, d, 0x90befffa, w12 += s1(w10) + w5 + s0(w13)); 344 | Round(d, e, f, g, h, a, b, c, 0xa4506ceb, w13 += s1(w11) + w6 + s0(w14)); 345 | Round(c, d, e, f, g, h, a, b, 0xbef9a3f7, w14 + s1(w12) + w7 + s0(w15)); 346 | Round(b, c, d, e, f, g, h, a, 0xc67178f2, w15 + s1(w13) + w8 + s0(w0)); 347 | 348 | s[0] = 0x6a09e667ul + a; 349 | 350 | } 351 | 352 | } // namespace sha256 353 | 354 | 355 | ////// SHA-256 356 | 357 | class CSHA256 358 | { 359 | private: 360 | uint32_t s[8]; 361 | unsigned char buf[64]; 362 | uint64_t bytes; 363 | 364 | public: 365 | static const size_t OUTPUT_SIZE = 32; 366 | 367 | CSHA256(); 368 | void Write(const unsigned char* data, size_t len); 369 | void Finalize(unsigned char hash[OUTPUT_SIZE]); 370 | 371 | }; 372 | 373 | CSHA256::CSHA256() { 374 | bytes = 0; 375 | s[0] = 0x6a09e667ul; 376 | s[1] = 0xbb67ae85ul; 377 | s[2] = 0x3c6ef372ul; 378 | s[3] = 0xa54ff53aul; 379 | s[4] = 0x510e527ful; 380 | s[5] = 0x9b05688cul; 381 | s[6] = 0x1f83d9abul; 382 | s[7] = 0x5be0cd19ul; 383 | } 384 | 385 | void CSHA256::Write(const unsigned char* data, size_t len) 386 | { 387 | const unsigned char* end = data + len; 388 | size_t bufsize = bytes % 64; 389 | if (bufsize && bufsize + len >= 64) { 390 | // Fill the buffer, and process it. 391 | memcpy(buf + bufsize, data, 64 - bufsize); 392 | bytes += 64 - bufsize; 393 | data += 64 - bufsize; 394 | _sha256::Transform(s, buf); 395 | bufsize = 0; 396 | } 397 | while (end >= data + 64) { 398 | // Process full chunks directly from the source. 399 | _sha256::Transform(s, data); 400 | bytes += 64; 401 | data += 64; 402 | } 403 | if (end > data) { 404 | // Fill the buffer with what remains. 405 | memcpy(buf + bufsize, data, end - data); 406 | bytes += end - data; 407 | } 408 | } 409 | 410 | void CSHA256::Finalize(unsigned char hash[OUTPUT_SIZE]) 411 | { 412 | unsigned char sizedesc[8]; 413 | WRITEBE64(sizedesc, bytes << 3); 414 | Write(_sha256::pad, 1 + ((119 - (bytes % 64)) % 64)); 415 | Write(sizedesc, 8); 416 | WRITEBE32(hash, s[0]); 417 | WRITEBE32(hash + 4, s[1]); 418 | WRITEBE32(hash + 8, s[2]); 419 | WRITEBE32(hash + 12, s[3]); 420 | WRITEBE32(hash + 16, s[4]); 421 | WRITEBE32(hash + 20, s[5]); 422 | WRITEBE32(hash + 24, s[6]); 423 | WRITEBE32(hash + 28, s[7]); 424 | } 425 | 426 | void sha256(unsigned char *input, size_t length, unsigned char *digest) { 427 | 428 | CSHA256 sha; 429 | sha.Write(input, length); 430 | sha.Finalize(digest); 431 | 432 | } 433 | 434 | const uint8_t sizedesc_32[8] = { 0,0,0,0,0,0,1,0 }; 435 | const uint8_t sizedesc_33[8] = { 0,0,0,0,0,0,1,8 }; 436 | const uint8_t sizedesc_65[8] = { 0,0,0,0,0,0,2,8 }; 437 | 438 | void sha256_33(unsigned char *input, unsigned char *digest) { 439 | 440 | uint32_t s[8]; 441 | 442 | _sha256::Initialize(s); 443 | memcpy(input + 33, _sha256::pad, 23); 444 | memcpy(input + 56, sizedesc_33, 8); 445 | _sha256::Transform(s, input); 446 | 447 | WRITEBE32(digest, s[0]); 448 | WRITEBE32(digest + 4, s[1]); 449 | WRITEBE32(digest + 8, s[2]); 450 | WRITEBE32(digest + 12, s[3]); 451 | WRITEBE32(digest + 16, s[4]); 452 | WRITEBE32(digest + 20, s[5]); 453 | WRITEBE32(digest + 24, s[6]); 454 | WRITEBE32(digest + 28, s[7]); 455 | 456 | 457 | } 458 | 459 | void sha256_65(unsigned char *input, unsigned char *digest) { 460 | 461 | uint32_t s[8]; 462 | 463 | memcpy(input + 65, _sha256::pad, 55); 464 | memcpy(input + 120, sizedesc_65, 8); 465 | 466 | _sha256::Initialize(s); 467 | _sha256::Transform(s, input); 468 | _sha256::Transform(s, input+64); 469 | 470 | WRITEBE32(digest, s[0]); 471 | WRITEBE32(digest + 4, s[1]); 472 | WRITEBE32(digest + 8, s[2]); 473 | WRITEBE32(digest + 12, s[3]); 474 | WRITEBE32(digest + 16, s[4]); 475 | WRITEBE32(digest + 20, s[5]); 476 | WRITEBE32(digest + 24, s[6]); 477 | WRITEBE32(digest + 28, s[7]); 478 | 479 | } 480 | 481 | void sha256_checksum(uint8_t *input, int length, uint8_t *checksum) { 482 | 483 | uint32_t s[8]; 484 | uint8_t b[64]; 485 | memcpy(b,input,length); 486 | memcpy(b + length, _sha256::pad, 56-length); 487 | WRITEBE64(b + 56, length << 3); 488 | _sha256::Transform2(s, b); 489 | WRITEBE32(checksum,s[0]); 490 | 491 | } 492 | 493 | std::string sha256_hex(unsigned char *digest) { 494 | 495 | char buf[2*32+1]; 496 | buf[2*32] = 0; 497 | for (int i = 0; i < 32; i++) 498 | sprintf(buf+i*2,"%02x",digest[i]); 499 | return std::string(buf); 500 | 501 | } 502 | 503 | -------------------------------------------------------------------------------- /hash/sha256_sse.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the VanitySearch distribution (https://github.com/JeanLucPons/VanitySearch). 3 | * Copyright (c) 2019 Jean Luc PONS. 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, version 3. 8 | * 9 | * This program is distributed in the hope that it will be useful, but 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | * General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | #include "sha256.h" 19 | #include 20 | #include 21 | #include 22 | 23 | namespace _sha256sse 24 | { 25 | 26 | 27 | #ifdef WIN64 28 | static const __declspec(align(16)) uint32_t _init[] = { 29 | #else 30 | static const uint32_t _init[] __attribute__ ((aligned (16))) = { 31 | #endif 32 | 0x6a09e667,0x6a09e667,0x6a09e667,0x6a09e667, 33 | 0xbb67ae85,0xbb67ae85,0xbb67ae85,0xbb67ae85, 34 | 0x3c6ef372,0x3c6ef372,0x3c6ef372,0x3c6ef372, 35 | 0xa54ff53a,0xa54ff53a,0xa54ff53a,0xa54ff53a, 36 | 0x510e527f,0x510e527f,0x510e527f,0x510e527f, 37 | 0x9b05688c,0x9b05688c,0x9b05688c,0x9b05688c, 38 | 0x1f83d9ab,0x1f83d9ab,0x1f83d9ab,0x1f83d9ab, 39 | 0x5be0cd19,0x5be0cd19,0x5be0cd19,0x5be0cd19 40 | }; 41 | 42 | //#define Maj(x,y,z) ((x&y)^(x&z)^(y&z)) 43 | //#define Ch(x,y,z) ((x&y)^(~x&z)) 44 | 45 | // The following functions are equivalent to the above 46 | //#define Maj(x,y,z) ((x & y) | (z & (x | y))) 47 | //#define Ch(x,y,z) (z ^ (x & (y ^ z))) 48 | 49 | #define Maj(b,c,d) _mm_or_si128(_mm_and_si128(b, c), _mm_and_si128(d, _mm_or_si128(b, c)) ) 50 | #define Ch(b,c,d) _mm_xor_si128(_mm_and_si128(b, c) , _mm_andnot_si128(b , d) ) 51 | #define ROR(x,n) _mm_or_si128( _mm_srli_epi32(x, n) , _mm_slli_epi32(x, 32 - n) ) 52 | #define SHR(x,n) _mm_srli_epi32(x, n) 53 | 54 | /* SHA256 Functions */ 55 | #define S0(x) (_mm_xor_si128(ROR((x), 2) , _mm_xor_si128(ROR((x), 13), ROR((x), 22)))) 56 | #define S1(x) (_mm_xor_si128(ROR((x), 6) , _mm_xor_si128(ROR((x), 11), ROR((x), 25)))) 57 | #define s0(x) (_mm_xor_si128(ROR((x), 7) , _mm_xor_si128(ROR((x), 18), SHR((x), 3)))) 58 | #define s1(x) (_mm_xor_si128(ROR((x), 17), _mm_xor_si128(ROR((x), 19), SHR((x), 10)))) 59 | 60 | #define add4(x0, x1, x2, x3) _mm_add_epi32(_mm_add_epi32(x0, x1), _mm_add_epi32(x2, x3)) 61 | #define add3(x0, x1, x2 ) _mm_add_epi32(_mm_add_epi32(x0, x1), x2) 62 | #define add5(x0, x1, x2, x3, x4) _mm_add_epi32(add3(x0, x1, x2), _mm_add_epi32(x3, x4)) 63 | 64 | 65 | #define Round(a, b, c, d, e, f, g, h, i, w) \ 66 | T1 = add5(h, S1(e), Ch(e, f, g), _mm_set1_epi32(i), w); \ 67 | d = _mm_add_epi32(d, T1); \ 68 | T2 = _mm_add_epi32(S0(a), Maj(a, b, c)); \ 69 | h = _mm_add_epi32(T1, T2); 70 | 71 | #define WMIX() \ 72 | w0 = add4(s1(w14), w9, s0(w1), w0); \ 73 | w1 = add4(s1(w15), w10, s0(w2), w1); \ 74 | w2 = add4(s1(w0), w11, s0(w3), w2); \ 75 | w3 = add4(s1(w1), w12, s0(w4), w3); \ 76 | w4 = add4(s1(w2), w13, s0(w5), w4); \ 77 | w5 = add4(s1(w3), w14, s0(w6), w5); \ 78 | w6 = add4(s1(w4), w15, s0(w7), w6); \ 79 | w7 = add4(s1(w5), w0, s0(w8), w7); \ 80 | w8 = add4(s1(w6), w1, s0(w9), w8); \ 81 | w9 = add4(s1(w7), w2, s0(w10), w9); \ 82 | w10 = add4(s1(w8), w3, s0(w11), w10); \ 83 | w11 = add4(s1(w9), w4, s0(w12), w11); \ 84 | w12 = add4(s1(w10), w5, s0(w13), w12); \ 85 | w13 = add4(s1(w11), w6, s0(w14), w13); \ 86 | w14 = add4(s1(w12), w7, s0(w15), w14); \ 87 | w15 = add4(s1(w13), w8, s0(w0), w15); 88 | 89 | // Initialise state 90 | void Initialize(__m128i *s) { 91 | memcpy(s, _init, sizeof(_init)); 92 | } 93 | 94 | // Perform 4 SHA in parallel using SSE2 95 | void Transform(__m128i *s, uint32_t *b0, uint32_t *b1, uint32_t *b2, uint32_t *b3) 96 | { 97 | __m128i a,b,c,d,e,f,g,h; 98 | __m128i w0, w1, w2, w3, w4, w5, w6, w7; 99 | __m128i w8, w9, w10, w11, w12, w13, w14, w15; 100 | __m128i T1, T2; 101 | 102 | a = _mm_load_si128(s + 0); 103 | b = _mm_load_si128(s + 1); 104 | c = _mm_load_si128(s + 2); 105 | d = _mm_load_si128(s + 3); 106 | e = _mm_load_si128(s + 4); 107 | f = _mm_load_si128(s + 5); 108 | g = _mm_load_si128(s + 6); 109 | h = _mm_load_si128(s + 7); 110 | 111 | w0 = _mm_set_epi32(b0[0], b1[0], b2[0], b3[0]); 112 | w1 = _mm_set_epi32(b0[1], b1[1], b2[1], b3[1]); 113 | w2 = _mm_set_epi32(b0[2], b1[2], b2[2], b3[2]); 114 | w3 = _mm_set_epi32(b0[3], b1[3], b2[3], b3[3]); 115 | w4 = _mm_set_epi32(b0[4], b1[4], b2[4], b3[4]); 116 | w5 = _mm_set_epi32(b0[5], b1[5], b2[5], b3[5]); 117 | w6 = _mm_set_epi32(b0[6], b1[6], b2[6], b3[6]); 118 | w7 = _mm_set_epi32(b0[7], b1[7], b2[7], b3[7]); 119 | w8 = _mm_set_epi32(b0[8], b1[8], b2[8], b3[8]); 120 | w9 = _mm_set_epi32(b0[9], b1[9], b2[9], b3[9]); 121 | w10 = _mm_set_epi32(b0[10], b1[10], b2[10], b3[10]); 122 | w11 = _mm_set_epi32(b0[11], b1[11], b2[11], b3[11]); 123 | w12 = _mm_set_epi32(b0[12], b1[12], b2[12], b3[12]); 124 | w13 = _mm_set_epi32(b0[13], b1[13], b2[13], b3[13]); 125 | w14 = _mm_set_epi32(b0[14], b1[14], b2[14], b3[14]); 126 | w15 = _mm_set_epi32(b0[15], b1[15], b2[15], b3[15]); 127 | 128 | Round(a, b, c, d, e, f, g, h, 0x428A2F98, w0); 129 | Round(h, a, b, c, d, e, f, g, 0x71374491, w1); 130 | Round(g, h, a, b, c, d, e, f, 0xB5C0FBCF, w2); 131 | Round(f, g, h, a, b, c, d, e, 0xE9B5DBA5, w3); 132 | Round(e, f, g, h, a, b, c, d, 0x3956C25B, w4); 133 | Round(d, e, f, g, h, a, b, c, 0x59F111F1, w5); 134 | Round(c, d, e, f, g, h, a, b, 0x923F82A4, w6); 135 | Round(b, c, d, e, f, g, h, a, 0xAB1C5ED5, w7); 136 | Round(a, b, c, d, e, f, g, h, 0xD807AA98, w8); 137 | Round(h, a, b, c, d, e, f, g, 0x12835B01, w9); 138 | Round(g, h, a, b, c, d, e, f, 0x243185BE, w10); 139 | Round(f, g, h, a, b, c, d, e, 0x550C7DC3, w11); 140 | Round(e, f, g, h, a, b, c, d, 0x72BE5D74, w12); 141 | Round(d, e, f, g, h, a, b, c, 0x80DEB1FE, w13); 142 | Round(c, d, e, f, g, h, a, b, 0x9BDC06A7, w14); 143 | Round(b, c, d, e, f, g, h, a, 0xC19BF174, w15); 144 | 145 | WMIX() 146 | 147 | Round(a, b, c, d, e, f, g, h, 0xE49B69C1, w0); 148 | Round(h, a, b, c, d, e, f, g, 0xEFBE4786, w1); 149 | Round(g, h, a, b, c, d, e, f, 0x0FC19DC6, w2); 150 | Round(f, g, h, a, b, c, d, e, 0x240CA1CC, w3); 151 | Round(e, f, g, h, a, b, c, d, 0x2DE92C6F, w4); 152 | Round(d, e, f, g, h, a, b, c, 0x4A7484AA, w5); 153 | Round(c, d, e, f, g, h, a, b, 0x5CB0A9DC, w6); 154 | Round(b, c, d, e, f, g, h, a, 0x76F988DA, w7); 155 | Round(a, b, c, d, e, f, g, h, 0x983E5152, w8); 156 | Round(h, a, b, c, d, e, f, g, 0xA831C66D, w9); 157 | Round(g, h, a, b, c, d, e, f, 0xB00327C8, w10); 158 | Round(f, g, h, a, b, c, d, e, 0xBF597FC7, w11); 159 | Round(e, f, g, h, a, b, c, d, 0xC6E00BF3, w12); 160 | Round(d, e, f, g, h, a, b, c, 0xD5A79147, w13); 161 | Round(c, d, e, f, g, h, a, b, 0x06CA6351, w14); 162 | Round(b, c, d, e, f, g, h, a, 0x14292967, w15); 163 | 164 | WMIX() 165 | 166 | Round(a, b, c, d, e, f, g, h, 0x27B70A85, w0); 167 | Round(h, a, b, c, d, e, f, g, 0x2E1B2138, w1); 168 | Round(g, h, a, b, c, d, e, f, 0x4D2C6DFC, w2); 169 | Round(f, g, h, a, b, c, d, e, 0x53380D13, w3); 170 | Round(e, f, g, h, a, b, c, d, 0x650A7354, w4); 171 | Round(d, e, f, g, h, a, b, c, 0x766A0ABB, w5); 172 | Round(c, d, e, f, g, h, a, b, 0x81C2C92E, w6); 173 | Round(b, c, d, e, f, g, h, a, 0x92722C85, w7); 174 | Round(a, b, c, d, e, f, g, h, 0xA2BFE8A1, w8); 175 | Round(h, a, b, c, d, e, f, g, 0xA81A664B, w9); 176 | Round(g, h, a, b, c, d, e, f, 0xC24B8B70, w10); 177 | Round(f, g, h, a, b, c, d, e, 0xC76C51A3, w11); 178 | Round(e, f, g, h, a, b, c, d, 0xD192E819, w12); 179 | Round(d, e, f, g, h, a, b, c, 0xD6990624, w13); 180 | Round(c, d, e, f, g, h, a, b, 0xF40E3585, w14); 181 | Round(b, c, d, e, f, g, h, a, 0x106AA070, w15); 182 | 183 | WMIX() 184 | 185 | Round(a, b, c, d, e, f, g, h, 0x19A4C116, w0); 186 | Round(h, a, b, c, d, e, f, g, 0x1E376C08, w1); 187 | Round(g, h, a, b, c, d, e, f, 0x2748774C, w2); 188 | Round(f, g, h, a, b, c, d, e, 0x34B0BCB5, w3); 189 | Round(e, f, g, h, a, b, c, d, 0x391C0CB3, w4); 190 | Round(d, e, f, g, h, a, b, c, 0x4ED8AA4A, w5); 191 | Round(c, d, e, f, g, h, a, b, 0x5B9CCA4F, w6); 192 | Round(b, c, d, e, f, g, h, a, 0x682E6FF3, w7); 193 | Round(a, b, c, d, e, f, g, h, 0x748F82EE, w8); 194 | Round(h, a, b, c, d, e, f, g, 0x78A5636F, w9); 195 | Round(g, h, a, b, c, d, e, f, 0x84C87814, w10); 196 | Round(f, g, h, a, b, c, d, e, 0x8CC70208, w11); 197 | Round(e, f, g, h, a, b, c, d, 0x90BEFFFA, w12); 198 | Round(d, e, f, g, h, a, b, c, 0xA4506CEB, w13); 199 | Round(c, d, e, f, g, h, a, b, 0xBEF9A3F7, w14); 200 | Round(b, c, d, e, f, g, h, a, 0xC67178F2, w15); 201 | 202 | s[0] = _mm_add_epi32(a, s[0]); 203 | s[1] = _mm_add_epi32(b, s[1]); 204 | s[2] = _mm_add_epi32(c, s[2]); 205 | s[3] = _mm_add_epi32(d, s[3]); 206 | s[4] = _mm_add_epi32(e, s[4]); 207 | s[5] = _mm_add_epi32(f, s[5]); 208 | s[6] = _mm_add_epi32(g, s[6]); 209 | s[7] = _mm_add_epi32(h, s[7]); 210 | 211 | } 212 | 213 | // Perform 4 SHA(SHA(bi))[0] in parallel using SSE2 214 | void Transform2(__m128i *s, uint32_t *b0, uint32_t *b1, uint32_t *b2, uint32_t *b3) { 215 | __m128i a, b, c, d, e, f, g, h; 216 | __m128i w0, w1, w2, w3, w4, w5, w6, w7; 217 | __m128i w8, w9, w10, w11, w12, w13, w14, w15; 218 | __m128i T1, T2; 219 | 220 | a = _mm_load_si128(s + 0); 221 | b = _mm_load_si128(s + 1); 222 | c = _mm_load_si128(s + 2); 223 | d = _mm_load_si128(s + 3); 224 | e = _mm_load_si128(s + 4); 225 | f = _mm_load_si128(s + 5); 226 | g = _mm_load_si128(s + 6); 227 | h = _mm_load_si128(s + 7); 228 | 229 | w0 = _mm_set_epi32(b0[0], b1[0], b2[0], b3[0]); 230 | w1 = _mm_set_epi32(b0[1], b1[1], b2[1], b3[1]); 231 | w2 = _mm_set_epi32(b0[2], b1[2], b2[2], b3[2]); 232 | w3 = _mm_set_epi32(b0[3], b1[3], b2[3], b3[3]); 233 | w4 = _mm_set_epi32(b0[4], b1[4], b2[4], b3[4]); 234 | w5 = _mm_set_epi32(b0[5], b1[5], b2[5], b3[5]); 235 | w6 = _mm_set_epi32(b0[6], b1[6], b2[6], b3[6]); 236 | w7 = _mm_set_epi32(b0[7], b1[7], b2[7], b3[7]); 237 | w8 = _mm_set_epi32(b0[8], b1[8], b2[8], b3[8]); 238 | w9 = _mm_set_epi32(b0[9], b1[9], b2[9], b3[9]); 239 | w10 = _mm_set_epi32(b0[10], b1[10], b2[10], b3[10]); 240 | w11 = _mm_set_epi32(b0[11], b1[11], b2[11], b3[11]); 241 | w12 = _mm_set_epi32(b0[12], b1[12], b2[12], b3[12]); 242 | w13 = _mm_set_epi32(b0[13], b1[13], b2[13], b3[13]); 243 | w14 = _mm_set_epi32(b0[14], b1[14], b2[14], b3[14]); 244 | w15 = _mm_set_epi32(b0[15], b1[15], b2[15], b3[15]); 245 | 246 | Round(a, b, c, d, e, f, g, h, 0x428A2F98, w0); 247 | Round(h, a, b, c, d, e, f, g, 0x71374491, w1); 248 | Round(g, h, a, b, c, d, e, f, 0xB5C0FBCF, w2); 249 | Round(f, g, h, a, b, c, d, e, 0xE9B5DBA5, w3); 250 | Round(e, f, g, h, a, b, c, d, 0x3956C25B, w4); 251 | Round(d, e, f, g, h, a, b, c, 0x59F111F1, w5); 252 | Round(c, d, e, f, g, h, a, b, 0x923F82A4, w6); 253 | Round(b, c, d, e, f, g, h, a, 0xAB1C5ED5, w7); 254 | Round(a, b, c, d, e, f, g, h, 0xD807AA98, w8); 255 | Round(h, a, b, c, d, e, f, g, 0x12835B01, w9); 256 | Round(g, h, a, b, c, d, e, f, 0x243185BE, w10); 257 | Round(f, g, h, a, b, c, d, e, 0x550C7DC3, w11); 258 | Round(e, f, g, h, a, b, c, d, 0x72BE5D74, w12); 259 | Round(d, e, f, g, h, a, b, c, 0x80DEB1FE, w13); 260 | Round(c, d, e, f, g, h, a, b, 0x9BDC06A7, w14); 261 | Round(b, c, d, e, f, g, h, a, 0xC19BF174, w15); 262 | 263 | WMIX() 264 | 265 | Round(a, b, c, d, e, f, g, h, 0xE49B69C1, w0); 266 | Round(h, a, b, c, d, e, f, g, 0xEFBE4786, w1); 267 | Round(g, h, a, b, c, d, e, f, 0x0FC19DC6, w2); 268 | Round(f, g, h, a, b, c, d, e, 0x240CA1CC, w3); 269 | Round(e, f, g, h, a, b, c, d, 0x2DE92C6F, w4); 270 | Round(d, e, f, g, h, a, b, c, 0x4A7484AA, w5); 271 | Round(c, d, e, f, g, h, a, b, 0x5CB0A9DC, w6); 272 | Round(b, c, d, e, f, g, h, a, 0x76F988DA, w7); 273 | Round(a, b, c, d, e, f, g, h, 0x983E5152, w8); 274 | Round(h, a, b, c, d, e, f, g, 0xA831C66D, w9); 275 | Round(g, h, a, b, c, d, e, f, 0xB00327C8, w10); 276 | Round(f, g, h, a, b, c, d, e, 0xBF597FC7, w11); 277 | Round(e, f, g, h, a, b, c, d, 0xC6E00BF3, w12); 278 | Round(d, e, f, g, h, a, b, c, 0xD5A79147, w13); 279 | Round(c, d, e, f, g, h, a, b, 0x06CA6351, w14); 280 | Round(b, c, d, e, f, g, h, a, 0x14292967, w15); 281 | 282 | WMIX() 283 | 284 | Round(a, b, c, d, e, f, g, h, 0x27B70A85, w0); 285 | Round(h, a, b, c, d, e, f, g, 0x2E1B2138, w1); 286 | Round(g, h, a, b, c, d, e, f, 0x4D2C6DFC, w2); 287 | Round(f, g, h, a, b, c, d, e, 0x53380D13, w3); 288 | Round(e, f, g, h, a, b, c, d, 0x650A7354, w4); 289 | Round(d, e, f, g, h, a, b, c, 0x766A0ABB, w5); 290 | Round(c, d, e, f, g, h, a, b, 0x81C2C92E, w6); 291 | Round(b, c, d, e, f, g, h, a, 0x92722C85, w7); 292 | Round(a, b, c, d, e, f, g, h, 0xA2BFE8A1, w8); 293 | Round(h, a, b, c, d, e, f, g, 0xA81A664B, w9); 294 | Round(g, h, a, b, c, d, e, f, 0xC24B8B70, w10); 295 | Round(f, g, h, a, b, c, d, e, 0xC76C51A3, w11); 296 | Round(e, f, g, h, a, b, c, d, 0xD192E819, w12); 297 | Round(d, e, f, g, h, a, b, c, 0xD6990624, w13); 298 | Round(c, d, e, f, g, h, a, b, 0xF40E3585, w14); 299 | Round(b, c, d, e, f, g, h, a, 0x106AA070, w15); 300 | 301 | WMIX() 302 | 303 | Round(a, b, c, d, e, f, g, h, 0x19A4C116, w0); 304 | Round(h, a, b, c, d, e, f, g, 0x1E376C08, w1); 305 | Round(g, h, a, b, c, d, e, f, 0x2748774C, w2); 306 | Round(f, g, h, a, b, c, d, e, 0x34B0BCB5, w3); 307 | Round(e, f, g, h, a, b, c, d, 0x391C0CB3, w4); 308 | Round(d, e, f, g, h, a, b, c, 0x4ED8AA4A, w5); 309 | Round(c, d, e, f, g, h, a, b, 0x5B9CCA4F, w6); 310 | Round(b, c, d, e, f, g, h, a, 0x682E6FF3, w7); 311 | Round(a, b, c, d, e, f, g, h, 0x748F82EE, w8); 312 | Round(h, a, b, c, d, e, f, g, 0x78A5636F, w9); 313 | Round(g, h, a, b, c, d, e, f, 0x84C87814, w10); 314 | Round(f, g, h, a, b, c, d, e, 0x8CC70208, w11); 315 | Round(e, f, g, h, a, b, c, d, 0x90BEFFFA, w12); 316 | Round(d, e, f, g, h, a, b, c, 0xA4506CEB, w13); 317 | Round(c, d, e, f, g, h, a, b, 0xBEF9A3F7, w14); 318 | Round(b, c, d, e, f, g, h, a, 0xC67178F2, w15); 319 | 320 | w0 = _mm_add_epi32(a, s[0]); 321 | w1 = _mm_add_epi32(b, s[1]); 322 | w2 = _mm_add_epi32(c, s[2]); 323 | w3 = _mm_add_epi32(d, s[3]); 324 | w4 = _mm_add_epi32(e, s[4]); 325 | w5 = _mm_add_epi32(f, s[5]); 326 | w6 = _mm_add_epi32(g, s[6]); 327 | w7 = _mm_add_epi32(h, s[7]); 328 | w8 = _mm_set1_epi32(0x80000000); 329 | w9 = _mm_xor_si128(w9,w9); 330 | w10 = _mm_xor_si128(w10, w10); 331 | w11 = _mm_xor_si128(w11, w11); 332 | w12 = _mm_xor_si128(w12, w12); 333 | w13 = _mm_xor_si128(w13, w13); 334 | w14 = _mm_xor_si128(w14, w14); 335 | w15 = _mm_set1_epi32(0x100); 336 | 337 | a = _mm_load_si128(s + 0); 338 | b = _mm_load_si128(s + 1); 339 | c = _mm_load_si128(s + 2); 340 | d = _mm_load_si128(s + 3); 341 | e = _mm_load_si128(s + 4); 342 | f = _mm_load_si128(s + 5); 343 | g = _mm_load_si128(s + 6); 344 | h = _mm_load_si128(s + 7); 345 | 346 | Round(a, b, c, d, e, f, g, h, 0x428A2F98, w0); 347 | Round(h, a, b, c, d, e, f, g, 0x71374491, w1); 348 | Round(g, h, a, b, c, d, e, f, 0xB5C0FBCF, w2); 349 | Round(f, g, h, a, b, c, d, e, 0xE9B5DBA5, w3); 350 | Round(e, f, g, h, a, b, c, d, 0x3956C25B, w4); 351 | Round(d, e, f, g, h, a, b, c, 0x59F111F1, w5); 352 | Round(c, d, e, f, g, h, a, b, 0x923F82A4, w6); 353 | Round(b, c, d, e, f, g, h, a, 0xAB1C5ED5, w7); 354 | Round(a, b, c, d, e, f, g, h, 0xD807AA98, w8); 355 | Round(h, a, b, c, d, e, f, g, 0x12835B01, w9); 356 | Round(g, h, a, b, c, d, e, f, 0x243185BE, w10); 357 | Round(f, g, h, a, b, c, d, e, 0x550C7DC3, w11); 358 | Round(e, f, g, h, a, b, c, d, 0x72BE5D74, w12); 359 | Round(d, e, f, g, h, a, b, c, 0x80DEB1FE, w13); 360 | Round(c, d, e, f, g, h, a, b, 0x9BDC06A7, w14); 361 | Round(b, c, d, e, f, g, h, a, 0xC19BF174, w15); 362 | 363 | WMIX() 364 | 365 | Round(a, b, c, d, e, f, g, h, 0xE49B69C1, w0); 366 | Round(h, a, b, c, d, e, f, g, 0xEFBE4786, w1); 367 | Round(g, h, a, b, c, d, e, f, 0x0FC19DC6, w2); 368 | Round(f, g, h, a, b, c, d, e, 0x240CA1CC, w3); 369 | Round(e, f, g, h, a, b, c, d, 0x2DE92C6F, w4); 370 | Round(d, e, f, g, h, a, b, c, 0x4A7484AA, w5); 371 | Round(c, d, e, f, g, h, a, b, 0x5CB0A9DC, w6); 372 | Round(b, c, d, e, f, g, h, a, 0x76F988DA, w7); 373 | Round(a, b, c, d, e, f, g, h, 0x983E5152, w8); 374 | Round(h, a, b, c, d, e, f, g, 0xA831C66D, w9); 375 | Round(g, h, a, b, c, d, e, f, 0xB00327C8, w10); 376 | Round(f, g, h, a, b, c, d, e, 0xBF597FC7, w11); 377 | Round(e, f, g, h, a, b, c, d, 0xC6E00BF3, w12); 378 | Round(d, e, f, g, h, a, b, c, 0xD5A79147, w13); 379 | Round(c, d, e, f, g, h, a, b, 0x06CA6351, w14); 380 | Round(b, c, d, e, f, g, h, a, 0x14292967, w15); 381 | 382 | WMIX() 383 | 384 | Round(a, b, c, d, e, f, g, h, 0x27B70A85, w0); 385 | Round(h, a, b, c, d, e, f, g, 0x2E1B2138, w1); 386 | Round(g, h, a, b, c, d, e, f, 0x4D2C6DFC, w2); 387 | Round(f, g, h, a, b, c, d, e, 0x53380D13, w3); 388 | Round(e, f, g, h, a, b, c, d, 0x650A7354, w4); 389 | Round(d, e, f, g, h, a, b, c, 0x766A0ABB, w5); 390 | Round(c, d, e, f, g, h, a, b, 0x81C2C92E, w6); 391 | Round(b, c, d, e, f, g, h, a, 0x92722C85, w7); 392 | Round(a, b, c, d, e, f, g, h, 0xA2BFE8A1, w8); 393 | Round(h, a, b, c, d, e, f, g, 0xA81A664B, w9); 394 | Round(g, h, a, b, c, d, e, f, 0xC24B8B70, w10); 395 | Round(f, g, h, a, b, c, d, e, 0xC76C51A3, w11); 396 | Round(e, f, g, h, a, b, c, d, 0xD192E819, w12); 397 | Round(d, e, f, g, h, a, b, c, 0xD6990624, w13); 398 | Round(c, d, e, f, g, h, a, b, 0xF40E3585, w14); 399 | Round(b, c, d, e, f, g, h, a, 0x106AA070, w15); 400 | 401 | WMIX() 402 | 403 | Round(a, b, c, d, e, f, g, h, 0x19A4C116, w0); 404 | Round(h, a, b, c, d, e, f, g, 0x1E376C08, w1); 405 | Round(g, h, a, b, c, d, e, f, 0x2748774C, w2); 406 | Round(f, g, h, a, b, c, d, e, 0x34B0BCB5, w3); 407 | Round(e, f, g, h, a, b, c, d, 0x391C0CB3, w4); 408 | Round(d, e, f, g, h, a, b, c, 0x4ED8AA4A, w5); 409 | Round(c, d, e, f, g, h, a, b, 0x5B9CCA4F, w6); 410 | Round(b, c, d, e, f, g, h, a, 0x682E6FF3, w7); 411 | Round(a, b, c, d, e, f, g, h, 0x748F82EE, w8); 412 | Round(h, a, b, c, d, e, f, g, 0x78A5636F, w9); 413 | Round(g, h, a, b, c, d, e, f, 0x84C87814, w10); 414 | Round(f, g, h, a, b, c, d, e, 0x8CC70208, w11); 415 | Round(e, f, g, h, a, b, c, d, 0x90BEFFFA, w12); 416 | Round(d, e, f, g, h, a, b, c, 0xA4506CEB, w13); 417 | Round(c, d, e, f, g, h, a, b, 0xBEF9A3F7, w14); 418 | Round(b, c, d, e, f, g, h, a, 0xC67178F2, w15); 419 | 420 | s[0] = _mm_add_epi32(a, s[0]); 421 | 422 | } 423 | 424 | } // end namespace 425 | 426 | void sha256sse_1B( 427 | uint32_t *i0, 428 | uint32_t *i1, 429 | uint32_t *i2, 430 | uint32_t *i3, 431 | unsigned char *d0, 432 | unsigned char *d1, 433 | unsigned char *d2, 434 | unsigned char *d3) { 435 | 436 | __m128i s[8]; 437 | 438 | _sha256sse::Initialize(s); 439 | _sha256sse::Transform(s,i0,i1,i2,i3); 440 | 441 | // Unpack 442 | __m128i mask = _mm_set_epi8(12, 13, 14, 15, /**/ 4, 5, 6, 7, /**/ 8, 9, 10, 11, /**/ 0, 1, 2, 3 ); 443 | 444 | __m128i u0 = _mm_unpacklo_epi32(s[0], s[1]); // S2_1 S2_0 S3_1 S3_0 445 | __m128i u1 = _mm_unpackhi_epi32(s[0], s[1]); // S0_1 S0_0 S1_1 S1_0 446 | 447 | __m128i u2 = _mm_unpacklo_epi32(s[2], s[3]); // S2_3 S2_2 S3_3 S3_2 448 | __m128i u3 = _mm_unpackhi_epi32(s[2], s[3]); // S0_3 S0_2 S1_3 S1_2 449 | 450 | __m128i _d3 = _mm_unpacklo_epi32(u0, u2); // S3_3 S3_1 S3_2 S3_0 451 | __m128i _d2 = _mm_unpackhi_epi32(u0, u2); // S2_3 S2_1 S2_2 S2_0 452 | __m128i _d1 = _mm_unpacklo_epi32(u1, u3); // S1_3 S1_1 S1_2 S1_0 453 | __m128i _d0 = _mm_unpackhi_epi32(u1, u3); // S0_3 S0_1 S0_2 S0_0 454 | 455 | _d0 = _mm_shuffle_epi8(_d0, mask); 456 | _d1 = _mm_shuffle_epi8(_d1, mask); 457 | _d2 = _mm_shuffle_epi8(_d2, mask); 458 | _d3 = _mm_shuffle_epi8(_d3, mask); 459 | 460 | _mm_store_si128((__m128i *)d0, _d0); 461 | _mm_store_si128((__m128i *)d1, _d1); 462 | _mm_store_si128((__m128i *)d2, _d2); 463 | _mm_store_si128((__m128i *)d3, _d3); 464 | 465 | // -------------------- 466 | 467 | u0 = _mm_unpacklo_epi32(s[4], s[5]); 468 | u1 = _mm_unpackhi_epi32(s[4], s[5]); 469 | 470 | u2 = _mm_unpacklo_epi32(s[6], s[7]); 471 | u3 = _mm_unpackhi_epi32(s[6], s[7]); 472 | 473 | _d3 = _mm_unpacklo_epi32(u0, u2); 474 | _d2 = _mm_unpackhi_epi32(u0, u2); 475 | _d1 = _mm_unpacklo_epi32(u1, u3); 476 | _d0 = _mm_unpackhi_epi32(u1, u3); 477 | 478 | _d0 = _mm_shuffle_epi8(_d0, mask); 479 | _d1 = _mm_shuffle_epi8(_d1, mask); 480 | _d2 = _mm_shuffle_epi8(_d2, mask); 481 | _d3 = _mm_shuffle_epi8(_d3, mask); 482 | 483 | _mm_store_si128((__m128i *)(d0 + 16), _d0); 484 | _mm_store_si128((__m128i *)(d1 + 16), _d1); 485 | _mm_store_si128((__m128i *)(d2 + 16), _d2); 486 | _mm_store_si128((__m128i *)(d3 + 16), _d3); 487 | 488 | } 489 | 490 | 491 | void sha256sse_2B( 492 | uint32_t *i0, 493 | uint32_t *i1, 494 | uint32_t *i2, 495 | uint32_t *i3, 496 | unsigned char *d0, 497 | unsigned char *d1, 498 | unsigned char *d2, 499 | unsigned char *d3) { 500 | 501 | __m128i s[8]; 502 | 503 | _sha256sse::Initialize(s); 504 | _sha256sse::Transform(s, i0, i1, i2, i3); 505 | _sha256sse::Transform(s, i0 + 16, i1 + 16, i2 + 16, i3 + 16); 506 | 507 | // Unpack 508 | __m128i mask = _mm_set_epi8(12, 13, 14, 15, /**/ 4, 5, 6, 7, /**/ 8, 9, 10, 11, /**/ 0, 1, 2, 3); 509 | 510 | __m128i u0 = _mm_unpacklo_epi32(s[0], s[1]); // S2_1 S2_0 S3_1 S3_0 511 | __m128i u1 = _mm_unpackhi_epi32(s[0], s[1]); // S0_1 S0_0 S1_1 S1_0 512 | 513 | __m128i u2 = _mm_unpacklo_epi32(s[2], s[3]); // S2_3 S2_2 S3_3 S3_2 514 | __m128i u3 = _mm_unpackhi_epi32(s[2], s[3]); // S0_3 S0_2 S1_3 S1_2 515 | 516 | __m128i _d3 = _mm_unpacklo_epi32(u0, u2); // S3_3 S3_1 S3_2 S3_0 517 | __m128i _d2 = _mm_unpackhi_epi32(u0, u2); // S2_3 S2_1 S2_2 S2_0 518 | __m128i _d1 = _mm_unpacklo_epi32(u1, u3); // S1_3 S1_1 S1_2 S1_0 519 | __m128i _d0 = _mm_unpackhi_epi32(u1, u3); // S0_3 S0_1 S0_2 S0_0 520 | 521 | _d0 = _mm_shuffle_epi8(_d0, mask); 522 | _d1 = _mm_shuffle_epi8(_d1, mask); 523 | _d2 = _mm_shuffle_epi8(_d2, mask); 524 | _d3 = _mm_shuffle_epi8(_d3, mask); 525 | 526 | _mm_store_si128((__m128i *)d0, _d0); 527 | _mm_store_si128((__m128i *)d1, _d1); 528 | _mm_store_si128((__m128i *)d2, _d2); 529 | _mm_store_si128((__m128i *)d3, _d3); 530 | 531 | // -------------------- 532 | 533 | u0 = _mm_unpacklo_epi32(s[4], s[5]); 534 | u1 = _mm_unpackhi_epi32(s[4], s[5]); 535 | 536 | u2 = _mm_unpacklo_epi32(s[6], s[7]); 537 | u3 = _mm_unpackhi_epi32(s[6], s[7]); 538 | 539 | _d3 = _mm_unpacklo_epi32(u0, u2); 540 | _d2 = _mm_unpackhi_epi32(u0, u2); 541 | _d1 = _mm_unpacklo_epi32(u1, u3); 542 | _d0 = _mm_unpackhi_epi32(u1, u3); 543 | 544 | _d0 = _mm_shuffle_epi8(_d0, mask); 545 | _d1 = _mm_shuffle_epi8(_d1, mask); 546 | _d2 = _mm_shuffle_epi8(_d2, mask); 547 | _d3 = _mm_shuffle_epi8(_d3, mask); 548 | 549 | _mm_store_si128((__m128i *)(d0 + 16), _d0); 550 | _mm_store_si128((__m128i *)(d1 + 16), _d1); 551 | _mm_store_si128((__m128i *)(d2 + 16), _d2); 552 | _mm_store_si128((__m128i *)(d3 + 16), _d3); 553 | 554 | } 555 | 556 | void sha256sse_checksum(uint32_t *i0, uint32_t *i1, uint32_t *i2, uint32_t *i3, 557 | uint8_t *d0, uint8_t *d1, uint8_t *d2, uint8_t *d3) { 558 | 559 | __m128i s[8]; 560 | 561 | _sha256sse::Initialize(s); 562 | _sha256sse::Transform2(s, i0, i1, i2, i3); 563 | 564 | #ifndef WIN64 565 | uint32_t *s32 = (uint32_t *)(&s[0]); 566 | *((uint32_t *)d0) = __builtin_bswap32(s32[3]); 567 | *((uint32_t *)d1) = __builtin_bswap32(s32[2]); 568 | *((uint32_t *)d2) = __builtin_bswap32(s32[1]); 569 | *((uint32_t *)d3) = __builtin_bswap32(s32[0]); 570 | #else 571 | *((uint32_t *)d0) = _byteswap_ulong(s[0].m128i_u32[3]); 572 | *((uint32_t *)d1) = _byteswap_ulong(s[0].m128i_u32[2]); 573 | *((uint32_t *)d2) = _byteswap_ulong(s[0].m128i_u32[1]); 574 | *((uint32_t *)d3) = _byteswap_ulong(s[0].m128i_u32[0]); 575 | #endif 576 | 577 | } 578 | 579 | #if 0 580 | void sha256sse_test() { 581 | 582 | unsigned char h0[32]; 583 | unsigned char h1[32]; 584 | unsigned char h2[32]; 585 | unsigned char h3[32]; 586 | unsigned char ch0[32]; 587 | unsigned char ch1[32]; 588 | unsigned char ch2[32]; 589 | unsigned char ch3[32]; 590 | unsigned char m0[64]; 591 | unsigned char m1[64]; 592 | unsigned char m2[64]; 593 | unsigned char m3[64]; 594 | 595 | strcpy((char *)m0, "This is a test message to test 01"); 596 | strcpy((char *)m1, "This is a test message to test 02"); 597 | strcpy((char *)m2, "This is a test message to test 03"); 598 | strcpy((char *)m3, "This is a test message to test 04"); 599 | 600 | sha256_33(m0, ch0); 601 | sha256_33(m1, ch1); 602 | sha256_33(m2, ch2); 603 | sha256_33(m3, ch3); 604 | 605 | sha256sse_33(m0,m1,m2,m3,h0,h1,h2,h3); 606 | 607 | if((sha256_hex(h0) != sha256_hex(ch0)) || 608 | (sha256_hex(h1) != sha256_hex(ch1)) || 609 | (sha256_hex(h2) != sha256_hex(ch2)) || 610 | (sha256_hex(h3) != sha256_hex(ch3))) { 611 | 612 | printf("SHA() Results Wrong !\n"); 613 | printf("SHA: %s\n", sha256_hex(ch0).c_str()); 614 | printf("SHA: %s\n", sha256_hex(ch1).c_str()); 615 | printf("SHA: %s\n", sha256_hex(ch2).c_str()); 616 | printf("SHA: %s\n\n", sha256_hex(ch3).c_str()); 617 | printf("SSE: %s\n", sha256_hex(h0).c_str()); 618 | printf("SSE: %s\n", sha256_hex(h1).c_str()); 619 | printf("SSE: %s\n", sha256_hex(h2).c_str()); 620 | printf("SSE: %s\n\n", sha256_hex(h3).c_str()); 621 | 622 | } 623 | 624 | printf("SHA() Results OK !\n"); 625 | 626 | } 627 | #endif 628 | --------------------------------------------------------------------------------