elastic_elgamal/proofs/commitment.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 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
//! Zero-knowledge proof of ElGamal encryption and Pedersen commitment equivalence.
use merlin::Transcript;
use rand_core::{CryptoRng, RngCore};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[cfg(feature = "serde")]
use crate::serde::ScalarHelper;
use crate::{
group::Group,
proofs::{TranscriptForGroup, VerificationError},
Ciphertext, CiphertextWithValue, PublicKey, SecretKey,
};
/// Zero-knowledge proof that an ElGamal ciphertext encrypts the same value as a Pedersen
/// commitment.
///
/// This proof can be used to switch from frameworks applicable to ElGamal ciphertexts, to ones
/// applicable to Pedersen commitments (e.g., [Bulletproofs] for range proofs).
///
/// [Bulletproofs]: https://crypto.stanford.edu/bulletproofs/
///
/// # Construction
///
/// We want to prove in zero knowledge the knowledge of scalars `r_e`, `v`, `r_c` such as
///
/// ```text
/// R = [r_e]G; B = [v]G + [r_e]K;
/// // (R, B) is ElGamal ciphertext of `v` for public key `K`
/// C = [v]G + [r_c]H;
/// // C is Pedersen commitment to `v`
/// ```
///
/// Here, we assume that the conventional group generator `G` is shared between encryption and
/// commitment protocols.
///
/// An interactive version of the proof can be built as a sigma protocol:
///
/// 1. **Commitment.** The prover generates 3 random scalars `e_r`, `e_v` and `e_c` and commits
/// to them via `E_r = [e_r]G`, `E_b = [e_v]G + [e_r]K`, and `E_c = [e_v]G + [e_c]H`.
/// 2. **Challenge.** The verifier sends to the prover random scalar `c`.
/// 3. **Response.** The prover computes the following scalars and sends them to the verifier.
///
/// ```text
/// s_r = e_r + c * r_e;
/// s_v = e_v + c * v;
/// s_c = e_c + c * r_c;
/// ```
///
/// The verification equations are
///
/// ```text
/// [s_r]G ?= E_r + [c]R;
/// [s_v]G + [s_r]K ?= E_b + [c]B;
/// [s_v]G + [s_c]H ?= E_c + [c]C;
/// ```
///
/// A non-interactive version of the proof is obtained by applying [Fiat–Shamir transform][fst].
/// As with other proofs, it is more efficient to represent a proof as the challenge
/// and responses (i.e., 4 scalars in total).
///
/// [fst]: https://en.wikipedia.org/wiki/Fiat%E2%80%93Shamir_heuristic
///
/// # Examples
///
/// ```
/// # use elastic_elgamal::{
/// # group::{ElementOps, ScalarOps, Group, Ristretto},
/// # Keypair, SecretKey, CommitmentEquivalenceProof, CiphertextWithValue,
/// # };
/// # use merlin::Transcript;
/// # use rand::thread_rng;
/// #
/// # const BLINDING_BASE: &[u8] = &[
/// # 140, 146, 64, 180, 86, 169, 230, 220, 101, 195, 119, 161, 4,
/// # 141, 116, 95, 148, 160, 140, 219, 127, 68, 203, 205, 123, 70,
/// # 243, 64, 72, 135, 17, 52,
/// # ];
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let blinding_base = // Blinding base for Pedersen commitments
/// // (e.g., from Bulletproofs)
/// # Ristretto::deserialize_element(BLINDING_BASE).unwrap();
/// let mut rng = thread_rng();
/// let (receiver, _) = Keypair::<Ristretto>::generate(&mut rng).into_tuple();
///
/// // Create an ElGamal ciphertext of `value` for `receiver`.
/// let value = 424242_u64;
/// let ciphertext = CiphertextWithValue::new(value, &receiver, &mut rng)
/// .generalize();
/// // Create a blinding factor for the Pedersen commitment of the same value.
/// let blinding = SecretKey::generate(&mut rng);
/// let (proof, commitment) = CommitmentEquivalenceProof::new(
/// &ciphertext,
/// &receiver,
/// &blinding,
/// blinding_base,
/// &mut Transcript::new(b"custom_proof"),
/// &mut rng,
/// );
/// // Use `commitment` and `blinding` in other proofs...
///
/// proof.verify(
/// &ciphertext.into(),
/// &receiver,
/// commitment,
/// blinding_base,
/// &mut Transcript::new(b"custom_proof"),
/// )?;
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(bound = ""))]
pub struct CommitmentEquivalenceProof<G: Group> {
#[cfg_attr(feature = "serde", serde(with = "ScalarHelper::<G>"))]
challenge: G::Scalar,
#[cfg_attr(feature = "serde", serde(with = "ScalarHelper::<G>"))]
randomness_response: G::Scalar,
#[cfg_attr(feature = "serde", serde(with = "ScalarHelper::<G>"))]
value_response: G::Scalar,
#[cfg_attr(feature = "serde", serde(with = "ScalarHelper::<G>"))]
commitment_response: G::Scalar,
}
impl<G: Group> CommitmentEquivalenceProof<G> {
/// Creates a proof based on the `ciphertext` for `receiver` and `commitment_blinding`
/// with `commitment_blinding_base` for a Pedersen commitment. (The latter two args
/// correspond to `r_c` and `H` in the [*Construction*](#construction) section, respectively.)
///
/// # Return value
///
/// Returns a proof together with the Pedersen commitment.
pub fn new<R: RngCore + CryptoRng>(
ciphertext: &CiphertextWithValue<G>,
receiver: &PublicKey<G>,
commitment_blinding: &SecretKey<G>,
commitment_blinding_base: G::Element,
transcript: &mut Transcript,
rng: &mut R,
) -> (Self, G::Element) {
let commitment = G::multi_mul(
[ciphertext.value(), commitment_blinding.expose_scalar()],
[G::generator(), commitment_blinding_base],
);
transcript.start_proof(b"commitment_equivalence");
transcript.append_element_bytes(b"K", receiver.as_bytes());
transcript.append_element::<G>(b"R", &ciphertext.inner().random_element);
transcript.append_element::<G>(b"B", &ciphertext.inner().blinded_element);
transcript.append_element::<G>(b"C", &commitment);
let random_scalar = SecretKey::<G>::generate(rng);
let value_scalar = SecretKey::<G>::generate(rng);
let commitment_scalar = SecretKey::<G>::generate(rng);
let random_commitment = G::mul_generator(random_scalar.expose_scalar());
transcript.append_element::<G>(b"[e_r]G", &random_commitment);
let value_element = G::mul_generator(value_scalar.expose_scalar());
let enc_blinding_commitment =
value_element + receiver.as_element() * random_scalar.expose_scalar();
transcript.append_element::<G>(b"[e_v]G + [e_r]K", &enc_blinding_commitment);
let commitment_commitment =
value_element + commitment_blinding_base * commitment_scalar.expose_scalar();
transcript.append_element::<G>(b"[e_v]G + [e_c]H", &commitment_commitment);
let challenge = transcript.challenge_scalar::<G>(b"c");
let randomness_response =
challenge * ciphertext.randomness().expose_scalar() + random_scalar.expose_scalar();
let value_response = challenge * ciphertext.value() + value_scalar.expose_scalar();
let commitment_response =
challenge * commitment_blinding.expose_scalar() + commitment_scalar.expose_scalar();
let proof = Self {
challenge,
randomness_response,
value_response,
commitment_response,
};
(proof, commitment)
}
/// # Errors
///
/// Returns an error if this proof does not verify.
pub fn verify(
&self,
ciphertext: &Ciphertext<G>,
receiver: &PublicKey<G>,
commitment: G::Element,
commitment_blinding_base: G::Element,
transcript: &mut Transcript,
) -> Result<(), VerificationError> {
transcript.start_proof(b"commitment_equivalence");
transcript.append_element_bytes(b"K", receiver.as_bytes());
transcript.append_element::<G>(b"R", &ciphertext.random_element);
transcript.append_element::<G>(b"B", &ciphertext.blinded_element);
transcript.append_element::<G>(b"C", &commitment);
let neg_challenge = -self.challenge;
let random_commitment = G::vartime_double_mul_generator(
&neg_challenge,
ciphertext.random_element,
&self.randomness_response,
);
transcript.append_element::<G>(b"[e_r]G", &random_commitment);
let enc_blinding_commitment = G::vartime_multi_mul(
[
&self.value_response,
&self.randomness_response,
&neg_challenge,
],
[
G::generator(),
receiver.as_element(),
ciphertext.blinded_element,
],
);
transcript.append_element::<G>(b"[e_v]G + [e_r]K", &enc_blinding_commitment);
let commitment_commitment = G::vartime_multi_mul(
[
&self.value_response,
&self.commitment_response,
&neg_challenge,
],
[G::generator(), commitment_blinding_base, commitment],
);
transcript.append_element::<G>(b"[e_v]G + [e_c]H", &commitment_commitment);
let expected_challenge = transcript.challenge_scalar::<G>(b"c");
if expected_challenge == self.challenge {
Ok(())
} else {
Err(VerificationError::ChallengeMismatch)
}
}
}
#[cfg(all(test, feature = "curve25519-dalek"))]
mod tests {
use super::*;
use crate::{
group::{ElementOps, Ristretto},
Keypair,
};
use bulletproofs::PedersenGens;
use rand::thread_rng;
#[test]
fn equivalence_proof_basics() {
let mut rng = thread_rng();
let (receiver, _) = Keypair::<Ristretto>::generate(&mut rng).into_tuple();
let value = 1234_u64;
let ciphertext = CiphertextWithValue::new(value, &receiver, &mut rng).generalize();
let commitment_gens = PedersenGens::default();
assert_eq!(commitment_gens.B, Ristretto::generator());
let blinding = SecretKey::generate(&mut rng);
let (proof, commitment) = CommitmentEquivalenceProof::new(
&ciphertext,
&receiver,
&blinding,
commitment_gens.B_blinding,
&mut Transcript::new(b"test"),
&mut rng,
);
assert_eq!(
commitment,
commitment_gens.commit(*ciphertext.value(), *blinding.expose_scalar())
);
let ciphertext = ciphertext.into();
proof
.verify(
&ciphertext,
&receiver,
commitment,
commitment_gens.B_blinding,
&mut Transcript::new(b"test"),
)
.unwrap();
let other_ciphertext = receiver.encrypt(8_u64, &mut rng);
let err = proof
.verify(
&other_ciphertext,
&receiver,
commitment,
commitment_gens.B_blinding,
&mut Transcript::new(b"test"),
)
.unwrap_err();
assert!(matches!(err, VerificationError::ChallengeMismatch));
let err = proof
.verify(
&ciphertext,
&receiver,
commitment + Ristretto::generator(),
commitment_gens.B_blinding,
&mut Transcript::new(b"test"),
)
.unwrap_err();
assert!(matches!(err, VerificationError::ChallengeMismatch));
let err = proof
.verify(
&ciphertext,
&receiver,
commitment,
commitment_gens.B_blinding,
&mut Transcript::new(b"other_test"),
)
.unwrap_err();
assert!(matches!(err, VerificationError::ChallengeMismatch));
}
}