niobium/fhetch_ir.h
Include: #include "niobium/fhetch_ir.h"
Opcodes
enum niobium::fhetch::FhOpcode
enum FhOpcode : uint8_t { FH_ILL = 0, FH_SR_ADDP = 1, FH_SR_SUBP = 2, FH_SR_MULP = 3, FH_SR_ADDPS = 4, FH_SR_SUBPS = 5, FH_SR_MULPS = 6, FH_SR_ADDPS_COEFF = 7, FH_SR_SUBPS_COEFF = 8, FH_SR_NEGP = 9, FH_SR_NTT = 10, FH_SR_INTT = 11, FH_SR_PERMUTE = 12, FH_SR_AUTOMORPH_EVAL = 13, FH_SR_AUTOMORPH_COEFF = 14, FH_SR_ROT_AUTOMORPH_COEFF = 15, FH_SR_ADDP_NI = 16, FH_SR_SUBP_NI = 17, FH_SR_MULP_NI = 18, FH_SR_ADDPS_NI = 19, FH_SR_SUBPS_NI = 20, FH_SR_MULPS_NI = 21, FH_SR_ADDPS_COEFF_NI = 22, FH_SR_SUBPS_COEFF_NI = 23, FH_SR_NEGP_NI = 24, FH_SR_FT = 25, FH_SR_IFT = 26, FH_HALT = 27 }FHETCH mnemonics, numbered stably — these values are on the wire (see fhetch_encoding.h), so existing entries must never be renumbered.
The vocabulary covers every mnemonic the recorder can emit, so any FHETCH program is representable. A given consumer may accept only a subset.
| Value | Description |
|---|---|
FH_ILL | |
FH_SR_ADDP | |
FH_SR_SUBP | |
FH_SR_MULP | |
FH_SR_ADDPS | |
FH_SR_SUBPS | |
FH_SR_MULPS | |
FH_SR_ADDPS_COEFF | |
FH_SR_SUBPS_COEFF | |
FH_SR_NEGP | |
FH_SR_NTT | |
FH_SR_INTT | |
FH_SR_PERMUTE | |
FH_SR_AUTOMORPH_EVAL | |
FH_SR_AUTOMORPH_COEFF | |
FH_SR_ROT_AUTOMORPH_COEFF | |
FH_SR_ADDP_NI | |
FH_SR_SUBP_NI | |
FH_SR_MULP_NI | |
FH_SR_ADDPS_NI | |
FH_SR_SUBPS_NI | |
FH_SR_MULPS_NI | |
FH_SR_ADDPS_COEFF_NI | |
FH_SR_SUBPS_COEFF_NI | |
FH_SR_NEGP_NI | |
FH_SR_FT | |
FH_SR_IFT | |
FH_HALT |
enum niobium::fhetch::OperandForm
enum class OperandForm : uint8_t { None, PolyPoly, PolyPolyNI, PolyScalar, PolyScalarNI, UnaryMod, UnaryNoMod, Ntt, AutomorphEval, AutomorphCoeff, RotAutomorphCoeff }Which operands an opcode carries, and therefore how its text line is laid out. Named for the shape rather than for one member of it, since several opcodes share each shape.
_NI forms are the non-integer (floating-point) variants. They take no modulus at all — a real distinction, not an omission: there is no ring to reduce into.
| Value | Description |
|---|---|
None | halt |
PolyPoly | dest, src1, src2, m= |
PolyPolyNI | dest, src1, src2 |
PolyScalar | dest, src1, imm, m= |
PolyScalarNI | dest, src1, imm |
UnaryMod | dest, src1, m= |
UnaryNoMod | dest, src1 |
Ntt | dest, src1, m= [, omega=] |
AutomorphEval | dest, src1, m= [, mask=, logn=], k= |
AutomorphCoeff | dest, src1, k=, m= |
RotAutomorphCoeff | dest, src1, offset=, m= |
struct niobium::fhetch::OpcodeInfo
struct niobium::fhetch::OpcodeInfoOne row of the opcode vocabulary. Adding an opcode means adding a row here and nothing else — the mnemonic table, the reverse lookup, and the operand shape all read from this.
Fields
| Name | Declaration | Description |
|---|---|---|
opcode | FhOpcode opcode | |
mnemonic | const char * mnemonic | |
form | OperandForm form |
niobium::fhetch::kOpcodeTable
constexpr OpcodeInfo kOpcodeTable = { {FH_SR_ADDP, "sr_addp", OperandForm::PolyPoly}, {FH_SR_SUBP, "sr_subp", OperandForm::PolyPoly}, {FH_SR_MULP, "sr_mulp", OperandForm::PolyPoly}, {FH_SR_ADDPS, "sr_addps", OperandForm::PolyScalar}, {FH_SR_SUBPS, "sr_subps", OperandForm::PolyScalar}, {FH_SR_MULPS, "sr_mulps", OperandForm::PolyScalar}, {FH_SR_ADDPS_COEFF, "sr_addps_coeff", OperandForm::PolyScalar}, {FH_SR_SUBPS_COEFF, "sr_subps_coeff", OperandForm::PolyScalar}, {FH_SR_NEGP, "sr_negp", OperandForm::UnaryMod}, {FH_SR_NTT, "sr_ntt", OperandForm::Ntt}, {FH_SR_INTT, "sr_intt", OperandForm::Ntt}, {FH_SR_PERMUTE, "sr_permute", OperandForm::UnaryMod}, {FH_SR_AUTOMORPH_EVAL, "sr_automorph_eval", OperandForm::AutomorphEval}, {FH_SR_AUTOMORPH_COEFF, "sr_automorph_coeff", OperandForm::AutomorphCoeff}, {FH_SR_ROT_AUTOMORPH_COEFF, "sr_rot_automorph_coeff", OperandForm::RotAutomorphCoeff}, {FH_SR_ADDP_NI, "sr_addp_ni", OperandForm::PolyPolyNI}, {FH_SR_SUBP_NI, "sr_subp_ni", OperandForm::PolyPolyNI}, {FH_SR_MULP_NI, "sr_mulp_ni", OperandForm::PolyPolyNI}, {FH_SR_ADDPS_NI, "sr_addps_ni", OperandForm::PolyScalarNI}, {FH_SR_SUBPS_NI, "sr_subps_ni", OperandForm::PolyScalarNI}, {FH_SR_MULPS_NI, "sr_mulps_ni", OperandForm::PolyScalarNI}, {FH_SR_ADDPS_COEFF_NI, "sr_addps_coeff_ni", OperandForm::PolyScalarNI}, {FH_SR_SUBPS_COEFF_NI, "sr_subps_coeff_ni", OperandForm::PolyScalarNI}, {FH_SR_NEGP_NI, "sr_negp_ni", OperandForm::UnaryNoMod}, {FH_SR_FT, "sr_ft", OperandForm::UnaryNoMod}, {FH_SR_IFT, "sr_ift", OperandForm::UnaryNoMod}, {FH_HALT, "halt", OperandForm::None}, }The vocabulary. Order is irrelevant to correctness; it follows the opcode numbering so a reader can eyeball it against the enum above.
niobium::fhetch::kOpcodeCount
constexpr size_t kOpcodeCount = sizeof(kOpcodeTable) / sizeof(kOpcodeTable[0])FH_ILL is deliberately absent from the table: it names "no opcode", so it has no mnemonic and no operand shape. Every other enumerator has a row.
niobium::fhetch::fh_opcode_info
constexpr const OpcodeInfo * fh_opcode_info(uint8_t opcode)The table row for an opcode, or nullptr for FH_ILL and unassigned values.
niobium::fhetch::fh_opcode_mnemonic
constexpr const char * fh_opcode_mnemonic(uint8_t opcode)Mnemonic string for an opcode — the exact token the .fhetch text form uses, so an instruction can be rendered back to its line verbatim. Returns nullptr for FH_ILL and unassigned values.
niobium::fhetch::fh_opcode_from_mnemonic
constexpr std::optional<FhOpcode> fh_opcode_from_mnemonic(std::string_view mnemonic)The opcode a mnemonic names, or nullopt when the token is not one. The reverse of fh_opcode_mnemonic(), so a reader and a writer agree by construction rather than by two tables being kept in step.
niobium::fhetch::fh_operand_form
constexpr OperandForm fh_operand_form(uint8_t opcode)Operand shape for an opcode. FH_ILL and unassigned values report None, which is what a consumer that cannot identify an instruction should assume it can read off it: nothing.
niobium::fhetch::fh_opcode_has_operands
constexpr bool fh_opcode_has_operands(uint8_t opcode)Whether an opcode carries a destination and first source.
Unassigned values report true, matching the binary decoder's assumption that an unrecognized record still has the two operands every real opcode but halt has. Changing that would change how a .fhex written by a newer toolchain decodes here, so it is behaviour, not a detail.
Instruction
struct niobium::fhetch::Instruction
struct niobium::fhetch::InstructionOne FHETCH instruction.
Operands are addresses, not SSA values: a program may write the same address more than once, and a destination may alias a source. That is not a stylistic choice — the OpenFHE recorder observes a library that mutates in place, so its traces genuinely are address-based (a measured CKKS bootstrap writes 819,380 instructions into 187,540 addresses, ~70% of them read-modify-write). Consumers that need single-assignment build it themselves.
Absent operands are nullopt rather than zero, because zero is a legal address, immediate, and modulus index. line_number is the only diagnostic carried: an instruction deliberately does not hold its own source text, which is the reader's and writer's business alone.
Fields
| Name | Declaration | Description |
|---|---|---|
opcode | FhOpcode opcode = FH_ILL | |
dest | uint32_t dest = 0 | Polynomial addresses. 32-bit: these are pre-compaction polynomial ids — a dense counter from zero, not a machine address — and the widest measured real trace reaches ~2^19. A reader that meets a wider value must reject it rather than truncate. |
src1 | uint32_t src1 = 0 | |
src2 | uint32_t src2 = 0 | |
immediate | std::optional<uint64_t> immediate | Scalar immediate, for the integer poly-scalar forms. |
fp_immediate | std::optional<double> fp_immediate | Scalar immediate for the non-integer poly-scalar forms (OperandForm::PolyScalarNI). A separate field because those operands are genuinely floating point — the specification's NonInteger scalars — and squeezing them into the integer one would misrepresent them. The text form renders this with 6 decimal places, so it does not round-trip an arbitrary double. The binary form carries the exact bit pattern and does. That asymmetry is the text form's, not the IR's. |
modulus | std::optional<uint64_t> modulus | Modulus, absent on the _NI forms and on halt. Normally an index into the program's modulus table; modulus_is_literal marks the case where a writer had no table entry and emitted the prime itself. The text form renders both as a bare m=<number> and so cannot tell them apart — only this flag can. |
modulus_is_literal | bool modulus_is_literal = false | |
omega | std::optional<uint64_t> omega | Root of unity for sr_ntt / sr_intt. A Niobium extension: the specification defines the transforms abstractly and says nothing about how omega is obtained. When present a consumer should prefer it over deriving one, so dropping it changes results rather than saving space. |
mask | std::optional<uint64_t> mask | sr_automorph_eval's mask and log2(ring dimension). |
logn | std::optional<uint8_t> logn | |
k | std::optional<uint64_t> k | Automorphism index, and the rotation offset of sr_rot_automorph_coeff. Distinct fields because they are distinct operands, even though no instruction carries both. |
offset | std::optional<uint64_t> offset | |
line_number | int line_number = 0 | 1-based line in the source text, or 0 when there was none (a builder produced this instruction directly). For diagnostics only. |
niobium::fhetch::kMaxAddress
constexpr uint64_t kMaxAddress = UINT32_MAXLargest address an Instruction can hold. A reader rejects anything wider instead of truncating it into a different, valid-looking address.
niobium::fhetch::kMaxModulusIndex
constexpr uint64_t kMaxModulusIndex = 255Largest modulus index the Niobium ISA can encode (its mod_index field is 8 bits). A literal modulus is unaffected — it is a prime, not an index. Exposed so a reader can reject an out-of-range index at the door rather than letting it fail later during encoding.
Program
struct niobium::fhetch::TraceMetadata
struct niobium::fhetch::TraceMetadataProgram and build metadata.
Both serializations carry this: the text form writes it as the leading # comment block, the binary form as TLV entries (one per FhMetaTag, see fhetch_encoding.h). It lives here rather than with either encoding because it describes the program, not the file. Every field default-constructs to the value a writer treats as "absent", which is why empty strings and zeros are omitted rather than written as empty entries.
Fields
| Name | Declaration | Description |
|---|---|---|
program_name | std::string program_name | |
program_version | std::string program_version | Version the text header renders as # Program: <name> v<version>. Held apart from the name so neither side has to glue the two together or split them back out. |
trace_file | std::string trace_file | |
description | std::string description | |
source_file | std::string source_file | |
source_line | int source_line = 0 | |
build_timestamp | std::string build_timestamp | |
instruction_count | int instruction_count = 0 | |
generated_timestamp | uint64_t generated_timestamp = 0 | |
scheme | std::string scheme | Crypto context. |
ring_dimension | int ring_dimension = 0 | |
multiplicative_depth | int multiplicative_depth = 0 | |
security_level | std::string security_level | |
modulus_chain | std::vector<uint64_t> modulus_chain | |
modulus_chain_length | int modulus_chain_length = 0 |
struct niobium::fhetch::Annotation
struct niobium::fhetch::AnnotationA comment the reader kept but did not interpret.
Comments are legal in the text form and are handled entirely by the reader — they are never instructions. Most are prose, but two carry structured data the recorder smuggled through them (# output <name> addr and # switchmodulus ...). Whether anything depends on those is unsettled, so they are preserved verbatim here rather than discarded, which keeps the question open without giving comments a place in the instruction stream.
Fields
| Name | Declaration | Description |
|---|---|---|
line_number | int line_number = 0 | |
text | std::string text | comment body, with the leading '#' and space removed |
after_instruction | size_t after_instruction = 0 | How many instructions precede this comment. A writer emits the annotation once that many instructions have been written, which is what makes a read/write round trip reproduce the file rather than gather all the comments at one end. Recorded traces interleave heavily — a measured CKKS bootstrap carries 82,719 comments between its instructions — so position is content, not decoration. |
struct niobium::fhetch::Program
struct niobium::fhetch::ProgramA whole FHETCH program in memory — what a reader produces, a writer consumes, and a consumer walks. Source-format-independent by construction: the same program read from .fhetch and from .fhex gives equal instructions.
Fields
| Name | Declaration | Description |
|---|---|---|
metadata | TraceMetadata metadata | |
modulus_table | std::vector<uint64_t> modulus_table | Moduli the program's m=<index> operands refer to. Index 0 is by convention the copy/zero-init sentinel (0xFFFFFFFFFFFFFFFF), not a prime. |
instructions | std::vector<Instruction> instructions | |
annotations | std::vector<Annotation> annotations |
Something not looking right? Suggest a change on GitHub.