concrete_integer/ciphertext/
mod.rs

1//! This module implements the ciphertext structures.
2use concrete_shortint;
3use serde::{Deserialize, Serialize};
4
5/// Id to recognize the key used to encrypt a block.
6#[derive(Debug, PartialEq, Eq, Copy, Clone, Serialize, Deserialize)]
7pub struct KeyId(pub usize);
8
9/// Structure containing a ciphertext in radix decomposition.
10#[derive(Serialize, Clone, Deserialize)]
11pub struct RadixCiphertext {
12    /// The blocks are stored from LSB to MSB
13    pub(crate) blocks: Vec<concrete_shortint::Ciphertext>,
14}
15
16pub trait IntegerCiphertext: Clone {
17    fn from_blocks(blocks: Vec<concrete_shortint::Ciphertext>) -> Self;
18    fn blocks(&self) -> &[concrete_shortint::Ciphertext];
19    fn blocks_mut(&mut self) -> &mut [concrete_shortint::Ciphertext];
20    fn moduli(&self) -> Vec<u64> {
21        self.blocks()
22            .iter()
23            .map(|x| x.message_modulus.0 as u64)
24            .collect()
25    }
26}
27
28impl IntegerCiphertext for RadixCiphertext {
29    fn blocks(&self) -> &[concrete_shortint::Ciphertext] {
30        &self.blocks
31    }
32    fn blocks_mut(&mut self) -> &mut [concrete_shortint::Ciphertext] {
33        &mut self.blocks
34    }
35    fn from_blocks(blocks: Vec<concrete_shortint::Ciphertext>) -> Self {
36        Self { blocks }
37    }
38}
39
40impl IntegerCiphertext for CrtCiphertext {
41    fn blocks(&self) -> &[concrete_shortint::Ciphertext] {
42        &self.blocks
43    }
44    fn blocks_mut(&mut self) -> &mut [concrete_shortint::Ciphertext] {
45        &mut self.blocks
46    }
47    fn from_blocks(blocks: Vec<concrete_shortint::Ciphertext>) -> Self {
48        let moduli = blocks.iter().map(|x| x.message_modulus.0 as u64).collect();
49        Self { blocks, moduli }
50    }
51}
52
53/// Structure containing a ciphertext in CRT decomposition.
54///
55/// For this CRT decomposition, each block is encrypted using
56/// the same parameters.
57#[derive(Serialize, Clone, Deserialize)]
58pub struct CrtCiphertext {
59    pub(crate) blocks: Vec<concrete_shortint::Ciphertext>,
60    pub(crate) moduli: Vec<u64>,
61}
62
63/// Structure containing a ciphertext in CRT decomposition.
64///
65/// For this CRT decomposition, not all blocks
66/// are encrypted using the same parameters
67#[derive(Serialize, Clone, Deserialize)]
68pub struct CrtMultiCiphertext {
69    pub(crate) blocks: Vec<concrete_shortint::Ciphertext>,
70    pub(crate) moduli: Vec<u64>,
71    pub(crate) key_ids: Vec<KeyId>,
72}