pub struct Triple {
ek: RistrettoPoint,
ct: RistrettoPoint,
pk: RistrettoPoint,
}Expand description
ElGamal ciphertext - the result of PublicKey::encrypt.
The associated public key is remembered to allow rerandomization, but this public key is not authenticated in any way. This means that anyone intercepting a triple may modify the public key without detection (but this does not cause the triple to be decryptable to the same plaintext by another public key.)
Fields§
§ek: RistrettoPointEphemeral key
ct: RistrettoPointCiphertext,
pk: RistrettoPointPublic key
Implementations§
Source§impl Triple
impl Triple
Sourcepub fn decrypt(self, sk: &PrivateKey) -> RistrettoPoint
pub fn decrypt(self, sk: &PrivateKey) -> RistrettoPoint
Decrypts the triple using the given private key sk. If the triple was encrypted
for a different private key, the result is a random point.
Sourcepub fn decrypt_and_check_pk(self, sk: &PrivateKey) -> Option<RistrettoPoint>
pub fn decrypt_and_check_pk(self, sk: &PrivateKey) -> Option<RistrettoPoint>
Decrypts the triple using the given private key sk if the triple claims to be encrypted
for the associated public key; returns None otherwise.
Warning This function can’t check whether the triple’s public key pk has been
tampered with.
While tampering cannot be prevented, the plaintext of a triple with spoofed pk can be
garbled, using Self::rerandomize.
Sourcepub fn spoof_pk(self, pk: PublicKey) -> Triple
pub fn spoof_pk(self, pk: PublicKey) -> Triple
Changes the public key of this triple, likely resulting in garbage down the road.
Used for demonstration purposes.
Sourcepub fn rerandomize(self) -> Triple
pub fn rerandomize(self) -> Triple
Changes the appearance of the ciphertext, but leaves the plaintext and the target public key unaltered. If the public key was spoofed, the plaintext is garbled.
use pubhubs::common::elgamal::{PrivateKey, random_point, random_scalar};
use curve25519_dalek::{
ristretto::RistrettoPoint,
constants::RISTRETTO_BASEPOINT_TABLE as B,
};
let M = random_point();
let sk = PrivateKey::random();
let pk = sk.public_key();
let r1 = random_scalar();
let r2 = random_scalar();
// Rerandomization leaves the plaintext unchanged:
let trip = pk.encrypt_with_random(r1, M).rerandomize_with_random(r2);
assert_eq!(trip, pk.encrypt_with_random(r1+r2,M));
// But if the public key was spoofed, the plaintext is garbled:
let sk2 = PrivateKey::random();
let pk2 = sk2.public_key().clone();
let trip = pk.encrypt_with_random(r1, M).spoof_pk(pk2).rerandomize_with_random(r2);
assert_eq!(trip.clone().decrypt_and_check_pk(&sk2),
Some(M + B * &(r1 * (sk.as_scalar()-sk2.as_scalar()))));
// Indeed, if sk =/= sk2, then r1(sk - sk2)B will be some random unknowable Ristretto
// point, because r1 should be a random scalar that has been thrown away.Sourcepub fn rerandomize_with_random(self, r: Scalar) -> Triple
pub fn rerandomize_with_random(self, r: Scalar) -> Triple
Like Self::rerandomize, but you can specify the random scalar used - which you shouldn’t except to make deterministic tests.
Sourcepub fn rsk_with_s(self, s: &Scalar) -> WithS<'_>
pub fn rsk_with_s(self, s: &Scalar) -> WithS<'_>
Like rsk but taking the parameters s and k thusly: rsk_with_s(s).and_k(k).
Sourcepub fn rsk(self, params: impl Params) -> Triple
pub fn rsk(self, params: impl Params) -> Triple
Changes the given ciphertext according to the params provided:
-
Multiplies the underlying plaintext by
params.s(); -
Multiplies the target public/private key by
params.k(); -
Rerandomizes the ciphertext using the scalar
params.r().If the public key
self.pkwas spoofed, the resulting plaintext is garbled, provided the scalarparams.r()was random.
If you only need to specify s and k, use triple.rsk_with_s(s).and_k(k) instead.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Triple
impl<'de> Deserialize<'de> for Triple
Source§fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error>
Source§impl Encoding<96> for Triple
impl Encoding<96> for Triple
Source§fn from_bytes(bytes: [u8; 96]) -> Option<Triple>
fn from_bytes(bytes: [u8; 96]) -> Option<Triple>
Some(object) from bytes if bytes encodes some object of type Self;
otherwise returns None.Source§fn from_slice(slice: &[u8]) -> Option<Self>
fn from_slice(slice: &[u8]) -> Option<Self>
[u8; N] from slice. Returns None if slice.len()!=N
or when the slice is not a valid encoding.Source§fn copy_to_slice(&self, slice: &mut [u8]) -> Option<()>
fn copy_to_slice(&self, slice: &mut [u8]) -> Option<()>
self into slice. Returns None when slice.len()!=N.Source§fn from_hex(hex: &str) -> Option<Self>
fn from_hex(hex: &str) -> Option<Self>
[u8; N] from the 2*N-digit hex string hex.
The case of the hex digits is ignored.impl Eq for Triple
impl StructuralPartialEq for Triple
Auto Trait Implementations§
impl Freeze for Triple
impl RefUnwindSafe for Triple
impl Send for Triple
impl Sync for Triple
impl Unpin for Triple
impl UnsafeUnpin for Triple
impl UnwindSafe for Triple
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more