Files
solana/zk-token-sdk/src/zk_token_elgamal/decryption.rs
samkim-crypto d2b23da9ea Zk token sdk clean decryption (#23478)
* zk-token-sdk: add decryption for pod elgamal ciphertexts

* zk-token-sdk: add decryption for pod elgamal ciphertexts

* zk-token-sdk: cargo fmt

* zk-token-sdk: minor update to docs

* zk-token-sdk: minor

* zk-token-sdk: fix bpf build error

* zk-token-sdk: more simplifying discrete log

* zk-token-sdk: fmt

* zk-token-sdk: minor update to doc
2022-03-04 15:57:19 -04:00

36 lines
1.1 KiB
Rust

#[cfg(not(target_arch = "bpf"))]
use crate::{
encryption::elgamal::{ElGamalCiphertext, ElGamalSecretKey},
zk_token_elgamal::pod,
};
#[cfg(not(target_arch = "bpf"))]
impl pod::ElGamalCiphertext {
pub fn decrypt(self, secret_key: &ElGamalSecretKey) -> Option<u64> {
let deserialized_ciphertext: Option<ElGamalCiphertext> = self.try_into().ok();
if let Some(ciphertext) = deserialized_ciphertext {
ciphertext.decrypt_u32(secret_key)
} else {
None
}
}
}
#[cfg(test)]
mod tests {
use {super::*, crate::encryption::elgamal::ElGamalKeypair};
#[test]
fn test_pod_decryption() {
let keypair = ElGamalKeypair::new_rand();
let pod_ciphertext = pod::ElGamalCiphertext([0u8; 64]);
assert_eq!(pod_ciphertext.decrypt(&keypair.secret).unwrap(), 0);
let amount = 55_u64;
let ciphertext = keypair.public.encrypt(amount);
let pod_ciphertext: pod::ElGamalCiphertext = ciphertext.into();
assert_eq!(pod_ciphertext.decrypt(&keypair.secret).unwrap(), 55);
}
}