Introducing a new way to review your PRs.

Read the announcement

solana-foundation / solana-go

Go SDK library and RPC client for the Solana Blockchain

View on GitHub

26 documents · Updated Nov 16, 2026

docs / zk-elgamal-proof-program

Zero-knowledge ElGamal proof program APIs

The zero-knowledge ElGamal proof package prepares encrypted amounts, zero-knowledge proofs, and Solana instructions that a confidential-token flow can check without revealing those amounts. It joins ElGamal encryption, Pedersen commitments, proof generation, proof checks, and proof-program instruction encoding through a small Go API. The package supplies proof data and verification instructions for higher-level program flows; it does not replace the instruction builders described in On-chain program instruction builders.

System role

The package sits between confidential-token account data and the proof instructions that carry and check that data. Callers keep the secret keys, openings, and plaintext witness values needed to create a proof. The resulting fixed binary proof data can be checked before submission and can be placed in an inline verification instruction or a proof account.

Confidential value model

An ElGamal ciphertext hides an amount under one public key. A Pedersen commitment hides the same kind of amount while allowing a proof to relate it to another value. A Pedersen opening is the secret randomness that makes a commitment and its proof valid. Grouped ciphertexts give the same amount to two or three public keys, such as a source, a recipient, and an optional auditor.

The package also keeps an authenticated-encryption form of an amount for an account owner. This form lets a proof builder learn a current balance for checks while the ElGamal form remains the value used by confidential account operations.

ValueRole in a confidential flow
ElGamal keypairHolds the secret needed to decrypt a ciphertext and to prove knowledge of that secret.
ElGamal ciphertextHides an amount and supports addition, subtraction, and addition or subtraction of a known amount.
Pedersen commitment and openingRepresent a hidden amount and its witness for equality and range proofs.
Grouped ciphertextEncrypts one amount for two or three public keys with a shared opening.
Authenticated ciphertextLets the holder recover a balance for local spend checks.

References: programs/…/doc.go• L1-14 programs/…/types.go• L13-16 programs/…/types.go• L51-96 programs/…/ae.go• L8-22

Package boundaries

The public API is split by responsibility. encryption creates and transforms encrypted values. proofdata creates and checks individual proof payloads. confidential combines those pieces into the proof sets required by mint, transfer, burn, and withdraw operations. zkprogram turns proof data into instructions and describes the on-chain proof context that other instructions can read. The parent page covers how these payloads relate to the wider instruction-builder set. Token, Token-2022, and associated token APIs covers the token program that consumes confidential-token data. Lookup tables and auxiliary program APIs covers other supporting program builders.

The package supports the following high-level uses:

  • Create or derive ElGamal keys.
  • Encrypt, decrypt, combine, and subtract hidden amounts.
  • Create commitments and openings for proof witnesses.
  • Build proof data for a single statement or a confidential operation.
  • Verify proof data locally through the same proof system used by the bridge.
  • Encode proof verification and context-state instructions for a Solana transaction.

References: programs/…/doc.go• L1-4 programs/…/proof_data.go• L12-26 programs/…/mint.go• L23-33 programs/…/transfer.go• L21-36 programs/…/instruction.go• L57-117

Encryption primitives

The encryption API provides the value operations that proof builders compose. These operations preserve the relation between hidden ciphertexts and their plaintext amounts, so a builder can compute a new balance without exposing the old balance or the change amount.

ElGamal operations

NewElGamalKeypair creates a random keypair. ElGamalKeypairFromSecret derives the same public key from a supplied secret, which supports wallet-bound key use. A public key can encrypt an amount with a fresh opening or with a caller-supplied opening. A matching keypair can decrypt values known to fit the package's 32-bit decryption path.

Ciphertexts under the same public key support homomorphic addition and subtraction. The API also combines low and high ciphertext parts as lo + hi × 2^bitLength. This split form is central to confidential transfers because the amount is carried as a 16-bit low part and a 32-bit high part.

References: programs/…/elgamal.go• L8-41 programs/…/elgamal.go• L44-67 programs/…/constants.go• L5-16

