Skip to content

Commit

Permalink
v2.1: [zk-sdk] Expose ElGamal decryption and proof program to wasm ta…
Browse files Browse the repository at this point in the history
…rget (backport of #3601) (#3630)
  • Loading branch information
mergify[bot] authored Nov 19, 2024
1 parent 0e50e66 commit d152868
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 37 deletions.
1 change: 1 addition & 0 deletions zk-sdk/src/encryption/discrete_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ impl DiscreteLog {
/// Solves the discrete log problem under the assumption that the solution
/// is a positive 32-bit number.
pub fn decode_u32(self) -> Option<u64> {
#[allow(unused_variables)]
if let Some(num_threads) = self.num_threads {
#[cfg(not(target_arch = "wasm32"))]
{
Expand Down
66 changes: 31 additions & 35 deletions zk-sdk/src/encryption/elgamal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,10 @@ use wasm_bindgen::prelude::*;
// types and functions exported for wasm targets in all of its dependencies
// (https://github.com/rustwasm/wasm-bindgen/issues/3759). We specifically exclude some of the
// dependencies that will cause unnecessary bloat to the wasm binary.
#[cfg(not(target_arch = "wasm32"))]
use {
crate::encryption::discrete_log::DiscreteLog,
sha3::Digest,
solana_derivation_path::DerivationPath,
solana_sdk::{
signature::Signature,
signer::{
keypair::generate_seed_from_seed_phrase_and_passphrase, EncodableKey, EncodableKeypair,
SeedDerivable, Signer, SignerError,
},
},
std::{
error,
io::{Read, Write},
path::Path,
},
};
use {
crate::{
encryption::{
discrete_log::DiscreteLog,
pedersen::{Pedersen, PedersenCommitment, PedersenOpening, G, H},
DECRYPT_HANDLE_LEN, ELGAMAL_CIPHERTEXT_LEN, ELGAMAL_KEYPAIR_LEN, ELGAMAL_PUBKEY_LEN,
ELGAMAL_SECRET_KEY_LEN, PEDERSEN_COMMITMENT_LEN,
Expand All @@ -61,6 +44,23 @@ use {
subtle::{Choice, ConstantTimeEq},
zeroize::Zeroize,
};
#[cfg(not(target_arch = "wasm32"))]
use {
sha3::Digest,
solana_derivation_path::DerivationPath,
solana_sdk::{
signature::Signature,
signer::{
keypair::generate_seed_from_seed_phrase_and_passphrase, EncodableKey, EncodableKeypair,
SeedDerivable, Signer, SignerError,
},
},
std::{
error,
io::{Read, Write},
path::Path,
},
};

/// Algorithm handle for the twisted ElGamal encryption scheme
pub struct ElGamal;
Expand Down Expand Up @@ -126,7 +126,6 @@ impl ElGamal {
///
/// The output of this function is of type `DiscreteLog`. To recover, the originally encrypted
/// amount, use `DiscreteLog::decode`.
#[cfg(not(target_arch = "wasm32"))]
fn decrypt(secret: &ElGamalSecretKey, ciphertext: &ElGamalCiphertext) -> DiscreteLog {
DiscreteLog::new(
*G,
Expand All @@ -139,7 +138,6 @@ impl ElGamal {
///
/// If the originally encrypted amount is not a positive 32-bit number, then the function
/// returns `None`.
#[cfg(not(target_arch = "wasm32"))]
fn decrypt_u32(secret: &ElGamalSecretKey, ciphertext: &ElGamalCiphertext) -> Option<u64> {
let discrete_log_instance = Self::decrypt(secret, ciphertext);
discrete_log_instance.decode_u32()
Expand Down Expand Up @@ -467,6 +465,19 @@ impl ElGamalSecretKey {
pub fn as_bytes(&self) -> &[u8; ELGAMAL_SECRET_KEY_LEN] {
self.0.as_bytes()
}

/// Decrypts a ciphertext using the ElGamal secret key.
///
/// The output of this function is of type `DiscreteLog`. To recover, the originally encrypted
/// message, use `DiscreteLog::decode`.
pub fn decrypt(&self, ciphertext: &ElGamalCiphertext) -> DiscreteLog {
ElGamal::decrypt(self, ciphertext)
}

/// Decrypts a ciphertext using the ElGamal secret key interpretting the message as type `u32`.
pub fn decrypt_u32(&self, ciphertext: &ElGamalCiphertext) -> Option<u64> {
ElGamal::decrypt_u32(self, ciphertext)
}
}

#[cfg(not(target_arch = "wasm32"))]
Expand Down Expand Up @@ -517,19 +528,6 @@ impl ElGamalSecretKey {

result.to_vec()
}

/// Decrypts a ciphertext using the ElGamal secret key.
///
/// The output of this function is of type `DiscreteLog`. To recover, the originally encrypted
/// message, use `DiscreteLog::decode`.
pub fn decrypt(&self, ciphertext: &ElGamalCiphertext) -> DiscreteLog {
ElGamal::decrypt(self, ciphertext)
}

/// Decrypts a ciphertext using the ElGamal secret key interpretting the message as type `u32`.
pub fn decrypt_u32(&self, ciphertext: &ElGamalCiphertext) -> Option<u64> {
ElGamal::decrypt_u32(self, ciphertext)
}
}

#[cfg(not(target_arch = "wasm32"))]
Expand Down Expand Up @@ -666,7 +664,6 @@ impl ElGamalCiphertext {
///
/// The output of this function is of type `DiscreteLog`. To recover, the originally encrypted
/// amount, use `DiscreteLog::decode`.
#[cfg(not(target_arch = "wasm32"))]
pub fn decrypt(&self, secret: &ElGamalSecretKey) -> DiscreteLog {
ElGamal::decrypt(secret, self)
}
Expand All @@ -676,7 +673,6 @@ impl ElGamalCiphertext {
///
/// If the originally encrypted amount is not a positive 32-bit number, then the function
/// returns `None`.
#[cfg(not(target_arch = "wasm32"))]
pub fn decrypt_u32(&self, secret: &ElGamalSecretKey) -> Option<u64> {
ElGamal::decrypt_u32(secret, self)
}
Expand Down
2 changes: 1 addition & 1 deletion zk-sdk/src/encryption/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{RISTRETTO_POINT_LEN, SCALAR_LEN};
pub(crate) mod macros;
#[cfg(not(target_os = "solana"))]
pub mod auth_encryption;
#[cfg(all(not(target_os = "solana"), not(target_arch = "wasm32")))]
#[cfg(not(target_os = "solana"))]
pub mod discrete_log;
#[cfg(not(target_os = "solana"))]
pub mod elgamal;
Expand Down
1 change: 0 additions & 1 deletion zk-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ pub mod pod;
mod range_proof;
mod sigma_proofs;
mod transcript;
#[cfg(not(target_arch = "wasm32"))]
pub mod zk_elgamal_proof_program;

/// Byte length of a compressed Ristretto point or scalar in Curve255519
Expand Down

0 comments on commit d152868

Please sign in to comment.