From c8f1c9223fbaf43e3cc436f34043eb3c7fdafd11 Mon Sep 17 00:00:00 2001 From: bsdevlin Date: Wed, 20 Feb 2019 17:16:13 -0500 Subject: [PATCH] Updates to testbench to fix bugs. --- ip_cores/blake2b/src/rtl/blake2b_pipe_top.sv | 30 ++++++------ ip_cores/blake2b/src/tb/blake2b_g_tb.sv | 10 ++-- ip_cores/blake2b/src/tb/blake2b_top_tb.sv | 9 ++-- zcash_verif/src/rtl/zcash_verif_equihash.sv | 48 ++++++++----------- zcash_verif/src/rtl/zcash_verif_pkg.sv | 1 - zcash_verif/src/tb/zcash_verif_equihash_tb.sv | 17 ++++--- 6 files changed, 52 insertions(+), 63 deletions(-) diff --git a/ip_cores/blake2b/src/rtl/blake2b_pipe_top.sv b/ip_cores/blake2b/src/rtl/blake2b_pipe_top.sv index 095af6c..5d117a4 100644 --- a/ip_cores/blake2b/src/rtl/blake2b_pipe_top.sv +++ b/ip_cores/blake2b/src/rtl/blake2b_pipe_top.sv @@ -32,9 +32,9 @@ module blake2b_pipe_top import blake2b_pkg::*; #( // If we fully unfold the pipeline, the message byte length is hard-coded - parameter MSG_LEN, - parameter MSG_VAR_BYTS = MSG_LEN, // Setting this != MSG_LEN will assume only those bytes are changing when fully unrolled - parameter CTL_BITS = 8 + parameter MSG_LEN, + parameter [(8*MSG_LEN)-1:0] MSG_VAR_BM = {MSG_LEN*8{1'b1}}, // A bit mask of what bitds in the message will change, can be used to reduce logic + parameter CTL_BITS = 8 ) ( input i_clk, i_rst, @@ -53,7 +53,7 @@ localparam NUM_PIPE = 2 + NUM_PASSES*(NUM_ROUNDS*2) + 2*NUM_PASSES - 1; logic [NUM_PIPE-1:0][15:0][63:0] v; logic [NUM_PIPE-1:0][7:0][63:0] h; -logic [NUM_PIPE-1:0][MSG_VAR_BYTS*8-1:0] msg; +logic [NUM_PIPE-1:0][MSG_LEN*8-1:0] msg; logic [MSG_LEN*8-1:0] msg_fixed; logic [7:0] byte_len; logic [NUM_PIPE-1:0][CTL_BITS-1:0] ctl; @@ -94,7 +94,8 @@ generate // First stage - depends if we are fully unrolling or not as where input comes from h[0] <= i_parameters ^ blake2b_pkg::IV; v[0] <= 0; - msg[0] <= i_block.dat; + for (int i = 0; i < MSG_LEN*8; i++) + msg[0][i] <= MSG_VAR_BM[i] ? i_block.dat[i] : 1'b0; if (i_block.val) begin msg_fixed <= i_block.dat; byte_len <= i_byte_len; @@ -151,18 +152,14 @@ generate end // Second stage if (o_hash.rdy) begin - // Shift message down either from previous pipeline or from fixed portion if (g0 < (NUM_PASSES - 1)) begin h[PIPE_G0+1] <= h[PIPE_G0]; init_local_work_vector_pipe(PIPE_G0+1, LAST_BLOCK ? byte_len : 128 , LAST_BLOCK); - msg[PIPE_G0+1] <= 0; - for (int i = 0; i < 128; i++) begin - if ((g0+1)*128 + i < MSG_VAR_BYTS) - msg[PIPE_G0+1][i*8 +: 8] <= msg[PIPE_G0][((g0+1)*128 + i)*8 +: 8]; - end + ctl[PIPE_G0+1] <= ctl[PIPE_G0]; end + msg[PIPE_G0+1] <= msg[PIPE_G0]; end end @@ -194,11 +191,12 @@ generate logic [63:0] msg0, msg1; logic [16*64-1:0] msg_; always_comb begin - msg_ = msg[PIPE_G2-1]; - //for (int i = MSG_VAR_BYTS; i < 16*64; i++) - for (int i = 0; i < 128; i++) - if (i + (g0*128) >= MSG_VAR_BYTS) - msg_[i*8 +: 8] = msg_fixed_int[i*8 +: 8]; + msg_ = 0; + for (int i = 0; i < 1024; i++) + if (((i + g0*1024) < MSG_LEN*8) && MSG_VAR_BM[i + (g0*1024)]) + msg_[i] = msg[PIPE_G2-1][i + g0*1024]; + else + msg_[i] = msg_fixed_int[i]; for (int i = 0; i < 8; i ++) begin msg0 = msg_[64*blake2b_pkg::SIGMA[16*(g1%10) + g2*8 + g3*2] +: 64]; msg1 = msg_[64*blake2b_pkg::SIGMA[16*(g1%10) + g2*8 + g3*2 + 1] +: 64]; diff --git a/ip_cores/blake2b/src/tb/blake2b_g_tb.sv b/ip_cores/blake2b/src/tb/blake2b_g_tb.sv index 8a47e82..a6a029d 100644 --- a/ip_cores/blake2b/src/tb/blake2b_g_tb.sv +++ b/ip_cores/blake2b/src/tb/blake2b_g_tb.sv @@ -15,7 +15,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . -*/ +*/ module blake2b_g_tb(); @@ -43,10 +43,10 @@ begin repeat (PIPELINES) @(posedge clk); #1; - assert (o_a == 64'hf0c9aa0de38b1b89) else $fatal(0, "%m %t:ERROR, o_a did not match", $time); - assert (o_b == 64'hbbdf863401fde49b) else $fatal(0, "%m %t:ERROR, o_b did not match", $time); - assert (o_c == 64'he85eb23c42183d3d) else $fatal(0, "%m %t:ERROR, o_c did not match", $time); - assert (o_d == 64'h7111fd8b6445099d) else $fatal(0, "%m %t:ERROR, o_d did not match", $time); + assert (o_a == 64'hf0c9aa0de38b1b89) else $fatal(1, "%m %t:ERROR, o_a did not match", $time); + assert (o_b == 64'hbbdf863401fde49b) else $fatal(1, "%m %t:ERROR, o_b did not match", $time); + assert (o_c == 64'he85eb23c42183d3d) else $fatal(1, "%m %t:ERROR, o_c did not match", $time); + assert (o_d == 64'h7111fd8b6445099d) else $fatal(1, "%m %t:ERROR, o_d did not match", $time); $display("test1 PASSED"); end diff --git a/ip_cores/blake2b/src/tb/blake2b_top_tb.sv b/ip_cores/blake2b/src/tb/blake2b_top_tb.sv index 97a6934..7fbfe49 100644 --- a/ip_cores/blake2b/src/tb/blake2b_top_tb.sv +++ b/ip_cores/blake2b/src/tb/blake2b_top_tb.sv @@ -20,9 +20,9 @@ module blake2b_top_tb(); -parameter USE_BLAKE2B_PIPE = 0; // This instantiates the pipelined version instead -parameter USE_BLAKE2B_PIPE_MSG_LEN = 140; // Has to be the maximum of whatever test case -parameter MSG_VAR_BYTS = USE_BLAKE2B_PIPE_MSG_LEN; +parameter USE_BLAKE2B_PIPE = 1; // This instantiates the pipelined version instead +parameter USE_BLAKE2B_PIPE_MSG_LEN = 144; // Has to be the maximum of whatever test case +parameter MSG_VAR_BM = {USE_BLAKE2B_PIPE_MSG_LEN*8{1'b1}}; import blake2b_pkg::*; import common_pkg::*; @@ -58,7 +58,7 @@ generate if ( USE_BLAKE2B_PIPE == 0 ) begin: DUT_GEN end else begin blake2b_pipe_top #( .MSG_LEN ( USE_BLAKE2B_PIPE_MSG_LEN ), - .MSG_VAR_BYTS ( MSG_VAR_BYTS ), + .MSG_VAR_BM ( MSG_VAR_BM ), .CTL_BITS ( 8 ) ) DUT ( @@ -150,7 +150,6 @@ endtask // Main testbench calls initial begin i_block.reset_source(); - i_byte_len = 3; out_hash.rdy = 1; parameters = {32'd0, 8'd1, 8'd1, 8'd0, 8'd64}; #200ns; diff --git a/zcash_verif/src/rtl/zcash_verif_equihash.sv b/zcash_verif/src/rtl/zcash_verif_equihash.sv index 4442e88..b4c2f30 100644 --- a/zcash_verif/src/rtl/zcash_verif_equihash.sv +++ b/zcash_verif/src/rtl/zcash_verif_equihash.sv @@ -54,7 +54,7 @@ logic all_checks_done; if_axi_stream #(.DAT_BYTS(BLAKE2B_DIGEST_BYTS), .CTL_BYTS(1)) blake2b_out_hash(i_clk); -if_axi_stream #(.DAT_BYTS(EQUIHASH_BLAKE2B_PIPE == 0 ? 128 : EQUIHASH_GEN_BYTS )) blake2b_in_hash(i_clk); +if_axi_stream #(.DAT_BYTS(EQUIHASH_GEN_BYTS)) blake2b_in_hash(i_clk); // We write the block into a port as it comes in and then read from the b port if_ram #(.RAM_WIDTH(DAT_BITS), .RAM_DEPTH(SOL_LIST_BYTS/DAT_BYTS)) equihash_sol_bram_if_a (i_clk, i_rst); @@ -81,8 +81,8 @@ always_ff @ (posedge i_clk) begin equihash_sol_bram_if_a.d <= i_axi.dat; if (i_axi.val && i_axi.rdy && ~cblockheader_val) begin - cblockheader <= {cblockheader, i_axi.dat}; - cblockheader_val <= (cblockheader_byts + DAT_BYTS) >= $bits(cblockheader_t)/8; + cblockheader[cblockheader_byts*8 +: DAT_BITS] <= i_axi.dat; + cblockheader_val <= (cblockheader_byts + DAT_BYTS) > $bits(cblockheader_t)/8; cblockheader_byts <= cblockheader_byts + DAT_BYTS; end @@ -215,32 +215,22 @@ function hash_solution(input [N-1:0] curr, input [N*INDICIES_PER_HASH-1:0] in); return curr; endfunction -// Instantiate the Blake2b block -generate if ( EQUIHASH_BLAKE2B_PIPE == 0 ) begin: BLAKE2B_GEN - blake2b_top DUT ( - .i_clk ( i_clk ), - .i_rst ( i_rst ), - .i_parameters ( parameters ), - .i_byte_len ( EQUIHASH_GEN_BYTS ), - .i_block ( blake2b_in_hash ), - .o_hash ( blake2b_out_hash ) - ); -end else begin - blake2b_pipe_top #( - .MSG_LEN ( EQUIHASH_GEN_BYTS ), - .MSG_VAR_BYTS ( 4 ), // Only lower 4 bytes of input to hash change - .CTL_BITS ( 8 ) - ) - DUT ( - .i_clk ( i_clk ), - .i_rst ( i_rst ), - .i_parameters ( parameters ), - .i_byte_len ( EQUIHASH_GEN_BYTS ), - .i_block ( blake2b_in_hash ), - .o_hash ( blake2b_out_hash ) - ); -end -endgenerate +// Instantiate the Blake2b block - use high performance pipelined version +localparam [EQUIHASH_GEN_BYTS*8-1:0] EQUIHASH_GEN_BYTS_BM = {{(EQUIHASH_GEN_BYTS*8-21){1'b0}}, {21{1'b1}}}; // Only lower 21 bits of input to hash change +blake2b_pipe_top #( + .MSG_LEN ( EQUIHASH_GEN_BYTS ), + .MSG_VAR_BM ( EQUIHASH_GEN_BYTS_BM ), + .CTL_BITS ( 8 ) +) +blake2b_pipe_top_i ( + .i_clk ( i_clk ), + .i_rst ( i_rst ), + .i_parameters ( parameters ), + .i_byte_len ( EQUIHASH_GEN_BYTS ), + .i_block ( blake2b_in_hash ), + .o_hash ( blake2b_out_hash ) +); + // Memory to store the equihash solution as it comes in. We use dual port, // one port for writing and one port for reading diff --git a/zcash_verif/src/rtl/zcash_verif_pkg.sv b/zcash_verif/src/rtl/zcash_verif_pkg.sv index fee3173..d0a78c7 100644 --- a/zcash_verif/src/rtl/zcash_verif_pkg.sv +++ b/zcash_verif/src/rtl/zcash_verif_pkg.sv @@ -22,7 +22,6 @@ package zcash_verif_pkg; // Variables used in the equihash PoW parameter [31:0] N = 200; parameter [31:0] K = 9; - parameter EQUIHASH_BLAKE2B_PIPE = 1; // Do we use the pipelined (high performance but large area) Blake2b core parameter INDICIES_PER_HASH = (512/N); parameter COLLISION_BIT_LEN = N/(K+1); parameter BLAKE2B_DIGEST_BYTS = (N*INDICIES_PER_HASH)/8; diff --git a/zcash_verif/src/tb/zcash_verif_equihash_tb.sv b/zcash_verif/src/tb/zcash_verif_equihash_tb.sv index 32afc7c..4e93052 100644 --- a/zcash_verif/src/tb/zcash_verif_equihash_tb.sv +++ b/zcash_verif/src/tb/zcash_verif_equihash_tb.sv @@ -65,7 +65,7 @@ file_to_axi #( .FP ( 0 ) ) file_to_axi_block241 ( - .i_file ({my_file_path_s, "/../data/block_241.bin"}), + .i_file ({my_file_path_s, "/../data/block_346.bin"}), .i_clk ( clk ), .i_rst ( rst ), .i_start ( start_241 ), @@ -82,15 +82,18 @@ DUT ( .o_mask_val ( mask_val ) ); -// This is a tests the sample block 241 -task test_block_241(); +// This is a tests the sample block 346 in the block chain +task test_block_346(); begin - $display("Running test_block_241..."); + $display("Running test_block_346..."); start_241 = 1; - while(!done_241) @(posedge clk); - $display("test_block_241 PASSED"); + while(!done_241 && !mask_val) @(posedge clk); + + assert (~(|mask)) else $fatal(1, "%m %t ERROR: test_block_346 mask was non-zero", $time); + assert (~mask.XOR_FAIL) else $fatal(1, "%m %t ERROR: test_block_346 failed XOR mask check", $time); + $display("test_block_346 PASSED"); end endtask @@ -99,7 +102,7 @@ endtask initial begin #200ns; - test_block_241(); + test_block_346(); #10us $finish();