Niobium .niob Language Reference

A practical guide to writing FHE applications in the Niobium DSL. For the formal grammar, see GRAMMAR.md.


Program Structure

An .niob program is split across three files by convention:

FileContainsDomain
shared.niobConstants, enums, structs, wire types, helper functionsBoth
client.niobKey generation, encryption, decryption stagesClient
server.niobEncrypted computation stagesServer

Files reference each other with use shared::* at the top.

use shared::*

External Module Declarations

Declare external C++ modules that should be included and linked:

extern weights from "vector_constants"

This generates #include "vector_constants.h" in the shared header and adds the module's source files to the CMakeLists.txt build. Vendor the C++ sources in the example directory and pass -DLOCAL_SRC_DIR=... so the example builds from this repo alone (no external repo required).


Declarations

Constants

const THRESHOLD: f64 = 0.8
const PAYLOAD_DIM: u32 = 8
const MAX_VAL: u32 = 256

Constants are evaluated at compile time and emitted as constexpr in C++.

Enums

enum InstanceSize { Toy, Small, Medium, Large }
enum Operation { ADD, MUL, ROTATE_ADD, ADDI, MULI }

Variants are auto-numbered starting from 0. Enums compile to C++ enum types and can be used in match expressions and as CLI arguments (passed as integers).

Structs

struct Instance {
    size: InstanceSize,
    record_dim: u32,
    ring_dim: u32,
    degrees: vec<u32>,
}

Structs are plain data containers. Fields use the same types as function parameters. Struct literals use Name { field: value } syntax with shorthand for same-name bindings:

Instance { size, record_dim: 128, ring_dim: 2048, degrees: [8, 4, 4] }
// 'size' is shorthand for 'size: size'

Wire Types

Wire types define what crosses the client-server boundary. The compiler generates all serialization code automatically.

wire CryptoParams {
    context: CryptoContext,
    public_key: PublicKey,
    eval_mult_key: EvalMultKey,
    eval_rot_keys: EvalAutomorphismKeys,
}

wire EncryptedInput {
    ciphertext: enc<f64>,
}

wire EncryptedResult {
    ciphertext: enc<f64>,
}

Wire types may NOT contain SecretKey (enforced by the semantic analyzer). Wire types with a single enc<T> field get optimized serialization.

Scheme Configuration

Declares the FHE scheme parameters. Placed in client.niob:

scheme CKKS {
    security: 128-classic    // or: not_set
    ring_dim: 2048           // optional: literal, inst.ring_dim, or omit entirely
    key_dist: uniform_ternary
    key_switch: hybrid
    scaling: flexible_auto
    precision: 42 bits
    first_mod: 57 bits
    depth: 23
}

The scheme can be overridden at runtime (e.g., for toy instances):

if inst.size == Toy {
    scheme.override(security: not_set, ring_dim: 2048)
}

Requires Declaration

Declares which FHE operations the application uses. The compiler generates the appropriate key generation calls:

requires { add, mul, rotate }
// Also available: chebyshev, running_sums, replicate

Type System

Primitive Types

TypeC++ equivalentDescription
boolboolBoolean
u8, u16, u32, u64uint8_t, ..., uint64_tUnsigned integers
i8, i16, i32, i64int8_t, ..., int64_tSigned integers
f32, f64float, doubleFloating point
stringstd::stringString
pathstd::filesystem::pathFile path

Composite Types

TypeC++ equivalentDescription
vec<T>std::vector<T>Dynamic vector
mat<T>std::vector<std::vector<T>>2D matrix
enc<T>Ciphertext<DCRTPoly>Encrypted value
enc<vec<T>>Ciphertext<DCRTPoly>Encrypted vector (packed slots)
(T1, T2)std::pair<T1, T2>Tuple (2 elements)
(T1, T2, T3)std::tuple<T1, T2, T3>Tuple (3+ elements)

Encryption Type Rules

The enc<T> type participates in arithmetic:

enc<T> + enc<T>  ->  enc<T>     (EvalAdd, both ciphertext)
enc<T> + T       ->  enc<T>     (EvalAdd, cipher-plain)
enc<T> * enc<T>  ->  enc<T>     (EvalMult, increases depth)
enc<T> * T       ->  enc<T>     (EvalMult, cipher-plain, no depth increase)
enc<T> - enc<T>  ->  enc<T>     (EvalSub)

