safe_zk_token_sdk/instruction/
mod.rs

1pub mod close_account;
2pub mod pubkey_validity;
3pub mod transfer;
4pub mod transfer_with_fee;
5pub mod withdraw;
6pub mod withdraw_withheld;
7
8#[cfg(not(target_os = "solana"))]
9use {
10    crate::{
11        encryption::{
12            elgamal::ElGamalCiphertext,
13            pedersen::{PedersenCommitment, PedersenOpening},
14        },
15        errors::ProofError,
16    },
17    curve25519_dalek::scalar::Scalar,
18};
19pub use {
20    close_account::CloseAccountData, pubkey_validity::PubkeyValidityData, transfer::TransferData,
21    transfer_with_fee::TransferWithFeeData, withdraw::WithdrawData,
22    withdraw_withheld::WithdrawWithheldTokensData,
23};
24
25#[cfg(not(target_os = "solana"))]
26pub trait Verifiable {
27    fn verify(&self) -> Result<(), ProofError>;
28}
29
30#[cfg(not(target_os = "solana"))]
31#[derive(Debug, Copy, Clone)]
32pub enum Role {
33    Source,
34    Destination,
35    Auditor,
36    WithdrawWithheldAuthority,
37}
38
39/// Takes in a 64-bit number `amount` and a bit length `bit_length`. It returns:
40///  - the `bit_length` low bits of `amount` interpretted as u64
41///  - the (64 - `bit_length`) high bits of `amount` interpretted as u64
42#[cfg(not(target_os = "solana"))]
43pub fn split_u64(amount: u64, bit_length: usize) -> (u64, u64) {
44    if bit_length == 64 {
45        (amount, 0)
46    } else {
47        let lo = amount << (64 - bit_length) >> (64 - bit_length);
48        let hi = amount >> bit_length;
49        (lo, hi)
50    }
51}
52
53#[cfg(not(target_os = "solana"))]
54pub fn combine_lo_hi_u64(amount_lo: u64, amount_hi: u64, bit_length: usize) -> u64 {
55    if bit_length == 64 {
56        amount_lo
57    } else {
58        amount_lo + (amount_hi << bit_length)
59    }
60}
61
62#[cfg(not(target_os = "solana"))]
63fn combine_lo_hi_ciphertexts(
64    ciphertext_lo: &ElGamalCiphertext,
65    ciphertext_hi: &ElGamalCiphertext,
66    bit_length: usize,
67) -> ElGamalCiphertext {
68    let two_power = (1_u64) << bit_length;
69    ciphertext_lo + &(ciphertext_hi * &Scalar::from(two_power))
70}
71
72#[cfg(not(target_os = "solana"))]
73pub fn combine_lo_hi_commitments(
74    comm_lo: &PedersenCommitment,
75    comm_hi: &PedersenCommitment,
76    bit_length: usize,
77) -> PedersenCommitment {
78    let two_power = (1_u64) << bit_length;
79    comm_lo + comm_hi * &Scalar::from(two_power)
80}
81
82#[cfg(not(target_os = "solana"))]
83pub fn combine_lo_hi_openings(
84    opening_lo: &PedersenOpening,
85    opening_hi: &PedersenOpening,
86    bit_length: usize,
87) -> PedersenOpening {
88    let two_power = (1_u64) << bit_length;
89    opening_lo + opening_hi * &Scalar::from(two_power)
90}