OSDN Git Service

test (#52)
[bytom/vapor.git] / mining / tensority / cgo_algorithm / lib / scrypt.h
index 0c3a8fa..24a9b4d 100644 (file)
-/* scrypt.h */
-#ifndef SCRYPT_H
-#define SCRYPT_H
-
-#include <stdint.h>
-#include <assert.h>
-#include <stdio.h>
-
-struct Words16 {
-  uint32_t w[16];
-};
-
-#define ROTL(a, b) (((a) << (b)) | ((a) >> (32 - (b))))
-
-inline void xor_salsa8(uint32_t B[16], const uint32_t Bx[16]) {
-  uint32_t x00,x01,x02,x03,x04,x05,x06,x07,x08,x09,x10,x11,x12,x13,x14,x15;
-  int i;
-
-  x00 = (B[ 0] ^= Bx[ 0]);
-  x01 = (B[ 1] ^= Bx[ 1]);
-  x02 = (B[ 2] ^= Bx[ 2]);
-  x03 = (B[ 3] ^= Bx[ 3]);
-  x04 = (B[ 4] ^= Bx[ 4]);
-  x05 = (B[ 5] ^= Bx[ 5]);
-  x06 = (B[ 6] ^= Bx[ 6]);
-  x07 = (B[ 7] ^= Bx[ 7]);
-  x08 = (B[ 8] ^= Bx[ 8]);
-  x09 = (B[ 9] ^= Bx[ 9]);
-  x10 = (B[10] ^= Bx[10]);
-  x11 = (B[11] ^= Bx[11]);
-  x12 = (B[12] ^= Bx[12]);
-  x13 = (B[13] ^= Bx[13]);
-  x14 = (B[14] ^= Bx[14]);
-  x15 = (B[15] ^= Bx[15]);
-  for (i = 0; i < 8; i += 2) {
-    /* Operate on columns. */
-    x04 ^= ROTL(x00 + x12,  7);  x09 ^= ROTL(x05 + x01,  7);
-    x14 ^= ROTL(x10 + x06,  7);  x03 ^= ROTL(x15 + x11,  7);
-
-    x08 ^= ROTL(x04 + x00,  9);  x13 ^= ROTL(x09 + x05,  9);
-    x02 ^= ROTL(x14 + x10,  9);  x07 ^= ROTL(x03 + x15,  9);
-
-    x12 ^= ROTL(x08 + x04, 13);  x01 ^= ROTL(x13 + x09, 13);
-    x06 ^= ROTL(x02 + x14, 13);  x11 ^= ROTL(x07 + x03, 13);
-
-    x00 ^= ROTL(x12 + x08, 18);  x05 ^= ROTL(x01 + x13, 18);
-    x10 ^= ROTL(x06 + x02, 18);  x15 ^= ROTL(x11 + x07, 18);
-
-    /* Operate on rows. */
-    x01 ^= ROTL(x00 + x03,  7);  x06 ^= ROTL(x05 + x04,  7);
-    x11 ^= ROTL(x10 + x09,  7);  x12 ^= ROTL(x15 + x14,  7);
-
-    x02 ^= ROTL(x01 + x00,  9);  x07 ^= ROTL(x06 + x05,  9);
-    x08 ^= ROTL(x11 + x10,  9);  x13 ^= ROTL(x12 + x15,  9);
-
-    x03 ^= ROTL(x02 + x01, 13);  x04 ^= ROTL(x07 + x06, 13);
-    x09 ^= ROTL(x08 + x11, 13);  x14 ^= ROTL(x13 + x12, 13);
-
-    x00 ^= ROTL(x03 + x02, 18);  x05 ^= ROTL(x04 + x07, 18);
-    x10 ^= ROTL(x09 + x08, 18);  x15 ^= ROTL(x14 + x13, 18);
-  }
-  B[ 0] += x00;
-  B[ 1] += x01;
-  B[ 2] += x02;
-  B[ 3] += x03;
-  B[ 4] += x04;
-  B[ 5] += x05;
-  B[ 6] += x06;
-  B[ 7] += x07;
-  B[ 8] += x08;
-  B[ 9] += x09;
-  B[10] += x10;
-  B[11] += x11;
-  B[12] += x12;
-  B[13] += x13;
-  B[14] += x14;
-  B[15] += x15;
-}
-
-struct Words32 {
-  Words16 lo, hi;
-  uint32_t get(uint32_t i) const {
-    if(i<16) return lo.w[i];
-    else if(i<32) return hi.w[i-16];
-    else assert(false);
-  }
-  void xor_other(const Words32& other) {
-    for(int i=0; i<16; i++) lo.w[i]^=other.lo.w[i];
-    for(int i=0; i<16; i++) hi.w[i]^=other.hi.w[i];
-  }
-};
-
-struct LTCMemory {
-  Words32 w32[1024];
-  const Words32& get(uint32_t i) const {
-    assert(i<1024);
-    return w32[i];
-  }
-  void printItems() {
-    printf("\nprint scrypt items\n");
-    for(int i = 0; i < 16; i++) {
-      printf(" ");
-      printf(" %u ", uint32_t(this->get(0).lo.w[i]));
-    }
-  }
-  void scrypt(Words32& X) {
-    for (int i = 0; i < 1024; i++) {
-      w32[i]=X;
-      xor_salsa8(X.lo.w, X.hi.w);
-      xor_salsa8(X.hi.w, X.lo.w);
-    }
-    for (int i = 0; i < 1024; i++) {
-      int j = X.hi.w[0] & 1023;
-      X.xor_other(w32[j]);
-      xor_salsa8(X.lo.w, X.hi.w);
-      xor_salsa8(X.hi.w, X.lo.w);
-    }
-  }
-};
-
-#endif
+/* scrypt.h */\r
+#ifndef SCRYPT_H\r
+#define SCRYPT_H\r
+\r
+#include <stdint.h>\r
+#include <assert.h>\r
+#include <stdio.h>\r
+\r
+struct Words16 {\r
+  uint32_t w[16];\r
+};\r
+\r
+#define ROTL(a, b) (((a) << (b)) | ((a) >> (32 - (b))))\r
+\r
+inline void xor_salsa8(uint32_t B[16], const uint32_t Bx[16]) {\r
+  uint32_t x00,x01,x02,x03,x04,x05,x06,x07,x08,x09,x10,x11,x12,x13,x14,x15;\r
+  int i;\r
+\r
+  x00 = (B[ 0] ^= Bx[ 0]);\r
+  x01 = (B[ 1] ^= Bx[ 1]);\r
+  x02 = (B[ 2] ^= Bx[ 2]);\r
+  x03 = (B[ 3] ^= Bx[ 3]);\r
+  x04 = (B[ 4] ^= Bx[ 4]);\r
+  x05 = (B[ 5] ^= Bx[ 5]);\r
+  x06 = (B[ 6] ^= Bx[ 6]);\r
+  x07 = (B[ 7] ^= Bx[ 7]);\r
+  x08 = (B[ 8] ^= Bx[ 8]);\r
+  x09 = (B[ 9] ^= Bx[ 9]);\r
+  x10 = (B[10] ^= Bx[10]);\r
+  x11 = (B[11] ^= Bx[11]);\r
+  x12 = (B[12] ^= Bx[12]);\r
+  x13 = (B[13] ^= Bx[13]);\r
+  x14 = (B[14] ^= Bx[14]);\r
+  x15 = (B[15] ^= Bx[15]);\r
+  for (i = 0; i < 8; i += 2) {\r
+    /* Operate on columns. */\r
+    x04 ^= ROTL(x00 + x12,  7);  x09 ^= ROTL(x05 + x01,  7);\r
+    x14 ^= ROTL(x10 + x06,  7);  x03 ^= ROTL(x15 + x11,  7);\r
+\r
+    x08 ^= ROTL(x04 + x00,  9);  x13 ^= ROTL(x09 + x05,  9);\r
+    x02 ^= ROTL(x14 + x10,  9);  x07 ^= ROTL(x03 + x15,  9);\r
+\r
+    x12 ^= ROTL(x08 + x04, 13);  x01 ^= ROTL(x13 + x09, 13);\r
+    x06 ^= ROTL(x02 + x14, 13);  x11 ^= ROTL(x07 + x03, 13);\r
+\r
+    x00 ^= ROTL(x12 + x08, 18);  x05 ^= ROTL(x01 + x13, 18);\r
+    x10 ^= ROTL(x06 + x02, 18);  x15 ^= ROTL(x11 + x07, 18);\r
+\r
+    /* Operate on rows. */\r
+    x01 ^= ROTL(x00 + x03,  7);  x06 ^= ROTL(x05 + x04,  7);\r
+    x11 ^= ROTL(x10 + x09,  7);  x12 ^= ROTL(x15 + x14,  7);\r
+\r
+    x02 ^= ROTL(x01 + x00,  9);  x07 ^= ROTL(x06 + x05,  9);\r
+    x08 ^= ROTL(x11 + x10,  9);  x13 ^= ROTL(x12 + x15,  9);\r
+\r
+    x03 ^= ROTL(x02 + x01, 13);  x04 ^= ROTL(x07 + x06, 13);\r
+    x09 ^= ROTL(x08 + x11, 13);  x14 ^= ROTL(x13 + x12, 13);\r
+\r
+    x00 ^= ROTL(x03 + x02, 18);  x05 ^= ROTL(x04 + x07, 18);\r
+    x10 ^= ROTL(x09 + x08, 18);  x15 ^= ROTL(x14 + x13, 18);\r
+  }\r
+  B[ 0] += x00;\r
+  B[ 1] += x01;\r
+  B[ 2] += x02;\r
+  B[ 3] += x03;\r
+  B[ 4] += x04;\r
+  B[ 5] += x05;\r
+  B[ 6] += x06;\r
+  B[ 7] += x07;\r
+  B[ 8] += x08;\r
+  B[ 9] += x09;\r
+  B[10] += x10;\r
+  B[11] += x11;\r
+  B[12] += x12;\r
+  B[13] += x13;\r
+  B[14] += x14;\r
+  B[15] += x15;\r
+}\r
+\r
+struct Words32 {\r
+  Words16 lo, hi;\r
+  uint32_t get(uint32_t i) const {\r
+    if(i<16) return lo.w[i];\r
+    else if(i<32) return hi.w[i-16];\r
+    else assert(false);\r
+  }\r
+  void xor_other(const Words32& other) {\r
+    for(int i=0; i<16; i++) lo.w[i]^=other.lo.w[i];\r
+    for(int i=0; i<16; i++) hi.w[i]^=other.hi.w[i];\r
+  }\r
+};\r
+\r
+struct LTCMemory {\r
+  Words32 w32[1024];\r
+  const Words32& get(uint32_t i) const {\r
+    assert(i<1024);\r
+    return w32[i];\r
+  }\r
+  void printItems() {\r
+    printf("\nprint scrypt items\n");\r
+    for(int i = 0; i < 16; i++) {\r
+      printf(" ");\r
+      printf(" %u ", uint32_t(this->get(0).lo.w[i]));\r
+    }\r
+  }\r
+  void scrypt(Words32& X) {\r
+    for (int i = 0; i < 1024; i++) {\r
+      w32[i]=X;\r
+      xor_salsa8(X.lo.w, X.hi.w);\r
+      xor_salsa8(X.hi.w, X.lo.w);\r
+    }\r
+    for (int i = 0; i < 1024; i++) {\r
+      int j = X.hi.w[0] & 1023;\r
+      X.xor_other(w32[j]);\r
+      xor_salsa8(X.lo.w, X.hi.w);\r
+      xor_salsa8(X.hi.w, X.lo.w);\r
+    }\r
+  }\r
+};\r
+\r
+#endif\r