The compiler tracks multiplicative depth through expressions.


Functions

Basic Functions

fn helper(x: f64, y: f64) -> f64 {
    return x + y
}

Functions in shared.niob (no domain annotation) are available to both client and server.

Default Parameters

fn compute(inst: Instance, op: Operation = 0, immediate: f64 = 0.0) { ... }

Parameters with defaults become optional CLI arguments when the function is a stage.

Functions Taking Encrypted Values

Functions that operate on encrypted data need CryptoContext. The compiler auto-detects functions with enc<T> parameters or return type and injects cc as the first parameter:

// In .niob:
fn dispatch(op: Operation, ct1: enc<f64>, ct2: enc<f64>, imm: f64) -> enc<f64> { ... }

// Generated C++:
Ciphertext<DCRTPoly> dispatch(CryptoContext<DCRTPoly> cc, Operation op,
                               Ciphertext<DCRTPoly> ct1, ...) { ... }

Annotations

Domain Annotations

@client   // Function runs in client domain (has secret key)
@server   // Function runs in server domain (no secret key)

Enforcement: Server functions cannot call decrypt(), load_secret_key(), or reference SecretKey. Violations are compile errors.

Stage Annotation

Each @stage("name") function compiles to a separate binary:

@client @stage("key_generation")
fn generate_keys(inst: Instance, mult_depth: u32 = 3) -> writes(CryptoParams) { ... }

The generated binary accepts CLI arguments:

Hardware Annotation

Generates Niobium record/replay instrumentation for server stages:

@server @stage("compute")
@hardware(cache_key: ["workload_size", "operation"])
fn compute(inst: Instance, op: Operation = 0) -> reads(CryptoParams), writes(EncryptedResult) { ... }

This generates Niobium client record/replay instrumentation (libnbfhetch): init() + enable_auto_tagging(), then a gate on is_cache_valid() — a record run executes the computation (start()/probe()/stop()) and serializes OpenFHE's own result; a cache-valid run executes zero FHE ops and reconstructs the output from the cached trace (replay()/result()). Inputs, eval keys, and the crypto context are tagged automatically by the instrumented-OpenFHE deserialize hooks (cooperative auto-tagging) — no explicit tag_input/tag_keys calls are emitted.

@encryptors(independent)

Declares the stage's encryption model (skill Stage 1, question 2). Default is single-encryptor: one party owns all the data and may pack records across SIMD slots (column-major). Annotating a stage @encryptors(independent) declares that each data owner encrypts independently — and the compiler then rejects cross-owner packing: encrypt() of a column slice spanning records (m[a..b, col]) is a compile error, because slots from different owners in one ciphertext would let one owner's key decrypt another's data. Per-record vectors (m[i], explicit vectors) remain fine; SIMD slots may still pack one owner's features.

@client @stage("encrypt_ballot")
@encryptors(independent)
fn encrypt_ballot(inst: Instance, voter: u32) -> reads(CryptoParams), writes(EncBallot) {
    let params = load(CryptoParams, from: keydir(inst))
    let ballots = load_matrix<f64>(ballot_file(inst), inst.n_candidates)
    return EncBallot { ct: encrypt(params.public_key, ballots[voter]) }  // OK: one owner's row
    // encrypt(params.public_key, ballots[0..inst.n_slots, 0])  <- compile error
}

Return Specifications

Stage functions use reads/writes instead of simple return types:

fn compute(inst: Instance)
    -> reads(CryptoParams, EncryptedInput),
       writes(EncryptedResult)
{ ... }

Statements

Let Bindings

let x = 42
let name: string = "hello"
let (degree, outscale) = if count_only { (247, 1.0) } else { (59, 0.504) }

Let bindings support:

Assignment

result = (result + ct2) * ct2
matrix[i][j] = value
acc[j] = acc[j] + term

Return

return EncryptedResult { ciphertext: result }
return value
return   // void return

Assert

assert rows(db) == inst.db_size
assert max_val > threshold, "marker not found"

Control Flow

If Statement / Expression

// Statement (no else required)
if inst.size == Toy {
    scheme.override(security: not_set, ring_dim: 2048)
}

// Expression (else required, produces a value)
let (degree, outscale) = if count_only { (247, 1.0) } else { (59, 0.504) }

For Loop / Expression

