Skip to content

Commit

Permalink
impl Display, FromStr, From<[u8; _]> for sigma and range proof …
Browse files Browse the repository at this point in the history
…pod types
  • Loading branch information
samkim-crypto committed Sep 3, 2024
1 parent 6601d37 commit 56ba5a5
Show file tree
Hide file tree
Showing 3 changed files with 234 additions and 5 deletions.
6 changes: 3 additions & 3 deletions zk-sdk/src/range_proof/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,21 @@ pub const INNER_PRODUCT_PROOF_U64_LEN: usize = 448;

/// Byte length of a range proof for an unsigned 64-bit number
pub const RANGE_PROOF_U64_LEN: usize =
INNER_PRODUCT_PROOF_U64_LEN + RANGE_PROOF_MODULO_INNER_PRODUCT_PROOF_LEN;
INNER_PRODUCT_PROOF_U64_LEN + RANGE_PROOF_MODULO_INNER_PRODUCT_PROOF_LEN; // 672 bytes

/// Byte length of an inner-product proof for a vector of length 128
pub const INNER_PRODUCT_PROOF_U128_LEN: usize = 512;

/// Byte length of a range proof for an unsigned 128-bit number
pub const RANGE_PROOF_U128_LEN: usize =
INNER_PRODUCT_PROOF_U128_LEN + RANGE_PROOF_MODULO_INNER_PRODUCT_PROOF_LEN;
INNER_PRODUCT_PROOF_U128_LEN + RANGE_PROOF_MODULO_INNER_PRODUCT_PROOF_LEN; // 736 bytes

/// Byte length of an inner-product proof for a vector of length 256
pub const INNER_PRODUCT_PROOF_U256_LEN: usize = 576;

/// Byte length of a range proof for an unsigned 256-bit number
pub const RANGE_PROOF_U256_LEN: usize =
INNER_PRODUCT_PROOF_U256_LEN + RANGE_PROOF_MODULO_INNER_PRODUCT_PROOF_LEN;
INNER_PRODUCT_PROOF_U256_LEN + RANGE_PROOF_MODULO_INNER_PRODUCT_PROOF_LEN; // 800 bytes

#[allow(non_snake_case)]
#[cfg(not(target_os = "solana"))]
Expand Down
55 changes: 54 additions & 1 deletion zk-sdk/src/range_proof/pod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ use crate::{
UNIT_LEN,
};
use {
crate::range_proof::*,
crate::{
pod::{impl_from_bytes, impl_from_str},
range_proof::*,
},
base64::{prelude::BASE64_STANDARD, Engine},
bytemuck::{Pod, Zeroable},
std::fmt,
};

/// The `RangeProof` type as a `Pod` restricted to proofs on 64-bit numbers.
Expand Down Expand Up @@ -41,6 +46,22 @@ impl TryFrom<PodRangeProofU64> for RangeProof {
}
}

const RANGE_PROOF_U64_MAX_BASE64_LEN: usize = 896;

impl fmt::Display for PodRangeProofU64 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", BASE64_STANDARD.encode(self.0))
}
}

impl_from_str!(
TYPE = PodRangeProofU64,
BYTES_LEN = RANGE_PROOF_U64_LEN,
BASE64_LEN = RANGE_PROOF_U64_MAX_BASE64_LEN
);

impl_from_bytes!(TYPE = PodRangeProofU64, BYTES_LEN = RANGE_PROOF_U64_LEN);

/// The `RangeProof` type as a `Pod` restricted to proofs on 128-bit numbers.
#[derive(Clone, Copy)]
#[repr(transparent)]
Expand Down Expand Up @@ -72,6 +93,22 @@ impl TryFrom<PodRangeProofU128> for RangeProof {
}
}

const RANGE_PROOF_U128_MAX_BASE64_LEN: usize = 984;

impl fmt::Display for PodRangeProofU128 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", BASE64_STANDARD.encode(self.0))
}
}

impl_from_str!(
TYPE = PodRangeProofU128,
BYTES_LEN = RANGE_PROOF_U128_LEN,
BASE64_LEN = RANGE_PROOF_U128_MAX_BASE64_LEN
);

