[zk-token-sdk] Refactor `AuthenticatedEncryptionError` and `ElGamalError` to errors module (#589)

* make the `errors` module available in sbf targets

* move `AuthenticatedEncryptionError` to the `errors` module

* move `ElGamalError` to the `errors` module
This commit is contained in:
samkim-crypto 2024-04-12 11:26:40 +09:00 committed by GitHub
parent ad94732d4f
commit b046c12cdc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 64 additions and 56 deletions

View File

@ -2,16 +2,8 @@
//!
//! This module is a simple wrapper of the `Aes128GcmSiv` implementation specialized for SPL
//! token-2022 where the plaintext is always `u64`.
#[cfg(not(target_os = "solana"))]
use {
aes_gcm_siv::{
aead::{Aead, NewAead},
Aes128GcmSiv,
},
rand::{rngs::OsRng, Rng},
thiserror::Error,
};
use {
crate::errors::AuthenticatedEncryptionError,
base64::{prelude::BASE64_STANDARD, Engine},
sha3::{Digest, Sha3_512},
solana_sdk::{
@ -30,6 +22,14 @@ use {
subtle::ConstantTimeEq,
zeroize::Zeroize,
};
#[cfg(not(target_os = "solana"))]
use {
aes_gcm_siv::{
aead::{Aead, NewAead},
Aes128GcmSiv,
},
rand::{rngs::OsRng, Rng},
};
/// Byte length of an authenticated encryption secret key
pub const AE_KEY_LEN: usize = 16;
@ -44,18 +44,6 @@ const CIPHERTEXT_LEN: usize = 24;
/// ciphertext and nonce components
const AE_CIPHERTEXT_LEN: usize = 36;
#[derive(Error, Clone, Debug, Eq, PartialEq)]
pub enum AuthenticatedEncryptionError {
#[error("key derivation method not supported")]
DerivationMethodNotSupported,
#[error("seed length too short for derivation")]
SeedLengthTooShort,
#[error("seed length too long for derivation")]
SeedLengthTooLong,
#[error("failed to deserialize")]
Deserialization,
}
struct AuthenticatedEncryption;
impl AuthenticatedEncryption {
/// Generates an authenticated encryption key.

View File

@ -21,6 +21,7 @@ use {
Pedersen, PedersenCommitment, PedersenOpening, G, H, PEDERSEN_COMMITMENT_LEN,
},
},
errors::ElGamalError,
RISTRETTO_POINT_LEN, SCALAR_LEN,
},
base64::{prelude::BASE64_STANDARD, Engine},
@ -41,7 +42,6 @@ use {
},
std::convert::TryInto,
subtle::{Choice, ConstantTimeEq},
thiserror::Error,
zeroize::Zeroize,
};
#[cfg(not(target_os = "solana"))]
@ -70,24 +70,6 @@ const ELGAMAL_SECRET_KEY_LEN: usize = SCALAR_LEN;
/// Byte length of an ElGamal keypair
pub const ELGAMAL_KEYPAIR_LEN: usize = ELGAMAL_PUBKEY_LEN + ELGAMAL_SECRET_KEY_LEN;
#[derive(Error, Clone, Debug, Eq, PartialEq)]
pub enum ElGamalError {
#[error("key derivation method not supported")]
DerivationMethodNotSupported,
#[error("seed length too short for derivation")]
SeedLengthTooShort,
#[error("seed length too long for derivation")]
SeedLengthTooLong,
#[error("failed to deserialize ciphertext")]
CiphertextDeserialization,
#[error("failed to deserialize public key")]
PubkeyDeserialization,
#[error("failed to deserialize keypair")]
KeypairDeserialization,
#[error("failed to deserialize secret key")]
SecretKeyDeserialization,
}
/// Algorithm handle for the twisted ElGamal encryption scheme
pub struct ElGamal;
impl ElGamal {

View File

@ -1,13 +1,42 @@
//! Errors related to proving and verifying proofs.
use {
crate::{
encryption::elgamal::ElGamalError,
range_proof::errors::{RangeProofGenerationError, RangeProofVerificationError},
sigma_proofs::errors::*,
},
thiserror::Error,
#[cfg(not(target_os = "solana"))]
use crate::{
range_proof::errors::{RangeProofGenerationError, RangeProofVerificationError},
sigma_proofs::errors::*,
};
use thiserror::Error;
#[derive(Error, Clone, Debug, Eq, PartialEq)]
pub enum AuthenticatedEncryptionError {
#[error("key derivation method not supported")]
DerivationMethodNotSupported,
#[error("seed length too short for derivation")]
SeedLengthTooShort,
#[error("seed length too long for derivation")]
SeedLengthTooLong,
#[error("failed to deserialize")]
Deserialization,
}
#[derive(Error, Clone, Debug, Eq, PartialEq)]
pub enum ElGamalError {
#[error("key derivation method not supported")]
DerivationMethodNotSupported,
#[error("seed length too short for derivation")]
SeedLengthTooShort,
#[error("seed length too long for derivation")]
SeedLengthTooLong,
#[error("failed to deserialize ciphertext")]
CiphertextDeserialization,
#[error("failed to deserialize public key")]
PubkeyDeserialization,
#[error("failed to deserialize keypair")]
KeypairDeserialization,
#[error("failed to deserialize secret key")]
SecretKeyDeserialization,
}
#[cfg(not(target_os = "solana"))]
#[derive(Error, Clone, Debug, Eq, PartialEq)]
pub enum ProofGenerationError {
#[error("not enough funds in account")]
@ -26,6 +55,7 @@ pub enum ProofGenerationError {
ProofLength,
}
#[cfg(not(target_os = "solana"))]
#[derive(Error, Clone, Debug, Eq, PartialEq)]
pub enum ProofVerificationError {
#[error("range proof verification failed")]
@ -51,35 +81,42 @@ pub enum SigmaProofType {
PubkeyValidityProof,
}
#[cfg(not(target_os = "solana"))]
#[derive(Error, Clone, Debug, Eq, PartialEq)]
pub enum TranscriptError {
#[error("point is the identity")]
ValidationError,
}
#[cfg(not(target_os = "solana"))]
impl From<EqualityProofVerificationError> for ProofVerificationError {
fn from(err: EqualityProofVerificationError) -> Self {
Self::SigmaProof(SigmaProofType::EqualityProof, err.0)
}
}
#[cfg(not(target_os = "solana"))]
impl From<FeeSigmaProofVerificationError> for ProofVerificationError {
fn from(err: FeeSigmaProofVerificationError) -> Self {
Self::SigmaProof(SigmaProofType::FeeSigmaProof, err.0)
}
}
#[cfg(not(target_os = "solana"))]
impl From<ZeroBalanceProofVerificationError> for ProofVerificationError {
fn from(err: ZeroBalanceProofVerificationError) -> Self {
Self::SigmaProof(SigmaProofType::ZeroBalanceProof, err.0)
}
}
#[cfg(not(target_os = "solana"))]
impl From<ValidityProofVerificationError> for ProofVerificationError {
fn from(err: ValidityProofVerificationError) -> Self {
Self::SigmaProof(SigmaProofType::ValidityProof, err.0)
}
}
#[cfg(not(target_os = "solana"))]
impl From<PubkeyValidityProofVerificationError> for ProofVerificationError {
fn from(err: PubkeyValidityProofVerificationError) -> Self {
Self::SigmaProof(SigmaProofType::PubkeyValidityProof, err.0)

View File

@ -23,16 +23,14 @@ pub(crate) mod macros;
#[cfg(not(target_os = "solana"))]
pub mod encryption;
#[cfg(not(target_os = "solana"))]
pub mod errors;
#[cfg(not(target_os = "solana"))]
mod range_proof;
#[cfg(not(target_os = "solana"))]
mod sigma_proofs;
#[cfg(not(target_os = "solana"))]
mod transcript;
// TODO: re-organize visibility
pub mod curve25519;
pub mod errors;
pub mod instruction;
pub mod zk_token_elgamal;
pub mod zk_token_proof_instruction;

View File

@ -49,7 +49,7 @@ impl From<PodRistrettoPoint> for pod::DecryptHandle {
mod target_arch {
use {
super::pod,
crate::{curve25519::scalar::PodScalar, encryption::elgamal::ElGamalError},
crate::{curve25519::scalar::PodScalar, errors::ElGamalError},
curve25519_dalek::{ristretto::CompressedRistretto, scalar::Scalar},
std::convert::TryFrom,
};

View File

@ -1,7 +1,7 @@
//! Plain Old Data types for the AES128-GCM-SIV authenticated encryption scheme.
#[cfg(not(target_os = "solana"))]
use crate::encryption::auth_encryption::{self as decoded, AuthenticatedEncryptionError};
use crate::{encryption::auth_encryption as decoded, errors::AuthenticatedEncryptionError};
use {
crate::zk_token_elgamal::pod::{impl_from_str, Pod, Zeroable},
base64::{prelude::BASE64_STANDARD, Engine},

View File

@ -2,7 +2,10 @@
#[cfg(not(target_os = "solana"))]
use {
crate::encryption::elgamal::{self as decoded, ElGamalError},
crate::{
encryption::elgamal::{self as decoded},
errors::ElGamalError,
},
curve25519_dalek::ristretto::CompressedRistretto,
};
use {

View File

@ -1,7 +1,7 @@
//! Plain Old Data types for the Grouped ElGamal encryption scheme.
#[cfg(not(target_os = "solana"))]
use crate::encryption::{elgamal::ElGamalError, grouped_elgamal::GroupedElGamalCiphertext};
use crate::{encryption::grouped_elgamal::GroupedElGamalCiphertext, errors::ElGamalError};
use {
crate::zk_token_elgamal::pod::{
elgamal::DECRYPT_HANDLE_LEN, pedersen::PEDERSEN_COMMITMENT_LEN, Pod, Zeroable,

View File

@ -3,7 +3,7 @@ use crate::zk_token_elgamal::pod::{
Zeroable,
};
#[cfg(not(target_os = "solana"))]
use crate::{encryption::elgamal::ElGamalError, instruction::transfer as decoded};
use crate::{errors::ElGamalError, instruction::transfer as decoded};
#[derive(Clone, Copy, Pod, Zeroable)]
#[repr(C)]

View File

@ -2,7 +2,7 @@
#[cfg(not(target_os = "solana"))]
use {
crate::encryption::{elgamal::ElGamalError, pedersen as decoded},
crate::{encryption::pedersen as decoded, errors::ElGamalError},
curve25519_dalek::ristretto::CompressedRistretto,
};
use {