`timescale 1ns / 1ps // Continuous health tests of NIST SP 800-90B, section 4.4, for a noise source that // delivers one SW-bit sample per sample_valid. // // Repetition Count Test (4.4.1): A = first sample, B = 1. For each next sample X: // if X == A then B = B + 1, and fail when B >= RCT_C; else A = X, B = 1. // Adaptive Proportion Test (4.4.2): A = first sample of a window of APT_W samples, B = 1. // For each of the next APT_W - 1 samples: if it equals A then B = B + 1, and fail // when B >= APT_C. Then the next sample starts a new window. // // Cutoffs come from the assessed min-entropy H per sample and the false-positive // probability alpha (see sp80090b_health.py): // RCT_C = 1 + ceil(-log2(alpha) / H) // APT_C = 1 + CRITBINOM(APT_W, 2^-H, 1 - alpha) // SP 800-90B sets APT_W = 1024 for binary sources and 512 for non-binary sources. // // Failures are sticky until rst. ready rises after STARTUP samples with no failure // (SP 800-90B start-up testing uses at least 1024 samples). module health_tests #( parameter int SW = 1, // sample width in bits parameter int RCT_C = 21, // H = 1, alpha = 2^-20 parameter int APT_W = 1024, parameter int APT_C = 589, // H = 1, alpha = 2^-20, W = 1024 (Table 2) parameter int STARTUP = 1024 ) ( input logic clk, input logic rst, // synchronous, active high input logic sample_valid, input logic [SW-1:0] sample, output logic rct_fail, output logic apt_fail, output logic ready // start-up passed and no failure since ); localparam int RCT_BW = $clog2(RCT_C + 1); localparam int APT_BW = $clog2(APT_W + 1); localparam int IDX_BW = $clog2(APT_W); localparam int ST_BW = $clog2(STARTUP + 1); // ---------------------------------------------------------------- RCT logic [SW-1:0] rct_a; logic [RCT_BW-1:0] rct_b; logic rct_started; always_ff @(posedge clk) begin if (rst) begin rct_started <= 1'b0; rct_b <= '0; rct_a <= '0; rct_fail <= 1'b0; end else if (sample_valid) begin if (!rct_started || sample != rct_a) begin rct_started <= 1'b1; rct_a <= sample; rct_b <= RCT_BW'(1); end else begin if (rct_b < RCT_BW'(RCT_C)) rct_b <= rct_b + 1'b1; // saturate, the flag is sticky if (rct_b + 1'b1 >= RCT_BW'(RCT_C)) rct_fail <= 1'b1; end end end // ---------------------------------------------------------------- APT logic [SW-1:0] apt_a; logic [APT_BW-1:0] apt_b; logic [IDX_BW-1:0] apt_i; // position in the window, 0 = reference sample always_ff @(posedge clk) begin if (rst) begin apt_i <= '0; apt_b <= '0; apt_a <= '0; apt_fail <= 1'b0; end else if (sample_valid) begin if (apt_i == '0) begin apt_a <= sample; apt_b <= APT_BW'(1); apt_i <= IDX_BW'(1); end else begin if (sample == apt_a) begin apt_b <= apt_b + 1'b1; if (apt_b + 1'b1 >= APT_BW'(APT_C)) apt_fail <= 1'b1; end apt_i <= (apt_i == IDX_BW'(APT_W - 1)) ? '0 : apt_i + 1'b1; end end end // ---------------------------------------------------------------- start-up logic [ST_BW-1:0] n_seen; always_ff @(posedge clk) begin if (rst) begin n_seen <= '0; end else if (sample_valid && n_seen < ST_BW'(STARTUP)) begin n_seen <= n_seen + 1'b1; end end assign ready = (n_seen == ST_BW'(STARTUP)) && !rct_fail && !apt_fail; endmodule