Pedersen operations

Pedersen operations create a commitment and a fresh opening for an amount or reuse a supplied opening. The API combines low and high commitments and openings with the same power-of-two rule used for ciphertexts. It also subtracts commitments and openings so a proof can describe a balance change or a net transfer amount.

The commitment and its opening must stay paired. A proof that uses a commitment with the wrong opening or amount will not establish the intended relation, even when the byte shape is valid.

References: programs/…/pedersen.go• L5-39 programs/…/types.go• L58-81 programs/…/utils.go• L137-168

Grouped encryption

Grouped encryption gives several key holders a decrypt handle for one amount. The two-handle and three-handle forms use the same Pedersen opening and can later expose one regular ElGamal ciphertext for a selected handle. The handle index is the position of the public key at encryption time.

The three-handle form is used for the source, destination, and auditor roles. If a mint has no auditor, the confidential builders use the identity public key in the third position so the proof layout stays the same.

References: programs/…/grouped.go• L9-35 programs/…/utils.go• L8-15 programs/…/utils.go• L46-86

Authenticated amount encryption

The AES-GCM-SIV API protects a uint64 amount for a holder who has the matching key. Confidential transfer, burn, and fee flows decrypt this value only to learn the current available balance and reject an amount that is too large. It is separate from the ElGamal ciphertext that the confidential-token state uses.

References: programs/…/ae.go• L8-22 programs/…/transfer.go• L25-47 programs/…/burn.go• L21-45

Proof data

Proof data joins the public context with a proof byte sequence and a proof-type tag. The context states what is being proved. The proof hides the witness while showing that the statement holds. Bytes produces the fixed-layout payload for a proof instruction, UnmarshalBinary reads that layout, and Verify checks it against the context.

Proof statement families

The package exposes proof families for key facts about confidential values. Each family has a typed context and a fixed-size proof value.

StatementWhat it establishes
Public-key validityThe prover knows the secret key for a public key.
Zero ciphertextA ciphertext encrypts zero under a public key.
Ciphertext-to-commitment equalityA ciphertext and a Pedersen commitment encode the same amount.
Ciphertext-to-ciphertext equalityTwo ciphertexts under different keys encode the same amount.
Percentage with capA fee is the requested percentage or the configured maximum.
Batched rangeEach committed amount fits its assigned bit length.
Grouped-ciphertext validityA grouped ciphertext is a valid encryption for all of its handles.

The range API supports total widths of 64, 128, or 256 bits. A batch has at most eight commitments. The assigned bit lengths must match the proof width, so the caller defines the value bounds as part of the proof context.

References: programs/…/proof_data.go• L32-46 programs/…/proof_data.go• L53-62 programs/…/proof_data.go• L147-180 programs/…/proofs.go• L62-99

Binary proof payloads

The proof-data API uses plain fixed-size byte layouts that match the Rust proof program. Serialization copies the complete typed value into a new byte buffer. Parsing accepts only a buffer whose length exactly matches that type, which protects the instruction boundary from truncated or extended proof data.

Each proof type has a numeric tag and a matching proof-data structure. The tag selects the verifier and the structure supplies the public context and proof bytes. The Go and Rust sides must therefore keep tags, field order, and sizes aligned.

References: programs/…/pod.go• L9-20 programs/…/proof_data.go• L13-26 programs/…/proof_data.go• L353-410

Generation and verification flow

Proof generation receives the witness values that the caller already knows. It returns proof data that repeats the public context in the on-chain layout. Verification uses only that proof data and its context, so a caller can reject a bad proof before building or submitting a transaction.

The normal relationship between encryption values, proof generation, local verification, and a higher-level instruction is:

flowchart LR
    A[Keys and plaintext witness] --> B[Ciphertexts and commitments]
    B --> C[Proof-data generator]
    C --> D[Typed proof data]
    D --> E[Local Verify]
    D --> F[Confidential instruction payload]
    F --> G[On-chain proof instruction]

