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

Commit 208751e

Browse files
committed
re-organize proof data using structs
1 parent 83fa895 commit 208751e

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed

token/confidential-transfer/proof-generation/src/withdraw.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ use {
1313

1414
const REMAINING_BALANCE_BIT_LENGTH: usize = 64;
1515

16+
/// Proof data required for a withdraw instruction
17+
pub struct WithdrawProofData {
18+
pub equality_proof_data: CiphertextCommitmentEqualityProofData,
19+
pub range_proof_data: BatchedRangeProofU64Data,
20+
}
21+
1622
pub fn withdraw_proof_data(
1723
current_available_balance: &ElGamalCiphertext,
1824
current_balance: u64,
1925
withdraw_amount: u64,
2026
elgamal_keypair: &ElGamalKeypair,
21-
) -> Result<
22-
(
23-
CiphertextCommitmentEqualityProofData,
24-
BatchedRangeProofU64Data,
25-
),
26-
TokenProofGenerationError,
27-
> {
27+
) -> Result<WithdrawProofData, TokenProofGenerationError> {
2828
// Calculate the remaining balance after withdraw
2929
let remaining_balance = current_balance
3030
.checked_sub(withdraw_amount)
@@ -56,5 +56,8 @@ pub fn withdraw_proof_data(
5656
)
5757
.map_err(TokenProofGenerationError::from)?;
5858

59-
Ok((equality_proof_data, range_proof_data))
59+
Ok(WithdrawProofData {
60+
equality_proof_data,
61+
range_proof_data,
62+
})
6063
}

token/confidential-transfer/proof-tests/tests/proof_test.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ use {
55
},
66
spl_token_confidential_transfer_proof_extraction::{
77
transfer::TransferProofContext, transfer_with_fee::TransferWithFeeProofContext,
8+
withdraw::WithdrawProofContext,
89
},
910
spl_token_confidential_transfer_proof_generation::{
10-
transfer::transfer_split_proof_data, transfer_with_fee::transfer_with_fee_split_proof_data,
11+
transfer::transfer_split_proof_data,
12+
transfer_with_fee::transfer_with_fee_split_proof_data,
13+
withdraw::{withdraw_proof_data, WithdrawProofData},
1114
},
1215
};
1316

@@ -140,3 +143,38 @@ fn test_transfer_with_fee_proof_validity(
140143
)
141144
.unwrap();
142145
}
146+
147+
#[test]
148+
fn test_withdraw_proof_correctness() {
149+
test_withdraw_validity(0, 0);
150+
test_withdraw_validity(77, 55);
151+
test_withdraw_validity(65535, 65535);
152+
test_withdraw_validity(65536, 65536);
153+
test_withdraw_validity(281474976710655, 281474976710655);
154+
}
155+
156+
fn test_withdraw_validity(spendable_balance: u64, withdraw_amount: u64) {
157+
let keypair = ElGamalKeypair::new_rand();
158+
159+
let spendable_ciphertext = keypair.pubkey().encrypt(spendable_balance);
160+
161+
let WithdrawProofData {
162+
equality_proof_data,
163+
range_proof_data,
164+
} = withdraw_proof_data(
165+
&spendable_ciphertext,
166+
spendable_balance,
167+
withdraw_amount,
168+
&keypair,
169+
)
170+
.unwrap();
171+
172+
equality_proof_data.verify_proof().unwrap();
173+
range_proof_data.verify_proof().unwrap();
174+
175+
WithdrawProofContext::verify_and_extract(
176+
equality_proof_data.context_data(),
177+
range_proof_data.context_data(),
178+
)
179+
.unwrap();
180+
}

0 commit comments

Comments
 (0)