impl_from_bytes!(TYPE = PodRangeProofU128, BYTES_LEN = RANGE_PROOF_U128_LEN);

/// The `RangeProof` type as a `Pod` restricted to proofs on 256-bit numbers.
#[derive(Clone, Copy)]
#[repr(transparent)]
Expand Down Expand Up @@ -103,6 +140,22 @@ impl TryFrom<PodRangeProofU256> for RangeProof {
}
}

const RANGE_PROOF_U256_MAX_BASE64_LEN: usize = 1068;

impl fmt::Display for PodRangeProofU256 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", BASE64_STANDARD.encode(self.0))
}
}

impl_from_str!(
TYPE = PodRangeProofU256,
BYTES_LEN = RANGE_PROOF_U256_LEN,
BASE64_LEN = RANGE_PROOF_U256_MAX_BASE64_LEN
);

impl_from_bytes!(TYPE = PodRangeProofU256, BYTES_LEN = RANGE_PROOF_U256_LEN);

#[cfg(not(target_os = "solana"))]
fn copy_range_proof_modulo_inner_product_proof(proof: &RangeProof, buf: &mut [u8]) {
let mut chunks = buf.chunks_mut(UNIT_LEN);
Expand Down
178 changes: 177 additions & 1 deletion zk-sdk/src/sigma_proofs/pod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ use crate::sigma_proofs::{
zero_ciphertext::ZeroCiphertextProof,
};
use {
crate::sigma_proofs::{errors::*, *},
crate::{
pod::{impl_from_bytes, impl_from_str},
sigma_proofs::{errors::*, *},
},
base64::{prelude::BASE64_STANDARD, Engine},
bytemuck::{Pod, Zeroable},
std::fmt,
};

/// The `CiphertextCommitmentEqualityProof` type as a `Pod`.
Expand All @@ -43,6 +48,25 @@ impl TryFrom<PodCiphertextCommitmentEqualityProof> for CiphertextCommitmentEqual
}
}

const CIPHERTEXT_COMMITMENT_EQUALITY_PROOF_MAX_BASE64_LEN: usize = 256;

impl fmt::Display for PodCiphertextCommitmentEqualityProof {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", BASE64_STANDARD.encode(self.0))
}
}

impl_from_str!(
TYPE = PodCiphertextCommitmentEqualityProof,
BYTES_LEN = CIPHERTEXT_COMMITMENT_EQUALITY_PROOF_LEN,
BASE64_LEN = CIPHERTEXT_COMMITMENT_EQUALITY_PROOF_MAX_BASE64_LEN
);

impl_from_bytes!(
TYPE = PodCiphertextCommitmentEqualityProof,
BYTES_LEN = CIPHERTEXT_COMMITMENT_EQUALITY_PROOF_LEN
);

/// The `CiphertextCiphertextEqualityProof` type as a `Pod`.
#[derive(Clone, Copy)]
#[repr(transparent)]
Expand All @@ -66,6 +90,25 @@ impl TryFrom<PodCiphertextCiphertextEqualityProof> for CiphertextCiphertextEqual
}
}

const CIPHERTEXT_CIPHERTEXT_EQUALITY_PROOF_MAX_BASE64_LEN: usize = 300;

impl fmt::Display for PodCiphertextCiphertextEqualityProof {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", BASE64_STANDARD.encode(self.0))
}
}

impl_from_str!(
TYPE = PodCiphertextCiphertextEqualityProof,
BYTES_LEN = CIPHERTEXT_CIPHERTEXT_EQUALITY_PROOF_LEN,
BASE64_LEN = CIPHERTEXT_CIPHERTEXT_EQUALITY_PROOF_MAX_BASE64_LEN
);

impl_from_bytes!(
TYPE = PodCiphertextCiphertextEqualityProof,
BYTES_LEN = CIPHERTEXT_CIPHERTEXT_EQUALITY_PROOF_LEN
);

