tb_drygascon128.v 6.8 KB
Newer Older
lwc-tester committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
`timescale 1ns / 1ps
`default_nettype none
`define assert(signal, value) \
        if (signal !== value) begin \
            $display("%t ASSERTION FAILED in %m: %x != %x",$realtime,signal,value); \
            #100; \
            $finish; \
        end

module tb_drygascon128();

//`define func_path u_dut

`ifndef func_path
`define func_path tb_drygascon128
`include "drygascon128_functions.v"
`endif

reg clk;
reg clk_en;
reg reset;
reg [31:0] din;
reg [3:0] ds;
reg wr_i;
reg wr_c;
reg wr_x;
reg [3:0] rounds;
reg start;
reg rd_r;
reg rd_c;
wire [31:0] dout;
wire idle;

drygascon128 u_dut(
    .clk(clk),
    .clk_en(clk_en),
    .rst(reset),
    .din(din),
    .ds(ds),
    .wr_i(wr_i),
    .wr_c(wr_c),
    .wr_x(wr_x),
    .rounds(rounds),
    .start(start),
    .rd_r(rd_r),
    .rd_c(rd_c),
    .dout(dout),
    .idle(idle)
);

wire [63:0] c0 = u_dut.c[0*64+:64];
wire [63:0] c1 = u_dut.c[1*64+:64];
wire [63:0] c2 = u_dut.c[2*64+:64];
wire [63:0] c3 = u_dut.c[3*64+:64];
wire [63:0] c4 = u_dut.c[4*64+:64];

reg [320-1:0] c_out;
reg [128-1:0] r_out;

wire done = idle;


task wait_clock;
begin
    @ (negedge clk);
end
endtask

task reset_dut;
begin
    wait_clock();
    reset = 1;
    wait_clock();
    reset = 0;
end
endtask

task wait_clocks;
    input integer nclocks;
    integer i;
begin
    for(i=0;i < nclocks; i= i+1) begin
        wait_clock();
    end
end
endtask

task start_op;
begin
    start = 1;
    @(negedge clk);
    start = 0;
end
endtask

reg done_reg;
always @(posedge clk) done_reg <= done;

task wait_done;
begin
    wait_clock();
    while(!done) begin
        wait_clock();
    end
end
endtask

task write_x;
    input [128-1:0] x;
    integer i;
begin
    wr_x = 1;
    for(i=0;i<4;i=i+1) begin
        din = x[i*32+:32];
        wait_clock();
    end
    wr_x=0;
end
endtask

task write_x_le;
    input [128-1:0] x;
begin
    write_x(`func_path.le_to_int128(x));
end
endtask

task write_c;
    input [320-1:0] c;
    integer i;
begin
    wr_c = 1;
    for(i=0;i<10;i=i+1) begin
        din = c[i*32+:32];
        wait_clock();
    end
    wr_c=0;
end
endtask

task write_c_le;
    input [320-1:0] c;
begin
    write_c(`func_path.le_to_int(c));
end
endtask

task read_c;
    integer i;
begin
    rd_c = 1;
    for(i=0;i<10;i=i+1) begin
        wait_clock();
        c_out[i*32+:32] = dout;
    end
    rd_c=0;
end
endtask

task check_c;
    input [320-1:0] expected;
begin
    read_c();
    `assert(c_out,expected)
end
endtask

task check_c_le;
    input [320-1:0] expected;
begin
    check_c(`func_path.le_to_int(expected));
end
endtask

task write_i;
    input [128-1:0] di;
    integer i;
begin
    wr_i = 1;
    for(i=0;i<4;i=i+1) begin
        din = di[i*32+:32];
        wait_clock();
    end
    wr_i=0;
end
endtask

task write_i_le;
    input [128-1:0] i;
