From 9fdadb503d915cf92295f83e72655adddecf0f71 Mon Sep 17 00:00:00 2001 From: Sam Kim Date: Wed, 13 Oct 2021 09:45:16 -0400 Subject: [PATCH] merge --- zk-token-sdk/src/encryption/elgamal.rs | 30 +++++++++++++++++--------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/zk-token-sdk/src/encryption/elgamal.rs b/zk-token-sdk/src/encryption/elgamal.rs index ed5ef0bcb..63faa306b 100644 --- a/zk-token-sdk/src/encryption/elgamal.rs +++ b/zk-token-sdk/src/encryption/elgamal.rs @@ -12,6 +12,7 @@ use { scalar::Scalar, }, ed25519_dalek::SecretKey as SigningKey, + solana_sdk::pubkey::Pubkey, serde::{Deserialize, Serialize}, std::collections::HashMap, std::convert::TryInto, @@ -56,14 +57,6 @@ impl ElGamal { } } - #[cfg(not(target_arch = "bpf"))] - #[allow(non_snake_case)] - pub fn from_signing_key(signing_key: &SigningKey, label: &'static [u8]) -> Self { - let secret = ElGamalSecretKey::new(signing_key, label); - let public = ElGamalPubkey::new(&secret); - Self { secret, public } - } - /// On input a public key and a message to be encrypted, the function /// returns an ElGamal ciphertext of the message under the public key. #[cfg(not(target_arch = "bpf"))] @@ -139,6 +132,21 @@ pub struct ElGamalKeypair { } impl ElGamalKeypair { + /// Generates the public and secret keys for ElGamal encryption from Ed25519 signing key and an + /// address. + #[cfg(not(target_arch = "bpf"))] + #[allow(non_snake_case)] + pub fn new(signing_key: &SigningKey, address: &Pubkey) -> Self { + let secret = ElGamalSecretKey::new(signing_key, address); + let public = ElGamalPubkey::new(&secret); + + Self { + secret, + public, + } + } + + /// Generates the public and secret keys for ElGamal encryption. #[cfg(not(target_arch = "bpf"))] #[allow(clippy::new_ret_no_self)] @@ -288,8 +296,10 @@ impl fmt::Display for ElGamalPubkey { #[zeroize(drop)] pub struct ElGamalSecretKey(Scalar); impl ElGamalSecretKey { - pub fn new(signing_key: &SigningKey, label: &'static [u8]) -> Self { - let hashable = [&signing_key.to_bytes(), label].concat(); + pub fn new(signing_key: &SigningKey, address: &Pubkey) -> Self { + let mut hashable = [0_u8; 64]; + hashable[..32].copy_from_slice(&signing_key.to_bytes()); + hashable[32..].copy_from_slice(&address.to_bytes()); ElGamalSecretKey(Scalar::hash_from_bytes::(&hashable)) }