// Loop statement
for i in 0..n {
    result = result + rotate(result, 1 << i)
}

// Destructured loop
for (i, row) in enumerate(batch) {
    encrypt(params.public_key, row, level: enc_level)
}

// For-expression (produces a vector of results)
let encrypted = for row in batch {
    encrypt(pk, row, level: 5)
}

// Inclusive range
for i in 1..=max_n_match(inst) { ... }

// Reversed range
for i in (0..s).rev() { ... }

Match Statement / Expression

// Expression (each arm produces a value)
fn instance(size: InstanceSize) -> Instance {
    match size {
        Toy    => Instance { size, ring_dim: 2048 },
        Small  => Instance { size, ring_dim: 65536 },
        Medium => Instance { size, ring_dim: 65536 },
        Large  => Instance { size, ring_dim: 65536 },
    }
}

// Multi-statement arms use blocks
match op {
    LARGE_ADD_MUL => {
        let result = ct1
        for i in 0..n { result = (result + ct2) * ct2 }
        return result
    },
    ADD => ct1 + ct2,
}

Expressions

Arithmetic Operators

OperatorOn encryptedCompiles to
a + benc + encNullSafeEvalAdd(cc, a, b)
a + benc + plaincc->EvalAdd(a, b)
a - benc - enc/plaincc->EvalSub(a, b)
a * benc * enccc->EvalMult(a, b)
a * benc * plaincc->EvalMult(a, b)
a *_norelin benc * enccc->EvalMultNoRelin(a, b)
a ^ bplain onlystd::pow(a, b)
a / bplain onlya / b

Comparison and Logical

a == b    a != b    a < b    a > b    a <= b    a >= b
a && b    a || b    !a
a ~= b    // approximate equality (CKKS)

Pipe Operator

Left-to-right function composition:

data |> transpose |> batch(n_slots(inst)) |> scale(0.5)
// Desugars to: scale(batch(transpose(data), n_slots(inst)), 0.5)

reduce(+, result) |> slot_sum(n_slots(inst))
// Desugars to: slot_sum(reduce(+, result), n_slots(inst))

Type Cast

round(value) |> as i16
count |> as i16
int(imm)     // cast to integer (in function-call style)

Closures

|x| x + 1                              // single expression
|x| sigmoid(x - THRESHOLD, outscale)   // expression with captures
|r, m| r * m                           // two parameters
|x| { let y = x + 1; return y * 2 }   // block body

Array Literals

[8, 4, 4]
[value_a]
[]                // empty
[[count]]         // nested

Struct Literals

Instance { size, record_dim: 128, ring_dim: 2048, degrees: [8, 4, 4] }
EncryptedResult { ciphertext: result }
CryptoParams { context: keys.context, public_key: keys.public }

Field Access and Indexing

inst.size
inst.ring_dim
keys.public
result[0]
matrix[i][j]
matrix[i..i+PAYLOAD_DIM, col]   // 2D slice
db.rows[j][i]

Method Calls

replicator.replicate(eqry)
results.push(rec)
scheme.override(security: not_set, ring_dim: 2048)

Built-in Functions

FHE Operations

FunctionDescriptionC++ output
encrypt(pk, data, level: n)Encrypt data with public keyMakeCKKSPackedPlaintext + Encrypt
decrypt(sk, ct)Decrypt ciphertextDecrypt + GetRealPackedValue
keygen()Generate keys from scheme configFull key generation IIFE
rotate(ct, n)Rotate ciphertext slotscc->EvalRotate(ct, n)
negate(ct)Negate ciphertextcc->EvalNegate(ct)
relin(ct)Relinearize after *_norelincc->Relinearize(ct)
chebyshev(fn, ct, domain:, degree:)Chebyshev function evaluationcc->EvalChebyshevFunction(...)
chebyshev(fn, ct, domain:, max_error:)Same, with the degree auto-selected at compile time: the closure is evaluated numerically, Chebyshev interpolants are fitted on the domain, and the minimal ladder degree (5, 13, 27, 59, 119, 247, 495, 1007, 2031) meeting the error target is chosen. nbc reports the selection as a note (degree, est. max approx error, levels consumed) and charges the implied depth. Requires a compile-time-evaluable closure (arithmetic + exp/tanh/abs + consts + pure user fns) and a literal/const domain; an unreachable tolerance is a compile errorcc->EvalChebyshevFunction(..., <selected degree>)
slot_sum(ct, n)Sum all slotscc->EvalSum(ct, n)
running_sums(cts, stride:, depth:)In-place running sumsRunningSums(cc, ...).eval_in_place(cts)
reduce(+, vec)Reduce vector with addEvalAddInPlace accumulation
clone(vec)Deep-copy ciphertext vector.Clone() each element
zero()Null ciphertext (for accumulation)Ciphertext<DCRTPoly>()
mul_monomial(ct, n)Low-level monomial multiplycc->GetScheme()->MultByMonomial(ct, n)

