OpenCores
URL https://opencores.org/ocsvn/btcfpgaminer/btcfpgaminer/trunk

Subversion Repositories btcfpgaminer

[/] [btcfpgaminer/] [trunk/] [sha256.vhd] - Blame information for rev 6

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 6 zeuscz
-- ------------------------------------------------------------------------
2
-- Copyright (C) 2010 Arif Endro Nugroho
3
-- All rights reserved.
4
-- 
5
-- Redistribution and use in source and binary forms, with or without
6
-- modification, are permitted provided that the following conditions
7
-- are met:
8
-- 
9
-- 1. Redistributions of source code must retain the above copyright
10
--    notice, this list of conditions and the following disclaimer.
11
-- 2. Redistributions in binary form must reproduce the above copyright
12
--    notice, this list of conditions and the following disclaimer in the
13
--    documentation and/or other materials provided with the distribution.
14
-- 
15
-- THIS SOFTWARE IS PROVIDED BY ARIF ENDRO NUGROHO "AS IS" AND ANY EXPRESS
16
-- OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17
-- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
-- DISCLAIMED. IN NO EVENT SHALL ARIF ENDRO NUGROHO BE LIABLE FOR ANY
19
-- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21
-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22
-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23
-- STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24
-- ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25
-- POSSIBILITY OF SUCH DAMAGE.
26
-- 
27
-- End Of License.
28
-- ------------------------------------------------------------------------
29
--
30
-- MaxMessage  <= 2^64 bits
31
-- BlockSize   ==  512 bits
32
-- WordSize    ==   32 bits
33
-- MDigestSize ==  256 bits
34
-- Security    ==  128 bits
35
--
36
-- SHLnx  = (x<<n)
37
-- SHRnx  = (x>>n)
38
-- ROTRnx = (x>>n) or (x<<w-n)
39
-- ROTLnx = (x<<n) or (x>>w-n)
40
--
41
-- f0 = ((x and y) xor (not(x) and z))              --   Ch(x,y,z)
42
-- f1 = ((x and y) xor (x and z)  xor (y and z)     --  Maj(x,y,z)
43
-- f2 = ROTR  2(x) xor ROTR 13(x) xor ROTR 22(x)    --   Sigma0(x)
44
-- f3 = ROTR  6(x) xor ROTR 11(x) xor ROTR 25(x)    --   Sigma1(x)
45
-- f4 = ROTR  7(x) xor ROTR 18(x) xor SHR   3(x)    --   Tetha0(x)
46
-- f5 = ROTR 17(x) xor ROTR 19(x) xor SHR  10(x)    --   Tetha1(x)
47
--
48
-- h0 = 0x6a09e667
49
-- h1 = 0xbb67ae85
50
-- h2 = 0x3c6ef372
51
-- h3 = 0xa54ff53a
52
-- h4 = 0x510e527f
53
-- h5 = 0x9b05688c
54
-- h6 = 0x1f83d9ab
55
-- h7 = 0x5be0cd19
56
--
57
-- k[0-63] looks like better implemented in ROM file
58
--         with 32 bit in each contants it would take
59
--         64 x 32 bit storage which equal to
60
--            2048 bit ROM
61
--
62
-- Step 1
63
-- W(t) = M(t)                                                  0 <= t <=  15 -- we need 16x32 (512) bit registers
64
-- W(t) = f5(W(t-2)) + W(t-7) + f4(W(t-15)) + W(t-16);         16 <= t <=  79
65
-- W    = f5(W(  1)) + W(  6) + f4(W(  14)) + W(  15);         16 <= t <=  79
66
--
67
-- Step 2
68
-- a = h0; b = h1; c = h2; d = h3; e = h4; f = h5; g = h6; h = h7;
69
--
70
-- Step 3
71
-- for t 0 step 1 to 63 do
72
-- T1= h + f3(e) + f0(e, f, g) + k(t) + W(t)
73
-- T2=     f2(a) + f1(a, b, c)
74
-- h = g
75
-- g = f
76
-- f = e
77
-- e = d + T1
78
-- d = c
79
-- c = b
80
-- b = a
81
-- a = T1 + T2
82
--
83
-- Step 4
84
-- H0 = a + h0;
85
-- H1 = b + h1;
86
-- H2 = c + h2;
87
-- H3 = d + h3;
88
-- H4 = e + H4;
89
-- H5 = f + H5;
90
-- H6 = g + H6;
91
-- H7 = h + H7;
92
--
93
--  31 63 95 127 159 191 223 255 287 319 351 383 415 447 479 511
94
-- 0 32 64 96 128 160 192 224 256 288 320 352 384 416 448 480 512
95
--    0  1  2   3   4   5   6   7   8   9   a   b   c   d   e   f
96
 
97
library ieee;
98
use ieee.std_logic_1164.all;
99
use ieee.numeric_std.all;
100
 
101
entity sha256 is
102
  port(
103
  m                : in  bit_vector ( 31 downto 0); -- 32 bit data path require 16 clock to load all 512 bits of each block
104
  init             : in  bit;                       --    initial message (init = 1 po dobu pouze prvniho taktu potom init = 0)
105
  ld               : in  bit;                       --    load signal (ld = 1 po dobu nacitani 512 bitu (16 taktu) potom ld = 0)
106
  md               : out bit_vector ( 31 downto 0); --    5 clock after active valid signal is the message hash result
107
  v                : out bit;                       --    hash output valid signal one clock advance
108
  clk              : in  std_logic;                       --    master clock signal
109
  rst              : in  std_logic                        --    master reset signal
110
  );
111
end sha256;
112
 
113
architecture phy of sha256 is
114
 
115
  -- citac for 0 to 15
116
  component c4b
117
    port (
118
    cnt            : out bit_vector (3 downto 0);
119
    clk            : std_logic;
120
    rst            : std_logic
121
    );
122
  end component;
123
 
124
  -- citac for 0 to 63
125
  component c6b
126
    port (
127
    cnt            : out bit_vector (5 downto 0);
128
    clk            : std_logic;
129
    rst            : std_logic
130
    );
131
  end component;
132
 
133
  component romk
134
    port (
135
    addr           : in  bit_vector (5 downto 0);
136
    k              : out bit_vector (31 downto 0)
137
    );
138
  end component;
139
 
140
  signal   ih      :     bit_vector ( 31 downto 0);
141
  signal   h0      :     bit_vector ( 31 downto 0);
142
  signal   h1      :     bit_vector ( 31 downto 0);
143
  signal   h2      :     bit_vector ( 31 downto 0);
144
  signal   h3      :     bit_vector ( 31 downto 0);
145
  signal   h4      :     bit_vector ( 31 downto 0);
146
  signal   h5      :     bit_vector ( 31 downto 0);
147
  signal   h6      :     bit_vector ( 31 downto 0);
148
  signal   h7      :     bit_vector ( 31 downto 0);
149
 
150
  signal   k       :     bit_vector ( 31 downto 0);
151
 
152
  signal   im      :     bit_vector ( 31 downto 0);
153
  signal   iw      :     bit_vector ( 31 downto 0);
154
  signal   w       :     bit_vector ( 31 downto 0); -- current working register
155
  signal   w0      :     bit_vector (511 downto 0); -- working register 1
156
 
157
  signal   a       :     bit_vector ( 31 downto 0); -- a register
158
  signal   b       :     bit_vector ( 31 downto 0); -- b register
159
  signal   c       :     bit_vector ( 31 downto 0); -- c register
160
  signal   d       :     bit_vector ( 31 downto 0); -- d register
161
  signal   e       :     bit_vector ( 31 downto 0); -- e register
162
  signal   f       :     bit_vector ( 31 downto 0); -- f register
163
  signal   g       :     bit_vector ( 31 downto 0); -- g register
164
  signal   h       :     bit_vector ( 31 downto 0); -- h register
165
 
166
  signal   f0      :     bit_vector ( 31 downto 0);
167
  signal   f1      :     bit_vector ( 31 downto 0);
168
  signal   f2      :     bit_vector ( 31 downto 0);
169
  signal   f3      :     bit_vector ( 31 downto 0);
170
  signal   f4      :     bit_vector ( 31 downto 0);
171
  signal   f5      :     bit_vector ( 31 downto 0);
172
 
173
  signal   ctr2    :     bit_vector (  3 downto 0); --  4  bit counter (zero to  7)
174
  signal   ctr2_rst:     std_logic;                       -- reset citace
175
  signal   ctr3    :     bit_vector (  5 downto 0); --  6  bit counter (zero to  63)
176
  signal   ctr3_rst:     std_logic;                       -- reset citace
177
 
178
  signal   vld     :     bit;
179
  signal   nld     :     bit;
180
  signal   ild     :     bit;
181
  signal   ild_rst :     bit;
182
 
183
begin
184
 
185
  -- mapovani portu pro 4bitovy citac
186
  ct2: entity work.c4b
187
  port map (
188
  cnt              => ctr2,
189
  clk              => clk,
190
  rst              => ctr2_rst
191
  );
192
 
193
  -- mapovani portu pro 6bitovy citac
194
  ct3: entity work.c6b
195
  port map (
196
  cnt              => ctr3,
197
  clk              => clk,
198
  rst              => ctr3_rst
199
  );
200
 
201
  rom0: entity work.romk
202
  port map (
203
  addr             => ctr3,
204
  k                => k
205
  );
206
 
207
 
208
  -- f0(e, f, g)
209
  f0 <= ((e and f) xor (not(e) and g));
210
  -- f1(a, b, c)
211
  f1 <= ((a and b) xor (a and c) xor (b and c));
212
  -- f2(a)
213
  f2 <= (a(1 downto 0) & a(31 downto 2)) xor (a(12 downto 0) & a(31 downto 13)) xor (a(21 downto 0) & a(31 downto  22));
214
  -- f3(e)
215
  f3 <= (e(5 downto 0) & e(31 downto 6)) xor (e(10 downto 0) & e(31 downto  11)) xor (e(24 downto 0) & e(31 downto 25));
216
  -- w0(479 downto 448)
217
  f4 <= (w0(454 downto 448) & w0(479 downto 455)) xor (w0(465 downto 448) & w0(479 downto 466)) xor (B"000" & w0(479 downto 451));
218
  -- w0( 63 downto  32)
219
  f5 <= (w0(48 downto 32) & w0(63 downto 49)) xor (w0(50 downto 32) & w0(63 downto 51)) xor (B"0000000000" & w0(63 downto 42));
220
 
221
  with ctr2( 2 downto 0) select -- omit bit 4
222
  ih               <= h0                                      when B"000",
223
                      h1                                      when B"001",
224
                      h2                                      when B"010",
225
                      h3                                      when B"011",
226
                      h4                                      when B"100",
227
                      h5                                      when B"101",
228
                      h6                                      when B"110",
229
                      h7                                      when B"111";
230
 
231
  iw <= to_bitvector(std_logic_vector(unsigned(to_stdlogicvector(f5)) + unsigned(to_stdlogicvector(w0(223 downto 192))) + unsigned(to_stdlogicvector(f4)) + unsigned(to_stdlogicvector(w0(511 downto 480)))));
232
 
233
  -- proces zpracovavajici nacitani dat pro hashovani
234
  process (clk)
235
  begin
236
    if ((clk = '1') and clk'event) then
237
      -- pokud je pozadovan reset dojde k vynulovani pracovniho registru w a w0
238
      if (rst = '1') then
239
        w <= (others => '0');
240
        w0 <= (others => '0');
241
      -- 0 <= t <= 15 first 512 bit block   
242
      elsif (nld = '1') then
243
        w <= im;
244
        w0(511 downto 0) <= (w0(479 downto  0) & im);
245
      else
246
        w <= iw(31 downto 0);
247
        w0(511 downto 0) <= (w0(479 downto 0) & iw(31 downto 0));
248
      end if;
249
    end if;
250
  end process;
251
 
252
  process (clk)
253
  begin
254
    if ((clk = '1') and clk'event) then
255
                -- pokud dojde k resetu nuluji
256
      if (rst = '1') then
257
        ild <= '0';
258
              nld <= '0';
259
              im <= (others => '0');
260
      else
261
        ild <= nld;
262
              nld <= ld;        -- nld je nastaven dle ld
263
              im <= m;  -- do im se nacte 32bitu vstupnich dat
264
      end if;
265
    end if;
266
  end process;
267
 
268
  process (clk)
269
  begin
270
    if ((clk = '1') and clk'event) then
271
      if ((ild_rst or to_bit(rst)) = '1') then
272
        vld <= '0';
273
      elsif (ctr3 = B"111111") then
274
        vld <=  '1';
275
      else
276
        vld <=  '0';
277
      end if;
278
    end if;
279
  end process;
280
 
281
  ild_rst          <= (ild xor ld) and ld;
282
  ctr2_rst         <=  to_stdulogic((ild_rst or to_bit(rst) or vld or not(ctr2(3) or not(ctr2(2)) or not(ctr2(1)) or not(ctr2(0)))));
283
  ctr3_rst         <=  to_stdulogic(ild_rst or to_bit(rst));-- (ctr3 = B"010011");          -- set to count to 63 ( 64 clock)
284
 
285
  process (clk)
286
  begin
287
    if ((clk = '1') and clk'event) then
288
      -- pokud se jedna o prvni pruchod inicializuji se registry (init = 1 pouze v prvnim taktu)
289
      if (init = '1') or (rst = '1') then
290
        h0 <= X"6a09e667";
291
        h1 <= X"bb67ae85";
292
        h2 <= X"3c6ef372";
293
        h3 <= X"a54ff53a";
294
        h4 <= X"510e527f";
295
        h5 <= X"9b05688c";
296
        h6 <= X"1f83d9ab";
297
        h7 <= X"5be0cd19";
298
                 -- h0 <= X"6a09e667";
299
       -- h1 <= X"bb67ae85";
300
       -- h2 <= X"3c6ef372";
301
       -- h3 <= X"a54ff53a";
302
       -- h4 <= X"510e527f";
303
       -- h5 <= X"9b05688c";
304
       -- h6 <= X"1f83d9ab";
305
       -- h7 <= X"5be0cd19";
306
      -- pokud se nejedna o prvni pruchod  
307
      elsif (vld = '1') then
308
        h0 <= to_bitvector(std_logic_vector( unsigned(to_stdlogicvector(a)) + unsigned(to_stdlogicvector(h0)) )); -- h0 <= a + h0;
309
        h1 <= to_bitvector(std_logic_vector( unsigned(to_stdlogicvector(b)) + unsigned(to_stdlogicvector(h1)) )); -- h1 <= b + h1;
310
        h2 <= to_bitvector(std_logic_vector( unsigned(to_stdlogicvector(c)) + unsigned(to_stdlogicvector(h2)) )); -- h2 <= c + h2;
311
        h3 <= to_bitvector(std_logic_vector( unsigned(to_stdlogicvector(d)) + unsigned(to_stdlogicvector(h3)) )); -- h3 <= d + h3;
312
        h4 <= to_bitvector(std_logic_vector( unsigned(to_stdlogicvector(e)) + unsigned(to_stdlogicvector(h4)) )); -- h4 <= e + h4;
313
        h5 <= to_bitvector(std_logic_vector( unsigned(to_stdlogicvector(f)) + unsigned(to_stdlogicvector(h5)) )); -- h5 <= f + h5;
314
        h6 <= to_bitvector(std_logic_vector( unsigned(to_stdlogicvector(g)) + unsigned(to_stdlogicvector(h6)) )); -- h6 <= g + h6;
315
        h7 <= to_bitvector(std_logic_vector( unsigned(to_stdlogicvector(h)) + unsigned(to_stdlogicvector(h7)) )); -- h7 <= h + h7;
316
      end if;
317
    end if;
318
  end process;
319
 
320
  process (clk)
321
  begin
322
    if ((clk = '1') and clk'event) then
323
         -- inicializace registru na pocatecni hodnoty pri resetu nebo pri...?
324
      if ((ild_rst or to_bit(rst)) = '1') then
325
        a <= h0;
326
        b <= h1;
327
        c <= h2;
328
        d <= h3;
329
        e <= h4;
330
        f <= h5;
331
        g <= h6;
332
        h <= h7;
333
       else
334
        h <=  g;
335
        g <=  f;
336
        f <=  e;
337
        e <= to_bitvector(std_logic_vector( unsigned(to_stdlogicvector(d)) + unsigned(to_stdlogicvector(h)) + unsigned(to_stdlogicvector(f3)) + unsigned(to_stdlogicvector(f0)) + unsigned(to_stdlogicvector(k)) + unsigned(to_stdlogicvector(w)) ));
338
        d <=  c;
339
        c <=  b;
340
        b <=  a;
341
        a <= to_bitvector(std_logic_vector( unsigned(to_stdlogicvector(h)) + unsigned(to_stdlogicvector(f3)) + unsigned(to_stdlogicvector(f0)) + unsigned(to_stdlogicvector(k)) + unsigned(to_stdlogicvector(w))  + unsigned(to_stdlogicvector(f2)) + unsigned(to_stdlogicvector(f1)) ));
342
      end if;
343
    end if;
344
  end process;
345
 
346
  md <=  ih;
347
  v <=  vld;
348
 
349
end phy;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.