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

Subversion Repositories btc_dsha256

[/] [btc_dsha256/] [trunk/] [rtl/] [vhdl/] [sha256core/] [sha_256_pkg.vhd] - Blame information for rev 2

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 nuxi1209
-- Copyright (c) 2013 VariStream
2
-- Author : Yu Peng 
3
 
4
library IEEE;
5
use IEEE.STD_LOGIC_1164.all;
6
use IEEE.std_logic_arith.all;
7
 
8
package sha_256_pkg is
9
        type tDwordArray is array (natural range<>) of std_logic_vector(31 downto 0);
10
 
11
        function sigma_0 (signal x : in std_logic_vector) return std_logic_vector;
12
        function sigma_1 (signal x : in std_logic_vector) return std_logic_vector;
13
 
14
        function sum_0 (signal x : in std_logic_vector) return std_logic_vector;
15
        function sum_1 (signal x : in std_logic_vector) return std_logic_vector;
16
 
17
        function chi (signal x : in std_logic_vector;
18
                                signal y : in std_logic_vector;
19
                                signal z : in std_logic_vector) return std_logic_vector;
20
 
21
        function maj (signal x : in std_logic_vector;
22
                                signal y : in std_logic_vector;
23
                                signal z : in std_logic_vector) return std_logic_vector;
24
 
25
        function getW_IS_CONST (is_const : in std_logic_vector) return std_logic_vector;
26
        function conv_str_to_msg (str : in string) return tDwordArray;
27
end sha_256_pkg;
28
 
29
package body sha_256_pkg is
30
 
31
function sigma_0 (signal x : in std_logic_vector) return std_logic_vector is
32
        variable tmp_0 : std_logic_vector(31 downto 0);
33
        variable tmp_1 : std_logic_vector(31 downto 0);
34
        variable tmp_2 : std_logic_vector(31 downto 0);
35
        variable r : std_logic_vector(31 downto 0);
36
begin
37
        tmp_0 := x(6 downto 0) & x(31 downto 7);
38
        tmp_1 := x(17 downto 0) & x(31 downto 18);
39
        tmp_2 := "000" & x(31 downto 3);
40
 
41
        r := tmp_0 xor tmp_1 xor tmp_2;
42
 
43
        return r;
44
end sigma_0;
45
 
46
 
47
function sigma_1 (signal x : in std_logic_vector) return std_logic_vector is
48
        variable tmp_0 : std_logic_vector(31 downto 0);
49
        variable tmp_1 : std_logic_vector(31 downto 0);
50
        variable tmp_2 : std_logic_vector(31 downto 0);
51
 
52
        variable r : std_logic_vector(31 downto 0);
53
begin
54
        tmp_0 := x(16 downto 0) & x(31 downto 17);
55
        tmp_1 := x(18 downto 0) & x(31 downto 19);
56
        tmp_2 := "0000000000" & x(31 downto 10);
57
 
58
        r := tmp_0 xor tmp_1 xor tmp_2;
59
 
60
        return r;
61
end sigma_1;
62
 
63
 
64
function chi (signal x : in std_logic_vector;
65
        signal y : in std_logic_vector;
66
        signal z : in std_logic_vector) return std_logic_vector is
67
 
68
    variable r : std_logic_vector(31 downto 0);
69
 
70
begin
71
        r := (x and y) xor (not(x) and z);
72
 
73
        return r;
74
end chi;
75
 
76
 
77
function maj (signal x : in std_logic_vector;
78
        signal y : in std_logic_vector;
79
        signal z : in std_logic_vector) return std_logic_vector is
80
 
81
        variable r : std_logic_vector(31 downto 0);
82
 
83
begin
84
        r := (x and y) xor (x and z) xor (y and z);
85
 
86
        return r;
87
 
88
end maj;
89
 
90
 
91
function sum_0 (signal x : in std_logic_vector) return std_logic_vector is
92
        variable tmp_0 : std_logic_vector(31 downto 0);
93
        variable tmp_1 : std_logic_vector(31 downto 0);
94
        variable tmp_2 : std_logic_vector(31 downto 0);
95
 
96
        variable r : std_logic_vector(31 downto 0);
97
begin
98
        tmp_0 := x(1 downto 0) & x(31 downto 2);
99
        tmp_1 := x(12 downto 0) & x(31 downto 13);
100
        tmp_2 := x(21 downto 0) & x(31 downto 22);
101
 
102
        r := tmp_0 xor tmp_1 xor tmp_2;
103
 
104
        return r;
105
 
106
end sum_0;
107
 
108
 
109
function sum_1 (signal x : in std_logic_vector) return std_logic_vector is
110
        variable tmp_0 : std_logic_vector(31 downto 0);
111
        variable tmp_1 : std_logic_vector(31 downto 0);
112
        variable tmp_2 : std_logic_vector(31 downto 0);
113
 
114
        variable r : std_logic_vector(31 downto 0);
115
begin
116
 
117
        tmp_0 := x(5 downto 0) & x(31 downto 6);
118
        tmp_1 := x(10 downto 0) & x(31 downto 11);
119
        tmp_2 := x(24 downto 0) & x(31 downto 25);
120
 
121
        r := tmp_0 xor tmp_1 xor tmp_2;
122
 
123
        return r;
124
end sum_1;
125
 
126
function getW_IS_CONST (is_const : in std_logic_vector) return std_logic_vector is
127
        variable r : std_logic_vector(0 to 63);
128
begin
129
        for i in 0 to 15 loop
130
                r(i) := is_const(i);
131
        end loop;
132
 
133
        for i in 16 to 63 loop
134
                r(i) := r(i-16) and r(i-15) and r(i-7) and r(i-2);
135
        end loop;
136
 
137
        return r;
138
end getW_IS_CONST;
139
 
140
function conv_str_to_msg (str : in string) return tDwordArray is
141
        variable str_len : integer := str'length;
142
        variable retval : tDwordArray(0 to 15) := (others=>(others=>'0'));
143
begin
144
        if str_len > 0 then
145
                for i in 0 to (str_len - 1) loop
146
                        retval(i / 4)(((4 - (i mod 4)) * 8 - 1) downto ((3 - (i mod 4)) * 8)) := conv_std_logic_vector(character'pos(str(i + 1)), 8);
147
                end loop;
148
        end if;
149
 
150
        retval(str_len / 4)(((4 - (str_len mod 4)) * 8 - 1) downto ((3 - (str_len mod 4)) * 8)) := X"80";
151
        retval(15) := conv_std_logic_vector((str_len * 8), 32);
152
 
153
        return retVal;
154
end conv_str_to_msg;
155
 
156
end sha_256_pkg;

powered by: WebSVN 2.1.0

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