elastic_elgamal

Struct Ciphertext

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

Source§

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>

Source§

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>

Source§

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>

Source§

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>

Source§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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

Source§

type Output = T

Should always be Self
Source§

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

Source§

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

Source§

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

Source§

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