solana_zk_token_sdk/encryption/grouped_elgamal.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
//! The twisted ElGamal group encryption implementation.
//!
//! The message space consists of any number that is representable as a scalar (a.k.a. "exponent")
//! for Curve25519.
//!
//! A regular twisted ElGamal ciphertext consists of two components:
//! - A Pedersen commitment that encodes a message to be encrypted
//! - A "decryption handle" that binds the Pedersen opening to a specific public key
//! The ciphertext can be generalized to hold not a single decryption handle, but multiple handles
//! pertaining to multiple ElGamal public keys. These ciphertexts are referred to as a "grouped"
//! ElGamal ciphertext.
//!
use {
crate::{
encryption::{
discrete_log::DiscreteLog,
elgamal::{DecryptHandle, ElGamalCiphertext, ElGamalPubkey, ElGamalSecretKey},
pedersen::{Pedersen, PedersenCommitment, PedersenOpening},
},
RISTRETTO_POINT_LEN,
},
curve25519_dalek::scalar::Scalar,
thiserror::Error,
};
#[derive(Error, Clone, Debug, Eq, PartialEq)]
pub enum GroupedElGamalError {
#[error("index out of bounds")]
IndexOutOfBounds,
}
/// Algorithm handle for the grouped ElGamal encryption
pub struct GroupedElGamal<const N: usize>;
impl<const N: usize> GroupedElGamal<N> {
/// Encrypts an amount under an array of ElGamal public keys.
///
/// This function is randomized. It internally samples a scalar element using `OsRng`.
pub fn encrypt<T: Into<Scalar>>(
pubkeys: [&ElGamalPubkey; N],
amount: T,
) -> GroupedElGamalCiphertext<N> {
let (commitment, opening) = Pedersen::new(amount);
let handles: [DecryptHandle; N] = pubkeys
.iter()
.map(|handle| handle.decrypt_handle(&opening))
.collect::<Vec<DecryptHandle>>()
.try_into()
.unwrap();
GroupedElGamalCiphertext {
commitment,
handles,
}
}
/// Encrypts an amount under an array of ElGamal public keys using a specified Pedersen
/// opening.
pub fn encrypt_with<T: Into<Scalar>>(
pubkeys: [&ElGamalPubkey; N],
amount: T,
opening: &PedersenOpening,
) -> GroupedElGamalCiphertext<N> {
let commitment = Pedersen::with(amount, opening);
let handles: [DecryptHandle; N] = pubkeys
.iter()
.map(|handle| handle.decrypt_handle(opening))
.collect::<Vec<DecryptHandle>>()
.try_into()
.unwrap();
GroupedElGamalCiphertext {
commitment,
handles,
}
}
/// Converts a grouped ElGamal ciphertext into a regular ElGamal ciphertext using the decrypt
/// handle at a specified index.
fn to_elgamal_ciphertext(
grouped_ciphertext: &GroupedElGamalCiphertext<N>,
index: usize,
) -> Result<ElGamalCiphertext, GroupedElGamalError> {
let handle = grouped_ciphertext
.handles
.get(index)
.ok_or(GroupedElGamalError::IndexOutOfBounds)?;
Ok(ElGamalCiphertext {
commitment: grouped_ciphertext.commitment,
handle: *handle,
})
}
/// Decrypts a grouped ElGamal ciphertext using an ElGamal secret key pertaining to a
/// decryption handle at a specified index.
///
/// The output of this function is of type `DiscreteLog`. To recover the originally encrypted
/// amount, use `DiscreteLog::decode`.
fn decrypt(
grouped_ciphertext: &GroupedElGamalCiphertext<N>,
secret: &ElGamalSecretKey,
index: usize,
) -> Result<DiscreteLog, GroupedElGamalError> {
Self::to_elgamal_ciphertext(grouped_ciphertext, index)
.map(|ciphertext| ciphertext.decrypt(secret))
}
/// Decrypts a grouped ElGamal ciphertext to a number that is interpreted as a positive 32-bit
/// number (but still of type `u64`).
///
/// If the originally encrypted amount is not a positive 32-bit number, then the function
/// Result contains `None`.
fn decrypt_u32(
grouped_ciphertext: &GroupedElGamalCiphertext<N>,
secret: &ElGamalSecretKey,
index: usize,
) -> Result<Option<u64>, GroupedElGamalError> {
Self::to_elgamal_ciphertext(grouped_ciphertext, index)
.map(|ciphertext| ciphertext.decrypt_u32(secret))
}
}
/// A grouped ElGamal ciphertext.
///
/// The type is defined with a generic constant parameter that specifies the number of
/// decryption handles that the ciphertext holds.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct GroupedElGamalCiphertext<const N: usize> {
pub commitment: PedersenCommitment,
pub handles: [DecryptHandle; N],
}
impl<const N: usize> GroupedElGamalCiphertext<N> {
/// Decrypts the grouped ElGamal ciphertext using an ElGamal secret key pertaining to a
/// specified index.
///
/// The output of this function is of type `DiscreteLog`. To recover the originally encrypted
/// amount, use `DiscreteLog::decode`.
pub fn decrypt(
&self,
secret: &ElGamalSecretKey,
index: usize,
) -> Result<DiscreteLog, GroupedElGamalError> {
GroupedElGamal::decrypt(self, secret, index)
}
/// Decrypts the grouped ElGamal ciphertext to a number that is interpreted as a positive 32-bit
/// number (but still of type `u64`).
///
/// If the originally encrypted amount is not a positive 32-bit number, then the function
/// returns `None`.
pub fn decrypt_u32(
&self,
secret: &ElGamalSecretKey,
index: usize,
) -> Result<Option<u64>, GroupedElGamalError> {
GroupedElGamal::decrypt_u32(self, secret, index)
}
/// The expected length of a serialized grouped ElGamal ciphertext.
///
/// A grouped ElGamal ciphertext consists of a Pedersen commitment and an array of decryption
/// handles. The commitment and decryption handles are each a single Curve25519 group element
/// that is serialized as 32 bytes. Therefore, the total byte length of a grouped ciphertext is
/// `(N+1) * 32`.
fn expected_byte_length() -> usize {
N.checked_add(1)
.and_then(|length| length.checked_mul(RISTRETTO_POINT_LEN))
.unwrap()
}
pub fn to_bytes(&self) -> Vec<u8> {
let mut buf = Vec::with_capacity(Self::expected_byte_length());
buf.extend_from_slice(&self.commitment.to_bytes());
self.handles
.iter()
.for_each(|handle| buf.extend_from_slice(&handle.to_bytes()));
buf
}
pub fn from_bytes(bytes: &[u8]) -> Option<Self> {
if bytes.len() != Self::expected_byte_length() {
return None;
}
let mut iter = bytes.chunks(RISTRETTO_POINT_LEN);
let commitment = PedersenCommitment::from_bytes(iter.next()?)?;
let mut handles = Vec::with_capacity(N);
for handle_bytes in iter {
handles.push(DecryptHandle::from_bytes(handle_bytes)?);
}
Some(Self {
commitment,
handles: handles.try_into().unwrap(),
})
}
}
#[cfg(test)]
mod tests {
use {super::*, crate::encryption::elgamal::ElGamalKeypair};
#[test]
fn test_grouped_elgamal_encrypt_decrypt_correctness() {
let elgamal_keypair_0 = ElGamalKeypair::new_rand();
let elgamal_keypair_1 = ElGamalKeypair::new_rand();
let elgamal_keypair_2 = ElGamalKeypair::new_rand();
let amount: u64 = 10;
let grouped_ciphertext = GroupedElGamal::encrypt(
[
elgamal_keypair_0.pubkey(),
elgamal_keypair_1.pubkey(),
elgamal_keypair_2.pubkey(),
],
amount,
);
assert_eq!(
Some(amount),
grouped_ciphertext
.decrypt_u32(elgamal_keypair_0.secret(), 0)
.unwrap()
);
assert_eq!(
Some(amount),
grouped_ciphertext
.decrypt_u32(elgamal_keypair_1.secret(), 1)
.unwrap()
);
assert_eq!(
Some(amount),
grouped_ciphertext
.decrypt_u32(elgamal_keypair_2.secret(), 2)
.unwrap()
);
assert_eq!(
GroupedElGamalError::IndexOutOfBounds,
grouped_ciphertext
.decrypt_u32(elgamal_keypair_0.secret(), 3)
.unwrap_err()
);
}
#[test]
fn test_grouped_ciphertext_bytes() {
let elgamal_keypair_0 = ElGamalKeypair::new_rand();
let elgamal_keypair_1 = ElGamalKeypair::new_rand();
let elgamal_keypair_2 = ElGamalKeypair::new_rand();
let amount: u64 = 10;
let grouped_ciphertext = GroupedElGamal::encrypt(
[
elgamal_keypair_0.pubkey(),
elgamal_keypair_1.pubkey(),
elgamal_keypair_2.pubkey(),
],
amount,
);
let produced_bytes = grouped_ciphertext.to_bytes();
assert_eq!(produced_bytes.len(), 128);
let decoded_grouped_ciphertext =
GroupedElGamalCiphertext::<3>::from_bytes(&produced_bytes).unwrap();
assert_eq!(
Some(amount),
decoded_grouped_ciphertext
.decrypt_u32(elgamal_keypair_0.secret(), 0)
.unwrap()
);
assert_eq!(
Some(amount),
decoded_grouped_ciphertext
.decrypt_u32(elgamal_keypair_1.secret(), 1)
.unwrap()
);
assert_eq!(
Some(amount),
decoded_grouped_ciphertext
.decrypt_u32(elgamal_keypair_2.secret(), 2)
.unwrap()
);
}
}