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.

ValueDescription
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.

ValueDescription
Nonehalt
PolyPolydest, src1, src2, m=
PolyPolyNIdest, src1, src2
PolyScalardest, src1, imm, m=
PolyScalarNIdest, src1, imm
UnaryModdest, src1, m=
UnaryNoModdest, src1
Nttdest, src1, m= [, omega=]
AutomorphEvaldest, src1, m= [, mask=, logn=], k=
AutomorphCoeffdest, src1, k=, m=
RotAutomorphCoeffdest, src1, offset=, m=

struct niobium::fhetch::OpcodeInfo

struct niobium::fhetch::OpcodeInfo

One 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

NameDeclarationDescription
opcodeFhOpcode opcode
mnemonicconst char * mnemonic
formOperandForm 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::Instruction

One 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

NameDeclarationDescription
opcodeFhOpcode opcode = FH_ILL
destuint32_t dest = 0Polynomial 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.
src1uint32_t src1 = 0
src2uint32_t src2 = 0
immediatestd::optional<uint64_t> immediateScalar immediate, for the integer poly-scalar forms.
fp_immediatestd::optional<double> fp_immediateScalar 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.
modulusstd::optional<uint64_t> modulusModulus, 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_literalbool modulus_is_literal = false
omegastd::optional<uint64_t> omegaRoot 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.
maskstd::optional<uint64_t> masksr_automorph_eval's mask and log2(ring dimension).
lognstd::optional<uint8_t> logn
kstd::optional<uint64_t> kAutomorphism index, and the rotation offset of sr_rot_automorph_coeff. Distinct fields because they are distinct operands, even though no instruction carries both.
offsetstd::optional<uint64_t> offset
line_numberint line_number = 01-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_MAX

Largest 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 = 255

Largest 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::TraceMetadata

Program 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

NameDeclarationDescription
program_namestd::string program_name
program_versionstd::string program_versionVersion 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_filestd::string trace_file
descriptionstd::string description
source_filestd::string source_file
source_lineint source_line = 0
build_timestampstd::string build_timestamp
instruction_countint instruction_count = 0
generated_timestampuint64_t generated_timestamp = 0
schemestd::string schemeCrypto context.
ring_dimensionint ring_dimension = 0
multiplicative_depthint multiplicative_depth = 0
security_levelstd::string security_level
modulus_chainstd::vector<uint64_t> modulus_chain
modulus_chain_lengthint modulus_chain_length = 0

struct niobium::fhetch::Annotation

struct niobium::fhetch::Annotation

A 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

NameDeclarationDescription
line_numberint line_number = 0
textstd::string textcomment body, with the leading '#' and space removed
after_instructionsize_t after_instruction = 0How 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::Program

A 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

NameDeclarationDescription
metadataTraceMetadata metadata
modulus_tablestd::vector<uint64_t> modulus_tableModuli the program's m=<index> operands refer to. Index 0 is by convention the copy/zero-init sentinel (0xFFFFFFFFFFFFFFFF), not a prime.
instructionsstd::vector<Instruction> instructions
annotationsstd::vector<Annotation> annotations

Something not looking right? Suggest a change on GitHub.