From 00b1d0930f9cd45260feff10a901a87d1314c6d1 Mon Sep 17 00:00:00 2001 From: samkim-crypto Date: Sun, 16 Oct 2022 07:06:57 +0900 Subject: [PATCH] [zk-token-sdk] Restructure proof error types (#28407) * add pubkey sigma proof * cargo fmt * add EncryptionError * add encryption errors --- zk-token-sdk/src/encryption/discrete_log.rs | 10 +++++----- zk-token-sdk/src/encryption/errors.rs | 10 ++++++++++ zk-token-sdk/src/encryption/mod.rs | 1 + zk-token-sdk/src/errors.rs | 14 ++++---------- zk-token-sdk/src/zk_token_elgamal/convert.rs | 10 +++++----- 5 files changed, 25 insertions(+), 20 deletions(-) create mode 100644 zk-token-sdk/src/encryption/errors.rs diff --git a/zk-token-sdk/src/encryption/discrete_log.rs b/zk-token-sdk/src/encryption/discrete_log.rs index bc5057699..e152dfe8b 100644 --- a/zk-token-sdk/src/encryption/discrete_log.rs +++ b/zk-token-sdk/src/encryption/discrete_log.rs @@ -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, @@ -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; @@ -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; diff --git a/zk-token-sdk/src/encryption/errors.rs b/zk-token-sdk/src/encryption/errors.rs new file mode 100644 index 000000000..e076da08c --- /dev/null +++ b/zk-token-sdk/src/encryption/errors.rs @@ -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, +} diff --git a/zk-token-sdk/src/encryption/mod.rs b/zk-token-sdk/src/encryption/mod.rs index a90b88a5f..0025ec735 100644 --- a/zk-token-sdk/src/encryption/mod.rs +++ b/zk-token-sdk/src/encryption/mod.rs @@ -13,4 +13,5 @@ pub mod auth_encryption; pub mod discrete_log; pub mod elgamal; +pub mod errors; pub mod pedersen; diff --git a/zk-token-sdk/src/errors.rs b/zk-token-sdk/src/errors.rs index 2b41fc04a..76cc5a5ee 100644 --- a/zk-token-sdk/src/errors.rs +++ b/zk-token-sdk/src/errors.rs @@ -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)] diff --git a/zk-token-sdk/src/zk_token_elgamal/convert.rs b/zk-token-sdk/src/zk_token_elgamal/convert.rs index f9fc92eeb..ccfdd2595 100644 --- a/zk-token-sdk/src/zk_token_elgamal/convert.rs +++ b/zk-token-sdk/src/zk_token_elgamal/convert.rs @@ -98,7 +98,7 @@ mod target_arch { type Error = ProofError; fn try_from(ct: pod::ElGamalCiphertext) -> Result { - Self::from_bytes(&ct.0).ok_or(ProofError::InconsistentCTData) + Self::from_bytes(&ct.0).ok_or(ProofError::CiphertextDeserialization) } } @@ -112,7 +112,7 @@ mod target_arch { type Error = ProofError; fn try_from(pk: pod::ElGamalPubkey) -> Result { - Self::from_bytes(&pk.0).ok_or(ProofError::InconsistentCTData) + Self::from_bytes(&pk.0).ok_or(ProofError::CiphertextDeserialization) } } @@ -147,7 +147,7 @@ mod target_arch { type Error = ProofError; fn try_from(pod: pod::PedersenCommitment) -> Result { - Self::from_bytes(&pod.0).ok_or(ProofError::InconsistentCTData) + Self::from_bytes(&pod.0).ok_or(ProofError::CiphertextDeserialization) } } @@ -171,7 +171,7 @@ mod target_arch { type Error = ProofError; fn try_from(pod: pod::DecryptHandle) -> Result { - Self::from_bytes(&pod.0).ok_or(ProofError::InconsistentCTData) + Self::from_bytes(&pod.0).ok_or(ProofError::CiphertextDeserialization) } } @@ -185,7 +185,7 @@ mod target_arch { type Error = ProofError; fn try_from(ct: pod::AeCiphertext) -> Result { - Self::from_bytes(&ct.0).ok_or(ProofError::InconsistentCTData) + Self::from_bytes(&ct.0).ok_or(ProofError::CiphertextDeserialization) } }