Skip to main content

CiphertextBlockSpec

Struct CiphertextBlockSpec 

Source
pub struct CiphertextBlockSpec(pub u8, pub u8);
Expand description

Specification for the bit layout of a single ciphertext block.

A ciphertext block is a fixed-precision integer partitioned into three contiguous regions, from MSB to LSB: [padding_bit | carry_bits | message_bits]. The padding bit (always 1 bit) ensures correct behavior with negacyclic lookup tables. The carry bits store intermediate results during homomorphic arithmetic. The message bits hold the actual encrypted data.

Use this specification to create EmulatedCiphertextBlock values via the factory methods from_message, from_carry, from_data, and from_complete.

Two specs are compatible for arithmetic operations when their message and carry sizes match. A CiphertextBlockSpec can also be compared with a PlaintextBlockSpec for equality — they are considered equal when their message sizes match, which determines whether plaintext and ciphertext blocks can be combined in mixed operations.

§Examples

use zhc_crypto::integer_semantics::CiphertextBlockSpec;

// Create a spec with 2 carry bits and 4 message bits (7 bits total with padding)
let spec = CiphertextBlockSpec(2, 4);

// Create blocks from different regions
let msg_block = spec.from_message(0b1010);
let carry_block = spec.from_carry(0b11);
let data_block = spec.from_data(0b11_1010); // carry | message

Tuple Fields§

§0: u8

The number of carry bits in this block layout.

§1: u8

The number of message bits in this block layout.

Implementations§

Source§

impl CiphertextBlockSpec

Source

pub fn padding_size(&self) -> u8

Returns the size of the padding region in bits.

The padding bit is always exactly 1 bit. It ensures that negacyclic PBS lookups access only the first half of the lookup table when the padding bit is zero.

Source

pub fn padding_mask(&self) -> u16

Source

pub fn carry_size(&self) -> u8

Returns the size of the carry region in bits.

The carry region stores intermediate results during homomorphic arithmetic operations, allowing multiple additions before a carry propagation is required.

Source

pub fn carry_mask(&self) -> u16

Source

pub fn message_size(&self) -> u8

Returns the size of the message region in bits.

The message region holds the actual encrypted payload. For a radix integer decomposition, this determines the radix base: a block with n message bits represents values in [0, 2^n).

Source

pub fn message_mask(&self) -> u16

Source

pub fn complete_size(&self) -> u8

Returns the total size of the block in bits.

This is the sum of padding, carry, and message sizes: 1 + carry_size + message_size.

Source

pub fn complete_mask(&self) -> u16

Source

pub fn data_size(&self) -> u8

Returns the size of the data region in bits.

The data region comprises both carry and message bits, excluding the padding bit: carry_size + message_size.

Source

pub fn data_mask(&self) -> u16

Source

pub fn from_message(&self, message: u16) -> EmulatedCiphertextBlock

Creates a ciphertext block with the given value in the message region.

The carry and padding regions are set to zero. The provided message value must fit within the message region; use overflows_message to check beforehand if needed.

§Panics

Panics if message exceeds the maximum value representable in the message region.

§Examples
use zhc_crypto::integer_semantics::CiphertextBlockSpec;

let spec = CiphertextBlockSpec(2, 4);
let block = spec.from_message(0b1010); // message = 10, carry = 0, padding = 0
Source

pub fn from_carry(&self, carry: u16) -> EmulatedCiphertextBlock

Creates a ciphertext block with the given value in the carry region.

The carry value is shifted into the carry position; message and padding regions are set to zero. The value must fit within the carry region.

§Panics

Panics if carry exceeds the maximum value representable in the carry region.

§Examples
use zhc_crypto::integer_semantics::CiphertextBlockSpec;

let spec = CiphertextBlockSpec(2, 4);
let block = spec.from_carry(0b11); // message = 0, carry = 3, padding = 0
Source

pub fn from_data(&self, data: u16) -> EmulatedCiphertextBlock

Creates a ciphertext block with the given value spanning both carry and message regions.

The data value occupies the lower data_size() bits (carry | message), with the padding bit set to zero. The value must fit within the data region.

§Panics

Panics if data exceeds the maximum value representable in the data region.

§Examples
use zhc_crypto::integer_semantics::CiphertextBlockSpec;

let spec = CiphertextBlockSpec(2, 4);
let block = spec.from_data(0b11_1010); // message = 10, carry = 3, padding = 0
Source

pub fn from_complete(&self, data: u16) -> EmulatedCiphertextBlock

Creates a ciphertext block with the given value spanning all regions including padding.

The data value occupies all complete_size() bits (padding | carry | message). This is the only factory method that can set the padding bit. The value must fit within the complete block size.

§Panics

Panics if data exceeds the maximum value representable in the complete block.

§Examples
use zhc_crypto::integer_semantics::CiphertextBlockSpec;

let spec = CiphertextBlockSpec(2, 4);
let block = spec.from_complete(0b1_11_1010); // message = 10, carry = 3, padding = 1
Source

pub fn overflows_message(&self, storage: u16) -> bool

Checks whether a value exceeds the message region capacity.

Returns true if storage >= 2^message_size.

Source

pub fn overflows_carry(&self, storage: u16) -> bool

Checks whether a value exceeds the data region capacity.

Returns true if storage >= 2^(message_size + carry_size). This checks overflow for values intended to span both message and carry regions.

Source

pub fn overflows_padding(&self, storage: u16) -> bool

Checks whether a value exceeds the complete block capacity.

Returns true if storage >= 2^(message_size + carry_size + 1). This checks overflow for values intended to span all regions including the padding bit.

Source

pub fn matching_plaintext_block_spec(&self) -> PlaintextBlockSpec

Returns the corresponding plaintext block specification.

The returned PlaintextBlockSpec has the same message size as this ciphertext spec, allowing plaintext blocks to be used in mixed ciphertext-plaintext operations.

Source

pub fn ciphertext_spec(&self, int_size: u16) -> CiphertextSpec

Creates a multi-block ciphertext specification using this block layout.

The int_size parameter specifies the total number of message bits across all blocks in the resulting integer. It must be a multiple of this spec’s message size.

§Panics

Panics if int_size is not divisible by the message size.

Trait Implementations§

Source§

impl Clone for CiphertextBlockSpec

Source§

fn clone(&self) -> CiphertextBlockSpec

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 CiphertextBlockSpec

Source§

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

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

impl Hash for CiphertextBlockSpec

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<PlaintextBlockSpec> for CiphertextBlockSpec

Source§

fn eq(&self, other: &PlaintextBlockSpec) -> 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 PartialEq for CiphertextBlockSpec

Source§

fn eq(&self, other: &CiphertextBlockSpec) -> 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 CiphertextBlockSpec

Source§

impl Eq for CiphertextBlockSpec

Source§

impl StructuralPartialEq for CiphertextBlockSpec

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,