diff --git a/zk-token-sdk/src/zk_token_elgamal/convert.rs b/zk-token-sdk/src/zk_token_elgamal/convert.rs index 5dee47cabb2069..d171a873d03e79 100644 --- a/zk-token-sdk/src/zk_token_elgamal/convert.rs +++ b/zk-token-sdk/src/zk_token_elgamal/convert.rs @@ -57,15 +57,6 @@ mod target_arch { transfer::{TransferAmountEncryption, TransferPubkeys}, transfer_with_fee::{FeeEncryption, FeeParameters, TransferWithFeePubkeys}, }, - sigma_proofs::{ - ciphertext_ciphertext_equality_proof::CiphertextCiphertextEqualityProof, - ciphertext_commitment_equality_proof::CiphertextCommitmentEqualityProof, - errors::*, - fee_proof::FeeSigmaProof, - pubkey_proof::PubkeyValidityProof, - validity_proof::{AggregatedValidityProof, ValidityProof}, - zero_balance_proof::ZeroBalanceProof, - }, }, curve25519_dalek::{ristretto::CompressedRistretto, scalar::Scalar}, std::convert::TryFrom, @@ -97,104 +88,6 @@ mod target_arch { } } - impl From for pod::CiphertextCommitmentEqualityProof { - fn from(proof: CiphertextCommitmentEqualityProof) -> Self { - Self(proof.to_bytes()) - } - } - - impl TryFrom for CiphertextCommitmentEqualityProof { - type Error = EqualityProofError; - - fn try_from(pod: pod::CiphertextCommitmentEqualityProof) -> Result { - Self::from_bytes(&pod.0) - } - } - - impl From for pod::CiphertextCiphertextEqualityProof { - fn from(proof: CiphertextCiphertextEqualityProof) -> Self { - Self(proof.to_bytes()) - } - } - - impl TryFrom for CiphertextCiphertextEqualityProof { - type Error = EqualityProofError; - - fn try_from(pod: pod::CiphertextCiphertextEqualityProof) -> Result { - Self::from_bytes(&pod.0) - } - } - - impl From for pod::ValidityProof { - fn from(proof: ValidityProof) -> Self { - Self(proof.to_bytes()) - } - } - - impl TryFrom for ValidityProof { - type Error = ValidityProofError; - - fn try_from(pod: pod::ValidityProof) -> Result { - Self::from_bytes(&pod.0) - } - } - - impl From for pod::AggregatedValidityProof { - fn from(proof: AggregatedValidityProof) -> Self { - Self(proof.to_bytes()) - } - } - - impl TryFrom for AggregatedValidityProof { - type Error = ValidityProofError; - - fn try_from(pod: pod::AggregatedValidityProof) -> Result { - Self::from_bytes(&pod.0) - } - } - - impl From for pod::ZeroBalanceProof { - fn from(proof: ZeroBalanceProof) -> Self { - Self(proof.to_bytes()) - } - } - - impl TryFrom for ZeroBalanceProof { - type Error = ZeroBalanceProofError; - - fn try_from(pod: pod::ZeroBalanceProof) -> Result { - Self::from_bytes(&pod.0) - } - } - - impl From for pod::FeeSigmaProof { - fn from(proof: FeeSigmaProof) -> Self { - Self(proof.to_bytes()) - } - } - - impl TryFrom for FeeSigmaProof { - type Error = FeeSigmaProofError; - - fn try_from(pod: pod::FeeSigmaProof) -> Result { - Self::from_bytes(&pod.0) - } - } - - impl From for pod::PubkeyValidityProof { - fn from(proof: PubkeyValidityProof) -> Self { - Self(proof.to_bytes()) - } - } - - impl TryFrom for PubkeyValidityProof { - type Error = PubkeyValidityProofError; - - fn try_from(pod: pod::PubkeyValidityProof) -> Result { - Self::from_bytes(&pod.0) - } - } - impl From for pod::TransferPubkeys { fn from(keys: TransferPubkeys) -> Self { Self { diff --git a/zk-token-sdk/src/zk_token_elgamal/pod/sigma_proofs.rs b/zk-token-sdk/src/zk_token_elgamal/pod/sigma_proofs.rs index 226b7f90ca5ea2..96da77a5d97bcb 100644 --- a/zk-token-sdk/src/zk_token_elgamal/pod/sigma_proofs.rs +++ b/zk-token-sdk/src/zk_token_elgamal/pod/sigma_proofs.rs @@ -1,61 +1,181 @@ +//! Plain Old Data types for sigma proofs. + +#[cfg(not(target_os = "solana"))] +use crate::sigma_proofs::{ + ciphertext_ciphertext_equality_proof::CiphertextCiphertextEqualityProof as DecodedCiphertextCiphertextEqualityProof, + ciphertext_commitment_equality_proof::CiphertextCommitmentEqualityProof as DecodedCiphertextCommitmentEqualityProof, + errors::*, + fee_proof::FeeSigmaProof as DecodedFeeSigmaProof, + pubkey_proof::PubkeyValidityProof as DecodedPubkeyValidityProof, + validity_proof::{ + AggregatedValidityProof as DecodedAggregatedValidityProof, + ValidityProof as DecodedValidityProof, + }, + zero_balance_proof::ZeroBalanceProof as DecodedZeroBalanceProof, +}; use crate::zk_token_elgamal::pod::{Pod, Zeroable}; -/// Serialization of `CiphertextCommitmentEqualityProof` +/// The `CiphertextCommitmentEqualityProof` type as a `Pod`. #[derive(Clone, Copy)] #[repr(transparent)] pub struct CiphertextCommitmentEqualityProof(pub [u8; 192]); -// `CiphertextCommitmentEqualityProof` is a Pod and Zeroable. -// Add the marker traits manually because `bytemuck` only adds them for some `u8` arrays -unsafe impl Zeroable for CiphertextCommitmentEqualityProof {} -unsafe impl Pod for CiphertextCommitmentEqualityProof {} +#[cfg(not(target_os = "solana"))] +impl From for CiphertextCommitmentEqualityProof { + fn from(decoded_proof: DecodedCiphertextCommitmentEqualityProof) -> Self { + Self(decoded_proof.to_bytes()) + } +} -/// Serialization of `CiphertextCiphertextEqualityProof` +#[cfg(not(target_os = "solana"))] +impl TryFrom for DecodedCiphertextCommitmentEqualityProof { + type Error = EqualityProofError; + + fn try_from(pod_proof: CiphertextCommitmentEqualityProof) -> Result { + Self::from_bytes(&pod_proof.0) + } +} + +/// The `CiphertextCiphertextEqualityProof` type as a `Pod`. #[derive(Clone, Copy)] #[repr(transparent)] pub struct CiphertextCiphertextEqualityProof(pub [u8; 224]); -// `CiphertextCiphertextEqualityProof` is a Pod and Zeroable. -// Add the marker traits manually because `bytemuck` only adds them for some `u8` arrays -unsafe impl Zeroable for CiphertextCiphertextEqualityProof {} -unsafe impl Pod for CiphertextCiphertextEqualityProof {} +#[cfg(not(target_os = "solana"))] +impl From for CiphertextCiphertextEqualityProof { + fn from(decoded_proof: DecodedCiphertextCiphertextEqualityProof) -> Self { + Self(decoded_proof.to_bytes()) + } +} + +#[cfg(not(target_os = "solana"))] +impl TryFrom for DecodedCiphertextCiphertextEqualityProof { + type Error = EqualityProofError; + + fn try_from(pod_proof: CiphertextCiphertextEqualityProof) -> Result { + Self::from_bytes(&pod_proof.0) + } +} -/// Serialization of validity proofs +/// The `ValidityProof` type as a `Pod`. #[derive(Clone, Copy)] #[repr(transparent)] pub struct ValidityProof(pub [u8; 160]); -// `ValidityProof` is a Pod and Zeroable. -// Add the marker traits manually because `bytemuck` only adds them for some `u8` arrays -unsafe impl Zeroable for ValidityProof {} -unsafe impl Pod for ValidityProof {} +#[cfg(not(target_os = "solana"))] +impl From for ValidityProof { + fn from(decoded_proof: DecodedValidityProof) -> Self { + Self(decoded_proof.to_bytes()) + } +} -/// Serialization of aggregated validity proofs +#[cfg(not(target_os = "solana"))] +impl TryFrom for DecodedValidityProof { + type Error = ValidityProofError; + + fn try_from(pod_proof: ValidityProof) -> Result { + Self::from_bytes(&pod_proof.0) + } +} + +/// The `AggregatedValidityProof` type as a `Pod`. #[derive(Clone, Copy)] #[repr(transparent)] pub struct AggregatedValidityProof(pub [u8; 160]); -// `AggregatedValidityProof` is a Pod and Zeroable. -// Add the marker traits manually because `bytemuck` only adds them for some `u8` arrays -unsafe impl Zeroable for AggregatedValidityProof {} -unsafe impl Pod for AggregatedValidityProof {} +#[cfg(not(target_os = "solana"))] +impl From for AggregatedValidityProof { + fn from(decoded_proof: DecodedAggregatedValidityProof) -> Self { + Self(decoded_proof.to_bytes()) + } +} + +#[cfg(not(target_os = "solana"))] +impl TryFrom for DecodedAggregatedValidityProof { + type Error = ValidityProofError; -/// Serialization of zero balance proofs + fn try_from(pod_proof: AggregatedValidityProof) -> Result { + Self::from_bytes(&pod_proof.0) + } +} + +/// The `ZeroBalanceProof` type as a `Pod`. #[derive(Clone, Copy)] #[repr(transparent)] pub struct ZeroBalanceProof(pub [u8; 96]); -// `ZeroBalanceProof` is a Pod and Zeroable. -// Add the marker traits manually because `bytemuck` only adds them for some `u8` arrays -unsafe impl Zeroable for ZeroBalanceProof {} -unsafe impl Pod for ZeroBalanceProof {} +#[cfg(not(target_os = "solana"))] +impl From for ZeroBalanceProof { + fn from(decoded_proof: DecodedZeroBalanceProof) -> Self { + Self(decoded_proof.to_bytes()) + } +} + +#[cfg(not(target_os = "solana"))] +impl TryFrom for DecodedZeroBalanceProof { + type Error = ZeroBalanceProofError; + + fn try_from(pod_proof: ZeroBalanceProof) -> Result { + Self::from_bytes(&pod_proof.0) + } +} -/// Serialization of fee sigma proof +/// The `FeeSigmaProof` type as a `Pod`. #[derive(Clone, Copy, Pod, Zeroable)] #[repr(transparent)] pub struct FeeSigmaProof(pub [u8; 256]); -/// Serialization of public-key sigma proof +#[cfg(not(target_os = "solana"))] +impl From for FeeSigmaProof { + fn from(decoded_proof: DecodedFeeSigmaProof) -> Self { + Self(decoded_proof.to_bytes()) + } +} + +#[cfg(not(target_os = "solana"))] +impl TryFrom for DecodedFeeSigmaProof { + type Error = FeeSigmaProofError; + + fn try_from(pod_proof: FeeSigmaProof) -> Result { + Self::from_bytes(&pod_proof.0) + } +} + +/// The `PubkeyValidityProof` type as a `Pod`. #[derive(Clone, Copy, Pod, Zeroable)] #[repr(transparent)] pub struct PubkeyValidityProof(pub [u8; 64]); + +#[cfg(not(target_os = "solana"))] +impl From for PubkeyValidityProof { + fn from(decoded_proof: DecodedPubkeyValidityProof) -> Self { + Self(decoded_proof.to_bytes()) + } +} + +#[cfg(not(target_os = "solana"))] +impl TryFrom for DecodedPubkeyValidityProof { + type Error = PubkeyValidityProofError; + + fn try_from(pod_proof: PubkeyValidityProof) -> Result { + Self::from_bytes(&pod_proof.0) + } +} + +// The sigma proof pod types are byte array wrappers, which are both `Pod` and `Zeroable`. However, +// the marker traits `bytemuck::Pod` and `bytemuck::Zeroable` can only be derived for power-of-two +// length byte arrays. Directly implement these traits for the sigma proof pod types. +unsafe impl Zeroable for CiphertextCommitmentEqualityProof {} +unsafe impl Pod for CiphertextCommitmentEqualityProof {} + +unsafe impl Zeroable for CiphertextCiphertextEqualityProof {} +unsafe impl Pod for CiphertextCiphertextEqualityProof {} + +unsafe impl Zeroable for ValidityProof {} +unsafe impl Pod for ValidityProof {} + +unsafe impl Zeroable for AggregatedValidityProof {} +unsafe impl Pod for AggregatedValidityProof {} + +unsafe impl Zeroable for ZeroBalanceProof {} +unsafe impl Pod for ZeroBalanceProof {}