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