Skip to main content

CiphertextSpec

Struct CiphertextSpec 

Source
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 block

Implementations§

Source§

impl CiphertextSpec

Source

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_size exceeds 128 bits (the underlying storage capacity)
  • block_carry_size is zero
  • block_message_size is zero
  • int_size is not divisible by block_message_size
Source

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).

Source

pub fn int_mask(&self) -> u128

Source

pub fn block_spec(&self) -> CiphertextBlockSpec

Returns the block specification shared by all blocks in this integer.

Source

pub fn block_count(&self) -> u8

Returns the number of blocks in this integer.

Computed as ceil(int_size / block_message_size).

Source

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().

Source

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.

Source

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]
Source

pub fn overflows_int(&self, storage: u128) -> bool

Checks whether a value exceeds the integer’s capacity.

Returns true if storage >= 2^int_size.

Source

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

Source§

fn clone(&self) -> CiphertextSpec

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CiphertextSpec

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Hash for CiphertextSpec

Source§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for CiphertextSpec

Source§

fn eq(&self, other: &CiphertextSpec) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for CiphertextSpec

Source§

impl Eq for CiphertextSpec

Source§

impl StructuralPartialEq for CiphertextSpec

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Annotation for T
where T: PartialEq + Eq + Debug + Clone + 'static,