Skip to content

Commit

Permalink
refactor convert logic for sigma proof types
Browse files Browse the repository at this point in the history
  • Loading branch information
samkim-crypto committed May 29, 2023
1 parent e0e5ba8 commit 9eb0678
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 134 deletions.
107 changes: 0 additions & 107 deletions zk-token-sdk/src/zk_token_elgamal/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -97,104 +88,6 @@ mod target_arch {
}
}

impl From<CiphertextCommitmentEqualityProof> for pod::CiphertextCommitmentEqualityProof {
fn from(proof: CiphertextCommitmentEqualityProof) -> Self {
Self(proof.to_bytes())
}
}

impl TryFrom<pod::CiphertextCommitmentEqualityProof> for CiphertextCommitmentEqualityProof {
type Error = EqualityProofError;

fn try_from(pod: pod::CiphertextCommitmentEqualityProof) -> Result<Self, Self::Error> {
Self::from_bytes(&pod.0)
}
}

impl From<CiphertextCiphertextEqualityProof> for pod::CiphertextCiphertextEqualityProof {
fn from(proof: CiphertextCiphertextEqualityProof) -> Self {
Self(proof.to_bytes())
}
}

impl TryFrom<pod::CiphertextCiphertextEqualityProof> for CiphertextCiphertextEqualityProof {
type Error = EqualityProofError;

fn try_from(pod: pod::CiphertextCiphertextEqualityProof) -> Result<Self, Self::Error> {
Self::from_bytes(&pod.0)
}
}

impl From<ValidityProof> for pod::ValidityProof {
fn from(proof: ValidityProof) -> Self {
Self(proof.to_bytes())
}
}

impl TryFrom<pod::ValidityProof> for ValidityProof {
type Error = ValidityProofError;

fn try_from(pod: pod::ValidityProof) -> Result<Self, Self::Error> {
Self::from_bytes(&pod.0)
}
}

impl From<AggregatedValidityProof> for pod::AggregatedValidityProof {
fn from(proof: AggregatedValidityProof) -> Self {
Self(proof.to_bytes())
}
}

impl TryFrom<pod::AggregatedValidityProof> for AggregatedValidityProof {
type Error = ValidityProofError;

fn try_from(pod: pod::AggregatedValidityProof) -> Result<Self, Self::Error> {
Self::from_bytes(&pod.0)
}
}

impl From<ZeroBalanceProof> for pod::ZeroBalanceProof {
fn from(proof: ZeroBalanceProof) -> Self {
Self(proof.to_bytes())
}
}

impl TryFrom<pod::ZeroBalanceProof> for ZeroBalanceProof {
type Error = ZeroBalanceProofError;

fn try_from(pod: pod::ZeroBalanceProof) -> Result<Self, Self::Error> {
Self::from_bytes(&pod.0)
}
}

impl From<FeeSigmaProof> for pod::FeeSigmaProof {
fn from(proof: FeeSigmaProof) -> Self {
Self(proof.to_bytes())
}
}

impl TryFrom<pod::FeeSigmaProof> for FeeSigmaProof {
type Error = FeeSigmaProofError;

fn try_from(pod: pod::FeeSigmaProof) -> Result<Self, Self::Error> {
Self::from_bytes(&pod.0)
}
}

impl From<PubkeyValidityProof> for pod::PubkeyValidityProof {
fn from(proof: PubkeyValidityProof) -> Self {
Self(proof.to_bytes())
}
}

impl TryFrom<pod::PubkeyValidityProof> for PubkeyValidityProof {
type Error = PubkeyValidityProofError;

fn try_from(pod: pod::PubkeyValidityProof) -> Result<Self, Self::Error> {
Self::from_bytes(&pod.0)
}
}

impl From<TransferPubkeys> for pod::TransferPubkeys {
fn from(keys: TransferPubkeys) -> Self {
Self {
Expand Down
174 changes: 147 additions & 27 deletions zk-token-sdk/src/zk_token_elgamal/pod/sigma_proofs.rs
Original file line number Diff line number Diff line change
@@ -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<DecodedCiphertextCommitmentEqualityProof> for CiphertextCommitmentEqualityProof {
fn from(decoded_proof: DecodedCiphertextCommitmentEqualityProof) -> Self {
Self(decoded_proof.to_bytes())
}
}

/// Serialization of `CiphertextCiphertextEqualityProof`
#[cfg(not(target_os = "solana"))]
impl TryFrom<CiphertextCommitmentEqualityProof> for DecodedCiphertextCommitmentEqualityProof {
type Error = EqualityProofError;

fn try_from(pod_proof: CiphertextCommitmentEqualityProof) -> Result<Self, Self::Error> {
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<DecodedCiphertextCiphertextEqualityProof> for CiphertextCiphertextEqualityProof {
fn from(decoded_proof: DecodedCiphertextCiphertextEqualityProof) -> Self {
Self(decoded_proof.to_bytes())
}
}

#[cfg(not(target_os = "solana"))]
impl TryFrom<CiphertextCiphertextEqualityProof> for DecodedCiphertextCiphertextEqualityProof {
type Error = EqualityProofError;

fn try_from(pod_proof: CiphertextCiphertextEqualityProof) -> Result<Self, Self::Error> {
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<DecodedValidityProof> 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<ValidityProof> for DecodedValidityProof {
type Error = ValidityProofError;

fn try_from(pod_proof: ValidityProof) -> Result<Self, Self::Error> {
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<DecodedAggregatedValidityProof> for AggregatedValidityProof {
fn from(decoded_proof: DecodedAggregatedValidityProof) -> Self {
Self(decoded_proof.to_bytes())
}
}

#[cfg(not(target_os = "solana"))]
impl TryFrom<AggregatedValidityProof> for DecodedAggregatedValidityProof {
type Error = ValidityProofError;

/// Serialization of zero balance proofs
fn try_from(pod_proof: AggregatedValidityProof) -> Result<Self, Self::Error> {
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<DecodedZeroBalanceProof> for ZeroBalanceProof {
fn from(decoded_proof: DecodedZeroBalanceProof) -> Self {
Self(decoded_proof.to_bytes())
}
}

#[cfg(not(target_os = "solana"))]
impl TryFrom<ZeroBalanceProof> for DecodedZeroBalanceProof {
type Error = ZeroBalanceProofError;

fn try_from(pod_proof: ZeroBalanceProof) -> Result<Self, Self::Error> {
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<DecodedFeeSigmaProof> for FeeSigmaProof {
fn from(decoded_proof: DecodedFeeSigmaProof) -> Self {
Self(decoded_proof.to_bytes())
}
}

#[cfg(not(target_os = "solana"))]
impl TryFrom<FeeSigmaProof> for DecodedFeeSigmaProof {
type Error = FeeSigmaProofError;

fn try_from(pod_proof: FeeSigmaProof) -> Result<Self, Self::Error> {
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<DecodedPubkeyValidityProof> for PubkeyValidityProof {
fn from(decoded_proof: DecodedPubkeyValidityProof) -> Self {
Self(decoded_proof.to_bytes())
}
}

#[cfg(not(target_os = "solana"))]
impl TryFrom<PubkeyValidityProof> for DecodedPubkeyValidityProof {
type Error = PubkeyValidityProofError;

fn try_from(pod_proof: PubkeyValidityProof) -> Result<Self, Self::Error> {
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 {}

0 comments on commit 9eb0678

Please sign in to comment.