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

Subversion Repositories securehash256bits

[/] [securehash256bits/] [trunk/] [sha256.vhd] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 feketebv
LIBRARY IEEE;
2
use IEEE.std_logic_1164.all;
3
--use IEEE.numeric_std.all;
4
use IEEE.std_logic_unsigned.all;
5
 
6
ENTITY sha256forBTC is
7
    Port ( reset        : in  STD_LOGIC;
8
           clock        : in  STD_LOGIC;
9
           --data input signals
10
           data         : in  STD_LOGIC_VECTOR (511 downto 0);
11
           enable : in  STD_LOGIC;
12
           busy   : out STD_LOGIC;
13
           --hash output signals
14
           digest       : out STD_LOGIC_VECTOR (255 downto 0);
15
           ready        : out STD_LOGIC);
16
end sha256forBTC;
17
 
18
ARCHITECTURE Behavioral of sha256forBTC is
19
 
20
   type hT is array (0 to 7) of STD_LOGIC_VECTOR(31 downto 0);
21
   constant hInit : hT :=
22
     (x"6a09e667", x"bb67ae85", x"3c6ef372", x"a54ff53a", x"510e527f", x"9b05688c", x"1f83d9ab", x"5be0cd19");
23
   type kT is array (0 to 63) of STD_LOGIC_VECTOR(31 downto 0);
24
   constant k : kT :=
25
     (x"428a2f98", x"71374491", x"b5c0fbcf", x"e9b5dba5", x"3956c25b", x"59f111f1", x"923f82a4", x"ab1c5ed5",
26
      x"d807aa98", x"12835b01", x"243185be", x"550c7dc3", x"72be5d74", x"80deb1fe", x"9bdc06a7", x"c19bf174",
27
      x"e49b69c1", x"efbe4786", x"0fc19dc6", x"240ca1cc", x"2de92c6f", x"4a7484aa", x"5cb0a9dc", x"76f988da",
28
      x"983e5152", x"a831c66d", x"b00327c8", x"bf597fc7", x"c6e00bf3", x"d5a79147", x"06ca6351", x"14292967",
29
      x"27b70a85", x"2e1b2138", x"4d2c6dfc", x"53380d13", x"650a7354", x"766a0abb", x"81c2c92e", x"92722c85",
30
      x"a2bfe8a1", x"a81a664b", x"c24b8b70", x"c76c51a3", x"d192e819", x"d6990624", x"f40e3585", x"106aa070",
31
      x"19a4c116", x"1e376c08", x"2748774c", x"34b0bcb5", x"391c0cb3", x"4ed8aa4a", x"5b9cca4f", x"682e6ff3",
32
      x"748f82ee", x"78a5636f", x"84c87814", x"8cc70208", x"90befffa", x"a4506ceb", x"bef9a3f7", x"c67178f2");
33
   signal a,b,c,d,e,f,g,h,h0,h1,h2,h3,h4,h5,h6,h7,s0,s1,su0,su1,maj,ch,temp1,temp2 : STD_LOGIC_VECTOR(31 downto 0);
34
   type wT is array (15 downto 0) of STD_LOGIC_VECTOR(31 downto 0);
35
   signal w : wT;
36
   signal wCNT, chunkCNT : STD_LOGIC_VECTOR(6 downto 0);
37
   signal intEnable : STD_LOGIC;
38
 
39
BEGIN
40
 
41
   fsm: process(clock)
42
   begin
43
      if rising_edge(clock) then
44
         if reset = '1' then
45
            chunkCNT <= "1000000";
46
         else
47
            if chunkCNT = "1000000" then
48
               if enable = '1' then
49
                  chunkCNT <= "0000000";
50
               end if;
51
            else
52
               chunkCNT <= chunkCNT + '1';
53
            end if;
54
         end if;
55
      ready <= not intEnable;
56
      end if;
57
   end process;
58
 
59
   intEnable <= not chunkCNT(6);
60
   busy <= intEnable;
61
 
62
   extension_pipe: process(clock)
63
   begin
