Skip to content

Commit

Permalink
Move wasm impl into PodElGamalPubkey
Browse files Browse the repository at this point in the history
  • Loading branch information
joncinque authored and samkim-crypto committed Oct 15, 2024
1 parent 70eb58a commit 03e76ef
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 92 deletions.
2 changes: 0 additions & 2 deletions zk-sdk/src/encryption/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ pub mod grouped_elgamal;
#[cfg(not(target_os = "solana"))]
pub mod pedersen;
pub mod pod;
#[cfg(all(not(target_os = "solana"), target_arch = "wasm32"))]
pub mod wasm;

/// Byte length of an authenticated encryption secret key
pub const AE_KEY_LEN: usize = 16;
Expand Down
74 changes: 74 additions & 0 deletions zk-sdk/src/encryption/pod/elgamal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ use {
bytemuck::Zeroable,
std::fmt,
};
#[cfg(target_arch = "wasm32")]
use {
js_sys::{Array, Uint8Array},
wasm_bindgen::prelude::*,
};

/// Maximum length of a base64 encoded ElGamal public key
const ELGAMAL_PUBKEY_MAX_BASE64_LEN: usize = 44;
Expand Down Expand Up @@ -80,8 +85,77 @@ impl TryFrom<PodElGamalCiphertext> for ElGamalCiphertext {
/// The `ElGamalPubkey` type as a `Pod`.
#[derive(Clone, Copy, Default, bytemuck_derive::Pod, bytemuck_derive::Zeroable, PartialEq, Eq)]
#[repr(transparent)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
pub struct PodElGamalPubkey(pub(crate) [u8; ELGAMAL_PUBKEY_LEN]);

#[cfg(target_arch = "wasm32")]
#[allow(non_snake_case)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
impl PodElGamalPubkey {
/// Create a new `PodElGamalPubkey` object
///
/// * `value` - optional public key as a base64 encoded string, `Uint8Array`, `[number]`
#[wasm_bindgen(constructor)]
pub fn constructor(value: JsValue) -> Result<PodElGamalPubkey, JsValue> {
if let Some(base64_str) = value.as_string() {
base64_str
.parse::<PodElGamalPubkey>()
.map_err(|e| e.to_string().into())
} else if let Some(uint8_array) = value.dyn_ref::<Uint8Array>() {
bytemuck::try_from_bytes(&uint8_array.to_vec())
.map_err(|err| JsValue::from(format!("Invalid Uint8Array ElGamalPubkey: {err:?}")))
.map(|pubkey| *pubkey)
} else if let Some(array) = value.dyn_ref::<Array>() {
let mut bytes = vec![];
let iterator = js_sys::try_iter(&array.values())?.expect("array to be iterable");
for x in iterator {
let x = x?;

if let Some(n) = x.as_f64() {
if (0. ..=255.).contains(&n) {
bytes.push(n as u8);
continue;
}
}
return Err(format!("Invalid array argument: {:?}", x).into());
}

bytemuck::try_from_bytes(&bytes)
.map_err(|err| JsValue::from(format!("Invalid Array pubkey: {err:?}")))
.map(|pubkey| *pubkey)
} else if value.is_undefined() {
Ok(PodElGamalPubkey::default())
} else {
Err("Unsupported argument".into())
}
}

/// Return the base64 string representation of the public key
pub fn toString(&self) -> String {
self.to_string()
}

/// Checks if two `ElGamalPubkey`s are equal
pub fn equals(&self, other: &PodElGamalPubkey) -> bool {
self == other
}

/// Return the `Uint8Array` representation of the public key
pub fn toBytes(&self) -> Box<[u8]> {
self.0.into()
}

pub fn compressed(decoded: &ElGamalPubkey) -> PodElGamalPubkey {
(*decoded).into()
}

pub fn decompressed(&self) -> Result<ElGamalPubkey, JsValue> {
(*self)
.try_into()
.map_err(|err| JsValue::from(format!("Invalid ElGamalPubkey: {err:?}")))
}
}

impl fmt::Debug for PodElGamalPubkey {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self.0)
Expand Down
82 changes: 0 additions & 82 deletions zk-sdk/src/encryption/wasm/elgamal.rs

This file was deleted.

1 change: 0 additions & 1 deletion zk-sdk/src/encryption/wasm/mod.rs

This file was deleted.

2 changes: 0 additions & 2 deletions zk-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ pub mod pod;
mod range_proof;
mod sigma_proofs;
mod transcript;
#[cfg(all(not(target_os = "solana"), target_arch = "wasm32"))]
pub mod wasm;
#[cfg(not(target_arch = "wasm32"))]
pub mod zk_elgamal_proof_program;

Expand Down
5 changes: 0 additions & 5 deletions zk-sdk/src/wasm.rs

This file was deleted.

0 comments on commit 03e76ef

Please sign in to comment.