be9db16fbc4a92fe6737e0a4a7d11843fabbf4c5
[NovacoinLibrary.git] / Novacoin / ScryptHash256.cs
1 \feffusing System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 using Medo.Security.Cryptography;
7 using System.Security.Cryptography;
8
9 namespace Novacoin
10 {
11     public class ScryptHash256 : Hash
12     {
13         // 32 bytes
14         public override int hashSize
15         {
16             get { return 32; }
17         }
18
19         public ScryptHash256() : base() { }
20         public ScryptHash256(byte[] bytesArray) : base(bytesArray) { }
21         public ScryptHash256(IList<byte> bytesList) : base(bytesList) { }
22
23         public static ScryptHash256 Compute256(IEnumerable<byte> inputBytes)
24         {
25             byte[] dataBytes = inputBytes.ToArray();
26
27             uint[] V = new uint[(131072 + 63) / sizeof(uint)];
28
29             uint[] X = null;
30             using (HMACSHA256 hmac = new HMACSHA256())
31             {
32                 Pbkdf2 df = new Pbkdf2(hmac, dataBytes, dataBytes, 1);
33                 byte[] keyBytes1 = df.GetBytes(128);
34
35                 X = Interop.ToUInt32Array(keyBytes1);
36             }
37
38             ushort i, j, k;
39             for (i = 0; i < 1024; i++)
40             {
41                 Array.Copy(X, 0, V, i * 32, 32);
42
43                 xor_salsa8(ref X, 0, ref X, 16);
44                 xor_salsa8(ref X, 16, ref X, 0);
45             }
46             for (i = 0; i < 1024; i++)
47             {
48                 j = (ushort)(32 * (X[16] & 1023));
49                 for (k = 0; k < 32; k++)
50                     X[k] ^= V[j + k];
51                 xor_salsa8(ref X, 0, ref X, 16);
52                 xor_salsa8(ref X, 16, ref X, 0);
53             }
54
55             byte[] xBytes = Interop.LEBytes(X);
56
57             byte[] keyBytes2 = null;
58             using (HMACSHA256 hmac = new HMACSHA256())
59             {
60                 Pbkdf2 df = new Pbkdf2(hmac, dataBytes, xBytes, 1);
61                 keyBytes2 = df.GetBytes(32);
62             }
63
64             return new ScryptHash256(keyBytes2);
65         }
66
67         private static void xor_salsa8(ref uint[] B, int indexB, ref uint[] Bx, int indexBx)
68         {
69             uint x00, x01, x02, x03, x04, x05, x06, x07, x08, x09, x10, x11, x12, x13, x14, x15;
70             byte i;
71
72             x00 = (B[indexB + 0] ^= Bx[indexBx + 0]);
73             x01 = (B[indexB + 1] ^= Bx[indexBx + 1]);
74             x02 = (B[indexB + 2] ^= Bx[indexBx + 2]);
75             x03 = (B[indexB + 3] ^= Bx[indexBx + 3]);
76             x04 = (B[indexB + 4] ^= Bx[indexBx + 4]);
77             x05 = (B[indexB + 5] ^= Bx[indexBx + 5]);
78             x06 = (B[indexB + 6] ^= Bx[indexBx + 6]);
79             x07 = (B[indexB + 7] ^= Bx[indexBx + 7]);
80             x08 = (B[indexB + 8] ^= Bx[indexBx + 8]);
81             x09 = (B[indexB + 9] ^= Bx[indexBx + 9]);
82             x10 = (B[indexB + 10] ^= Bx[indexBx + 10]);
83             x11 = (B[indexB + 11] ^= Bx[indexBx + 11]);
84             x12 = (B[indexB + 12] ^= Bx[indexBx + 12]);
85             x13 = (B[indexB + 13] ^= Bx[indexBx + 13]);
86             x14 = (B[indexB + 14] ^= Bx[indexBx + 14]);
87             x15 = (B[indexB + 15] ^= Bx[indexBx + 15]);
88
89             Func<uint, int, uint> R = (a, b) => (((a) << (b)) | ((a) >> (32 - (b))));
90
91             for (i = 0; i < 8; i += 2)
92             {
93                 /* Operate on columns. */
94                 x04 ^= R(x00 + x12, 7); x09 ^= R(x05 + x01, 7);
95                 x14 ^= R(x10 + x06, 7); x03 ^= R(x15 + x11, 7);
96
97                 x08 ^= R(x04 + x00, 9); x13 ^= R(x09 + x05, 9);
98                 x02 ^= R(x14 + x10, 9); x07 ^= R(x03 + x15, 9);
99
100                 x12 ^= R(x08 + x04, 13); x01 ^= R(x13 + x09, 13);
101                 x06 ^= R(x02 + x14, 13); x11 ^= R(x07 + x03, 13);
102
103                 x00 ^= R(x12 + x08, 18); x05 ^= R(x01 + x13, 18);
104                 x10 ^= R(x06 + x02, 18); x15 ^= R(x11 + x07, 18);
105
106                 /* Operate on rows. */
107                 x01 ^= R(x00 + x03, 7); x06 ^= R(x05 + x04, 7);
108                 x11 ^= R(x10 + x09, 7); x12 ^= R(x15 + x14, 7);
109
110                 x02 ^= R(x01 + x00, 9); x07 ^= R(x06 + x05, 9);
111                 x08 ^= R(x11 + x10, 9); x13 ^= R(x12 + x15, 9);
112
113                 x03 ^= R(x02 + x01, 13); x04 ^= R(x07 + x06, 13);
114                 x09 ^= R(x08 + x11, 13); x14 ^= R(x13 + x12, 13);
115
116                 x00 ^= R(x03 + x02, 18); x05 ^= R(x04 + x07, 18);
117                 x10 ^= R(x09 + x08, 18); x15 ^= R(x14 + x13, 18);
118             }
119
120             B[indexB + 0] += x00;
121             B[indexB + 1] += x01;
122             B[indexB + 2] += x02;
123             B[indexB + 3] += x03;
124             B[indexB + 4] += x04;
125             B[indexB + 5] += x05;
126             B[indexB + 6] += x06;
127             B[indexB + 7] += x07;
128             B[indexB + 8] += x08;
129             B[indexB + 9] += x09;
130             B[indexB + 10] += x10;
131             B[indexB + 11] += x11;
132             B[indexB + 12] += x12;
133             B[indexB + 13] += x13;
134             B[indexB + 14] += x14;
135             B[indexB + 15] += x15;
136         }
137     }
138 }