-
Notifications
You must be signed in to change notification settings - Fork 1
/
blake2.sol
211 lines (162 loc) · 5.55 KB
/
blake2.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
contract BLAKE2b {
//CONSTANTS
uint8[16][12] public SIGMA = [
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15 ],
[ 14,10, 4, 8, 9,15,13, 6, 1,12, 0, 2,11, 7, 5, 3 ],
[ 11, 8,12, 0, 5, 2,15,13,10,14, 3, 6, 7, 1, 9, 4 ],
[ 7, 9, 3, 1,13,12,11,14, 2, 6, 5,10, 4, 0,15, 8 ],
[ 9, 0, 5, 7, 2, 4,10,15,14, 1,11,12, 6, 8, 3,13 ],
[ 2,12, 6,10, 0,11, 8, 3, 4,13, 7, 5,15,14, 1, 9 ],
[ 12, 5, 1,15,14,13, 4,10, 0, 7, 6, 3, 9, 2, 8,11 ],
[ 13,11, 7,14,12, 1, 3, 9, 5, 0,15, 4, 8, 6, 2,10 ],
[ 6,15,14, 9,11, 3, 0, 8,12, 2,13, 7, 1, 4,10, 5 ],
[ 10, 2, 8, 4, 7, 6, 1, 5,15,11, 9,14, 3,12,13 ,0 ],
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15 ],
[ 14,10, 4, 8, 9,15,13, 6, 1,12, 0, 2,11, 7, 5, 3 ]
];
uint64[8] public IV = [
0x6a09e667f3bcc908, 0xbb67ae8584caa73b,
0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1,
0x510e527fade682d1, 0x9b05688c2b3e6c1f,
0x1f83d9abfb41bd6b, 0x5be0cd19137e2179
];
struct BLAKE2b_ctx {
uint8[128] b; //input buffer
uint64[8] h; //chained state
uint64[2] t; //total bytes
uint64 c; //Size of b
uint outlen; //diigest output size
}
function G(uint64[16] v, uint a, uint b, uint c, uint d, uint64 x, uint64 y) constant { //OPTIMIZE HERE
v[a] = v[a] + v[b] + x;
v[d] = rotate(v[d] ^ v[a], 32);
v[c] = v[c] + v[d];
v[b] = rotate(v[b] ^ v[c], 24);
v[a] = v[a] + v[b] + y;
v[d] = rotate(v[d] ^ v[a], 16);
v[c] = v[c] + v[d];
v[b] = rotate(v[b] ^ v[c], 63);
}
function compress(BLAKE2b_ctx ctx, bool last) private {
uint64[16] memory v;
uint64[16] memory m;
for(uint i=0; i<8; i++){
v[i] = ctx.h[i];
v[i+8] = IV[i];
}
v[12] = v[12] ^ ctx.t[0];
v[13] = v[13] ^ ctx.t[1];
if(last) v[14] = ~v[14];
for(i = 0; i <16; i++){
uint64 mi = 0;
for(uint j = 0; j < 8; j++){
mi = mi ^ shift_left(ctx.b[i*8 + j], j*8);
}
m[i] = mi;
}
for(i=0; i<12; i++){
G( v, 0, 4, 8, 12, m[SIGMA[i][0]], m[SIGMA[i][1]]);
G( v, 1, 5, 9, 13, m[SIGMA[i][2]], m[SIGMA[i][3]]);
G( v, 2, 6, 10, 14, m[SIGMA[i][4]], m[SIGMA[i][5]]);
G( v, 3, 7, 11, 15, m[SIGMA[i][6]], m[SIGMA[i][7]]);
G( v, 0, 5, 10, 15, m[SIGMA[i][8]], m[SIGMA[i][9]]);
G( v, 1, 6, 11, 12, m[SIGMA[i][10]], m[SIGMA[i][11]]);
G( v, 2, 7, 8, 13, m[SIGMA[i][12]], m[SIGMA[i][13]]);
G( v, 3, 4, 9, 14, m[SIGMA[i][14]], m[SIGMA[i][15]]);
}
for(i=0; i<8; ++i){
ctx.h[i] = ctx.h[i] ^ v[i] ^ v[i+8];
}
}
function init(BLAKE2b_ctx ctx, uint64 outlen, bytes key) private{
uint i;
if(outlen == 0 || outlen > 64 || key.length > 64) throw;
for(i = 0; i< 8; i++){
ctx.h[i] = IV[i];
}
ctx.h[0] = ctx.h[0] ^ 0x01010000 ^ shift_left(uint64(key.length), 8) ^ outlen; // Set up parameter block
ctx.t[0] = 0;
ctx.t[1] = 0;
ctx.c = 0;
ctx.outlen = outlen;
i = key.length;
for(i = key.length; i < 128; i++){
ctx.b[i] = 0;
}
if(key.length > 0){
update(ctx, key);
ctx.c = 128;
}
}
function update(BLAKE2b_ctx ctx, bytes input) private {
uint i;
for(i = 0; i < input.length; i++){
if(ctx.c == 128){ //buffer full?
ctx.t[0] += ctx.c; //add counters
if(ctx.t[0] < ctx.c){ //overflow?
ctx.t[1] ++; //carry to high word
}
compress(ctx, false);
ctx.c = 0;
}
ctx.b[ctx.c++] = uint8(input[i]);
}
}
function finalize(BLAKE2b_ctx ctx, uint64[8] out) private {
uint i;
ctx.t[0] += ctx.c;
if(ctx.t[0] < ctx.c) ctx.t[1]++;
while(ctx.c < 128){
ctx.b[ctx.c++] = 0;
}
compress(ctx,true);
for(i=0; i < ctx.outlen / 8; i++){
out[i] = toLittleEndian(ctx.h[i]);
}
if(ctx.outlen < 64){
out[ctx.outlen/8] = shift_right(toLittleEndian(ctx.h[ctx.outlen/8]),64-8*(ctx.outlen%8));
}
}
function blake2b(bytes input, bytes key, uint64 outlen) constant returns(uint64[8]){
BLAKE2b_ctx memory ctx;
uint64[8] memory out;
init(ctx, outlen, key);
update(ctx, input);
finalize(ctx, out);
return out;
}
// Utility functions
function getWords(uint64 a) constant returns (uint64 b) { //Flips endianness of words
return (a & 0xFF00000000000000 )/ 0x1000000000000000^
shift_left(a & 0x00FF000000000000, 8)/ 0x0010000000000000^
shift_left(a & 0x0000FF0000000000, 16)/0x0000100000000000^
shift_left(a & 0x000000FF00000000, 24)/0x0000001000000000^
shift_left(a & 0x00000000FF000000, 32)/0x0000000010000000^
shift_left(a & 0x0000000000FF0000, 40)/0x0000000000100000^
shift_left(a & 0x000000000000FF00, 48)/0x0000000000001000^
shift_left(a & 0x00000000000000FF, 56)/0x0000000000000010;
}
function shift_right(uint64 a, uint shift) constant returns(uint64 b){
return uint64(a / 2**shift);
}
function shift_left(uint64 a, uint shift) constant returns(uint64){
return uint64((a * 2**shift) % (2**64));
}
function rotate(uint64 a, uint shift) constant returns(uint64 b){
return shift_right(a, shift) ^ shift_left(a, 64-shift);
}
function assign (bytes32 a, uint b, byte c) constant returns (bytes32 d){
uint e = uint(c);
assembly {
mstore(0,a)
mstore8(b,e)
return(0,0x20)
}
}
function toLittleEndian(uint64 a) returns(uint64 b){
bytes8 temp = bytes8(a);
for(uint i; i < 8; i++){
b = uint64(b ^ (uint64(temp[i]) * (2**(0x08 * i))));
}
}
}