A proof can fail during generation when its inputs are malformed or inconsistent. It can also generate successfully and then fail verification when the context, proof bytes, amount, or opening do not match. The caller should treat Verify as the final check of the exact bytes that will cross the program boundary.

References: programs/…/proof_data.go• L12-29 programs/…/proofs.go• L10-60 programs/…/proofs_test.go• L26-46 programs/…/proofs_test.go• L152-181

Range and handle proofs

Range proofs certify that hidden amounts fit in stated bounds. Confidential balance changes use a 128-bit proof with a 64-bit final balance, a 16-bit low amount, a 32-bit high amount, and a zero pad. Fee transfers use a 256-bit proof because they also cover fee parts, the fee rounding delta, and the net amount.

Grouped-handle proofs certify that every recipient of a grouped ciphertext sees a valid encryption. Batched grouped proofs apply that check to the low and high parts of a split amount. The confidential builders extract the auditor's two single-key ciphertexts from the three-handle proof context when an auditor is present.

References: programs/…/utils.go• L46-86 programs/…/utils.go• L118-131 programs/…/utils.go• L211-232 programs/…/transfer_with_fee.go• L142-168

Proof program instruction APIs

The zkprogram package maps typed proof data to instructions for the deployed zero-knowledge ElGamal proof program. It also registers the program ID with the general Solana instruction decoder, so encoded proof instructions can be recognized when transactions are decoded.

Verification instruction forms

Every supported proof has a matching verification instruction. The instruction can carry the complete proof inline or point to proof bytes stored in another account at a byte offset. Either form can also name a context state account that receives or supplies the verified public statement.

The instruction builder checks that the instruction kind and proof-data type agree before it encodes the discriminant and payload. The separate CloseContextState instruction closes a context state account and sends its lamports to a destination when the context authority signs.

References: programs/…/instruction.go• L12-49 programs/…/instruction.go• L57-117 programs/…/zkprogram.go• L8-22

Proof locations

ProofLocation describes how a confidential-token instruction finds a proof. An instruction-offset location refers to a proof carried by a sibling instruction and keeps the proof data in the builder. A context-state location refers to an account that already stores the verified context. ConfidentialTransferProofLocation selects the context-state form when the caller supplies an account; otherwise it selects the instruction-offset form.

The zero value is not a valid location. An offset location needs proof data. A context-state location needs a non-zero account. This choice lets higher-level token builders use the same proof contract for inline proof instructions and for proofs verified earlier in a transaction.

References: programs/…/proof_location.go• L12-35 programs/…/proof_location.go• L37-65 programs/…/proof_location.go• L76-82 programs/…/proof_location_test.go• L10-30

Proof context state

A proof context state account is an on-chain receipt of a verified public statement. Its data records the authority that may close the account, the proof type, and the serialized context. A later instruction can use the account instead of carrying the full proof again.

The account must be allocated to the exact size of its metadata and context before verification. After the consuming instruction no longer needs the receipt, the authority can close the account and recover its lamports. The account parser accepts the uninitialized type for an empty state and rejects unknown proof types or context lengths that do not match the selected proof.

References: programs/…/state.go• L11-35 programs/…/state.go• L38-67 programs/…/instruction.go• L51-67 programs/…/example_test.go• L12-49

Rust encoding parity

The Go instruction and state APIs follow the Rust proof-program interface. Rust parity vectors cover every proof type, both proof locations, context state encoding, account flags, and the close instruction. The parity generators also emit confidential-token builder vectors for standard token-2022 transfers, fee transfers, and confidential mint and burn operations. These vectors check the shared proof-location contract at each caller boundary.

The generators use pinned Rust interface crates and emit deterministic JSON instruction descriptions for Go parity tests. Shared helpers provide stable patterned accounts and byte values, while each generator covers offset, context-state, multisignature, and operation-specific variants where the caller API supports them.

These vectors act as a compatibility check rather than as a runtime dependency. A Rust interface change must produce new vectors and a matching Go result before the two clients can safely exchange instructions.

