Documentation Index
Fetch the complete documentation index at: https://mintlify.com/octra-labs/pvac_hfhe_cpp/llms.txt
Use this file to discover all available pages before exploring further.
Overview
This module implements arithmetic operations in the finite field modulo the Mersenne prime p = 2^127 - 1. All operations are implemented using 128-bit arithmetic with two 64-bit words, where the high word uses only 63 bits.Type definition
u128
Unsigned 128-bit integer type.Requires compiler support for 128-bit integers. Will fail to compile if not available.
MASK63
Mask for the upper 63 bits.Construction
fp_from_u64
Creates a field element from a 64-bit unsigned integer.Input value (automatically reduced modulo p)
Field element with value x mod p
fp_from_words
Creates a field element from two 64-bit words with automatic modular reduction.Low 64 bits
High 64 bits (will be reduced to 63 bits)
Field element representing (hi * 2^64 + lo) mod p
Basic arithmetic
fp_add
Addition in the field.First operand
Second operand
Result of (a + b) mod p
fp_neg
Negation in the field.Input element
Result of (-a) mod p = (p - a) mod p
fp_sub
Subtraction in the field.Minuend
Subtrahend
Result of (a - b) mod p
fp_mul
Multiplication in the field.First factor
Second factor
Result of (a * b) mod p
Multiplication uses optimized platform-specific implementations (MSVC intrinsics, GCC inline assembly, or portable 128-bit arithmetic).
Advanced operations
fp_inv
Multiplicative inverse in the field.Non-zero field element
Result b such that (a * b) mod p = 1
fp_inv_ct
Constant-time multiplicative inverse using windowed exponentiation.Non-zero field element
Multiplicative inverse of a
This function computes a^(p-2) mod p using Fermat’s little theorem. It uses windowed exponentiation with window size 5 for efficiency while maintaining constant-time operation.
fp_pow_u64
Exponentiation with a 64-bit exponent.Base element
Exponent
Result of a^e mod p
Uses binary exponentiation (square-and-multiply) for efficiency.
Low-level operations
mul128x128
Multiplies two 128-bit integers to produce a 256-bit result.Low word of first operand
High word of first operand
Low word of second operand
High word of second operand
Output: bits [0:63] of result
Output: bits [64:127] of result
Output: bits [128:191] of result
Output: bits [192:255] of result
This function has three implementations:
- MSVC: Uses
_umul128intrinsic - GCC on x86-64: Uses inline assembly with
mulq - Portable: Uses 128-bit integer arithmetic
fp_reduce256
Reduces a 256-bit integer modulo p.Bits [0:63]
Bits [64:127]
Bits [128:191]
Bits [192:255]
Result reduced modulo p = 2^127 - 1
Uses the fact that 2^127 ≡ 1 (mod p) to perform efficient reduction via addition rather than division.
Implementation details
Field modulus
The field modulus is the Mersenne prime:Representation
Field elements are represented in the range [0, p) using two 64-bit words:lo: bits [0:63]hi: bits [64:126] (bit 127 is always 0)
Modular reduction
Reduction modulo 2^127 - 1 is optimized using the identity:Platform support
Supported compilers:- GCC 4.6+
- Clang 3.0+
- MSVC with Clang frontend
- ICC (Intel C++ Compiler)
Related
- Types - Fp type definition
- BitVec operations - Binary vector operations