Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 639e016

Browse files
committed
add dpendency to spl-token-confidential-transfer-* crates
1 parent 9daa22c commit 639e016

File tree

12 files changed

+247
-190
lines changed

12 files changed

+247
-190
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,48 @@
1-
use solana_zk_sdk::encryption::pod::grouped_elgamal::{
2-
PodGroupedElGamalCiphertext2Handles, PodGroupedElGamalCiphertext3Handles,
1+
use {
2+
crate::errors::TokenProofExtractionError,
3+
solana_zk_sdk::encryption::pod::{
4+
elgamal::PodElGamalCiphertext,
5+
grouped_elgamal::{
6+
PodGroupedElGamalCiphertext2Handles, PodGroupedElGamalCiphertext3Handles,
7+
},
8+
pedersen::PodPedersenCommitment,
9+
},
310
};
411

512
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
613
#[repr(C)]
714
pub struct PodTransferAmountCiphertext(pub(crate) PodGroupedElGamalCiphertext3Handles);
815

16+
impl PodTransferAmountCiphertext {
17+
pub fn extract_commitment(&self) -> PodPedersenCommitment {
18+
self.0.extract_commitment()
19+
}
20+
21+
pub fn try_extract_ciphertext(
22+
&self,
23+
index: usize,
24+
) -> Result<PodElGamalCiphertext, TokenProofExtractionError> {
25+
self.0
26+
.try_extract_ciphertext(index)
27+
.map_err(|_| TokenProofExtractionError::CiphertextExtraction)
28+
}
29+
}
30+
931
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
1032
#[repr(C)]
1133
pub struct PodFeeCiphertext(pub(crate) PodGroupedElGamalCiphertext2Handles);
34+
35+
impl PodFeeCiphertext {
36+
pub fn extract_commitment(&self) -> PodPedersenCommitment {
37+
self.0.extract_commitment()
38+
}
39+
40+
pub fn try_extract_ciphertext(
41+
&self,
42+
index: usize,
43+
) -> Result<PodElGamalCiphertext, TokenProofExtractionError> {
44+
self.0
45+
.try_extract_ciphertext(index)
46+
.map_err(|_| TokenProofExtractionError::CiphertextExtraction)
47+
}
48+
}

token/confidential-transfer/proof-extraction/src/errors.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ pub enum TokenProofExtractionError {
1212
FeeParametersMismatch,
1313
#[error("Curve arithmetic failed")]
1414
CurveArithmetic,
15+
#[error("Ciphertext extraction failed")]
16+
CiphertextExtraction,
1517
}

token/program-2022/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ solana-security-txt = "1.1.1"
2727
solana-zk-sdk = "2.0.3"
2828
spl-memo = { version = "5.0", path = "../../memo/program", features = [ "no-entrypoint" ] }
2929
spl-token = { version = "6.0", path = "../program", features = ["no-entrypoint"] }
30+
spl-token-confidential-transfer-ciphertext-arithmetic = { version = "0.1.0", path = "../confidential-transfer/ciphertext-arithmetic" }
31+
spl-token-confidential-transfer-proof-extraction = { version = "0.1.0", path = "../confidential-transfer/proof-extraction" }
3032
spl-token-group-interface = { version = "0.3.0", path = "../../token-group/interface" }
3133
spl-token-metadata-interface = { version = "0.4.0", path = "../../token-metadata/interface" }
3234
spl-transfer-hook-interface = { version = "0.7.0", path = "../transfer-hook/interface" }
@@ -37,6 +39,9 @@ serde = { version = "1.0.207", optional = true }
3739
serde_with = { version = "3.9.0", optional = true }
3840
base64 = { version = "0.22.1", optional = true }
3941

42+
[target.'cfg(not(target_os = "solana"))'.dependencies]
43+
spl-token-confidential-transfer-proof-generation = { version = "0.1.0", path = "../confidential-transfer/proof-generation"}
44+
4045
[dev-dependencies]
4146
lazy_static = "1.5.0"
4247
proptest = "1.5"

token/program-2022/src/error.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
//! Error types
22
3+
#[cfg(not(target_os = "solana"))]
4+
use spl_token_confidential_transfer_proof_generation::errors::TokenProofGenerationError;
35
use {
46
num_derive::FromPrimitive,
57
solana_program::{
68
decode_error::DecodeError,
79
msg,
810
program_error::{PrintProgramError, ProgramError},
911
},
12+
spl_token_confidential_transfer_proof_extraction::errors::TokenProofExtractionError,
1013
thiserror::Error,
1114
};
1215

@@ -243,6 +246,18 @@ pub enum TokenError {
243246
/// Ciphertext arithmetic failed
244247
#[error("Ciphertext arithmetic failed")]
245248
CiphertextArithmeticFailed,
249+
/// Pedersen commitments did not match
250+
#[error("Pedersen commitment mismatch")]
251+
PedersenCommitmentMismatch,
252+
/// Range proof length did not match
253+
#[error("Range proof length mismatch")]
254+
RangeProofLengthMismatch,
255+
/// Illegal transfer amount bit length
256+
#[error("Illegal transfer amount bit length")]
257+
IllegalBitLength,
258+
/// Fee calculation failed
259+
#[error("Fee calculation failed")]
260+
FeeCalculation,
246261
}
247262
impl From<TokenError> for ProgramError {
248263
fn from(e: TokenError) -> Self {
@@ -418,6 +433,49 @@ impl PrintProgramError for TokenError {
418433
TokenError::CiphertextArithmeticFailed => {
419434
msg!("Ciphertext arithmetic failed")
420435
}
436+
TokenError::PedersenCommitmentMismatch => {
437+
msg!("Pedersen commitments did not match")
438+
}
439+
TokenError::RangeProofLengthMismatch => {
440+
msg!("Range proof lengths did not match")
441+
}
442+
TokenError::IllegalBitLength => {
443+
msg!("Illegal transfer amount bit length")
444+
}
445+
TokenError::FeeCalculation => {
446+
msg!("Transfer fee calculation failed")
447+
}
448+
}
449+
}
450+
}
451+
452+
#[cfg(not(target_os = "solana"))]
453+
impl From<TokenProofGenerationError> for TokenError {
454+
fn from(e: TokenProofGenerationError) -> Self {
455+
match e {
456+
TokenProofGenerationError::ProofGeneration(_) => TokenError::ProofGeneration,
457+
TokenProofGenerationError::NotEnoughFunds => TokenError::InsufficientFunds,
458+
TokenProofGenerationError::IllegalAmountBitLength => TokenError::IllegalBitLength,
459+
TokenProofGenerationError::FeeCalculation => TokenError::FeeCalculation,
460+
}
461+
}
462+
}
463+
464+
impl From<TokenProofExtractionError> for TokenError {
465+
fn from(e: TokenProofExtractionError) -> Self {
466+
match e {
467+
TokenProofExtractionError::ElGamalPubkeyMismatch => {
468+
TokenError::ConfidentialTransferElGamalPubkeyMismatch
469+
}
470+
TokenProofExtractionError::PedersenCommitmentMismatch => {
471+
TokenError::PedersenCommitmentMismatch
472+
}
473+
TokenProofExtractionError::RangeProofLengthMismatch => {
474+
TokenError::RangeProofLengthMismatch
475+
}
476+
TokenProofExtractionError::FeeParametersMismatch => TokenError::FeeParametersMismatch,
477+
TokenProofExtractionError::CurveArithmetic => TokenError::CiphertextArithmeticFailed,
478+
TokenProofExtractionError::CiphertextExtraction => TokenError::MalformedCiphertext,
421479
}
422480
}
423481
}

token/program-2022/src/extension/confidential_transfer/account_info.rs

Lines changed: 11 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,22 @@ use {
22
crate::{
33
error::TokenError,
44
extension::confidential_transfer::{
5-
split_proof_generation::transfer_split_proof_data, ConfidentialTransferAccount,
6-
DecryptableBalance, EncryptedBalance, PENDING_BALANCE_LO_BIT_LENGTH,
5+
ConfidentialTransferAccount, DecryptableBalance, EncryptedBalance,
6+
PENDING_BALANCE_LO_BIT_LENGTH,
77
},
88
},
99
bytemuck::{Pod, Zeroable},
10-
solana_zk_token_sdk::{
10+
solana_zk_sdk::{
1111
encryption::{
1212
auth_encryption::{AeCiphertext, AeKey},
1313
elgamal::{ElGamalKeypair, ElGamalPubkey, ElGamalSecretKey},
1414
},
15-
instruction::{
16-
transfer::{FeeParameters, TransferData, TransferWithFeeData},
17-
withdraw::WithdrawData,
18-
zero_balance::ZeroBalanceProofData,
19-
BatchedGroupedCiphertext3HandlesValidityProofData, BatchedRangeProofU128Data,
20-
CiphertextCommitmentEqualityProofData,
21-
},
15+
zk_elgamal_proof_program::proof_data::ZeroCiphertextProofData,
2216
},
2317
spl_pod::primitives::PodU64,
18+
spl_token_confidential_transfer_proof_generation::transfer::{
19+
transfer_split_proof_data, TransferProofData,
20+
},
2421
};
2522

2623
/// Confidential transfer extension information needed to construct an
@@ -44,13 +41,13 @@ impl EmptyAccountAccountInfo {
4441
pub fn generate_proof_data(
4542
&self,
4643
elgamal_keypair: &ElGamalKeypair,
47-
) -> Result<ZeroBalanceProofData, TokenError> {
44+
) -> Result<ZeroCiphertextProofData, TokenError> {
4845
let available_balance = self
4946
.available_balance
5047
.try_into()
5148
.map_err(|_| TokenError::MalformedCiphertext)?;
5249

53-
ZeroBalanceProofData::new(elgamal_keypair, &available_balance)
50+
ZeroCiphertextProofData::new(elgamal_keypair, &available_balance)
5451
.map_err(|_| TokenError::ProofGeneration)
5552
}
5653
}
@@ -240,37 +237,6 @@ impl TransferAccountInfo {
240237
.ok_or(TokenError::AccountDecryption)
241238
}
242239

243-
/// Create a transfer proof data.
244-
pub fn generate_transfer_proof_data(
245-
&self,
246-
transfer_amount: u64,
247-
elgamal_keypair: &ElGamalKeypair,
248-
aes_key: &AeKey,
249-
destination_elgamal_pubkey: &ElGamalPubkey,
250-
auditor_elgamal_pubkey: Option<&ElGamalPubkey>,
251-
) -> Result<TransferData, TokenError> {
252-
let current_source_available_balance = self
253-
.available_balance
254-
.try_into()
255-
.map_err(|_| TokenError::MalformedCiphertext)?;
256-
let current_source_decrypted_available_balance =
257-
self.decrypted_available_balance(aes_key)?;
258-
259-
let default_auditor_pubkey = ElGamalPubkey::default();
260-
let auditor_elgamal_pubkey = auditor_elgamal_pubkey.unwrap_or(&default_auditor_pubkey);
261-
262-
TransferData::new(
263-
transfer_amount,
264-
(
265-
current_source_decrypted_available_balance,
266-
&current_source_available_balance,
267-
),
268-
elgamal_keypair,
269-
(destination_elgamal_pubkey, auditor_elgamal_pubkey),
270-
)
271-
.map_err(|_| TokenError::ProofGeneration)
272-
}
273-
274240
/// Create a transfer proof data that is split into equality, ciphertext
275241
/// validity, and range proofs.
276242
pub fn generate_split_transfer_proof_data(
@@ -280,14 +246,7 @@ impl TransferAccountInfo {
280246
aes_key: &AeKey,
281247
destination_elgamal_pubkey: &ElGamalPubkey,
282248
auditor_elgamal_pubkey: Option<&ElGamalPubkey>,
283-
) -> Result<
284-
(
285-
CiphertextCommitmentEqualityProofData,
286-
BatchedGroupedCiphertext3HandlesValidityProofData,
287-
BatchedRangeProofU128Data,
288-
),
289-
TokenError,
290-
> {
249+
) -> Result<TransferProofData, TokenError> {
291250
let current_available_balance = self
292251
.available_balance
293252
.try_into()
@@ -306,48 +265,7 @@ impl TransferAccountInfo {
306265
destination_elgamal_pubkey,
307266
auditor_elgamal_pubkey,
308267
)
309-
}
310-
311-
/// Create a transfer with fee proof data
312-
#[allow(clippy::too_many_arguments)]
313-
pub fn generate_transfer_with_fee_proof_data(
314-
&self,
315-
transfer_amount: u64,
316-
elgamal_keypair: &ElGamalKeypair,
317-
aes_key: &AeKey,
318-
destination_elgamal_pubkey: &ElGamalPubkey,
319-
auditor_elgamal_pubkey: Option<&ElGamalPubkey>,
320-
withdraw_withheld_authority_elgamal_pubkey: &ElGamalPubkey,
321-
fee_rate_basis_points: u16,
322-
maximum_fee: u64,
323-
) -> Result<TransferWithFeeData, TokenError> {
324-
let current_source_available_balance = self
325-
.available_balance
326-
.try_into()
327-
.map_err(|_| TokenError::MalformedCiphertext)?;
328-
let current_source_decrypted_available_balance =
329-
self.decrypted_available_balance(aes_key)?;
330-
331-
let default_auditor_pubkey = ElGamalPubkey::default();
332-
let auditor_elgamal_pubkey = auditor_elgamal_pubkey.unwrap_or(&default_auditor_pubkey);
333-
334-
let fee_parameters = FeeParameters {
335-
fee_rate_basis_points,
336-
maximum_fee,
337-
};
338-
339-
TransferWithFeeData::new(
340-
transfer_amount,
341-
(
342-
current_source_decrypted_available_balance,
343-
&current_source_available_balance,
344-
),
345-
elgamal_keypair,
346-
(destination_elgamal_pubkey, auditor_elgamal_pubkey),
347-
fee_parameters,
348-
withdraw_withheld_authority_elgamal_pubkey,
349-
)
350-
.map_err(|_| TokenError::ProofGeneration)
268+
.map_err(|e| -> TokenError { e.into() })
351269
}
352270

353271
/// Update the decryptable available balance.

token/program-2022/src/extension/confidential_transfer/mod.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,10 @@ pub mod processor;
3333
/// Transfer Extension
3434
pub mod verify_proof;
3535

36-
/// Helper functions to generate split zero-knowledge proofs for confidential
37-
/// transfers in the Confidential Transfer Extension.
38-
///
39-
/// The logic in this submodule should belong to the `solana-zk-token-sdk` and
40-
/// will be removed with the next upgrade to the Solana program.
41-
#[cfg(not(target_os = "solana"))]
42-
pub mod split_proof_generation;
43-
4436
/// Confidential Transfer Extension account information needed for instructions
4537
#[cfg(not(target_os = "solana"))]
4638
pub mod account_info;
4739

48-
/// Ciphertext extraction and proof related helper logic
49-
///
50-
/// This submodule should be removed with the next upgrade to the Solana program
51-
pub mod ciphertext_extraction;
52-
5340
/// ElGamal ciphertext containing an account balance
5441
pub type EncryptedBalance = PodElGamalCiphertext;
5542
/// Authenticated encryption containing an account balance

0 commit comments

Comments
 (0)