References: programs/…/rust_parity_test.go• L61-159 programs/…/lib.rs• L1-66 programs/…/zkprogram.rs• L1-15 programs/…/zkprogram.rs• L57-95 programs/…/token2022.rs• L1-17 programs/…/mintburn.rs• L1-17 programs/…/transferfee.rs• L1-14

Confidential operation builders

The confidential package turns an operation's current encrypted state and private witness values into the proof set expected by a confidential-token instruction. Each builder checks the local balance rules first, then creates ciphertext validity, balance equality, fee, and range proofs as needed.

Balance-changing operations

Transfer, mint, and burn share one balance-change pattern. The builder splits the amount into low and high parts. It encrypts both parts for the operation's key roles. It combines the source or supply handle and proves that adding or subtracting that ciphertext produces a commitment to the new balance. Mint also rejects a result that does not fit in the 64-bit balance model.

BuilderBalance relationProof set
TransferSplitProofDataCurrent source balance minus transfer amount equals the new balance.Equality, three-handle amount validity, and 128-bit range.
MintSplitProofDataCurrent supply plus mint amount equals the new supply.Equality, three-handle amount validity, and 128-bit range.
BurnSplitProofDataCurrent source balance minus burn amount equals the new balance.Equality, three-handle amount validity, and 128-bit range.

The builders reject an amount above the 48-bit encrypted amount limit. Transfer and burn also reject a spend above the balance. Mint rejects a balance increase above the 64-bit balance limit and reports that overflow as ErrIllegalAmountBitLength. A missing auditor does not change the proof shape because the third handle uses the identity key.

References: programs/…/utils.go• L171-232 programs/…/transfer.go• L9-62 programs/…/mint.go• L10-59 programs/…/burn.go• L9-59 programs/…/constants.go• L5-16 programs/…/errors.go• L5-14

Withdrawal operation

NewWithdrawProofData handles a withdrawal whose amount is public. It subtracts that amount from the current ElGamal balance. It then proves that the resulting ciphertext matches a commitment to the remaining balance and that the remaining balance fits in 64 bits.

Unlike transfer, burn, and fee transfer, this builder receives the current balance as plaintext. It does not need an authenticated balance ciphertext to perform its local spend check.

References: programs/…/withdraw.go• L8-56

Fee-bearing transfer

TransferWithFeeSplitProofData extends the transfer pattern for a fee-enabled mint. It proves the new source balance and the transfer amount. It also encrypts the fee for the destination and withdraw-withheld-authority keys. A percentage-with-cap proof binds the fee to the transfer amount and fee rate. The final range proof covers the balance, transfer parts, fee parts, rounding values, and net amount.

The fee is rounded up and then limited by the maximum fee. When the maximum applies, the builder uses the capped branch of the fee proof. The fee cannot exceed the transfer amount.

References: programs/…/transfer_with_fee.go• L10-48 programs/…/transfer_with_fee.go• L49-79 programs/…/transfer_with_fee.go• L94-169 programs/…/constants.go• L18-27

Runtime boundary

The Go API uses an embedded Rust proof engine compiled to WASM. The bridge is the boundary between typed Go values and the proof engine's exported operations. This keeps the public package focused on value types and proof contracts while the Rust side supplies the cryptographic proof system.

WASM call boundary

The bridge passes scalar integers directly and passes keys, ciphertexts, commitments, openings, and serialized proof data as byte buffers. It checks the returned byte size before decoding a typed result. A successful proof result becomes a Go proof-data value. A status-only result becomes a Go error or a successful return.

Each call uses one pooled WASM instance. Calls wait for an available instance, so the pool bounds concurrent proof work. The configured concurrency value must be set before the first proof call because it determines the pool size.

References: programs/…/wasm_call.go• L16-69 programs/…/wasm_call.go• L180-255 programs/…/wasm.go• L17-33 programs/…/wasm.go• L61-111

Runtime configuration

SetMaxConcurrency sets the maximum number of proof calls that can run at the same time. The default is the host CPU count. Values below one are ignored. A later setting has no effect after the first proof has caused the WASM runtime and its instance pool to initialize.

