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

Subversion Repositories sha3

[/] [sha3/] [trunk/] [high_throughput_core/] [rtl/] [round2in1.v] - Blame information for rev 7

Go to most recent revision | 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 round2in1(in, round_const_1, round_const_2, out);
26
    input  [1599:0] in;
27
    input  [63:0]   round_const_1, round_const_2;
28
    output [1599:0] out;
29
 
30
    /* "a ~ g" for round 1 */
31
    wire   [63:0]   a[4:0][4:0];
32
    wire   [63:0]   b[4:0];
33
    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];
34
 
35
    /* "aa ~ gg" for round 2 */
36
    wire   [63:0]   bb[4:0];
37
    wire   [63:0]   cc[4:0][4:0], dd[4:0][4:0], ee[4:0][4:0], ff[4:0][4:0], gg[4:0][4:0];
38
 
39
    genvar x, y;
40
 
41
    /* assign "a[x][y][z] == in[w(5y+x)+z]" */
42
    generate
43
      for(y=0; y<5; y=y+1)
44
        begin : L0
45
          for(x=0; x<5; x=x+1)
46
            begin : L1
47
              assign a[x][y] = in[`high_pos(x,y) : `low_pos(x,y)];
48
            end
49
        end
50
    endgenerate
51
 
52
    /* calc "b[x] == a[x][0] ^ a[x][1] ^ ... ^ a[x][4]" */
53
    generate
54
      for(x=0; x<5; x=x+1)
55
        begin : L2
56
          assign b[x] = a[x][0] ^ a[x][1] ^ a[x][2] ^ a[x][3] ^ a[x][4];
57
        end
58
    endgenerate
59
 
60
    /* calc "c == theta(a)" */
61
    generate
62
      for(y=0; y<5; y=y+1)
63
        begin : L3
64
          for(x=0; x<5; x=x+1)
65
            begin : L4
