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

Subversion Repositories sha3

[/] [sha3/] [trunk/] [low_throughput_core/] [testbench/] [test_keccak.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
`timescale 1ns / 1ps
18
`define P 20
19
 
20
module test_keccak;
21
 
22
    // Inputs
23
    reg clk;
24
    reg reset;
25
    reg [31:0] in;
26
    reg in_ready;
27
    reg is_last;
28
    reg [1:0] byte_num;
29
 
30
    // Outputs
31
    wire buffer_full;
32
    wire [511:0] out;
33
    wire out_ready;
34
 
35
    // Var
36
    integer i;
37
 
38
    // Instantiate the Unit Under Test (UUT)
39
    keccak uut (
40
        .clk(clk),
41
        .reset(reset),
42
        .in(in),
43
        .in_ready(in_ready),
44
        .is_last(is_last),
45
        .byte_num(byte_num),
46
        .buffer_full(buffer_full),
47
        .out(out),
48
        .out_ready(out_ready)
49
    );
50
 
51
    initial begin
52
        // Initialize Inputs
53
        clk = 0;
54
        reset = 0;
55
        in = 0;
56
        in_ready = 0;
57
        is_last = 0;
58
        byte_num = 0;
59
 
60
        // Wait 100 ns for global reset to finish
61
        #100;
62
 
63
        // Add stimulus here
64
        @ (negedge clk);
65
 
66
        // SHA3-512("The quick brown fox jumps over the lazy dog")
67
        reset = 1; #(`P); reset = 0;
68
        in_ready = 1; is_last = 0;
69
        in = "The "; #(`P);
70
        in = "quic"; #(`P);
71
        in = "k br"; #(`P);
72
        in = "own "; #(`P);
73
        in = "fox "; #(`P);
74
        in = "jump"; #(`P);
75
        in = "s ov"; #(`P);
76
        in = "er t"; #(`P);
77
        in = "he l"; #(`P);
78
        in = "azy "; #(`P);
79
        in = "dog "; byte_num = 3; is_last = 1; #(`P); /* !!! not in = "dog" */
80
        in_ready = 0; is_last = 0;
81
        while (out_ready !== 1)
82
            #(`P);
83
        check(512'hd135bb84d0439dbac432247ee573a23ea7d3c9deb2a968eb31d47c4fb45f1ef4422d6c531b5b9bd6f449ebcc449ea94d0a8f05f62130fda612da53c79659f609);
84
 
85
        // SHA3-512("The quick brown fox jumps over the lazy dog.")
86
        reset = 1; #(`P); reset = 0;
87
        in_ready = 1; is_last = 0;
88
        in = "The "; #(`P);
89
        in = "quic"; #(`P);
90
        in = "k br"; #(`P);
91
        in = "own "; #(`P);
92
        in = "fox "; #(`P);
93
        in = "jump"; #(`P);
94
        in = "s ov"; #(`P);
95
        in = "er t"; #(`P);
96
        in = "he l"; #(`P);
97
        in = "azy "; #(`P);
98
        in = "dog."; #(`P);
99
        in = 0; byte_num = 0; is_last = 1; #(`P); /* !!! */
100
        in_ready = 0; is_last = 0;
101
        while (out_ready !== 1)
102
            #(`P);
103
        check(512'hab7192d2b11f51c7dd744e7b3441febf397ca07bf812cceae122ca4ded6387889064f8db9230f173f6d1ab6e24b6e50f065b039f799f5592360a6558eb52d760);
104
 
105
        // hash an string "\xA1\xA2\xA3\xA4\xA5", len == 5
106
        reset = 1; #(`P); reset = 0;
107
        #(7*`P); // wait some cycles
108
        in_ready = 1; is_last = 0; byte_num = 1;
109
        in = 32'hA1A2A3A4;
110
        #(`P);
111
        is_last = 1; byte_num = 1;
112
        in = 32'hA5000000;
113
        #(`P);
114
        in = 32'h12345678; // next input
115
        in_ready = 1;
116
        is_last = 1;
117
        #(`P/2);
118
        if (buffer_full === 1) error; // should be 0
119
        #(`P/2);
120
        in_ready = 0;
121
        is_last = 0;
122
 
123
        while (out_ready !== 1)
124
            #(`P);
125
        check(512'h12f4a85b68b091e8836219e79dfff7eb9594a42f5566515423b2aa4c67c454de83a62989e44b5303022bfe8c1a9976781b747a596cdab0458e20d8750df6ddfb);
126
        for(i=0; i<5; i=i+1)
127
          begin
128
            #(`P);
129
            if (buffer_full !== 0) error; // should keep 0
130
          end
131
 
132
        // hash an empty string, should not eat next input
133
        reset = 1; #(`P); reset = 0;
134
        #(7*`P); // wait some cycles
135
        in = 32'h12345678; // should not be eat
136
        byte_num = 0;
137
        in_ready = 1;
138
        is_last = 1;
139
        #(`P);
140
        in = 32'hddddd; // should not be eat
141
        in_ready = 1; // next input
142
        is_last = 1;
143
        #(`P);
144
        in_ready = 0;
145
        is_last = 0;
146
 
147
        while (out_ready !== 1)
148
            #(`P);
149
        check(512'h0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e);
150
        for(i=0; i<5; i=i+1)
151
          begin
152
            #(`P);
153
            if (buffer_full !== 0) error; // should keep 0
154
          end
155
 
156
        // hash an (576-8) bit string
157
        reset = 1; #(`P); reset = 0;
158
        #(4*`P); // wait some cycles
159
        in_ready = 1;
160
        byte_num = 3; /* should have no effect */
161
        is_last = 0;
162
        for (i=0; i<8; i=i+1)
163
          begin
164
            in = 32'hEFCDAB90; #(`P);
165
            in = 32'h78563412; #(`P);
166
          end
167
        in = 32'hEFCDAB90; #(`P);
168
        in = 32'h78563412; is_last = 1; #(`P);
169
        in_ready = 0;
170
        is_last = 0;
171
        while (out_ready !== 1)
172
            #(`P);
173
        check(512'hf7f6b44069dba8900b6711ffcbe40523d4bb718cc8ed7f0a0bd28a1b18ee9374359f0ca0c9c1e96fcfca29ee2f282b46d5045eff01f7a7549eaa6b652cbf6270);
174
 
175
        // pad an (576-64) bit string
176
        reset = 1; #(`P); reset = 0;
177
        // don't wait any cycle
178
        in_ready = 1;
179
        byte_num = 7; /* should have no effect */
180
        is_last = 0;
181
        for (i=0; i<8; i=i+1)
182
          begin
183
            in = 32'hEFCDAB90; #(`P);
184
            in = 32'h78563412; #(`P);
185
          end
186
        is_last = 1;
187
        byte_num = 0;
188
        #(`P);
189
        in_ready = 0;
190
        is_last = 0;
191
        in = 0;
192
        while (out_ready !== 1)
193
            #(`P);
194
        check(512'hccd91653872c106f6eea1b8b68a4c2901c8d9bed9c180201f8a6144e7e6e6c251afcb6f6da44780b2d9aabff254036664719425469671f7e21fb67e5280a27ed);
195
 
196
        // pad an (576*2-16) bit string
197
        reset = 1; #(`P); reset = 0;
198
        in_ready = 1;
199
        byte_num = 1; /* should have no effect */
200
        is_last = 0;
201
        for (i=0; i<9; i=i+1)
202
          begin
203
            in = 32'hEFCDAB90; #(`P);
204
            in = 32'h78563412; #(`P);
205
          end
206
        #(`P/2);
207
        if (buffer_full !== 1) error; // should not eat
208
        #(`P/2);
209
        in = 32'h999; // should not eat this
210
        in_ready = 0;
211
        #(`P/2);
212
        if (buffer_full !== 0) error; // should not eat, but buffer should not be full
213
        #(`P/2);
214
        #(`P);
215
        // feed next (576-16) bit
216
        in_ready = 1;
217
        for (i=0; i<8; i=i+1)
218
          begin
219
            in = 32'hEFCDAB90; #(`P);
220
            in = 32'h78563412; #(`P);
221
          end
222
        in = 32'hEFCDAB90; #(`P);
223
        byte_num = 2;
224
        is_last = 1;
225
        in = 32'h78563412;
226
        #(`P);
227
        is_last = 0;
228
        in_ready = 0;
229
        while (out_ready !== 1)
230
            #(`P);
231
        check(512'h0f385323604e279251e80f928cfd9ce9492ba5df775063ea106eebe2a2c7785a3e33b4397fca66e90f67470334c66ea12016cb1f06170b9b033f158a7c01933e);
232
 
233
        $display("Good!");
234
        $finish;
235
    end
236
 
237
    always #(`P/2) clk = ~ clk;
238
 
239
    task error;
240
        begin
241
              $display("E");
242
              $finish;
243
        end
244
    endtask
245
 
246
    task check;
247
        input [511:0] wish;
248
        begin
249
          if (out !== wish)
250
            begin
251
              $display("%h %h", out, wish); error;
252
            end
253
        end
254
    endtask
255
endmodule
256
 
257
`undef P

powered by: WebSVN 2.1.0

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