1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
use {
crate::zk_token_elgamal::pod,
bytemuck::{Pod, Zeroable},
};
#[cfg(not(target_arch = "bpf"))]
use {
crate::{
encryption::{
elgamal::{ElGamalCiphertext, ElGamalKeypair, ElGamalPubkey},
pedersen::{Pedersen, PedersenCommitment, PedersenOpening},
},
errors::ProofError,
instruction::Verifiable,
range_proof::RangeProof,
sigma_proofs::equality_proof::EqualityProof,
transcript::TranscriptProtocol,
},
merlin::Transcript,
std::convert::TryInto,
};
#[derive(Clone, Copy, Pod, Zeroable)]
#[repr(C)]
pub struct WithdrawData {
pub elgamal_pubkey: pod::ElGamalPubkey,
pub final_balance_ct: pod::ElGamalCiphertext,
pub proof: WithdrawProof,
}
impl WithdrawData {
#[cfg(not(target_arch = "bpf"))]
pub fn new(
amount: u64,
source_keypair: &ElGamalKeypair,
current_balance: u64,
current_balance_ct: ElGamalCiphertext,
) -> Self {
let final_balance = current_balance - amount;
let amount_encoded = source_keypair
.public
.encrypt_with(amount, &PedersenOpening::default());
let final_balance_ct = current_balance_ct - amount_encoded;
let proof = WithdrawProof::new(source_keypair, final_balance, &final_balance_ct);
Self {
elgamal_pubkey: source_keypair.public.into(),
final_balance_ct: final_balance_ct.into(),
proof,
}
}
}
#[cfg(not(target_arch = "bpf"))]
impl Verifiable for WithdrawData {
fn verify(&self) -> Result<(), ProofError> {
let elgamal_pubkey = self.elgamal_pubkey.try_into()?;
let final_balance_ct = self.final_balance_ct.try_into()?;
self.proof.verify(&elgamal_pubkey, &final_balance_ct)
}
}
#[derive(Clone, Copy, Pod, Zeroable)]
#[repr(C)]
#[allow(non_snake_case)]
pub struct WithdrawProof {
pub commitment: pod::PedersenCommitment,
pub equality_proof: pod::EqualityProof,
pub range_proof: pod::RangeProof64,
}
#[allow(non_snake_case)]
#[cfg(not(target_arch = "bpf"))]
impl WithdrawProof {
fn transcript_new() -> Transcript {
Transcript::new(b"WithdrawProof")
}
pub fn new(
source_keypair: &ElGamalKeypair,
final_balance: u64,
final_balance_ct: &ElGamalCiphertext,
) -> Self {
let mut transcript = Self::transcript_new();
transcript.withdraw_proof_domain_sep();
let (commitment, opening) = Pedersen::new(final_balance);
let P_EG = source_keypair.public.get_point();
let C_EG = final_balance_ct.message_comm.get_point();
let D_EG = final_balance_ct.decrypt_handle.get_point();
let C_Ped = commitment.get_point();
transcript.append_point(b"P_EG", &P_EG.compress());
transcript.append_point(b"C_EG", &C_EG.compress());
transcript.append_point(b"D_EG", &D_EG.compress());
transcript.append_point(b"C_Ped", &C_Ped.compress());
let equality_proof = EqualityProof::new(
source_keypair,
final_balance_ct,
final_balance,
&opening,
&mut transcript,
);
let range_proof = RangeProof::new(
vec![final_balance],
vec![64],
vec![&opening],
&mut transcript,
);
WithdrawProof {
commitment: commitment.into(),
equality_proof: equality_proof.try_into().expect("equality proof"),
range_proof: range_proof.try_into().expect("range proof"),
}
}
pub fn verify(
&self,
source_pk: &ElGamalPubkey,
final_balance_ct: &ElGamalCiphertext,
) -> Result<(), ProofError> {
let mut transcript = Self::transcript_new();
let commitment: PedersenCommitment = self.commitment.try_into()?;
let equality_proof: EqualityProof = self.equality_proof.try_into()?;
let range_proof: RangeProof = self.range_proof.try_into()?;
transcript.withdraw_proof_domain_sep();
let P_EG = source_pk.get_point();
let C_EG = final_balance_ct.message_comm.get_point();
let D_EG = final_balance_ct.decrypt_handle.get_point();
let C_Ped = commitment.get_point();
transcript.append_point(b"P_EG", &P_EG.compress());
transcript.append_point(b"C_EG", &C_EG.compress());
transcript.append_point(b"D_EG", &D_EG.compress());
transcript.append_point(b"C_Ped", &C_Ped.compress());
equality_proof.verify(source_pk, final_balance_ct, &commitment, &mut transcript)?;
range_proof.verify(
vec![&commitment.get_point().compress()],
vec![64_usize],
&mut transcript,
)?;
Ok(())
}
}
#[cfg(test)]
mod test {
use {super::*, crate::encryption::elgamal::ElGamalKeypair};
#[test]
fn test_withdraw_correctness() {
let elgamal_keypair = ElGamalKeypair::default();
let current_balance: u64 = 77;
let current_balance_ct = elgamal_keypair.public.encrypt(current_balance);
let withdraw_amount: u64 = 55;
let data = WithdrawData::new(
withdraw_amount,
&elgamal_keypair,
current_balance,
current_balance_ct,
);
assert!(data.verify().is_ok());
let wrong_balance: u64 = 99;
let data = WithdrawData::new(
withdraw_amount,
&elgamal_keypair,
wrong_balance,
current_balance_ct,
);
assert!(data.verify().is_err());
}
}