Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(integer): reorganize concrete-integer
This commit reorganize concrete-integer's source tree as well as it does a few improvements of the API. The improvements are: - Creation of a Crt and Radix ciphertext types for improved type safety - Creation of Wrapper RadixClientKey and CrtClientKey - add gen_keys_crt and gen_keys_radix and make them use the internal-keycache to speedup doctests runs. BREAKING_CHANGE: Ciphertext type does not exists anymore as there are now two 3 ciphertext types.
- Loading branch information
Showing
57 changed files
with
3,030 additions
and
2,821 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,42 @@ | ||
//! This module implements the ciphertext structure containing an encryption of an integer message. | ||
//! This module implements the ciphertext structures. | ||
use concrete_shortint; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
/// Id to recognize the key used to encrypt a block. | ||
#[derive(Debug, PartialEq, Eq, Copy, Clone, Serialize, Deserialize)] | ||
pub struct KeyId(pub usize); | ||
|
||
/// A structure containing a ciphertext, meant to encrypt an (large) integer message. | ||
/// It is used to evaluate a integer circuits homomorphically. | ||
/// Structure containing a ciphertext in radix decomposition. | ||
#[derive(Serialize, Clone, Deserialize)] | ||
pub struct Ciphertext { | ||
pub(crate) ct_vec: Vec<concrete_shortint::ciphertext::Ciphertext>, | ||
pub(crate) message_modulus_vec: Vec<u64>, | ||
|
||
// KeyId used to identify the encryption key of a bloc | ||
// (mainly used in the CRT) | ||
pub(crate) key_id_vec: Vec<KeyId>, | ||
pub struct RadixCiphertext { | ||
/// The blocks are stored from LSB to MSB | ||
pub(crate) blocks: Vec<concrete_shortint::Ciphertext>, | ||
} | ||
|
||
impl Ciphertext { | ||
impl RadixCiphertext { | ||
/// Returns the slice of blocks that the ciphertext is composed of. | ||
pub fn blocks(&self) -> &[concrete_shortint::Ciphertext] { | ||
&self.ct_vec | ||
&self.blocks | ||
} | ||
} | ||
|
||
/// Structure containing a ciphertext in CRT decomposition. | ||
/// | ||
/// For this CRT decomposition, each block is encrypted using | ||
/// the same parameters. | ||
#[derive(Serialize, Clone, Deserialize)] | ||
pub struct CrtCiphertext { | ||
pub(crate) blocks: Vec<concrete_shortint::Ciphertext>, | ||
pub(crate) moduli: Vec<u64>, | ||
} | ||
|
||
/// Structure containing a ciphertext in CRT decomposition. | ||
/// | ||
/// For this CRT decomposition, not all blocks | ||
/// are encrypted using the same parameters | ||
#[derive(Serialize, Clone, Deserialize)] | ||
pub struct CrtMultiCiphertext { | ||
pub(crate) blocks: Vec<concrete_shortint::Ciphertext>, | ||
pub(crate) moduli: Vec<u64>, | ||
pub(crate) key_ids: Vec<KeyId>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use super::ClientKey; | ||
use crate::CrtCiphertext; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
|
||
/// Client key "specialized" for CRT decomposition. | ||
/// | ||
/// This key is a simple wrapper of the [ClientKey], | ||
/// that only encrypt and decrypt in CRT decomposition. | ||
/// | ||
/// # Example | ||
/// | ||
/// ```rust | ||
/// use concrete_integer::CrtClientKey; | ||
/// use concrete_shortint::parameters::PARAM_MESSAGE_2_CARRY_2; | ||
/// | ||
/// let basis = vec![2, 3, 5]; | ||
/// let cks = CrtClientKey::new(PARAM_MESSAGE_2_CARRY_2, basis); | ||
/// | ||
/// let msg = 13_u64; | ||
/// | ||
/// // Encryption: | ||
/// let ct = cks.encrypt(msg); | ||
/// | ||
/// // Decryption: | ||
/// let dec = cks.decrypt(&ct); | ||
/// assert_eq!(msg, dec); | ||
/// ``` | ||
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)] | ||
pub struct CrtClientKey { | ||
key: ClientKey, | ||
moduli: Vec<u64>, | ||
} | ||
|
||
impl AsRef<ClientKey> for CrtClientKey { | ||
fn as_ref(&self) -> &ClientKey { | ||
&self.key | ||
} | ||
} | ||
|
||
impl CrtClientKey { | ||
pub fn new(parameters: concrete_shortint::Parameters, moduli: Vec<u64>) -> Self { | ||
Self { | ||
key: ClientKey::new(parameters), | ||
moduli, | ||
} | ||
} | ||
|
||
pub fn encrypt(&self, message: u64) -> CrtCiphertext { | ||
self.key.encrypt_crt(message, self.moduli.clone()) | ||
} | ||
|
||
pub fn decrypt(&self, ciphertext: &CrtCiphertext) -> u64 { | ||
self.key.decrypt_crt(ciphertext) | ||
} | ||
|
||
/// Returns the parameters used by the client key. | ||
pub fn parameters(&self) -> concrete_shortint::Parameters { | ||
self.key.parameters() | ||
} | ||
} | ||
|
||
impl From<(ClientKey, Vec<u64>)> for CrtClientKey { | ||
fn from((key, moduli): (ClientKey, Vec<u64>)) -> Self { | ||
Self { key, moduli } | ||
} | ||
} |
Oops, something went wrong.