`timescale 1ns / 1ps // HARDWARE-ONLY SKELETON. NOT TESTED ON HARDWARE. NOT A CERTIFIED ENTROPY SOURCE. // // Ring-oscillator noise source: NUM_RO free-running rings. Each ring output is sampled // by its own flip-flops on the system clock, and the sampled bits are XORed. Sampling // each ring before the XOR follows Wold and Tan (2009); a single flip-flop after an XOR // of many fast rings sees too many transitions per clock period. // // The randomness comes from timing jitter in the silicon, which a logic simulator does // not model: in simulation the rings either stay at X or toggle in a fixed pattern, so // this file is linted but never simulated for entropy. // // To use it on a device you must // * keep every ring node through synthesis and implementation (KEEP and DONT_TOUCH // are the AMD Vivado attributes, documented in UG912; other tools use their own // attribute or constraint), // * allow the intentional combinational loops in the tool's design rule checks, // * check placement and the timing reports, // * feed raw_bit to health_tests.sv, assess the min-entropy of the raw bits with the // SP 800-90B procedures, and condition the output before use as key material. // // STAGES is the number of inverting stages per ring including the enable NAND. It must // be odd, or the ring has a stable state and does not oscillate. module ro_trng #( parameter int NUM_RO = 8, parameter int STAGES = 3, parameter int DIV = 16 // one raw bit every DIV clock cycles ) ( input logic clk, input logic rst, // synchronous, active high input logic enable, // 0 stops all rings (each NAND output held at 1) output logic raw_bit, output logic raw_valid ); if (STAGES % 2 == 0 || STAGES < 3) begin : g_bad_stages $error("ro_trng: STAGES must be odd and at least 3"); end logic [NUM_RO-1:0] ro_out; for (genvar r = 0; r < NUM_RO; r++) begin : g_ring // Intentional combinational loop. Verilator reports a loop like this as // UNOPTFLAT (it cannot schedule it); the waiver is limited to this declaration. /* verilator lint_off UNOPTFLAT */ (* keep = "true", dont_touch = "true" *) logic [STAGES-1:0] node; /* verilator lint_on UNOPTFLAT */ assign node[0] = ~(enable & node[STAGES-1]); for (genvar s = 1; s < STAGES; s++) begin : g_inv assign node[s] = ~node[s-1]; end assign ro_out[r] = node[STAGES-1]; end // Each ring changes at times unrelated to clk. The first flip-flop samples it and // may go metastable; the second gives it a clock period to settle (lesson 12). logic [NUM_RO-1:0] samp1, samp2; logic mix; always_ff @(posedge clk) begin samp1 <= ro_out; samp2 <= samp1; mix <= ^samp2; end // Take one bit every DIV cycles, so that more jitter accumulates between raw bits. logic [$clog2(DIV)-1:0] cnt; always_ff @(posedge clk) begin if (rst) begin cnt <= '0; raw_valid <= 1'b0; raw_bit <= 1'b0; end else begin raw_valid <= 1'b0; if (cnt == $clog2(DIV)'(DIV - 1)) begin cnt <= '0; raw_valid <= enable; raw_bit <= mix; end else begin cnt <= cnt + 1'b1; end end end endmodule