Skip to content

Commit

Permalink
[zk-token-sdk] Restructure proof error types (solana-labs#28407)
Browse files Browse the repository at this point in the history
* add pubkey sigma proof

* cargo fmt

* add EncryptionError

* add encryption errors
  • Loading branch information
samkim-crypto authored and nickfrosty committed Jan 4, 2023
1 parent 9808a8e commit dc68283
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 20 deletions.
10 changes: 5 additions & 5 deletions zk-token-sdk/src/encryption/discrete_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#![cfg(not(target_os = "solana"))]

use {
crate::errors::ProofError,
crate::encryption::errors::EncryptionError,
curve25519_dalek::{
constants::RISTRETTO_BASEPOINT_POINT as G,
ristretto::RistrettoPoint,
Expand Down Expand Up @@ -100,10 +100,10 @@ impl DiscreteLog {
}

/// Adjusts number of threads in a discrete log instance.
pub fn num_threads(&mut self, num_threads: usize) -> Result<(), ProofError> {
pub fn num_threads(&mut self, num_threads: usize) -> Result<(), EncryptionError> {
// number of threads must be a positive power-of-two integer
if num_threads == 0 || (num_threads & (num_threads - 1)) != 0 || num_threads > 65536 {
return Err(ProofError::DiscreteLogThreads);
return Err(EncryptionError::DiscreteLogThreads);
}

self.num_threads = num_threads;
Expand All @@ -117,9 +117,9 @@ impl DiscreteLog {
pub fn set_compression_batch_size(
&mut self,
compression_batch_size: usize,
) -> Result<(), ProofError> {
) -> Result<(), EncryptionError> {
if compression_batch_size >= TWO16 as usize {
return Err(ProofError::DiscreteLogBatchSize);
return Err(EncryptionError::DiscreteLogBatchSize);
}
self.compression_batch_size = compression_batch_size;

Expand Down
10 changes: 10 additions & 0 deletions zk-token-sdk/src/encryption/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//! Errors related to the twisted ElGamal encryption scheme.
use thiserror::Error;

#[derive(Error, Clone, Debug, Eq, PartialEq)]
pub enum EncryptionError {
#[error("discrete log number of threads not power-of-two")]
DiscreteLogThreads,
#[error("discrete log batch size too large")]
DiscreteLogBatchSize,
}
1 change: 1 addition & 0 deletions zk-token-sdk/src/encryption/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
pub mod auth_encryption;
pub mod discrete_log;
pub mod elgamal;
pub mod errors;
pub mod pedersen;
14 changes: 4 additions & 10 deletions zk-token-sdk/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,12 @@ pub enum ProofError {
ZeroBalanceProof,
#[error("validity proof failed to verify")]
ValidityProof,
#[error(
"`zk_token_elgamal::pod::ElGamalCiphertext` contains invalid ElGamalCiphertext ciphertext"
)]
InconsistentCTData,
#[error("failed to decrypt ciphertext from transfer data")]
Decryption,
#[error("discrete log number of threads not power-of-two")]
DiscreteLogThreads,
#[error("discrete log batch size too large")]
DiscreteLogBatchSize,
#[error("public-key sigma proof failed to verify")]
PubkeySigmaProof,
#[error("failed to decrypt ciphertext")]
Decryption,
#[error("invalid ciphertext data")]
CiphertextDeserialization,
}

#[derive(Error, Clone, Debug, Eq, PartialEq)]
Expand Down
10 changes: 5 additions & 5 deletions zk-token-sdk/src/zk_token_elgamal/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ mod target_arch {
type Error = ProofError;

fn try_from(ct: pod::ElGamalCiphertext) -> Result<Self, Self::Error> {
Self::from_bytes(&ct.0).ok_or(ProofError::InconsistentCTData)
Self::from_bytes(&ct.0).ok_or(ProofError::CiphertextDeserialization)
}
}

Expand All @@ -112,7 +112,7 @@ mod target_arch {
type Error = ProofError;

fn try_from(pk: pod::ElGamalPubkey) -> Result<Self, Self::Error> {
Self::from_bytes(&pk.0).ok_or(ProofError::InconsistentCTData)
Self::from_bytes(&pk.0).ok_or(ProofError::CiphertextDeserialization)
}
}

Expand Down Expand Up @@ -147,7 +147,7 @@ mod target_arch {
type Error = ProofError;

fn try_from(pod: pod::PedersenCommitment) -> Result<Self, Self::Error> {
Self::from_bytes(&pod.0).ok_or(ProofError::InconsistentCTData)
Self::from_bytes(&pod.0).ok_or(ProofError::CiphertextDeserialization)
}
}

Expand All @@ -171,7 +171,7 @@ mod target_arch {
type Error = ProofError;

fn try_from(pod: pod::DecryptHandle) -> Result<Self, Self::Error> {
Self::from_bytes(&pod.0).ok_or(ProofError::InconsistentCTData)
Self::from_bytes(&pod.0).ok_or(ProofError::CiphertextDeserialization)
}
}

Expand All @@ -185,7 +185,7 @@ mod target_arch {
type Error = ProofError;

fn try_from(ct: pod::AeCiphertext) -> Result<Self, Self::Error> {
Self::from_bytes(&ct.0).ok_or(ProofError::InconsistentCTData)
Self::from_bytes(&ct.0).ok_or(ProofError::CiphertextDeserialization)
}
}

Expand Down

0 comments on commit dc68283

Please sign in to comment.