/// The `GroupedCiphertext2HandlesValidityProof` type as a `Pod`.
#[derive(Clone, Copy)]
#[repr(transparent)]
Expand All @@ -89,6 +132,25 @@ impl TryFrom<PodGroupedCiphertext2HandlesValidityProof> for GroupedCiphertext2Ha
}
}

const GROUPED_CIPHERTEXT_2_HANDLES_VALIDITY_PROOF_MAX_BASE64_LEN: usize = 216;

impl fmt::Display for PodGroupedCiphertext2HandlesValidityProof {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", BASE64_STANDARD.encode(self.0))
}
}

impl_from_str!(
TYPE = PodGroupedCiphertext2HandlesValidityProof,
BYTES_LEN = GROUPED_CIPHERTEXT_2_HANDLES_VALIDITY_PROOF_LEN,
BASE64_LEN = GROUPED_CIPHERTEXT_2_HANDLES_VALIDITY_PROOF_MAX_BASE64_LEN
);

impl_from_bytes!(
TYPE = PodGroupedCiphertext2HandlesValidityProof,
BYTES_LEN = GROUPED_CIPHERTEXT_2_HANDLES_VALIDITY_PROOF_LEN
);

/// The `GroupedCiphertext3HandlesValidityProof` type as a `Pod`.
#[derive(Clone, Copy)]
#[repr(transparent)]
Expand All @@ -112,6 +174,25 @@ impl TryFrom<PodGroupedCiphertext3HandlesValidityProof> for GroupedCiphertext3Ha
}
}

const GROUPED_CIPHERTEXT_3_HANDLES_VALIDITY_PROOF_MAX_BASE64_LEN: usize = 256;

impl fmt::Display for PodGroupedCiphertext3HandlesValidityProof {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", BASE64_STANDARD.encode(self.0))
}
}

impl_from_str!(
TYPE = PodGroupedCiphertext3HandlesValidityProof,
BYTES_LEN = GROUPED_CIPHERTEXT_3_HANDLES_VALIDITY_PROOF_LEN,
BASE64_LEN = GROUPED_CIPHERTEXT_3_HANDLES_VALIDITY_PROOF_MAX_BASE64_LEN
);

impl_from_bytes!(
TYPE = PodGroupedCiphertext3HandlesValidityProof,
BYTES_LEN = GROUPED_CIPHERTEXT_3_HANDLES_VALIDITY_PROOF_LEN
);

/// The `BatchedGroupedCiphertext2HandlesValidityProof` type as a `Pod`.
#[derive(Clone, Copy)]
#[repr(transparent)]
Expand Down Expand Up @@ -141,6 +222,25 @@ impl TryFrom<PodBatchedGroupedCiphertext2HandlesValidityProof>
}
}

const BATCHED_GROUPED_CIPHERTEXT_2_HANDLES_VALIDITY_PROOF_MAX_BASE64_LEN: usize = 216;

impl fmt::Display for PodBatchedGroupedCiphertext2HandlesValidityProof {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", BASE64_STANDARD.encode(self.0))
}
}

impl_from_str!(
TYPE = PodBatchedGroupedCiphertext2HandlesValidityProof,
BYTES_LEN = BATCHED_GROUPED_CIPHERTEXT_2_HANDLES_VALIDITY_PROOF_LEN,
BASE64_LEN = BATCHED_GROUPED_CIPHERTEXT_2_HANDLES_VALIDITY_PROOF_MAX_BASE64_LEN
);

impl_from_bytes!(
TYPE = PodBatchedGroupedCiphertext2HandlesValidityProof,
BYTES_LEN = BATCHED_GROUPED_CIPHERTEXT_2_HANDLES_VALIDITY_PROOF_LEN
);

/// The `BatchedGroupedCiphertext3HandlesValidityProof` type as a `Pod`.
#[derive(Clone, Copy)]
#[repr(transparent)]
Expand Down Expand Up @@ -170,6 +270,25 @@ impl TryFrom<PodBatchedGroupedCiphertext3HandlesValidityProof>
}
}

