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
use {
    crate::zk_token_elgamal::pod,
    bytemuck::{Pod, Zeroable},
};
#[cfg(not(target_arch = "bpf"))]
use {
    crate::{
        encryption::{
            elgamal::{ElGamalCiphertext, ElGamalKeypair, ElGamalPubkey},
            pedersen::PedersenOpening,
        },
        errors::ProofError,
        instruction::Verifiable,
        sigma_proofs::equality_proof::CtxtCtxtEqualityProof,
        transcript::TranscriptProtocol,
    },
    merlin::Transcript,
    std::convert::TryInto,
};

/// This struct includes the cryptographic proof *and* the account data information needed to verify
/// the proof
///
/// - The pre-instruction should call WithdrawData::verify_proof(&self)
/// - The actual program should check that `current_ct` is consistent with what is
///   currently stored in the confidential token account TODO: update this statement
///
#[derive(Clone, Copy, Pod, Zeroable)]
#[repr(C)]
pub struct WithdrawWithheldTokensData {
    pub pubkey_withdraw_withheld_authority: pod::ElGamalPubkey,

    pub pubkey_dest: pod::ElGamalPubkey,

    pub ciphertext_withdraw_withheld_authority: pod::ElGamalCiphertext,

    pub ciphertext_dest: pod::ElGamalCiphertext,

    pub proof: WithdrawWithheldTokensProof,
}

impl WithdrawWithheldTokensData {
    #[cfg(not(target_arch = "bpf"))]
    pub fn new(
        keypair_withdraw_withheld_authority: &ElGamalKeypair,
        pubkey_dest: &ElGamalPubkey,
        ciphertext_withdraw_withheld_authority: &ElGamalCiphertext,
        amount: u64,
    ) -> Result<Self, ProofError> {
        let opening_dest = PedersenOpening::new_rand();
        let ciphertext_dest = pubkey_dest.encrypt_with(amount, &opening_dest);

        let pod_pubkey_withdraw_withheld_authority =
            pod::ElGamalPubkey(keypair_withdraw_withheld_authority.public.to_bytes());
        let pod_pubkey_dest = pod::ElGamalPubkey(pubkey_dest.to_bytes());
        let pod_ciphertext_withdraw_withheld_authority =
            pod::ElGamalCiphertext(ciphertext_withdraw_withheld_authority.to_bytes());
        let pod_ciphertext_dest = pod::ElGamalCiphertext(ciphertext_dest.to_bytes());

        let mut transcript = WithdrawWithheldTokensProof::transcript_new(
            &pod_pubkey_withdraw_withheld_authority,
            &pod_pubkey_dest,
            &pod_ciphertext_withdraw_withheld_authority,
            &pod_ciphertext_dest,
        );

        let proof = WithdrawWithheldTokensProof::new(
            keypair_withdraw_withheld_authority,
            pubkey_dest,
            ciphertext_withdraw_withheld_authority,
            amount,
            &opening_dest,
            &mut transcript,
        );

        Ok(Self {
            pubkey_withdraw_withheld_authority: pod_pubkey_withdraw_withheld_authority,
            pubkey_dest: pod_pubkey_dest,
            ciphertext_withdraw_withheld_authority: pod_ciphertext_withdraw_withheld_authority,
            ciphertext_dest: pod_ciphertext_dest,
            proof,
        })
    }
}

#[cfg(not(target_arch = "bpf"))]
impl Verifiable for WithdrawWithheldTokensData {
    fn verify(&self) -> Result<(), ProofError> {
        let mut transcript = WithdrawWithheldTokensProof::transcript_new(
            &self.pubkey_withdraw_withheld_authority,
            &self.pubkey_dest,
            &self.ciphertext_withdraw_withheld_authority,
            &self.ciphertext_dest,
        );

        let pubkey_withdraw_withheld_authority =
            self.pubkey_withdraw_withheld_authority.try_into()?;
        let pubkey_dest = self.pubkey_dest.try_into()?;
        let ciphertext_withdraw_withheld_authority =
            self.ciphertext_withdraw_withheld_authority.try_into()?;
        let ciphertext_dest = self.ciphertext_dest.try_into()?;

        self.proof.verify(
            &pubkey_withdraw_withheld_authority,
            &pubkey_dest,
            &ciphertext_withdraw_withheld_authority,
            &ciphertext_dest,
            &mut transcript,
        )
    }
}

/// This struct represents the cryptographic proof component that certifies the account's solvency
/// for withdrawal
#[derive(Clone, Copy, Pod, Zeroable)]
#[repr(C)]
#[allow(non_snake_case)]
pub struct WithdrawWithheldTokensProof {
    pub proof: pod::CtxtCtxtEqualityProof,
}

#[allow(non_snake_case)]
#[cfg(not(target_arch = "bpf"))]
impl WithdrawWithheldTokensProof {
    fn transcript_new(
        pubkey_withdraw_withheld_authority: &pod::ElGamalPubkey,
        pubkey_dest: &pod::ElGamalPubkey,
        ciphertext_withdraw_withheld_authority: &pod::ElGamalCiphertext,
        ciphertext_dest: &pod::ElGamalCiphertext,
    ) -> Transcript {
        let mut transcript = Transcript::new(b"WithdrawWithheldTokensProof");

        transcript.append_pubkey(
            b"withdraw-withheld-authority-pubkey",
            pubkey_withdraw_withheld_authority,
        );
        transcript.append_pubkey(b"dest-pubkey", pubkey_dest);

        transcript.append_ciphertext(
            b"ciphertext-withdraw-withheld-authority",
            ciphertext_withdraw_withheld_authority,
        );
        transcript.append_ciphertext(b"ciphertext-dest", ciphertext_dest);

        transcript
    }

    pub fn new(
        keypair_withdraw_withheld_authority: &ElGamalKeypair,
        pubkey_dest: &ElGamalPubkey,
        ciphertext_withdraw_withheld_authority: &ElGamalCiphertext,
        amount: u64,
        opening_dest: &PedersenOpening,
        transcript: &mut Transcript,
    ) -> Self {
        let equality_proof = CtxtCtxtEqualityProof::new(
            keypair_withdraw_withheld_authority,
            pubkey_dest,
            ciphertext_withdraw_withheld_authority,
            amount,
            opening_dest,
            transcript,
        );

        Self {
            proof: equality_proof.into(),
        }
    }

    pub fn verify(
        &self,
        pubkey_source: &ElGamalPubkey,
        pubkey_dest: &ElGamalPubkey,
        ciphertext_source: &ElGamalCiphertext,
        ciphertext_dest: &ElGamalCiphertext,
        transcript: &mut Transcript,
    ) -> Result<(), ProofError> {
        let proof: CtxtCtxtEqualityProof = self.proof.try_into()?;
        proof.verify(
            pubkey_source,
            pubkey_dest,
            ciphertext_source,
            ciphertext_dest,
            transcript,
        )?;

        Ok(())
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_close_account_correctness() {
        let keypair_withdraw_withheld_authority = ElGamalKeypair::new_rand();
        let keypair_dest = ElGamalKeypair::new_rand();

        let amount: u64 = 55;
        let ciphertext_withdraw_withheld_authority =
            keypair_withdraw_withheld_authority.public.encrypt(amount);

        let withdraw_withheld_tokens_data = WithdrawWithheldTokensData::new(
            &keypair_withdraw_withheld_authority,
            &keypair_dest.public,
            &ciphertext_withdraw_withheld_authority,
            amount,
        )
        .unwrap();

        assert!(withdraw_withheld_tokens_data.verify().is_ok());
    }
}