solana-zk-token-sdk cryptographic primitives and exposes them via wasm-bindgen for use in browsers and Node.js.
Why Off-Chain?
Zero-knowledge proof generation is computationally expensive (elliptic curve operations, multi-exponentiation). Performing this on-chain would exceed Solana’s compute unit limits. Instead:- Client generates proof — Using x0-zk-proofs in WASM (~50ms per proof)
- Client submits proof — Proof data included in transaction instruction
- On-chain verifier checks — x0-zk-verifier validates the proof (much cheaper than generation)
Installation
npm / yarn
From source
WASM-Exported Functions
version()
Returns the crate version string.
generate_pubkey_validity_proof(elgamal_keypair)
Generates a proof that an ElGamal public key is correctly derived from its secret.
Output: 64-byte
PubkeyValidityData proof
generate_withdraw_proof(elgamal_keypair, balance_ciphertext, withdraw_amount)
Generates a proof that a withdrawal is valid against an encrypted balance.
elgamal_keypair— 64-byte keypairbalance_ciphertext— 64-byte ElGamal ciphertext of current balancewithdraw_amount—u64amount to withdraw
proofData (160 bytes) and newBalance (BigInt)
generate_zero_balance_proof(elgamal_keypair, balance_ciphertext)
Generates a proof that an encrypted balance is zero.
decrypt_elgamal_u64(elgamal_keypair, ciphertext)
Decrypts an ElGamal ciphertext to recover the underlying u64 value.
Internal Architecture
Dependencies
| Crate | Purpose |
|---|---|
solana-zk-token-sdk | ElGamal encryption, proof generation types |
wasm-bindgen | Rust ↔ JavaScript FFI |
js-sys | JavaScript object construction (for return types) |
bytemuck | Zero-copy proof data serialization |
Error Handling
All functions return JavaScript errors viaJsValue. The internal error type:
SDK Integration
The TypeScript SDK wraps these WASM functions in a higher-level API:Test Coverage
The crate includes 47 native unit tests and 14 WASM integration tests covering:- ElGamal keypair reconstruction and roundtrip encryption/decryption
- Proof generation for all four proof types
- Edge cases: zero amounts, maximum amounts, wrong keypairs, invalid inputs
- Full lifecycle: keypair generation → proof creation → bytemuck serialization