[zk-token-sdk] Refactor `zk-token-elgamal` conversion code for `elgamal` and `pedersen` pod types (#31846)

* refactor `DecryptHandle` into `pod::elgamal` from `pod::pedersen`

* refactor conversion for elgamal

* refactor conversion for pedersen

* change variable names

* add brief description of the types

* fix variable names in comments
This commit is contained in:
samkim-crypto 2023-05-29 12:32:53 +09:00 committed by GitHub
parent 24a4563484
commit f7b34e46ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 108 additions and 89 deletions

View File

@ -52,11 +52,7 @@ mod target_arch {
super::pod,
crate::{
curve25519::scalar::PodScalar,
encryption::{
auth_encryption::AeCiphertext,
elgamal::{DecryptHandle, ElGamalCiphertext, ElGamalPubkey},
pedersen::PedersenCommitment,
},
encryption::auth_encryption::AeCiphertext,
errors::{ProofError, ProofVerificationError},
instruction::{
transfer::{TransferAmountEncryption, TransferPubkeys},
@ -91,34 +87,6 @@ mod target_arch {
}
}
impl From<ElGamalCiphertext> for pod::ElGamalCiphertext {
fn from(ct: ElGamalCiphertext) -> Self {
Self(ct.to_bytes())
}
}
impl TryFrom<pod::ElGamalCiphertext> for ElGamalCiphertext {
type Error = ProofError;
fn try_from(ct: pod::ElGamalCiphertext) -> Result<Self, Self::Error> {
Self::from_bytes(&ct.0).ok_or(ProofError::CiphertextDeserialization)
}
}
impl From<ElGamalPubkey> for pod::ElGamalPubkey {
fn from(pk: ElGamalPubkey) -> Self {
Self(pk.to_bytes())
}
}
impl TryFrom<pod::ElGamalPubkey> for ElGamalPubkey {
type Error = ProofError;
fn try_from(pk: pod::ElGamalPubkey) -> Result<Self, Self::Error> {
Self::from_bytes(&pk.0).ok_or(ProofError::CiphertextDeserialization)
}
}
impl From<CompressedRistretto> for pod::CompressedRistretto {
fn from(cr: CompressedRistretto) -> Self {
Self(cr.to_bytes())
@ -131,53 +99,6 @@ mod target_arch {
}
}
impl From<PedersenCommitment> for pod::PedersenCommitment {
fn from(comm: PedersenCommitment) -> Self {
Self(comm.to_bytes())
}
}
// For proof verification, interpret pod::PedersenComm directly as CompressedRistretto
#[cfg(not(target_os = "solana"))]
impl From<pod::PedersenCommitment> for CompressedRistretto {
fn from(pod: pod::PedersenCommitment) -> Self {
Self(pod.0)
}
}
#[cfg(not(target_os = "solana"))]
impl TryFrom<pod::PedersenCommitment> for PedersenCommitment {
type Error = ProofError;
fn try_from(pod: pod::PedersenCommitment) -> Result<Self, Self::Error> {
Self::from_bytes(&pod.0).ok_or(ProofError::CiphertextDeserialization)
}
}
#[cfg(not(target_os = "solana"))]
impl From<DecryptHandle> for pod::DecryptHandle {
fn from(handle: DecryptHandle) -> Self {
Self(handle.to_bytes())
}
}
// For proof verification, interpret pod::PedersenDecHandle as CompressedRistretto
#[cfg(not(target_os = "solana"))]
impl From<pod::DecryptHandle> for CompressedRistretto {
fn from(pod: pod::DecryptHandle) -> Self {
Self(pod.0)
}
}
#[cfg(not(target_os = "solana"))]
impl TryFrom<pod::DecryptHandle> for DecryptHandle {
type Error = ProofError;
fn try_from(pod: pod::DecryptHandle) -> Result<Self, Self::Error> {
Self::from_bytes(&pod.0).ok_or(ProofError::CiphertextDeserialization)
}
}
impl From<AeCiphertext> for pod::AeCiphertext {
fn from(ct: AeCiphertext) -> Self {
Self(ct.to_bytes())

View File

@ -1,9 +1,17 @@
//! Plain Old Data types for the ElGamal encryption scheme.
use {
crate::zk_token_elgamal::pod::{Pod, Zeroable},
base64::{prelude::BASE64_STANDARD, Engine},
std::fmt,
};
#[cfg(not(target_os = "solana"))]
use {
crate::{encryption::elgamal as decoded, errors::ProofError},
curve25519_dalek::ristretto::CompressedRistretto,
};
/// The `ElGamalCiphertext` type as a `Pod`.
#[derive(Clone, Copy, Pod, Zeroable, PartialEq, Eq)]
#[repr(transparent)]
pub struct ElGamalCiphertext(pub [u8; 64]);
@ -26,6 +34,23 @@ impl Default for ElGamalCiphertext {
}
}
#[cfg(not(target_os = "solana"))]
impl From<decoded::ElGamalCiphertext> for ElGamalCiphertext {
fn from(decoded_ciphertext: decoded::ElGamalCiphertext) -> Self {
Self(decoded_ciphertext.to_bytes())
}
}
#[cfg(not(target_os = "solana"))]
impl TryFrom<ElGamalCiphertext> for decoded::ElGamalCiphertext {
type Error = ProofError;
fn try_from(pod_ciphertext: ElGamalCiphertext) -> Result<Self, Self::Error> {
Self::from_bytes(&pod_ciphertext.0).ok_or(ProofError::CiphertextDeserialization)
}
}
/// The `ElGamalPubkey` type as a `Pod`.
#[derive(Clone, Copy, Default, Pod, Zeroable, PartialEq, Eq)]
#[repr(transparent)]
pub struct ElGamalPubkey(pub [u8; 32]);
@ -41,3 +66,54 @@ impl fmt::Display for ElGamalPubkey {
write!(f, "{}", BASE64_STANDARD.encode(self.0))
}
}
#[cfg(not(target_os = "solana"))]
impl From<decoded::ElGamalPubkey> for ElGamalPubkey {
fn from(decoded_pubkey: decoded::ElGamalPubkey) -> Self {
Self(decoded_pubkey.to_bytes())
}
}
#[cfg(not(target_os = "solana"))]
impl TryFrom<ElGamalPubkey> for decoded::ElGamalPubkey {
type Error = ProofError;
fn try_from(pod_pubkey: ElGamalPubkey) -> Result<Self, Self::Error> {
Self::from_bytes(&pod_pubkey.0).ok_or(ProofError::CiphertextDeserialization)
}
}
/// The `DecryptHandle` type as a `Pod`.
#[derive(Clone, Copy, Default, Pod, Zeroable, PartialEq, Eq)]
#[repr(transparent)]
pub struct DecryptHandle(pub [u8; 32]);
impl fmt::Debug for DecryptHandle {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self.0)
}
}
#[cfg(not(target_os = "solana"))]
impl From<decoded::DecryptHandle> for DecryptHandle {
fn from(decoded_handle: decoded::DecryptHandle) -> Self {
Self(decoded_handle.to_bytes())
}
}
// For proof verification, interpret pod::DecryptHandle as CompressedRistretto
#[cfg(not(target_os = "solana"))]
impl From<DecryptHandle> for CompressedRistretto {
fn from(pod_handle: DecryptHandle) -> Self {
Self(pod_handle.0)
}
}
#[cfg(not(target_os = "solana"))]
impl TryFrom<DecryptHandle> for decoded::DecryptHandle {
type Error = ProofError;
fn try_from(pod_handle: DecryptHandle) -> Result<Self, Self::Error> {
Self::from_bytes(&pod_handle.0).ok_or(ProofError::CiphertextDeserialization)
}
}

View File

@ -13,12 +13,12 @@ use {
pub use {
auth_encryption::AeCiphertext,
bytemuck::{Pod, Zeroable},
elgamal::{ElGamalCiphertext, ElGamalPubkey},
elgamal::{DecryptHandle, ElGamalCiphertext, ElGamalPubkey},
instruction::{
FeeEncryption, FeeParameters, TransferAmountEncryption, TransferPubkeys,
TransferWithFeePubkeys,
},
pedersen::{DecryptHandle, PedersenCommitment},
pedersen::PedersenCommitment,
range_proof::{RangeProof128, RangeProof256, RangeProof64},
sigma_proofs::{
AggregatedValidityProof, CiphertextCiphertextEqualityProof,

View File

@ -1,8 +1,16 @@
//! Plain Old Data type for the Pedersen commitment scheme.
use {
crate::zk_token_elgamal::pod::{Pod, Zeroable},
std::fmt,
};
#[cfg(not(target_os = "solana"))]
use {
crate::{encryption::pedersen as decoded, errors::ProofError},
curve25519_dalek::ristretto::CompressedRistretto,
};
/// The `PedersenCommitment` type as a `Pod`.
#[derive(Clone, Copy, Default, Pod, Zeroable, PartialEq, Eq)]
#[repr(transparent)]
pub struct PedersenCommitment(pub [u8; 32]);
@ -13,12 +21,26 @@ impl fmt::Debug for PedersenCommitment {
}
}
#[derive(Clone, Copy, Default, Pod, Zeroable, PartialEq, Eq)]
#[repr(transparent)]
pub struct DecryptHandle(pub [u8; 32]);
impl fmt::Debug for DecryptHandle {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self.0)
#[cfg(not(target_os = "solana"))]
impl From<decoded::PedersenCommitment> for PedersenCommitment {
fn from(decoded_commitment: decoded::PedersenCommitment) -> Self {
Self(decoded_commitment.to_bytes())
}
}
// For proof verification, interpret pod::PedersenCommitment directly as CompressedRistretto
#[cfg(not(target_os = "solana"))]
impl From<PedersenCommitment> for CompressedRistretto {
fn from(pod_commitment: PedersenCommitment) -> Self {
Self(pod_commitment.0)
}
}
#[cfg(not(target_os = "solana"))]
impl TryFrom<PedersenCommitment> for decoded::PedersenCommitment {
type Error = ProofError;
fn try_from(pod_commitment: PedersenCommitment) -> Result<Self, Self::Error> {
Self::from_bytes(&pod_commitment.0).ok_or(ProofError::CiphertextDeserialization)
}
}