niobium/fhetch_api.h
FHETCH Polynomial IR — C++ API
Spec reference: https://fhetch.org
"Polynomial Intermediate Representation for Fully Homomorphic Encryption"
(FHETCH Consortium, 2025)Include: #include "niobium/fhetch_api.h"
Enumerations
enum niobium::fhetch::NumberType
enum class NumberType { Integer, NonInteger }Whether polynomial/scalar components are integer or non-integer. Integer: components modulo prime/composite integer moduli. NonInteger: fixed-point or floating-point components.
| Value | Description |
|---|---|
Integer | |
NonInteger |
enum niobium::fhetch::Format
enum class Format { Coefficient, Evaluation }Polynomial representation domain. Coefficient: time-domain (standard polynomial coefficients). Evaluation: frequency-domain (NTT/FFT point values).
| Value | Description |
|---|---|
Coefficient | |
Evaluation |
Type aliases
niobium::fhetch::ModuliBase
using ModuliBase = std::vector<uint64_t>An ordered set of prime moduli defining an RNS base.
Hardware capabilities (information flowing upward from hardware)
struct niobium::fhetch::HardwareCapabilities
struct niobium::fhetch::HardwareCapabilitiesCapabilities advertised by a hardware target to the compiler/library.
Fields
| Name | Declaration | Description |
|---|---|---|
supported_ring_dimensions | std::vector<uint64_t> supported_ring_dimensions | |
max_modulus_bits | int max_modulus_bits = 63 | |
max_modulus_chain_length | int max_modulus_chain_length = 64 | |
supported_gadgets | std::vector<std::string> supported_gadgets | |
supported_optional_ops | std::vector<std::string> supported_optional_ops |
Program parameters (information flowing downward to hardware)
struct niobium::fhetch::ProgramParameters
struct niobium::fhetch::ProgramParametersParameter choices flowing from the compiler/library to the hardware.
Fields
| Name | Declaration | Description |
|---|---|---|
ring_dimension | uint64_t ring_dimension = 0 | |
rns_primes | ModuliBase rns_primes | |
representation | Format representation = Format::Evaluation | |
coefficient_precision_bits | int coefficient_precision_bits = 64 | |
prime_congruence | uint64_t prime_congruence = 0 |
Basic Data Type: Polynomial (single-residue polynomial, SRP)
class niobium::fhetch::Polynomial
class niobium::fhetch::PolynomialOpaque polynomial type representing a vector of components (coefficients or evaluation-point values) under a single modulus.
Corresponds to the "Polynomial" basic data type in the FHETCH Polynomial IR specification. Integer polynomials operate modulo a prime q; non-integer polynomials use fixed-point or floating-point components.
Polynomial
Polynomial()Construct an empty (invalid) polynomial.
Polynomial(const Polynomial &other)Polynomial(Polynomial &&other) noexcept~Polynomial
~Polynomial()operator=
Polynomial & operator=(const Polynomial &other)Polynomial & operator=(Polynomial &&other) noexceptzeros
static Polynomial zeros(uint64_t ring_dim, Format fmt=Format::Evaluation)Construct a zero-initialized integer polynomial.
Parameters
ring_dim— Number of components (ring dimension N).fmt— Representation domain.
zeros_ni
static Polynomial zeros_ni(uint64_t ring_dim, Format fmt=Format::Evaluation)Construct a zero-initialized non-integer polynomial.
from_data
static Polynomial from_data(const std::vector<uint64_t> &components, uint64_t ring_dim, Format fmt=Format::Evaluation)Construct an integer polynomial from raw component data.
Parameters
components— Vector of uint64_t values (length = ring_dim).ring_dim— Ring dimension N.fmt— Representation domain.
static Polynomial from_data(std::vector<uint64_t> &&components, uint64_t ring_dim, Format fmt=Format::Evaluation)Construct an integer polynomial by taking ownership of the caller's component buffer. The buffer is moved into the polynomial's internal storage; no copy is performed. Caller must ensure components.size() == ring_dim; the constructor does not validate (matches the const-ref overload's pre-existing behavior).
from_data_ni
static Polynomial from_data_ni(const std::vector<double> &components, uint64_t ring_dim, Format fmt=Format::Evaluation)Construct a non-integer polynomial from raw component data.
ring_dimension
uint64_t ring_dimension() constReturns: Ring dimension N.
number_type
NumberType number_type() constReturns: Whether this is an integer or non-integer polynomial.
format
Format format() constReturns: Current representation domain (coefficient or evaluation).
is_valid
bool is_valid() constReturns: true if the polynomial holds valid data.
operator bool
explicit operator bool() constint_data
const std::vector<uint64_t> & int_data() constInteger-component values. Throws std::runtime_error if the polynomial is non-integer or invalid. Length == ring_dimension().
impl
PolynomialImpl * impl() constAccess the internal implementation (for Niobium internals only).
Basic Data Type: Scalar
class niobium::fhetch::Scalar
class niobium::fhetch::ScalarOpaque scalar type representing a single value under a modulus (integer) or a fixed-point/floating-point value (non-integer).
Scalar
Scalar()Scalar(const Scalar &other)Scalar(Scalar &&other) noexcept~Scalar
~Scalar()operator=
Scalar & operator=(const Scalar &other)Scalar & operator=(Scalar &&other) noexceptfrom_int
static Scalar from_int(uint64_t value)Construct an integer scalar with a given value.
from_double
static Scalar from_double(double value)Construct a non-integer scalar with a given value.
number_type
NumberType number_type() constReturns: Whether this is an integer or non-integer scalar.
is_valid
bool is_valid() constoperator bool
explicit operator bool() constimpl
ScalarImpl * impl() constSRP Array (SRPA) — array of single-residue polynomials
class niobium::fhetch::SRPArray
class niobium::fhetch::SRPArrayOrdered array of single-residue polynomials.
SRPArray
SRPArray()Construct an empty array (length 0).
SRPArray(const SRPArray &other)SRPArray(SRPArray &&other) noexceptexplicit SRPArray(size_t n)Construct an array of n default-initialized polynomials.
SRPArray(std::initializer_list<Polynomial> polys)Construct from a list of polynomials.
~SRPArray
~SRPArray()operator=
SRPArray & operator=(const SRPArray &other)SRPArray & operator=(SRPArray &&other) noexceptlength
size_t length() constReturns: Number of elements.
operator[]
Polynomial & operator[](size_t i)Access element at index i. Throws std::out_of_range if i >= length().
const Polynomial & operator[](size_t i) constappend
void append(const Polynomial &p)Append a polynomial to the end of the array.
is_valid
bool is_valid() constoperator bool
explicit operator bool() constimpl
SRPArrayImpl * impl() constMulti-Residue Polynomial (MRP)
class niobium::fhetch::MRP
class niobium::fhetch::MRPA set of single-residue polynomials together with a set of moduli (the "base"), where each polynomial is associated with one modulus.
Indexed by modulus value: mrp[q] returns the polynomial for prime q.
MRP
MRP()Construct an empty MRP with an empty base.
MRP(const MRP &other)MRP(MRP &&other) noexceptexplicit MRP(const ModuliBase &base, uint64_t ring_dim=0)Construct an MRP with a given base (zero-initialized polynomials).
Parameters
base— Ordered set of prime moduli.ring_dim— Ring dimension N for each polynomial.
~MRP
~MRP()operator=
MRP & operator=(const MRP &other)MRP & operator=(MRP &&other) noexceptfrom_pairs
static MRP from_pairs(const std::vector<std::pair<Polynomial, uint64_t>> &pairs)Construct an MRP from explicit (polynomial, modulus) pairs.
base
const ModuliBase & base() constReturns: The moduli base (ordered set of primes).
num_residues
size_t num_residues() constReturns: Number of residues (== base().size()).
operator[]
Polynomial & operator[](uint64_t q)Access the polynomial for modulus q. Throws if q is not in the base.
const Polynomial & operator[](uint64_t q) constis_valid
bool is_valid() constoperator bool
explicit operator bool() constimpl
MRPImpl * impl() constMulti-Residue Scalar (MRS)
class niobium::fhetch::MRS
class niobium::fhetch::MRSA set of scalars together with a set of moduli, analogous to MRP but for scalar values.
MRS
MRS()MRS(const MRS &other)MRS(MRS &&other) noexceptexplicit MRS(const ModuliBase &base)Construct an MRS with a given base (zero-initialized scalars).
~MRS
~MRS()operator=
MRS & operator=(const MRS &other)MRS & operator=(MRS &&other) noexceptfrom_pairs
static MRS from_pairs(const std::vector<std::pair<Scalar, uint64_t>> &pairs)Construct from explicit (scalar, modulus) pairs.
base
const ModuliBase & base() constReturns: The moduli base.
num_residues
size_t num_residues() constReturns: Number of residues.
operator[]
Scalar & operator[](uint64_t q)Access the scalar for modulus q.
const Scalar & operator[](uint64_t q) constis_valid
bool is_valid() constoperator bool
explicit operator bool() constimpl
MRSImpl * impl() constMRP Array (MRPA)
class niobium::fhetch::MRPArray
class niobium::fhetch::MRPArrayAn array of multi-residue polynomials. Each element may have a different base and number of residues.
MRPArray
MRPArray()Construct an empty array (length 0).
MRPArray(const MRPArray &other)MRPArray(MRPArray &&other) noexceptexplicit MRPArray(size_t n)Construct an array of n default-initialized MRPs.
MRPArray(std::initializer_list<MRP> mrps)Construct from a list of MRPs.
~MRPArray
~MRPArray()operator=
MRPArray & operator=(const MRPArray &other)MRPArray & operator=(MRPArray &&other) noexceptlength
size_t length() constReturns: Number of elements.
operator[]
MRP & operator[](size_t i)Access element at index i. Throws std::out_of_range if i >= length().
const MRP & operator[](size_t i) constappend
void append(const MRP &m)Append an MRP to the end of the array.
is_valid
bool is_valid() constoperator bool
explicit operator bool() constimpl
MRPArrayImpl * impl() constINPUT / OUTPUT TAGGING
niobium::fhetch::tag_input
void tag_input(const std::string &name, const Polynomial &p)Mark a single-residue polynomial as a named input. The name is used as the key in the inputs JSON file and registers the polynomial data for serialization during replay index creation.
niobium::fhetch::tag_output
void tag_output(const std::string &name, const Polynomial &p)Mark a single-residue polynomial as a named output (probe point). The name is used as the key in the outputs JSON file.
niobium::fhetch::tag_input
void tag_input(const std::string &name, const MRP &m)Mark all residues of an MRP as named inputs.
niobium::fhetch::tag_output
void tag_output(const std::string &name, const MRP &m)Mark all residues of an MRP as named outputs.
niobium::fhetch::tag_input
void tag_input(const std::string &name, const SRPArray &arr)Mark all elements of an SRP array as named inputs. SRPArray is indexed by position (not modulus), so it can hold polynomials with duplicate moduli — unlike MRP.
niobium::fhetch::tag_output
void tag_output(const std::string &name, const SRPArray &arr)Mark all elements of an SRP array as named outputs.
niobium::fhetch::tag_input
void tag_input(const std::string &name, const MRPArray &arr)Mark all elements of an MRP array as named inputs.
niobium::fhetch::tag_output
void tag_output(const std::string &name, const MRPArray &arr)Mark all elements of an MRP array as named outputs.
niobium::fhetch::reset_for_epoch
void reset_for_epoch()Reset FHETCH state for a new epoch. Clears input/output registries, resets address allocator to 0, and invalidates the replay output cache.
niobium::fhetch::get_input_ring_dimension
uint64_t get_input_ring_dimension()Get the ring dimension from the first registered input (0 if none). Used internally to populate synthetic crypto_context for replay.
niobium::fhetch::save_input_data
void save_input_data()Save all named input data to JSON files. Writes per-input files and a master index to the program directory in the format expected by the replay engine. Called automatically during create_replay_index() in FHETCH mode.
niobium::fhetch::save_probe_outputs
void save_probe_outputs()Save all named probe outputs to a JSON file. Writes to <program_dir>/<program_name>.outputs.json using the same format as the regular compiler flow. Called automatically during Compiler::stop() in FHETCH mode, or can be called explicitly.
RESULT RETRIEVAL (after replay)
niobium::fhetch::result
bool result(const std::string &name, Polynomial &p)Retrieve a single-residue polynomial result from replay output. Reads from replay_outputs_integer.json, finds the named output, and populates the Polynomial with computed coefficient values.
Parameters
name— The output name used in tag_output().p— Polynomial to populate with result data.
Returns: true if the result was found and populated successfully.
niobium::fhetch::result
bool result(const std::string &name, MRP &m)Retrieve a multi-residue polynomial result from replay output.
Parameters
name— The output name used in tag_output().m— MRP to populate with result data.
Returns: true if the result was found and populated successfully.
niobium::fhetch::result
bool result(const std::string &name, MRPArray &arr)Retrieve an MRP array result from replay output.
Parameters
name— The output name used in tag_output().arr— MRPArray to populate with result data.
Returns: true if the result was found and populated successfully.
BASELINE INSTRUCTIONS (required by all compliant hardware)
niobium::fhetch::sr_addp
Polynomial sr_addp(const Polynomial &a, const Polynomial &b, uint64_t q)Polynomial addition: f_i = (a_i + b_i) mod q.
niobium::fhetch::sr_addps
Polynomial sr_addps(const Polynomial &a, const Scalar &s, uint64_t q)Polynomial scalar addition (evaluation representation): f_i = (a_i + s) mod q
niobium::fhetch::sr_addps_coeff
Polynomial sr_addps_coeff(const Polynomial &a, const Scalar &s, uint64_t q)Polynomial scalar addition (coefficient representation): f_0 = (a_0 + s) mod q, f_{i>0} = a_i
niobium::fhetch::sr_negp
Polynomial sr_negp(const Polynomial &a, uint64_t q)Polynomial negation: f_i = (-a_i) mod q.
niobium::fhetch::sr_subp
Polynomial sr_subp(const Polynomial &a, const Polynomial &b, uint64_t q)Polynomial subtraction: f_i = (a_i - b_i) mod q.
niobium::fhetch::sr_subps
Polynomial sr_subps(const Polynomial &a, const Scalar &s, uint64_t q)Polynomial scalar subtraction (evaluation representation): f_i = (a_i - s) mod q
niobium::fhetch::sr_subps_coeff
Polynomial sr_subps_coeff(const Polynomial &a, const Scalar &s, uint64_t q)Polynomial scalar subtraction (coefficient representation): f_0 = (a_0 - s) mod q, f_{i>0} = a_i
niobium::fhetch::sr_mulp
Polynomial sr_mulp(const Polynomial &a, const Polynomial &b, uint64_t q)Polynomial (component-wise) multiplication: f_i = (a_i * b_i) mod q This is the polynomial product only when both a and b are in evaluation representation.
niobium::fhetch::sr_mulps
Polynomial sr_mulps(const Polynomial &a, const Scalar &s, uint64_t q)Polynomial scalar multiplication: f_i = (a_i * s) mod q.
niobium::fhetch::sr_ntt
Polynomial sr_ntt(const Polynomial &a, uint64_t q)Negacyclic Number Theoretic Transform. Takes a from coefficient representation to evaluation representation relative to modulus q, using a 2N-th primitive root of unity psi mod q.
niobium::fhetch::sr_intt
Polynomial sr_intt(const Polynomial &a, uint64_t q)Inverse Negacyclic Number Theoretic Transform. Takes a from evaluation representation to coefficient representation relative to modulus q.
niobium::fhetch::sr_permute
Polynomial sr_permute(const Polynomial &a, const std::vector<uint64_t> &srcs, const std::vector<int> &signs, uint64_t q)General permutation with sign flips.
Parameters
a— Input polynomial.srcs— Source index for each output position (values in [0, N-1]).signs— Sign flip for each position (+1 or -1).q— Modulus (used for sign flip via q - value).
Returns: f_i = signs[i] * a[srcs[i]] (mod q when signs[i] == -1).
niobium::fhetch::halt
void halt()Halt: signal the hardware to stop and notify the host.
Non-integer polynomial arithmetic (no modulus)
niobium::fhetch::sr_addp_ni
Polynomial sr_addp_ni(const Polynomial &a, const Polynomial &b)Non-integer polynomial addition: f_i = a_i + b_i.
niobium::fhetch::sr_addps_ni
Polynomial sr_addps_ni(const Polynomial &a, const Scalar &s)Non-integer polynomial scalar addition (evaluation): f_i = a_i + s.
niobium::fhetch::sr_addps_coeff_ni
Polynomial sr_addps_coeff_ni(const Polynomial &a, const Scalar &s)Non-integer polynomial scalar addition (coefficient): f_0 = a_0 + s, f_{i>0} = a_i
niobium::fhetch::sr_negp_ni
Polynomial sr_negp_ni(const Polynomial &a)Non-integer polynomial negation: f_i = -a_i.
niobium::fhetch::sr_subp_ni
Polynomial sr_subp_ni(const Polynomial &a, const Polynomial &b)Non-integer polynomial subtraction: f_i = a_i - b_i.
niobium::fhetch::sr_subps_ni
Polynomial sr_subps_ni(const Polynomial &a, const Scalar &s)Non-integer polynomial scalar subtraction (evaluation): f_i = a_i - s.
niobium::fhetch::sr_subps_coeff_ni
Polynomial sr_subps_coeff_ni(const Polynomial &a, const Scalar &s)Non-integer polynomial scalar subtraction (coefficient): f_0 = a_0 - s, f_{i>0} = a_i
niobium::fhetch::sr_mulp_ni
Polynomial sr_mulp_ni(const Polynomial &a, const Polynomial &b)Non-integer polynomial multiplication: f_i = a_i * b_i.
niobium::fhetch::sr_mulps_ni
Polynomial sr_mulps_ni(const Polynomial &a, const Scalar &s)Non-integer polynomial scalar multiplication: f_i = a_i * s.
Fourier Transforms (alternative to NTT for TFHE/FHEW)
niobium::fhetch::sr_ft
Polynomial sr_ft(const Polynomial &a)Negacyclic Fourier Transform (complex-valued). Alternative to sr_ntt for hardware supporting only FHEW/TFHE schemes.
niobium::fhetch::sr_ift
Polynomial sr_ift(const Polynomial &a)Inverse Negacyclic Fourier Transform (complex-valued). Must be used if and only if sr_ft is used in place of sr_ntt.
Coefficient access
niobium::fhetch::sr_coeff_extract
Scalar sr_coeff_extract(const Polynomial &p, uint64_t i)Extract the i-th component of polynomial p as a scalar.
niobium::fhetch::sr_coeff_assign
Polynomial sr_coeff_assign(const Polynomial &p, uint64_t i, const Scalar &val)Return a new polynomial with component at position i set to val.
Torus and sample operations (TFHE/FHEW)
niobium::fhetch::sr_torus_mod_reduce
Polynomial sr_torus_mod_reduce(const Polynomial &p, double c)Torus modular reduction. Returns a polynomial with coefficients in [c-0.5, c+0.5) equal to those of p up to an integer polynomial.
niobium::fhetch::sr_sample_extract
std::vector<uint64_t> sr_sample_extract(const SRPArray &rlwe, uint64_t lwe_dim)Sample extraction (TFHE/FHEW). Takes an RLWE cipher (a, b) as an SRPArray of length 2 and returns an LWE cipher as a vector of integer components.
GADGETS — Polynomial level (Class I: simple, verifiable by inspection)
niobium::fhetch::bind_modulus
void bind_modulus(const Polynomial &p, uint64_t q)Record the address→modulus metadata binding for p without emitting an instruction (upgrades a copy-sentinel binding; never downgrades a real one). Use after recording a canonical sentinel-modulus op (pass-through copy, eval-form automorphism) when the residue's real modulus is known: captured output shapes need it for transport probe serialization.
niobium::fhetch::sr_automorph_eval
Polynomial sr_automorph_eval(const Polynomial &x, uint64_t k)Galois automorphism in evaluation representation.
Parameters
x— Integer polynomial in evaluation form.k— Odd integer in [1, 2N-1].
niobium::fhetch::sr_automorph_eval
Polynomial sr_automorph_eval(const Polynomial &x, uint64_t k, uint64_t q)Galois automorphism in evaluation representation, bound to the residue's modulus q. Prefer this overload when the modulus is known: it records the address-to-modulus binding the transport replay's trace-modulus contract requires for outputs (the modulus-less overload emits the copy sentinel, whose outputs cannot be probe-serialized by nbcc_fhetch_replay).
niobium::fhetch::sr_automorph_coeff
Polynomial sr_automorph_coeff(const Polynomial &x, uint64_t k, uint64_t q)Galois automorphism in coefficient representation.
Parameters
x— Integer polynomial in coefficient form.k— Odd integer in [1, 2N-1].q— Modulus.
niobium::fhetch::sr_rot_automorph_coeff
Polynomial sr_rot_automorph_coeff(const Polynomial &x, uint64_t offset, uint64_t q)Negacyclic rotation automorphism in coefficient representation.
Parameters
x— Integer polynomial in coefficient form.offset— Rotation offset in [0, N-1].q— Modulus.
niobium::fhetch::sr_batch_ft
SRPArray sr_batch_ft(const SRPArray &x)Batch forward Fourier/NTT transform over an SRP array.
niobium::fhetch::sr_batch_ift
SRPArray sr_batch_ift(const SRPArray &x)Batch inverse Fourier/NTT transform over an SRP array.
GADGETS — Multi-Residue basic arithmetic
niobium::fhetch::mr_addp
MRP mr_addp(const MRP &x, const MRP &y)MRP addition: for each q in base, z[q] = sr_addp(x[q], y[q], q). Both operands must share the same base.
niobium::fhetch::mr_subp
MRP mr_subp(const MRP &x, const MRP &y)MRP subtraction: for each q in base, z[q] = sr_subp(x[q], y[q], q).
niobium::fhetch::mr_mulp
MRP mr_mulp(const MRP &x, const MRP &y)MRP multiplication: for each q in base, z[q] = sr_mulp(x[q], y[q], q).
niobium::fhetch::mr_mulps
MRP mr_mulps(const MRP &x, const MRS &s)MRP-scalar multiplication: z[q] = sr_mulps(x[q], s[q], q).
niobium::fhetch::mr_addps
MRP mr_addps(const MRP &x, const MRS &s)MRP-scalar addition: z[q] = sr_addps(x[q], s[q], q).
niobium::fhetch::mr_subps
MRP mr_subps(const MRP &x, const MRS &s)MRP-scalar subtraction (poly minus scalar, per residue): z[q] = sr_subps(x[q], s[q], q) i.e. z[q]_i = (x[q]_i - s[q]) mod q. There is no scalar-minus-poly variant; build it from sr_negp + mr_subps at the call site if needed.
niobium::fhetch::mr_ntt
MRP mr_ntt(const MRP &x)MRP NTT: apply sr_ntt to each residue.
niobium::fhetch::mr_intt
MRP mr_intt(const MRP &x)MRP inverse NTT: apply sr_intt to each residue.
niobium::fhetch::mr_automorph_eval
MRP mr_automorph_eval(const MRP &x, uint64_t k)MRP Galois automorphism (X -> X^k) in evaluation representation: z[q] = sr_automorph_eval(x[q], k) for every q in x.base(). k must be odd in [1, 2N-1]. The action is a pure permutation of the 2N-th-root-of-unity evaluation slots and is independent of q (sr_automorph_eval uses COPY_MODULUS), so the per- residue lift is trivial. This is NOT the same as mr_rot_automorph_coeff: that op operates in coefficient form and performs a negacyclic shift; this op permutes evaluation-form slots.
niobium::fhetch::mr_rot_automorph_coeff
MRP mr_rot_automorph_coeff(const MRP &x, uint64_t offset)MRP negacyclic rotation in coefficient representation: z[q] = sr_rot_automorph_coeff(x[q], offset, q) for every q in x.base(). offset is in [0, N-1]. Per residue: z[q][i] = signs[i] * x[q][(i + offset) mod N] signs[i] = (-1)((i + offset) // N) i.e. each output coefficient reads from offset slots ahead in the input, with a sign flip when the read wraps past N (because XN = -1). This is multiplication by X{-offset} in R_q = Z_q[X]/(XN+1) — a LEFT shift of the coefficient vector. Operates on coefficient-form polys; the eval-form Galois automorphism above is the separate op for slot rotation.
niobium::fhetch::mr_zeros
MRP mr_zeros(const ModuliBase &target_base, uint64_t ring_dim)Construct a zero-initialized MRP with the given base and ring dimension.
GADGETS — MRP residue manipulation
niobium::fhetch::mr_append_srp
MRP mr_append_srp(const MRP &x, const Polynomial &a, uint64_t q_a)Append a single-residue polynomial to an MRP under modulus q_a. Returns a new MRP with base = x.base ∪ {q_a}.
niobium::fhetch::mr_union
MRP mr_union(const MRP &x, const MRP &y)Union of two MRPs with mutually exclusive bases. Returns a new MRP with base = x.base ∪ y.base.
niobium::fhetch::mr_subset
MRP mr_subset(const MRP &x, const ModuliBase &subbase)Subset of an MRP restricted to a sub-base.
Parameters
subbase— Must be a subset of x.base().
GADGETS — Fast Base Conversion and CKKS Rescale
enum niobium::fhetch::FbcVariant
enum class FbcVariant { Standard, ReducedNoise }Approximate-FBC algorithm variant.
- Standard: y[p] = sum_i ((x[q_i] * q_hat[i]) mod q_i) * q_star[i,p] mod p Matches the default OpenFHE ApproxSwitchCRTBasis path (compile-time WITH_REDUCED_NOISE=OFF). Cited in Cheon-Han-Kim-Kim-Song SAC 2018 §4.1 (BasisExtension).
- ReducedNoise: same, but the scaled residue is rebased from q_i to p via signed-preserving SwitchModulus before the q_star multiply. Matches OpenFHE's ApproxSwitchCRTBasis when compiled with WITH_REDUCED_NOISE=ON. Produces ciphertexts with ~1 bit less FBC noise at the cost of extra muli/addi instructions per (source prime, target prime) pair.
Callers wanting bit-parity with a specific OpenFHE build can gate the variant at the call site; the 2-arg overloads default to ReducedNoise, so programs linked against the niobium-instrumented OpenFHE (which ships with WITH_REDUCED_NOISE=ON) match the CPU reference without passing the variant explicitly.
| Value | Description |
|---|---|
Standard | |
ReducedNoise |
enum niobium::fhetch::FbcCenterShape
enum class FbcCenterShape { ThreeOp, FourOp }Lowering shape for the FBC center_mod_q_into_p (signed-center + cross-prime rebase) gadget, selectable at the call site like FbcVariant. ThreeOp — the simulator-native shift / rebase / unshift lowering (sr_addps, sr_mulps imm=1, sr_addps): three instructions. FourOp — the same, prefixed by a leading sr_mulps imm=1 IDENTITY so the op chain forms the muli/addi/muli/addi quadruple the hardware replay driver (fhetch_driver) recognizes and substitutes with a single SwitchModulus (Montgomery under --niobium_hw): four ops. Both lower to the identical value (the leading multiply is by 1), so a trace replays bit-for-bit the same either way; FourOp only exists to be recognized on the hardware replay path. ThreeOp is the default — local-sim/ordinary replay never does the substitution, so the identity is dead weight there (it is ~half of all sr_mulps in a deep CKKS trace).
| Value | Description |
|---|---|
ThreeOp | |
FourOp |
niobium::fhetch::fast_base_convert
MRP fast_base_convert(const MRP &x, const ModuliBase &target_base, FbcVariant variant, FbcCenterShape shape)Fast base conversion. Converts a coefficient-mode MRP from its current base to target_base using the CRT-based approximate conversion algorithm.
Parameters
x— Coefficient-mode MRP in source base.target_base— Target moduli base.variant— FBC algorithm variant (see FbcVariant).shape— FBC center lowering shape (see FbcCenterShape).
niobium::fhetch::fast_base_convert
MRP fast_base_convert(const MRP &x, const ModuliBase &target_base, FbcVariant variant)Fast base conversion (default center shape = ThreeOp). Equivalent to fast_base_convert(x, target_base, variant, FbcCenterShape::ThreeOp).
niobium::fhetch::fast_base_convert
MRP fast_base_convert(const MRP &x, const ModuliBase &target_base)Fast base conversion (default variant). Equivalent to fast_base_convert(x, target_base, FbcVariant::ReducedNoise) — the variant that matches the niobium-instrumented OpenFHE's CPU-reference output. Retained as a distinct symbol so binaries built against pre-variant headers keep linking without rebuild.
niobium::fhetch::rescale_fbc
MRP rescale_fbc(const MRP &x, const ModuliBase &rescale_base, FbcVariant variant, FbcCenterShape shape)CKKS rescale using fast base conversion. Removes the residues in rescale_base from x and rescales.
Parameters
x— Coefficient-mode MRP.rescale_base— Moduli to remove (subset of x.base()).variant— FBC algorithm variant used by the internal lift (see FbcVariant).shape— FBC center lowering shape (see FbcCenterShape).
Returns: MRP in base = x.base() \ rescale_base.
niobium::fhetch::rescale_fbc
MRP rescale_fbc(const MRP &x, const ModuliBase &rescale_base, FbcVariant variant)CKKS rescale (default center shape = ThreeOp). Equivalent to rescale_fbc(x, rescale_base, variant, FbcCenterShape::ThreeOp).
niobium::fhetch::rescale_fbc
MRP rescale_fbc(const MRP &x, const ModuliBase &rescale_base)CKKS rescale (default variant + center shape). Equivalent to rescale_fbc(x, rescale_base, FbcVariant::ReducedNoise, FbcCenterShape::ThreeOp). Retained as a distinct symbol for ABI compatibility (see fast_base_convert overload).
GADGETS — MRP Array operations
niobium::fhetch::mrpa_dotproduct
MRP mrpa_dotproduct(const MRPArray &x, const MRPArray &y)MRPA dot-product. Returns a single MRP that is the sum of element-wise MRP products: z = sum_i mr_mulp(x[i], y[i]) Both arrays must have the same length. All MRPs must share the same base.
GADGETS — Decomposition (key-switching and TFHE)
niobium::fhetch::dig_decomp
MRPArray dig_decomp(const MRP &x, const std::vector<ModuliBase> &digit_bases, const ModuliBase &p_base, FbcCenterShape shape)CKKS/BGV/BFV digit decomposition for hybrid key-switching.
Parameters
x— Coefficient-mode MRP in RNS base representing Q.digit_bases— List of d mutually exclusive moduli sub-bases.p_base— RNS base for the temporary modulus P.shape— FBC center lowering shape for the internal lift (see FbcCenterShape); defaults to ThreeOp.
Returns: MRPA of length d; element i is base-extended from digit_bases[i] to (x.base() ∪ p_base).
niobium::fhetch::dig_decomp
MRPArray dig_decomp(const MRP &x, const std::vector<ModuliBase> &digit_bases, const ModuliBase &p_base)dig_decomp with default center shape = ThreeOp.
niobium::fhetch::gadget_decomp
SRPArray gadget_decomp(const Polynomial &x, uint64_t base, uint64_t n_levels)TFHE gadget decomposition (unsigned integer version).
Parameters
x— Single-residue polynomial with integer coefficients.base— Decomposition basis B.n_levels— Number of decomposition levels l.
Returns: SRPArray of length n_levels.
niobium::fhetch::gadget_decomp_pow2
SRPArray gadget_decomp_pow2(const Polynomial &x, uint64_t log_base, uint64_t n_levels)TFHE gadget decomposition for power-of-two base.
Parameters
x— Single-residue polynomial with integer coefficients.log_base— log2 of the decomposition basis.n_levels— Number of decomposition levels l.
Returns: SRPArray of length n_levels.
GADGETS — GSW/RLWE External Product (requires optional TFHE ops)
niobium::fhetch::gsw_rlwe_ext_prod
SRPArray gsw_rlwe_ext_prod(const SRPArray &gsw, const SRPArray &rlwe_in, uint64_t l, uint64_t base)GSW/RLWE external product.
Parameters
gsw— GSW ciphertext as SRPArray of 4*l polynomials.rlwe_in— RLWE ciphertext as SRPArray of 2 polynomials.l— Number of gadget decomposition levels.base— Decomposition basis B.
Returns: RLWE ciphertext as SRPArray of 2 polynomials.
CKKS Bootstrapping (optional high-level operation)
niobium::fhetch::ckks_bootstrap
MRPArray ckks_bootstrap(const MRPArray &ct_in, const MRPArray &aux_data)CKKS bootstrapping.
Parameters
ct_in— Input ciphertext as MRPA of length 2.aux_data— Auxiliary data (key-switching keys, plaintext polynomials).
Returns: Refreshed ciphertext as MRPA of length 2.
FILE I/O — Single objects
niobium::fhetch::save_polynomial
bool save_polynomial(const Polynomial &p, const std::filesystem::path &file)Save a single polynomial to a binary file.
Returns: true on success.
niobium::fhetch::load_polynomial
bool load_polynomial(Polynomial &p, const std::filesystem::path &file)Load a single polynomial from a binary file.
Returns: true on success (p is populated).
niobium::fhetch::save_scalar
bool save_scalar(const Scalar &s, const std::filesystem::path &file)Save a single scalar to a binary file.
niobium::fhetch::load_scalar
bool load_scalar(Scalar &s, const std::filesystem::path &file)Load a single scalar from a binary file.
niobium::fhetch::save_mrp
bool save_mrp(const MRP &m, const std::filesystem::path &file)Save a single MRP to a binary file (all residues + base metadata).
niobium::fhetch::load_mrp
bool load_mrp(MRP &m, const std::filesystem::path &file)Load a single MRP from a binary file.
niobium::fhetch::save_mrs
bool save_mrs(const MRS &m, const std::filesystem::path &file)Save a single MRS to a binary file.
niobium::fhetch::load_mrs
bool load_mrs(MRS &m, const std::filesystem::path &file)Load a single MRS from a binary file.
FILE I/O — Arrays / groups
niobium::fhetch::save_srp_array
bool save_srp_array(const SRPArray &arr, const std::filesystem::path &file)Save an SRP array (group of single-residue polynomials) to a binary file.
niobium::fhetch::load_srp_array
bool load_srp_array(SRPArray &arr, const std::filesystem::path &file)Load an SRP array from a binary file.
niobium::fhetch::save_mrp_array
bool save_mrp_array(const MRPArray &arr, const std::filesystem::path &file)Save an MRP array (group of multi-residue polynomials) to a binary file.
niobium::fhetch::load_mrp_array
bool load_mrp_array(MRPArray &arr, const std::filesystem::path &file)Load an MRP array from a binary file.
FILE I/O — Directory-based (one file per residue/tower)
niobium::fhetch::save_mrp_dir
bool save_mrp_dir(const MRP &m, const std::filesystem::path &dir)Save an MRP to a directory with one file per residue polynomial. Creates files named <dir>/residue_<modulus>.bin plus a manifest.json.
niobium::fhetch::load_mrp_dir
bool load_mrp_dir(MRP &m, const std::filesystem::path &dir)Load an MRP from a directory written by save_mrp_dir.
niobium::fhetch::save_mrp_array_dir
bool save_mrp_array_dir(const MRPArray &arr, const std::filesystem::path &dir)Save an MRP array to a directory with one subdirectory per element. Creates <dir>/mrp_<index>/ subdirectories, each containing per-residue files.
niobium::fhetch::load_mrp_array_dir
bool load_mrp_array_dir(MRPArray &arr, const std::filesystem::path &dir)Load an MRP array from a directory written by save_mrp_array_dir.
FILE I/O — JSON (human-readable, for debugging)
niobium::fhetch::save_polynomial_json
bool save_polynomial_json(const Polynomial &p, const std::filesystem::path &file)Save a polynomial in JSON format.
niobium::fhetch::load_polynomial_json
bool load_polynomial_json(Polynomial &p, const std::filesystem::path &file)Load a polynomial from JSON format.
niobium::fhetch::save_mrp_json
bool save_mrp_json(const MRP &m, const std::filesystem::path &file)Save an MRP in JSON format (all residues + base metadata).
niobium::fhetch::load_mrp_json
bool load_mrp_json(MRP &m, const std::filesystem::path &file)Load an MRP from JSON format.
niobium::fhetch::save_mrp_array_json
bool save_mrp_array_json(const MRPArray &arr, const std::filesystem::path &file)Save an MRP array in JSON format.
niobium::fhetch::load_mrp_array_json
bool load_mrp_array_json(MRPArray &arr, const std::filesystem::path &file)Load an MRP array from JSON format.
Something not looking right? Suggest a change on GitHub.