const BATCHED_GROUPED_CIPHERTEXT_3_HANDLES_VALIDITY_PROOF_MAX_BASE64_LEN: usize = 256;

impl fmt::Display for PodBatchedGroupedCiphertext3HandlesValidityProof {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", BASE64_STANDARD.encode(self.0))
}
}

impl_from_str!(
TYPE = PodBatchedGroupedCiphertext3HandlesValidityProof,
BYTES_LEN = BATCHED_GROUPED_CIPHERTEXT_3_HANDLES_VALIDITY_PROOF_LEN,
BASE64_LEN = BATCHED_GROUPED_CIPHERTEXT_3_HANDLES_VALIDITY_PROOF_MAX_BASE64_LEN
);

impl_from_bytes!(
TYPE = PodBatchedGroupedCiphertext3HandlesValidityProof,
BYTES_LEN = BATCHED_GROUPED_CIPHERTEXT_3_HANDLES_VALIDITY_PROOF_LEN
);

/// The `ZeroCiphertextProof` type as a `Pod`.
#[derive(Clone, Copy)]
#[repr(transparent)]
Expand All @@ -191,6 +310,25 @@ impl TryFrom<PodZeroCiphertextProof> for ZeroCiphertextProof {
}
}

const ZERO_CIPHERTEXT_PROOF_MAX_BASE64_LEN: usize = 128;

impl fmt::Display for PodZeroCiphertextProof {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", BASE64_STANDARD.encode(self.0))
}
}

impl_from_str!(
TYPE = PodZeroCiphertextProof,
BYTES_LEN = ZERO_CIPHERTEXT_PROOF_LEN,
BASE64_LEN = ZERO_CIPHERTEXT_PROOF_MAX_BASE64_LEN
);

impl_from_bytes!(
TYPE = PodZeroCiphertextProof,
BYTES_LEN = ZERO_CIPHERTEXT_PROOF_LEN
);

/// The `PercentageWithCapProof` type as a `Pod`.
#[derive(Clone, Copy, bytemuck_derive::Pod, bytemuck_derive::Zeroable)]
#[repr(transparent)]
Expand All @@ -212,6 +350,25 @@ impl TryFrom<PodPercentageWithCapProof> for PercentageWithCapProof {
}
}

const PERCENTAGE_WITH_CAP_PROOF_MAX_BASE64_LEN: usize = 344;

impl fmt::Display for PodPercentageWithCapProof {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", BASE64_STANDARD.encode(self.0))
}
}

impl_from_str!(
TYPE = PodPercentageWithCapProof,
BYTES_LEN = PERCENTAGE_WITH_CAP_PROOF_LEN,
BASE64_LEN = PERCENTAGE_WITH_CAP_PROOF_MAX_BASE64_LEN
);

impl_from_bytes!(
TYPE = PodPercentageWithCapProof,
BYTES_LEN = PERCENTAGE_WITH_CAP_PROOF_LEN
);

/// The `PubkeyValidityProof` type as a `Pod`.
#[derive(Clone, Copy, bytemuck_derive::Pod, bytemuck_derive::Zeroable)]
#[repr(transparent)]
Expand All @@ -233,6 +390,25 @@ impl TryFrom<PodPubkeyValidityProof> for PubkeyValidityProof {
}
}

const PUBKEY_VALIDITY_PROOF_MAX_BASE64_LEN: usize = 88;

impl fmt::Display for PodPubkeyValidityProof {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", BASE64_STANDARD.encode(self.0))
}
}

impl_from_str!(
TYPE = PodPubkeyValidityProof,
BYTES_LEN = PUBKEY_VALIDITY_PROOF_LEN,
BASE64_LEN = PUBKEY_VALIDITY_PROOF_MAX_BASE64_LEN
);

impl_from_bytes!(
TYPE = PodPubkeyValidityProof,
BYTES_LEN = PUBKEY_VALIDITY_PROOF_LEN
);

// The sigma proof pod types are wrappers for byte arrays, 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.
Expand Down

0 comments on commit 56ba5a5

Please sign in to comment.