OSDN Git Service

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