64
      if rising_edge(clock) then
65
         if enable = '1' then
66
            w(0) <= data(31 downto 0);
67
            w(1) <= data(63 downto 32);
68
            w(2) <= data(95 downto 64);
69
            w(3) <= data(127 downto 96);
70
            w(4) <= data(159 downto 128);
71
            w(5) <= data(191 downto 160);
72
            w(6) <= data(223 downto 192);
73
            w(7) <= data(255 downto 224);
74
            w(8) <= data(287 downto 256);
75
            w(9) <= data(319 downto 288);
76
            w(10) <= data(351 downto 320);
77
            w(11) <= data(383 downto 352);
78
            w(12) <= data(415 downto 384);
79
            w(13) <= data(447 downto 416);
80
            w(14) <= data(479 downto 448);
81
            w(15) <= data(511 downto 480);
82
         elsif intEnable = '1' then
83
            w <= w(14 downto 0) & (w(15) + s0 + w(6) + s1);
84
         end if;
85
      end if;
86
   end process;
87
   --extension_pipe asynchron circuitry
88
   s0 <= (w(14)(6 downto 0) & w(14)(31 downto 7)) xor (w(14)(17 downto 0) & w(14)(31 downto 18)) xor ("000" & w(14)(31 downto 3));
89
   s1 <= (w(1)(16 downto 0) & w(1)(31 downto 17)) xor (w(1)(18 downto 0) & w(1)(31 downto 19)) xor ("0000000000" & w(1)(31 downto 10));
90
   --end of extension_pipe asynchron circuitry
91
 
92
   main_loop_pipe: process(clock)
93
   begin
94
      if rising_edge(clock) then
95
         if reset = '1' then
96
            a <= hInit(0);
97
            b <= hInit(1);
98
            c <= hInit(2);
99
            d <= hInit(3);
100
            e <= hInit(4);
101
            f <= hInit(5);
102
            g <= hInit(6);
103
            h <= hInit(7);
104
         elsif intEnable = '0' then
105
            a <= h0 + a;
106
            b <= h1 + b;
107
            c <= h2 + c;
108
            d <= h3 + d;
109
            e <= h4 + e;
110
            f <= h5 + f;
111
            g <= h6 + g;
112
            h <= h7 + h;
113
         else
114
            h <= g;
115
            g <= f;
116
            f <= e;
117
            e <= d + temp1;
118
            d <= c;
119
            c <= b;
120
            b <= a;
121
            a <= temp2;
122
         end if;
123
      end if;
124
   end process;
125
   --main_loop_pipe asynchron circuitry
126
   su1 <= (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));
127
   ch <= (e and f) xor ((not e) and g);
128
   temp1 <= h + su1 + ch + k(CONV_INTEGER(chunkCNT(5 downto 0))) + w(15);
129
   su0 <= (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));
130
   maj <= (a and (b xor c)) xor (b and c);
131
   temp2 <= temp1 + su0 + maj;
132
   --end of main_loop_pipe asynchron circuitry
133
 
134
   add_hash_chunk: process(clock)
135
   begin
136
      if rising_edge(clock) then
137
         if reset = '1' then
138
            h0 <= x"00000000";
139
            h1 <= x"00000000";
140
            h2 <= x"00000000";
141
            h3 <= x"00000000";
142
            h4 <= x"00000000";
143
            h5 <= x"00000000";
144
            h6 <= x"00000000";
145
            h7 <= x"00000000";
146
         else
147
            if intEnable = '0' then
148
               h0 <= h0 + a;
149
               h1 <= h1 + b;
150
               h2 <= h2 + c;
151
               h3 <= h3 + d;
152
               h4 <= h4 + e;
153
               h5 <= h5 + f;
154
               h6 <= h6 + g;
155
               h7 <= h7 + h;
156
            end if;
157
         end if;
158
      end if;
159
   end process;
160
 
161
   digest <= h0 & h1 & h2 & h3 & h4 & h5 & h6 & h7;
162
 
163
end Behavioral;

powered by: WebSVN 2.1.0

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