66
              assign c[x][y] = a[x][y] ^ b[`sub_1(x)] ^ `rot_up_1(b[`add_1(x)]);
67
            end
68
        end
69
    endgenerate
70
 
71
    /* calc "d == rho(c)" */
72
    assign d[0][0] = c[0][0];
73
    assign d[1][0] = `rot_up_1(c[1][0]);
74
    assign d[2][0] = `rot_up(c[2][0], 62);
75
    assign d[3][0] = `rot_up(c[3][0], 28);
76
    assign d[4][0] = `rot_up(c[4][0], 27);
77
    assign d[0][1] = `rot_up(c[0][1], 36);
78
    assign d[1][1] = `rot_up(c[1][1], 44);
79
    assign d[2][1] = `rot_up(c[2][1], 6);
80
    assign d[3][1] = `rot_up(c[3][1], 55);
81
    assign d[4][1] = `rot_up(c[4][1], 20);
82
    assign d[0][2] = `rot_up(c[0][2], 3);
83
    assign d[1][2] = `rot_up(c[1][2], 10);
84
    assign d[2][2] = `rot_up(c[2][2], 43);
85
    assign d[3][2] = `rot_up(c[3][2], 25);
86
    assign d[4][2] = `rot_up(c[4][2], 39);
87
    assign d[0][3] = `rot_up(c[0][3], 41);
88
    assign d[1][3] = `rot_up(c[1][3], 45);
89
    assign d[2][3] = `rot_up(c[2][3], 15);
90
    assign d[3][3] = `rot_up(c[3][3], 21);
91
    assign d[4][3] = `rot_up(c[4][3], 8);
92
    assign d[0][4] = `rot_up(c[0][4], 18);
93
    assign d[1][4] = `rot_up(c[1][4], 2);
94
    assign d[2][4] = `rot_up(c[2][4], 61);
95
    assign d[3][4] = `rot_up(c[3][4], 56);
96
    assign d[4][4] = `rot_up(c[4][4], 14);
97
 
98
    /* calc "e == pi(d)" */
99
    assign e[0][0] = d[0][0];
100
    assign e[0][2] = d[1][0];
101
    assign e[0][4] = d[2][0];
102
    assign e[0][1] = d[3][0];
103
    assign e[0][3] = d[4][0];
104
    assign e[1][3] = d[0][1];
105
    assign e[1][0] = d[1][1];
106
    assign e[1][2] = d[2][1];
107
    assign e[1][4] = d[3][1];
108
    assign e[1][1] = d[4][1];
109
    assign e[2][1] = d[0][2];
110
    assign e[2][3] = d[1][2];
111
    assign e[2][0] = d[2][2];
112
    assign e[2][2] = d[3][2];
113
    assign e[2][4] = d[4][2];
114
    assign e[3][4] = d[0][3];
115
    assign e[3][1] = d[1][3];
116
    assign e[3][3] = d[2][3];
117
    assign e[3][0] = d[3][3];
118
    assign e[3][2] = d[4][3];
119
    assign e[4][2] = d[0][4];
120
    assign e[4][4] = d[1][4];
121
    assign e[4][1] = d[2][4];
122
    assign e[4][3] = d[3][4];
123
    assign e[4][0] = d[4][4];
124
 
125
    /* calc "f = chi(e)" */
126
    generate
127
      for(y=0; y<5; y=y+1)
128
        begin : L5
129
          for(x=0; x<5; x=x+1)
130
            begin : L6
131
              assign f[x][y] = e[x][y] ^ ((~ e[`add_1(x)][y]) & e[`add_2(x)][y]);
132
            end
133
        end
134
    endgenerate
135
 
136
    /* calc "g = iota(f)" */
137
    generate
138
      for(x=0; x<64; x=x+1)
139
        begin : L60
140
          if(x==0 || x==1 || x==3 || x==7 || x==15 || x==31 || x==63)
141
            assign g[0][0][x] = f[0][0][x] ^ round_const_1[x];
142
          else
143
            assign g[0][0][x] = f[0][0][x];
144
        end
145
    endgenerate
146
 
147
    generate
148
      for(y=0; y<5; y=y+1)
149
        begin : L7
150
          for(x=0; x<5; x=x+1)
151
            begin : L8
152
              if(x!=0 || y!=0)
153
                assign g[x][y] = f[x][y];
154
            end
155
        end
156
    endgenerate
157
 
158
    /* round 2 */
159
 
160
    /* calc "bb[x] == g[x][0] ^ g[x][1] ^ ... ^ g[x][4]" */
161
    generate
162
      for(x=0; x<5; x=x+1)
163
        begin : L12
164
          assign bb[x] = g[x][0] ^ g[x][1] ^ g[x][2] ^ g[x][3] ^ g[x][4];
165
        end
166
    endgenerate
167
 
168
    /* calc "cc == theta(g)" */
169
    generate
170
      for(y=0; y<5; y=y+1)
171
        begin : L13
172
          for(x=0; x<5; x=x+1)
173
            begin : L14
174
              assign cc[x][y] = g[x][y] ^ bb[`sub_1(x)] ^ `rot_up_1(bb[`add_1(x)]);
175
            end
176
        end
177
    endgenerate
178
 
179
    /* calc "dd == rho(cc)" */
180
    assign dd[0][0] = cc[0][0];
181
    assign dd[1][0] = `rot_up_1(cc[1][0]);
182
    assign dd[2][0] = `rot_up(cc[2][0], 62);
183
    assign dd[3][0] = `rot_up(cc[3][0], 28);
184
    assign dd[4][0] = `rot_up(cc[4][0], 27);
185
    assign dd[0][1] = `rot_up(cc[0][1], 36);
186
    assign dd[1][1] = `rot_up(cc[1][1], 44);
187
    assign dd[2][1] = `rot_up(cc[2][1], 6);
188
    assign dd[3][1] = `rot_up(cc[3][1], 55);
189
    assign dd[4][1] = `rot_up(cc[4][1], 20);
190
    assign dd[0][2] = `rot_up(cc[0][2], 3);
191
    assign dd[1][2] = `rot_up(cc[1][2], 10);
192
    assign dd[2][2] = `rot_up(cc[2][2], 43);
