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