use crate::arithmetic::wasm::group_elements::WASMGroupElement;
use crate::arithmetic::wasm::scalars::WASMScalarNonZero;
use crate::core::elgamal::{decrypt, encrypt, ElGamal};
use derive_more::{Deref, From, Into};
use wasm_bindgen::prelude::*;
#[derive(Copy, Clone, Eq, PartialEq, Debug, From, Into, Deref)]
#[wasm_bindgen(js_name = ElGamal)]
pub struct WASMElGamal(pub(crate) ElGamal);
#[wasm_bindgen(js_class = "ElGamal")]
impl WASMElGamal {
#[wasm_bindgen(js_name = toBytes)]
pub fn to_bytes(&self) -> Vec<u8> {
self.0.to_bytes().to_vec()
}
#[wasm_bindgen(js_name = fromBytes)]
pub fn from_bytes(v: Vec<u8>) -> Option<WASMElGamal> {
ElGamal::from_slice(v.as_slice()).map(WASMElGamal)
}
#[wasm_bindgen(js_name = toBase64)]
pub fn to_base64(self) -> String {
self.0.to_base64()
}
#[wasm_bindgen(js_name = fromBase64)]
pub fn from_base64(s: &str) -> Option<WASMElGamal> {
ElGamal::from_base64(s).map(WASMElGamal)
}
}
#[wasm_bindgen(js_name = encrypt)]
pub fn encrypt_wasm(gm: &WASMGroupElement, gy: &WASMGroupElement) -> WASMElGamal {
let mut rng = rand::rng();
encrypt(gm, gy, &mut rng).into()
}
#[wasm_bindgen(js_name = decrypt)]
#[cfg(feature = "elgamal3")]
pub fn decrypt_wasm(encrypted: &WASMElGamal, y: &WASMScalarNonZero) -> Option<WASMGroupElement> {
decrypt(encrypted, y).map(|x| x.into())
}
#[wasm_bindgen(js_name = decrypt)]
#[cfg(not(feature = "elgamal3"))]
pub fn decrypt_wasm(encrypted: &WASMElGamal, y: &WASMScalarNonZero) -> WASMGroupElement {
decrypt(encrypted, y).into()
}