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:

  • G is the conventional group generator
  • r is a random scalar selected by the encrypting party
  • K and k are 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 = thread_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 = thread_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 = thread_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>

source

pub fn zero() -> Self

Represents encryption of zero value without the blinding factor.

source

pub fn non_blinded<T>(value: T) -> Self
where G::Scalar: From<T>,

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.

source

pub fn random_element(&self) -> &G::Element

Returns a reference to the random element.

source

pub fn blinded_element(&self) -> &G::Element

Returns a reference to the blinded element.

source

pub fn to_bytes(self) -> Vec<u8>

Serializes this ciphertext as two group elements (the random element, then the blinded value).

Trait Implementations§

source§

impl<G: Group> Add for Ciphertext<G>

§

type Output = Ciphertext<G>

The resulting type after applying the + operator.
source§

fn add(self, rhs: Self) -> Self

Performs the + operation. Read more
source§

impl<G: Group> AddAssign for Ciphertext<G>

source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
source§

impl<G: Clone + Group> Clone for Ciphertext<G>
where G::Element: Clone,

source§

fn clone(&self) -> Ciphertext<G>

Returns a copy 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<G: Group> Debug for Ciphertext<G>

source§

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

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

impl<'de, G: Group> Deserialize<'de> for Ciphertext<G>

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<G: Group, V: Zeroize> From<CiphertextWithValue<G, V>> for Ciphertext<G>

source§

fn from(ciphertext: CiphertextWithValue<G, V>) -> Self

Converts to this type from the input type.
source§

impl<G: Group> Mul<&<G as ScalarOps>::Scalar> for Ciphertext<G>

§

type Output = Ciphertext<G>

The resulting type after applying the * operator.
source§

fn mul(self, rhs: &G::Scalar) -> Self

Performs the * operation. Read more
source§

impl<G: Group> Mul<u64> for Ciphertext<G>

§

type Output = Ciphertext<G>

The resulting type after applying the * operator.
source§

fn mul(self, rhs: u64) -> Self

Performs the * operation. Read more
source§

impl<G: Group> Neg for Ciphertext<G>

§

type Output = Ciphertext<G>

The resulting type after applying the - operator.
source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
source§

impl<G: Group> Serialize for Ciphertext<G>

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<G: Group> Sub for Ciphertext<G>

§

type Output = Ciphertext<G>

The resulting type after applying the - operator.
source§

fn sub(self, rhs: Self) -> Self

Performs the - operation. Read more
source§

impl<G: Group> SubAssign for Ciphertext<G>

source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
source§

impl<G: Copy + Group> Copy for Ciphertext<G>
where G::Element: Copy,

Auto Trait Implementations§

§

impl<G> Freeze for Ciphertext<G>
where <G as ElementOps>::Element: Freeze,

§

impl<G> RefUnwindSafe for Ciphertext<G>

§

impl<G> Send for Ciphertext<G>
where <G as ElementOps>::Element: Send,

§

impl<G> Sync for Ciphertext<G>
where <G as ElementOps>::Element: Sync,

§

impl<G> Unpin for Ciphertext<G>
where <G as ElementOps>::Element: Unpin,

§

impl<G> UnwindSafe for Ciphertext<G>
where <G as ElementOps>::Element: UnwindSafe,

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> 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> Same for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

§

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>,

§

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>,

§

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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

§

impl<T, Rhs, Output> GroupOps<Rhs, Output> for T
where T: Add<Rhs, Output = Output> + Sub<Rhs, Output = Output> + AddAssign<Rhs> + SubAssign<Rhs>,