|
| 1 | +/* Copyright (c) 2026 Zane Hambly |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ |
| 5 | + |
| 6 | +/* |
| 7 | + * tk_hash.c -- Synthesis fingerprint for Takahe RTL |
| 8 | + * |
| 9 | + * Hash a netlist deterministically. Two runs on the same |
| 10 | + * input produce the same digest. When they don't, something |
| 11 | + * has crept in that you didn't intend: a drifted pass |
| 12 | + * iteration order, an uninitialised byte, a clock cycle |
| 13 | + * the gremlins won at darts. |
| 14 | + * |
| 15 | + * FNV-1a, 64 bits. Not a cryptographic hash. It won't |
| 16 | + * survive an adversary but it will catch the day your |
| 17 | + * pipeline turned non-deterministic, which is the one |
| 18 | + * thing anyone actually wants to know about a synthesis |
| 19 | + * tool. IBM used CRCs to detect tape errors for forty |
| 20 | + * years before anyone needed SHA-anything. The principle |
| 21 | + * holds. |
| 22 | + * |
| 23 | + * Canonical form: bump HS_VER any time you change what |
| 24 | + * goes into the hash or in what order. Saved digests |
| 25 | + * become meaningfully invalid the moment the form changes, |
| 26 | + * like postage stamps after a regime change. |
| 27 | + */ |
| 28 | + |
| 29 | +#include "takahe.h" |
| 30 | + |
| 31 | +#define HS_VER 1u |
| 32 | +#define HS_OFFSET 0xcbf29ce484222325ULL |
| 33 | +#define HS_PRIME 0x100000001b3ULL |
| 34 | + |
| 35 | +/* ---- FNV-1a primitives ---- |
| 36 | + * Twelve lines, no dependencies, faster than fetching |
| 37 | + * SHA-256 off the internet. A measurable engineering |
| 38 | + * virtue. */ |
| 39 | + |
| 40 | +static uint64_t |
| 41 | +hs_byte(uint64_t h, uint8_t b) |
| 42 | +{ |
| 43 | + h ^= (uint64_t)b; |
| 44 | + h *= HS_PRIME; |
| 45 | + return h; |
| 46 | +} |
| 47 | + |
| 48 | +static uint64_t |
| 49 | +hs_u32(uint64_t h, uint32_t v) |
| 50 | +{ |
| 51 | + h = hs_byte(h, (uint8_t)( v & 0xffu)); |
| 52 | + h = hs_byte(h, (uint8_t)((v >> 8) & 0xffu)); |
| 53 | + h = hs_byte(h, (uint8_t)((v >> 16) & 0xffu)); |
| 54 | + h = hs_byte(h, (uint8_t)((v >> 24) & 0xffu)); |
| 55 | + return h; |
| 56 | +} |
| 57 | + |
| 58 | +static uint64_t |
| 59 | +hs_u64(uint64_t h, uint64_t v) |
| 60 | +{ |
| 61 | + h = hs_u32(h, (uint32_t)( v & 0xffffffffULL)); |
| 62 | + h = hs_u32(h, (uint32_t)( v >> 32)); |
| 63 | + return h; |
| 64 | +} |
| 65 | + |
| 66 | +/* ---- Public: hash a module ---- |
| 67 | + * Walks nets then cells in array order. The walk order |
| 68 | + * is part of the canonical form: anyone changing pool |
| 69 | + * allocation strategy must bump HS_VER. Cell and net |
| 70 | + * indices are deterministic per run on the same input, |
| 71 | + * which is all a fingerprint needs to be useful. |
| 72 | + * |
| 73 | + * Names are NOT hashed. A wire renamed from q to q_r is |
| 74 | + * the same hardware. The hash agrees. */ |
| 75 | + |
| 76 | +uint64_t |
| 77 | +mp_hash(const rt_mod_t *M) |
| 78 | +{ |
| 79 | + uint64_t h = HS_OFFSET; |
| 80 | + uint32_t i; |
| 81 | + uint8_t j; |
| 82 | + |
| 83 | + if (!M) return 0; |
| 84 | + |
| 85 | + /* Magic + version. Later versions diverge from byte one, |
| 86 | + * so a v1 digest will never collide with a v2 digest by |
| 87 | + * accident. Belt, braces, and a length of fencing wire. */ |
| 88 | + h = hs_byte(h, (uint8_t)'T'); |
| 89 | + h = hs_byte(h, (uint8_t)'K'); |
| 90 | + h = hs_byte(h, (uint8_t)'H'); |
| 91 | + h = hs_byte(h, (uint8_t)'1'); |
| 92 | + h = hs_u32(h, HS_VER); |
| 93 | + |
| 94 | + /* Nets. Index 0 is the sentinel; every pool reserves it |
| 95 | + * for "nobody's home". Skip it deliberately. */ |
| 96 | + h = hs_u32(h, M->n_net); |
| 97 | + for (i = 1; i < M->n_net; i++) { |
| 98 | + const rt_net_t *n = &M->nets[i]; |
| 99 | + h = hs_u32 (h, n->width); |
| 100 | + h = hs_byte(h, n->radix); |
| 101 | + h = hs_byte(h, n->is_port); |
| 102 | + h = hs_u32 (h, n->driver); |
| 103 | + } |
| 104 | + |
| 105 | + /* Cells. RT_CELL_COUNT marks a slot vacated by some |
| 106 | + * pass — record the gap as a single zero byte so a |
| 107 | + * deletion doesn't collide with an unrelated rewrite. */ |
| 108 | + h = hs_u32(h, M->n_cell); |
| 109 | + for (i = 1; i < M->n_cell; i++) { |
| 110 | + const rt_cell_t *c = &M->cells[i]; |
| 111 | + if (c->type == RT_CELL_COUNT) { |
| 112 | + h = hs_byte(h, 0); |
| 113 | + continue; |
| 114 | + } |
| 115 | + h = hs_byte(h, (uint8_t)c->type); |
| 116 | + h = hs_u32 (h, c->width); |
| 117 | + h = hs_u32 (h, c->out); |
| 118 | + h = hs_byte(h, c->n_in); |
| 119 | + for (j = 0; j < c->n_in; j++) |
| 120 | + h = hs_u32(h, c->ins[j]); |
| 121 | + h = hs_u64(h, (uint64_t)c->param); |
| 122 | + } |
| 123 | + |
| 124 | + return h; |
| 125 | +} |
0 commit comments