Data I/O

FunctionDescription
load(WireType, from: path)Deserialize a wire type from disk
load_all(WireType, from: path)Load all indexed intermediate results
save(WireType{...}, to: path)Serialize a wire type to disk
save_secret_key(sk, path)Save secret key (client only)
load_secret_key(path)Load secret key (client only)
load_matrix<T>(path, dim)Read matrix from disk (text for .txt/.csv, binary otherwise)
load_vec<T>(path, dim)Read binary vector from disk
load_model(path)Load binary model file (auto-generates loader from struct)
extern_call("func", args...)Call external C++ function with cc auto-prepended
str(value)Convert value to string (std::to_string)
print(value)Print value to stdout

Collection Operations

FunctionDescription
map(collection, fn)Apply function to each element
zip_map(a, b, fn)Apply binary function element-wise
len(v)Vector length
rows(m)Matrix row count
sort(v)Sort a vector
argmax(v)Index and value of maximum element (returns pair)
enumerate(v)Iterate with (index, element) pairs
stride(start, end, step)Generate integer sequence with step
vec_zeros<T>(n)Zero-initialized vector
mat_zeros<T>(r, c)Zero-initialized matrix

Data Transformation

FunctionDescription
transpose(matrix)Transpose 2D matrix
batch(matrix, size)Split into batches of size rows
tile(vec, n)Repeat vector to fill n elements
prepend_column(matrix, val)Add column with constant value
scale(batches, factor)Scale all values by factor
slot_mask(n_slots, n_cols, row_range: a..b)Create a binary slot mask

Path and Instance Helpers

FunctionDescription
root()Project root directory
instance(size)Create Instance from InstanceSize
instance_name(size)String name for an instance size
iodir(inst)I/O directory for instance
keydir(inst)Key directory for instance
encdir(inst)Encrypted data directory for instance
datadir(inst)Dataset directory for instance

Math

FunctionDescription
abs(x)Absolute value
round(x)Round to nearest integer
ceil_div(a, b)Ceiling division
log2(x)Log base 2 (integer)
exp(x)Exponential

Codegen Patterns

How + compiles for encrypted values

How wire types serialize

Each wire type generates custom serialization:

How stages become binaries

A @stage("name") function generates a .cpp file with:

  1. The function implementation
  2. A main() with CLI argument parsing
  3. Key loading (for server stages)
  4. Wire type serialization at entry/exit
  5. Niobium client record/replay instrumentation (if @hardware) — always compiled in; cooperative auto-tagging via libniobium_client_autofacade

Common Patterns

Encrypt two values and compute

// client.niob
@client @stage("encrypt")
fn encrypt_inputs(inst: Instance, a: f64 = 3.0, b: f64 = 5.0)
    -> reads(CryptoParams), writes(EncryptedInput)
{
    let params = load(CryptoParams, from: keydir(inst))
    let ct_a = encrypt(params.public_key, [a], slots: 1)
    let ct_b = encrypt(params.public_key, [b], slots: 1)
    save(EncryptedInput { ciphertext: ct_a }, to: iodir(inst) / "a.bin")
    save(EncryptedInput { ciphertext: ct_b }, to: iodir(inst) / "b.bin")
}

// server.niob
@server @stage("compute")
@hardware(cache_key: ["workload_size"])
fn compute(inst: Instance)
    -> reads(CryptoParams, EncryptedInput), writes(EncryptedResult)
{
    let params = load(CryptoParams, from: keydir(inst))
    let a = load(EncryptedInput, from: iodir(inst) / "a.bin").ciphertext
    let b = load(EncryptedInput, from: iodir(inst) / "b.bin").ciphertext
    let result = a + b
    return EncryptedResult { ciphertext: result }
}

