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
The text utilities provide functions for encrypting and decrypting strings using PVAC-HFHE. These functions handle the packing of byte data into field elements and manage variable-length message encoding.Functions
enc_text
Encrypts a string into a vector of ciphertexts, packing 15 bytes per ciphertext.The public key for encryption
The secret key for encryption
The text message to encrypt
A vector of ciphertexts where:
[0]contains the encrypted message length[1...]contain the encrypted message data (15 bytes per ciphertext)
Encoding strategy
- Length prefix: The first ciphertext encodes
msg.size()as a 64-bit value - Data packing: Each subsequent ciphertext encodes up to 15 bytes using
pack_15_bytes_to_fp - Depth hinting: Each chunk uses increasing depth hints (starting at 2) to optimize noise management
Example
The depth hint increases with each chunk to manage noise accumulation. This makes later chunks slightly more expensive but maintains decryptability for long messages.
dec_text
Decrypts a vector of ciphertexts back into the original string.The public key (used for decryption context)
The secret key for decryption
The encrypted text ciphertexts (must be in the format produced by
enc_text)The decrypted plaintext message. Returns an empty string if
cts is empty.Decoding process
- Decrypt the length from
cts[0] - Decrypt each chunk from
cts[1...]usingunpack_fp_to_15_bytes - Concatenate all bytes and truncate to the original length
Error handling
- If
length.hi != 0, a warning is printed tostderrand the value is clipped to 64 bits - If the buffer is shorter than the expected length, the result is truncated
The decrypted buffer always allocates
(length + 16) bytes to handle potential rounding, but only returns the exact length bytes.pack_15_bytes_to_fp
Packs up to 15 bytes into a single field element.Pointer to the byte array to pack
Number of bytes to pack (clamped to maximum of 15)
A field element containing the packed bytes in little-endian order.
Packing layout
Bytes are packed into the 120-bit field element (Fp = 2×64 bits, with 63 bits used in the high word):
Capacity
- Maximum: 15 bytes (120 bits)
- Field size: 127 bits
- Safety margin: 7 bits unused
Only the first 15 bytes are packed. If
len > 15, excess bytes are ignored. Bytes beyond len are treated as zero.unpack_fp_to_15_bytes
Unpacks a field element into 15 bytes.The field element to unpack
Output buffer (must have space for at least 15 bytes)
Unpacking layout
The inverse ofpack_15_bytes_to_fp:
Buffer requirements
- Output buffer must be at least 15 bytes
- No bounds checking is performed
- Always writes exactly 15 bytes
This function always writes 15 bytes. If the original data was shorter, the extra bytes will contain the padding (zeros) from the packing operation.
Usage patterns
Basic text encryption
Custom data encoding
For non-text binary data, you can use the packing functions directly:Length limits
- Maximum message length: 2^64 - 1 bytes (practically unlimited)
- Ciphertext expansion:
ceil(message_length / 15) + 1ciphertexts - Each ciphertext adds ~150-300 KB depending on noise levels
Performance considerations
Ciphertext size
For a message of length L:Depth management
The increasing depth hint strategy means:- First chunks encrypt faster (depth 2)
- Later chunks are slower but maintain correctness
- For very long messages (>1 KB), consider batching or compression
Related functions
enc_value- Used internally for length encodingenc_fp_depth- Used internally for chunk encryptiondec_value- Used internally for decryption