Struct concrete_core::crypto::gsw::GswCiphertext [−][src]
pub struct GswCiphertext<Cont, Scalar> { /* fields omitted */ }
Expand description
A GSW ciphertext.
Implementations
pub fn allocate(
value: Scalar,
lwe_size: LweSize,
decomp_level: DecompositionLevelCount,
decomp_base_log: DecompositionBaseLog
) -> Self where
Scalar: Numeric,
pub fn allocate(
value: Scalar,
lwe_size: LweSize,
decomp_level: DecompositionLevelCount,
decomp_base_log: DecompositionBaseLog
) -> Self where
Scalar: Numeric,
Allocates a new GSW ciphertext whose coefficients are all value
.
Example
use concrete_commons::parameters::{DecompositionBaseLog, DecompositionLevelCount, LweSize};
use concrete_core::crypto::gsw::GswCiphertext;
let gsw = GswCiphertext::allocate(
9 as u8,
LweSize(7),
DecompositionLevelCount(3),
DecompositionBaseLog(4),
);
assert_eq!(gsw.lwe_size(), LweSize(7));
assert_eq!(gsw.decomposition_level_count(), DecompositionLevelCount(3));
assert_eq!(gsw.decomposition_base_log(), DecompositionBaseLog(4));
pub fn from_container(
cont: Cont,
lwe_size: LweSize,
decomp_base_log: DecompositionBaseLog
) -> Self where
Cont: AsRefSlice<Element = Scalar>,
Scalar: Numeric,
pub fn from_container(
cont: Cont,
lwe_size: LweSize,
decomp_base_log: DecompositionBaseLog
) -> Self where
Cont: AsRefSlice<Element = Scalar>,
Scalar: Numeric,
Creates a gsw ciphertext from an existing container.
Example
use concrete_commons::parameters::{DecompositionBaseLog, DecompositionLevelCount, LweSize};
use concrete_core::crypto::gsw::GswCiphertext;
let gsw = GswCiphertext::from_container(
vec![9 as u8; 7 * 7 * 3],
LweSize(7),
DecompositionBaseLog(4),
);
assert_eq!(gsw.lwe_size(), LweSize(7));
assert_eq!(gsw.decomposition_level_count(), DecompositionLevelCount(3));
assert_eq!(gsw.decomposition_base_log(), DecompositionBaseLog(4));
Returns the size of the lwe ciphertexts composing the gsw ciphertext.
Example
use concrete_commons::parameters::{DecompositionBaseLog, DecompositionLevelCount, LweSize};
use concrete_core::crypto::gsw::GswCiphertext;
let gsw = GswCiphertext::allocate(
9 as u8,
LweSize(7),
DecompositionLevelCount(3),
DecompositionBaseLog(4),
);
assert_eq!(gsw.lwe_size(), LweSize(7));
Returns the number of decomposition levels used in the ciphertext.
Example
use concrete_commons::parameters::{DecompositionBaseLog, DecompositionLevelCount, LweSize};
use concrete_core::crypto::gsw::GswCiphertext;
let gsw = GswCiphertext::allocate(
9 as u8,
LweSize(7),
DecompositionLevelCount(3),
DecompositionBaseLog(4),
);
assert_eq!(gsw.decomposition_level_count(), DecompositionLevelCount(3));
Returns a borrowed list composed of all the LWE ciphertexts composing current ciphertext.
Example
use concrete_commons::parameters::{
CiphertextCount, DecompositionBaseLog, DecompositionLevelCount, LweSize,
};
use concrete_core::crypto::gsw::GswCiphertext;
let gsw = GswCiphertext::allocate(
9 as u8,
LweSize(7),
DecompositionLevelCount(3),
DecompositionBaseLog(4),
);
let list = gsw.as_lwe_list();
assert_eq!(list.lwe_size(), LweSize(7));
assert_eq!(list.count(), CiphertextCount(3 * 7));
pub fn as_mut_lwe_list(&mut self) -> LweList<&mut [Scalar]> where
Self: AsMutTensor<Element = Scalar>,
pub fn as_mut_lwe_list(&mut self) -> LweList<&mut [Scalar]> where
Self: AsMutTensor<Element = Scalar>,
Returns a mutably borrowed LweList
composed of all the LWE ciphertexts composing
current ciphertext.
Example
use concrete_commons::parameters::{
CiphertextCount, DecompositionBaseLog, DecompositionLevelCount, LweSize,
};
use concrete_core::crypto::gsw::GswCiphertext;
use concrete_core::math::tensor::{AsMutTensor, AsRefTensor};
let mut gsw = GswCiphertext::allocate(
9 as u8,
LweSize(7),
DecompositionLevelCount(3),
DecompositionBaseLog(4),
);
let mut list = gsw.as_mut_lwe_list();
list.as_mut_tensor().fill_with_element(0);
assert_eq!(list.lwe_size(), LweSize(7));
assert_eq!(list.count(), CiphertextCount(3 * 7));
gsw.as_tensor().iter().for_each(|a| assert_eq!(*a, 0));
Returns the logarithm of the base used for the gadget decomposition.
Example
use concrete_commons::parameters::{DecompositionBaseLog, DecompositionLevelCount, LweSize};
use concrete_core::crypto::gsw::GswCiphertext;
let gsw = GswCiphertext::allocate(
9 as u8,
LweSize(7),
DecompositionLevelCount(3),
DecompositionBaseLog(4),
);
assert_eq!(gsw.decomposition_base_log(), DecompositionBaseLog(4));
pub fn level_matrix_iter(
&self
) -> impl DoubleEndedIterator<Item = GswLevelMatrix<&[<Self as AsRefTensor>::Element]>> where
Self: AsRefTensor,
pub fn level_matrix_iter(
&self
) -> impl DoubleEndedIterator<Item = GswLevelMatrix<&[<Self as AsRefTensor>::Element]>> where
Self: AsRefTensor,
Returns an iterator over borrowed level matrices.
Note
This iterator iterates over the levels from the lower to the higher level in the usual
order. To iterate in the reverse order, you can use rev()
on the iterator.
Example
use concrete_commons::parameters::{DecompositionBaseLog, DecompositionLevelCount, LweSize};
use concrete_core::crypto::gsw::GswCiphertext;
let gsw = GswCiphertext::allocate(
9 as u8,
LweSize(7),
DecompositionLevelCount(3),
DecompositionBaseLog(4),
);
for level_matrix in gsw.level_matrix_iter() {
assert_eq!(level_matrix.row_iter().count(), 7);
for lwe in level_matrix.row_iter() {
assert_eq!(lwe.lwe_size(), LweSize(7));
}
}
assert_eq!(gsw.level_matrix_iter().count(), 3);
pub fn level_matrix_iter_mut(
&mut self
) -> impl DoubleEndedIterator<Item = GswLevelMatrix<&mut [<Self as AsRefTensor>::Element]>> where
Self: AsMutTensor,
pub fn level_matrix_iter_mut(
&mut self
) -> impl DoubleEndedIterator<Item = GswLevelMatrix<&mut [<Self as AsRefTensor>::Element]>> where
Self: AsMutTensor,
Returns an iterator over mutably borrowed level matrices.
Note
This iterator iterates over the levels from the lower to the higher level in the usual
order. To iterate in the reverse order, you can use rev()
on the iterator.
Example
use concrete_commons::parameters::{DecompositionBaseLog, DecompositionLevelCount, LweSize};
use concrete_core::crypto::gsw::GswCiphertext;
use concrete_core::math::tensor::{AsMutTensor, AsRefTensor};
let mut gsw = GswCiphertext::allocate(
9 as u8,
LweSize(7),
DecompositionLevelCount(3),
DecompositionBaseLog(4),
);
for mut level_matrix in gsw.level_matrix_iter_mut() {
for mut lwe in level_matrix.row_iter_mut() {
lwe.as_mut_tensor().fill_with_element(9);
}
}
assert!(gsw.as_tensor().iter().all(|a| *a == 9));
assert_eq!(gsw.level_matrix_iter_mut().count(), 3);
pub fn external_product<C1, C2>(
&self,
output: &mut LweCiphertext<C1>,
lwe: &LweCiphertext<C2>
) where
Self: AsRefTensor<Element = Scalar>,
LweCiphertext<C1>: AsMutTensor<Element = Scalar>,
LweCiphertext<C2>: AsRefTensor<Element = Scalar>,
Scalar: UnsignedTorus,
pub fn external_product<C1, C2>(
&self,
output: &mut LweCiphertext<C1>,
lwe: &LweCiphertext<C2>
) where
Self: AsRefTensor<Element = Scalar>,
LweCiphertext<C1>: AsMutTensor<Element = Scalar>,
LweCiphertext<C2>: AsRefTensor<Element = Scalar>,
Scalar: UnsignedTorus,
Computes the external product and adds it to the output
Example
use concrete_commons::dispersion::LogStandardDev;
use concrete_commons::parameters::LweDimension;
use concrete_commons::parameters::LweSize;
use concrete_core::crypto::encoding::Plaintext;
use concrete_core::crypto::gsw::GswCiphertext;
use concrete_core::crypto::lwe::LweCiphertext;
use concrete_core::crypto::secret::LweSecretKey;
use concrete_core::crypto::secret::generators::{
EncryptionRandomGenerator, SecretRandomGenerator,
};
use concrete_commons::parameters::DecompositionLevelCount;
use concrete_commons::parameters::DecompositionBaseLog;
let mut secret_generator = SecretRandomGenerator::new(None);
let mut encryption_generator = EncryptionRandomGenerator::new(None);
let lwe_sk = LweSecretKey::generate_binary(LweDimension(256), &mut secret_generator);
let mut gsw = GswCiphertext::allocate(
0 as u32,
LweSize(257),
DecompositionLevelCount(3),
DecompositionBaseLog(7),
);
let std_dev = LogStandardDev(-20.);
lwe_sk.encrypt_constant_gsw(
&mut gsw,
&Plaintext(1 as u32),
std_dev,
&mut encryption_generator,
);
let mut ciphertext = LweCiphertext::allocate(0 as u32, LweSize(257));
let mut res = LweCiphertext::allocate(0 as u32, LweSize(257));
lwe_sk.encrypt_lwe(
&mut ciphertext,
&Plaintext(0 as u32),
std_dev,
&mut encryption_generator,
);
gsw.external_product(&mut res, &ciphertext);
pub fn cmux<C0, C1, COut>(
&self,
output: &mut LweCiphertext<COut>,
ct0: &LweCiphertext<C0>,
ct1: &LweCiphertext<C1>
) where
LweCiphertext<C0>: AsRefTensor<Element = Scalar>,
LweCiphertext<C1>: AsRefTensor<Element = Scalar>,
LweCiphertext<Vec<Scalar>>: AsMutTensor<Element = Scalar>,
LweCiphertext<COut>: AsMutTensor<Element = Scalar>,
Self: AsRefTensor<Element = Scalar>,
Scalar: UnsignedTorus,
pub fn cmux<C0, C1, COut>(
&self,
output: &mut LweCiphertext<COut>,
ct0: &LweCiphertext<C0>,
ct1: &LweCiphertext<C1>
) where
LweCiphertext<C0>: AsRefTensor<Element = Scalar>,
LweCiphertext<C1>: AsRefTensor<Element = Scalar>,
LweCiphertext<Vec<Scalar>>: AsMutTensor<Element = Scalar>,
LweCiphertext<COut>: AsMutTensor<Element = Scalar>,
Self: AsRefTensor<Element = Scalar>,
Scalar: UnsignedTorus,
Computes the CMux between ct0 and ct1 and writes the result in ouptut
Example
use concrete_commons::dispersion::LogStandardDev;
use concrete_commons::parameters::{
DecompositionBaseLog, DecompositionLevelCount, LweDimension, LweSize,
};
use concrete_core::crypto::encoding::Plaintext;
use concrete_core::crypto::gsw::GswCiphertext;
use concrete_core::crypto::lwe::LweCiphertext;
use concrete_core::crypto::secret::generators::{
EncryptionRandomGenerator, SecretRandomGenerator,
};
use concrete_core::crypto::secret::LweSecretKey;
let mut secret_generator = SecretRandomGenerator::new(None);
let mut encryption_generator = EncryptionRandomGenerator::new(None);
let lwe_sk = LweSecretKey::generate_binary(LweDimension(256), &mut secret_generator);
let mut gsw = GswCiphertext::allocate(
0 as u32,
LweSize(257),
DecompositionLevelCount(3),
DecompositionBaseLog(7),
);
let std_dev = LogStandardDev(-20.);
lwe_sk.encrypt_constant_gsw(
&mut gsw,
&Plaintext(1 as u32),
std_dev,
&mut encryption_generator,
);
let mut ciphertext0 = LweCiphertext::allocate(0 as u32, LweSize(257));
let mut ciphertext1 = LweCiphertext::allocate(0 as u32, LweSize(257));
let mut out = LweCiphertext::allocate(0 as u32, LweSize(257));
lwe_sk.encrypt_lwe(
&mut ciphertext0,
&Plaintext(0 as u32),
std_dev,
&mut encryption_generator,
);
lwe_sk.encrypt_lwe(
&mut ciphertext1,
&Plaintext(1 as u32),
std_dev,
&mut encryption_generator,
);
gsw.cmux(&mut out, &ciphertext0, &ciphertext1);
Trait Implementations
impl<Element, Cont, Scalar> AsMutTensor for GswCiphertext<Cont, Scalar> where
Cont: AsMutSlice<Element = Element>,
Scalar: Numeric,
impl<Element, Cont, Scalar> AsMutTensor for GswCiphertext<Cont, Scalar> where
Cont: AsMutSlice<Element = Element>,
Scalar: Numeric,
impl<Element, Cont, Scalar> AsRefTensor for GswCiphertext<Cont, Scalar> where
Cont: AsRefSlice<Element = Element>,
Scalar: Numeric,
impl<Element, Cont, Scalar> AsRefTensor for GswCiphertext<Cont, Scalar> where
Cont: AsRefSlice<Element = Element>,
Scalar: Numeric,
impl<Cont, Scalar> IntoTensor for GswCiphertext<Cont, Scalar> where
Cont: AsRefSlice,
Scalar: Numeric,
impl<Cont, Scalar> IntoTensor for GswCiphertext<Cont, Scalar> where
Cont: AsRefSlice,
Scalar: Numeric,
type Element = <Cont as AsRefSlice>::Element
type Element = <Cont as AsRefSlice>::Element
The element type of the collection container.
type Container = Cont
type Container = Cont
The type of the collection container.
Consumes self
and returns an owned tensor.