pub struct Ciphertext<G: Group> { /* private fields */ }Expand description
Ciphertext for ElGamal encryption.
A ciphertext consists of 2 group elements: the random element R and a blinded encrypted
value B. If the ciphertext encrypts integer value v, it holds that
R = [r]G;
B = [v]G + [r]K = [v]G + [k]R;where:
Gis the conventional group generatorris a random scalar selected by the encrypting partyKandkare the recipient’s public and private keys, respectively.
Ciphertexts are partially homomorphic: they can be added together or multiplied by a scalar value.
§Examples
Basic usage and arithmetic for ciphertexts:
// Generate a keypair for the ciphertext receiver.
let mut rng = rand::rng();
let receiver = Keypair::<Ristretto>::generate(&mut rng);
// Create a couple of ciphertexts.
let mut enc = receiver.public().encrypt(2_u64, &mut rng);
enc += receiver.public().encrypt(3_u64, &mut rng) * 4;
// Check that the ciphertext decrypts to 2 + 3 * 4 = 14.
let lookup_table = DiscreteLogTable::new(0..20);
let decrypted = receiver.secret().decrypt(enc, &lookup_table);
assert_eq!(decrypted, Some(14));Creating a ciphertext of a boolean value together with a proof:
// Generate a keypair for the ciphertext receiver.
let mut rng = rand::rng();
let receiver = Keypair::<Ristretto>::generate(&mut rng);
// Create and verify a boolean encryption.
let (enc, proof) =
receiver.public().encrypt_bool(false, &mut rng);
receiver.public().verify_bool(enc, &proof)?;Creating a ciphertext of an integer value together with a range proof:
// Generate the ciphertext receiver.
let mut rng = rand::rng();
let receiver = Keypair::<Ristretto>::generate(&mut rng);
// Find the optimal range decomposition for our range
// and specialize it for the Ristretto group.
let range = RangeDecomposition::optimal(100).into();
let (ciphertext, proof) = receiver
.public()
.encrypt_range(&range, 42, &mut rng);
// Check that the the proof verifies.
receiver.public().verify_range(&range, ciphertext, &proof)?;Implementations§
Source§impl<G: Group> Ciphertext<G>
impl<G: Group> Ciphertext<G>
Sourcepub fn from_elements(
random_element: G::Element,
blinded_element: G::Element,
) -> Self
pub fn from_elements( random_element: G::Element, blinded_element: G::Element, ) -> Self
Creates Ciphertext instance from random_element and blinded_element.
Sourcepub fn non_blinded<T>(value: T) -> Self
pub fn non_blinded<T>(value: T) -> Self
Creates a non-blinded encryption of the specified scalar value, i.e., (O, [value]G)
where O is identity and G is the conventional group generator.
Sourcepub fn random_element(&self) -> &G::Element
pub fn random_element(&self) -> &G::Element
Returns a reference to the random element.
Sourcepub fn blinded_element(&self) -> &G::Element
pub fn blinded_element(&self) -> &G::Element
Returns a reference to the blinded element.
Trait Implementations§
Source§impl<G: Group> Add for Ciphertext<G>
impl<G: Group> Add for Ciphertext<G>
Source§impl<G: Group> AddAssign for Ciphertext<G>
impl<G: Group> AddAssign for Ciphertext<G>
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+= operation. Read moreSource§impl<G: Clone + Group> Clone for Ciphertext<G>
impl<G: Clone + Group> Clone for Ciphertext<G>
Source§fn clone(&self) -> Ciphertext<G>
fn clone(&self) -> Ciphertext<G>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<G: Group> Debug for Ciphertext<G>
impl<G: Group> Debug for Ciphertext<G>
Source§impl<'de, G: Group> Deserialize<'de> for Ciphertext<G>
impl<'de, G: Group> Deserialize<'de> for Ciphertext<G>
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<G: Group, V: Zeroize> From<CiphertextWithValue<G, V>> for Ciphertext<G>
impl<G: Group, V: Zeroize> From<CiphertextWithValue<G, V>> for Ciphertext<G>
Source§fn from(ciphertext: CiphertextWithValue<G, V>) -> Self
fn from(ciphertext: CiphertextWithValue<G, V>) -> Self
Source§impl<G: Group> Neg for Ciphertext<G>
impl<G: Group> Neg for Ciphertext<G>
Source§impl<G: Group> Serialize for Ciphertext<G>
impl<G: Group> Serialize for Ciphertext<G>
Source§impl<G: Group> Sub for Ciphertext<G>
impl<G: Group> Sub for Ciphertext<G>
Source§impl<G: Group> SubAssign for Ciphertext<G>
impl<G: Group> SubAssign for Ciphertext<G>
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
-= operation. Read more