niobium/fhetch_sim/simulator.h
FHETCH Simulator — instruction-set simulator for .fhetch trace files.
Reads a FHETCH instruction trace and executes each operation using OpenFHE modular arithmetic, producing computed polynomial values in memory. This is the functional equivalent of the niobium-compiler's FHE_SIM replay target, but operating on the FHETCH text format instead of the internal .seq format.
Include: #include "niobium/fhetch_sim/simulator.h"
Declarations
struct niobium::fhetch_sim::SimResult
struct niobium::fhetch_sim::SimResultResult of running the simulator.
Fields
| Name | Declaration | Description |
|---|---|---|
instructions_executed | size_t instructions_executed = 0 | |
errors | size_t errors = 0 | |
elapsed_seconds | double elapsed_seconds = 0.0 |
class niobium::fhetch_sim::Simulator
class niobium::fhetch_sim::SimulatorFHETCH instruction-set simulator.
Usage: Simulator sim; sim.set_ring_dimension(4096); sim.load_trace("program.fhetch"); sim.load_input("a", {1, 2, 3, ...}, modulus); auto result = sim.run(); auto values = sim.get_polynomial(address);
Simulator
Simulator()~Simulator
~Simulator()set_ring_dimension
void set_ring_dimension(uint64_t N)Set the ring dimension (N) for all polynomials.
load_trace
bool load_trace(const std::filesystem::path &trace_file)Load a .fhetch trace file.
Returns: true if parsing succeeded.
store_polynomial
void store_polynomial(uint64_t address, const std::vector<uint64_t> &values, uint64_t modulus)Store a polynomial in simulator memory at the given address. Used to populate inputs before running. The rvalue overload donates the buffer instead of copying.
void store_polynomial(uint64_t address, std::vector<uint64_t> &&values, uint64_t modulus)run
SimResult run()Run the loaded trace.
get_polynomial
std::vector<uint64_t> get_polynomial(uint64_t address) constRetrieve a polynomial from simulator memory after execution.
Returns: empty vector if address is uninitialized.
get_modulus
uint64_t get_modulus(uint64_t address) constGet the modulus associated with a memory address.
is_initialized
bool is_initialized(uint64_t address) constCheck if a memory address is initialized.
get_read_before_write_addresses
std::vector<uint64_t> get_read_before_write_addresses() constGet the set of addresses that are read before being written in the trace. These are the "live-in" addresses that need input data.
set_live_out_addresses
void set_live_out_addresses(const std::vector<uint64_t> &addrs)Scan the trace for instructions that initialize an address with a known constant value of zero (e.g. sr_mulps x, x, 0, m=...) BEFORE it appears as a read anywhere, and materialize the zero vector in simulator memory at that address. Called by replay() as part of the load-time population so the data-parent chain can propagate from zero-initialized sources. Mark addresses that must NOT be freed during execution. The compiler's replay() pass calls this with every probe/output address before run(), so write_replay_outputs() can still read them after execution finishes.
compute_liveness
void compute_liveness()Liveness-driven free schedule: a memory.erase() is scheduled at each address's last read, and at every write whose value is never read afterwards (dead stores and rewrites after the last read — near-SSA traces retain hundreds of thousands of such buffers otherwise). Must be called after load_trace() and (optionally) set_live_out_addresses(); live-out addresses are never freed. NIOBIUM_DISABLE_SIM_FREES skips the pass entirely.
get_free_after_for_test
const std::vector<std::vector<uint64_t>> & get_free_after_for_test() constTest-only accessor: returns the per-instruction free schedule built by compute_liveness(). Each inner vector holds the addresses freed after the instruction at that index. Used by test_simulator_liveness; production callers should not rely on this.
Something not looking right? Suggest a change on GitHub.