pub struct CiphertextSpec { /* private fields */ }Expand description
Specification for a multi-block radix ciphertext representing a large integer.
TFHE operates on large integers by decomposing them into multiple LWE ciphertext blocks using
a fixed radix. Each block holds a portion of the integer’s bits according to a shared
CiphertextBlockSpec. This specification defines both the total integer size and the
per-block layout.
The total int_size must be a multiple of the block’s message size — this ensures the integer
can be evenly partitioned across blocks. For example, a 64-bit integer with 4-bit message
blocks requires exactly 16 blocks.
Use from_int to create an EmulatedCiphertext from a raw integer value,
or random to generate a random ciphertext for testing.
§Examples
use zhc_crypto::integer_semantics::CiphertextSpec;
// Create a spec for 16-bit integers using blocks with 2 carry bits and 4 message bits
let spec = CiphertextSpec::new(16, 2, 4);
assert_eq!(spec.block_count(), 4); // 16 / 4 = 4 blocks
// Create a ciphertext from an integer value
let ct = spec.from_int(0x1234);
// Access individual blocks
let block_0 = ct.get_block(0); // least significant blockImplementations§
Source§impl CiphertextSpec
impl CiphertextSpec
Sourcepub fn new(
int_size: u16,
block_carry_size: u8,
block_message_size: u8,
) -> CiphertextSpec
pub fn new( int_size: u16, block_carry_size: u8, block_message_size: u8, ) -> CiphertextSpec
Creates a new ciphertext specification with the given parameters.
The int_size defines the total number of message bits in the integer. The
block_carry_size and block_message_size define the per-block layout. The integer
size must be divisible by the block message size so blocks partition the integer evenly.
§Panics
Panics if:
int_sizeexceeds 128 bits (the underlying storage capacity)block_carry_sizeis zeroblock_message_sizeis zeroint_sizeis not divisible byblock_message_size
Sourcepub fn int_size(&self) -> u16
pub fn int_size(&self) -> u16
Returns the total size of the integer in bits.
This is the sum of all message bits across all blocks, representing the maximum value
range [0, 2^int_size).
pub fn int_mask(&self) -> u128
Sourcepub fn block_spec(&self) -> CiphertextBlockSpec
pub fn block_spec(&self) -> CiphertextBlockSpec
Returns the block specification shared by all blocks in this integer.
Sourcepub fn block_count(&self) -> u8
pub fn block_count(&self) -> u8
Returns the number of blocks in this integer.
Computed as ceil(int_size / block_message_size).
Sourcepub fn block_mask(&self, ith: u8) -> u128
pub fn block_mask(&self, ith: u8) -> u128
Returns a bitmask selecting the message bits of the ith block within the integer.
Block 0 is the least significant block. The returned mask can be used to extract or clear a specific block’s contribution to the integer value.
§Panics
Panics if ith >= block_count().
Sourcepub fn random(&self) -> EmulatedCiphertext
pub fn random(&self) -> EmulatedCiphertext
Generates a random ciphertext with uniformly distributed message bits.
Uses a thread-local PRNG seeded deterministically. Useful for testing and fuzzing.
Sourcepub fn from_int(&self, int: u128) -> EmulatedCiphertext
pub fn from_int(&self, int: u128) -> EmulatedCiphertext
Creates a ciphertext from a raw integer value.
The integer is stored directly; individual blocks can then be accessed via
EmulatedCiphertext::get_block. All blocks will have zero carry and padding bits.
§Panics
Panics if int >= 2^int_size.
§Examples
use zhc_crypto::integer_semantics::CiphertextSpec;
let spec = CiphertextSpec::new(8, 2, 2);
let ct = spec.from_int(0b1011_0110);
assert_eq!(ct.get_block(0).spec().from_message(0b10), ct.get_block(0)); // bits [1:0]
assert_eq!(ct.get_block(1).spec().from_message(0b01), ct.get_block(1)); // bits [3:2]Sourcepub fn overflows_int(&self, storage: u128) -> bool
pub fn overflows_int(&self, storage: u128) -> bool
Checks whether a value exceeds the integer’s capacity.
Returns true if storage >= 2^int_size.
Sourcepub fn matching_plaintext_spec(&self) -> PlaintextSpec
pub fn matching_plaintext_spec(&self) -> PlaintextSpec
Returns the corresponding plaintext specification.
The returned PlaintextSpec has the same integer size and block message size,
allowing plaintext integers to be used in mixed ciphertext-plaintext operations.
Trait Implementations§
Source§impl Clone for CiphertextSpec
impl Clone for CiphertextSpec
Source§fn clone(&self) -> CiphertextSpec
fn clone(&self) -> CiphertextSpec
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more