github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/mining/tensority/cgo_algorithm/lib/scrypt.h (about) 1 /* scrypt.h */ 2 #ifndef SCRYPT_H 3 #define SCRYPT_H 4 5 #include <stdint.h> 6 #include <assert.h> 7 #include <stdio.h> 8 9 struct Words16 { 10 uint32_t w[16]; 11 }; 12 13 #define ROTL(a, b) (((a) << (b)) | ((a) >> (32 - (b)))) 14 15 inline void xor_salsa8(uint32_t B[16], const uint32_t Bx[16]) { 16 uint32_t x00,x01,x02,x03,x04,x05,x06,x07,x08,x09,x10,x11,x12,x13,x14,x15; 17 int i; 18 19 x00 = (B[ 0] ^= Bx[ 0]); 20 x01 = (B[ 1] ^= Bx[ 1]); 21 x02 = (B[ 2] ^= Bx[ 2]); 22 x03 = (B[ 3] ^= Bx[ 3]); 23 x04 = (B[ 4] ^= Bx[ 4]); 24 x05 = (B[ 5] ^= Bx[ 5]); 25 x06 = (B[ 6] ^= Bx[ 6]); 26 x07 = (B[ 7] ^= Bx[ 7]); 27 x08 = (B[ 8] ^= Bx[ 8]); 28 x09 = (B[ 9] ^= Bx[ 9]); 29 x10 = (B[10] ^= Bx[10]); 30 x11 = (B[11] ^= Bx[11]); 31 x12 = (B[12] ^= Bx[12]); 32 x13 = (B[13] ^= Bx[13]); 33 x14 = (B[14] ^= Bx[14]); 34 x15 = (B[15] ^= Bx[15]); 35 for (i = 0; i < 8; i += 2) { 36 /* Operate on columns. */ 37 x04 ^= ROTL(x00 + x12, 7); x09 ^= ROTL(x05 + x01, 7); 38 x14 ^= ROTL(x10 + x06, 7); x03 ^= ROTL(x15 + x11, 7); 39 40 x08 ^= ROTL(x04 + x00, 9); x13 ^= ROTL(x09 + x05, 9); 41 x02 ^= ROTL(x14 + x10, 9); x07 ^= ROTL(x03 + x15, 9); 42 43 x12 ^= ROTL(x08 + x04, 13); x01 ^= ROTL(x13 + x09, 13); 44 x06 ^= ROTL(x02 + x14, 13); x11 ^= ROTL(x07 + x03, 13); 45 46 x00 ^= ROTL(x12 + x08, 18); x05 ^= ROTL(x01 + x13, 18); 47 x10 ^= ROTL(x06 + x02, 18); x15 ^= ROTL(x11 + x07, 18); 48 49 /* Operate on rows. */ 50 x01 ^= ROTL(x00 + x03, 7); x06 ^= ROTL(x05 + x04, 7); 51 x11 ^= ROTL(x10 + x09, 7); x12 ^= ROTL(x15 + x14, 7); 52 53 x02 ^= ROTL(x01 + x00, 9); x07 ^= ROTL(x06 + x05, 9); 54 x08 ^= ROTL(x11 + x10, 9); x13 ^= ROTL(x12 + x15, 9); 55 56 x03 ^= ROTL(x02 + x01, 13); x04 ^= ROTL(x07 + x06, 13); 57 x09 ^= ROTL(x08 + x11, 13); x14 ^= ROTL(x13 + x12, 13); 58 59 x00 ^= ROTL(x03 + x02, 18); x05 ^= ROTL(x04 + x07, 18); 60 x10 ^= ROTL(x09 + x08, 18); x15 ^= ROTL(x14 + x13, 18); 61 } 62 B[ 0] += x00; 63 B[ 1] += x01; 64 B[ 2] += x02; 65 B[ 3] += x03; 66 B[ 4] += x04; 67 B[ 5] += x05; 68 B[ 6] += x06; 69 B[ 7] += x07; 70 B[ 8] += x08; 71 B[ 9] += x09; 72 B[10] += x10; 73 B[11] += x11; 74 B[12] += x12; 75 B[13] += x13; 76 B[14] += x14; 77 B[15] += x15; 78 } 79 80 struct Words32 { 81 Words16 lo, hi; 82 uint32_t get(uint32_t i) const { 83 if(i<16) return lo.w[i]; 84 else if(i<32) return hi.w[i-16]; 85 else assert(false); 86 } 87 void xor_other(const Words32& other) { 88 for(int i=0; i<16; i++) lo.w[i]^=other.lo.w[i]; 89 for(int i=0; i<16; i++) hi.w[i]^=other.hi.w[i]; 90 } 91 }; 92 93 struct LTCMemory { 94 Words32 w32[1024]; 95 const Words32& get(uint32_t i) const { 96 assert(i<1024); 97 return w32[i]; 98 } 99 void printItems() { 100 printf("\nprint scrypt items\n"); 101 for(int i = 0; i < 16; i++) { 102 printf(" "); 103 printf(" %u ", uint32_t(this->get(0).lo.w[i])); 104 } 105 } 106 void scrypt(Words32& X) { 107 for (int i = 0; i < 1024; i++) { 108 w32[i]=X; 109 xor_salsa8(X.lo.w, X.hi.w); 110 xor_salsa8(X.hi.w, X.lo.w); 111 } 112 for (int i = 0; i < 1024; i++) { 113 int j = X.hi.w[0] & 1023; 114 X.xor_other(w32[j]); 115 xor_salsa8(X.lo.w, X.hi.w); 116 xor_salsa8(X.hi.w, X.lo.w); 117 } 118 } 119 }; 120 121 #endif