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

Subversion Repositories sha3

[/] [sha3/] [trunk/] [low_throughput_core/] [rtl/] [round.v] - Blame information for rev 6

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 6 homer.hsin
/*
2
 * Copyright 2013, Homer Hsing <homer.hsing@gmail.com>
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 * http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
 
17
`define low_pos(x,y)        `high_pos(x,y) - 63
18
`define high_pos(x,y)       1599 - 64*(5*y+x)
19
`define add_1(x)            (x == 4 ? 0 : x + 1)
20
`define add_2(x)            (x == 3 ? 0 : x == 4 ? 1 : x + 2)
21
`define sub_1(x)            (x == 0 ? 4 : x - 1)
22
`define rot_up(in, n)       {in[63-n:0], in[63:63-n+1]}
23
`define rot_up_1(in)        {in[62:0], in[63]}
24
 
25
module round(in, round_const, out);
26
    input  [1599:0] in;
27
    input  [63:0]   round_const;
28
    output [1599:0] out;
29
 
30
    wire   [63:0]   a[4:0][4:0];
31
    wire   [63:0]   b[4:0];
32
    wire   [63:0]   c[4:0][4:0], d[4:0][4:0], e[4:0][4:0], f[4:0][4:0], g[4:0][4:0];
33
 
34
    genvar x, y;
35
 
36
    /* assign "a[x][y][z] == in[w(5y+x)+z]" */
37
    generate
38
      for(y=0; y<5; y=y+1)
39
        begin : L0
40
          for(x=0; x<5; x=x+1)
41
            begin : L1
42
              assign a[x][y] = in[`high_pos(x,y) : `low_pos(x,y)];
43
            end
44
        end
45
    endgenerate
46
 
47
    /* calc "b[x] == a[x][0] ^ a[x][1] ^ ... ^ a[x][4]" */
48
    generate
49
      for(x=0; x<5; x=x+1)
50
        begin : L2
51
          assign b[x] = a[x][0] ^ a[x][1] ^ a[x][2] ^ a[x][3] ^ a[x][4];
52
        end
53
    endgenerate
54
 
55
    /* calc "c == theta(a)" */
56
    generate
57
      for(y=0; y<5; y=y+1)
58
        begin : L3
59
          for(x=0; x<5; x=x+1)
60
            begin : L4
61
              assign c[x][y] = a[x][y] ^ b[`sub_1(x)] ^ `rot_up_1(b[`add_1(x)]);
62
            end
63
        end
64
    endgenerate
65
 
66
    /* calc "d == rho(c)" */
67
    assign d[0][0] = c[0][0];
68
    assign d[1][0] = `rot_up_1(c[1][0]);
69
    assign d[2][0] = `rot_up(c[2][0], 62);
70
    assign d[3][0] = `rot_up(c[3][0], 28);
71
    assign d[4][0] = `rot_up(c[4][0], 27);
72
    assign d[0][1] = `rot_up(c[0][1], 36);
73
    assign d[1][1] = `rot_up(c[1][1], 44);
74
    assign d[2][1] = `rot_up(c[2][1], 6);
75
    assign d[3][1] = `rot_up(c[3][1], 55);
76
    assign d[4][1] = `rot_up(c[4][1], 20);
77
    assign d[0][2] = `rot_up(c[0][2], 3);
78
    assign d[1][2] = `rot_up(c[1][2], 10);
79
    assign d[2][2] = `rot_up(c[2][2], 43);
80
    assign d[3][2] = `rot_up(c[3][2], 25);
81
    assign d[4][2] = `rot_up(c[4][2], 39);
82
    assign d[0][3] = `rot_up(c[0][3], 41);
83
    assign d[1][3] = `rot_up(c[1][3], 45);
84
    assign d[2][3] = `rot_up(c[2][3], 15);
85
    assign d[3][3] = `rot_up(c[3][3], 21);
86
    assign d[4][3] = `rot_up(c[4][3], 8);
87
    assign d[0][4] = `rot_up(c[0][4], 18);
88
    assign d[1][4] = `rot_up(c[1][4], 2);
89
    assign d[2][4] = `rot_up(c[2][4], 61);
90
    assign d[3][4] = `rot_up(c[3][4], 56);
91
    assign d[4][4] = `rot_up(c[4][4], 14);
92
 
93
    /* calc "e == pi(d)" */
94
    assign e[0][0] = d[0][0];
95
    assign e[0][2] = d[1][0];
96
    assign e[0][4] = d[2][0];
97
    assign e[0][1] = d[3][0];
98
    assign e[0][3] = d[4][0];
99
    assign e[1][3] = d[0][1];
100
    assign e[1][0] = d[1][1];
101
    assign e[1][2] = d[2][1];
102
    assign e[1][4] = d[3][1];
103
    assign e[1][1] = d[4][1];
104
    assign e[2][1] = d[0][2];
105
    assign e[2][3] = d[1][2];
106
    assign e[2][0] = d[2][2];
107
    assign e[2][2] = d[3][2];
108
    assign e[2][4] = d[4][2];
109
    assign e[3][4] = d[0][3];
110
    assign e[3][1] = d[1][3];
111
    assign e[3][3] = d[2][3];
112
    assign e[3][0] = d[3][3];
113
    assign e[3][2] = d[4][3];
114
    assign e[4][2] = d[0][4];
115
    assign e[4][4] = d[1][4];
116
    assign e[4][1] = d[2][4];
117
    assign e[4][3] = d[3][4];
118
    assign e[4][0] = d[4][4];
119
 
120
    /* calc "f = chi(e)" */
121
    generate
122
      for(y=0; y<5; y=y+1)
123
        begin : L5
124
          for(x=0; x<5; x=x+1)
125
            begin : L6
126
              assign f[x][y] = e[x][y] ^ ((~ e[`add_1(x)][y]) & e[`add_2(x)][y]);
127
            end
128
        end
129
    endgenerate
130
 
131
    /* calc "g = iota(f)" */
132
    generate
133
      for(x=0; x<64; x=x+1)
134
        begin : L60
135
          if(x==0 || x==1 || x==3 || x==7 || x==15 || x==31 || x==63)
136
            assign g[0][0][x] = f[0][0][x] ^ round_const[x];
137
          else
138
            assign g[0][0][x] = f[0][0][x];
139
        end
140
    endgenerate
141
 
142
    generate
143
      for(y=0; y<5; y=y+1)
144
        begin : L7
145
          for(x=0; x<5; x=x+1)
146
            begin : L8
147
              if(x!=0 || y!=0)
148
                assign g[x][y] = f[x][y];
149
            end
150
        end
151
    endgenerate
152
 
153
    /* assign "out[w(5y+x)+z] == out_var[x][y][z]" */
154
    generate
155
      for(y=0; y<5; y=y+1)
156
        begin : L99
157
          for(x=0; x<5; x=x+1)
158
            begin : L100
159
              assign out[`high_pos(x,y) : `low_pos(x,y)] = g[x][y];
160
            end
161
        end
162
    endgenerate
163
endmodule
164
 
165
`undef low_pos
166
`undef high_pos
167
`undef add_1
168
`undef add_2
169
`undef sub_1
170
`undef rot_up
171
`undef rot_up_1

powered by: WebSVN 2.1.0

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