Use a lower value when proof work must share CPU or memory with other application work. Use the default when the process can use the available CPU capacity for proof generation.

References: programs/…/config.go• L8-27 programs/…/wasm.go• L53-59

Error contract

Errors preserve the main boundary between local input checks and proof-engine results. The root package maps proof-engine status codes to sentinel errors. The confidential package adds operation-level errors for balance, amount-size, fee, and balance-overflow rules.

Error classMeaning
ErrBadInputThe bridge received invalid encoded data.
ErrProofGenerationThe witness could not produce a proof.
ErrProofVerificationThe proof does not establish its statement.
ErrDecryptionAn authenticated or ElGamal decryption failed.
ErrOutOfMemoryThe WASM guest could not allocate the needed space.
ErrNotEnoughFundsA confidential spend exceeds the recovered balance.
ErrIllegalAmountBitLengthAn encrypted amount is above the supported 48-bit limit.
ErrFeeCalculationA fee would not produce a valid fee transfer.
ErrBalanceOverflowA balance increase does not fit in the supported 64-bit balance.

References: programs/…/errors.go• L8-49 programs/…/errors.go• L5-14 programs/…/utils.go• L23-31

Secret-material lifetime

The Rust proof engine clears temporary guest buffers after calls. Go secret types are fixed-size value arrays and are not zeroized by the Go runtime. Keep ElGamal secrets, Pedersen openings, and authenticated-encryption keys out of logs, dumps, and long-lived caches. Re-derive a secret when that is safer than storing another copy.

References: programs/…/doc.go• L6-14 programs/…/wasm_call.go• L22-34 programs/…/wasm_call.go• L72-77 programs/…/wasm_call.go• L112-133

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the Go proof type, proof-type tag, field order, and Rust verifier layout aligned. A byte-compatible payload can still select the wrong verifier if its tag changes.
  • Keep low/high amount widths aligned across amount splitting, ciphertext recombination, grouped-handle proofs, and every confidential range proof.
  • Keep the 64-bit balance limit aligned with mint overflow checks and the balance fields covered by range proofs.
  • When changing fee math, update the fee commitment relation, percentage-with-cap inputs, delta bounds, net amount, and the 256-bit range layout together.
  • When changing the third grouped handle, preserve the no-auditor identity-key behavior and the auditor ciphertext extraction index.
  • Keep verification instruction discriminants, proof-data types, and context-state proof-type bytes aligned across Go and Rust.
  • When changing context layouts, update exact account sizing, context parsing, instruction payload lengths, and the Rust parity vectors together.
  • Preserve the distinction between an instruction-offset proof and a context-state proof when changing token-program builders or ProofLocation validation.
  • Keep every token-2022 parity generator and its checked-in JSON fixture aligned when a caller instruction, account order, signer layout, or proof-location argument changes.
  • Keep the parity generator's pinned Rust interface versions and shared vector encoding helpers aligned with the Go parity reader.
  • Set concurrency before the first proof call when changing runtime capacity. The first call fixes the WASM pool size for the process.
  • Rebuild the checked-in WASM blob from the pinned Rust sources when the Rust bridge exports or its proof dependencies change.
  • Treat changes to secret-value copying or buffer cleanup as security changes because Go secret arrays are not zeroized.

Important failure modes

