Skip to content

Commit

Permalink
[zk-sdk] Expose ElGamal decryption and proof program to wasm target (#…
Browse files Browse the repository at this point in the history
…3601)

* expose `zk_elgamal_proof_program` to wasm target

* expose ElGamal decryption to wasm target
  • Loading branch information
samkim-crypto authored Nov 14, 2024
1 parent 9b29626 commit 8b720f7
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 35 deletions.
2 changes: 1 addition & 1 deletion zk-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ merlin = { workspace = true }
num-derive = { workspace = true }
num-traits = { workspace = true }
solana-instruction = { workspace = true, features = ["std"] }
solana-pubkey = { workspace = true }
solana-pubkey = { workspace = true, features = ["bytemuck"] }
solana-sdk-ids = { workspace = true }
thiserror = { workspace = true }

Expand Down
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
60 changes: 28 additions & 32 deletions zk-sdk/src/encryption/elgamal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +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_seed_derivable::SeedDerivable,
solana_seed_phrase::generate_seed_from_seed_phrase_and_passphrase,
solana_signature::Signature,
solana_signer::{EncodableKey, EncodableKeypair, 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 @@ -58,6 +44,20 @@ use {
subtle::{Choice, ConstantTimeEq},
zeroize::Zeroize,
};
#[cfg(not(target_arch = "wasm32"))]
use {
sha3::Digest,
solana_derivation_path::DerivationPath,
solana_seed_derivable::SeedDerivable,
solana_seed_phrase::generate_seed_from_seed_phrase_and_passphrase,
solana_signature::Signature,
solana_signer::{EncodableKey, EncodableKeypair, Signer, SignerError},
std::{
error,
io::{Read, Write},
path::Path,
},
};

/// Algorithm handle for the twisted ElGamal encryption scheme
pub struct ElGamal;
Expand Down Expand Up @@ -123,7 +123,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 @@ -136,7 +135,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 @@ -464,6 +462,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 @@ -514,19 +525,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 @@ -663,7 +661,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 @@ -673,7 +670,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 8b720f7

Please sign in to comment.