|
| 1 | +// Copyright lowRISC contributors (OpenTitan project). |
| 2 | +// Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +#include <cstdint> |
| 6 | +#include <cstdio> |
| 7 | +#include <queue> |
| 8 | +#include <random> |
| 9 | + |
| 10 | +#include "Votbn_mask_accelerator.h" |
| 11 | +#include "verilated.h" |
| 12 | +#include "verilated_vcd_c.h" |
| 13 | + |
| 14 | +static constexpr int kWidth = 32; |
| 15 | +static constexpr uint32_t kShareMask = ~0u; |
| 16 | + |
| 17 | +#include "otbn_tb_utils.h" |
| 18 | + |
| 19 | +// mask_op_e encodings from otbn_pkg.sv, paired with display names. |
| 20 | +// arith_out: true when result_o carries an arithmetic sharing (s0+s1 mod q), |
| 21 | +// false when it carries a Boolean sharing (s0^s1). |
| 22 | +struct Mode { |
| 23 | + uint8_t enc; |
| 24 | + const char *name; |
| 25 | + bool arith_out; |
| 26 | +}; |
| 27 | +static constexpr Mode kModes[] = { |
| 28 | + {0x17, "SecAdd", false}, |
| 29 | + {0x0c, "SecAddMod", false}, |
| 30 | + {0x0b, "ArithToBool", false}, |
| 31 | + {0x10, "BoolToArith", true}, |
| 32 | +}; |
| 33 | + |
| 34 | +static constexpr int kNumModes = sizeof(kModes) / sizeof(kModes[0]); |
| 35 | +static constexpr int kVecSize = 8; |
| 36 | +static constexpr int kElemPerMode = 1000 * kVecSize; |
| 37 | +static constexpr int kRemaskWords = 2; |
| 38 | + |
| 39 | +int main(int argc, char **argv) { |
| 40 | + VerilatedContext *const contextp = new VerilatedContext; |
| 41 | + contextp->commandArgs(argc, argv); |
| 42 | + contextp->traceEverOn(true); |
| 43 | + |
| 44 | + Votbn_mask_accelerator *const dut = |
| 45 | + new Votbn_mask_accelerator(contextp, "TOP"); |
| 46 | + |
| 47 | + // Access Verilator packed [N:0][W-1:0] ports as flat uint32_t[] |
| 48 | + // (little-endian). |
| 49 | + uint32_t *const rand_raw = reinterpret_cast<uint32_t *>(&dut->rand_i); |
| 50 | + uint32_t *const remask_raw = |
| 51 | + reinterpret_cast<uint32_t *>(&dut->remask_rand_i); |
| 52 | + uint32_t *const result_raw = reinterpret_cast<uint32_t *>(&dut->result_o); |
| 53 | + uint32_t *const in0_raw = reinterpret_cast<uint32_t *>(&dut->in0_i); |
| 54 | + uint32_t *const in1_raw = reinterpret_cast<uint32_t *>(&dut->in1_i); |
| 55 | + |
| 56 | + // VCD waveform writer receives signal snapshots on each vcd->dump() call. |
| 57 | + VerilatedVcdC *const vcd = new VerilatedVcdC; |
| 58 | + dut->trace(vcd, 99); |
| 59 | + vcd->open("dump.vcd"); |
| 60 | + |
| 61 | + // Mersenne Twister RNG with a fixed seed for deterministic, reproducible |
| 62 | + // timulus. |
| 63 | + std::mt19937 rng(42); |
| 64 | + // Produces uniformly distributed random uint32_t values (full 32-bit range). |
| 65 | + std::uniform_int_distribution<uint32_t> dist; |
| 66 | + |
| 67 | + // Initialise all inputs to a known state before the first eval(). |
| 68 | + dut->clk_i = 0; |
| 69 | + dut->rst_ni = 0; |
| 70 | + dut->sec_wipe_running_i = 0; |
| 71 | + dut->wvalid_i = 0; |
| 72 | + dut->rready_i = 1; |
| 73 | + dut->mask_op_i = kModes[0].enc; |
| 74 | + dut->mod_i = dist(rng) & kShareMask; |
| 75 | + // mod_i = 0 would break rejection sampling |
| 76 | + if (dut->mod_i == 0) |
| 77 | + dut->mod_i = 1; |
| 78 | + for (int i = 0; i < kRandWords; i++) |
| 79 | + rand_raw[i] = 0; |
| 80 | + for (int i = 0; i < kRemaskWords; i++) |
| 81 | + remask_raw[i] = 0; |
| 82 | + set_share(in0_raw, 0, 0); |
| 83 | + set_share(in0_raw, 1, 0); |
| 84 | + set_share(in1_raw, 0, 0); |
| 85 | + set_share(in1_raw, 1, 0); |
| 86 | + dut->eval(); |
| 87 | + |
| 88 | + // Reset the DUT. |
| 89 | + run_reset(dut, vcd, contextp); |
| 90 | + |
| 91 | + int total_errs = 0; |
| 92 | + const uint32_t mod = dut->mod_i; |
| 93 | + |
| 94 | + for (int mode = 0; mode < kNumModes; mode++) { |
| 95 | + // Set the operation mode and advance by one cycle. |
| 96 | + dut->mask_op_i = kModes[mode].enc; |
| 97 | + tick(dut, vcd, contextp); |
| 98 | + |
| 99 | + int n_stims = 0, n_checks = 0, n_errs = 0; |
| 100 | + |
| 101 | + // FIFO of expected kWidth-bit results. Each entry is pushed when a stimulus |
| 102 | + // is accepted (wvalid_i && wready_o) and popped when rvalid_o fires. |
| 103 | + std::queue<uint32_t> exp_queue; |
| 104 | + |
| 105 | + // Stimulus and result-check loop. |
| 106 | + while (n_checks < kElemPerMode) { |
| 107 | + // Drive fresh randomness before the rising edge. |
| 108 | + // For BoolToArith, wready_o is combinational on remask_rand_i[0]. |
| 109 | + for (int i = 0; i < kRandWords; i++) |
| 110 | + rand_raw[i] = dist(rng); |
| 111 | + for (int i = 0; i < kRemaskWords; i++) |
| 112 | + remask_raw[i] = dist(rng) & kShareMask; |
| 113 | + |
| 114 | + uint32_t golden = 0; |
| 115 | + if (n_stims < kElemPerMode) { |
| 116 | + uint32_t a0, a1, b0 = 0, b1 = 0; |
| 117 | + if (mode == 1) { // SecAddMod |
| 118 | + const uint32_t inp1 = dist(rng) % mod; |
| 119 | + const uint32_t inp2 = dist(rng) % mod; |
| 120 | + const uint32_t ma = dist(rng) & kShareMask; |
| 121 | + const uint32_t mb = dist(rng) & kShareMask; |
| 122 | + a0 = (inp1 - mod) ^ ma; |
| 123 | + a1 = ma; |
| 124 | + b0 = inp2 ^ mb; |
| 125 | + b1 = mb; |
| 126 | + golden = (uint32_t)(((uint64_t)inp1 + inp2) % mod); |
| 127 | + } else if (mode == 2) { // ArithToBool |
| 128 | + const uint32_t inp1 = dist(rng) % mod; |
| 129 | + const uint32_t inp2 = dist(rng) % mod; |
| 130 | + a0 = inp1; |
| 131 | + a1 = inp2; |
| 132 | + golden = (uint32_t)(((uint64_t)inp1 + inp2) % mod); |
| 133 | + } else if (mode == 3) { // BoolToArith |
| 134 | + const uint32_t secret = dist(rng) % mod; |
| 135 | + const uint32_t ma = dist(rng) & kShareMask; |
| 136 | + a0 = secret ^ ma; |
| 137 | + a1 = ma; |
| 138 | + golden = secret; |
| 139 | + } else { // SecAdd |
| 140 | + a0 = dist(rng) & kShareMask; |
| 141 | + a1 = dist(rng) & kShareMask; |
| 142 | + b0 = dist(rng) & kShareMask; |
| 143 | + b1 = dist(rng) & kShareMask; |
| 144 | + golden = ((a0 ^ a1) + (b0 ^ b1)) & kShareMask; |
| 145 | + } |
| 146 | + set_share(in0_raw, 0, a0); |
| 147 | + set_share(in0_raw, 1, a1); |
| 148 | + set_share(in1_raw, 0, b0); |
| 149 | + set_share(in1_raw, 1, b1); |
| 150 | + dut->wvalid_i = 1; |
| 151 | + } else { |
| 152 | + dut->wvalid_i = 0; |
| 153 | + } |
| 154 | + |
| 155 | + // Settle combinational outputs after driving fresh randomness so that |
| 156 | + // wready_o reflects the current remask_rand_i before we sample it. |
| 157 | + // For BoolToArith, wready_o is gated by (mask_mod < mod_i) which is |
| 158 | + // purely combinational on remask_rand_i[0], without this eval() the |
| 159 | + // acceptance decision would be one cycle stale. |
| 160 | + dut->eval(); |
| 161 | + const bool accepted = dut->wvalid_i && dut->wready_o; |
| 162 | + |
| 163 | + // Rising edge |
| 164 | + dut->clk_i = 1; |
| 165 | + dut->eval(); |
| 166 | + |
| 167 | + if (accepted) { |
| 168 | + exp_queue.push(golden); |
| 169 | + n_stims++; |
| 170 | + } |
| 171 | + |
| 172 | + if (dut->rvalid_o && !exp_queue.empty()) { |
| 173 | + const uint32_t s0 = get_share(result_raw, 0); |
| 174 | + const uint32_t s1 = get_share(result_raw, 1); |
| 175 | + const uint32_t got = kModes[mode].arith_out |
| 176 | + ? (uint32_t)(((uint64_t)s0 + s1) % mod) |
| 177 | + : (s0 ^ s1) & kShareMask; |
| 178 | + const uint32_t exp = exp_queue.front(); |
| 179 | + exp_queue.pop(); |
| 180 | + n_checks++; |
| 181 | + if (got != exp) { |
| 182 | + n_errs++; |
| 183 | + printf( |
| 184 | + "[FAIL] mode=%d t=%-8llu | exp=%08x got=%08x (s0=%08x s1=%08x)\n", |
| 185 | + mode, (unsigned long long)contextp->time(), exp, got, s0, s1); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + vcd->dump(contextp->time()); |
| 190 | + contextp->timeInc(1); |
| 191 | + |
| 192 | + // Falling edge |
| 193 | + dut->clk_i = 0; |
| 194 | + dut->eval(); |
| 195 | + vcd->dump(contextp->time()); |
| 196 | + contextp->timeInc(1); |
| 197 | + } |
| 198 | + |
| 199 | + printf("Mode %d (%s): %s - %d errors / %d checks\n", mode, |
| 200 | + kModes[mode].name, n_errs ? "FAIL" : "PASS", n_errs, n_checks); |
| 201 | + total_errs += n_errs; |
| 202 | + } |
| 203 | + |
| 204 | + if (total_errs) |
| 205 | + printf("Test ***FAILED*** %d total errors\n", total_errs); |
| 206 | + else |
| 207 | + printf("Test ***PASSED*** all 4 modes\n"); |
| 208 | + |
| 209 | + dut->final(); |
| 210 | + vcd->close(); |
| 211 | + delete vcd; |
| 212 | + delete dut; |
| 213 | + delete contextp; |
| 214 | + return total_errs ? 1 : 0; |
| 215 | +} |
0 commit comments