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

Represents encryption of zero value without the blinding factor.

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.

Returns a reference to the random element.

Returns a reference to the blinded element.

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

Trait Implementations

The resulting type after applying the + operator.

Performs the + operation. Read more

Performs the += operation. Read more

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Deserialize this value from the given Serde deserializer. Read more

Converts to this type from the input type.

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the - operator.

Performs the unary - operation. Read more

Serialize this value into the given Serde serializer. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

Performs the -= operation. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.