begin
    write_i(`func_path.le_to_int128(i));
end
endtask


task read_r;
    integer i;
begin
    rd_r = 1;
    for(i=0;i<4;i=i+1) begin
        wait_clock();
        r_out[i*32+:32] = dout;
    end
    rd_r=0;
end
endtask

task check_r;
    input [128-1:0] expected;
begin
    read_r();
    `assert(r_out,expected)
end
endtask

task check_r_le;
    input [128-1:0] expected;
begin
    check_r(`func_path.le_to_int128(expected));
end
endtask

task gascon128_f;
    input [3:0] ds_in;
    input [3:0] rounds_in;
    input [128-1:0] i;
begin
rounds=rounds_in;
ds = ds_in;
write_i(i);
start_op();
wait_done();
end
endtask

task gascon128_g;
    input [3:0] rounds_in;
begin
rounds=rounds_in;
start_op();
wait_done();
end
endtask

task gascon128_f_le;
    input [3:0] ds_in;
    input [3:0] rounds_in;
    input [128-1:0] i;
begin
gascon128_f(ds_in,rounds_in,`func_path.le_to_int128(i));
end
endtask

task check_gascon128_f_le;
    input [3:0] ds_in;
    input [3:0] rounds_in;
    input [128-1:0] i;
    input [128-1:0] expected;
begin
gascon128_f_le(ds_in,rounds_in,i);
check_r_le(expected);
end
endtask

task check_gascon128_g_le;
    input [3:0] rounds_in;
    input [128-1:0] expected;
begin
gascon128_g(rounds_in);
check_r_le(expected);
end
endtask

initial begin
clk = 0;
forever #5 clk = ~clk;
end

`define DRYGASCON128_IROUNDS 11
`define DRYGASCON128_ROUNDS   7

task init_dut;
begin
start = 0;
clk_en = 1;
wr_c=0;
wr_x=0;
wr_i=0;
rd_c=0;
rd_r=0;
ds=0;
rounds=11;
reset_dut();
end
endtask

task basic_test;
begin
    init_dut();
    write_x_le(128'h28292A2B2C2D2E2F3031323334353637);
    `assert(u_dut.x,`func_path.le_to_int128(128'h28292A2B2C2D2E2F3031323334353637))
    write_c_le(320'h000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F2021222324252627);
    `assert(u_dut.c,`func_path.le_to_int(320'h000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F2021222324252627))
    check_c_le(320'h000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F2021222324252627);
    check_gascon128_f_le(6,`DRYGASCON128_IROUNDS,128'hF0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF,128'hF1FBA3D719B00A49BF170F832EB7649F);
    write_c_le(320'h000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F2021222324252627);
    check_gascon128_f_le(6,`DRYGASCON128_IROUNDS,128'hF0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF,128'hF1FBA3D719B00A49BF170F832EB7649F);
end
endtask

`define PAD 1
`define FINAL 1
`define DS_S 2
`define DS_D 1
`define DS_A 2
`define DS_M 3

task init_hash;
begin
    init_dut();
    write_x_le(128'hA4093822299F31D0082EFA98EC4E6C89);
    `assert(u_dut.x,`func_path.le_to_int128(128'hA4093822299F31D0082EFA98EC4E6C89))
    write_c_le(320'h243F6A8885A308D313198A2E03707344243F6A8885A308D313198A2E03707344243F6A8885A308D3);
    `assert(u_dut.c,`func_path.le_to_int(320'h243F6A8885A308D313198A2E03707344243F6A8885A308D313198A2E03707344243F6A8885A308D3))
    check_c_le(320'h243F6A8885A308D313198A2E03707344243F6A8885A308D313198A2E03707344243F6A8885A308D3);
end
endtask

task hash_null;
begin
    init_hash();
    check_gascon128_f_le(`func_path.compute_ds(`PAD,`DS_S,`FINAL),`DRYGASCON128_ROUNDS,128'h01000000000000000000000000000000,128'h1EDC77386E20A37C721D6E77ADABB9C4);
    check_gascon128_g_le(`DRYGASCON128_ROUNDS,128'h830F199F5ED25284A13C1D84B9FC257A);
end
endtask

task hash_detailed_tv;
begin
    init_hash();
    check_gascon128_f_le(`func_path.compute_ds(`PAD,`DS_S,`FINAL),`DRYGASCON128_ROUNDS,128'h00010203040506070100000000000000,128'hCDE2DEE0235345CBFA51EC2CE5743571);
    check_gascon128_g_le(`DRYGASCON128_ROUNDS,128'h8EC0133EC2756E035FA404C1CE511E24);
end
endtask

task hash_tv17;
begin
    init_hash();
    gascon128_f_le(0,`DRYGASCON128_ROUNDS,128'h000102030405060708090A0B0C0D0E0F);
    check_gascon128_f_le(`func_path.compute_ds(`PAD,`DS_S,`FINAL),`DRYGASCON128_ROUNDS,128'h10010000000000000000000000000000,128'h20CDB78974D692100612978096CCFE82);
    check_gascon128_g_le(`DRYGASCON128_ROUNDS,128'hE39F15969F493FAD8FA870F93B7252EA);
end
endtask

initial begin
    basic_test();
    hash_null();
    hash_detailed_tv();
    hash_tv17();
    $display("sim pass");
    $finish();
end

endmodule
`default_nettype wire