Replace to_elgamal_ciphertext with From trait for ElGamalCiphertext

This commit is contained in:
Sam Kim 2021-10-05 09:14:04 -04:00 committed by Michael Vines
parent a40e7fc59b
commit c150b4b197
3 changed files with 19 additions and 20 deletions

View File

@ -154,7 +154,7 @@ impl ElGamalPubkey {
/// Generate a decryption token from an ElGamal public key and a Pedersen
/// opening.
pub fn gen_decrypt_handle(self, open: &PedersenOpening) -> PedersenDecryptHandle {
PedersenDecryptHandle::generate_handle(open, &self)
PedersenDecryptHandle::new(&self, open)
}
}
@ -288,6 +288,15 @@ impl ElGamalCiphertext {
}
}
impl From<(PedersenCommitment, PedersenDecryptHandle)> for ElGamalCiphertext {
fn from((comm, handle): (PedersenCommitment, PedersenDecryptHandle)) -> Self {
ElGamalCiphertext {
message_comm: comm,
decrypt_handle: handle,
}
}
}
impl<'a, 'b> Add<&'b ElGamalCiphertext> for &'a ElGamalCiphertext {
type Output = ElGamalCiphertext;
@ -389,8 +398,8 @@ mod tests {
let decrypt_handle_1 = pk_1.gen_decrypt_handle(&open);
let decrypt_handle_2 = pk_2.gen_decrypt_handle(&open);
let ct_1 = decrypt_handle_1.to_elgamal_ciphertext(comm);
let ct_2 = decrypt_handle_2.to_elgamal_ciphertext(comm);
let ct_1: ElGamalCiphertext = (comm, decrypt_handle_1).into();
let ct_2: ElGamalCiphertext = (comm, decrypt_handle_2).into();
let expected_instance = DiscreteLog {
generator: PedersenBase::default().G,

View File

@ -1,7 +1,7 @@
#[cfg(not(target_arch = "bpf"))]
use rand::{rngs::OsRng, CryptoRng, RngCore};
use {
crate::encryption::elgamal::{ElGamalCiphertext, ElGamalPubkey},
crate::encryption::elgamal::ElGamalPubkey,
core::ops::{Add, Div, Mul, Sub},
curve25519_dalek::{
constants::{RISTRETTO_BASEPOINT_COMPRESSED, RISTRETTO_BASEPOINT_POINT},
@ -47,8 +47,6 @@ pub struct Pedersen;
impl Pedersen {
/// Given a number as input, the function returns a Pedersen commitment of
/// the number and its corresponding opening.
///
/// TODO: Interface that takes a random generator as input
#[cfg(not(target_arch = "bpf"))]
#[allow(clippy::new_ret_no_self)]
pub fn new<T: Into<Scalar>>(amount: T) -> (PedersenCommitment, PedersenOpening) {
@ -255,22 +253,14 @@ define_div_variants!(
#[derive(Serialize, Deserialize, Default, Clone, Copy, Debug, Eq, PartialEq)]
pub struct PedersenDecryptHandle(pub(crate) RistrettoPoint);
impl PedersenDecryptHandle {
pub fn new(pk: &ElGamalPubkey, open: &PedersenOpening) -> Self {
Self(pk.get_point() * open.get_scalar())
}
pub fn get_point(&self) -> RistrettoPoint {
self.0
}
pub fn generate_handle(open: &PedersenOpening, pk: &ElGamalPubkey) -> PedersenDecryptHandle {
PedersenDecryptHandle(open.get_scalar() * pk.get_point())
}
/// Maps a decryption token and Pedersen commitment to ElGamal ciphertext
pub fn to_elgamal_ciphertext(self, comm: PedersenCommitment) -> ElGamalCiphertext {
ElGamalCiphertext {
message_comm: comm,
decrypt_handle: self,
}
}
#[allow(clippy::wrong_self_convention)]
pub fn to_bytes(&self) -> [u8; 32] {
self.0.compress().to_bytes()

View File

@ -157,7 +157,7 @@ impl TransferData {
.try_into()?;
let decryption_handle = combine_u32_handles(decryption_handle_lo, decryption_handle_hi);
Ok(decryption_handle.to_elgamal_ciphertext(transfer_comm))
Ok((transfer_comm, decryption_handle).into())
}
/// Extracts the lo and hi destination ciphertexts associated with a transfer data and returns
@ -173,7 +173,7 @@ impl TransferData {
self.validity_proof.decryption_handles_hi.dest.try_into()?;
let decryption_handle = combine_u32_handles(decryption_handle_lo, decryption_handle_hi);
Ok(decryption_handle.to_elgamal_ciphertext(transfer_comm))
Ok((transfer_comm, decryption_handle).into())
}
}