193
    assign dd[3][2] = `rot_up(cc[3][2], 25);
194
    assign dd[4][2] = `rot_up(cc[4][2], 39);
195
    assign dd[0][3] = `rot_up(cc[0][3], 41);
196
    assign dd[1][3] = `rot_up(cc[1][3], 45);
197
    assign dd[2][3] = `rot_up(cc[2][3], 15);
198
    assign dd[3][3] = `rot_up(cc[3][3], 21);
199
    assign dd[4][3] = `rot_up(cc[4][3], 8);
200
    assign dd[0][4] = `rot_up(cc[0][4], 18);
201
    assign dd[1][4] = `rot_up(cc[1][4], 2);
202
    assign dd[2][4] = `rot_up(cc[2][4], 61);
203
    assign dd[3][4] = `rot_up(cc[3][4], 56);
204
    assign dd[4][4] = `rot_up(cc[4][4], 14);
205
 
206
    /* calc "ee == pi(dd)" */
207
    assign ee[0][0] = dd[0][0];
208
    assign ee[0][2] = dd[1][0];
209
    assign ee[0][4] = dd[2][0];
210
    assign ee[0][1] = dd[3][0];
211
    assign ee[0][3] = dd[4][0];
212
    assign ee[1][3] = dd[0][1];
213
    assign ee[1][0] = dd[1][1];
214
    assign ee[1][2] = dd[2][1];
215
    assign ee[1][4] = dd[3][1];
216
    assign ee[1][1] = dd[4][1];
217
    assign ee[2][1] = dd[0][2];
218
    assign ee[2][3] = dd[1][2];
219
    assign ee[2][0] = dd[2][2];
220
    assign ee[2][2] = dd[3][2];
221
    assign ee[2][4] = dd[4][2];
222
    assign ee[3][4] = dd[0][3];
223
    assign ee[3][1] = dd[1][3];
224
    assign ee[3][3] = dd[2][3];
225
    assign ee[3][0] = dd[3][3];
226
    assign ee[3][2] = dd[4][3];
227
    assign ee[4][2] = dd[0][4];
228
    assign ee[4][4] = dd[1][4];
229
    assign ee[4][1] = dd[2][4];
230
    assign ee[4][3] = dd[3][4];
231
    assign ee[4][0] = dd[4][4];
232
 
233
    /* calc "ff = chi(ee)" */
234
    generate
235
      for(y=0; y<5; y=y+1)
236
        begin : L15
237
          for(x=0; x<5; x=x+1)
238
            begin : L16
239
              assign ff[x][y] = ee[x][y] ^ ((~ ee[`add_1(x)][y]) & ee[`add_2(x)][y]);
240
            end
241
        end
242
    endgenerate
243
 
244
    /* calc "gg = iota(ff)" */
245
    generate
246
      for(x=0; x<64; x=x+1)
247
        begin : L160
248
          if(x==0 || x==1 || x==3 || x==7 || x==15 || x==31 || x==63)
249
            assign gg[0][0][x] = ff[0][0][x] ^ round_const_2[x];
250
          else
251
            assign gg[0][0][x] = ff[0][0][x];
252
        end
253
    endgenerate
254
 
255
    generate
256
      for(y=0; y<5; y=y+1)
257
        begin : L17
258
          for(x=0; x<5; x=x+1)
259
            begin : L18
260
              if(x!=0 || y!=0)
261
                assign gg[x][y] = ff[x][y];
262
            end
263
        end
264
    endgenerate
265
 
266
    /* assign "out[w(5y+x)+z] == out_var[x][y][z]" */
267
    generate
268
      for(y=0; y<5; y=y+1)
269
        begin : L99
270
          for(x=0; x<5; x=x+1)
271
            begin : L100
272
              assign out[`high_pos(x,y) : `low_pos(x,y)] = gg[x][y];
273
            end
274
        end
275
    endgenerate
276
endmodule
277
 
278
`undef low_pos
279
`undef high_pos
280
`undef add_1
281
`undef add_2
282
`undef sub_1
283
`undef rot_up
284
`undef rot_up_1

powered by: WebSVN 2.1.0

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