Dispatch via match expression

fn dispatch(op: Operation, ct1: enc<f64>, ct2: enc<f64>, imm: f64) -> enc<f64> {
    match op {
        ADD  => ct1 + ct2,
        MUL  => ct1 * ct2,
        ADDI => ct1 + imm,
        MULI => ct1 * imm,
    }
}

Batch encrypt with for-expression

return EncryptedDB {
    rows: for (i, batch) in enumerate(db_batched) {
        for (j, row) in enumerate(batch) {
            encrypt(params.public_key, row, level: enc_level)
        }
    },
}

Pipeline with pipes

let processed = payloads
    |> prepend_column(2 * MAX_VAL * PRECISION)
    |> transpose
    |> batch(n_slots(inst))
    |> scale(1.0 / PRECISION)

Chebyshev approximation with closure

result = for ct in result {
    chebyshev(|x| sigmoid(x - THRESHOLD, outscale), ct,
              domain: [-1.0, 1.0], degree: degree)
}

Tuple return types

Functions can return tuples, which compile to std::pair (2 elements) or std::tuple (3+ elements):

fn autoencoder_forward(inst: Instance, ae: Autoencoder,
                       inputs: vec<enc<vec<f64>>>, sig_coeffs: vec<f64>)
    -> (vec<enc<vec<f64>>>, vec<enc<vec<f64>>>)
{
    let hidden: vec<enc<vec<f64>>> = for j in 0..n_hid { ... }
    let recon: vec<enc<vec<f64>>> = for i in 0..n_vis { ... }
    return (hidden, recon)
}

// Destructure at call site:
let (hidden, recon) = autoencoder_forward(inst, ae, inputs, coeffs)

Chebyshev with named constants

chebyshev(base_sigmoid, acc, domain: [SIGMOID_LO, SIGMOID_HI],
          degree: CHEB_ORDER)

Loading a binary model file

let model = load_model(model_file(inst))

When the program defines a KitNETModel struct, the codegen auto-generates a load_kitnet_model() function that reads the binary model format (header, Chebyshev coefficients, feature maps, autoencoder weights, anomaly detector weights).

Calling external C++ functions

Use extern_call to invoke machine-generated or hand-written C++ functions. The crypto context cc is automatically prepended as the first argument.

// In shared.niob: declare the external module
extern weights from "vector_constants"

// In server.niob: wrapper function that delegates to external C++
fn mlp(ct: enc<vec<f64>>) -> enc<vec<f64>> {
    extern_call("mlp", ct)
}

// Usage in a stage:
let result = mlp(input.ciphertext)

The codegen detects wrapper functions (body = single extern_call) and generates direct calls to the external function with cc prepended. The CMakeLists.txt auto-discovers source files matching the function name pattern (e.g., mlp_openfhe.cpp, mlp_function_split_0.cpp, mlp_common.cpp, mlp_bridge.cpp) in LOCAL_SRC_DIR/ — vendor those sources in the example directory so it builds from this repo alone. (An optional SUBMISSION_DIR/src/ is also searched, and takes precedence, for swapping in a real/large model from a separate package; see ml-inference.)

Required files for extern_call("X", ...) (by convention — missing headers cause build errors):

See dsl_fhe/examples/ml-inference-fhe/mlp_bridge.{h,cpp} for a reference implementation.

Indexed wire types for batch processing

// Batch save (multiple files):
fn encrypt_input(inst: Instance) -> writes(EncryptedInput[*]) {
    for i in 0..inst.batch_count {
        save(EncryptedInput { ciphertext: ct },
             to: dir / ("cipher_input_" + str(i) + ".bin"))
    }
}

// Batch load (per-index):
fn compute(inst: Instance, batch_id: u32)
    -> reads(EncryptedInput[batch_id]) { ... }

// Batch load (all at once):
fn decrypt(inst: Instance) -> reads(EncryptedResult[*]) {
    for i in 0..inst.batch_count {
        let encrypted = load(EncryptedResult,
                             from: dir / ("result_" + str(i) + ".bin"))
    }
}

Vector-of-ciphertext wire types

Wire types with vec<enc<T>> fields serialize per-element:

wire EncryptedFeatures {
    features: vec<enc<vec<f64>>>,   // features_0.bin, features_1.bin, ...
}

Something not looking right? Suggest a change on GitHub.