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

Commit fd4fdb0

Browse files
committed
add withdraw proof generation
1 parent 09d85c9 commit fd4fdb0

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub mod encryption;
1010
pub mod errors;
1111
pub mod transfer;
1212
pub mod transfer_with_fee;
13+
pub mod withdraw;
1314

1415
/// The low bit length of the encrypted transfer amount
1516
pub const TRANSFER_AMOUNT_LO_BITS: usize = 16;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use {
2+
crate::errors::TokenProofGenerationError,
3+
solana_zk_sdk::{
4+
encryption::{
5+
elgamal::{ElGamal, ElGamalCiphertext, ElGamalKeypair},
6+
pedersen::Pedersen,
7+
},
8+
zk_elgamal_proof_program::proof_data::{
9+
BatchedRangeProofU64Data, CiphertextCommitmentEqualityProofData,
10+
},
11+
},
12+
};
13+
14+
const REMAINING_BALANCE_BIT_LENGTH: usize = 64;
15+
16+
pub fn withdraw_proof_data(
17+
current_available_balance: &ElGamalCiphertext,
18+
current_balance: u64,
19+
withdraw_amount: u64,
20+
elgamal_keypair: &ElGamalKeypair,
21+
) -> Result<
22+
(
23+
CiphertextCommitmentEqualityProofData,
24+
BatchedRangeProofU64Data,
25+
),
26+
TokenProofGenerationError,
27+
> {
28+
// Calculate the remaining balance after withdraw
29+
let remaining_balance = current_balance
30+
.checked_sub(withdraw_amount)
31+
.ok_or(TokenProofGenerationError::NotEnoughFunds)?;
32+
33+
// Generate a Pedersen commitment for the remaining balance
34+
let (remaining_balance_commitment, remaining_balance_opening) =
35+
Pedersen::new(remaining_balance);
36+
37+
// Compute the remaining balance ciphertext
38+
#[allow(clippy::arithmetic_side_effects)]
39+
let remaining_balance_ciphertext = current_available_balance - ElGamal::encode(withdraw_amount);
40+
41+
// Generate proof data
42+
let equality_proof_data = CiphertextCommitmentEqualityProofData::new(
43+
elgamal_keypair,
44+
&remaining_balance_ciphertext,
45+
&remaining_balance_commitment,
46+
&remaining_balance_opening,
47+
remaining_balance,
48+
)
49+
.map_err(TokenProofGenerationError::from)?;
50+
51+
let range_proof_data = BatchedRangeProofU64Data::new(
52+
vec![&remaining_balance_commitment],
53+
vec![remaining_balance],
54+
vec![REMAINING_BALANCE_BIT_LENGTH],
55+
vec![&remaining_balance_opening],
56+
)
57+
.map_err(TokenProofGenerationError::from)?;
58+
59+
Ok((equality_proof_data, range_proof_data))
60+
}

0 commit comments

Comments
 (0)