UNPKG

@ghostspeak/sdk

Version:

TypeScript SDK for GhostSpeak AI Agent Commerce Protocol - Production Ready Beta

222 lines (215 loc) 9.98 kB
import { Address } from '@solana/addresses'; /** * Unified ElGamal Encryption Module * * This module consolidates all ElGamal implementations into a single, optimized solution * for client-side privacy features. * * Features: * - Twisted ElGamal encryption on curve25519 * - Bulletproof range proofs (0 to 2^64) * - Validity and equality proofs * - Homomorphic addition/subtraction * - WASM optimization support */ interface ElGamalKeypair { publicKey: Uint8Array; secretKey: Uint8Array; } interface ElGamalCiphertext { commitment: PedersenCommitment; handle: DecryptHandle; } interface PedersenCommitment { commitment: Uint8Array; } interface DecryptHandle { handle: Uint8Array; } interface RangeProof { proof: Uint8Array; commitment: Uint8Array; } interface ValidityProof { proof: Uint8Array; } interface EqualityProof { proof: Uint8Array; } interface TransferProof { rangeProof: RangeProof; validityProof: ValidityProof; equalityProof: EqualityProof; } interface WithdrawProof { proof: Uint8Array; } declare const PROOF_SIZES: { readonly RANGE_PROOF: 674; readonly VALIDITY_PROOF: 160; readonly EQUALITY_PROOF: 192; readonly WITHDRAW_PROOF: 80; readonly ZERO_BALANCE_PROOF: 96; readonly FEE_SIGMA_PROOF: 256; readonly PUBKEY_VALIDITY_PROOF: 64; }; /** * Generate a new ElGamal keypair with cryptographic validation */ declare function generateKeypair(): ElGamalKeypair; /** * Derive ElGamal keypair from seed with cryptographic validation */ declare function deriveKeypair(seed: Uint8Array): ElGamalKeypair; /** * Encrypt a value using twisted ElGamal */ declare function encrypt(publicKey: Uint8Array, value: bigint): { ciphertext: ElGamalCiphertext; randomness: Uint8Array; }; /** * Decrypt an ElGamal ciphertext (brute force for small values) */ declare function decrypt(secretKey: Uint8Array, ciphertext: ElGamalCiphertext, maxValue?: number): bigint | null; /** * Add two ElGamal ciphertexts (homomorphic addition) with constant-time operations */ declare function addCiphertexts(ct1: ElGamalCiphertext, ct2: ElGamalCiphertext): ElGamalCiphertext; /** * Subtract two ElGamal ciphertexts (homomorphic subtraction) with constant-time operations */ declare function subtractCiphertexts(ct1: ElGamalCiphertext, ct2: ElGamalCiphertext): ElGamalCiphertext; /** * Generate range proof for encrypted amount with input validation */ declare function generateRangeProof(value: bigint, commitment: PedersenCommitment, randomness: Uint8Array): Promise<RangeProof>; /** * Generate validity proof for ciphertext with comprehensive validation */ declare function generateValidityProof(publicKey: Uint8Array, ciphertext: ElGamalCiphertext, randomness: Uint8Array): Promise<ValidityProof>; /** * Generate equality proof for transfer with comprehensive validation */ declare function generateEqualityProof(sourceCiphertext: ElGamalCiphertext, destCiphertext: ElGamalCiphertext, transferAmount: bigint, sourceRandomness: Uint8Array, destRandomness: Uint8Array): Promise<EqualityProof>; /** * Generate complete transfer proof */ declare function generateTransferProof(sourceBalance: bigint, transferAmount: bigint, sourceKeypair: ElGamalKeypair, destPubkey: Uint8Array, _auditorPubkey?: Uint8Array): Promise<TransferProof>; /** * Generate withdraw proof with comprehensive validation */ declare function generateWithdrawProof(balance: bigint, keypair: ElGamalKeypair, ciphertext: ElGamalCiphertext): Promise<WithdrawProof>; /** * Convert ElGamal public key to Solana address format */ declare function elGamalPubkeyToAddress(pubkey: Uint8Array): Promise<Address>; /** * Load WASM module for performance optimization */ declare function loadWasmModule$1(): Promise<void>; declare const _default$1: { generateKeypair: typeof generateKeypair; deriveKeypair: typeof deriveKeypair; encrypt: typeof encrypt; decrypt: typeof decrypt; addCiphertexts: typeof addCiphertexts; subtractCiphertexts: typeof subtractCiphertexts; generateRangeProof: typeof generateRangeProof; generateValidityProof: typeof generateValidityProof; generateEqualityProof: typeof generateEqualityProof; generateTransferProof: typeof generateTransferProof; generateWithdrawProof: typeof generateWithdrawProof; elGamalPubkeyToAddress: typeof elGamalPubkeyToAddress; loadWasmModule: typeof loadWasmModule$1; PROOF_SIZES: { readonly RANGE_PROOF: 674; readonly VALIDITY_PROOF: 160; readonly EQUALITY_PROOF: 192; readonly WITHDRAW_PROOF: 80; readonly ZERO_BALANCE_PROOF: 96; readonly FEE_SIGMA_PROOF: 256; readonly PUBKEY_VALIDITY_PROOF: 64; }; }; type elgamal_DecryptHandle = DecryptHandle; type elgamal_ElGamalCiphertext = ElGamalCiphertext; type elgamal_ElGamalKeypair = ElGamalKeypair; type elgamal_EqualityProof = EqualityProof; declare const elgamal_PROOF_SIZES: typeof PROOF_SIZES; type elgamal_PedersenCommitment = PedersenCommitment; type elgamal_RangeProof = RangeProof; type elgamal_TransferProof = TransferProof; type elgamal_ValidityProof = ValidityProof; type elgamal_WithdrawProof = WithdrawProof; declare const elgamal_addCiphertexts: typeof addCiphertexts; declare const elgamal_decrypt: typeof decrypt; declare const elgamal_deriveKeypair: typeof deriveKeypair; declare const elgamal_elGamalPubkeyToAddress: typeof elGamalPubkeyToAddress; declare const elgamal_encrypt: typeof encrypt; declare const elgamal_generateEqualityProof: typeof generateEqualityProof; declare const elgamal_generateKeypair: typeof generateKeypair; declare const elgamal_generateRangeProof: typeof generateRangeProof; declare const elgamal_generateTransferProof: typeof generateTransferProof; declare const elgamal_generateValidityProof: typeof generateValidityProof; declare const elgamal_generateWithdrawProof: typeof generateWithdrawProof; declare const elgamal_subtractCiphertexts: typeof subtractCiphertexts; declare namespace elgamal { export { type elgamal_DecryptHandle as DecryptHandle, type elgamal_ElGamalCiphertext as ElGamalCiphertext, type elgamal_ElGamalKeypair as ElGamalKeypair, type elgamal_EqualityProof as EqualityProof, elgamal_PROOF_SIZES as PROOF_SIZES, type elgamal_PedersenCommitment as PedersenCommitment, type elgamal_RangeProof as RangeProof, type elgamal_TransferProof as TransferProof, type elgamal_ValidityProof as ValidityProof, type elgamal_WithdrawProof as WithdrawProof, elgamal_addCiphertexts as addCiphertexts, elgamal_decrypt as decrypt, _default$1 as default, elgamal_deriveKeypair as deriveKeypair, elgamal_elGamalPubkeyToAddress as elGamalPubkeyToAddress, elgamal_encrypt as encrypt, elgamal_generateEqualityProof as generateEqualityProof, elgamal_generateKeypair as generateKeypair, elgamal_generateRangeProof as generateRangeProof, elgamal_generateTransferProof as generateTransferProof, elgamal_generateValidityProof as generateValidityProof, elgamal_generateWithdrawProof as generateWithdrawProof, loadWasmModule$1 as loadWasmModule, elgamal_subtractCiphertexts as subtractCiphertexts }; } /** * WASM Bridge for Cryptographic Operations * * Optional performance optimization layer that loads WASM modules * for compute-intensive cryptographic operations. */ interface WasmModule { generate_range_proof: (value: string, commitment: Uint8Array, randomness: Uint8Array) => Promise<Uint8Array>; generate_validity_proof: (publicKey: Uint8Array, commitment: Uint8Array, handle: Uint8Array, randomness: Uint8Array) => Promise<Uint8Array>; generate_equality_proof: (sourceCommitment: Uint8Array, destCommitment: Uint8Array, amount: string, sourceRandomness: Uint8Array, destRandomness: Uint8Array) => Promise<Uint8Array>; generate_withdraw_proof: (balance: string, secretKey: Uint8Array, commitment: Uint8Array, handle: Uint8Array) => Promise<Uint8Array>; scalar_multiply: (point: Uint8Array, scalar: Uint8Array) => Uint8Array; point_add: (point1: Uint8Array, point2: Uint8Array) => Uint8Array; point_subtract: (point1: Uint8Array, point2: Uint8Array) => Uint8Array; } /** * Load WASM module for cryptographic operations */ declare function loadWasmModule(): Promise<void>; /** * Check if WASM module is available */ declare function isWasmAvailable(): boolean; /** * Get WASM module instance */ declare function getWasmModule(): WasmModule | null; /** * Benchmark WASM vs JavaScript performance */ declare function benchmarkWasm(): Promise<{ wasmTime: number; jsTime: number; speedup: number; } | null>; /** * Create a function that falls back to JS if WASM fails */ declare function createWasmFallback<T extends (...args: unknown[]) => unknown>(wasmFn: T | undefined, jsFallback: T): T; declare const _default: { loadWasmModule: typeof loadWasmModule; isWasmAvailable: typeof isWasmAvailable; getWasmModule: typeof getWasmModule; benchmarkWasm: typeof benchmarkWasm; createWasmFallback: typeof createWasmFallback; }; type wasmBridge_WasmModule = WasmModule; declare const wasmBridge_benchmarkWasm: typeof benchmarkWasm; declare const wasmBridge_createWasmFallback: typeof createWasmFallback; declare const wasmBridge_getWasmModule: typeof getWasmModule; declare const wasmBridge_isWasmAvailable: typeof isWasmAvailable; declare const wasmBridge_loadWasmModule: typeof loadWasmModule; declare namespace wasmBridge { export { type wasmBridge_WasmModule as WasmModule, wasmBridge_benchmarkWasm as benchmarkWasm, wasmBridge_createWasmFallback as createWasmFallback, _default as default, wasmBridge_getWasmModule as getWasmModule, wasmBridge_isWasmAvailable as isWasmAvailable, wasmBridge_loadWasmModule as loadWasmModule }; } export { type ElGamalCiphertext, type ElGamalKeypair, type TransferProof, type WasmModule, type WithdrawProof, decrypt, elgamal, encrypt, generateKeypair, generateTransferProof, generateWithdrawProof, isWasmAvailable, loadWasmModule, wasmBridge };