TriggerConsequenceGuard
A ciphertext and commitment use different amounts or openings.Proof generation fails or the generated proof fails verification.Build both values from the same plaintext witness and call Verify on the final proof data.
A batched range proof has unequal inputs or bit lengths that do not sum to 64, 128, or 256.The builder rejects the request before proof work.Keep each commitment, amount, bit length, and opening at the same index.
A proof instruction carries a proof-data type that does not match its discriminant.Instruction encoding fails and no invalid proof instruction is built.Pair each ProofInstruction with the matching ProofType.
A context state account has the wrong size or context type.Account parsing or on-chain verification rejects the state.Allocate ContextStateSize bytes and keep the metadata tag aligned with the context.
A proof location is empty or an offset location has no proof data.A confidential-token builder rejects the proof reference.Select either a non-zero context account or a non-zero instruction offset with proof data.
A transfer or burn exceeds the recovered balance.The confidential builder returns ErrNotEnoughFunds.Decrypt the authenticated balance with the matching key and apply the spend check.
An encrypted transfer amount exceeds 48 bits.The operation builder returns ErrIllegalAmountBitLength.Apply the package amount limit before splitting the amount.
A mint would make the confidential balance exceed 64 bits.The operation builder returns ErrIllegalAmountBitLength.Check the resulting balance against the package balance width before proof generation.
A fee exceeds the transfer amount or uses inconsistent fee witnesses.Fee proof construction returns an error or verification fails.Apply the cap and verify the fee, delta, complement, and net-amount relations together.
A caller instruction changes without regenerating its Rust parity vector.Go parity tests fail or the Go and Rust clients may disagree about token-2022 instruction bytes.Regenerate the matching mint/burn, transfer, or fee vector and compare it with the Go fixture.
Rust parity vectors do not match the Go encoding.Compatibility tests fail and the clients may disagree about instruction or state bytes.Regenerate vectors from the pinned Rust interface and review the Go encoding change.
The WASM blob does not match the pinned Rust sources.Reproducible generation checks fail and the bridge may not match the Go API.Run the package generation check after bridge or Rust changes.
A caller changes concurrency after the first proof call.The active WASM pool keeps its earlier size.Call SetMaxConcurrency during process setup.

Key files

FileWhy to read it
programs/…/doc.goDefines the encryption package scope and its secret-material warning.
programs/…/elgamal.goShows the public ElGamal key and ciphertext operations.
programs/…/pedersen.goShows commitment and opening operations used by proofs.
programs/…/grouped.goShows multi-recipient ciphertexts and handle extraction.
programs/…/proof_data.goDefines proof contexts, proof types, serialization, and verification.
programs/…/pod.goDefines the exact-size byte boundary for proof data and contexts.
programs/…/proofs.goMaps proof-generation APIs to the supported proof statements.
programs/…/utils.goConnects amount splitting, grouped encryption, balance changes, and range proofs.
programs/…/transfer.goShows the standard confidential transfer proof set.
programs/…/transfer_with_fee.goShows fee proof composition and the wider range proof.
programs/…/mint.goShows confidential supply-increase proof composition and overflow handling.
programs/…/burn.goShows confidential supply-decrease proof composition.
programs/…/withdraw.goShows the public-amount withdrawal proof path.
programs/…/constants.goDefines balance, amount, and fee widths used by confidential proofs.
programs/…/errors.goDefines operation-level errors for balance, amount, fee, and overflow rules.
programs/…/instruction.goDefines proof verification and context-close instruction forms.
programs/…/proof_location.goDefines the two ways a consuming instruction locates a proof.
programs/…/state.goDefines the serialized proof context state account.
programs/…/rust_parity_test.goChecks Go proof, instruction, account, and state encodings against Rust vectors.
programs/…/lib.rsProvides shared deterministic values and JSON output for Rust caller vectors.
programs/…/zkprogram.rsGenerates the Rust compatibility vectors for the proof program API.
programs/…/token2022.rsGenerates shared proof-location vectors for token-2022 confidential account and transfer callers.
programs/…/mintburn.rsGenerates token-2022 confidential mint and burn instruction vectors.
programs/…/transferfee.rsGenerates token-2022 confidential fee configuration and withheld-token instruction vectors.
programs/…/Cargo.tomlPins the Rust interface crates used to generate compatibility vectors.
programs/…/config.goDefines proof concurrency configuration and its initialization rule.
programs/…/wasm.goDefines the embedded WASM runtime and instance pool boundary.
programs/…/wasm_call.goDefines argument, result, memory, and error handling across the bridge.
programs/…/verify.rsShows how proof tags map to on-chain proof-data types at the Rust boundary.
programs/…/main.goExplains how the embedded bridge blob is rebuilt and checked.