├── tests ├── vanitytargets.txt ├── 64.txt ├── 66.txt ├── 64.rmd ├── 66.rmd ├── minikeys.txt ├── 120.txt ├── 125.txt ├── 130.txt ├── 63.pub ├── test120.txt ├── 1to32.txt ├── puzzleswopublickey.txt ├── 1to32.rmd ├── 1to32.eth ├── in.txt ├── unsolvedpuzzles.txt ├── unsolvedpuzzles.rmd └── 1to63_65.txt ├── base58 ├── libbase58.h └── base58.c ├── .gitignore ├── TODO.md ├── util.h ├── secp256k1 ├── Random.h ├── IntGroup.h ├── Point.h ├── IntGroup.cpp ├── Point.cpp ├── SECP256k1.h ├── Random.cpp └── Int.h ├── hashing.h ├── LICENSE ├── rmd160 ├── rmd160.h └── rmd160.c ├── hash ├── sha512.h ├── sha256.h ├── ripemd160.h ├── ripemd160.cpp └── sha512.cpp ├── bloom ├── LICENSE ├── bloom.h └── bloom.cpp ├── oldbloom ├── LICENSE ├── oldbloom.h └── bloom.cpp ├── gmp256k1 ├── Random.h ├── IntGroup.h ├── IntGroup.cpp ├── Point.h ├── Point.cpp ├── Random.cpp ├── GMP256K1.h ├── IntMod.cpp ├── Int.h └── Int.cpp ├── sha3 ├── keccak.h ├── sha3.h └── keccak.c ├── xxhash ├── xxhash.c └── LICENSE ├── util.c ├── hashing.c ├── BSGSD.md ├── CHANGELOG.md └── Makefile /tests/vanitytargets.txt: -------------------------------------------------------------------------------- 1 | 1GoodBoy 2 | 1BadBoy 3 | -------------------------------------------------------------------------------- /tests/64.txt: -------------------------------------------------------------------------------- 1 | 16jY7qLJnxb7CHZyqBP8qca9d51gAjyXQN 2 | -------------------------------------------------------------------------------- /tests/66.txt: -------------------------------------------------------------------------------- 1 | 13zb1hQbWVsc2S7ZTZnP2G4undNNpdh5so 2 | -------------------------------------------------------------------------------- /tests/64.rmd: -------------------------------------------------------------------------------- 1 | 3ee4133d991f52fdf6a25c9834e0745ac74248a4 2 | -------------------------------------------------------------------------------- /tests/66.rmd: -------------------------------------------------------------------------------- 1 | 20d45a6a762535700ce9e0b216e31994335db8a5 2 | -------------------------------------------------------------------------------- /tests/minikeys.txt: -------------------------------------------------------------------------------- 1 | 15azScMmHvFPAQfQafrKr48E9MqRRXSnVv 2 | -------------------------------------------------------------------------------- /tests/120.txt: -------------------------------------------------------------------------------- 1 | 02CEB6CBBCDBDF5EF7150682150F4CE2C6F4807B349827DCDBDD1F2EFA885A2630 2 | -------------------------------------------------------------------------------- /tests/125.txt: -------------------------------------------------------------------------------- 1 | 0233709eb11e0d4439a729f21c2c443dedb727528229713f0065721ba8fa46f00e 2 | -------------------------------------------------------------------------------- /tests/130.txt: -------------------------------------------------------------------------------- 1 | 03633cbe3ec02b9401c5effa144c5b4d22f87940259634858fc7e59b1c09937852 2 | -------------------------------------------------------------------------------- /tests/63.pub: -------------------------------------------------------------------------------- 1 | 0365ec2994b8cc0a20d40dd69edfe55ca32a54bcbbaa6b0ddcff36049301a54579 2 | 3 | -------------------------------------------------------------------------------- /tests/test120.txt: -------------------------------------------------------------------------------- 1 | 043ffa1cc011a8d23dec502c7656fb3f93dbe4c61f91fd443ba444b4ec2dd8e6f0406c36edf3d8a0dfaa7b8f309b8f1276a5c04131762c23594f130a023742bdde 2 | 046534b9e9d56624f5850198f6ac462f482fec8a60262728ee79a91cac1d60f8d6a92d5131a20f78e26726a63d212158b20b14c3025ebb9968c890c4bab90bfc69 3 | 4 | -------------------------------------------------------------------------------- /base58/libbase58.h: -------------------------------------------------------------------------------- 1 | #ifndef LIBBASE58_H 2 | #define LIBBASE58_H 3 | 4 | #include 5 | #include 6 | 7 | #ifdef __cplusplus 8 | extern "C" { 9 | #endif 10 | 11 | extern bool (*b58_sha256_impl)(void *, const void *, size_t); 12 | 13 | extern bool b58tobin(void *bin, size_t *binsz, const char *b58, size_t b58sz); 14 | extern int b58check(const void *bin, size_t binsz, const char *b58, size_t b58sz); 15 | 16 | extern bool b58enc_custom(char *b58, size_t *b58sz, const void *bin, size_t binsz,char* buffer); 17 | extern bool b58enc(char *b58, size_t *b58sz, const void *bin, size_t binsz); 18 | extern bool b58check_enc(char *b58c, size_t *b58c_sz, uint8_t ver, const void *data, size_t datasz); 19 | 20 | #ifdef __cplusplus 21 | } 22 | #endif 23 | 24 | #endif -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | bPfile 2 | hexcharstoraw 3 | *.bin 4 | keyhunt 5 | bsgsd 6 | KEYFOUNDKEYFOUND.txt 7 | VANITYKEYFOUND.txt 8 | *.dat 9 | *.tbl 10 | *.blm 11 | 12 | # Prerequisites 13 | *.d 14 | 15 | # Object files 16 | *.o 17 | *.ko 18 | *.obj 19 | *.elf 20 | 21 | # Linker output 22 | *.ilk 23 | *.map 24 | *.exp 25 | 26 | # Precompiled Headers 27 | *.gch 28 | *.pch 29 | 30 | # Libraries 31 | *.lib 32 | *.a 33 | *.la 34 | *.lo 35 | 36 | # Shared objects (inc. Windows DLLs) 37 | *.dll 38 | *.so 39 | *.so.* 40 | *.dylib 41 | 42 | # Executables 43 | *.exe 44 | *.out 45 | *.app 46 | *.i*86 47 | *.x86_64 48 | *.hex 49 | 50 | # Debug files 51 | *.dSYM/ 52 | *.su 53 | *.idb 54 | *.pdb 55 | 56 | # Kernel Module Compile Results 57 | *.mod* 58 | *.cmd 59 | .tmp_versions/ 60 | modules.order 61 | Module.symvers 62 | Mkfile.old 63 | dkms.conf 64 | -------------------------------------------------------------------------------- /TODO.md: -------------------------------------------------------------------------------- 1 | #TODO 2 | - Implement the new way to genetatekey to mode `bsgs` this will improve the speed of bsgs ten times more. 3 | - GPU support 4 | - Make a test files for All cases of input data with fixed ranges of search 5 | - address BTC legacy, bech32, ETH 6 | 7 | #DONE 8 | - Optimize Point Addition, maybe with a custom bignumber lib instead libgmp 9 | This was done in the version `0.1.20210412 secp256k1` we change from libgmp to secp256k1 10 | - Added sha3 same files used by brainflayer 11 | - Added mode rmd160 12 | - Fixed the bug in Partition process of Introsort 13 | - Fixed Quicksort edges cases (All data already sorted) 14 | To fix it Introsort was inmplement 15 | - Fixed bottleneck of Point - Scalar multiplication 16 | This was fix implementing a fixed Doubling Point G 17 | Also part of this was made by bypassing it and implementing Point addition 18 | -------------------------------------------------------------------------------- /util.h: -------------------------------------------------------------------------------- 1 | #ifndef CUSTOMUTILH 2 | #define CUSTOMUTILH 3 | 4 | typedef struct str_list { 5 | int n; 6 | char **data; 7 | int *lengths; 8 | }List; 9 | 10 | typedef struct str_tokenizer { 11 | int current; 12 | int n; 13 | char **tokens; 14 | }Tokenizer; 15 | 16 | char *ltrim(char *str, const char *seps); 17 | char *rtrim(char *str, const char *seps); 18 | char *trim(char *str, const char *seps); 19 | int indexOf(char *s,const char **array,int length_array); 20 | 21 | int hexchr2bin(char hex, char *out); 22 | int hexs2bin(char *hex, unsigned char *out); 23 | char *tohex(char *ptr,int length); 24 | void tohex_dst(char *ptr,int length,char *dst); 25 | 26 | int hasMoreTokens(Tokenizer *t); 27 | char *nextToken(Tokenizer *t); 28 | 29 | int isValidHex(char *data); 30 | void freetokenizer(Tokenizer *t); 31 | void stringtokenizer(char *data,Tokenizer *t); 32 | 33 | #endif // CUSTOMUTILH 34 | -------------------------------------------------------------------------------- /secp256k1/Random.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the BSGS distribution (https://github.com/JeanLucPons/BSGS). 3 | * Copyright (c) 2020 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 RANDOM_H 19 | #define RANDOM_H 20 | 21 | double rnd(); 22 | unsigned long rndl(); 23 | void rseed(unsigned long seed); 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /hashing.h: -------------------------------------------------------------------------------- 1 | #ifndef HASHSING 2 | #define HASHSING 3 | 4 | int sha256(const unsigned char *data, size_t length, unsigned char *digest); 5 | int rmd160(const unsigned char *data, size_t length, unsigned char *digest); 6 | int keccak(const unsigned char *data, size_t length, unsigned char *digest); 7 | bool sha256_file(const char* file_name, unsigned char * checksum); 8 | 9 | int rmd160_4(size_t length, const unsigned char *data0, const unsigned char *data1, 10 | const unsigned char *data2, const unsigned char *data3, 11 | unsigned char *digest0, unsigned char *digest1, 12 | unsigned char *digest2, unsigned char *digest3); 13 | 14 | int sha256_4(size_t length, const unsigned char *data0, const unsigned char *data1, 15 | const unsigned char *data2, const unsigned char *data3, 16 | unsigned char *digest0, unsigned char *digest1, 17 | unsigned char *digest2, unsigned char *digest3); 18 | 19 | #endif // HASHSING -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Luis Alberto 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /secp256k1/IntGroup.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the BSGS distribution (https://github.com/JeanLucPons/BSGS). 3 | * Copyright (c) 2020 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 INTGROUPH 19 | #define INTGROUPH 20 | 21 | #include "Int.h" 22 | #include 23 | 24 | class IntGroup { 25 | 26 | public: 27 | 28 | IntGroup(int size); 29 | ~IntGroup(); 30 | void Set(Int *pts); 31 | void ModInv(); 32 | 33 | private: 34 | 35 | Int *ints; 36 | Int *subp; 37 | int size; 38 | 39 | }; 40 | 41 | #endif // INTGROUPCPUH 42 | -------------------------------------------------------------------------------- /tests/1to32.txt: -------------------------------------------------------------------------------- 1 | 1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH 2 | 1CUNEBjYrCn2y1SdiUMohaKUi4wpP326Lb 3 | 19ZewH8Kk1PDbSNdJ97FP4EiCjTRaZMZQA 4 | 1EhqbyUMvvs7BfL8goY6qcPbD6YKfPqb7e 5 | 1E6NuFjCi27W5zoXg8TRdcSRq84zJeBW3k 6 | 1PitScNLyp2HCygzadCh7FveTnfmpPbfp8 7 | 1McVt1vMtCC7yn5b9wgX1833yCcLXzueeC 8 | 1M92tSqNmQLYw33fuBvjmeadirh1ysMBxK 9 | 1CQFwcjw1dwhtkVWBttNLDtqL7ivBonGPV 10 | 1LeBZP5QCwwgXRtmVUvTVrraqPUokyLHqe 11 | 1PgQVLmst3Z314JrQn5TNiys8Hc38TcXJu 12 | 1DBaumZxUkM4qMQRt2LVWyFJq5kDtSZQot 13 | 1Pie8JkxBT6MGPz9Nvi3fsPkr2D8q3GBc1 14 | 1ErZWg5cFCe4Vw5BzgfzB74VNLaXEiEkhk 15 | 1QCbW9HWnwQWiQqVo5exhAnmfqKRrCRsvW 16 | 1BDyrQ6WoF8VN3g9SAS1iKZcPzFfnDVieY 17 | 1HduPEXZRdG26SUT5Yk83mLkPyjnZuJ7Bm 18 | 1GnNTmTVLZiqQfLbAdp9DVdicEnB5GoERE 19 | 1NWmZRpHH4XSPwsW6dsS3nrNWfL1yrJj4w 20 | 1HsMJxNiV7TLxmoF6uJNkydxPFDog4NQum 21 | 14oFNXucftsHiUMY8uctg6N487riuyXs4h 22 | 1CfZWK1QTQE3eS9qn61dQjV89KDjZzfNcv 23 | 1L2GM8eE7mJWLdo3HZS6su1832NX2txaac 24 | 1rSnXMr63jdCuegJFuidJqWxUPV7AtUf7 25 | 15JhYXn6Mx3oF4Y7PcTAv2wVVAuCFFQNiP 26 | 1JVnST957hGztonaWK6FougdtjxzHzRMMg 27 | 128z5d7nN7PkCuX5qoA4Ys6pmxUYnEy86k 28 | 12jbtzBb54r97TCwW3G1gCFoumpckRAPdY 29 | 19EEC52krRUK1RkUAEZmQdjTyHT7Gp1TYT 30 | 1LHtnpd8nU5VHEMkG2TMYYNUjjLc992bps 31 | 1LhE6sCTuGae42Axu1L1ZB7L96yi9irEBE 32 | 1FRoHA9xewq7DjrZ1psWJVeTer8gHRqEvR 33 | 34 | -------------------------------------------------------------------------------- /rmd160/rmd160.h: -------------------------------------------------------------------------------- 1 | /* RMD160.H - header file for RMD160.C 2 | */ 3 | #ifndef _RMD160_H_ 4 | #define _RMD160_H_ 5 | 6 | #include 7 | #include 8 | 9 | #define RMD160_BLOCKBYTES 64 10 | #define RMD160_BLOCKWORDS 16 11 | 12 | #define RMD160_HASHBYTES 20 13 | #define RMD160_HASHWORDS 5 14 | 15 | /* For compatibility */ 16 | #define RIPEMD160_BLOCKBYTES 64 17 | #define RIPEMD160_BLOCKWORDS 16 18 | 19 | #define RIPEMD160_HASHBYTES 20 20 | #define RIPEMD160_HASHWORDS 5 21 | 22 | /* RIPEMD160 context. */ 23 | typedef struct RMD160Context { 24 | uint32_t key[RIPEMD160_BLOCKWORDS]; 25 | uint32_t iv[RIPEMD160_HASHWORDS]; 26 | uint32_t bytesHi, bytesLo; 27 | } RMD160_CTX; 28 | 29 | #define RIPEMD160Context RMD160Context 30 | 31 | #ifdef _WIN64 32 | #else 33 | #include 34 | 35 | __BEGIN_DECLS 36 | #endif 37 | 38 | void RMD160Init(RMD160_CTX *); 39 | void RMD160Update(RMD160_CTX *, const unsigned char *, unsigned int); 40 | void RMD160Final(unsigned char [RMD160_HASHBYTES], RMD160_CTX *); 41 | char * RMD160End(RMD160_CTX *, char *); 42 | char * RMD160File(const char *, char *); 43 | void RMD160Data(const unsigned char *, unsigned int, char *); 44 | #ifdef _WIN64 45 | #else 46 | __END_DECLS 47 | 48 | #endif 49 | 50 | #endif /* _RMD160_H_ */ 51 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /secp256k1/Point.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the BSGS distribution (https://github.com/JeanLucPons/BSGS). 3 | * Copyright (c) 2020 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 POINTH 19 | #define POINTH 20 | 21 | #include "Int.h" 22 | 23 | class Point { 24 | 25 | public: 26 | 27 | Point(); 28 | Point(Int *cx,Int *cy,Int *cz); 29 | Point(Int *cx, Int *cz); 30 | Point(const Point &p); 31 | ~Point(); 32 | bool isZero(); 33 | bool equals(Point &p); 34 | void Set(Point &p); 35 | void Set(Int *cx, Int *cy,Int *cz); 36 | void Clear(); 37 | void Reduce(); 38 | //std::string toString(); 39 | 40 | Int x; 41 | Int y; 42 | Int z; 43 | 44 | }; 45 | 46 | #endif // POINTH 47 | -------------------------------------------------------------------------------- /tests/puzzleswopublickey.txt: -------------------------------------------------------------------------------- 1 | 3ee4133d991f52fdf6a25c9834e0745ac74248a4 2 | 20d45a6a762535700ce9e0b216e31994335db8a5 3 | 739437bb3dd6d1983e66629c5f08c70e52769371 4 | e0b8a2baee1b77fc703455f39d51477451fc8cfc 5 | 61eb8a50c86b0584bb727dd65bed8d2400d6d5aa 6 | f6f5431d25bbf7b12e8add9af5e3475c44a0a5b8 7 | bf7413e8df4e7a34ce9dc13e2f2648783ec54adb 8 | 105b7f253f0ebd7843adaebbd805c944bfb863e4 9 | 9f1adb20baeacc38b3f49f3df6906a0e48f2df3d 10 | 86f9fea5cdecf033161dd2f8f8560768ae0a6d14 11 | 783c138ac81f6a52398564bb17455576e8525b29 12 | 35003c3ef8759c92092f8488fca59a042859018c 13 | 67671d5490c272e3ab7ddd34030d587738df33da 14 | 351e605fac813965951ba433b7c2956bf8ad95ce 15 | 20d28d4e87543947c7e4913bcdceaa16e2f8f061 16 | 24cef184714bbd030833904f5265c9c3e12a95a2 17 | 7c99ce73e19f9fbfcce4825ae88261e2b0b0b040 18 | c60111ed3d63b49665747b0e31eb382da5193535 19 | fbc708d671c03e26661b9c08f77598a529858b5e 20 | 38a968fdfb457654c51bcfc4f9174d6ee487bb41 21 | 5c3862203d1e44ab3af441503e22db97b1c5097e 22 | 9978f61b92d16c5f1a463a0995df70da1f7a7d2a 23 | 6534b31208fe6e100d29f9c9c75aac8bf06fbb38 24 | 463013cd41279f2fd0c31d0a16db3972bfffac8d 25 | c6927a00970d0165327d0a6db7950f05720c295c 26 | 2da63cbd251d23c7b633cb287c09e6cf888b3fe4 27 | 578d94dc6f40fff35f91f6fba9b71c46b361dff2 28 | 7eefddd979a1d6bb6f29757a1f463579770ba566 29 | c01bf430a97cbcdaedddba87ef4ea21c456cebdb 30 | -------------------------------------------------------------------------------- /bloom/LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Copyright (c) 2012,2015,2016,2017 Jyri J. Virkki 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions are 7 | met: 8 | 9 | 1. Redistributions of source code must retain the above copyright 10 | notice, this list of conditions and the following disclaimer. 11 | 12 | 2. Redistributions in binary form must reproduce the above copyright 13 | notice, this list of conditions and the following disclaimer in the 14 | documentation and/or other materials provided with the distribution. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 | HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | -------------------------------------------------------------------------------- /oldbloom/LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Copyright (c) 2012,2015,2016,2017 Jyri J. Virkki 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions are 7 | met: 8 | 9 | 1. Redistributions of source code must retain the above copyright 10 | notice, this list of conditions and the following disclaimer. 11 | 12 | 2. Redistributions in binary form must reproduce the above copyright 13 | notice, this list of conditions and the following disclaimer in the 14 | documentation and/or other materials provided with the distribution. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 | HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | -------------------------------------------------------------------------------- /gmp256k1/Random.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Keyhunt distribution (https://github.com/albertobsd/keyhunt). 3 | Copyright (c) 2020 Luis Alberto 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | */ 23 | 24 | #ifndef RANDOM_H 25 | #define RANDOM_H 26 | 27 | void int_randominit(); 28 | int random_bytes(unsigned char *buffer,int bytes); 29 | 30 | #endif -------------------------------------------------------------------------------- /tests/1to32.rmd: -------------------------------------------------------------------------------- 1 | 751e76e8199196d454941c45d1b3a323f1433bd6 2 | 7dd65592d0ab2fe0d0257d571abf032cd9db93dc 3 | 5dedfbf9ea599dd4e3ca6a80b333c472fd0b3f69 4 | 9652d86bedf43ad264362e6e6eba6eb764508127 5 | 8f9dff39a81ee4abcbad2ad8bafff090415a2be8 6 | f93ec34e9e34a8f8ff7d600cdad83047b1bcb45c 7 | e2192e8a7dd8dd1c88321959b477968b941aa973 8 | dce76b2613052ea012204404a97b3c25eac31715 9 | 7d0f6c64afb419bbd7e971e943d7404b0e0daab4 10 | d7729816650e581d7462d52ad6f732da0e2ec93b 11 | f8c698da3164ef8fa4258692d118cc9a902c5acc 12 | 85a1f9ba4da24c24e582d9b891dacbd1b043f971 13 | f932d0188616c964416b91fb9cf76ba9790a921e 14 | 97f9281a1383879d72ac52a6a3e9e8b9a4a4f655 15 | fe7c45126731f7384640b0b0045fd40bac72e2a2 16 | 7025b4efb3ff42eb4d6d71fab6b53b4f4967e3dd 17 | b67cb6edeabc0c8b927c9ea327628e7aa63e2d52 18 | ad1e852b08eba53df306ec9daa8c643426953f94 19 | ebfbe6819fcdebab061732ce91df7d586a037dee 20 | b907c3a2a3b27789dfb509b730dd47703c272868 21 | 29a78213caa9eea824acf08022ab9dfc83414f56 22 | 7ff45303774ef7a52fffd8011981034b258cb86b 23 | d0a79df189fe1ad5c306cc70497b358415da579e 24 | 0959e80121f36aea13b3bad361c15dac26189e2f 25 | 2f396b29b27324300d0c59b17c3abc1835bd3dbb 26 | bfebb73562d4541b32a02ba664d140b5a574792f 27 | 0c7aaf6caa7e5424b63d317f0f8f1f9fa40d5560 28 | 1306b9e4ff56513a476841bac7ba48d69516b1da 29 | 5a416cc9148f4a377b672c8ae5d3287adaafadec 30 | d39c4704664e1deb76c9331e637564c257d68a08 31 | d805f6f251f7479ebd853b3d0f4b9b2656d92f1d 32 | 9e42601eeaedc244e15f17375adb0e2cd08efdc9 33 | 34 | -------------------------------------------------------------------------------- /tests/1to32.eth: -------------------------------------------------------------------------------- 1 | 0x7e5f4552091a69125d5dfcb7b8c2659029395bdf 2 | 0x6813eb9362372eef6200f3b1dbc3f819671cba69 3 | 0xd41c057fd1c78805aac12b0a94a405c0461a6fbb 4 | 0xf1f6619b38a98d6de0800f1defc0a6399eb6d30c 5 | 0x157bfbecd023fd6384dad2bded5dad7e27bf92e4 6 | 0xf472086186382fca55cd182de196520abd76f69d 7 | 0x1ac6f9601f2f616badcea8a0a307e1a3c14767a4 8 | 0xf084bbaabee1a700a8faa404027db620a5aa0059 9 | 0x4aff4d8fa086e079584c5532569135d1ac620a87 10 | 0x86b1b106aeac5c5d2b16f9596755811cf976f34e 11 | 0x2c559fed4b8a8e12e170740d4dabb4fc7d3f9b49 12 | 0xd15a50df0d1d54b0931f462fda35a458aa301e42 13 | 0xdba02a942650eb93cf2566dbcb7d945b9544afcd 14 | 0x0fc0e54f047efb3fd041837213ecb572ad594263 15 | 0x8b7696dca505e006408f9b0e8bdffdd20993e15e 16 | 0xf1527b97640c3935df498110dccd4ce9181c7743 17 | 0x6ef0691fa9edd5a21e6be331d5c4aa21c18a520d 18 | 0x3224ebd2c99ca8afa709701d40868f16b38c70a2 19 | 0xf050fe24f538bf83140a0af5a725b4f8d16f242e 20 | 0x5abfc823a8b0691ebada0a41cd6fab2f15a0a628 21 | 0x4ec431274ed8e1996f1451775b9e11e7db613955 22 | 0x504ec8f62a160702b3414a2b460789e9c36b7a2a 23 | 0x86df25873cb695d3fa3781f34a6b55e5f17dba74 24 | 0xd2c71c0b28f045d0e6facc19a3a6a81a85a17ce4 25 | 0xa96e58a5ca39ed6baf8bc7692e7d79df039fbcb8 26 | 0xe392229bd7664f4ce539303ce60a2235d915caca 27 | 0x2dc9fb4b9ffa330b3c3371311a31e08e962241af 28 | 0xfda5c442e76a95f96c09782f1a15d3b58e32404f 29 | 0x5f3358241c45ac7987f5ed24e4003843d4c369cb 30 | 0x083522c909665aa7ac151892a3a847cbb382b5d4 31 | 0xbd21f11c662cad4fa062e5741310083004086e52 32 | 0xa1401f0c1e75cc40055d9e6a3f25adf11ea876ec 33 | -------------------------------------------------------------------------------- /sha3/keccak.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2015 Taylor R. Campbell 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #ifndef KECCAK_H 28 | #define KECCAK_H 29 | 30 | #include 31 | 32 | void keccakf1600(uint64_t A[25]); 33 | 34 | #endif /* KECCAK_H */ 35 | -------------------------------------------------------------------------------- /gmp256k1/IntGroup.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Keyhunt distribution (https://github.com/albertobsd/keyhunt). 3 | Copyright (c) 2020 Luis Alberto 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | */ 23 | 24 | #ifndef INTGROUPH 25 | #define INTGROUPH 26 | 27 | #include "Int.h" 28 | #include 29 | 30 | class IntGroup { 31 | public: 32 | IntGroup(int size); 33 | ~IntGroup(); 34 | void Set(Int *pts); 35 | void ModInv(); 36 | private: 37 | Int *ints; 38 | Int *subp; 39 | int size; 40 | }; 41 | 42 | #endif // INTGROUPCPUH 43 | -------------------------------------------------------------------------------- /secp256k1/IntGroup.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the BSGS distribution (https://github.com/JeanLucPons/BSGS). 3 | * Copyright (c) 2020 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 "IntGroup.h" 19 | 20 | using namespace std; 21 | 22 | IntGroup::IntGroup(int size) { 23 | this->size = size; 24 | subp = (Int *)malloc(size * sizeof(Int)); 25 | } 26 | 27 | IntGroup::~IntGroup() { 28 | free(subp); 29 | } 30 | 31 | void IntGroup::Set(Int *pts) { 32 | ints = pts; 33 | } 34 | 35 | // Compute modular inversion of the whole group 36 | void IntGroup::ModInv() { 37 | 38 | Int newValue; 39 | Int inverse; 40 | 41 | subp[0].Set(&ints[0]); 42 | for (int i = 1; i < size; i++) { 43 | subp[i].ModMulK1(&subp[i - 1], &ints[i]); 44 | } 45 | 46 | // Do the inversion 47 | inverse.Set(&subp[size - 1]); 48 | inverse.ModInv(); 49 | 50 | for (int i = size - 1; i > 0; i--) { 51 | newValue.ModMulK1(&subp[i - 1], &inverse); 52 | inverse.ModMulK1(&ints[i]); 53 | ints[i].Set(&newValue); 54 | } 55 | 56 | ints[0].Set(&inverse); 57 | 58 | } -------------------------------------------------------------------------------- /gmp256k1/IntGroup.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the BSGS distribution (https://github.com/JeanLucPons/BSGS). 3 | * Copyright (c) 2020 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 "IntGroup.h" 19 | 20 | using namespace std; 21 | 22 | IntGroup::IntGroup(int size) { 23 | this->size = size; 24 | subp = (Int *)calloc(size,sizeof(Int)); 25 | } 26 | 27 | IntGroup::~IntGroup() { 28 | free(subp); 29 | } 30 | 31 | void IntGroup::Set(Int *pts) { 32 | ints = pts; 33 | } 34 | 35 | // Compute modular inversion of the whole group 36 | void IntGroup::ModInv() { 37 | 38 | Int newValue; 39 | Int inverse; 40 | 41 | subp[0].Set(&ints[0]); 42 | for (int i = 1; i < size; i++) { 43 | subp[i].ModMulK1(&subp[i - 1], &ints[i]); 44 | } 45 | 46 | // Do the inversion 47 | inverse.Set(&subp[size - 1]); 48 | inverse.ModInv(); 49 | 50 | for (int i = size - 1; i > 0; i--) { 51 | newValue.ModMulK1(&subp[i - 1], &inverse); 52 | inverse.ModMulK1(&ints[i]); 53 | ints[i].Set(&newValue); 54 | } 55 | 56 | ints[0].Set(&inverse); 57 | 58 | } -------------------------------------------------------------------------------- /gmp256k1/Point.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Keyhunt distribution (https://github.com/albertobsd/keyhunt). 3 | Copyright (c) 2020 Luis Alberto 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | */ 23 | 24 | #ifndef POINTH 25 | #define POINTH 26 | 27 | #include "Int.h" 28 | 29 | class Point { 30 | public: 31 | Point(); 32 | Point(Int *cx, Int *cy,Int *cz); 33 | Point& operator=(const Point& other); 34 | Point(const Point &p); 35 | ~Point(); 36 | bool isZero(); 37 | bool equals(Point &p); 38 | void Set(Point &p); 39 | void Set(Int *cx, Int *cy,Int *cz); 40 | void Clear(); 41 | void Reduce(); 42 | void print(const char *str); 43 | Int x; 44 | Int y; 45 | Int z; 46 | }; 47 | 48 | #endif // POINTH -------------------------------------------------------------------------------- /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 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | void sha256(uint8_t *input,size_t length, uint8_t *digest); 27 | void sha256_33(uint8_t *input, uint8_t *digest); 28 | void sha256_65(uint8_t *input, uint8_t *digest); 29 | void sha256_checksum(uint8_t *input, int length, uint8_t *checksum); 30 | bool sha256_file(const char* file_name, uint8_t* checksum); 31 | void sha256sse_1B(uint32_t *i0, uint32_t *i1, uint32_t *i2, uint32_t *i3, 32 | uint8_t *d0, uint8_t *d1, uint8_t *d2, uint8_t *d3); 33 | void sha256sse_2B(uint32_t *i0, uint32_t *i1, uint32_t *i2, uint32_t *i3, 34 | uint8_t *d0, uint8_t *d1, uint8_t *d2, uint8_t *d3); 35 | void sha256sse_checksum(uint32_t *i0, uint32_t *i1, uint32_t *i2, uint32_t *i3, 36 | uint8_t *d0, uint8_t *d1, uint8_t *d2, uint8_t *d3); 37 | std::string sha256_hex(unsigned char *digest); 38 | void sha256sse_test(); 39 | 40 | #endif -------------------------------------------------------------------------------- /secp256k1/Point.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the BSGS distribution (https://github.com/JeanLucPons/BSGS). 3 | * Copyright (c) 2020 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 "Point.h" 19 | 20 | Point::Point() { 21 | } 22 | 23 | Point::Point(const Point &p) { 24 | x.Set((Int *)&p.x); 25 | y.Set((Int *)&p.y); 26 | z.Set((Int *)&p.z); 27 | } 28 | 29 | Point::Point(Int *cx,Int *cy,Int *cz) { 30 | x.Set(cx); 31 | y.Set(cy); 32 | z.Set(cz); 33 | } 34 | 35 | Point::Point(Int *cx, Int *cz) { 36 | x.Set(cx); 37 | z.Set(cz); 38 | } 39 | 40 | void Point::Clear() { 41 | x.SetInt32(0); 42 | y.SetInt32(0); 43 | z.SetInt32(0); 44 | } 45 | 46 | void Point::Set(Int *cx, Int *cy,Int *cz) { 47 | x.Set(cx); 48 | y.Set(cy); 49 | z.Set(cz); 50 | } 51 | 52 | Point::~Point() { 53 | } 54 | 55 | void Point::Set(Point &p) { 56 | x.Set(&p.x); 57 | y.Set(&p.y); 58 | z.Set(&p.z); 59 | } 60 | 61 | bool Point::isZero() { 62 | return x.IsZero() && y.IsZero(); 63 | } 64 | 65 | void Point::Reduce() { 66 | 67 | Int i(&z); 68 | i.ModInv(); 69 | x.ModMul(&x,&i); 70 | y.ModMul(&y,&i); 71 | z.SetInt32(1); 72 | 73 | } 74 | 75 | bool Point::equals(Point &p) { 76 | return x.IsEqual(&p.x) && y.IsEqual(&p.y) && z.IsEqual(&p.z); 77 | } 78 | -------------------------------------------------------------------------------- /xxhash/xxhash.c: -------------------------------------------------------------------------------- 1 | /* 2 | * xxHash - Extremely Fast Hash algorithm 3 | * Copyright (C) 2012-2020 Yann Collet 4 | * 5 | * BSD 2-Clause License (https://www.opensource.org/licenses/bsd-license.php) 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are 9 | * met: 10 | * 11 | * * Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * * Redistributions in binary form must reproduce the above 14 | * copyright notice, this list of conditions and the following disclaimer 15 | * in the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | * You can contact the author at: 31 | * - xxHash homepage: https://www.xxhash.com 32 | * - xxHash source repository: https://github.com/Cyan4973/xxHash 33 | */ 34 | 35 | 36 | /* 37 | * xxhash.c instantiates functions defined in xxhash.h 38 | */ 39 | 40 | #define XXH_STATIC_LINKING_ONLY /* access advanced declarations */ 41 | #define XXH_IMPLEMENTATION /* access definitions */ 42 | 43 | #include "xxhash.h" 44 | -------------------------------------------------------------------------------- /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 | #include 25 | #include 26 | #include 27 | 28 | /** A hasher class for RIPEMD-160. */ 29 | class CRIPEMD160 30 | { 31 | private: 32 | uint32_t s[5]; 33 | unsigned char buf[64]; 34 | uint64_t bytes; 35 | 36 | public: 37 | CRIPEMD160(); 38 | void Write(const unsigned char* data, size_t len); 39 | void Finalize(unsigned char hash[20]); 40 | }; 41 | 42 | void ripemd160(unsigned char *input,int length,unsigned char *digest); 43 | void ripemd160_32(unsigned char *input, unsigned char *digest); 44 | void ripemd160sse_32(uint8_t *i0, uint8_t *i1, uint8_t *i2, uint8_t *i3, 45 | uint8_t *d0, uint8_t *d1, uint8_t *d2, uint8_t *d3); 46 | void ripemd160sse_test(); 47 | std::string ripemd160_hex(unsigned char *digest); 48 | 49 | static inline bool ripemd160_comp_hash(uint8_t *h0, uint8_t *h1) { 50 | uint32_t *h0i = (uint32_t *)h0; 51 | uint32_t *h1i = (uint32_t *)h1; 52 | return (h0i[0] == h1i[0]) && 53 | (h0i[1] == h1i[1]) && 54 | (h0i[2] == h1i[2]) && 55 | (h0i[3] == h1i[3]) && 56 | (h0i[4] == h1i[4]); 57 | } 58 | 59 | #endif // RIPEMD160_H 60 | -------------------------------------------------------------------------------- /tests/in.txt: -------------------------------------------------------------------------------- 1 | 0459A3BFDAD718C9D3FAC7C187F1139F0815AC5D923910D516E186AFDA28B221DC994327554CED887AAE5D211A2407CDD025CFC3779ECB9C9D7F2F1A1DDF3E9FF8 2 | 04A50FBBB20757CC0E9C41C49DD9DF261646EE7936272F3F68C740C9DA50D42BCD3E48440249D6BC78BC928AA52B1921E9690EBA823CBC7F3AF54B3707E6A73F34 3 | 0404A49211C0FE07C9F7C94695996F8826E09545375A3CF9677F2D780A3EB70DE3BD05357CAF8340CB041B1D46C5BB6B88CD9859A083B0804EF63D498B29D31DD1 4 | 040B39E3F26AF294502A5BE708BB87AEDD9F895868011E60C1D2ABFCA202CD7A4D1D18283AF49556CF33E1EA71A16B2D0E31EE7179D88BE7F6AA0A7C5498E5D97F 5 | 04837A31977A73A630C436E680915934A58B8C76EB9B57A42C3C717689BE8C0493E46726DE04352832790FD1C99D9DDC2EE8A96E50CAD4DCC3AF1BFB82D51F2494 6 | 040ECDB6359D41D2FD37628C718DDA9BE30E65801A88A00C3C5BDF36E7EE6ADBBAD71A2A535FCB54D56913E7F37D8103BA33ED6441D019D0922AC363FCC792C29A 7 | 0422DD52FCFA3A4384F0AFF199D019E481D335923D8C00BADAD42FFFC80AF8FCF038F139D652842243FC841E7C5B3E477D901F88C5AB0B88EE13D80080E413F2ED 8 | 04DB4F1B249406B8BD662F78CBA46F5E90E20FE27FC69D0FBAA2F06E6E50E536695DF83B68FD0F396BB9BFCF6D4FE312F32A43CF3FA1FE0F81DF70C877593B64E0 9 | 043BD0330D7381917F8860F1949ACBCCFDC7863422EEE2B6DB7EDD551850196687528B6D2BC0AA7A5855D168B26C6BAF9DDCD04B585D42C7B9913F60421716D37A 10 | 04332A02CA42C481EAADB7ADB97DF89033B23EA291FDA809BEA3CE5C3B73B20C49C410D1AD42A9247EB8FF217935C9E28411A08B325FBF28CC2AF8182CE2B5CE38 11 | 04513981849DE1A1327DEF34B51F5011C5070603CA22E6D868263CB7C908525F0C19EBA6BD2A8DCF651E4342512EDEACB6EA22DA323A194E25C6A1614ABD259BC0 12 | 04D4E6FA664BD75A508C0FF0ED6F2C52DA2ADD7C3F954D9C346D24318DBD2ECFC6805511F46262E10A25F252FD525AF1CBCC46016B6CD0A7705037364309198DA1 13 | 0456B468963752924DBF56112633DC57F07C512E3671A16CD7375C58469164599D1E04011D3E9004466C814B144A9BCB7E47D5BACA1B90DA0C4752603781BF5873 14 | 04D5BE7C653773CEE06A238020E953CFCD0F22BE2D045C6E5B4388A3F11B4586CBB4B177DFFD111F6A15A453009B568E95798B0227B60D8BEAC98AF671F31B0E2B 15 | 04B1985389D8AB680DEDD67BBA7CA781D1A9E6E5974AAD2E70518125BAD5783EB5355F46E927A030DB14CF8D3940C1BED7FB80624B32B349AB5A05226AF15A2228 16 | 0455B95BEF84A6045A505D015EF15E136E0A31CC2AA00FA4BCA62E5DF215EE981B3B4D6BCE33718DC6CF59F28B550648D7E8B2796AC36F25FF0C01F8BC42A16FD9 17 | 18 | -------------------------------------------------------------------------------- /xxhash/LICENSE: -------------------------------------------------------------------------------- 1 | xxHash Library 2 | Copyright (c) 2012-2020 Yann Collet 3 | All rights reserved. 4 | 5 | BSD 2-Clause License (https://www.opensource.org/licenses/bsd-license.php) 6 | 7 | Redistribution and use in source and binary forms, with or without modification, 8 | are permitted provided that the following conditions are met: 9 | 10 | * Redistributions of source code must retain the above copyright notice, this 11 | list of conditions and the following disclaimer. 12 | 13 | * Redistributions in binary form must reproduce the above copyright notice, this 14 | list of conditions and the following disclaimer in the documentation and/or 15 | other materials provided with the distribution. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 18 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 21 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 24 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | 28 | ---------------------------------------------------- 29 | 30 | xxhsum command line interface 31 | Copyright (c) 2013-2020 Yann Collet 32 | All rights reserved. 33 | 34 | GPL v2 License 35 | 36 | This program is free software; you can redistribute it and/or modify 37 | it under the terms of the GNU General Public License as published by 38 | the Free Software Foundation; either version 2 of the License, or 39 | (at your option) any later version. 40 | 41 | This program is distributed in the hope that it will be useful, 42 | but WITHOUT ANY WARRANTY; without even the implied warranty of 43 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 44 | GNU General Public License for more details. 45 | 46 | You should have received a copy of the GNU General Public License along 47 | with this program; if not, write to the Free Software Foundation, Inc., 48 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 49 | -------------------------------------------------------------------------------- /secp256k1/SECP256k1.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the BSGS distribution (https://github.com/JeanLucPons/BSGS). 3 | * Copyright (c) 2020 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 SECP256K1H 19 | #define SECP256K1H 20 | 21 | #include "Point.h" 22 | #include 23 | 24 | // Address type 25 | #define P2PKH 0 26 | #define P2SH 1 27 | #define BECH32 2 28 | 29 | 30 | class Secp256K1 { 31 | 32 | public: 33 | 34 | Secp256K1(); 35 | ~Secp256K1(); 36 | void Init(); 37 | Point ComputePublicKey(Int *privKey); 38 | Point NextKey(Point &key); 39 | bool EC(Point &p); 40 | 41 | Point ScalarMultiplication(Point &P,Int *scalar); 42 | 43 | char* GetPublicKeyHex(bool compressed, Point &p); 44 | void GetPublicKeyHex(bool compressed, Point &pubKey,char *dst); 45 | 46 | char* GetPublicKeyRaw(bool compressed, Point &p); 47 | void GetPublicKeyRaw(bool compressed, Point &pubKey,char *dst); 48 | 49 | bool ParsePublicKeyHex(char *str,Point &p,bool &isCompressed); 50 | 51 | void GetHash160(int type,bool compressed, 52 | Point &k0, Point &k1, Point &k2, Point &k3, 53 | uint8_t *h0, uint8_t *h1, uint8_t *h2, uint8_t *h3); 54 | 55 | void GetHash160(int type,bool compressed, Point &pubKey, unsigned char *hash); 56 | 57 | void GetHash160_fromX(int type,unsigned char prefix, 58 | Int *k0,Int *k1,Int *k2,Int *k3, 59 | uint8_t *h0,uint8_t *h1,uint8_t *h2,uint8_t *h3); 60 | 61 | 62 | Point Add(Point &p1, Point &p2); 63 | Point Add2(Point &p1, Point &p2); 64 | Point AddDirect(Point &p1, Point &p2); 65 | Point Double(Point &p); 66 | Point DoubleDirect(Point &p); 67 | Point Negation(Point &p); 68 | 69 | Point G; // Generator 70 | Int P; // Prime for the finite field 71 | Int order; // Curve order 72 | 73 | private: 74 | 75 | uint8_t GetByte(char *str,int idx); 76 | Int GetY(Int x, bool isEven); 77 | Point GTable[256*32]; // Generator table 78 | 79 | }; 80 | 81 | #endif // SECP256K1H 82 | -------------------------------------------------------------------------------- /gmp256k1/Point.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the BSGS distribution (https://github.com/JeanLucPons/BSGS). 3 | * Copyright (c) 2020 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 "Point.h" 19 | #include 20 | 21 | Point::Point() { 22 | } 23 | 24 | Point::Point(const Point &p) { 25 | 26 | mpz_set(x.num,p.x.num); 27 | mpz_set(y.num,p.y.num); 28 | mpz_set(z.num,p.z.num); 29 | } 30 | 31 | Point::Point(Int *cx,Int *cy,Int *cz) { 32 | mpz_set(x.num,cx->num); 33 | mpz_set(y.num,cy->num); 34 | mpz_set(z.num,cz->num); 35 | } 36 | 37 | void Point::Clear() { 38 | mpz_set_ui(x.num,0); 39 | mpz_set_ui(y.num,0); 40 | mpz_set_ui(z.num,0); 41 | } 42 | 43 | void Point::Set(Int *cx, Int *cy,Int *cz) { 44 | mpz_set(x.num,cx->num); 45 | mpz_set(y.num,cy->num); 46 | mpz_set(z.num,cz->num); 47 | } 48 | 49 | Point::~Point() { 50 | 51 | } 52 | 53 | void Point::Set(Point &p) { 54 | mpz_set(x.num,p.x.num); 55 | mpz_set(y.num,p.y.num); 56 | mpz_set(z.num,p.z.num); 57 | } 58 | 59 | bool Point::isZero() { 60 | return x.IsZero() && y.IsZero(); 61 | } 62 | 63 | void Point::Reduce() { 64 | Int i(&z); 65 | i.ModInv(); 66 | x.ModMul(&x,&i); 67 | y.ModMul(&y,&i); 68 | z.SetInt32(1); 69 | } 70 | 71 | bool Point::equals(Point &p) { 72 | return x.IsEqual(&p.x) && y.IsEqual(&p.y) && z.IsEqual(&p.z); 73 | } 74 | 75 | // Copy assignment operator 76 | Point& Point::operator=(const Point& other) { 77 | // Check for self-assignment 78 | if (this == &other) { 79 | return *this; 80 | } 81 | // Assign the values from 'other' to the current object 82 | mpz_set(x.num,other.x.num); 83 | mpz_set(y.num,other.y.num); 84 | mpz_set(z.num,other.z.num); 85 | 86 | // Return the current object 87 | return *this; 88 | } 89 | 90 | /* 91 | void Point::print(const char *str) { 92 | char *ptrs[3]; 93 | ptrs[0] = x.GetBase16(); 94 | ptrs[1] = y.GetBase16(); 95 | ptrs[2] = z.GetBase16(); 96 | printf("Point %s\n",str); 97 | printf("X: %s\n",ptrs[0]); 98 | printf("Y: %s\n",ptrs[1]); 99 | printf("Z: %s\n",ptrs[2]); 100 | printf("End Point\n"); 101 | for(int i = 0; i<3; i++) { 102 | free(ptrs[i]); 103 | } 104 | } 105 | */ -------------------------------------------------------------------------------- /gmp256k1/Random.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | #if defined(_WIN32) || defined(_WIN64) 8 | #include 9 | #include 10 | #pragma comment(lib, "bcrypt.lib") 11 | #elif __unix__ || __unix || __APPLE__ || __MACH__ || __CYGWIN__ 12 | #include 13 | #include 14 | #include 15 | #include 16 | #if defined(GRND_NONBLOCK) 17 | #define USE_GETRANDOM 18 | #endif 19 | #endif 20 | 21 | #include "Int.h" 22 | 23 | static int r_state_mt_ready = 0; 24 | static gmp_randstate_t r_state_mt; 25 | 26 | 27 | void int_randominit() { 28 | if(r_state_mt_ready) { 29 | fprintf(stderr,"r_state_mt already initialized, file %s, line %i\n",__FILE__,__LINE__ - 1); 30 | exit(0); 31 | } 32 | mpz_t mpz_seed; 33 | int bytes_readed,bytes = 64; 34 | unsigned char seed[64]; 35 | bytes_readed = random_bytes(seed, bytes); 36 | if(bytes_readed != bytes) { 37 | fprintf(stderr,"Error random_bytes(), file %s, line %i\n",__FILE__,__LINE__ - 2); 38 | exit(0); 39 | } 40 | mpz_init(mpz_seed); 41 | mpz_import(mpz_seed,bytes,1,sizeof(unsigned char),0,0,seed); 42 | gmp_randinit_mt(r_state_mt); 43 | gmp_randseed(r_state_mt,mpz_seed); 44 | r_state_mt_ready = 1; 45 | mpz_clear(mpz_seed); 46 | memset(seed,0,bytes); 47 | } 48 | 49 | void Int::Rand(int nbit) { 50 | if(!r_state_mt_ready) { 51 | fprintf(stderr,"Error Rand(), file %s, line %i\n",__FILE__,__LINE__ - 1); 52 | exit(0); 53 | } 54 | mpz_urandomb(num,r_state_mt,nbit); 55 | mpz_setbit(num,nbit-1); 56 | } 57 | 58 | void Int::Rand(Int *min,Int *max) { 59 | if(!r_state_mt_ready) { 60 | fprintf(stderr,"Error Rand(), file %s, line %i\n",__FILE__,__LINE__ - 1); 61 | exit(0); 62 | } 63 | Int diff(max); 64 | diff.Sub(min); 65 | this->Rand(256); 66 | this->Mod(&diff); 67 | this->Add(min); 68 | } 69 | 70 | int random_bytes(unsigned char *buffer,int bytes) { 71 | #if defined(_WIN32) || defined(_WIN64) 72 | if (!BCryptGenRandom(NULL, buffer, length, BCRYPT_USE_SYSTEM_PREFERRED_RNG)) { 73 | fprintf(stderr,"Not BCryptGenRandom available\n"); 74 | exit(EXIT_FAILURE); 75 | } 76 | else 77 | return bytes; 78 | #elif __unix__ || __unix || __APPLE__ || __MACH__ || __CYGWIN__ 79 | #ifdef USE_GETRANDOM 80 | return syscall(SYS_getrandom, buffer, bytes, GRND_NONBLOCK); 81 | #else 82 | int fd = open("/dev/urandom", O_RDONLY); 83 | if (fd == -1) { 84 | fprintf(stderr,"Not /dev/urandom available\n"); 85 | exit(EXIT_FAILURE); 86 | } 87 | ssize_t result = read(fd, buffer, bytes); 88 | close(fd); 89 | return result; 90 | #endif 91 | #else 92 | #error "Unsupported platform" 93 | #endif 94 | } 95 | -------------------------------------------------------------------------------- /gmp256k1/GMP256K1.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Keyhunt distribution (https://github.com/albertobsd/keyhunt). 3 | Copyright (c) 2020 Luis Alberto 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | */ 23 | 24 | #ifndef SECP256K1H 25 | #define SECP256K1H 26 | 27 | #include "Point.h" 28 | #include 29 | 30 | // Address type 31 | #define P2PKH 0 32 | #define P2SH 1 33 | #define BECH32 2 34 | 35 | 36 | class Secp256K1 { 37 | 38 | public: 39 | 40 | Secp256K1(); 41 | ~Secp256K1(); 42 | void Init(); 43 | Point ComputePublicKey(Int *privKey); 44 | Point Add(Point &p1, Point &p2); 45 | Point Add2(Point &p1, Point &p2); 46 | Point NextKey(Point &key); 47 | bool EC(Point &p); 48 | 49 | void GetHash160_fromX(int type,unsigned char prefix, 50 | Int *k0,Int *k1,Int *k2,Int *k3, 51 | uint8_t *h0,uint8_t *h1,uint8_t *h2,uint8_t *h3); 52 | bool ParsePublicKeyHex(char *str,Point &p,bool &isCompressed); 53 | Point ScalarMultiplication(Point &P,Int *scalar); 54 | char* GetPublicKeyHex(bool compressed, Point &p); 55 | void GetPublicKeyHex(bool compressed, Point &pubKey,char *dst); 56 | 57 | char* GetPublicKeyRaw(bool compressed, Point &p); 58 | void GetPublicKeyRaw(bool compressed, Point &pubKey,char *dst); 59 | void GetHash160(int type,bool compressed, Point &pubKey, unsigned char *hash); 60 | void GetHash160(int type,bool compressed, 61 | Point &k0, Point &k1, Point &k2, Point &k3, 62 | uint8_t *h0, uint8_t *h1, uint8_t *h2, uint8_t *h3); 63 | 64 | Point Negation(Point &p); 65 | Point Double(Point &p); 66 | Point DoubleDirect(Point &p); 67 | Point AddDirect(Point &p1, Point &p2); 68 | Point G; // Generator 69 | Int P; // Prime for the finite field 70 | Int order; // Curve order 71 | 72 | private: 73 | 74 | uint8_t GetByte(char *str,int idx); 75 | Int GetY(Int x, bool isEven); 76 | Point GTable[256*32]; // Generator table 77 | 78 | }; 79 | 80 | #endif // SECP256K1H 81 | -------------------------------------------------------------------------------- /tests/unsolvedpuzzles.txt: -------------------------------------------------------------------------------- 1 | 13zb1hQbWVsc2S7ZTZnP2G4undNNpdh5so 2 | 1BY8GQbnueYofwSuFAT3USAhGjPrkxDdW9 3 | 1MVDYgVaSN6iKKEsbzRUAYFrYJadLYZvvZ 4 | 19vkiEajfhuZ8bs8Zu2jgmC6oqZbWqhxhG 5 | 1PWo3JeB9jrGwfHDNpdGK54CRas7fsVzXU 6 | 1JTK7s9YVYywfm5XUH7RNhHJH1LshCaRFR 7 | 12VVRNPi4SJqUTsp6FmqDqY5sGosDtysn4 8 | 1FWGcVDK3JGzCC3WtkYetULPszMaK2Jksv 9 | 1DJh2eHFYQfACPmrvpyWc8MSTYKh7w9eRF 10 | 1Bxk4CQdqL9p22JEtDfdXMsng1XacifUtE 11 | 15qF6X51huDjqTmF9BJgxXdt1xcj46Jmhb 12 | 1ARk8HWJMn8js8tQmGUJeQHjSE7KRkn2t8 13 | 15qsCm78whspNQFydGJQk5rexzxTQopnHZ 14 | 13zYrYhhJxp6Ui1VV7pqa5WDhNWM45ARAC 15 | 14MdEb4eFcT3MVG5sPFG4jGLuHJSnt1Dk2 16 | 1CMq3SvFcVEcpLMuuH8PUcNiqsK1oicG2D 17 | 1K3x5L6G57Y494fDqBfrojD28UJv4s5JcK 18 | 1PxH3K1Shdjb7gSEoTX7UPDZ6SH4qGPrvq 19 | 16AbnZjZZipwHMkYKBSfswGWKDmXHjEpSf 20 | 19QciEHbGVNY4hrhfKXmcBBCrJSBZ6TaVt 21 | 1EzVHtmbN4fs4MiNk3ppEnKKhsmXYJ4s74 22 | 1AE8NzzgKE7Yhz7BWtAcAAxiFMbPo82NB5 23 | 17Q7tuG2JwFFU9rXVj3uZqRtioH3mx2Jad 24 | 1K6xGMUbs6ZTXBnhw1pippqwK6wjBWtNpL 25 | 15ANYzzCp5BFHcCnVFzXqyibpzgPLWaD8b 26 | 18ywPwj39nGjqBrQJSzZVq2izR12MDpDr8 27 | 1CaBVPrwUxbQYYswu32w7Mj4HR4maNoJSX 28 | 1JWnE6p6UN7ZJBN7TtcbNDoRcjFtuDWoNL 29 | 1CKCVdbDJasYmhswB6HKZHEAnNaDpK7W4n 30 | 1PXv28YxmYMaB8zxrKeZBW8dt2HK7RkRPX 31 | 1AcAmB6jmtU6AiEcXkmiNE9TNVPsj9DULf 32 | 1EQJvpsmhazYCcKX5Au6AZmZKRnzarMVZu 33 | 18KsfuHuzQaBTNLASyj15hy4LuqPUo1FNB 34 | 15EJFC5ZTs9nhsdvSUeBXjLAuYq3SWaxTc 35 | 1HB1iKUqeffnVsvQsbpC6dNi1XKbyNuqao 36 | 1GvgAXVCbA8FBjXfWiAms4ytFeJcKsoyhL 37 | 1824ZJQ7nKJ9QFTRBqn7z7dHV5EGpzUpH3 38 | 18A7NA9FTsnJxWgkoFfPAFbQzuQxpRtCos 39 | 1NeGn21dUDDeqFQ63xb2SpgUuXuBLA4WT4 40 | 174SNxfqpdMGYy5YQcfLbSTK3MRNZEePoy 41 | 1MnJ6hdhvK37VLmqcdEwqC3iFxyWH2PHUV 42 | 1KNRfGWw7Q9Rmwsc6NT5zsdvEb9M2Wkj5Z 43 | 1PJZPzvGX19a7twf5HyD2VvNiPdHLzm9F6 44 | 1GuBBhf61rnvRe4K8zu8vdQB3kHzwFqSy7 45 | 1GDSuiThEV64c166LUFC9uDcVdGjqkxKyh 46 | 1Me3ASYt5JCTAK2XaC32RMeH34PdprrfDx 47 | 1CdufMQL892A69KXgv6UNBD17ywWqYpKut 48 | 1BkkGsX9ZM6iwL3zbqs7HWBV7SvosR6m8N 49 | 1PXAyUB8ZoH3WD8n5zoAthYjN15yN5CVq5 50 | 1AWCLZAjKbV1P7AHvaPNCKiB7ZWVDMxFiz 51 | 1G6EFyBRU86sThN3SSt3GrHu1sA7w7nzi4 52 | 1MZ2L1gFrCtkkn6DnTT2e4PFUTHw9gNwaj 53 | 1Hz3uv3nNZzBVMXLGadCucgjiCs5W9vaGz 54 | 1Fo65aKq8s8iquMt6weF1rku1moWVEd5Ua 55 | 16zRPnT8znwq42q7XeMkZUhb1bKqgRogyy 56 | 1KrU4dHE5WrW8rhWDsTRjR21r8t3dsrS3R 57 | 17uDfp5r4n441xkgLFmhNoSW1KWp6xVLD 58 | 13A3JrvXmvg5w9XGvyyR4JEJqiLz8ZySY3 59 | 16RGFo6hjq9ym6Pj7N5H7L1NR1rVPJyw2v 60 | 1UDHPdovvR985NrWSkdWQDEQ1xuRiTALq 61 | 15nf31J46iLuK1ZkTnqHo7WgN5cARFK3RA 62 | 1Ab4vzG6wEQBDNQM1B2bvUz4fqXXdFk2WT 63 | 1Fz63c775VV9fNyj25d9Xfw3YHE6sKCxbt 64 | 1QKBaU6WAeycb3DbKbLBkX7vJiaS8r42Xo 65 | 1CD91Vm97mLQvXhrnoMChhJx4TP9MaQkJo 66 | 15MnK2jXPqTMURX4xC3h4mAZxyCcaWWEDD 67 | 13N66gCzWWHEZBxhVxG18P8wyjEWF9Yoi1 68 | 1NevxKDYuDcCh1ZMMi6ftmWwGrZKC6j7Ux 69 | 19GpszRNUej5yYqxXoLnbZWKew3KdVLkXg 70 | 1M7ipcdYHey2Y5RZM34MBbpugghmjaV89P 71 | 18aNhurEAJsw6BAgtANpexk5ob1aGTwSeL 72 | 1FwZXt6EpRT7Fkndzv6K4b4DFoT4trbMrV 73 | 1CXvTzR6qv8wJ7eprzUKeWxyGcHwDYP1i2 74 | 1MUJSJYtGPVGkBCTqGspnxyHahpt5Te8jy 75 | 13Q84TNNvgcL3HJiqQPvyBb9m4hxjS3jkV 76 | 1LuUHyrQr8PKSvbcY1v1PiuGuqFjWpDumN 77 | 18192XpzzdDi2K11QVHR7td2HcPS6Qs5vg 78 | 1NgVmsCCJaKLzGyKLFJfVequnFW9ZvnMLN 79 | 1AoeP37TmHdFh8uN72fu9AqgtLrUwcv2wJ 80 | 1FTpAbQa4h8trvhQXjXnmNhqdiGBd1oraE 81 | 14JHoRAdmJg3XR4RjMDh6Wed6ft6hzbQe9 82 | 19z6waranEf8CcP8FqNgdwUe1QRxvUNKBG 83 | 14u4nA5sugaswb6SZgn5av2vuChdMnD9E5 84 | 1NBC8uXJy1GiJ6drkiZa1WuKn51ps7EPTv 85 | 86 | -------------------------------------------------------------------------------- /gmp256k1/IntMod.cpp: -------------------------------------------------------------------------------- 1 | #include "Int.h" 2 | 3 | static Int _P; // Field characteristic 4 | static Int _R2o; // R^2 for SecpK1 order modular mult 5 | static Int *_O; // Field Order 6 | 7 | void Int::Mod(Int *A) { 8 | mpz_mod(num,num,A->num); 9 | } 10 | 11 | void Int::ModInv() { 12 | mpz_invert(num,num,_P.num); 13 | } 14 | 15 | void Int::ModNeg() { 16 | mpz_neg(num,num); 17 | mpz_add(num,num,_P.num); 18 | } 19 | 20 | void Int::ModAdd(Int *a) { 21 | mpz_t p; 22 | mpz_add(num,num,a->num); 23 | mpz_init_set(p,num); 24 | mpz_sub(p,p,_P.num); 25 | if(mpz_cmp_ui(p,0) >= 0) 26 | mpz_set(num,p); 27 | mpz_clear(p); 28 | } 29 | 30 | 31 | void Int::ModAdd(uint32_t a) { 32 | mpz_t p; 33 | mpz_add_ui(num,num,a); 34 | mpz_init_set(p,num); 35 | mpz_sub(p,p,_P.num); 36 | if(mpz_cmp_ui(p,0) >= 0) 37 | mpz_set(num,p); 38 | mpz_clear(p); 39 | } 40 | 41 | void Int::ModAdd(Int *a, Int *b) { 42 | mpz_t p; 43 | mpz_add(num,a->num,b->num); 44 | mpz_init_set(p,num); 45 | mpz_sub(p,p,_P.num); 46 | if(mpz_cmp_ui(p,0) >= 0) 47 | mpz_set(num,p); 48 | mpz_clear(p); 49 | } 50 | 51 | void Int::ModMul(Int *a) { // this <- this*b (mod n) 52 | mpz_mul(num,num,a->num); 53 | mpz_mod(num,num,_P.num); 54 | } 55 | 56 | void Int::ModMul(Int *a,Int *b) { // this <- a*b (mod n) 57 | mpz_mul(num,a->num,b->num); 58 | mpz_mod(num,num,_P.num); 59 | } 60 | 61 | void Int::ModSub(Int *a) { 62 | mpz_sub(num,num,a->num); 63 | if (mpz_cmp_ui(num,0) < 0 ) 64 | mpz_add(num,num,_P.num); 65 | } 66 | 67 | void Int::ModSub(Int *a,Int *b) { 68 | mpz_sub(num,a->num,b->num); 69 | if (mpz_cmp_ui(num,0) < 0 ) 70 | mpz_add(num,num,_P.num); 71 | } 72 | 73 | void Int::ModSub(uint64_t a) { 74 | Int A(a); 75 | mpz_add(num,num,A.num); 76 | if (mpz_cmp_ui(num,0) < 0) 77 | mpz_add(num,num,_P.num); 78 | } 79 | 80 | 81 | void Int::ModMulK1(Int *a, Int *b) { 82 | mpz_mul(num,a->num,b->num); 83 | mpz_mod(num,num,_P.num); 84 | } 85 | 86 | void Int::ModMulK1(Int *a) { 87 | mpz_mul(num,num,a->num); 88 | mpz_mod(num,num,_P.num); 89 | } 90 | 91 | 92 | 93 | void Int::ModSquareK1(Int *a) { 94 | mpz_powm_ui(num,a->num,2,_P.num); 95 | } 96 | 97 | void Int::ModDouble() { 98 | mpz_t p; 99 | mpz_add(num,num,num); 100 | mpz_init_set(p,num); 101 | mpz_sub(p,p,_P.num); 102 | if(mpz_cmp_ui(p,0) > 0) { 103 | mpz_set(num,p); 104 | } 105 | mpz_clear(p); 106 | } 107 | 108 | void Int::ModSqrt() { 109 | mpz_sqrt(num,num); 110 | mpz_mod(num,num,_P.num); 111 | } 112 | 113 | bool Int::HasSqrt() { 114 | if(mpz_perfect_square_p(num) != 0) 115 | return true; 116 | return false; 117 | } 118 | 119 | 120 | /* Initializator of some Values P and N (Order) */ 121 | 122 | void Int::SetupField(Int *n) { 123 | _P.Set(n); 124 | } 125 | 126 | void Int::InitK1(Int *order) { 127 | _O = order; 128 | _R2o.SetBase16("9D671CD581C69BC5E697F5E45BCD07C6741496C20E7CF878896CF21467D7D140"); 129 | } 130 | 131 | /* This next Opeations that have endin in order are modulo N */ 132 | 133 | void Int::ModMulK1order(Int *a) { 134 | mpz_mul(num,num,a->num); 135 | mpz_mod(num,num,_O->num); 136 | } 137 | 138 | void Int::ModAddK1order(Int *a, Int *b) { 139 | mpz_add(num,num,a->num); 140 | mpz_add(num,num,b->num); 141 | mpz_sub(num,num,_O->num); 142 | if (mpz_cmp_ui(num,0) < 0 ) 143 | mpz_add(num,num,_O->num); 144 | } 145 | 146 | void Int::ModInvorder() { 147 | mpz_invert(num,num,_O->num); 148 | } 149 | -------------------------------------------------------------------------------- /tests/unsolvedpuzzles.rmd: -------------------------------------------------------------------------------- 1 | 20d45a6a762535700ce9e0b216e31994335db8a5 2 | 739437bb3dd6d1983e66629c5f08c70e52769371 3 | e0b8a2baee1b77fc703455f39d51477451fc8cfc 4 | 61eb8a50c86b0584bb727dd65bed8d2400d6d5aa 5 | f6f5431d25bbf7b12e8add9af5e3475c44a0a5b8 6 | bf7413e8df4e7a34ce9dc13e2f2648783ec54adb 7 | 105b7f253f0ebd7843adaebbd805c944bfb863e4 8 | 9f1adb20baeacc38b3f49f3df6906a0e48f2df3d 9 | 86f9fea5cdecf033161dd2f8f8560768ae0a6d14 10 | 783c138ac81f6a52398564bb17455576e8525b29 11 | 35003c3ef8759c92092f8488fca59a042859018c 12 | 67671d5490c272e3ab7ddd34030d587738df33da 13 | 351e605fac813965951ba433b7c2956bf8ad95ce 14 | 20d28d4e87543947c7e4913bcdceaa16e2f8f061 15 | 24cef184714bbd030833904f5265c9c3e12a95a2 16 | 7c99ce73e19f9fbfcce4825ae88261e2b0b0b040 17 | c60111ed3d63b49665747b0e31eb382da5193535 18 | fbc708d671c03e26661b9c08f77598a529858b5e 19 | 38a968fdfb457654c51bcfc4f9174d6ee487bb41 20 | 5c3862203d1e44ab3af441503e22db97b1c5097e 21 | 9978f61b92d16c5f1a463a0995df70da1f7a7d2a 22 | 6534b31208fe6e100d29f9c9c75aac8bf06fbb38 23 | 463013cd41279f2fd0c31d0a16db3972bfffac8d 24 | c6927a00970d0165327d0a6db7950f05720c295c 25 | 2da63cbd251d23c7b633cb287c09e6cf888b3fe4 26 | 578d94dc6f40fff35f91f6fba9b71c46b361dff2 27 | 7eefddd979a1d6bb6f29757a1f463579770ba566 28 | c01bf430a97cbcdaedddba87ef4ea21c456cebdb 29 | 7c1a77205c03b9909663b2034faa0b544e6bc96b 30 | f72b812932f6d7102233971d65cec0a22b89e136 31 | 695fd6dcf33f47166b25de968b2932b351b0afc4 32 | 93022af9a38f3ebb0c3f15dd1c83f8fadaf64e74 33 | 505aaa63a5e209dfb90cee683a8e227a8c278e47 34 | 2e644e46b042ffa86da35c54d7275f1abe6d4911 35 | b166c44f12c7fc565f37ff6288ee64e0f0ec9a0b 36 | aeb0a0197442d4ade8ef41442d557b0e22b85ac0 37 | 4cfc43fe12a330c8164251e38c0c0c3c84cf86f6 38 | 4e81efec43c5195aeca0e3877664330418b8e48e 39 | ed673389e4b12925316f9166d56d701829e53cf8 40 | 42773005f9594cd16b10985d428418acb7f352ec 41 | e3f381c34a20da049779b44cae0417c7fb2898d0 42 | c97f9591e28687be1c4d972e25be7c372a3221b4 43 | f4a4e1c11a5bbbd2fc139d221825407c66e0b8b4 44 | ae6804b35c82f47f8b0a42d8c5e514fe5ef0a883 45 | a6e4818537e42f7b3f021daa810367dad4dda16f 46 | e263b62ea294b9650615a13b926e75944c823990 47 | 7fa4515066ba6905f894b2078f9af7b1379169cf 48 | 75f74467ce7214f1767406d5ed12012aa523c48e 49 | f7079256aa027dc437cbb539f955472416725fc8 50 | 683ea8a1ef06eada90556017d44323b5c04e00f1 51 | a58708aa98ad35c889bb36d8049bf9e9cacfd02a 52 | e170ef514689d7230da362a0c121a07723550512 53 | ba4c2748360a6b66263e11d1dc8658463ca5ff18 54 | a24922852051a9002ebf4c864a55acb75bb4cf75 55 | 41b4b36a6c036568972380177eca2916cacd71de 56 | cecd3ca4319651bd3afd1e23ab66e111ed38d16d 57 | 014e15e4ea6da460cc7835e262676baa37988e4f 58 | 17a5ebfaf62e73f149e33ba674836801f13a80b9 59 | 3b6f58a75a54bfd85d1bc6c51180fdc732992326 60 | 05257be4b57ee43fc09762d5d3a9ad4a6e1a0364 61 | 3482f8986e13c018692053a784481c63a3554c9c 62 | 692a8e583866fc9056f5c61a45969fb9d868a08c 63 | a45dae9cd5d3fde21e5aa9a95367d107267b3b8a 64 | ffbb35a7bb9bbe16c1aa2534f7ff11d59c8e3d1a 65 | 7af50f73fd580f1713af3a6f9c5de49643ec6fc6 66 | 2fcea55e6d027a2ba7c7ebe95eedf47766730fe2 67 | 19ed3e03d19ddcedd5fa86543be820b3a7951650 68 | ed87120066e244ff5331d5f8625873d7a3acc39c 69 | 5abf369388deb8072741b4eb43ef10fa9388a729 70 | dca7ebfb78ce21884300f133d89244bc4b1b756f 71 | 5318b9d7fcc93873f768725eb68ba2c924bb07ee 72 | a3e3612e586fd206efb8eee6ccd58318e182829a 73 | 7e827e3b90da24c2a15f7b67e3bbece39955a5d0 74 | e08c4d3bc9cf2b3e2cb88de2bfaa4fe8c7aa3f24 75 | 1a4fb632f0de0c53a0a31d57f840a19e56c645ee 76 | da56cd815fa2f0d6a4ce6d25ed7b1a01d9f9bc6b 77 | 4ccf94a1b0efd63cddeee0ef5eee5ebe720cfcbf 78 | edd2e206825fa8949d1304cd82c08d64b222f2eb 79 | 6b8b7830f73c5bf9e8beb9f161ad82b3bde992e4 80 | 9ea3f29aaedf7da10b1488934c50a39e271b0b64 81 | 242d790e5a168043c76f0539fd894b73ee67b3b3 82 | 628dacebb0faa7f81670e174ca4c8a95a7e37029 83 | 2ac1295b4e54b3f15bb0a99f84018d2082495645 84 | e84818e1bf7f699aa6e28ef9edfb582099099292 85 | -------------------------------------------------------------------------------- /sha3/sha3.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2015 Taylor R. Campbell 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #ifndef SHA3_H 28 | #define SHA3_H 29 | 30 | #include 31 | #include 32 | 33 | struct sha3 { 34 | uint64_t A[25]; 35 | unsigned nb; /* number of bytes remaining to fill buffer */ 36 | }; 37 | 38 | typedef struct { struct sha3 C224; } SHA3_224_CTX; 39 | typedef struct { struct sha3 C256; } SHA3_256_CTX; 40 | typedef struct { struct sha3 C384; } SHA3_384_CTX; 41 | typedef struct { struct sha3 C512; } SHA3_512_CTX; 42 | typedef struct { struct sha3 C128; } SHAKE128_CTX; 43 | typedef struct { struct sha3 C256; } SHAKE256_CTX; 44 | 45 | #define SHA3_224_DIGEST_LENGTH 28 46 | #define SHA3_256_DIGEST_LENGTH 32 47 | #define SHA3_384_DIGEST_LENGTH 48 48 | #define SHA3_512_DIGEST_LENGTH 64 49 | 50 | void SHA3_224_Init(SHA3_224_CTX *); 51 | void SHA3_224_Update(SHA3_224_CTX *, const uint8_t *, size_t); 52 | void SHA3_224_Final(uint8_t[SHA3_224_DIGEST_LENGTH], SHA3_224_CTX *); 53 | 54 | void SHA3_256_Init(SHA3_256_CTX *); 55 | void SHA3_256_Update(SHA3_256_CTX *, const uint8_t *, size_t); 56 | void SHA3_256_Final(uint8_t[SHA3_256_DIGEST_LENGTH], SHA3_256_CTX *); 57 | 58 | void SHA3_384_Init(SHA3_384_CTX *); 59 | void SHA3_384_Update(SHA3_384_CTX *, const uint8_t *, size_t); 60 | void SHA3_384_Final(uint8_t[SHA3_384_DIGEST_LENGTH], SHA3_384_CTX *); 61 | 62 | void SHA3_512_Init(SHA3_512_CTX *); 63 | void SHA3_512_Update(SHA3_512_CTX *, const uint8_t *, size_t); 64 | void SHA3_512_Final(uint8_t[SHA3_512_DIGEST_LENGTH], SHA3_512_CTX *); 65 | 66 | void SHAKE128_Init(SHAKE128_CTX *); 67 | void SHAKE128_Update(SHAKE128_CTX *, const uint8_t *, size_t); 68 | void SHAKE128_Final(uint8_t *, size_t, SHAKE128_CTX *); 69 | 70 | void SHAKE256_Init(SHAKE256_CTX *); 71 | void SHAKE256_Update(SHAKE256_CTX *, const uint8_t *, size_t); 72 | void SHAKE256_Final(uint8_t *, size_t, SHAKE256_CTX *); 73 | 74 | #define KECCAK_256_Init SHA3_256_Init 75 | #define KECCAK_256_Update SHA3_256_Update 76 | void KECCAK_256_Final(uint8_t[SHA3_256_DIGEST_LENGTH], SHA3_256_CTX *); 77 | 78 | #define KECCAK_384_Init SHA3_384_Init 79 | #define KECCAK_384_Update SHA3_384_Update 80 | void KECCAK_384_Final(uint8_t[SHA3_384_DIGEST_LENGTH], SHA3_384_CTX *); 81 | 82 | #define KECCAK_512_Init SHA3_512_Init 83 | #define KECCAK_512_Update SHA3_512_Update 84 | void KECCAK_512_Final(uint8_t[SHA3_512_DIGEST_LENGTH], SHA3_512_CTX *); 85 | 86 | int SHA3_Selftest(void); 87 | 88 | #endif /* SHA3_H */ 89 | -------------------------------------------------------------------------------- /secp256k1/Random.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the BSGS distribution (https://github.com/JeanLucPons/BSGS). 3 | * Copyright (c) 2020 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 | 19 | #include "Random.h" 20 | 21 | #if defined(_WIN64) && !defined(__CYGWIN__) 22 | #else 23 | #include 24 | #endif 25 | 26 | #ifdef __unix__ 27 | #ifdef __CYGWIN__ 28 | #else 29 | #include 30 | #endif 31 | #endif 32 | 33 | #define RK_STATE_LEN 624 34 | 35 | /* State of the RNG */ 36 | typedef struct rk_state_ 37 | { 38 | unsigned long key[RK_STATE_LEN]; 39 | int pos; 40 | } rk_state; 41 | 42 | rk_state localState; 43 | 44 | /* Maximum generated random value */ 45 | #define RK_MAX 0xFFFFFFFFUL 46 | 47 | void rk_seed(unsigned long seed, rk_state *state) 48 | { 49 | int pos; 50 | seed &= 0xffffffffUL; 51 | 52 | /* Knuth's PRNG as used in the Mersenne Twister reference implementation */ 53 | for (pos=0; poskey[pos] = seed; 56 | seed = (1812433253UL * (seed ^ (seed >> 30)) + pos + 1) & 0xffffffffUL; 57 | } 58 | 59 | state->pos = RK_STATE_LEN; 60 | } 61 | 62 | /* Magic Mersenne Twister constants */ 63 | #define N 624 64 | #define M 397 65 | #define MATRIX_A 0x9908b0dfUL 66 | #define UPPER_MASK 0x80000000UL 67 | #define LOWER_MASK 0x7fffffffUL 68 | 69 | #ifdef _WIN64 70 | // Disable "unary minus operator applied to unsigned type, result still unsigned" warning. 71 | #pragma warning(disable : 4146) 72 | #endif 73 | 74 | /* Slightly optimised reference implementation of the Mersenne Twister */ 75 | inline unsigned long rk_random(rk_state *state) 76 | { 77 | unsigned long y; 78 | 79 | if (state->pos == RK_STATE_LEN) 80 | { 81 | int i; 82 | 83 | for (i=0;ikey[i] & UPPER_MASK) | (state->key[i+1] & LOWER_MASK); 86 | state->key[i] = state->key[i+M] ^ (y>>1) ^ (-(y & 1) & MATRIX_A); 87 | } 88 | for (;ikey[i] & UPPER_MASK) | (state->key[i+1] & LOWER_MASK); 91 | state->key[i] = state->key[i+(M-N)] ^ (y>>1) ^ (-(y & 1) & MATRIX_A); 92 | } 93 | y = (state->key[N-1] & UPPER_MASK) | (state->key[0] & LOWER_MASK); 94 | state->key[N-1] = state->key[M-1] ^ (y>>1) ^ (-(y & 1) & MATRIX_A); 95 | 96 | state->pos = 0; 97 | } 98 | 99 | y = state->key[state->pos++]; 100 | 101 | /* Tempering */ 102 | y ^= (y >> 11); 103 | y ^= (y << 7) & 0x9d2c5680UL; 104 | y ^= (y << 15) & 0xefc60000UL; 105 | y ^= (y >> 18); 106 | 107 | return y; 108 | } 109 | 110 | inline double rk_double(rk_state *state) 111 | { 112 | /* shifts : 67108864 = 0x4000000, 9007199254740992 = 0x20000000000000 */ 113 | long a = rk_random(state) >> 5, b = rk_random(state) >> 6; 114 | return (a * 67108864.0 + b) / 9007199254740992.0; 115 | } 116 | 117 | // Initialise the random generator with the specified seed 118 | void rseed(unsigned long seed) { 119 | rk_seed(seed,&localState); 120 | //srand(seed); 121 | } 122 | 123 | #if defined(_WIN64) && !defined(__CYGWIN__) 124 | unsigned long rndl() { 125 | return rk_random(&localState); 126 | } 127 | #else 128 | unsigned long rndl() { 129 | unsigned long r; 130 | int bytes_read = getrandom(&r, sizeof(unsigned long), GRND_NONBLOCK ); 131 | if (bytes_read > 0) { 132 | return r; 133 | } 134 | else { 135 | /*Fail safe */ 136 | return rk_random(&localState); 137 | } 138 | } 139 | 140 | #endif 141 | 142 | // Returns a uniform distributed double value in the interval ]0,1[ 143 | double rnd() { 144 | return rk_double(&localState); 145 | } 146 | -------------------------------------------------------------------------------- /util.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "util.h" 6 | 7 | 8 | char *ltrim(char *str, const char *seps) { 9 | size_t totrim; 10 | if (seps == NULL) { 11 | seps = "\t\n\v\f\r "; 12 | } 13 | totrim = strspn(str, seps); 14 | if (totrim > 0) { 15 | size_t len = strlen(str); 16 | if (totrim == len) { 17 | str[0] = '\0'; 18 | } 19 | else { 20 | memmove(str, str + totrim, len + 1 - totrim); 21 | } 22 | } 23 | return str; 24 | } 25 | 26 | char *rtrim(char *str, const char *seps) { 27 | int i; 28 | if (seps == NULL) { 29 | seps = "\t\n\v\f\r "; 30 | } 31 | i = strlen(str) - 1; 32 | while (i >= 0 && strchr(seps, str[i]) != NULL) { 33 | str[i] = '\0'; 34 | i--; 35 | } 36 | return str; 37 | } 38 | 39 | char *trim(char *str, const char *seps) { 40 | return ltrim(rtrim(str, seps), seps); 41 | } 42 | 43 | int indexOf(char *s,const char **array,int length_array) { 44 | int index = -1,i,continuar = 1; 45 | for(i = 0; i current < t->n) { 56 | t->current++; 57 | return t->tokens[t->current-1]; 58 | } 59 | else { 60 | return NULL; 61 | } 62 | } 63 | int hasMoreTokens(Tokenizer *t) { 64 | return (t->current < t->n); 65 | } 66 | 67 | void stringtokenizer(char *data,Tokenizer *t) { 68 | char *token; 69 | t->tokens = NULL; 70 | t->n = 0; 71 | t->current = 0; 72 | trim(data,"\t\n\r :"); 73 | token = strtok(data," \t:"); 74 | while(token != NULL) { 75 | t->n++; 76 | t->tokens = (char**) realloc(t->tokens,sizeof(char*)*t->n); 77 | if(t->tokens == NULL) { 78 | printf("Out of memory\n"); 79 | exit(0); 80 | } 81 | t->tokens[t->n - 1] = token; 82 | token = strtok(NULL," \t:"); 83 | } 84 | } 85 | 86 | void freetokenizer(Tokenizer *t) { 87 | if(t->n > 0) { 88 | free(t->tokens); 89 | } 90 | memset(t,0,sizeof(Tokenizer)); 91 | } 92 | 93 | 94 | /* 95 | Aux function to get the hexvalues of the data 96 | */ 97 | char *tohex(char *ptr,int length){ 98 | char *buffer; 99 | int offset = 0; 100 | unsigned char c; 101 | buffer = (char *) malloc((length * 2)+1); 102 | for (int i = 0; i = '0' && hex <= '9') { 151 | *out = hex - '0'; 152 | } else if (hex >= 'A' && hex <= 'F') { 153 | *out = hex - 'A' + 10; 154 | } else if (hex >= 'a' && hex <= 'f') { 155 | *out = hex - 'a' + 10; 156 | } else { 157 | return 0; 158 | } 159 | 160 | return 1; 161 | } 162 | 163 | void addItemList(char *data, List *l) { 164 | l->data = (char**) realloc(l->data,sizeof(char*)* (l->n +1)); 165 | l->data[l->n] = data; 166 | l->n++; 167 | } 168 | 169 | int isValidHex(char *data) { 170 | char c; 171 | int len,i,valid = 1; 172 | len = strlen(data); 173 | for(i = 0 ; i < len && valid ;i++ ) { 174 | c = data[i]; 175 | valid = ( (c >= '0' && c <='9') || (c >= 'A' && c <='F' ) || (c >= 'a' && c <='f' ) ); 176 | } 177 | return valid; 178 | } 179 | -------------------------------------------------------------------------------- /tests/1to63_65.txt: -------------------------------------------------------------------------------- 1 | 0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 2 | 02f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9 3 | 025cbdf0646e5db4eaa398f365f2ea7a0e3d419b7e0330e39ce92bddedcac4f9bc 4 | 022f01e5e15cca351daff3843fb70f3c2f0a1bdd05e5af888a67784ef3e10a2a01 5 | 02352bbf4a4cdd12564f93fa332ce333301d9ad40271f8107181340aef25be59d5 6 | 03f2dac991cc4ce4b9ea44887e5c7c0bce58c80074ab9d4dbaeb28531b7739f530 7 | 0296516a8f65774275278d0d7420a88df0ac44bd64c7bae07c3fe397c5b3300b23 8 | 0308bc89c2f919ed158885c35600844d49890905c79b357322609c45706ce6b514 9 | 0243601d61c836387485e9514ab5c8924dd2cfd466af34ac95002727e1659d60f7 10 | 03a7a4c30291ac1db24b4ab00c442aa832f7794b5a0959bec6e8d7fee802289dcd 11 | 038b05b0603abd75b0c57489e451f811e1afe54a8715045cdf4888333f3ebc6e8b 12 | 038b00fcbfc1a203f44bf123fc7f4c91c10a85c8eae9187f9d22242b4600ce781c 13 | 03aadaaab1db8d5d450b511789c37e7cfeb0eb8b3e61a57a34166c5edc9a4b869d 14 | 03b4f1de58b8b41afe9fd4e5ffbdafaeab86c5db4769c15d6e6011ae7351e54759 15 | 02fea58ffcf49566f6e9e9350cf5bca2861312f422966e8db16094beb14dc3df2c 16 | 029d8c5d35231d75eb87fd2c5f05f65281ed9573dc41853288c62ee94eb2590b7a 17 | 033f688bae8321b8e02b7e6c0a55c2515fb25ab97d85fda842449f7bfa04e128c3 18 | 020ce4a3291b19d2e1a7bf73ee87d30a6bdbc72b20771e7dfff40d0db755cd4af1 19 | 0385663c8b2f90659e1ccab201694f4f8ec24b3749cfe5030c7c3646a709408e19 20 | 033c4a45cbd643ff97d77f41ea37e843648d50fd894b864b0d52febc62f6454f7c 21 | 031a746c78f72754e0be046186df8a20cdce5c79b2eda76013c647af08d306e49e 22 | 023ed96b524db5ff4fe007ce730366052b7c511dc566227d929070b9ce917abb43 23 | 03f82710361b8b81bdedb16994f30c80db522450a93e8e87eeb07f7903cf28d04b 24 | 036ea839d22847ee1dce3bfc5b11f6cf785b0682db58c35b63d1342eb221c3490c 25 | 03057fbea3a2623382628dde556b2a0698e32428d3cd225f3bd034dca82dd7455a 26 | 024e4f50a2a3eccdb368988ae37cd4b611697b26b29696e42e06d71368b4f3840f 27 | 031a864bae3922f351f1b57cfdd827c25b7e093cb9c88a72c1cd893d9f90f44ece 28 | 03e9e661838a96a65331637e2a3e948dc0756e5009e7cb5c36664d9b72dd18c0a7 29 | 026caad634382d34691e3bef43ed4a124d8909a8a3362f91f1d20abaaf7e917b36 30 | 030d282cf2ff536d2c42f105d0b8588821a915dc3f9a05bd98bb23af67a2e92a5b 31 | 0387dc70db1806cd9a9a76637412ec11dd998be666584849b3185f7f9313c8fd28 32 | 0209c58240e50e3ba3f833c82655e8725c037a2294e14cf5d73a5df8d56159de69 33 | 03a355aa5e2e09dd44bb46a4722e9336e9e3ee4ee4e7b7a0cf5785b283bf2ab579 34 | 033cdd9d6d97cbfe7c26f902faf6a435780fe652e159ec953650ec7b1004082790 35 | 02f6a8148a62320e149cb15c544fe8a25ab483a0095d2280d03b8a00a7feada13d 36 | 02b3e772216695845fa9dda419fb5daca28154d8aa59ea302f05e916635e47b9f6 37 | 027d2c03c3ef0aec70f2c7e1e75454a5dfdd0e1adea670c1b3a4643c48ad0f1255 38 | 03c060e1e3771cbeccb38e119c2414702f3f5181a89652538851d2e3886bdd70c6 39 | 022d77cd1467019a6bf28f7375d0949ce30e6b5815c2758b98a74c2700bc006543 40 | 03a2efa402fd5268400c77c20e574ba86409ededee7c4020e4b9f0edbee53de0d4 41 | 03b357e68437da273dcf995a474a524439faad86fc9effc300183f714b0903468b 42 | 03eec88385be9da803a0d6579798d977a5d0c7f80917dab49cb73c9e3927142cb6 43 | 02a631f9ba0f28511614904df80d7f97a4f43f02249c8909dac92276ccf0bcdaed 44 | 025e466e97ed0e7910d3d90ceb0332df48ddf67d456b9e7303b50a3d89de357336 45 | 026ecabd2d22fdb737be21975ce9a694e108eb94f3649c586cc7461c8abf5da71a 46 | 03fd5487722d2576cb6d7081426b66a3e2986c1ce8358d479063fb5f2bb6dd5849 47 | 023a12bd3caf0b0f77bf4eea8e7a40dbe27932bf80b19ac72f5f5a64925a594196 48 | 0291bee5cf4b14c291c650732faa166040e4c18a14731f9a930c1e87d3ec12debb 49 | 02591d682c3da4a2a698633bf5751738b67c343285ebdc3492645cb44658911484 50 | 03f46f41027bbf44fafd6b059091b900dad41e6845b2241dc3254c7cdd3c5a16c6 51 | 028c6c67bef9e9eebe6a513272e50c230f0f91ed560c37bc9b033241ff6c3be78f 52 | 0374c33bd548ef02667d61341892134fcf216640bc2201ae61928cd0874f6314a7 53 | 020faaf5f3afe58300a335874c80681cf66933e2a7aeb28387c0d28bb048bc6349 54 | 034af4b81f8c450c2c870ce1df184aff1297e5fcd54944d98d81e1a545ffb22596 55 | 0385a30d8413af4f8f9e6312400f2d194fe14f02e719b24c3f83bf1fd233a8f963 56 | 033f2db2074e3217b3e5ee305301eeebb1160c4fa1e993ee280112f6348637999a 57 | 02a521a07e98f78b03fc1e039bc3a51408cd73119b5eb116e583fe57dc8db07aea 58 | 0311569442e870326ceec0de24eb5478c19e146ecd9d15e4666440f2f638875f42 59 | 0241267d2d7ee1a8e76f8d1546d0d30aefb2892d231cee0dde7776daf9f8021485 60 | 0348e843dc5b1bd246e6309b4924b81543d02b16c8083df973a89ce2c7eb89a10d 61 | 0249a43860d115143c35c09454863d6f82a95e47c1162fb9b2ebe0186eb26f453f 62 | 03231a67e424caf7d01a00d5cd49b0464942255b8e48766f96602bdfa4ea14fea8 63 | 0365ec2994b8cc0a20d40dd69edfe55ca32a54bcbbaa6b0ddcff36049301a54579 64 | 0230210c23b1a047bc9bdbb13448e67deddc108946de6de639bcc75d47c0216b1b 65 | 66 | -------------------------------------------------------------------------------- /hashing.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "hashing.h" 7 | #include "sha3/sha3.h" 8 | 9 | int sha256(const unsigned char *data, size_t length, unsigned char *digest) { 10 | SHA256_CTX ctx; 11 | if (SHA256_Init(&ctx) != 1) { 12 | printf("Failed to initialize SHA256 context\n"); 13 | return 1; 14 | } 15 | if (SHA256_Update(&ctx, data, length) != 1) { 16 | printf("Failed to update digest\n"); 17 | return 1; 18 | } 19 | if (SHA256_Final(digest, &ctx) != 1) { 20 | printf("Failed to finalize digest\n"); 21 | return 1; 22 | } 23 | return 0; // Success 24 | } 25 | 26 | int sha256_4(size_t length, const unsigned char *data0, const unsigned char *data1, 27 | const unsigned char *data2, const unsigned char *data3, 28 | unsigned char *digest0, unsigned char *digest1, 29 | unsigned char *digest2, unsigned char *digest3) { 30 | SHA256_CTX ctx[4]; 31 | 32 | if (SHA256_Init(&ctx[0]) != 1 || SHA256_Init(&ctx[1]) != 1 || 33 | SHA256_Init(&ctx[2]) != 1 || SHA256_Init(&ctx[3]) != 1) { 34 | printf("Failed to initialize SHA256 contexts\n"); 35 | return 1; 36 | } 37 | 38 | if (SHA256_Update(&ctx[0], data0, length) != 1 || 39 | SHA256_Update(&ctx[1], data1, length) != 1 || 40 | SHA256_Update(&ctx[2], data2, length) != 1 || 41 | SHA256_Update(&ctx[3], data3, length) != 1) { 42 | printf("Failed to update digests\n"); 43 | return 1; 44 | } 45 | 46 | if (SHA256_Final(digest0, &ctx[0]) != 1 || 47 | SHA256_Final(digest1, &ctx[1]) != 1 || 48 | SHA256_Final(digest2, &ctx[2]) != 1 || 49 | SHA256_Final(digest3, &ctx[3]) != 1) { 50 | printf("Failed to finalize digests\n"); 51 | return 1; 52 | } 53 | 54 | return 0; // Success 55 | } 56 | 57 | // Function for hashing 58 | int keccak(const unsigned char *data, size_t length, unsigned char *digest) { 59 | SHA3_256_CTX ctx; 60 | SHA3_256_Init(&ctx); 61 | SHA3_256_Update(&ctx,data,length); 62 | KECCAK_256_Final(digest,&ctx); 63 | return 0; // Success 64 | } 65 | 66 | 67 | 68 | int rmd160(const unsigned char *data, size_t length, unsigned char *digest) { 69 | RIPEMD160_CTX ctx; 70 | if (RIPEMD160_Init(&ctx) != 1) { 71 | printf("Failed to initialize RIPEMD-160 context\n"); 72 | return 1; 73 | } 74 | if (RIPEMD160_Update(&ctx, data, length) != 1) { 75 | printf("Failed to update digest\n"); 76 | return 1; 77 | } 78 | if (RIPEMD160_Final(digest, &ctx) != 1) { 79 | printf("Failed to finalize digest\n"); 80 | return 1; 81 | } 82 | return 0; // Success 83 | } 84 | 85 | int rmd160_4(size_t length, const unsigned char *data0, const unsigned char *data1, 86 | const unsigned char *data2, const unsigned char *data3, 87 | unsigned char *digest0, unsigned char *digest1, 88 | unsigned char *digest2, unsigned char *digest3) { 89 | RIPEMD160_CTX ctx[4]; 90 | 91 | if (RIPEMD160_Init(&ctx[0]) != 1 || RIPEMD160_Init(&ctx[1]) != 1 || 92 | RIPEMD160_Init(&ctx[2]) != 1 || RIPEMD160_Init(&ctx[3]) != 1) { 93 | printf("Failed to initialize RIPEMD-160 contexts\n"); 94 | return 1; 95 | } 96 | 97 | if (RIPEMD160_Update(&ctx[0], data0, length) != 1 || 98 | RIPEMD160_Update(&ctx[1], data1, length) != 1 || 99 | RIPEMD160_Update(&ctx[2], data2, length) != 1 || 100 | RIPEMD160_Update(&ctx[3], data3, length) != 1) { 101 | printf("Failed to update digests\n"); 102 | return 1; 103 | } 104 | 105 | if (RIPEMD160_Final(digest0, &ctx[0]) != 1 || 106 | RIPEMD160_Final(digest1, &ctx[1]) != 1 || 107 | RIPEMD160_Final(digest2, &ctx[2]) != 1 || 108 | RIPEMD160_Final(digest3, &ctx[3]) != 1) { 109 | printf("Failed to finalize digests\n"); 110 | return 1; 111 | } 112 | 113 | return 0; // Success 114 | } 115 | 116 | bool sha256_file(const char* file_name, uint8_t* digest) { 117 | FILE* file = fopen(file_name, "rb"); 118 | if (file == NULL) { 119 | printf("Failed to open file: %s\n", file_name); 120 | return false; 121 | } 122 | 123 | uint8_t buffer[8192]; // Buffer to read file contents 124 | size_t bytes_read; 125 | 126 | SHA256_CTX ctx; 127 | if (SHA256_Init(&ctx) != 1) { 128 | printf("Failed to initialize SHA256 context\n"); 129 | fclose(file); 130 | return false; 131 | } 132 | 133 | while ((bytes_read = fread(buffer, 1, sizeof(buffer), file)) > 0) { 134 | if (SHA256_Update(&ctx, buffer, bytes_read) != 1) { 135 | printf("Failed to update digest\n"); 136 | fclose(file); 137 | return false; 138 | } 139 | } 140 | 141 | if (SHA256_Final(digest, &ctx) != 1) { 142 | printf("Failed to finalize digest\n"); 143 | fclose(file); 144 | return false; 145 | } 146 | 147 | fclose(file); 148 | return true; 149 | } -------------------------------------------------------------------------------- /gmp256k1/Int.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Keyhunt distribution (https://github.com/albertobsd/keyhunt). 3 | Copyright (c) 2020 Luis Alberto 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | */ 23 | 24 | // Big integer class (GMP) 25 | 26 | #ifndef BIGINTH 27 | #define BIGINTH 28 | 29 | #include "Random.h" 30 | #include 31 | #include 32 | #include 33 | 34 | class Int { 35 | public: 36 | mpz_t num; 37 | 38 | Int(); 39 | Int(const char*); 40 | Int(const int32_t); 41 | Int(const uint32_t); 42 | Int(const int64_t); 43 | Int(const uint64_t); 44 | Int(const Int*); 45 | Int(const Int&); 46 | 47 | /* Aritmetic*/ 48 | void Add(const uint64_t); 49 | void Add(const uint32_t); 50 | void Add(const Int*); 51 | void Add(const Int*,const Int*); 52 | void AddOne(); 53 | void Sub(const uint64_t); 54 | void Sub(const uint32_t); 55 | void Sub(Int *); 56 | void Sub(Int *a, Int *b); 57 | void Mult(Int *); 58 | void Mult(uint64_t ); 59 | void IMult(int64_t ); 60 | 61 | void Div(Int *a,Int *mod = NULL); 62 | /* 63 | void Mult(Int *a,uint64_t b); 64 | void IMult(Int *a, int64_t b); 65 | void Mult(Int *a,Int *b)M 66 | */ 67 | void Neg(); 68 | void Abs(); 69 | 70 | bool IsGreater(Int *a); 71 | bool IsGreaterOrEqual(Int *a); 72 | bool IsLowerOrEqual(Int *a); 73 | bool IsLower(Int *a); 74 | bool IsEqual(Int *a); 75 | bool IsZero(); 76 | bool IsOne(); 77 | //bool IsStrictPositive(); 78 | bool IsPositive(); 79 | bool IsNegative(); 80 | bool IsEven(); 81 | bool IsOdd(); 82 | 83 | /*Setters*/ 84 | void SetInt64(const uint64_t value); 85 | void SetInt32(const uint32_t value); 86 | void Set(const Int* other); 87 | void Set(const char *str); 88 | void SetBase10(const char *str); 89 | void SetBase16(const char *str); 90 | 91 | // Size 92 | int GetSize(); 93 | int GetBitLength(); 94 | // 95 | uint64_t GetInt64(); 96 | uint32_t GetInt32(); 97 | int GetBit(uint32_t n); 98 | unsigned char GetByte(int n); 99 | void Get32Bytes(unsigned char *buff); 100 | void Set32Bytes(unsigned char *buff); 101 | 102 | char* GetBase2(); 103 | char* GetBase10(); 104 | char* GetBase16(); 105 | 106 | 107 | void SetBit(uint32_t n); 108 | void ClearBit(uint32_t n); 109 | /* 110 | char* GetBaseN(int n,const char *charset); 111 | char* GetBlockStr(); 112 | char* GetC64Str(int nbDigit); 113 | */ 114 | // Left shift 115 | void ShiftL(uint32_t n); 116 | void Mod(Int *a); // this <- this (mod a) 117 | 118 | /* 119 | All next mod n are setup as mod P, where P = FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F 120 | */ 121 | void ModInv(); // this <- this^-1 (mod n) 122 | void ModAdd(Int *a); // this <- this+a (mod n) [0 30 | 31 | #include "keccak.h" 32 | 33 | #define secret /* can't use in variable-time operations, should zero */ 34 | 35 | #define FOR5(X, STMT) do \ 36 | { \ 37 | (X) = 0; (STMT); \ 38 | (X) = 1; (STMT); \ 39 | (X) = 2; (STMT); \ 40 | (X) = 3; (STMT); \ 41 | (X) = 4; (STMT); \ 42 | } while (0) 43 | 44 | static inline secret uint64_t 45 | rol64(secret uint64_t v, unsigned c) 46 | { 47 | 48 | return ((v << c) | (v >> (64 - c))); 49 | } 50 | 51 | static inline void 52 | keccakf1600_theta(secret uint64_t A[25]) 53 | { 54 | secret uint64_t C0, C1, C2, C3, C4; 55 | unsigned y; 56 | 57 | C0 = C1 = C2 = C3 = C4 = 0; 58 | #pragma GCC diagnostic ignored "-Wpedantic" 59 | FOR5(y, { 60 | C0 ^= A[0 + 5*y]; 61 | C1 ^= A[1 + 5*y]; 62 | C2 ^= A[2 + 5*y]; 63 | C3 ^= A[3 + 5*y]; 64 | C4 ^= A[4 + 5*y]; 65 | }); 66 | FOR5(y, { 67 | A[0 + 5*y] ^= C4 ^ rol64(C1, 1); 68 | A[1 + 5*y] ^= C0 ^ rol64(C2, 1); 69 | A[2 + 5*y] ^= C1 ^ rol64(C3, 1); 70 | A[3 + 5*y] ^= C2 ^ rol64(C4, 1); 71 | A[4 + 5*y] ^= C3 ^ rol64(C0, 1); 72 | }); 73 | #pragma GCC diagnostic pop 74 | } 75 | 76 | static inline void 77 | keccakf1600_rho_pi(secret uint64_t A[25]) 78 | { 79 | secret uint64_t T, U; 80 | 81 | /* 82 | * Permute by (x,y) |---> (y, 2x + 3y mod 5) starting at (1,0), 83 | * rotate the ith element by (i + 1)(i + 2)/2 mod 64. 84 | */ 85 | U = A[ 1]; T = U; 86 | U = A[10]; A[10] = rol64(T, 1); T = U; 87 | U = A[ 7]; A[ 7] = rol64(T, 3); T = U; 88 | U = A[11]; A[11] = rol64(T, 6); T = U; 89 | U = A[17]; A[17] = rol64(T, 10); T = U; 90 | U = A[18]; A[18] = rol64(T, 15); T = U; 91 | U = A[ 3]; A[ 3] = rol64(T, 21); T = U; 92 | U = A[ 5]; A[ 5] = rol64(T, 28); T = U; 93 | U = A[16]; A[16] = rol64(T, 36); T = U; 94 | U = A[ 8]; A[ 8] = rol64(T, 45); T = U; 95 | U = A[21]; A[21] = rol64(T, 55); T = U; 96 | U = A[24]; A[24] = rol64(T, 2); T = U; 97 | U = A[ 4]; A[ 4] = rol64(T, 14); T = U; 98 | U = A[15]; A[15] = rol64(T, 27); T = U; 99 | U = A[23]; A[23] = rol64(T, 41); T = U; 100 | U = A[19]; A[19] = rol64(T, 56); T = U; 101 | U = A[13]; A[13] = rol64(T, 8); T = U; 102 | U = A[12]; A[12] = rol64(T, 25); T = U; 103 | U = A[ 2]; A[ 2] = rol64(T, 43); T = U; 104 | U = A[20]; A[20] = rol64(T, 62); T = U; 105 | U = A[14]; A[14] = rol64(T, 18); T = U; 106 | U = A[22]; A[22] = rol64(T, 39); T = U; 107 | U = A[ 9]; A[ 9] = rol64(T, 61); T = U; 108 | U = A[ 6]; A[ 6] = rol64(T, 20); T = U; 109 | A[ 1] = rol64(T, 44); 110 | } 111 | 112 | static inline void 113 | keccakf1600_chi(secret uint64_t A[25]) 114 | { 115 | secret uint64_t B0, B1, B2, B3, B4; 116 | unsigned y; 117 | 118 | #pragma GCC diagnostic ignored "-Wpedantic" 119 | FOR5(y, { 120 | B0 = A[0 + 5*y]; 121 | B1 = A[1 + 5*y]; 122 | B2 = A[2 + 5*y]; 123 | B3 = A[3 + 5*y]; 124 | B4 = A[4 + 5*y]; 125 | A[0 + 5*y] ^= ~B1 & B2; 126 | A[1 + 5*y] ^= ~B2 & B3; 127 | A[2 + 5*y] ^= ~B3 & B4; 128 | A[3 + 5*y] ^= ~B4 & B0; 129 | A[4 + 5*y] ^= ~B0 & B1; 130 | }); 131 | #pragma GCC diagnostic pop 132 | } 133 | 134 | static void 135 | keccakf1600_round(secret uint64_t A[25]) 136 | { 137 | 138 | keccakf1600_theta(A); 139 | keccakf1600_rho_pi(A); 140 | keccakf1600_chi(A); 141 | } 142 | 143 | void 144 | keccakf1600(secret uint64_t A[25]) 145 | { 146 | /* 147 | * RC[i] = \sum_{j = 0,...,6} rc(j + 7i) 2^(2^j - 1), 148 | * rc(t) = (x^t mod x^8 + x^6 + x^5 + x^4 + 1) mod x in GF(2)[x] 149 | */ 150 | static const uint64_t RC[24] = { 151 | 0x0000000000000001ULL, 152 | 0x0000000000008082ULL, 153 | 0x800000000000808aULL, 154 | 0x8000000080008000ULL, 155 | 0x000000000000808bULL, 156 | 0x0000000080000001ULL, 157 | 0x8000000080008081ULL, 158 | 0x8000000000008009ULL, 159 | 0x000000000000008aULL, 160 | 0x0000000000000088ULL, 161 | 0x0000000080008009ULL, 162 | 0x000000008000000aULL, 163 | 0x000000008000808bULL, 164 | 0x800000000000008bULL, 165 | 0x8000000000008089ULL, 166 | 0x8000000000008003ULL, 167 | 0x8000000000008002ULL, 168 | 0x8000000000000080ULL, 169 | 0x000000000000800aULL, 170 | 0x800000008000000aULL, 171 | 0x8000000080008081ULL, 172 | 0x8000000000008080ULL, 173 | 0x0000000080000001ULL, 174 | 0x8000000080008008ULL, 175 | }; 176 | unsigned i; 177 | 178 | for (i = 0; i < 24; i++) { 179 | keccakf1600_round(A); 180 | A[0] ^= RC[i]; 181 | } 182 | } 183 | -------------------------------------------------------------------------------- /BSGSD.md: -------------------------------------------------------------------------------- 1 | # BSGSD 2 | 3 | `BSGS` method but as local `server`, final `D` stand for daemon. 4 | 5 | ### Compilation 6 | Same as keyhunt we need to do 7 | ```make bsgsd``` 8 | 9 | ### Parameters 10 | 11 | - `-6` To skip file checksum 12 | - `-t number` Threads Number 13 | - `-k factor` Same K factor dor keyhunt 14 | - `-n number` Length of the Range to scan each cycle, same as keyhunt 15 | - `-i ip` IP for listening default is `127.0.0.1` 16 | - `-p port` Port for listening default is `8080` 17 | 18 | bsgsd use the same keyhunt files `.blm` and `.tbl` 19 | 20 | ### Server 21 | This program is an small and custom server without any protocol. 22 | By default the server only listen on `localhost` port `8080` 23 | ``` 24 | localhost:8080 25 | ``` 26 | Tha main advantage of this server is that BSGS blooms and table are always on RAM 27 | Clients need to send a single line and wait for reply 28 | 29 | Format of the client request: 30 | ``` 31 | : 32 | ``` 33 | example puzzle 63 34 | 35 | ``` 36 | 0365ec2994b8cc0a20d40dd69edfe55ca32a54bcbbaa6b0ddcff36049301a54579 4000000000000000:8000000000000000 37 | ``` 38 | The search is done Sequentialy Client need to knows more o less the time expect time to solve. 39 | 40 | The server only reply one single line. Client must read that line and proceed according its content, possible replies: 41 | 42 | - `404 Not Found` if the key wasn't in the given range 43 | - `400 Bad Request`if there is some error on client request 44 | - `value` hexadecimal value with the Private KEY in case of be found 45 | 46 | The server will close the Conection inmediatly after send that line, also in case some other error the server will close the Conection without send any error message. Client need to hadle the Conection status by his own. 47 | 48 | ### Example 49 | 50 | Run the server in one terminal: 51 | ``` 52 | ./bsgsd -k 4096 -t 8 -6 53 | [+] Version 0.2.230519 Satoshi Quest, developed by AlbertoBSD 54 | [+] K factor 4096 55 | [+] Threads : 8 56 | [W] Skipping checksums on files 57 | [+] Mode BSGS secuential 58 | [+] N = 0x100000000000 59 | [+] Bloom filter for 17179869184 elements : 58890.60 MB 60 | [+] Bloom filter for 536870912 elements : 1840.33 MB 61 | [+] Bloom filter for 16777216 elements : 57.51 MB 62 | [+] Allocating 256.00 MB for 16777216 bP Points 63 | [+] Reading bloom filter from file keyhunt_bsgs_4_17179869184.blm .... Done! 64 | [+] Reading bloom filter from file keyhunt_bsgs_6_536870912.blm .... Done! 65 | [+] Reading bP Table from file keyhunt_bsgs_2_16777216.tbl .... Done! 66 | [+] Reading bloom filter from file keyhunt_bsgs_7_16777216.blm .... Done! 67 | [+] Listening in 127.0.0.1:8080 68 | ``` 69 | Once that you see `[+] Listening in 127.0.0.1:8080` the server is ready to process client requests 70 | 71 | Now we can connect it in annother terminal with `netcat` as client, this server is `64 GB` ram, expected time for puzzle 63 `~8` Seconds 72 | 73 | command: 74 | ``` 75 | time echo "0365ec2994b8cc0a20d40dd69edfe55ca32a54bcbbaa6b0ddcff36049301a54579 4000000000000000:8000000000000000" | nc -v localhost 8080 76 | ``` 77 | ``` 78 | time echo "0365ec2994b8cc0a20d40dd69edfe55ca32a54bcbbaa6b0ddcff36049301a54579 4000000000000000:8000000000000000" | nc -v localhost 8080 79 | localhost.localdomain [127.0.0.1] 8080 (http-alt) open 80 | 7cce5efdaccf6808 81 | real 0m7.551s 82 | user 0m0.002s 83 | sys 0m0.001s 84 | ``` 85 | If you notice the answer from the server is `7cce5efdaccf6808` 86 | 87 | **Other example `404 Not Found`:** 88 | 89 | ``` 90 | time echo "0233709eb11e0d4439a729f21c2c443dedb727528229713f0065721ba8fa46f00e 4000000000000000:8000000000000000" | nc -v localhost 8080 91 | localhost.localdomain [127.0.0.1] 8080 (http-alt) open 92 | 404 Not Found 93 | real 0m7.948s 94 | user 0m0.003s 95 | sys 0m0.000s 96 | ``` 97 | 98 | ### One client at the time 99 | To maximize the Speed of BSGS this server only attends one client at the time. 100 | I know what are you thinking, but if you are doing 10 ranges of 63 bits, you can send only one range and the time and the program only will take 80 seconds (Based on the speed of the previous example). 101 | 102 | But if i do the program multi-client, and you send the 10 ranges at the same time in 10 different connections, the whole process will also take 80 seconds... so is only question of how your client send the data and manage the ranges.. 103 | 104 | ### Client 105 | 106 | Here is a small python example to implent by your self as client. 107 | 108 | ``` 109 | import socket 110 | import time 111 | 112 | def send_and_receive_line(host, port, message): 113 | # Create a TCP socket 114 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 115 | 116 | try: 117 | # Connect to the server 118 | sock.connect((host, port)) 119 | 120 | # Send the message 121 | start_time = time.time() 122 | sock.sendall(message.encode()) 123 | 124 | # Receive the reply 125 | reply = sock.recv(1024).decode() 126 | end_time = time.time() 127 | 128 | # Calculate the elapsed time 129 | elapsed_time = end_time - start_time 130 | sock.close() 131 | return reply, elapsed_time 132 | 133 | except ConnectionResetError: 134 | print("Server closed the connection without replying.") 135 | return None, None 136 | 137 | except ConnectionRefusedError: 138 | print("Connection refused. Make sure the server is running and the host/port are correct.") 139 | return None, None 140 | 141 | except AttributeError: 142 | pass 143 | return None, None 144 | 145 | 146 | # TCP connection details 147 | host = 'localhost' # Change this to the server's hostname or IP address 148 | port = 8080 # Change this to the server's port number 149 | 150 | # Message to send 151 | message = '0365ec2994b8cc0a20d40dd69edfe55ca32a54bcbbaa6b0ddcff36049301a54579 4000000000000000:8000000000000000' 152 | 153 | # Number of iterations in the loop 154 | num_iterations = 5 155 | 156 | # Loop for sending and receiving messages 157 | for i in range(num_iterations): 158 | reply, elapsed_time = send_and_receive_line(host, port, message) 159 | if reply is not None: 160 | print(f'Received reply: {reply}') 161 | print(f'Elapsed time: {elapsed_time} seconds') 162 | ``` 163 | 164 | The previous client example only repeat 5 times the same target, change it according to your needs. -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Version 0.2.230519 Satoshi Quest 2 | - Speed x2 in BSGS mode for main version 3 | 4 | # Version 0.2.230507 Satoshi Quest 5 | - fixed some variables names 6 | - fixed bug in addvanity (realloc problem with dirty memory) 7 | - Added option -6 to skip SHA256 checksum when you read the files (Improved startup process) 8 | - Added warning when you Endomorphism and BSGS, THEY DON'T WORK together! 9 | - Legacy version for ARM processor and other systems 10 | - remove pub2rmd 11 | 12 | # Version 0.2.230430 Satoshi Quest 13 | - fixed typos in README 14 | - Speed counter fixed for Compress search without endomorphism check https://github.com/albertobsd/keyhunt/tree/development#Speeds 15 | 16 | # Version 0.2.230428 Satoshi Quest 17 | - Merge of address and rmd160 speeds 18 | - Added option for endomorphism 19 | - Added SAVE bloom filter and table option for adddress, rmd160, minikeys and xpoint 20 | - Improved Makefile options 21 | - Updated random function to use the Linux RNG with the function getrandom 22 | 23 | # Version 0.2.211117 SSE Trick or treat ¡Beta! 24 | - Minikeys new sequential generator and x2 times more speed 25 | - third bloom filter check for bsgs 20% less memory usage 26 | 27 | # Version 0.2.211031 Trick or treat ¡Beta! 28 | - Minikeys improvements in speed 29 | - Test to try solve the https://github.com/albertobsd/keyhunt/issues/139 issue 30 | 31 | # Version 0.2.211026 Chocolate ¡Beta! 32 | - Solved https://github.com/albertobsd/keyhunt/issues/130 33 | - Minikeys new generator improvements in speed 34 | 35 | # Version 0.2.211024 Chocolate ¡Beta! 36 | - Ethereum support 37 | - Double speed for rmd160 mode 38 | - Minikeys mode support 39 | - Stride option 40 | 41 | # Version 0.2.211018 Chocolate ¡Beta! 42 | - Solved some bugs: https://github.com/albertobsd/keyhunt/issues/122 https://github.com/albertobsd/keyhunt/issues/111 43 | - Files are going to be updated automatillyca 44 | -- from keyhunt_bsgs_3_*.blm to keyhunt_bsgs_4*.blm 45 | -- from keyhunt_bsgs_1_*.blm to keyhunt_bsgs_5*.blm 46 | -- the program will notify you when time to delete the old files 47 | 48 | # Version 0.2.211012 Chocolate ¡Beta! 49 | - Fixed the slow bP table generation. 50 | -- This fix make obsolete the files keyhunt_bsgs_0_*.blm 51 | -- please delete those files, please do: 52 | 53 | ``` 54 | rm keyhunt_bsgs_0_*.blm 55 | ``` 56 | 57 | - Added multi vanitysearch for address mode 58 | 59 | 60 | # Version 0.2.211007 Chocolate ¡Beta! 61 | - BSGS improvements: 62 | -- 10x more Speed 63 | -- new submodes for BSGS, secuential (default), backward, both, random and dance 64 | -- automatic file generation for bloom filter file and bPtable file. 65 | -- Good bye to bPfile. 66 | - Memory check periodically for bloom filters and bP Table 67 | 68 | # Version 0.1.20210420 secp256k1 69 | - Solved Issues 49, 50 51 70 | See: 71 | https://github.com/albertobsd/keyhunt/issues/51 72 | https://github.com/albertobsd/keyhunt/issues/50 73 | https://github.com/albertobsd/keyhunt/issues/49 74 | - Solved Issues 56 https://github.com/albertobsd/keyhunt/issues/56 75 | - Added mutex to the bloom filter for multithread writing 76 | 77 | # Version 0.1.20210412 secp256k1 78 | - Full migration from libgmp to secp256k1 79 | - Change the way for keygeneration for modes xpoint, address, and rmd160 80 | - Improve performance for xpoint mode, now is ten times faster 81 | - Change N variable type for modes address,rmd160 and xpoint, from uint32_t to uint64_t 82 | - Added method pub2rmd to search publickeys of the puzzles and other legacy address (Compress publickeys only) 83 | 84 | # Version 0.1.20210331 85 | - Small changes to be compiled with mingw on Windows 86 | - Changed sort functions and binary search for modes address/rmd160/xpoint, now those modes can load MAX 2^64 items 87 | - xpoint input file now can contains Comments after the line of data 88 | - from this version all furthers developments will be in the branch `development` 89 | 90 | # Version 0.1.20210328 91 | - Added a progress counter (this solve bug https://github.com/albertobsd/keyhunt/issues/18 ) 92 | - Added multithread for precalculating bP items or reading then from file 93 | - Fixed the code to avoid warnings (this solve the issue https://github.com/albertobsd/keyhunt/issues/19) 94 | 95 | # Version 0.1.20210322 96 | - Added xxhash for bloomfilter this hash have better performance than murmurhash2. And it is 64 bits hash :) 97 | - We reduce the number of items of the bPtable in ram using a second bloom filter, thanks @iceland2k14 98 | - The ram saved space is around 80%, so we can use a bigger K value, around 4 or 5 times bigger than previous version 99 | 100 | # Version 0.1.20210320 K*BSGS 101 | - Solved little error with compress and uncompress new param -l. See https://github.com/albertobsd/keyhunt/issues/17 102 | - function bsgs optimized to use a little less RAM (not related with Pfile) 103 | - Again removed some compile warnings. See https://github.com/albertobsd/keyhunt/issues/16 104 | 105 | # Version 0.1.20210311 K*BSGS 106 | - Added mode rmd160, this method works two times faster than Address method. This mode can search all the altcoins 107 | 108 | 109 | # Version 0.1.20210311 K*BSGS 110 | - Solved some bug when the publickeys in the input file was invalid but the program keeps running with 0 publickeys 111 | - Now publickeys can be compressed, not only uncompressed 112 | 113 | # Version 0.1.20210306 K*BSGS 114 | - Added K factor for BSGS 115 | - Added bPfile.c to generate a precalculated file 116 | - Remove unused files about keccak and sha3 117 | - Change Bloom filter limits and % of error from 0.001 to 0.00001 in bloomfilter. 118 | 119 | # Version 0.1.20210112 BSGS 120 | - Added mode BSGS this work with a file with uncompressed keys 121 | - Updated bloom filter to allow More items 122 | 123 | # Version 0.1.20201228 124 | - Change Quicksort to Introsort, this solve some edge cases of quicksort. 125 | - Introsort is avaible to keyhunt and hexcharstoraw. worst case. O(N log N). 126 | - Aling of some output text 127 | 128 | # Version 0.1.20201223 129 | - Added new tool hexcharstoraw to create a raw binary file for xpoint from a text-hexadecimal file 130 | - Added option -w to work with raw binary file, this file contains xpoint in binary format fixed to 32 bytes 131 | 132 | # Version 0.1.20201222 133 | - Fixed some ugly bug in the searchbinary function thanks to Ujang 134 | - Added to stdout the vanitykeys found with -v option 135 | 136 | # Version 0.1.20201221 137 | - Fixed search by xpoint. 138 | - Added -e option to skip the sort process whe the file is already sorted. 139 | - Fixed debugcount when upto N is less than debugcount. 140 | - Changed "-R upto" to "-R" and added "-n upto" option. 141 | 142 | # Version 0.1.20201218 143 | - Minor bugs fixed. 144 | 145 | # Version 0.1.20201217 146 | - First Release 147 | - Thanks to all CryptoHunters to make this code possible 148 | -------------------------------------------------------------------------------- /bloom/bloom.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2019, Jyri J. Virkki 3 | * All rights reserved. 4 | * 5 | * This file is under BSD license. See LICENSE file. 6 | */ 7 | 8 | #ifndef _BLOOM_H 9 | #define _BLOOM_H 10 | 11 | #ifdef _WIN64 12 | #include 13 | #endif 14 | 15 | #ifdef __cplusplus 16 | extern "C" { 17 | #endif 18 | 19 | 20 | /** *************************************************************************** 21 | * Structure to keep track of one bloom filter. Caller needs to 22 | * allocate this and pass it to the functions below. First call for 23 | * every struct must be to bloom_init(). 24 | * 25 | */ 26 | struct bloom 27 | { 28 | // These fields are part of the public interface of this structure. 29 | // Client code may read these values if desired. Client code MUST NOT 30 | // modify any of these. 31 | uint64_t entries; 32 | uint64_t bits; 33 | uint64_t bytes; 34 | uint8_t hashes; 35 | long double error; 36 | 37 | // Fields below are private to the implementation. These may go away or 38 | // change incompatibly at any moment. Client code MUST NOT access or rely 39 | // on these. 40 | uint8_t ready; 41 | uint8_t major; 42 | uint8_t minor; 43 | double bpe; 44 | uint8_t *bf; 45 | }; 46 | /* 47 | Customs 48 | */ 49 | 50 | /* 51 | int bloom_loadcustom(struct bloom * bloom, char * filename); 52 | int bloom_savecustom(struct bloom * bloom, char * filename); 53 | */ 54 | 55 | 56 | /** *************************************************************************** 57 | * Initialize the bloom filter for use. 58 | * 59 | * The filter is initialized with a bit field and number of hash functions 60 | * according to the computations from the wikipedia entry: 61 | * http://en.wikipedia.org/wiki/Bloom_filter 62 | * 63 | * Optimal number of bits is: 64 | * bits = (entries * ln(error)) / ln(2)^2 65 | * 66 | * Optimal number of hash functions is: 67 | * hashes = bpe * ln(2) 68 | * 69 | * Parameters: 70 | * ----------- 71 | * bloom - Pointer to an allocated struct bloom (see above). 72 | * entries - The expected number of entries which will be inserted. 73 | * Must be at least 1000 (in practice, likely much larger). 74 | * error - Probability of collision (as long as entries are not 75 | * exceeded). 76 | * 77 | * Return: 78 | * ------- 79 | * 0 - on success 80 | * 1 - on failure 81 | * 82 | */ 83 | int bloom_init2(struct bloom * bloom, uint64_t entries, long double error); 84 | 85 | 86 | /** 87 | * DEPRECATED. 88 | * Kept for compatibility with libbloom v.1. To be removed in v3.0. 89 | * 90 | */ 91 | int bloom_init(struct bloom * bloom, uint64_t entries, long double error); 92 | 93 | 94 | /** *************************************************************************** 95 | * Check if the given element is in the bloom filter. Remember this may 96 | * return false positive if a collision occurred. 97 | * 98 | * Parameters: 99 | * ----------- 100 | * bloom - Pointer to an allocated struct bloom (see above). 101 | * buffer - Pointer to buffer containing element to check. 102 | * len - Size of 'buffer'. 103 | * 104 | * Return: 105 | * ------- 106 | * 0 - element is not present 107 | * 1 - element is present (or false positive due to collision) 108 | * -1 - bloom not initialized 109 | * 110 | */ 111 | int bloom_check(struct bloom * bloom, const void * buffer, int len); 112 | 113 | 114 | /** *************************************************************************** 115 | * Add the given element to the bloom filter. 116 | * The return code indicates if the element (or a collision) was already in, 117 | * so for the common check+add use case, no need to call check separately. 118 | * 119 | * Parameters: 120 | * ----------- 121 | * bloom - Pointer to an allocated struct bloom (see above). 122 | * buffer - Pointer to buffer containing element to add. 123 | * len - Size of 'buffer'. 124 | * 125 | * Return: 126 | * ------- 127 | * 0 - element was not present and was added 128 | * 1 - element (or a collision) had already been added previously 129 | * -1 - bloom not initialized 130 | * 131 | */ 132 | int bloom_add(struct bloom * bloom, const void * buffer, int len); 133 | 134 | 135 | /** *************************************************************************** 136 | * Print (to stdout) info about this bloom filter. Debugging aid. 137 | * 138 | */ 139 | void bloom_print(struct bloom * bloom); 140 | 141 | 142 | /** *************************************************************************** 143 | * Deallocate internal storage. 144 | * 145 | * Upon return, the bloom struct is no longer usable. You may call bloom_init 146 | * again on the same struct to reinitialize it again. 147 | * 148 | * Parameters: 149 | * ----------- 150 | * bloom - Pointer to an allocated struct bloom (see above). 151 | * 152 | * Return: none 153 | * 154 | */ 155 | void bloom_free(struct bloom * bloom); 156 | 157 | 158 | /** *************************************************************************** 159 | * Erase internal storage. 160 | * 161 | * Erases all elements. Upon return, the bloom struct returns to its initial 162 | * (initialized) state. 163 | * 164 | * Parameters: 165 | * ----------- 166 | * bloom - Pointer to an allocated struct bloom (see above). 167 | * 168 | * Return: 169 | * 0 - on success 170 | * 1 - on failure 171 | * 172 | */ 173 | int bloom_reset(struct bloom * bloom); 174 | 175 | 176 | /** *************************************************************************** 177 | * Save a bloom filter to a file. 178 | * 179 | * Parameters: 180 | * ----------- 181 | * bloom - Pointer to an allocated struct bloom (see above). 182 | * filename - Create (or overwrite) bloom data to this file. 183 | * 184 | * Return: 185 | * 0 - on success 186 | * 1 - on failure 187 | * 188 | */ 189 | //int bloom_save(struct bloom * bloom, char * filename); 190 | 191 | 192 | /** *************************************************************************** 193 | * Load a bloom filter from a file. 194 | * 195 | * This functions loads a file previously saved with bloom_save(). 196 | * 197 | * Parameters: 198 | * ----------- 199 | * bloom - Pointer to an allocated struct bloom (see above). 200 | * filename - Load bloom filter data from this file. 201 | * 202 | * Return: 203 | * 0 - on success 204 | * > 0 - on failure 205 | * 206 | */ 207 | //int bloom_load(struct bloom * bloom, char * filename); 208 | 209 | 210 | /** *************************************************************************** 211 | * Returns version string compiled into library. 212 | * 213 | * Return: version string 214 | * 215 | */ 216 | const char * bloom_version(); 217 | 218 | #ifdef __cplusplus 219 | } 220 | #endif 221 | 222 | #endif -------------------------------------------------------------------------------- /base58/base58.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012-2014 Luke Dashjr 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the standard MIT license. See COPYING for more details. 6 | */ 7 | 8 | #ifndef _WIN64 9 | #include 10 | #else 11 | #include 12 | #endif 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | #include "libbase58.h" 20 | 21 | bool (*b58_sha256_impl)(void *, const void *, size_t) = NULL; 22 | 23 | static const int8_t b58digits_map[] = { 24 | -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, 25 | -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, 26 | -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, 27 | -1, 0, 1, 2, 3, 4, 5, 6, 7, 8,-1,-1,-1,-1,-1,-1, 28 | -1, 9,10,11,12,13,14,15, 16,-1,17,18,19,20,21,-1, 29 | 22,23,24,25,26,27,28,29, 30,31,32,-1,-1,-1,-1,-1, 30 | -1,33,34,35,36,37,38,39, 40,41,42,43,-1,44,45,46, 31 | 47,48,49,50,51,52,53,54, 55,56,57,-1,-1,-1,-1,-1, 32 | }; 33 | 34 | typedef uint64_t b58_maxint_t; 35 | typedef uint32_t b58_almostmaxint_t; 36 | #define b58_almostmaxint_bits (sizeof(b58_almostmaxint_t) * 8) 37 | static const b58_almostmaxint_t b58_almostmaxint_mask = ((((b58_maxint_t)1) << b58_almostmaxint_bits) - 1); 38 | 39 | bool b58tobin(void *bin, size_t *binszp, const char *b58, size_t b58sz) 40 | { 41 | size_t binsz = *binszp; 42 | const unsigned char *b58u = (void*)b58; 43 | unsigned char *binu = bin; 44 | size_t outisz = (binsz + sizeof(b58_almostmaxint_t) - 1) / sizeof(b58_almostmaxint_t); 45 | b58_almostmaxint_t outi[outisz]; 46 | b58_maxint_t t; 47 | b58_almostmaxint_t c; 48 | size_t i, j; 49 | uint8_t bytesleft = binsz % sizeof(b58_almostmaxint_t); 50 | b58_almostmaxint_t zeromask = bytesleft ? (b58_almostmaxint_mask << (bytesleft * 8)) : 0; 51 | unsigned zerocount = 0; 52 | 53 | if (!b58sz) 54 | b58sz = strlen(b58); 55 | 56 | for (i = 0; i < outisz; ++i) { 57 | outi[i] = 0; 58 | } 59 | 60 | // Leading zeros, just count 61 | for (i = 0; i < b58sz && b58u[i] == '1'; ++i) 62 | ++zerocount; 63 | 64 | for ( ; i < b58sz; ++i) 65 | { 66 | if (b58u[i] & 0x80) 67 | // High-bit set on invalid digit 68 | return false; 69 | if (b58digits_map[b58u[i]] == -1) 70 | // Invalid base58 digit 71 | return false; 72 | c = (unsigned)b58digits_map[b58u[i]]; 73 | for (j = outisz; j--; ) 74 | { 75 | t = ((b58_maxint_t)outi[j]) * 58 + c; 76 | c = t >> b58_almostmaxint_bits; 77 | outi[j] = t & b58_almostmaxint_mask; 78 | } 79 | if (c) 80 | // Output number too big (carry to the next int32) 81 | return false; 82 | if (outi[0] & zeromask) 83 | // Output number too big (last int32 filled too far) 84 | return false; 85 | } 86 | 87 | j = 0; 88 | if (bytesleft) { 89 | for (i = bytesleft; i > 0; --i) { 90 | *(binu++) = (outi[0] >> (8 * (i - 1))) & 0xff; 91 | } 92 | ++j; 93 | } 94 | 95 | for (; j < outisz; ++j) 96 | { 97 | for (i = sizeof(*outi); i > 0; --i) { 98 | *(binu++) = (outi[j] >> (8 * (i - 1))) & 0xff; 99 | } 100 | } 101 | 102 | // Count canonical base58 byte count 103 | binu = bin; 104 | for (i = 0; i < binsz; ++i) 105 | { 106 | if (binu[i]) 107 | break; 108 | --*binszp; 109 | } 110 | *binszp += zerocount; 111 | 112 | return true; 113 | } 114 | 115 | static 116 | bool my_dblsha256(void *hash, const void *data, size_t datasz) 117 | { 118 | uint8_t buf[0x20]; 119 | return b58_sha256_impl(buf, data, datasz) && b58_sha256_impl(hash, buf, sizeof(buf)); 120 | } 121 | 122 | int b58check(const void *bin, size_t binsz, const char *base58str, size_t b58sz) 123 | { 124 | unsigned char buf[32]; 125 | const uint8_t *binc = bin; 126 | unsigned i; 127 | if (binsz < 4) 128 | return -4; 129 | if (!my_dblsha256(buf, bin, binsz - 4)) 130 | return -2; 131 | if (memcmp(&binc[binsz - 4], buf, 4)) 132 | return -1; 133 | 134 | // Check number of zeros is correct AFTER verifying checksum (to avoid possibility of accessing base58str beyond the end) 135 | for (i = 0; binc[i] == '\0' && base58str[i] == '1'; ++i) 136 | {} // Just finding the end of zeros, nothing to do in loop 137 | if (binc[i] == '\0' || base58str[i] == '1') 138 | return -3; 139 | 140 | return binc[0]; 141 | } 142 | 143 | static const char b58digits_ordered[] = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; 144 | 145 | bool b58enc(char *b58, size_t *b58sz, const void *data, size_t binsz) 146 | { 147 | const uint8_t *bin = data; 148 | int carry; 149 | size_t i, j, high, zcount = 0; 150 | size_t size; 151 | 152 | while (zcount < binsz && !bin[zcount]) 153 | ++zcount; 154 | 155 | size = (binsz - zcount) * 138 / 100 + 1; 156 | uint8_t buf[size]; 157 | memset(buf, 0, size); 158 | 159 | for (i = zcount, high = size - 1; i < binsz; ++i, high = j) 160 | { 161 | for (carry = bin[i], j = size - 1; (j > high) || carry; --j) 162 | { 163 | carry += 256 * buf[j]; 164 | buf[j] = carry % 58; 165 | carry /= 58; 166 | if (!j) { 167 | // Otherwise j wraps to maxint which is > high 168 | break; 169 | } 170 | } 171 | } 172 | 173 | for (j = 0; j < size && !buf[j]; ++j); 174 | 175 | if (*b58sz <= zcount + size - j) 176 | { 177 | *b58sz = zcount + size - j + 1; 178 | return false; 179 | } 180 | 181 | if (zcount) 182 | memset(b58, '1', zcount); 183 | for (i = zcount; j < size; ++i, ++j) 184 | b58[i] = b58digits_ordered[buf[j]]; 185 | b58[i] = '\0'; 186 | *b58sz = i + 1; 187 | 188 | return true; 189 | } 190 | 191 | bool b58enc_custom(char *b58, size_t *b58sz, const void *data, size_t binsz,char *buffer) 192 | { 193 | const uint8_t *bin = data; 194 | int carry; 195 | size_t i, j, high, zcount = 0; 196 | size_t size; 197 | 198 | while (zcount < binsz && !bin[zcount]) 199 | ++zcount; 200 | 201 | size = (binsz - zcount) * 138 / 100 + 1; 202 | uint8_t buf[size]; 203 | memset(buf, 0, size); 204 | 205 | for (i = zcount, high = size - 1; i < binsz; ++i, high = j) 206 | { 207 | for (carry = bin[i], j = size - 1; (j > high) || carry; --j) 208 | { 209 | carry += 256 * buf[j]; 210 | buf[j] = carry % 58; 211 | carry /= 58; 212 | if (!j) { 213 | // Otherwise j wraps to maxint which is > high 214 | break; 215 | } 216 | } 217 | } 218 | 219 | for (j = 0; j < size && !buf[j]; ++j); 220 | 221 | if (*b58sz <= zcount + size - j) 222 | { 223 | *b58sz = zcount + size - j + 1; 224 | return false; 225 | } 226 | 227 | if (zcount) 228 | memset(b58, buffer[0], zcount); 229 | for (i = zcount; j < size; ++i, ++j) 230 | b58[i] = buffer[buf[j]]; 231 | b58[i] = '\0'; 232 | *b58sz = i + 1; 233 | 234 | return true; 235 | } 236 | 237 | 238 | bool b58check_enc(char *b58c, size_t *b58c_sz, uint8_t ver, const void *data, size_t datasz) 239 | { 240 | uint8_t buf[1 + datasz + 0x20]; 241 | uint8_t *hash = &buf[1 + datasz]; 242 | 243 | buf[0] = ver; 244 | memcpy(&buf[1], data, datasz); 245 | if (!my_dblsha256(hash, buf, datasz + 1)) 246 | { 247 | *b58c_sz = 0; 248 | return false; 249 | } 250 | 251 | return b58enc(b58c, b58c_sz, buf, 1 + datasz + 4); 252 | } 253 | -------------------------------------------------------------------------------- /oldbloom/oldbloom.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2019, Jyri J. Virkki 3 | * All rights reserved. 4 | * 5 | * This file is under BSD license. See LICENSE file. 6 | */ 7 | 8 | #ifndef _OLDBLOOM_H 9 | #define _OLDBLOOM_H 10 | 11 | #if defined(_WIN64) && !defined(__CYGWIN__) 12 | #include 13 | #else 14 | #endif 15 | #ifdef __cplusplus 16 | extern "C" { 17 | #endif 18 | 19 | 20 | /** *************************************************************************** 21 | * Structure to keep track of one bloom filter. Caller needs to 22 | * allocate this and pass it to the functions below. First call for 23 | * every struct must be to bloom_init(). 24 | * 25 | */ 26 | struct oldbloom 27 | { 28 | // These fields are part of the public interface of this structure. 29 | // Client code may read these values if desired. Client code MUST NOT 30 | // modify any of these. 31 | uint64_t entries; 32 | uint64_t bits; 33 | uint64_t bytes; 34 | uint8_t hashes; 35 | long double error; 36 | 37 | // Fields below are private to the implementation. These may go away or 38 | // change incompatibly at any moment. Client code MUST NOT access or rely 39 | // on these. 40 | uint8_t ready; 41 | uint8_t major; 42 | uint8_t minor; 43 | double bpe; 44 | uint8_t checksum[32]; 45 | uint8_t checksum_backup[32]; 46 | uint8_t *bf; 47 | #if defined(_WIN64) && !defined(__CYGWIN__) 48 | HANDLE mutex; 49 | #else 50 | pthread_mutex_t mutex; 51 | #endif 52 | }; 53 | /* 54 | Customs 55 | */ 56 | /* 57 | int oldbloom_loadcustom(struct oldbloom * bloom, char * filename); 58 | int oldbloom_savecustom(struct oldbloom * bloom, char * filename); 59 | */ 60 | 61 | /** *************************************************************************** 62 | * Initialize the bloom filter for use. 63 | * 64 | * The filter is initialized with a bit field and number of hash functions 65 | * according to the computations from the wikipedia entry: 66 | * http://en.wikipedia.org/wiki/Bloom_filter 67 | * 68 | * Optimal number of bits is: 69 | * bits = (entries * ln(error)) / ln(2)^2 70 | * 71 | * Optimal number of hash functions is: 72 | * hashes = bpe * ln(2) 73 | * 74 | * Parameters: 75 | * ----------- 76 | * bloom - Pointer to an allocated struct bloom (see above). 77 | * entries - The expected number of entries which will be inserted. 78 | * Must be at least 1000 (in practice, likely much larger). 79 | * error - Probability of collision (as long as entries are not 80 | * exceeded). 81 | * 82 | * Return: 83 | * ------- 84 | * 0 - on success 85 | * 1 - on failure 86 | * 87 | */ 88 | int oldbloom_init2(struct oldbloom * bloom, uint64_t entries, long double error); 89 | 90 | 91 | /** 92 | * DEPRECATED. 93 | * Kept for compatibility with libbloom v.1. To be removed in v3.0. 94 | * 95 | */ 96 | int oldbloom_init(struct oldbloom * bloom, uint64_t entries, long double error); 97 | 98 | 99 | /** *************************************************************************** 100 | * Check if the given element is in the bloom filter. Remember this may 101 | * return false positive if a collision occurred. 102 | * 103 | * Parameters: 104 | * ----------- 105 | * bloom - Pointer to an allocated struct bloom (see above). 106 | * buffer - Pointer to buffer containing element to check. 107 | * len - Size of 'buffer'. 108 | * 109 | * Return: 110 | * ------- 111 | * 0 - element is not present 112 | * 1 - element is present (or false positive due to collision) 113 | * -1 - bloom not initialized 114 | * 115 | */ 116 | int oldbloom_check(struct oldbloom * bloom, const void * buffer, int len); 117 | 118 | 119 | /** *************************************************************************** 120 | * Add the given element to the bloom filter. 121 | * The return code indicates if the element (or a collision) was already in, 122 | * so for the common check+add use case, no need to call check separately. 123 | * 124 | * Parameters: 125 | * ----------- 126 | * bloom - Pointer to an allocated struct bloom (see above). 127 | * buffer - Pointer to buffer containing element to add. 128 | * len - Size of 'buffer'. 129 | * 130 | * Return: 131 | * ------- 132 | * 0 - element was not present and was added 133 | * 1 - element (or a collision) had already been added previously 134 | * -1 - bloom not initialized 135 | * 136 | */ 137 | int oldbloom_add(struct oldbloom * bloom, const void * buffer, int len); 138 | 139 | 140 | /** *************************************************************************** 141 | * Print (to stdout) info about this bloom filter. Debugging aid. 142 | * 143 | */ 144 | void oldbloom_print(struct oldbloom * bloom); 145 | 146 | 147 | /** *************************************************************************** 148 | * Deallocate internal storage. 149 | * 150 | * Upon return, the bloom struct is no longer usable. You may call bloom_init 151 | * again on the same struct to reinitialize it again. 152 | * 153 | * Parameters: 154 | * ----------- 155 | * bloom - Pointer to an allocated struct bloom (see above). 156 | * 157 | * Return: none 158 | * 159 | */ 160 | void oldbloom_free(struct oldbloom * bloom); 161 | 162 | 163 | /** *************************************************************************** 164 | * Erase internal storage. 165 | * 166 | * Erases all elements. Upon return, the bloom struct returns to its initial 167 | * (initialized) state. 168 | * 169 | * Parameters: 170 | * ----------- 171 | * bloom - Pointer to an allocated struct bloom (see above). 172 | * 173 | * Return: 174 | * 0 - on success 175 | * 1 - on failure 176 | * 177 | */ 178 | int oldbloom_reset(struct oldbloom * bloom); 179 | 180 | 181 | /** *************************************************************************** 182 | * Save a bloom filter to a file. 183 | * 184 | * Parameters: 185 | * ----------- 186 | * bloom - Pointer to an allocated struct bloom (see above). 187 | * filename - Create (or overwrite) bloom data to this file. 188 | * 189 | * Return: 190 | * 0 - on success 191 | * 1 - on failure 192 | * 193 | */ 194 | //int oldbloom_save(struct oldbloom * bloom, char * filename); 195 | 196 | 197 | /** *************************************************************************** 198 | * Load a bloom filter from a file. 199 | * 200 | * This functions loads a file previously saved with bloom_save(). 201 | * 202 | * Parameters: 203 | * ----------- 204 | * bloom - Pointer to an allocated struct bloom (see above). 205 | * filename - Load bloom filter data from this file. 206 | * 207 | * Return: 208 | * 0 - on success 209 | * > 0 - on failure 210 | * 211 | */ 212 | //int oldbloom_load(struct oldbloom * bloom, char * filename); 213 | 214 | 215 | /** *************************************************************************** 216 | * Returns version string compiled into library. 217 | * 218 | * Return: version string 219 | * 220 | */ 221 | const char * oldbloom_version(); 222 | 223 | #ifdef __cplusplus 224 | } 225 | #endif 226 | 227 | #endif -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | default: 2 | g++ -m64 -march=native -mtune=native -mssse3 -Wall -Wextra -Wno-deprecated-copy -Ofast -ftree-vectorize -flto -c oldbloom/bloom.cpp -o oldbloom.o 3 | g++ -m64 -march=native -mtune=native -mssse3 -Wall -Wextra -Wno-deprecated-copy -Ofast -ftree-vectorize -flto -c bloom/bloom.cpp -o bloom.o 4 | gcc -m64 -march=native -mtune=native -mssse3 -Wall -Wextra -Wno-unused-parameter -Ofast -ftree-vectorize -c base58/base58.c -o base58.o 5 | gcc -m64 -march=native -mtune=native -mssse3 -Wall -Wextra -Ofast -ftree-vectorize -c rmd160/rmd160.c -o rmd160.o 6 | g++ -m64 -march=native -mtune=native -mssse3 -Wall -Wextra -Wno-deprecated-copy -Ofast -ftree-vectorize -c sha3/sha3.c -o sha3.o 7 | g++ -m64 -march=native -mtune=native -mssse3 -Wall -Wextra -Wno-deprecated-copy -Ofast -ftree-vectorize -c sha3/keccak.c -o keccak.o 8 | gcc -m64 -march=native -mtune=native -mssse3 -Wall -Wextra -Ofast -ftree-vectorize -c xxhash/xxhash.c -o xxhash.o 9 | g++ -m64 -march=native -mtune=native -mssse3 -Wall -Wextra -Wno-deprecated-copy -Ofast -ftree-vectorize -c util.c -o util.o 10 | g++ -m64 -march=native -mtune=native -mssse3 -Wall -Wextra -Wno-deprecated-copy -Ofast -ftree-vectorize -c secp256k1/Int.cpp -o Int.o 11 | g++ -m64 -march=native -mtune=native -mssse3 -Wall -Wextra -Wno-deprecated-copy -Ofast -ftree-vectorize -c secp256k1/Point.cpp -o Point.o 12 | g++ -m64 -march=native -mtune=native -mssse3 -Wall -Wextra -Wno-deprecated-copy -Ofast -ftree-vectorize -c secp256k1/SECP256K1.cpp -o SECP256K1.o 13 | g++ -m64 -march=native -mtune=native -mssse3 -Wall -Wextra -Wno-deprecated-copy -Ofast -ftree-vectorize -c secp256k1/IntMod.cpp -o IntMod.o 14 | g++ -m64 -march=native -mtune=native -mssse3 -Wall -Wextra -Wno-deprecated-copy -Ofast -ftree-vectorize -flto -c secp256k1/Random.cpp -o Random.o 15 | g++ -m64 -march=native -mtune=native -mssse3 -Wall -Wextra -Wno-deprecated-copy -Ofast -ftree-vectorize -flto -c secp256k1/IntGroup.cpp -o IntGroup.o 16 | g++ -m64 -march=native -mtune=native -mssse3 -Wall -Wextra -Wno-deprecated-copy -Ofast -o hash/ripemd160.o -ftree-vectorize -flto -c hash/ripemd160.cpp 17 | g++ -m64 -march=native -mtune=native -mssse3 -Wall -Wextra -Wno-deprecated-copy -Ofast -o hash/sha256.o -ftree-vectorize -flto -c hash/sha256.cpp 18 | g++ -m64 -march=native -mtune=native -mssse3 -Wall -Wextra -Wno-deprecated-copy -Ofast -o hash/ripemd160_sse.o -ftree-vectorize -flto -c hash/ripemd160_sse.cpp 19 | g++ -m64 -march=native -mtune=native -mssse3 -Wall -Wextra -Wno-deprecated-copy -Ofast -o hash/sha256_sse.o -ftree-vectorize -flto -c hash/sha256_sse.cpp 20 | g++ -m64 -march=native -mtune=native -mssse3 -Wall -Wextra -Wno-deprecated-copy -Ofast -ftree-vectorize -o keyhunt keyhunt.cpp base58.o rmd160.o hash/ripemd160.o hash/ripemd160_sse.o hash/sha256.o hash/sha256_sse.o bloom.o oldbloom.o xxhash.o util.o Int.o Point.o SECP256K1.o IntMod.o Random.o IntGroup.o sha3.o keccak.o -lm -lpthread 21 | rm -r *.o 22 | clean: 23 | rm keyhunt 24 | legacy: 25 | g++ -march=native -mtune=native -Wall -Wextra -Ofast -ftree-vectorize -flto -c oldbloom/bloom.cpp -o oldbloom.o 26 | g++ -march=native -mtune=native -Wall -Wextra -Ofast -ftree-vectorize -flto -c bloom/bloom.cpp -o bloom.o 27 | gcc -march=native -mtune=native -Wno-unused-result -Ofast -ftree-vectorize -c base58/base58.c -o base58.o 28 | gcc -march=native -mtune=native -Wall -Wextra -Ofast -ftree-vectorize -c xxhash/xxhash.c -o xxhash.o 29 | g++ -march=native -mtune=native -Wall -Wextra -Ofast -ftree-vectorize -c util.c -o util.o 30 | g++ -march=native -mtune=native -Wall -Wextra -Ofast -ftree-vectorize -c sha3/sha3.c -o sha3.o 31 | g++ -march=native -mtune=native -Wall -Wextra -Ofast -ftree-vectorize -c sha3/keccak.c -o keccak.o 32 | g++ -march=native -mtune=native -Wall -Wextra -Ofast -ftree-vectorize -c hashing.c -o hashing.o 33 | g++ -march=native -mtune=native -Wall -Wextra -Ofast -ftree-vectorize -c gmp256k1/Int.cpp -o Int.o 34 | g++ -march=native -mtune=native -Wall -Wextra -Ofast -ftree-vectorize -c gmp256k1/Point.cpp -o Point.o 35 | g++ -march=native -mtune=native -Wall -Wextra -Ofast -ftree-vectorize -c gmp256k1/GMP256K1.cpp -o GMP256K1.o 36 | g++ -march=native -mtune=native -Wall -Wextra -Ofast -ftree-vectorize -c gmp256k1/IntMod.cpp -o IntMod.o 37 | g++ -march=native -mtune=native -Wall -Wextra -Ofast -ftree-vectorize -flto -c gmp256k1/Random.cpp -o Random.o 38 | g++ -march=native -mtune=native -Wall -Wextra -Ofast -ftree-vectorize -flto -c gmp256k1/IntGroup.cpp -o IntGroup.o 39 | g++ -march=native -mtune=native -Wall -Wextra -Ofast -ftree-vectorize -o keyhunt keyhunt_legacy.cpp base58.o bloom.o oldbloom.o xxhash.o util.o Int.o Point.o GMP256K1.o IntMod.o IntGroup.o Random.o hashing.o sha3.o keccak.o -lm -lpthread -lcrypto -lgmp 40 | rm -r *.o 41 | bsgsd: 42 | g++ -m64 -march=native -mtune=native -mssse3 -Wall -Wextra -Wno-deprecated-copy -Ofast -ftree-vectorize -flto -c oldbloom/bloom.cpp -o oldbloom.o 43 | g++ -m64 -march=native -mtune=native -mssse3 -Wall -Wextra -Wno-deprecated-copy -Ofast -ftree-vectorize -flto -c bloom/bloom.cpp -o bloom.o 44 | gcc -m64 -march=native -mtune=native -mssse3 -Wall -Wextra -Wno-unused-parameter -Ofast -ftree-vectorize -c base58/base58.c -o base58.o 45 | gcc -m64 -march=native -mtune=native -mssse3 -Wall -Wextra -Ofast -ftree-vectorize -c rmd160/rmd160.c -o rmd160.o 46 | g++ -m64 -march=native -mtune=native -mssse3 -Wall -Wextra -Wno-deprecated-copy -Ofast -ftree-vectorize -c sha3/sha3.c -o sha3.o 47 | g++ -m64 -march=native -mtune=native -mssse3 -Wall -Wextra -Wno-deprecated-copy -Ofast -ftree-vectorize -c sha3/keccak.c -o keccak.o 48 | gcc -m64 -march=native -mtune=native -mssse3 -Wall -Wextra -Ofast -ftree-vectorize -c xxhash/xxhash.c -o xxhash.o 49 | g++ -m64 -march=native -mtune=native -mssse3 -Wall -Wextra -Wno-deprecated-copy -Ofast -ftree-vectorize -c util.c -o util.o 50 | g++ -m64 -march=native -mtune=native -mssse3 -Wall -Wextra -Wno-deprecated-copy -Ofast -ftree-vectorize -c secp256k1/Int.cpp -o Int.o 51 | g++ -m64 -march=native -mtune=native -mssse3 -Wall -Wextra -Wno-deprecated-copy -Ofast -ftree-vectorize -c secp256k1/Point.cpp -o Point.o 52 | g++ -m64 -march=native -mtune=native -mssse3 -Wall -Wextra -Wno-deprecated-copy -Ofast -ftree-vectorize -c secp256k1/SECP256K1.cpp -o SECP256K1.o 53 | g++ -m64 -march=native -mtune=native -mssse3 -Wall -Wextra -Wno-deprecated-copy -Ofast -ftree-vectorize -c secp256k1/IntMod.cpp -o IntMod.o 54 | g++ -m64 -march=native -mtune=native -mssse3 -Wall -Wextra -Wno-deprecated-copy -Ofast -ftree-vectorize -flto -c secp256k1/Random.cpp -o Random.o 55 | g++ -m64 -march=native -mtune=native -mssse3 -Wall -Wextra -Wno-deprecated-copy -Ofast -ftree-vectorize -flto -c secp256k1/IntGroup.cpp -o IntGroup.o 56 | g++ -m64 -march=native -mtune=native -mssse3 -Wall -Wextra -Wno-deprecated-copy -Ofast -o hash/ripemd160.o -ftree-vectorize -flto -c hash/ripemd160.cpp 57 | g++ -m64 -march=native -mtune=native -mssse3 -Wall -Wextra -Wno-deprecated-copy -Ofast -o hash/sha256.o -ftree-vectorize -flto -c hash/sha256.cpp 58 | g++ -m64 -march=native -mtune=native -mssse3 -Wall -Wextra -Wno-deprecated-copy -Ofast -o hash/ripemd160_sse.o -ftree-vectorize -flto -c hash/ripemd160_sse.cpp 59 | g++ -m64 -march=native -mtune=native -mssse3 -Wall -Wextra -Wno-deprecated-copy -Ofast -o hash/sha256_sse.o -ftree-vectorize -flto -c hash/sha256_sse.cpp 60 | g++ -m64 -march=native -mtune=native -mssse3 -Wall -Wextra -Wno-deprecated-copy -Ofast -ftree-vectorize -o bsgsd bsgsd.cpp base58.o rmd160.o hash/ripemd160.o hash/ripemd160_sse.o hash/sha256.o hash/sha256_sse.o bloom.o oldbloom.o xxhash.o util.o Int.o Point.o SECP256K1.o IntMod.o Random.o IntGroup.o sha3.o keccak.o -lm -lpthread 61 | rm -r *.o 62 | -------------------------------------------------------------------------------- /gmp256k1/Int.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Keyhunt distribution (https://github.com/albertobsd/keyhunt). 3 | * Copyright (c) 2023 albertobsd. 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 | // Big integer class (libgmp) 19 | 20 | #include "Int.h" 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #define U64STRINGSIZE 30 27 | 28 | Int::Int() { 29 | mpz_init_set_ui(num,0); 30 | } 31 | 32 | Int::Int(const int32_t i32) { 33 | mpz_init_set_si(num,i32); 34 | } 35 | 36 | Int::Int(const uint32_t u32) { 37 | mpz_init_set_ui(num,u32); 38 | } 39 | 40 | Int::Int(const Int *other) { 41 | mpz_init_set(num,other->num); 42 | } 43 | 44 | Int::Int(const char *str) { 45 | mpz_init_set_str(num,str,0); 46 | } 47 | 48 | Int::Int(const uint64_t u64) { 49 | char my_str_value[U64STRINGSIZE]; // 30 digits + null terminator 50 | snprintf(my_str_value,U64STRINGSIZE, "%lu", u64); 51 | mpz_init_set_str(num,my_str_value,0); 52 | } 53 | 54 | Int::Int(const int64_t i64) { 55 | char my_str_value[U64STRINGSIZE]; // 30 digits + null terminator 56 | snprintf(my_str_value,U64STRINGSIZE,"%li", i64); 57 | mpz_init_set_str(num,my_str_value,0); 58 | } 59 | 60 | Int::Int(const Int &value) { 61 | mpz_init_set(num,value.num); 62 | } 63 | 64 | 65 | void Int::Add(const uint64_t u64) { 66 | mpz_t value; 67 | char my_str_value[U64STRINGSIZE]; // 30 digits + null terminator 68 | snprintf(my_str_value,U64STRINGSIZE,"%lu", u64); 69 | mpz_init_set_str(value,my_str_value,0); 70 | mpz_add(num,num,value); 71 | mpz_clear(value); 72 | } 73 | 74 | void Int::Add(const uint32_t u32) { 75 | mpz_add_ui(num,num,u32); 76 | } 77 | 78 | void Int::Add(const Int *a) { 79 | mpz_add(num,num,a->num); 80 | } 81 | 82 | void Int::Add(const Int *a,const Int *b) { 83 | mpz_add(num,a->num,b->num); 84 | } 85 | 86 | void Int::Sub(const uint32_t u32) { 87 | mpz_sub_ui(num,num,u32); 88 | } 89 | 90 | void Int::Sub(const uint64_t u64) { 91 | mpz_t value; 92 | char my_str_value[U64STRINGSIZE]; // 30 digits + null terminator 93 | snprintf(my_str_value,U64STRINGSIZE,"%lu", u64); 94 | mpz_init_set_str(value,my_str_value,0); 95 | mpz_sub(num,num,value); 96 | mpz_clear(value); 97 | } 98 | 99 | void Int::Sub(Int *a) { 100 | mpz_sub(num,num,a->num); 101 | } 102 | 103 | void Int::Sub(Int *a, Int *b) { 104 | mpz_sub(num,a->num,b->num); 105 | } 106 | 107 | void Int::Mult(Int *a) { 108 | mpz_mul(num,num,a->num); 109 | } 110 | 111 | void Int::Mult(uint64_t u64) { 112 | mpz_t value; 113 | char my_str_value[U64STRINGSIZE]; // 30 digits + null terminator 114 | snprintf(my_str_value,U64STRINGSIZE,"%lu", u64); 115 | mpz_init_set_str(value,my_str_value,0); 116 | mpz_mul(num,num,value); 117 | mpz_clear(value); 118 | } 119 | 120 | void Int::IMult(int64_t i64) { 121 | mpz_t value; 122 | char my_str_value[U64STRINGSIZE]; // 30 digits + null terminator 123 | snprintf(my_str_value,U64STRINGSIZE,"%li", i64); 124 | mpz_init_set_str(value,my_str_value,0); 125 | mpz_mul(num,num,value); 126 | mpz_clear(value); 127 | } 128 | 129 | void Int::Neg() { 130 | mpz_neg(num,num); 131 | } 132 | 133 | void Int::Abs() { 134 | mpz_abs(num,num); 135 | } 136 | 137 | bool Int::IsGreater(Int *a) { 138 | if(mpz_cmp(num,a->num) > 0) 139 | return true; 140 | return false; 141 | } 142 | 143 | bool Int::IsGreaterOrEqual(Int *a) { 144 | if(mpz_cmp(num,a->num) >= 0) 145 | return true; 146 | return false; 147 | } 148 | 149 | bool Int::IsLowerOrEqual(Int *a) { 150 | if(mpz_cmp(num,a->num) <= 0) 151 | return true; 152 | return false; 153 | } 154 | 155 | bool Int::IsLower(Int *a) { 156 | if(mpz_cmp(num,a->num) < 0) 157 | return true; 158 | return false; 159 | } 160 | 161 | bool Int::IsEqual(Int *a) { 162 | if(mpz_cmp(num,a->num) == 0) 163 | return true; 164 | return false; 165 | } 166 | 167 | bool Int::IsZero() { 168 | if(mpz_cmp_ui(num,0) == 0) 169 | return true; 170 | return false; 171 | } 172 | 173 | bool Int::IsOne() { 174 | if(mpz_cmp_ui(num,1) == 0) 175 | return true; 176 | return false; 177 | } 178 | 179 | bool Int::IsPositive() { 180 | if(mpz_cmp_ui(num,0) >= 0) 181 | return true; 182 | return false; 183 | } 184 | 185 | bool Int::IsNegative() { 186 | if(mpz_cmp_ui(num,0) < 0) 187 | return true; 188 | return false; 189 | } 190 | 191 | bool Int::IsEven() { 192 | if(mpz_tstbit(num,0) == 0) 193 | return true; 194 | return false; 195 | } 196 | 197 | bool Int::IsOdd() { 198 | if(mpz_tstbit(num,0) == 1) 199 | return true; 200 | return false; 201 | } 202 | 203 | int Int::GetSize() { 204 | int r = mpz_sizeinbase(num,2); 205 | if(r % 8 == 0) 206 | return (int)(r/8); 207 | else 208 | return (int)(r/8) + 1; 209 | } 210 | 211 | int Int::GetBitLength() { 212 | return mpz_sizeinbase(num,2); 213 | } 214 | 215 | uint64_t Int::GetInt64() { 216 | char *temp =NULL; 217 | uint64_t r; 218 | temp = mpz_get_str(NULL,10,num); 219 | r = strtoull(temp,NULL,10); 220 | free(temp); 221 | return r; 222 | } 223 | 224 | uint32_t Int::GetInt32() { 225 | return mpz_get_ui(num); 226 | } 227 | 228 | int Int::GetBit(uint32_t n) { 229 | return mpz_tstbit(num,n); 230 | } 231 | 232 | void Int::SetBit(uint32_t n) { 233 | mpz_setbit(num,n); 234 | } 235 | 236 | void Int::ClearBit(uint32_t n) { 237 | mpz_clrbit(num,n); 238 | } 239 | 240 | 241 | void Int::Get32Bytes(unsigned char *buff) { 242 | size_t count, size = this->GetSize(); 243 | memset(buff, 0, 32); 244 | mpz_export(buff + 32 - size, &count, 0, 1, 0, 0, num); 245 | } 246 | 247 | void Int::Set32Bytes(unsigned char *buff) { 248 | mpz_import(num,32,0,1,0,0,buff); 249 | } 250 | 251 | unsigned char Int::GetByte(int n) { 252 | unsigned char buffer[32]; 253 | size_t count, size = this->GetSize(); 254 | memset(buffer, 0, 32); 255 | mpz_export(buffer + 32 - size, &count, 0, 1, 0, 0, num); 256 | return buffer[n]; 257 | } 258 | 259 | char* Int::GetBase2() { 260 | return mpz_get_str(NULL,2,num); 261 | } 262 | 263 | char* Int::GetBase10() { 264 | return mpz_get_str(NULL,10,num); 265 | } 266 | 267 | char* Int::GetBase16() { 268 | return mpz_get_str(NULL,16,num); 269 | } 270 | 271 | void Int::SetInt64(uint64_t value) { 272 | char my_str_value[U64STRINGSIZE]; // 30 digits + null terminator 273 | snprintf(my_str_value, U64STRINGSIZE, "%lu", value); 274 | mpz_set_str(num,my_str_value,0); 275 | } 276 | 277 | void Int::SetInt32(const uint32_t value) { 278 | mpz_set_ui(num,value); 279 | } 280 | 281 | void Int::Set(const Int* other) { 282 | mpz_set(num,other->num); 283 | } 284 | 285 | void Int::Set(const char *str) { 286 | mpz_set_str(num,str,0); 287 | } 288 | 289 | void Int::SetBase10(const char *str) { 290 | mpz_set_str(num,str,10); 291 | } 292 | 293 | void Int::SetBase16(const char *str) { 294 | mpz_set_str(num,str,16); 295 | } 296 | 297 | Int::~Int() { 298 | mpz_clear(num); 299 | } 300 | 301 | // Copy assignment operator 302 | Int& Int::operator=(const Int& other) { 303 | // Check for self-assignment 304 | if (this == &other) { 305 | return *this; 306 | } 307 | 308 | // Assign the values from 'other' to the current object 309 | mpz_set(num,other.num); 310 | 311 | // Return the current object 312 | return *this; 313 | } 314 | 315 | void Int::AddOne() { 316 | mpz_add_ui(num,num,1); 317 | } 318 | 319 | void Int::ShiftL(uint32_t n) { 320 | mpz_mul_2exp(num,num,n); 321 | 322 | } 323 | 324 | void Int::Div(Int *a,Int *mod) { 325 | if(mpz_cmp(num,a->num) < 0) { 326 | if(mod) mpz_set(mod->num,num); 327 | CLEAR(); 328 | return; 329 | } 330 | if(mpz_cmp_ui(a->num,0) == 0) { 331 | printf("Divide by 0!\n"); 332 | return; 333 | } 334 | 335 | if(mpz_cmp(num,a->num) == 0) { 336 | if(mod) mod->CLEAR(); 337 | mpz_set_ui(num,1); 338 | return; 339 | } 340 | if(mod) { 341 | mpz_fdiv_qr (num, mod->num, num, a->num); 342 | } 343 | else { 344 | mpz_fdiv_q(num,num,a->num); 345 | } 346 | } 347 | 348 | void Int::CLEAR() { 349 | mpz_set_ui(num,0); 350 | } -------------------------------------------------------------------------------- /bloom/bloom.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2019, Jyri J. Virkki 3 | * All rights reserved. 4 | * 5 | * This file is under BSD license. See LICENSE file. 6 | */ 7 | 8 | /* 9 | * Refer to bloom.h for documentation on the public interfaces. 10 | */ 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #include "bloom.h" 26 | #include "../xxhash/xxhash.h" 27 | 28 | #define MAKESTRING(n) STRING(n) 29 | #define STRING(n) #n 30 | #define BLOOM_MAGIC "libbloom2" 31 | #define BLOOM_VERSION_MAJOR 2 32 | #define BLOOM_VERSION_MINOR 201 33 | 34 | inline static int test_bit_set_bit(uint8_t *bf, uint64_t bit, int set_bit) 35 | { 36 | uint64_t byte = bit >> 3; 37 | uint8_t c = bf[byte]; // expensive memory access 38 | uint8_t mask = 1 << (bit % 8); 39 | if (c & mask) { 40 | return 1; 41 | } else { 42 | if (set_bit) { 43 | bf[byte] = c | mask; 44 | } 45 | return 0; 46 | } 47 | } 48 | 49 | inline static int test_bit(uint8_t *bf, uint64_t bit) 50 | { 51 | uint64_t byte = bit >> 3; 52 | uint8_t c = bf[byte]; // expensive memory access 53 | uint8_t mask = 1 << (bit % 8); 54 | if (c & mask) { 55 | return 1; 56 | } else { 57 | return 0; 58 | } 59 | } 60 | 61 | static int bloom_check_add(struct bloom * bloom, const void * buffer, int len, int add) 62 | { 63 | if (bloom->ready == 0) { 64 | printf("bloom at %p not initialized!\n", (void *)bloom); 65 | return -1; 66 | } 67 | uint8_t hits = 0; 68 | uint64_t a = XXH64(buffer, len, 0x59f2815b16f81798); 69 | uint64_t b = XXH64(buffer, len, a); 70 | uint64_t x; 71 | uint8_t i; 72 | for (i = 0; i < bloom->hashes; i++) { 73 | x = (a + b*i) % bloom->bits; 74 | if (test_bit_set_bit(bloom->bf, x, add)) { 75 | hits++; 76 | } else if (!add) { 77 | // Don't care about the presence of all the bits. Just our own. 78 | return 0; 79 | } 80 | } 81 | if (hits == bloom->hashes) { 82 | return 1; // 1 == element already in (or collision) 83 | } 84 | return 0; 85 | } 86 | 87 | // DEPRECATED - Please migrate to bloom_init2. 88 | int bloom_init(struct bloom * bloom, uint64_t entries, long double error) 89 | { 90 | return bloom_init2(bloom, entries, error); 91 | } 92 | 93 | int bloom_init2(struct bloom * bloom, uint64_t entries, long double error) 94 | { 95 | memset(bloom, 0, sizeof(struct bloom)); 96 | if (entries < 1000 || error <= 0 || error >= 1) { 97 | return 1; 98 | } 99 | bloom->entries = entries; 100 | bloom->error = error; 101 | 102 | long double num = -log(bloom->error); 103 | long double denom = 0.480453013918201; // ln(2)^2 104 | bloom->bpe = (num / denom); 105 | 106 | long double dentries = (long double)entries; 107 | long double allbits = dentries * bloom->bpe; 108 | bloom->bits = (uint64_t)allbits; 109 | 110 | bloom->bytes = (uint64_t) bloom->bits / 8; 111 | if (bloom->bits % 8) { 112 | bloom->bytes +=1; 113 | } 114 | 115 | bloom->hashes = (uint8_t)ceil(0.693147180559945 * bloom->bpe); // ln(2) 116 | 117 | bloom->bf = (uint8_t *)calloc(bloom->bytes, sizeof(uint8_t)); 118 | if (bloom->bf == NULL) { // LCOV_EXCL_START 119 | return 1; 120 | } // LCOV_EXCL_STOP 121 | 122 | bloom->ready = 1; 123 | bloom->major = BLOOM_VERSION_MAJOR; 124 | bloom->minor = BLOOM_VERSION_MINOR; 125 | return 0; 126 | } 127 | 128 | int bloom_check(struct bloom * bloom, const void * buffer, int len) 129 | { 130 | if (bloom->ready == 0) { 131 | printf("bloom at %p not initialized!\n", (void *)bloom); 132 | return -1; 133 | } 134 | uint8_t hits = 0; 135 | uint64_t a = XXH64(buffer, len, 0x59f2815b16f81798); 136 | uint64_t b = XXH64(buffer, len, a); 137 | uint64_t x; 138 | uint8_t i; 139 | for (i = 0; i < bloom->hashes; i++) { 140 | x = (a + b*i) % bloom->bits; 141 | if (test_bit(bloom->bf, x)) { 142 | hits++; 143 | } else { 144 | return 0; 145 | } 146 | } 147 | if (hits == bloom->hashes) { 148 | return 1; // 1 == element already in (or collision) 149 | } 150 | return 0; 151 | } 152 | 153 | 154 | int bloom_add(struct bloom * bloom, const void * buffer, int len) 155 | { 156 | return bloom_check_add(bloom, buffer, len, 1); 157 | } 158 | 159 | void bloom_print(struct bloom * bloom) 160 | { 161 | printf("bloom at %p\n", (void *)bloom); 162 | if (!bloom->ready) { printf(" *** NOT READY ***\n"); } 163 | printf(" ->version = %d.%d\n", bloom->major, bloom->minor); 164 | printf(" ->entries = %" PRIu64 "\n", bloom->entries); 165 | printf(" ->error = %Lf\n", bloom->error); 166 | printf(" ->bits = %" PRIu64 "\n", bloom->bits); 167 | printf(" ->bits per elem = %f\n", bloom->bpe); 168 | printf(" ->bytes = %" PRIu64 "\n", bloom->bytes); 169 | unsigned int KB = bloom->bytes / 1024; 170 | unsigned int MB = KB / 1024; 171 | printf(" (%u KB, %u MB)\n", KB, MB); 172 | printf(" ->hash functions = %d\n", bloom->hashes); 173 | } 174 | 175 | void bloom_free(struct bloom * bloom) 176 | { 177 | if (bloom->ready) { 178 | free(bloom->bf); 179 | } 180 | bloom->ready = 0; 181 | } 182 | 183 | int bloom_reset(struct bloom * bloom) 184 | { 185 | if (!bloom->ready) return 1; 186 | memset(bloom->bf, 0, bloom->bytes); 187 | return 0; 188 | } 189 | /* 190 | int bloom_save(struct bloom * bloom, char * filename) 191 | { 192 | if (filename == NULL || filename[0] == 0) { 193 | return 1; 194 | } 195 | 196 | int fd = open(filename, O_WRONLY | O_CREAT, 0644); 197 | if (fd < 0) { 198 | return 1; 199 | } 200 | 201 | ssize_t out = write(fd, BLOOM_MAGIC, strlen(BLOOM_MAGIC)); 202 | if (out != strlen(BLOOM_MAGIC)) { goto save_error; } // LCOV_EXCL_LINE 203 | 204 | uint16_t size = sizeof(struct bloom); 205 | out = write(fd, &size, sizeof(uint16_t)); 206 | if (out != sizeof(uint16_t)) { goto save_error; } // LCOV_EXCL_LINE 207 | 208 | out = write(fd, bloom, sizeof(struct bloom)); 209 | if (out != sizeof(struct bloom)) { goto save_error; } // LCOV_EXCL_LINE 210 | 211 | out = write(fd, bloom->bf, bloom->bytes); 212 | if (out != bloom->bytes) { goto save_error; } // LCOV_EXCL_LINE 213 | 214 | close(fd); 215 | return 0; 216 | // LCOV_EXCL_START 217 | save_error: 218 | close(fd); 219 | return 1; 220 | // LCOV_EXCL_STOP 221 | } 222 | 223 | 224 | int bloom_load(struct bloom * bloom, char * filename) 225 | { 226 | int rv = 0; 227 | 228 | if (filename == NULL || filename[0] == 0) { return 1; } 229 | if (bloom == NULL) { return 2; } 230 | 231 | memset(bloom, 0, sizeof(struct bloom)); 232 | 233 | int fd = open(filename, O_RDONLY); 234 | if (fd < 0) { return 3; } 235 | 236 | char line[30]; 237 | memset(line, 0, 30); 238 | ssize_t in = read(fd, line, strlen(BLOOM_MAGIC)); 239 | 240 | if (in != strlen(BLOOM_MAGIC)) { 241 | rv = 4; 242 | goto load_error; 243 | } 244 | 245 | if (strncmp(line, BLOOM_MAGIC, strlen(BLOOM_MAGIC))) { 246 | rv = 5; 247 | goto load_error; 248 | } 249 | 250 | uint16_t size; 251 | in = read(fd, &size, sizeof(uint16_t)); 252 | if (in != sizeof(uint16_t)) { 253 | rv = 6; 254 | goto load_error; 255 | } 256 | 257 | if (size != sizeof(struct bloom)) { 258 | rv = 7; 259 | goto load_error; 260 | } 261 | 262 | in = read(fd, bloom, sizeof(struct bloom)); 263 | if (in != sizeof(struct bloom)) { 264 | rv = 8; 265 | goto load_error; 266 | } 267 | 268 | bloom->bf = NULL; 269 | if (bloom->major != BLOOM_VERSION_MAJOR) { 270 | rv = 9; 271 | goto load_error; 272 | } 273 | 274 | bloom->bf = (unsigned char *)malloc(bloom->bytes); 275 | if (bloom->bf == NULL) { rv = 10; goto load_error; } // LCOV_EXCL_LINE 276 | 277 | in = read(fd, bloom->bf, bloom->bytes); 278 | if (in != bloom->bytes) { 279 | rv = 11; 280 | free(bloom->bf); 281 | bloom->bf = NULL; 282 | goto load_error; 283 | } 284 | 285 | close(fd); 286 | return rv; 287 | 288 | load_error: 289 | close(fd); 290 | bloom->ready = 0; 291 | return rv; 292 | } 293 | */ 294 | 295 | const char * bloom_version() 296 | { 297 | return MAKESTRING(BLOOM_VERSION); 298 | } 299 | -------------------------------------------------------------------------------- /oldbloom/bloom.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2019, Jyri J. Virkki 3 | * All rights reserved. 4 | * 5 | * This file is under BSD license. See LICENSE file. 6 | */ 7 | 8 | /* 9 | * Refer to bloom.h for documentation on the public interfaces. 10 | */ 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #include "oldbloom.h" 26 | #include "../xxhash/xxhash.h" 27 | 28 | #define MAKESTRING(n) STRING(n) 29 | #define STRING(n) #n 30 | #define BLOOM_MAGIC "libbloom2" 31 | #define BLOOM_VERSION_MAJOR 2 32 | #define BLOOM_VERSION_MINOR 200 33 | 34 | inline static int oldtest_bit_set_bit(uint8_t *bf, uint64_t bit, int set_bit) 35 | { 36 | uint64_t byte = bit >> 3; 37 | uint8_t c = bf[byte]; // expensive memory access 38 | uint8_t mask = 1 << (bit % 8); 39 | if (c & mask) { 40 | return 1; 41 | } else { 42 | if (set_bit) { 43 | bf[byte] = c | mask; 44 | } 45 | return 0; 46 | } 47 | } 48 | 49 | inline static int oldtest_bit(uint8_t *bf, uint64_t bit) 50 | { 51 | uint64_t byte = bit >> 3; 52 | uint8_t c = bf[byte]; // expensive memory access 53 | uint8_t mask = 1 << (bit % 8); 54 | if (c & mask) { 55 | return 1; 56 | } else { 57 | return 0; 58 | } 59 | } 60 | 61 | static int oldbloom_check_add(struct oldbloom * bloom, const void * buffer, int len, int add) 62 | { 63 | if (bloom->ready == 0) { 64 | printf("bloom at %p not initialized!\n", (void *)bloom); 65 | return -1; 66 | } 67 | uint8_t hits = 0; 68 | uint64_t a = XXH64(buffer, len, 0x59f2815b16f81798); 69 | uint64_t b = XXH64(buffer, len, a); 70 | uint64_t x; 71 | uint8_t i; 72 | int r; 73 | for (i = 0; i < bloom->hashes; i++) { 74 | x = (a + b*i) % bloom->bits; 75 | #if defined(_WIN64) && !defined(__CYGWIN__) 76 | WaitForSingleObject(bloom->mutex, INFINITE); 77 | r = oldtest_bit_set_bit(bloom->bf, x, add); 78 | ReleaseMutex(bloom->mutex); 79 | #else 80 | pthread_mutex_lock((pthread_mutex_t*)&bloom->mutex); 81 | r = oldtest_bit_set_bit(bloom->bf, x, add); 82 | pthread_mutex_unlock((pthread_mutex_t*)&bloom->mutex); 83 | #endif 84 | if (r) { 85 | hits++; 86 | } else if (!add) { 87 | // Don't care about the presence of all the bits. Just our own. 88 | return 0; 89 | } 90 | } 91 | if (hits == bloom->hashes) { 92 | return 1; // 1 == element already in (or collision) 93 | } 94 | return 0; 95 | } 96 | 97 | // DEPRECATED - Please migrate to bloom_init2. 98 | int oldbloom_init(struct oldbloom * bloom, uint64_t entries, long double error) 99 | { 100 | return oldbloom_init2(bloom, entries, error); 101 | } 102 | 103 | int oldbloom_init2(struct oldbloom * bloom, uint64_t entries, long double error) 104 | { 105 | memset(bloom, 0, sizeof(struct oldbloom)); 106 | if (entries < 1000 || error <= 0 || error >= 1) { 107 | return 1; 108 | } 109 | bloom->entries = entries; 110 | bloom->error = error; 111 | 112 | long double num = -log(bloom->error); 113 | long double denom = 0.480453013918201; // ln(2)^2 114 | bloom->bpe = (num / denom); 115 | 116 | long double dentries = (long double)entries; 117 | long double allbits = dentries * bloom->bpe; 118 | bloom->bits = (uint64_t)allbits; 119 | 120 | bloom->bytes = (uint64_t) bloom->bits / 8; 121 | if (bloom->bits % 8) { 122 | bloom->bytes +=1; 123 | } 124 | 125 | bloom->hashes = (uint8_t)ceil(0.693147180559945 * bloom->bpe); // ln(2) 126 | 127 | bloom->bf = (uint8_t *)calloc(bloom->bytes, sizeof(uint8_t)); 128 | if (bloom->bf == NULL) { // LCOV_EXCL_START 129 | return 1; 130 | } // LCOV_EXCL_STOP 131 | 132 | bloom->ready = 1; 133 | bloom->major = BLOOM_VERSION_MAJOR; 134 | bloom->minor = BLOOM_VERSION_MINOR; 135 | return 0; 136 | } 137 | 138 | int oldbloom_check(struct oldbloom * bloom, const void * buffer, int len) 139 | { 140 | if (bloom->ready == 0) { 141 | printf("bloom at %p not initialized!\n", (void *)bloom); 142 | return -1; 143 | } 144 | uint8_t hits = 0; 145 | uint64_t a = XXH64(buffer, len, 0x59f2815b16f81798); 146 | uint64_t b = XXH64(buffer, len, a); 147 | uint64_t x; 148 | uint8_t i; 149 | for (i = 0; i < bloom->hashes; i++) { 150 | x = (a + b*i) % bloom->bits; 151 | if (oldtest_bit(bloom->bf, x)) { 152 | hits++; 153 | } else { 154 | return 0; 155 | } 156 | } 157 | if (hits == bloom->hashes) { 158 | return 1; // 1 == element already in (or collision) 159 | } 160 | return 0; 161 | } 162 | 163 | 164 | int oldbloom_add(struct oldbloom * bloom, const void * buffer, int len) 165 | { 166 | return oldbloom_check_add(bloom, buffer, len, 1); 167 | } 168 | 169 | void oldbloom_print(struct oldbloom * bloom) 170 | { 171 | printf("bloom at %p\n", (void *)bloom); 172 | if (!bloom->ready) { printf(" *** NOT READY ***\n"); } 173 | printf(" ->version = %d.%d\n", bloom->major, bloom->minor); 174 | printf(" ->entries = %" PRIu64 "\n", bloom->entries); 175 | printf(" ->error = %Lf\n", bloom->error); 176 | printf(" ->bits = %" PRIu64 "\n", bloom->bits); 177 | printf(" ->bits per elem = %f\n", bloom->bpe); 178 | printf(" ->bytes = %" PRIu64 "\n", bloom->bytes); 179 | unsigned int KB = bloom->bytes / 1024; 180 | unsigned int MB = KB / 1024; 181 | printf(" (%u KB, %u MB)\n", KB, MB); 182 | printf(" ->hash functions = %d\n", bloom->hashes); 183 | } 184 | 185 | void oldbloom_free(struct oldbloom * bloom) 186 | { 187 | if (bloom->ready) { 188 | free(bloom->bf); 189 | } 190 | bloom->ready = 0; 191 | } 192 | 193 | int oldbloom_reset(struct oldbloom * bloom) 194 | { 195 | if (!bloom->ready) return 1; 196 | memset(bloom->bf, 0, bloom->bytes); 197 | return 0; 198 | } 199 | 200 | /* 201 | int oldbloom_save(struct oldbloom * bloom, char * filename) 202 | { 203 | if (filename == NULL || filename[0] == 0) { 204 | return 1; 205 | } 206 | 207 | int fd = open(filename, O_WRONLY | O_CREAT, 0644); 208 | if (fd < 0) { 209 | return 1; 210 | } 211 | 212 | ssize_t out = write(fd, BLOOM_MAGIC, strlen(BLOOM_MAGIC)); 213 | if (out != strlen(BLOOM_MAGIC)) { goto save_error; } // LCOV_EXCL_LINE 214 | 215 | uint16_t size = sizeof(struct oldbloom); 216 | out = write(fd, &size, sizeof(uint16_t)); 217 | if (out != sizeof(uint16_t)) { goto save_error; } // LCOV_EXCL_LINE 218 | 219 | out = write(fd, bloom, sizeof(struct oldbloom)); 220 | if (out != sizeof(struct oldbloom)) { goto save_error; } // LCOV_EXCL_LINE 221 | 222 | out = write(fd, bloom->bf, bloom->bytes); 223 | if (out != bloom->bytes) { goto save_error; } // LCOV_EXCL_LINE 224 | 225 | close(fd); 226 | return 0; 227 | // LCOV_EXCL_START 228 | save_error: 229 | close(fd); 230 | return 1; 231 | // LCOV_EXCL_STOP 232 | } 233 | 234 | 235 | int oldbloom_load(struct oldbloom * bloom, char * filename) 236 | { 237 | int rv = 0; 238 | 239 | if (filename == NULL || filename[0] == 0) { return 1; } 240 | if (bloom == NULL) { return 2; } 241 | 242 | memset(bloom, 0, sizeof(struct oldbloom)); 243 | 244 | int fd = open(filename, O_RDONLY); 245 | if (fd < 0) { return 3; } 246 | 247 | char line[30]; 248 | memset(line, 0, 30); 249 | ssize_t in = read(fd, line, strlen(BLOOM_MAGIC)); 250 | 251 | if (in != strlen(BLOOM_MAGIC)) { 252 | rv = 4; 253 | goto load_error; 254 | } 255 | 256 | if (strncmp(line, BLOOM_MAGIC, strlen(BLOOM_MAGIC))) { 257 | rv = 5; 258 | goto load_error; 259 | } 260 | 261 | uint16_t size; 262 | in = read(fd, &size, sizeof(uint16_t)); 263 | if (in != sizeof(uint16_t)) { 264 | rv = 6; 265 | goto load_error; 266 | } 267 | 268 | if (size != sizeof(struct oldbloom)) { 269 | rv = 7; 270 | goto load_error; 271 | } 272 | 273 | in = read(fd, bloom, sizeof(struct oldbloom)); 274 | if (in != sizeof(struct oldbloom)) { 275 | rv = 8; 276 | goto load_error; 277 | } 278 | 279 | bloom->bf = NULL; 280 | if (bloom->major != BLOOM_VERSION_MAJOR) { 281 | rv = 9; 282 | goto load_error; 283 | } 284 | 285 | bloom->bf = (unsigned char *)malloc(bloom->bytes); 286 | if (bloom->bf == NULL) { rv = 10; goto load_error; } // LCOV_EXCL_LINE 287 | 288 | in = read(fd, bloom->bf, bloom->bytes); 289 | if (in != bloom->bytes) { 290 | rv = 11; 291 | free(bloom->bf); 292 | bloom->bf = NULL; 293 | goto load_error; 294 | } 295 | 296 | close(fd); 297 | return rv; 298 | 299 | load_error: 300 | close(fd); 301 | bloom->ready = 0; 302 | return rv; 303 | } 304 | */ 305 | 306 | const char * oldbloom_version() 307 | { 308 | return MAKESTRING(BLOOM_VERSION); 309 | } 310 | -------------------------------------------------------------------------------- /secp256k1/Int.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the BSGS distribution (https://github.com/JeanLucPons/BSGS). 3 | * Copyright (c) 2020 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 | // Big integer class (Fixed size) 19 | 20 | #ifndef BIGINTH 21 | #define BIGINTH 22 | 23 | #include "Random.h" 24 | #include 25 | #include 26 | 27 | // We need 1 extra block for Knuth div algorithm , Montgomery multiplication and ModInv 28 | #define BISIZE 256 29 | 30 | #if BISIZE==256 31 | #define NB64BLOCK 5 32 | #define NB32BLOCK 10 33 | #elif BISIZE==512 34 | #define NB64BLOCK 9 35 | #define NB32BLOCK 18 36 | #else 37 | #error Unsuported size 38 | #endif 39 | 40 | class Int { 41 | 42 | public: 43 | 44 | Int(); 45 | Int(int32_t i32); 46 | Int(int64_t i64); 47 | Int(uint64_t u64); 48 | Int(Int *a); 49 | 50 | // Op 51 | void Add(uint64_t a); 52 | void Add(Int *a); 53 | void Add(Int *a,Int *b); 54 | void AddOne(); 55 | void Sub(uint64_t a); 56 | void Sub(Int *a); 57 | void Sub(Int *a, Int *b); 58 | void SubOne(); 59 | void Mult(Int *a); 60 | void Mult(uint64_t a); 61 | void IMult(int64_t a); 62 | void Mult(Int *a,uint64_t b); 63 | void IMult(Int *a, int64_t b); 64 | void Mult(Int *a,Int *b); 65 | void Div(Int *a,Int *mod = NULL); 66 | void MultModN(Int *a, Int *b, Int *n); 67 | void Neg(); 68 | void Abs(); 69 | 70 | // Right shift (signed) 71 | void ShiftR(uint32_t n); 72 | void ShiftR32Bit(); 73 | void ShiftR64Bit(); 74 | // Left shift 75 | void ShiftL(uint32_t n); 76 | void ShiftL32Bit(); 77 | void ShiftL64Bit(); 78 | 79 | // Comp 80 | bool IsGreater(Int *a); 81 | bool IsGreaterOrEqual(Int *a); 82 | bool IsLowerOrEqual(Int *a); 83 | bool IsLower(Int *a); 84 | bool IsEqual(Int *a); 85 | bool IsZero(); 86 | bool IsOne(); 87 | bool IsStrictPositive(); 88 | bool IsPositive(); 89 | bool IsNegative(); 90 | bool IsEven(); 91 | bool IsOdd(); 92 | 93 | // Modular arithmetic 94 | 95 | // Setup field 96 | // n is the field characteristic 97 | // R used in Montgomery mult (R = 2^size(n)) 98 | // R2 = R^2, R3 = R^3, R4 = R^4 99 | static void SetupField(Int *n, Int *R = NULL, Int *R2 = NULL, Int *R3 = NULL, Int *R4 = NULL); 100 | static Int *GetR(); // Return R 101 | static Int *GetR2(); // Return R2 102 | static Int *GetR3(); // Return R3 103 | static Int *GetR4(); // Return R4 104 | static Int* GetFieldCharacteristic(); // Return field characteristic 105 | 106 | void GCD(Int *a); // this <- GCD(this,a) 107 | void Mod(Int *n); // this <- this (mod n) 108 | void ModInv(); // this <- this^-1 (mod n) 109 | void MontgomeryMult(Int *a,Int *b); // this <- a*b*R^-1 (mod n) 110 | void MontgomeryMult(Int *a); // this <- this*a*R^-1 (mod n) 111 | void ModAdd(Int *a); // this <- this+a (mod n) [0 227 | #endif 228 | 229 | static void inline imm_mul(uint64_t *x, uint64_t y, uint64_t *dst) { 230 | 231 | unsigned char c = 0; 232 | uint64_t h, carry; 233 | dst[0] = _umul128(x[0], y, &h); carry = h; 234 | c = _addcarry_u64(c, _umul128(x[1], y, &h), carry, dst + 1); carry = h; 235 | c = _addcarry_u64(c, _umul128(x[2], y, &h), carry, dst + 2); carry = h; 236 | c = _addcarry_u64(c, _umul128(x[3], y, &h), carry, dst + 3); carry = h; 237 | c = _addcarry_u64(c, _umul128(x[4], y, &h), carry, dst + 4); carry = h; 238 | #if NB64BLOCK > 5 239 | c = _addcarry_u64(c, _umul128(x[5], y, &h), carry, dst + 5); carry = h; 240 | c = _addcarry_u64(c, _umul128(x[6], y, &h), carry, dst + 6); carry = h; 241 | c = _addcarry_u64(c, _umul128(x[7], y, &h), carry, dst + 7); carry = h; 242 | c = _addcarry_u64(c, _umul128(x[8], y, &h), carry, dst + 8); carry = h; 243 | #endif 244 | 245 | } 246 | 247 | static void inline imm_umul(uint64_t *x, uint64_t y, uint64_t *dst) { 248 | 249 | // Assume that x[NB64BLOCK-1] is 0 250 | unsigned char c = 0; 251 | uint64_t h, carry; 252 | dst[0] = _umul128(x[0], y, &h); carry = h; 253 | c = _addcarry_u64(c, _umul128(x[1], y, &h), carry, dst + 1); carry = h; 254 | c = _addcarry_u64(c, _umul128(x[2], y, &h), carry, dst + 2); carry = h; 255 | c = _addcarry_u64(c, _umul128(x[3], y, &h), carry, dst + 3); carry = h; 256 | #if NB64BLOCK > 5 257 | c = _addcarry_u64(c, _umul128(x[4], y, &h), carry, dst + 4); carry = h; 258 | c = _addcarry_u64(c, _umul128(x[5], y, &h), carry, dst + 5); carry = h; 259 | c = _addcarry_u64(c, _umul128(x[6], y, &h), carry, dst + 6); carry = h; 260 | c = _addcarry_u64(c, _umul128(x[7], y, &h), carry, dst + 7); carry = h; 261 | #endif 262 | _addcarry_u64(c, 0ULL, carry, dst + (NB64BLOCK - 1)); 263 | 264 | } 265 | 266 | static void inline shiftR(unsigned char n, uint64_t *d) { 267 | 268 | d[0] = __shiftright128(d[0], d[1], n); 269 | d[1] = __shiftright128(d[1], d[2], n); 270 | d[2] = __shiftright128(d[2], d[3], n); 271 | d[3] = __shiftright128(d[3], d[4], n); 272 | #if NB64BLOCK > 5 273 | d[4] = __shiftright128(d[4], d[5], n); 274 | d[5] = __shiftright128(d[5], d[6], n); 275 | d[6] = __shiftright128(d[6], d[7], n); 276 | d[7] = __shiftright128(d[7], d[8], n); 277 | #endif 278 | d[NB64BLOCK-1] = ((int64_t)d[NB64BLOCK-1]) >> n; 279 | 280 | } 281 | 282 | static void inline shiftL(unsigned char n, uint64_t *d) { 283 | 284 | #if NB64BLOCK > 5 285 | d[8] = __shiftleft128(d[7], d[8], n); 286 | d[7] = __shiftleft128(d[6], d[7], n); 287 | d[6] = __shiftleft128(d[5], d[6], n); 288 | d[5] = __shiftleft128(d[4], d[5], n); 289 | #endif 290 | d[4] = __shiftleft128(d[3], d[4], n); 291 | d[3] = __shiftleft128(d[2], d[3], n); 292 | d[2] = __shiftleft128(d[1], d[2], n); 293 | d[1] = __shiftleft128(d[0], d[1], n); 294 | d[0] = d[0] << n; 295 | 296 | } 297 | 298 | #endif // BIGINTH 299 | -------------------------------------------------------------------------------- /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 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | /// Internal RIPEMD-160 implementation. 27 | namespace _ripemd160 { 28 | 29 | /** Initialize RIPEMD-160 state. */ 30 | void inline Initialize(uint32_t* s) 31 | { 32 | s[0] = 0x67452301ul; 33 | s[1] = 0xEFCDAB89ul; 34 | s[2] = 0x98BADCFEul; 35 | s[3] = 0x10325476ul; 36 | s[4] = 0xC3D2E1F0ul; 37 | } 38 | 39 | #ifndef WIN64 40 | inline uint32_t _rotl(uint32_t x, uint8_t r) { 41 | asm("roll %1,%0" : "+r" (x) : "c" (r)); 42 | return x; 43 | } 44 | #endif 45 | 46 | #define ROL(x,n) _rotl(x,n) 47 | 48 | #define f1(x, y, z) (x ^ y ^ z) 49 | #define f2(x, y, z) ((x & y) | (~x & z)) 50 | #define f3(x, y, z) ((x | ~y) ^ z) 51 | #define f4(x, y, z) ((x & z) | (~z & y)) 52 | #define f5(x, y, z) (x ^ (y | ~z)) 53 | 54 | #define Round(a,b,c,d,e,f,x,k,r) \ 55 | a = ROL(a + f + x + k, r) + e; \ 56 | c = ROL(c, 10); 57 | 58 | #define R11(a,b,c,d,e,x,r) Round(a, b, c, d, e, f1(b, c, d), x, 0, r) 59 | #define R21(a,b,c,d,e,x,r) Round(a, b, c, d, e, f2(b, c, d), x, 0x5A827999ul, r) 60 | #define R31(a,b,c,d,e,x,r) Round(a, b, c, d, e, f3(b, c, d), x, 0x6ED9EBA1ul, r) 61 | #define R41(a,b,c,d,e,x,r) Round(a, b, c, d, e, f4(b, c, d), x, 0x8F1BBCDCul, r) 62 | #define R51(a,b,c,d,e,x,r) Round(a, b, c, d, e, f5(b, c, d), x, 0xA953FD4Eul, r) 63 | #define R12(a,b,c,d,e,x,r) Round(a, b, c, d, e, f5(b, c, d), x, 0x50A28BE6ul, r) 64 | #define R22(a,b,c,d,e,x,r) Round(a, b, c, d, e, f4(b, c, d), x, 0x5C4DD124ul, r) 65 | #define R32(a,b,c,d,e,x,r) Round(a, b, c, d, e, f3(b, c, d), x, 0x6D703EF3ul, r) 66 | #define R42(a,b,c,d,e,x,r) Round(a, b, c, d, e, f2(b, c, d), x, 0x7A6D76E9ul, r) 67 | #define R52(a,b,c,d,e,x,r) Round(a, b, c, d, e, f1(b, c, d), x, 0, r) 68 | 69 | /** Perform a RIPEMD-160 transformation, processing a 64-byte chunk. */ 70 | void Transform(uint32_t* s, const unsigned char* chunk) 71 | { 72 | uint32_t a1 = s[0], b1 = s[1], c1 = s[2], d1 = s[3], e1 = s[4]; 73 | uint32_t a2 = a1, b2 = b1, c2 = c1, d2 = d1, e2 = e1; 74 | uint32_t w[16]; 75 | memcpy(w,chunk,16*sizeof(uint32_t)); 76 | 77 | R11(a1, b1, c1, d1, e1, w[0], 11); 78 | R12(a2, b2, c2, d2, e2, w[5], 8); 79 | R11(e1, a1, b1, c1, d1, w[1], 14); 80 | R12(e2, a2, b2, c2, d2, w[14], 9); 81 | R11(d1, e1, a1, b1, c1, w[2], 15); 82 | R12(d2, e2, a2, b2, c2, w[7], 9); 83 | R11(c1, d1, e1, a1, b1, w[3], 12); 84 | R12(c2, d2, e2, a2, b2, w[0], 11); 85 | R11(b1, c1, d1, e1, a1, w[4], 5); 86 | R12(b2, c2, d2, e2, a2, w[9], 13); 87 | R11(a1, b1, c1, d1, e1, w[5], 8); 88 | R12(a2, b2, c2, d2, e2, w[2], 15); 89 | R11(e1, a1, b1, c1, d1, w[6], 7); 90 | R12(e2, a2, b2, c2, d2, w[11], 15); 91 | R11(d1, e1, a1, b1, c1, w[7], 9); 92 | R12(d2, e2, a2, b2, c2, w[4], 5); 93 | R11(c1, d1, e1, a1, b1, w[8], 11); 94 | R12(c2, d2, e2, a2, b2, w[13], 7); 95 | R11(b1, c1, d1, e1, a1, w[9], 13); 96 | R12(b2, c2, d2, e2, a2, w[6], 7); 97 | R11(a1, b1, c1, d1, e1, w[10], 14); 98 | R12(a2, b2, c2, d2, e2, w[15], 8); 99 | R11(e1, a1, b1, c1, d1, w[11], 15); 100 | R12(e2, a2, b2, c2, d2, w[8], 11); 101 | R11(d1, e1, a1, b1, c1, w[12], 6); 102 | R12(d2, e2, a2, b2, c2, w[1], 14); 103 | R11(c1, d1, e1, a1, b1, w[13], 7); 104 | R12(c2, d2, e2, a2, b2, w[10], 14); 105 | R11(b1, c1, d1, e1, a1, w[14], 9); 106 | R12(b2, c2, d2, e2, a2, w[3], 12); 107 | R11(a1, b1, c1, d1, e1, w[15], 8); 108 | R12(a2, b2, c2, d2, e2, w[12], 6); 109 | 110 | R21(e1, a1, b1, c1, d1, w[7], 7); 111 | R22(e2, a2, b2, c2, d2, w[6], 9); 112 | R21(d1, e1, a1, b1, c1, w[4], 6); 113 | R22(d2, e2, a2, b2, c2, w[11], 13); 114 | R21(c1, d1, e1, a1, b1, w[13], 8); 115 | R22(c2, d2, e2, a2, b2, w[3], 15); 116 | R21(b1, c1, d1, e1, a1, w[1], 13); 117 | R22(b2, c2, d2, e2, a2, w[7], 7); 118 | R21(a1, b1, c1, d1, e1, w[10], 11); 119 | R22(a2, b2, c2, d2, e2, w[0], 12); 120 | R21(e1, a1, b1, c1, d1, w[6], 9); 121 | R22(e2, a2, b2, c2, d2, w[13], 8); 122 | R21(d1, e1, a1, b1, c1, w[15], 7); 123 | R22(d2, e2, a2, b2, c2, w[5], 9); 124 | R21(c1, d1, e1, a1, b1, w[3], 15); 125 | R22(c2, d2, e2, a2, b2, w[10], 11); 126 | R21(b1, c1, d1, e1, a1, w[12], 7); 127 | R22(b2, c2, d2, e2, a2, w[14], 7); 128 | R21(a1, b1, c1, d1, e1, w[0], 12); 129 | R22(a2, b2, c2, d2, e2, w[15], 7); 130 | R21(e1, a1, b1, c1, d1, w[9], 15); 131 | R22(e2, a2, b2, c2, d2, w[8], 12); 132 | R21(d1, e1, a1, b1, c1, w[5], 9); 133 | R22(d2, e2, a2, b2, c2, w[12], 7); 134 | R21(c1, d1, e1, a1, b1, w[2], 11); 135 | R22(c2, d2, e2, a2, b2, w[4], 6); 136 | R21(b1, c1, d1, e1, a1, w[14], 7); 137 | R22(b2, c2, d2, e2, a2, w[9], 15); 138 | R21(a1, b1, c1, d1, e1, w[11], 13); 139 | R22(a2, b2, c2, d2, e2, w[1], 13); 140 | R21(e1, a1, b1, c1, d1, w[8], 12); 141 | R22(e2, a2, b2, c2, d2, w[2], 11); 142 | 143 | R31(d1, e1, a1, b1, c1, w[3], 11); 144 | R32(d2, e2, a2, b2, c2, w[15], 9); 145 | R31(c1, d1, e1, a1, b1, w[10], 13); 146 | R32(c2, d2, e2, a2, b2, w[5], 7); 147 | R31(b1, c1, d1, e1, a1, w[14], 6); 148 | R32(b2, c2, d2, e2, a2, w[1], 15); 149 | R31(a1, b1, c1, d1, e1, w[4], 7); 150 | R32(a2, b2, c2, d2, e2, w[3], 11); 151 | R31(e1, a1, b1, c1, d1, w[9], 14); 152 | R32(e2, a2, b2, c2, d2, w[7], 8); 153 | R31(d1, e1, a1, b1, c1, w[15], 9); 154 | R32(d2, e2, a2, b2, c2, w[14], 6); 155 | R31(c1, d1, e1, a1, b1, w[8], 13); 156 | R32(c2, d2, e2, a2, b2, w[6], 6); 157 | R31(b1, c1, d1, e1, a1, w[1], 15); 158 | R32(b2, c2, d2, e2, a2, w[9], 14); 159 | R31(a1, b1, c1, d1, e1, w[2], 14); 160 | R32(a2, b2, c2, d2, e2, w[11], 12); 161 | R31(e1, a1, b1, c1, d1, w[7], 8); 162 | R32(e2, a2, b2, c2, d2, w[8], 13); 163 | R31(d1, e1, a1, b1, c1, w[0], 13); 164 | R32(d2, e2, a2, b2, c2, w[12], 5); 165 | R31(c1, d1, e1, a1, b1, w[6], 6); 166 | R32(c2, d2, e2, a2, b2, w[2], 14); 167 | R31(b1, c1, d1, e1, a1, w[13], 5); 168 | R32(b2, c2, d2, e2, a2, w[10], 13); 169 | R31(a1, b1, c1, d1, e1, w[11], 12); 170 | R32(a2, b2, c2, d2, e2, w[0], 13); 171 | R31(e1, a1, b1, c1, d1, w[5], 7); 172 | R32(e2, a2, b2, c2, d2, w[4], 7); 173 | R31(d1, e1, a1, b1, c1, w[12], 5); 174 | R32(d2, e2, a2, b2, c2, w[13], 5); 175 | 176 | R41(c1, d1, e1, a1, b1, w[1], 11); 177 | R42(c2, d2, e2, a2, b2, w[8], 15); 178 | R41(b1, c1, d1, e1, a1, w[9], 12); 179 | R42(b2, c2, d2, e2, a2, w[6], 5); 180 | R41(a1, b1, c1, d1, e1, w[11], 14); 181 | R42(a2, b2, c2, d2, e2, w[4], 8); 182 | R41(e1, a1, b1, c1, d1, w[10], 15); 183 | R42(e2, a2, b2, c2, d2, w[1], 11); 184 | R41(d1, e1, a1, b1, c1, w[0], 14); 185 | R42(d2, e2, a2, b2, c2, w[3], 14); 186 | R41(c1, d1, e1, a1, b1, w[8], 15); 187 | R42(c2, d2, e2, a2, b2, w[11], 14); 188 | R41(b1, c1, d1, e1, a1, w[12], 9); 189 | R42(b2, c2, d2, e2, a2, w[15], 6); 190 | R41(a1, b1, c1, d1, e1, w[4], 8); 191 | R42(a2, b2, c2, d2, e2, w[0], 14); 192 | R41(e1, a1, b1, c1, d1, w[13], 9); 193 | R42(e2, a2, b2, c2, d2, w[5], 6); 194 | R41(d1, e1, a1, b1, c1, w[3], 14); 195 | R42(d2, e2, a2, b2, c2, w[12], 9); 196 | R41(c1, d1, e1, a1, b1, w[7], 5); 197 | R42(c2, d2, e2, a2, b2, w[2], 12); 198 | R41(b1, c1, d1, e1, a1, w[15], 6); 199 | R42(b2, c2, d2, e2, a2, w[13], 9); 200 | R41(a1, b1, c1, d1, e1, w[14], 8); 201 | R42(a2, b2, c2, d2, e2, w[9], 12); 202 | R41(e1, a1, b1, c1, d1, w[5], 6); 203 | R42(e2, a2, b2, c2, d2, w[7], 5); 204 | R41(d1, e1, a1, b1, c1, w[6], 5); 205 | R42(d2, e2, a2, b2, c2, w[10], 15); 206 | R41(c1, d1, e1, a1, b1, w[2], 12); 207 | R42(c2, d2, e2, a2, b2, w[14], 8); 208 | 209 | R51(b1, c1, d1, e1, a1, w[4], 9); 210 | R52(b2, c2, d2, e2, a2, w[12], 8); 211 | R51(a1, b1, c1, d1, e1, w[0], 15); 212 | R52(a2, b2, c2, d2, e2, w[15], 5); 213 | R51(e1, a1, b1, c1, d1, w[5], 5); 214 | R52(e2, a2, b2, c2, d2, w[10], 12); 215 | R51(d1, e1, a1, b1, c1, w[9], 11); 216 | R52(d2, e2, a2, b2, c2, w[4], 9); 217 | R51(c1, d1, e1, a1, b1, w[7], 6); 218 | R52(c2, d2, e2, a2, b2, w[1], 12); 219 | R51(b1, c1, d1, e1, a1, w[12], 8); 220 | R52(b2, c2, d2, e2, a2, w[5], 5); 221 | R51(a1, b1, c1, d1, e1, w[2], 13); 222 | R52(a2, b2, c2, d2, e2, w[8], 14); 223 | R51(e1, a1, b1, c1, d1, w[10], 12); 224 | R52(e2, a2, b2, c2, d2, w[7], 6); 225 | R51(d1, e1, a1, b1, c1, w[14], 5); 226 | R52(d2, e2, a2, b2, c2, w[6], 8); 227 | R51(c1, d1, e1, a1, b1, w[1], 12); 228 | R52(c2, d2, e2, a2, b2, w[2], 13); 229 | R51(b1, c1, d1, e1, a1, w[3], 13); 230 | R52(b2, c2, d2, e2, a2, w[13], 6); 231 | R51(a1, b1, c1, d1, e1, w[8], 14); 232 | R52(a2, b2, c2, d2, e2, w[14], 5); 233 | R51(e1, a1, b1, c1, d1, w[11], 11); 234 | R52(e2, a2, b2, c2, d2, w[0], 15); 235 | R51(d1, e1, a1, b1, c1, w[6], 8); 236 | R52(d2, e2, a2, b2, c2, w[3], 13); 237 | R51(c1, d1, e1, a1, b1, w[15], 5); 238 | R52(c2, d2, e2, a2, b2, w[9], 11); 239 | R51(b1, c1, d1, e1, a1, w[13], 6); 240 | R52(b2, c2, d2, e2, a2, w[11], 11); 241 | 242 | uint32_t t = s[0]; 243 | s[0] = s[1] + c1 + d2; 244 | s[1] = s[2] + d1 + e2; 245 | s[2] = s[3] + e1 + a2; 246 | s[3] = s[4] + a1 + b2; 247 | s[4] = t + b1 + c2; 248 | } 249 | 250 | } // namespace ripemd160 251 | 252 | CRIPEMD160::CRIPEMD160() : bytes(0) 253 | { 254 | _ripemd160::Initialize(s); 255 | } 256 | 257 | void CRIPEMD160::Write(const unsigned char* data, size_t len) 258 | { 259 | const unsigned char* end = data + len; 260 | size_t bufsize = bytes % 64; 261 | if (bufsize && bufsize + len >= 64) { 262 | // Fill the buffer, and process it. 263 | memcpy(buf + bufsize, data, 64 - bufsize); 264 | bytes += 64 - bufsize; 265 | data += 64 - bufsize; 266 | _ripemd160::Transform(s, buf); 267 | bufsize = 0; 268 | } 269 | while (end >= data + 64) { 270 | // Process full chunks directly from the source. 271 | _ripemd160::Transform(s, data); 272 | bytes += 64; 273 | data += 64; 274 | } 275 | if (end > data) { 276 | // Fill the buffer with what remains. 277 | memcpy(buf + bufsize, data, end - data); 278 | bytes += end - data; 279 | } 280 | } 281 | 282 | void CRIPEMD160::Finalize(unsigned char hash[20]) 283 | { 284 | static const unsigned char pad[64] = {0x80}; 285 | unsigned char sizedesc[8]; 286 | *(uint64_t *)sizedesc = bytes << 3; 287 | Write(pad, 1 + ((119 - (bytes % 64)) % 64)); 288 | Write(sizedesc, 8); 289 | memcpy(hash,s,20); 290 | } 291 | 292 | static const uint64_t sizedesc_32 = 32 << 3; 293 | static const unsigned char pad[64] = { 0x80 }; 294 | 295 | void ripemd160_32(unsigned char *input, unsigned char *digest) { 296 | 297 | uint32_t *s = (uint32_t *)digest; 298 | _ripemd160::Initialize(s); 299 | memcpy(input+32,pad,24); 300 | memcpy(input+56,&sizedesc_32,8); 301 | _ripemd160::Transform(s, input); 302 | 303 | } 304 | 305 | void ripemd160(unsigned char *input,int length,unsigned char *digest) { 306 | 307 | CRIPEMD160 cripe; 308 | cripe.Write(input,length); 309 | cripe.Finalize(digest); 310 | 311 | } 312 | 313 | std::string ripemd160_hex(unsigned char *digest) { 314 | 315 | char buf[2 * 20 + 1]; 316 | buf[2 * 20] = 0; 317 | for (int i = 0; i < 20; i++) 318 | sprintf(buf + i * 2, "%02x", (int)digest[i]); 319 | return std::string(buf); 320 | 321 | } 322 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /rmd160/rmd160.c: -------------------------------------------------------------------------------- 1 | /* 2 | * RIPEMD160.c - European RIPE Message Digest, 160 bit (RIPEMD-160) 3 | * 4 | * The algorithm is by Hans Dobbertin, Antoon Bosselaers, and Bart Preneel. 5 | * 6 | * The code below is based on the reference implementation by Bosselaers. 7 | * It is available at the time of writing from 8 | * http://www.esat.kuleuven.ac.be/~bosselae/ripemd160.html 9 | * 10 | * Hacked for use in libmd by Martin Hinner 11 | */ 12 | 13 | #include 14 | 15 | #include "rmd160.h" 16 | 17 | /* macro definitions */ 18 | 19 | /* ROL(x, n) cyclically rotates x over n bits to the left */ 20 | /* x must be of an unsigned 32 bits type and 0 <= n < 32. */ 21 | #define ROL(x, n) (((x) << (n)) | ((x) >> (32-(n)))) 22 | 23 | /* the three basic functions F(), G() and H() */ 24 | #define F(x, y, z) ((x) ^ (y) ^ (z)) 25 | #define G(x, y, z) (((x) & (y)) | (~(x) & (z))) 26 | #define H(x, y, z) (((x) | ~(y)) ^ (z)) 27 | #define I(x, y, z) (((x) & (z)) | ((y) & ~(z))) 28 | #define J(x, y, z) ((x) ^ ((y) | ~(z))) 29 | 30 | /* the eight basic operations FF() through III() */ 31 | #define FF(a, b, c, d, e, x, s) {\ 32 | (a) += F((b), (c), (d)) + (x);\ 33 | (a) = ROL((a), (s)) + (e);\ 34 | (c) = ROL((c), 10);\ 35 | } 36 | 37 | #define GG(a, b, c, d, e, x, s) {\ 38 | (a) += G((b), (c), (d)) + (x) + 0x5a827999UL;\ 39 | (a) = ROL((a), (s)) + (e);\ 40 | (c) = ROL((c), 10);\ 41 | } 42 | 43 | #define HH(a, b, c, d, e, x, s) {\ 44 | (a) += H((b), (c), (d)) + (x) + 0x6ed9eba1UL;\ 45 | (a) = ROL((a), (s)) + (e);\ 46 | (c) = ROL((c), 10);\ 47 | } 48 | 49 | #define II(a, b, c, d, e, x, s) {\ 50 | (a) += I((b), (c), (d)) + (x) + 0x8f1bbcdcUL;\ 51 | (a) = ROL((a), (s)) + (e);\ 52 | (c) = ROL((c), 10);\ 53 | } 54 | 55 | #define JJ(a, b, c, d, e, x, s) {\ 56 | (a) += J((b), (c), (d)) + (x) + 0xa953fd4eUL;\ 57 | (a) = ROL((a), (s)) + (e);\ 58 | (c) = ROL((c), 10);\ 59 | } 60 | 61 | #define FFF(a, b, c, d, e, x, s) {\ 62 | (a) += F((b), (c), (d)) + (x);\ 63 | (a) = ROL((a), (s)) + (e);\ 64 | (c) = ROL((c), 10);\ 65 | } 66 | 67 | #define GGG(a, b, c, d, e, x, s) {\ 68 | (a) += G((b), (c), (d)) + (x) + 0x7a6d76e9UL;\ 69 | (a) = ROL((a), (s)) + (e);\ 70 | (c) = ROL((c), 10);\ 71 | } 72 | 73 | #define HHH(a, b, c, d, e, x, s) {\ 74 | (a) += H((b), (c), (d)) + (x) + 0x6d703ef3UL;\ 75 | (a) = ROL((a), (s)) + (e);\ 76 | (c) = ROL((c), 10);\ 77 | } 78 | 79 | #define III(a, b, c, d, e, x, s) {\ 80 | (a) += I((b), (c), (d)) + (x) + 0x5c4dd124UL;\ 81 | (a) = ROL((a), (s)) + (e);\ 82 | (c) = ROL((c), 10);\ 83 | } 84 | 85 | #define JJJ(a, b, c, d, e, x, s) {\ 86 | (a) += J((b), (c), (d)) + (x) + 0x50a28be6UL;\ 87 | (a) = ROL((a), (s)) + (e);\ 88 | (c) = ROL((c), 10);\ 89 | } 90 | 91 | /* 92 | initializes MDbuffer to "magic constants" 93 | */ 94 | static void 95 | RMDinit (uint32_t * MDbuf) 96 | { 97 | MDbuf[0] = 0x67452301UL; 98 | MDbuf[1] = 0xefcdab89UL; 99 | MDbuf[2] = 0x98badcfeUL; 100 | MDbuf[3] = 0x10325476UL; 101 | MDbuf[4] = 0xc3d2e1f0UL; 102 | } 103 | 104 | 105 | /* 106 | the compression function. 107 | transforms MDbuf using message bytes X[0] through X[15] 108 | */ 109 | static void 110 | RMDcompress (uint32_t * MDbuf, uint32_t * X) 111 | { 112 | uint32_t aa = MDbuf[0], bb = MDbuf[1], cc = MDbuf[2], 113 | dd = MDbuf[3], ee = MDbuf[4]; 114 | uint32_t aaa = MDbuf[0], bbb = MDbuf[1], ccc = MDbuf[2], 115 | ddd = MDbuf[3], eee = MDbuf[4]; 116 | 117 | 118 | /* round 1 */ 119 | FF (aa, bb, cc, dd, ee, X[0], 11); 120 | FF (ee, aa, bb, cc, dd, X[1], 14); 121 | FF (dd, ee, aa, bb, cc, X[2], 15); 122 | FF (cc, dd, ee, aa, bb, X[3], 12); 123 | FF (bb, cc, dd, ee, aa, X[4], 5); 124 | FF (aa, bb, cc, dd, ee, X[5], 8); 125 | FF (ee, aa, bb, cc, dd, X[6], 7); 126 | FF (dd, ee, aa, bb, cc, X[7], 9); 127 | FF (cc, dd, ee, aa, bb, X[8], 11); 128 | FF (bb, cc, dd, ee, aa, X[9], 13); 129 | FF (aa, bb, cc, dd, ee, X[10], 14); 130 | FF (ee, aa, bb, cc, dd, X[11], 15); 131 | FF (dd, ee, aa, bb, cc, X[12], 6); 132 | FF (cc, dd, ee, aa, bb, X[13], 7); 133 | FF (bb, cc, dd, ee, aa, X[14], 9); 134 | FF (aa, bb, cc, dd, ee, X[15], 8); 135 | 136 | /* round 2 */ 137 | GG (ee, aa, bb, cc, dd, X[7], 7); 138 | GG (dd, ee, aa, bb, cc, X[4], 6); 139 | GG (cc, dd, ee, aa, bb, X[13], 8); 140 | GG (bb, cc, dd, ee, aa, X[1], 13); 141 | GG (aa, bb, cc, dd, ee, X[10], 11); 142 | GG (ee, aa, bb, cc, dd, X[6], 9); 143 | GG (dd, ee, aa, bb, cc, X[15], 7); 144 | GG (cc, dd, ee, aa, bb, X[3], 15); 145 | GG (bb, cc, dd, ee, aa, X[12], 7); 146 | GG (aa, bb, cc, dd, ee, X[0], 12); 147 | GG (ee, aa, bb, cc, dd, X[9], 15); 148 | GG (dd, ee, aa, bb, cc, X[5], 9); 149 | GG (cc, dd, ee, aa, bb, X[2], 11); 150 | GG (bb, cc, dd, ee, aa, X[14], 7); 151 | GG (aa, bb, cc, dd, ee, X[11], 13); 152 | GG (ee, aa, bb, cc, dd, X[8], 12); 153 | 154 | /* round 3 */ 155 | HH (dd, ee, aa, bb, cc, X[3], 11); 156 | HH (cc, dd, ee, aa, bb, X[10], 13); 157 | HH (bb, cc, dd, ee, aa, X[14], 6); 158 | HH (aa, bb, cc, dd, ee, X[4], 7); 159 | HH (ee, aa, bb, cc, dd, X[9], 14); 160 | HH (dd, ee, aa, bb, cc, X[15], 9); 161 | HH (cc, dd, ee, aa, bb, X[8], 13); 162 | HH (bb, cc, dd, ee, aa, X[1], 15); 163 | HH (aa, bb, cc, dd, ee, X[2], 14); 164 | HH (ee, aa, bb, cc, dd, X[7], 8); 165 | HH (dd, ee, aa, bb, cc, X[0], 13); 166 | HH (cc, dd, ee, aa, bb, X[6], 6); 167 | HH (bb, cc, dd, ee, aa, X[13], 5); 168 | HH (aa, bb, cc, dd, ee, X[11], 12); 169 | HH (ee, aa, bb, cc, dd, X[5], 7); 170 | HH (dd, ee, aa, bb, cc, X[12], 5); 171 | 172 | /* round 4 */ 173 | II (cc, dd, ee, aa, bb, X[1], 11); 174 | II (bb, cc, dd, ee, aa, X[9], 12); 175 | II (aa, bb, cc, dd, ee, X[11], 14); 176 | II (ee, aa, bb, cc, dd, X[10], 15); 177 | II (dd, ee, aa, bb, cc, X[0], 14); 178 | II (cc, dd, ee, aa, bb, X[8], 15); 179 | II (bb, cc, dd, ee, aa, X[12], 9); 180 | II (aa, bb, cc, dd, ee, X[4], 8); 181 | II (ee, aa, bb, cc, dd, X[13], 9); 182 | II (dd, ee, aa, bb, cc, X[3], 14); 183 | II (cc, dd, ee, aa, bb, X[7], 5); 184 | II (bb, cc, dd, ee, aa, X[15], 6); 185 | II (aa, bb, cc, dd, ee, X[14], 8); 186 | II (ee, aa, bb, cc, dd, X[5], 6); 187 | II (dd, ee, aa, bb, cc, X[6], 5); 188 | II (cc, dd, ee, aa, bb, X[2], 12); 189 | 190 | /* round 5 */ 191 | JJ (bb, cc, dd, ee, aa, X[4], 9); 192 | JJ (aa, bb, cc, dd, ee, X[0], 15); 193 | JJ (ee, aa, bb, cc, dd, X[5], 5); 194 | JJ (dd, ee, aa, bb, cc, X[9], 11); 195 | JJ (cc, dd, ee, aa, bb, X[7], 6); 196 | JJ (bb, cc, dd, ee, aa, X[12], 8); 197 | JJ (aa, bb, cc, dd, ee, X[2], 13); 198 | JJ (ee, aa, bb, cc, dd, X[10], 12); 199 | JJ (dd, ee, aa, bb, cc, X[14], 5); 200 | JJ (cc, dd, ee, aa, bb, X[1], 12); 201 | JJ (bb, cc, dd, ee, aa, X[3], 13); 202 | JJ (aa, bb, cc, dd, ee, X[8], 14); 203 | JJ (ee, aa, bb, cc, dd, X[11], 11); 204 | JJ (dd, ee, aa, bb, cc, X[6], 8); 205 | JJ (cc, dd, ee, aa, bb, X[15], 5); 206 | JJ (bb, cc, dd, ee, aa, X[13], 6); 207 | 208 | /* parallel round 1 */ 209 | JJJ (aaa, bbb, ccc, ddd, eee, X[5], 8); 210 | JJJ (eee, aaa, bbb, ccc, ddd, X[14], 9); 211 | JJJ (ddd, eee, aaa, bbb, ccc, X[7], 9); 212 | JJJ (ccc, ddd, eee, aaa, bbb, X[0], 11); 213 | JJJ (bbb, ccc, ddd, eee, aaa, X[9], 13); 214 | JJJ (aaa, bbb, ccc, ddd, eee, X[2], 15); 215 | JJJ (eee, aaa, bbb, ccc, ddd, X[11], 15); 216 | JJJ (ddd, eee, aaa, bbb, ccc, X[4], 5); 217 | JJJ (ccc, ddd, eee, aaa, bbb, X[13], 7); 218 | JJJ (bbb, ccc, ddd, eee, aaa, X[6], 7); 219 | JJJ (aaa, bbb, ccc, ddd, eee, X[15], 8); 220 | JJJ (eee, aaa, bbb, ccc, ddd, X[8], 11); 221 | JJJ (ddd, eee, aaa, bbb, ccc, X[1], 14); 222 | JJJ (ccc, ddd, eee, aaa, bbb, X[10], 14); 223 | JJJ (bbb, ccc, ddd, eee, aaa, X[3], 12); 224 | JJJ (aaa, bbb, ccc, ddd, eee, X[12], 6); 225 | 226 | /* parallel round 2 */ 227 | III (eee, aaa, bbb, ccc, ddd, X[6], 9); 228 | III (ddd, eee, aaa, bbb, ccc, X[11], 13); 229 | III (ccc, ddd, eee, aaa, bbb, X[3], 15); 230 | III (bbb, ccc, ddd, eee, aaa, X[7], 7); 231 | III (aaa, bbb, ccc, ddd, eee, X[0], 12); 232 | III (eee, aaa, bbb, ccc, ddd, X[13], 8); 233 | III (ddd, eee, aaa, bbb, ccc, X[5], 9); 234 | III (ccc, ddd, eee, aaa, bbb, X[10], 11); 235 | III (bbb, ccc, ddd, eee, aaa, X[14], 7); 236 | III (aaa, bbb, ccc, ddd, eee, X[15], 7); 237 | III (eee, aaa, bbb, ccc, ddd, X[8], 12); 238 | III (ddd, eee, aaa, bbb, ccc, X[12], 7); 239 | III (ccc, ddd, eee, aaa, bbb, X[4], 6); 240 | III (bbb, ccc, ddd, eee, aaa, X[9], 15); 241 | III (aaa, bbb, ccc, ddd, eee, X[1], 13); 242 | III (eee, aaa, bbb, ccc, ddd, X[2], 11); 243 | 244 | /* parallel round 3 */ 245 | HHH (ddd, eee, aaa, bbb, ccc, X[15], 9); 246 | HHH (ccc, ddd, eee, aaa, bbb, X[5], 7); 247 | HHH (bbb, ccc, ddd, eee, aaa, X[1], 15); 248 | HHH (aaa, bbb, ccc, ddd, eee, X[3], 11); 249 | HHH (eee, aaa, bbb, ccc, ddd, X[7], 8); 250 | HHH (ddd, eee, aaa, bbb, ccc, X[14], 6); 251 | HHH (ccc, ddd, eee, aaa, bbb, X[6], 6); 252 | HHH (bbb, ccc, ddd, eee, aaa, X[9], 14); 253 | HHH (aaa, bbb, ccc, ddd, eee, X[11], 12); 254 | HHH (eee, aaa, bbb, ccc, ddd, X[8], 13); 255 | HHH (ddd, eee, aaa, bbb, ccc, X[12], 5); 256 | HHH (ccc, ddd, eee, aaa, bbb, X[2], 14); 257 | HHH (bbb, ccc, ddd, eee, aaa, X[10], 13); 258 | HHH (aaa, bbb, ccc, ddd, eee, X[0], 13); 259 | HHH (eee, aaa, bbb, ccc, ddd, X[4], 7); 260 | HHH (ddd, eee, aaa, bbb, ccc, X[13], 5); 261 | 262 | /* parallel round 4 */ 263 | GGG (ccc, ddd, eee, aaa, bbb, X[8], 15); 264 | GGG (bbb, ccc, ddd, eee, aaa, X[6], 5); 265 | GGG (aaa, bbb, ccc, ddd, eee, X[4], 8); 266 | GGG (eee, aaa, bbb, ccc, ddd, X[1], 11); 267 | GGG (ddd, eee, aaa, bbb, ccc, X[3], 14); 268 | GGG (ccc, ddd, eee, aaa, bbb, X[11], 14); 269 | GGG (bbb, ccc, ddd, eee, aaa, X[15], 6); 270 | GGG (aaa, bbb, ccc, ddd, eee, X[0], 14); 271 | GGG (eee, aaa, bbb, ccc, ddd, X[5], 6); 272 | GGG (ddd, eee, aaa, bbb, ccc, X[12], 9); 273 | GGG (ccc, ddd, eee, aaa, bbb, X[2], 12); 274 | GGG (bbb, ccc, ddd, eee, aaa, X[13], 9); 275 | GGG (aaa, bbb, ccc, ddd, eee, X[9], 12); 276 | GGG (eee, aaa, bbb, ccc, ddd, X[7], 5); 277 | GGG (ddd, eee, aaa, bbb, ccc, X[10], 15); 278 | GGG (ccc, ddd, eee, aaa, bbb, X[14], 8); 279 | 280 | /* parallel round 5 */ 281 | FFF (bbb, ccc, ddd, eee, aaa, X[12], 8); 282 | FFF (aaa, bbb, ccc, ddd, eee, X[15], 5); 283 | FFF (eee, aaa, bbb, ccc, ddd, X[10], 12); 284 | FFF (ddd, eee, aaa, bbb, ccc, X[4], 9); 285 | FFF (ccc, ddd, eee, aaa, bbb, X[1], 12); 286 | FFF (bbb, ccc, ddd, eee, aaa, X[5], 5); 287 | FFF (aaa, bbb, ccc, ddd, eee, X[8], 14); 288 | FFF (eee, aaa, bbb, ccc, ddd, X[7], 6); 289 | FFF (ddd, eee, aaa, bbb, ccc, X[6], 8); 290 | FFF (ccc, ddd, eee, aaa, bbb, X[2], 13); 291 | FFF (bbb, ccc, ddd, eee, aaa, X[13], 6); 292 | FFF (aaa, bbb, ccc, ddd, eee, X[14], 5); 293 | FFF (eee, aaa, bbb, ccc, ddd, X[0], 15); 294 | FFF (ddd, eee, aaa, bbb, ccc, X[3], 13); 295 | FFF (ccc, ddd, eee, aaa, bbb, X[9], 11); 296 | FFF (bbb, ccc, ddd, eee, aaa, X[11], 11); 297 | 298 | /* combine results */ 299 | ddd += cc + MDbuf[1]; /* final result for MDbuf[0] */ 300 | MDbuf[1] = MDbuf[2] + dd + eee; 301 | MDbuf[2] = MDbuf[3] + ee + aaa; 302 | MDbuf[3] = MDbuf[4] + aa + bbb; 303 | MDbuf[4] = MDbuf[0] + bb + ccc; 304 | MDbuf[0] = ddd; 305 | } 306 | 307 | 308 | /* 309 | puts bytes from strptr into X and pad out; appends length 310 | and finally, compresses the last block(s) 311 | note: length in bits == 8 * (lswlen + 2^32 mswlen). 312 | note: there are (lswlen mod 64) bytes left in strptr. 313 | */ 314 | static void 315 | RMDFinish (uint32_t * MDbuf, uint8_t * strptr, uint32_t lswlen, 316 | uint32_t mswlen) 317 | { 318 | uint32_t i; /* counter */ 319 | uint32_t X[16]; /* message words */ 320 | 321 | memset (X, 0, 16 * sizeof (uint32_t)); 322 | 323 | /* put bytes from strptr into X */ 324 | for (i = 0; i < (lswlen & 63); i++) 325 | { 326 | /* byte i goes into word X[i div 4] at pos. 8*(i mod 4) */ 327 | X[i >> 2] ^= (uint32_t) * strptr++ << (8 * (i & 3)); 328 | } 329 | 330 | /* append the bit m_n == 1 */ 331 | X[(lswlen >> 2) & 15] ^= (uint32_t) 1 << (8 * (lswlen & 3) + 7); 332 | 333 | if ((lswlen & 63) > 55) 334 | { 335 | /* length goes to next block */ 336 | RMDcompress (MDbuf, X); 337 | memset (X, 0, 16 * sizeof (uint32_t)); 338 | } 339 | 340 | /* append length in bits */ 341 | X[14] = lswlen << 3; 342 | X[15] = (lswlen >> 29) | (mswlen << 3); 343 | RMDcompress (MDbuf, X); 344 | } 345 | 346 | /* 347 | Shuffle the bytes into little-endian order within words, as per the 348 | RIPEMD-160 spec (which follows MD4 conventions). 349 | */ 350 | static void 351 | rmd160ByteSwap (uint32_t * dest, uint8_t const *src, unsigned int words) 352 | { 353 | do 354 | { 355 | *dest++ = (uint32_t) ((unsigned) src[3] << 8 | src[2]) << 16 | 356 | ((unsigned) src[1] << 8 | src[0]); 357 | src += 4; 358 | } 359 | while (--words); 360 | } 361 | 362 | 363 | /* 364 | Initialize the RIPEMD-160 values 365 | */ 366 | void 367 | RMD160Init (RMD160_CTX * ctx) 368 | { 369 | /* Set the h-vars to their initial values */ 370 | RMDinit (ctx->iv); 371 | 372 | /* Initialise bit count */ 373 | ctx->bytesHi = 0; 374 | ctx->bytesLo = 0; 375 | } 376 | 377 | /* 378 | Update the RIPEMD-160 hash state for a block of data. 379 | */ 380 | void RMD160Update(RMD160_CTX *ctx, const unsigned char *buf, unsigned int len) 381 | { 382 | unsigned i; 383 | 384 | /* Update bitcount */ 385 | uint32_t t = ctx->bytesLo; 386 | if ((ctx->bytesLo = t + len) < t) 387 | ctx->bytesHi++; /* Carry from low to high */ 388 | 389 | i = (unsigned) t % RIPEMD160_BLOCKBYTES; /* Bytes already in ctx->key */ 390 | 391 | /* i is always less than RIPEMD160_BLOCKBYTES. */ 392 | if (RIPEMD160_BLOCKBYTES - i > len) 393 | { 394 | memcpy ((uint8_t *) ctx->key + i, buf, len); 395 | return; 396 | } 397 | 398 | if (i) 399 | { /* First chunk is an odd size */ 400 | memcpy ((uint8_t *) ctx->key + i, buf, RIPEMD160_BLOCKBYTES - i); 401 | rmd160ByteSwap (ctx->key, (uint8_t *) ctx->key, RIPEMD160_BLOCKWORDS); 402 | RMDcompress (ctx->iv, ctx->key); 403 | buf += RIPEMD160_BLOCKBYTES - i; 404 | len -= RIPEMD160_BLOCKBYTES - i; 405 | } 406 | 407 | /* Process data in 64-byte chunks */ 408 | while (len >= RIPEMD160_BLOCKBYTES) 409 | { 410 | rmd160ByteSwap (ctx->key, buf, RIPEMD160_BLOCKWORDS); 411 | RMDcompress (ctx->iv, ctx->key); 412 | buf += RIPEMD160_BLOCKBYTES; 413 | len -= RIPEMD160_BLOCKBYTES; 414 | } 415 | 416 | /* Handle any remaining bytes of data. */ 417 | if (len) 418 | memcpy (ctx->key, buf, len); 419 | } 420 | 421 | 422 | /* 423 | Final wrapup - MD4 style padding on last block. 424 | */ 425 | void 426 | RMD160Final (unsigned char digest[20], RMD160_CTX * ctx) 427 | { 428 | int i; 429 | uint32_t t; 430 | 431 | RMDFinish (ctx->iv, (uint8_t *) ctx->key, ctx->bytesLo, ctx->bytesHi); 432 | 433 | for (i = 0; i < RIPEMD160_HASHWORDS; i++) 434 | { 435 | t = ctx->iv[i]; 436 | digest[i * 4 + 0] = (uint8_t) t; 437 | digest[i * 4 + 1] = (uint8_t) (t >> 8); 438 | digest[i * 4 + 2] = (uint8_t) (t >> 16); 439 | digest[i * 4 + 3] = (uint8_t) (t >> 24); 440 | } 441 | 442 | memset (ctx, 0, sizeof (RMD160_CTX)); /* In case it's sensitive */ 443 | } 444 | 445 | void RMD160Data(const unsigned char *buf, unsigned int len, char *out) { 446 | RMD160_CTX ctx; 447 | RMD160Init(&ctx); 448 | RMD160Update(&ctx,(unsigned char *)buf,len); 449 | RMD160Final((unsigned char *)out,&ctx); 450 | } 451 | --------------------------------------------------------------------------------