@sip-protocol/sdk 0.7.2 → 0.7.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +267 -0
- package/dist/{TransportWebUSB-TQ7WZ4LE.mjs → TransportWebUSB-YQMAGJAJ.mjs} +12 -9
- package/dist/browser.d.mts +10 -4
- package/dist/browser.d.ts +10 -4
- package/dist/browser.js +48874 -18336
- package/dist/browser.mjs +674 -48
- package/dist/chunk-4GRJ5MAW.mjs +152 -0
- package/dist/chunk-5D7A3L3W.mjs +717 -0
- package/dist/chunk-64AYA5F5.mjs +7834 -0
- package/dist/chunk-GMDGB22A.mjs +379 -0
- package/dist/chunk-I534WKN7.mjs +328 -0
- package/dist/chunk-IBZVA5Y7.mjs +1003 -0
- package/dist/chunk-PRRZAWJE.mjs +223 -0
- package/dist/{chunk-UJCSKKID.mjs → chunk-XGB3TDIC.mjs} +13 -1
- package/dist/chunk-YWGJ77A2.mjs +33806 -0
- package/dist/{chunk-6WGN57S2.mjs → chunk-Z3K7W5S3.mjs} +48 -0
- package/dist/constants-LHAAUC2T.mjs +51 -0
- package/dist/dist-2OGQ7FED.mjs +3957 -0
- package/dist/dist-IFHPYLDX.mjs +254 -0
- package/dist/fulfillment_proof-ANHVPKTB.mjs +21 -0
- package/dist/funding_proof-ICFZ5LHY.mjs +21 -0
- package/dist/index-DXh2IGkz.d.ts +24681 -0
- package/dist/index-DeE1ZzA4.d.mts +24681 -0
- package/dist/index.d.mts +9 -3
- package/dist/index.d.ts +9 -3
- package/dist/index.js +48676 -17318
- package/dist/index.mjs +583 -19
- package/dist/interface-Bf7w1PLW.d.mts +679 -0
- package/dist/interface-Bf7w1PLW.d.ts +679 -0
- package/dist/{noir-DKfEzWy9.d.mts → noir-kzbLVTei.d.mts} +31 -21
- package/dist/{noir-DKfEzWy9.d.ts → noir-kzbLVTei.d.ts} +31 -21
- package/dist/proofs/halo2.d.mts +151 -0
- package/dist/proofs/halo2.d.ts +151 -0
- package/dist/proofs/halo2.js +350 -0
- package/dist/proofs/halo2.mjs +11 -0
- package/dist/proofs/kimchi.d.mts +160 -0
- package/dist/proofs/kimchi.d.ts +160 -0
- package/dist/proofs/kimchi.js +431 -0
- package/dist/proofs/kimchi.mjs +13 -0
- package/dist/proofs/noir.d.mts +1 -1
- package/dist/proofs/noir.d.ts +1 -1
- package/dist/proofs/noir.js +74 -18
- package/dist/proofs/noir.mjs +84 -24
- package/dist/solana-U3MEGU7W.mjs +280 -0
- package/dist/validity_proof-3POXLPNY.mjs +21 -0
- package/package.json +54 -21
- package/src/adapters/index.ts +41 -0
- package/src/adapters/jupiter.ts +571 -0
- package/src/adapters/near-intents.ts +135 -0
- package/src/advisor/advisor.ts +653 -0
- package/src/advisor/index.ts +54 -0
- package/src/advisor/tools.ts +303 -0
- package/src/advisor/types.ts +164 -0
- package/src/chains/ethereum/announcement.ts +536 -0
- package/src/chains/ethereum/bnb-optimizations.ts +474 -0
- package/src/chains/ethereum/commitment.ts +522 -0
- package/src/chains/ethereum/constants.ts +462 -0
- package/src/chains/ethereum/deployment.ts +596 -0
- package/src/chains/ethereum/gas-estimation.ts +538 -0
- package/src/chains/ethereum/index.ts +268 -0
- package/src/chains/ethereum/optimizations.ts +614 -0
- package/src/chains/ethereum/privacy-adapter.ts +855 -0
- package/src/chains/ethereum/registry.ts +584 -0
- package/src/chains/ethereum/rpc.ts +905 -0
- package/src/chains/ethereum/stealth.ts +491 -0
- package/src/chains/ethereum/token.ts +790 -0
- package/src/chains/ethereum/transfer.ts +637 -0
- package/src/chains/ethereum/types.ts +456 -0
- package/src/chains/ethereum/viewing-key.ts +455 -0
- package/src/chains/near/commitment.ts +608 -0
- package/src/chains/near/constants.ts +284 -0
- package/src/chains/near/function-call.ts +871 -0
- package/src/chains/near/history.ts +654 -0
- package/src/chains/near/implicit-account.ts +840 -0
- package/src/chains/near/index.ts +393 -0
- package/src/chains/near/native-transfer.ts +658 -0
- package/src/chains/near/nep141.ts +775 -0
- package/src/chains/near/privacy-adapter.ts +889 -0
- package/src/chains/near/resolver.ts +971 -0
- package/src/chains/near/rpc.ts +1016 -0
- package/src/chains/near/stealth.ts +419 -0
- package/src/chains/near/types.ts +317 -0
- package/src/chains/near/viewing-key.ts +876 -0
- package/src/chains/solana/anchor-transfer.ts +386 -0
- package/src/chains/solana/commitment.ts +577 -0
- package/src/chains/solana/constants.ts +126 -12
- package/src/chains/solana/ephemeral-keys.ts +543 -0
- package/src/chains/solana/index.ts +276 -1
- package/src/chains/solana/key-derivation.ts +418 -0
- package/src/chains/solana/kit-compat.ts +334 -0
- package/src/chains/solana/optimizations.ts +560 -0
- package/src/chains/solana/privacy-adapter.ts +605 -0
- package/src/chains/solana/providers/generic.ts +201 -0
- package/src/chains/solana/providers/helius-enhanced-types.ts +336 -0
- package/src/chains/solana/providers/helius-enhanced.ts +623 -0
- package/src/chains/solana/providers/helius.ts +402 -0
- package/src/chains/solana/providers/index.ts +85 -0
- package/src/chains/solana/providers/interface.ts +221 -0
- package/src/chains/solana/providers/quicknode.ts +409 -0
- package/src/chains/solana/providers/triton.ts +426 -0
- package/src/chains/solana/providers/webhook.ts +790 -0
- package/src/chains/solana/rpc-client.ts +1150 -0
- package/src/chains/solana/scan.ts +170 -73
- package/src/chains/solana/sol-transfer.ts +732 -0
- package/src/chains/solana/spl-transfer.ts +886 -0
- package/src/chains/solana/stealth-scanner.ts +703 -0
- package/src/chains/solana/sunspot-verifier.ts +453 -0
- package/src/chains/solana/transaction-builder.ts +755 -0
- package/src/chains/solana/transfer.ts +74 -5
- package/src/chains/solana/types.ts +77 -7
- package/src/chains/solana/utils.ts +110 -0
- package/src/chains/solana/viewing-key.ts +807 -0
- package/src/compliance/fireblocks.ts +921 -0
- package/src/compliance/index.ts +37 -0
- package/src/compliance/range-sas.ts +956 -0
- package/src/config/endpoints.ts +100 -0
- package/src/crypto.ts +11 -8
- package/src/errors.ts +82 -0
- package/src/evm/erc4337-relayer.ts +830 -0
- package/src/evm/index.ts +47 -0
- package/src/fees/calculator.ts +396 -0
- package/src/fees/index.ts +87 -0
- package/src/fees/near-contract.ts +429 -0
- package/src/fees/types.ts +268 -0
- package/src/index.ts +785 -1
- package/src/intent.ts +6 -3
- package/src/logger.ts +324 -0
- package/src/network/index.ts +80 -0
- package/src/network/proxy.ts +691 -0
- package/src/optimizations/index.ts +541 -0
- package/src/oracle/types.ts +1 -0
- package/src/privacy-backends/arcium-types.ts +727 -0
- package/src/privacy-backends/arcium.ts +719 -0
- package/src/privacy-backends/combined-privacy.ts +866 -0
- package/src/privacy-backends/cspl-token.ts +595 -0
- package/src/privacy-backends/cspl-types.ts +512 -0
- package/src/privacy-backends/cspl.ts +907 -0
- package/src/privacy-backends/health.ts +488 -0
- package/src/privacy-backends/inco-types.ts +323 -0
- package/src/privacy-backends/inco.ts +616 -0
- package/src/privacy-backends/index.ts +336 -0
- package/src/privacy-backends/interface.ts +906 -0
- package/src/privacy-backends/lru-cache.ts +343 -0
- package/src/privacy-backends/magicblock.ts +458 -0
- package/src/privacy-backends/mock.ts +258 -0
- package/src/privacy-backends/privacycash-types.ts +278 -0
- package/src/privacy-backends/privacycash.ts +456 -0
- package/src/privacy-backends/private-swap.ts +570 -0
- package/src/privacy-backends/rate-limiter.ts +683 -0
- package/src/privacy-backends/registry.ts +690 -0
- package/src/privacy-backends/router.ts +626 -0
- package/src/privacy-backends/shadowwire.ts +449 -0
- package/src/privacy-backends/sip-native.ts +256 -0
- package/src/privacy-logger.ts +191 -0
- package/src/production-safety.ts +373 -0
- package/src/proofs/aggregator.ts +1029 -0
- package/src/proofs/browser-composer.ts +1150 -0
- package/src/proofs/browser.ts +113 -25
- package/src/proofs/cache/index.ts +127 -0
- package/src/proofs/cache/interface.ts +545 -0
- package/src/proofs/cache/key-generator.ts +188 -0
- package/src/proofs/cache/lru-cache.ts +481 -0
- package/src/proofs/cache/multi-tier-cache.ts +575 -0
- package/src/proofs/cache/persistent-cache.ts +788 -0
- package/src/proofs/compliance-proof.ts +872 -0
- package/src/proofs/composer/base.ts +923 -0
- package/src/proofs/composer/index.ts +25 -0
- package/src/proofs/composer/interface.ts +518 -0
- package/src/proofs/composer/types.ts +383 -0
- package/src/proofs/converters/halo2.ts +452 -0
- package/src/proofs/converters/index.ts +208 -0
- package/src/proofs/converters/interface.ts +363 -0
- package/src/proofs/converters/kimchi.ts +462 -0
- package/src/proofs/converters/noir.ts +451 -0
- package/src/proofs/fallback.ts +888 -0
- package/src/proofs/halo2.ts +42 -0
- package/src/proofs/index.ts +471 -0
- package/src/proofs/interface.ts +13 -0
- package/src/proofs/kimchi.ts +42 -0
- package/src/proofs/lazy.ts +1004 -0
- package/src/proofs/mock.ts +25 -1
- package/src/proofs/noir.ts +111 -30
- package/src/proofs/orchestrator.ts +960 -0
- package/src/proofs/parallel/concurrency.ts +297 -0
- package/src/proofs/parallel/dependency-graph.ts +602 -0
- package/src/proofs/parallel/executor.ts +420 -0
- package/src/proofs/parallel/index.ts +131 -0
- package/src/proofs/parallel/interface.ts +685 -0
- package/src/proofs/parallel/worker-pool.ts +644 -0
- package/src/proofs/providers/halo2.ts +560 -0
- package/src/proofs/providers/index.ts +34 -0
- package/src/proofs/providers/kimchi.ts +641 -0
- package/src/proofs/validator.ts +881 -0
- package/src/proofs/verifier.ts +867 -0
- package/src/quantum/index.ts +112 -0
- package/src/quantum/winternitz-vault.ts +639 -0
- package/src/quantum/wots.ts +611 -0
- package/src/settlement/backends/direct-chain.ts +1 -0
- package/src/settlement/index.ts +9 -0
- package/src/settlement/router.ts +732 -46
- package/src/solana/index.ts +72 -0
- package/src/solana/jito-relayer.ts +687 -0
- package/src/solana/noir-verifier-types.ts +430 -0
- package/src/solana/noir-verifier.ts +816 -0
- package/src/stealth/address-derivation.ts +193 -0
- package/src/stealth/ed25519.ts +431 -0
- package/src/stealth/index.ts +233 -0
- package/src/stealth/meta-address.ts +221 -0
- package/src/stealth/secp256k1.ts +368 -0
- package/src/stealth/utils.ts +194 -0
- package/src/stealth.ts +50 -1504
- package/src/surveillance/algorithms/address-reuse.ts +143 -0
- package/src/surveillance/algorithms/cluster.ts +247 -0
- package/src/surveillance/algorithms/exchange.ts +295 -0
- package/src/surveillance/algorithms/temporal.ts +337 -0
- package/src/surveillance/analyzer.ts +442 -0
- package/src/surveillance/index.ts +64 -0
- package/src/surveillance/scoring.ts +372 -0
- package/src/surveillance/types.ts +264 -0
- package/src/sync/index.ts +106 -0
- package/src/sync/manager.ts +504 -0
- package/src/sync/mock-provider.ts +318 -0
- package/src/sync/oblivious.ts +625 -0
- package/src/tokens/index.ts +15 -0
- package/src/tokens/registry.ts +301 -0
- package/src/utils/deprecation.ts +94 -0
- package/src/utils/index.ts +9 -0
- package/src/wallet/ethereum/index.ts +68 -0
- package/src/wallet/ethereum/metamask-privacy.ts +420 -0
- package/src/wallet/ethereum/multi-wallet.ts +646 -0
- package/src/wallet/ethereum/privacy-adapter.ts +700 -0
- package/src/wallet/ethereum/types.ts +3 -1
- package/src/wallet/ethereum/walletconnect-adapter.ts +675 -0
- package/src/wallet/hardware/index.ts +10 -0
- package/src/wallet/hardware/ledger-privacy.ts +414 -0
- package/src/wallet/index.ts +71 -0
- package/src/wallet/near/adapter.ts +626 -0
- package/src/wallet/near/index.ts +86 -0
- package/src/wallet/near/meteor-wallet.ts +1153 -0
- package/src/wallet/near/my-near-wallet.ts +790 -0
- package/src/wallet/near/wallet-selector.ts +702 -0
- package/src/wallet/solana/adapter.ts +6 -4
- package/src/wallet/solana/index.ts +13 -0
- package/src/wallet/solana/privacy-adapter.ts +567 -0
- package/src/wallet/sui/types.ts +6 -4
- package/src/zcash/rpc-client.ts +13 -6
- package/dist/chunk-3INS3PR5.mjs +0 -884
- package/dist/chunk-3OVABDRH.mjs +0 -17096
- package/dist/chunk-DLDWZFYC.mjs +0 -1495
- package/dist/chunk-E6SZWREQ.mjs +0 -57
- package/dist/chunk-G33LB27A.mjs +0 -16166
- package/dist/chunk-HGU6HZRC.mjs +0 -231
- package/dist/chunk-L2K34JCU.mjs +0 -1496
- package/dist/chunk-SN4ZDTVW.mjs +0 -16166
- package/dist/constants-VOI7BSLK.mjs +0 -27
- package/dist/index-BYZbDjal.d.ts +0 -11390
- package/dist/index-CHB3KuOB.d.mts +0 -11859
- package/dist/index-CzWPI6Le.d.ts +0 -11859
- package/dist/index-xbWjohNq.d.mts +0 -11390
- package/dist/solana-5EMCTPTS.mjs +0 -46
- package/dist/solana-Q4NAVBTS.mjs +0 -46
|
@@ -0,0 +1,641 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Kimchi Proof Provider
|
|
3
|
+
*
|
|
4
|
+
* Implements ComposableProofProvider interface for Mina's Kimchi proof system.
|
|
5
|
+
* Kimchi is the proving system behind Mina Protocol, providing succinct proofs
|
|
6
|
+
* and efficient recursive composition via Pickles.
|
|
7
|
+
*
|
|
8
|
+
* Key features:
|
|
9
|
+
* - Constant-size proofs (~22KB)
|
|
10
|
+
* - Recursive proof composition (Pickles wrapper)
|
|
11
|
+
* - Efficient verification
|
|
12
|
+
* - Browser WASM support via o1js
|
|
13
|
+
*
|
|
14
|
+
* @see https://docs.minaprotocol.com/ for Kimchi documentation
|
|
15
|
+
* @packageDocumentation
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { randomBytes, bytesToHex } from '@noble/hashes/utils'
|
|
19
|
+
|
|
20
|
+
import type { HexString } from '@sip-protocol/types'
|
|
21
|
+
import {
|
|
22
|
+
ProofAggregationStrategy,
|
|
23
|
+
} from '@sip-protocol/types'
|
|
24
|
+
|
|
25
|
+
import type {
|
|
26
|
+
ComposableProofProvider,
|
|
27
|
+
} from '../composer/interface'
|
|
28
|
+
|
|
29
|
+
import type {
|
|
30
|
+
ProofSystem,
|
|
31
|
+
ProofProviderCapabilities,
|
|
32
|
+
ProofProviderStatus,
|
|
33
|
+
ProofProviderMetrics,
|
|
34
|
+
SingleProof,
|
|
35
|
+
} from '@sip-protocol/types'
|
|
36
|
+
|
|
37
|
+
import type {
|
|
38
|
+
ProofGenerationRequest,
|
|
39
|
+
ProofGenerationResult,
|
|
40
|
+
} from '../composer/types'
|
|
41
|
+
|
|
42
|
+
// ─── Configuration ──────────────────────────────────────────────────────────
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Kimchi circuit configuration
|
|
46
|
+
*/
|
|
47
|
+
export interface KimchiCircuitConfig {
|
|
48
|
+
/** Circuit identifier */
|
|
49
|
+
id: string
|
|
50
|
+
/** Circuit name */
|
|
51
|
+
name: string
|
|
52
|
+
/** Number of gates in the circuit */
|
|
53
|
+
gateCount: number
|
|
54
|
+
/** Public input count */
|
|
55
|
+
publicInputCount: number
|
|
56
|
+
/** Whether circuit uses recursion (Pickles) */
|
|
57
|
+
usesRecursion: boolean
|
|
58
|
+
/** Verification key hash (for caching) */
|
|
59
|
+
verificationKeyHash?: string
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Kimchi provider configuration
|
|
64
|
+
*/
|
|
65
|
+
export interface KimchiProviderConfig {
|
|
66
|
+
/**
|
|
67
|
+
* Pre-configured circuits
|
|
68
|
+
*/
|
|
69
|
+
circuits?: KimchiCircuitConfig[]
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Enable verbose logging
|
|
73
|
+
* @default false
|
|
74
|
+
*/
|
|
75
|
+
verbose?: boolean
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Network for proof generation
|
|
79
|
+
* @default 'testnet'
|
|
80
|
+
*/
|
|
81
|
+
network?: 'mainnet' | 'testnet' | 'local'
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Enable Pickles recursive proving
|
|
85
|
+
* @default true
|
|
86
|
+
*/
|
|
87
|
+
enablePickles?: boolean
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Proof compilation cache directory
|
|
91
|
+
*/
|
|
92
|
+
cacheDir?: string
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Number of workers for parallel compilation
|
|
96
|
+
* @default 2
|
|
97
|
+
*/
|
|
98
|
+
compileWorkers?: number
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// ─── Default Configuration ──────────────────────────────────────────────────
|
|
102
|
+
|
|
103
|
+
const DEFAULT_KIMCHI_CONFIG: Required<KimchiProviderConfig> = {
|
|
104
|
+
circuits: [],
|
|
105
|
+
verbose: false,
|
|
106
|
+
network: 'testnet',
|
|
107
|
+
enablePickles: true,
|
|
108
|
+
cacheDir: '',
|
|
109
|
+
compileWorkers: 2,
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// ─── Constants ──────────────────────────────────────────────────────────────
|
|
113
|
+
|
|
114
|
+
/** Standard Kimchi proof size (~22KB) */
|
|
115
|
+
const KIMCHI_PROOF_SIZE = 22 * 1024
|
|
116
|
+
|
|
117
|
+
/** Kimchi system version (o1js version) */
|
|
118
|
+
const KIMCHI_VERSION = '1.0.0'
|
|
119
|
+
|
|
120
|
+
// ─── Kimchi Provider ────────────────────────────────────────────────────────
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Kimchi Proof Provider
|
|
124
|
+
*
|
|
125
|
+
* Implements ComposableProofProvider for Mina's Kimchi proof system.
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* ```typescript
|
|
129
|
+
* const provider = new KimchiProvider({
|
|
130
|
+
* enablePickles: true,
|
|
131
|
+
* network: 'testnet',
|
|
132
|
+
* })
|
|
133
|
+
*
|
|
134
|
+
* await provider.initialize()
|
|
135
|
+
*
|
|
136
|
+
* const result = await provider.generateProof({
|
|
137
|
+
* circuitId: 'transfer_proof',
|
|
138
|
+
* privateInputs: { sender: '...', amount: '...' },
|
|
139
|
+
* publicInputs: { commitment: '...' },
|
|
140
|
+
* })
|
|
141
|
+
* ```
|
|
142
|
+
*/
|
|
143
|
+
export class KimchiProvider implements ComposableProofProvider {
|
|
144
|
+
readonly system: ProofSystem = 'kimchi'
|
|
145
|
+
|
|
146
|
+
private _config: Required<KimchiProviderConfig>
|
|
147
|
+
private _status: ProofProviderStatus
|
|
148
|
+
private _capabilities: ProofProviderCapabilities
|
|
149
|
+
private _metrics: ProofProviderMetrics
|
|
150
|
+
private _circuits: Map<string, KimchiCircuitConfig> = new Map()
|
|
151
|
+
private _compiledCircuits: Map<string, { compiled: boolean; vkHash?: string }> = new Map()
|
|
152
|
+
private _initPromise: Promise<void> | null = null
|
|
153
|
+
private _initError: Error | null = null
|
|
154
|
+
|
|
155
|
+
constructor(config: KimchiProviderConfig = {}) {
|
|
156
|
+
this._config = { ...DEFAULT_KIMCHI_CONFIG, ...config }
|
|
157
|
+
|
|
158
|
+
this._metrics = {
|
|
159
|
+
proofsGenerated: 0,
|
|
160
|
+
proofsVerified: 0,
|
|
161
|
+
avgGenerationTimeMs: 0,
|
|
162
|
+
avgVerificationTimeMs: 0,
|
|
163
|
+
successRate: 1,
|
|
164
|
+
memoryUsageBytes: 0,
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
this._status = {
|
|
168
|
+
isReady: false,
|
|
169
|
+
isBusy: false,
|
|
170
|
+
queueLength: 0,
|
|
171
|
+
metrics: this._metrics,
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
this._capabilities = {
|
|
175
|
+
system: 'kimchi',
|
|
176
|
+
supportsRecursion: this._config.enablePickles,
|
|
177
|
+
supportsBatchVerification: true,
|
|
178
|
+
supportsBrowser: true,
|
|
179
|
+
supportsNode: true,
|
|
180
|
+
maxProofSize: KIMCHI_PROOF_SIZE * 10, // Allow some overhead
|
|
181
|
+
supportedStrategies: [
|
|
182
|
+
ProofAggregationStrategy.SEQUENTIAL,
|
|
183
|
+
ProofAggregationStrategy.PARALLEL,
|
|
184
|
+
ProofAggregationStrategy.BATCH,
|
|
185
|
+
...(this._config.enablePickles ? [ProofAggregationStrategy.RECURSIVE] : []),
|
|
186
|
+
],
|
|
187
|
+
availableCircuits: [],
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// Pre-load circuit configs
|
|
191
|
+
for (const circuit of this._config.circuits) {
|
|
192
|
+
this._circuits.set(circuit.id, circuit)
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// ─── Properties ───────────────────────────────────────────────────────────
|
|
197
|
+
|
|
198
|
+
get capabilities(): ProofProviderCapabilities {
|
|
199
|
+
return {
|
|
200
|
+
...this._capabilities,
|
|
201
|
+
availableCircuits: Array.from(this._circuits.keys()),
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
get status(): ProofProviderStatus {
|
|
206
|
+
return { ...this._status }
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// ─── Lifecycle ────────────────────────────────────────────────────────────
|
|
210
|
+
|
|
211
|
+
async initialize(): Promise<void> {
|
|
212
|
+
if (this._status.isReady) return
|
|
213
|
+
|
|
214
|
+
if (this._initPromise) {
|
|
215
|
+
return this._initPromise
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
this._initPromise = this.doInitialize()
|
|
219
|
+
return this._initPromise
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
private async doInitialize(): Promise<void> {
|
|
223
|
+
try {
|
|
224
|
+
if (this._config.verbose) {
|
|
225
|
+
console.log('[KimchiProvider] Initializing...')
|
|
226
|
+
console.log(`[KimchiProvider] Network: ${this._config.network}`)
|
|
227
|
+
console.log(`[KimchiProvider] Pickles enabled: ${this._config.enablePickles}`)
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// TODO: Initialize o1js/snarkyjs
|
|
231
|
+
// For now, simulate initialization
|
|
232
|
+
await this.simulateAsyncLoad()
|
|
233
|
+
|
|
234
|
+
this._status.isReady = true
|
|
235
|
+
|
|
236
|
+
if (this._config.verbose) {
|
|
237
|
+
console.log('[KimchiProvider] Initialization complete')
|
|
238
|
+
console.log(`[KimchiProvider] Available circuits: ${Array.from(this._circuits.keys()).join(', ') || 'none'}`)
|
|
239
|
+
}
|
|
240
|
+
} catch (error) {
|
|
241
|
+
this._initError = error instanceof Error ? error : new Error(String(error))
|
|
242
|
+
this._status.lastError = this._initError.message
|
|
243
|
+
throw this._initError
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
async waitUntilReady(timeoutMs = 30000): Promise<void> {
|
|
248
|
+
if (this._status.isReady) return
|
|
249
|
+
|
|
250
|
+
if (this._initError) {
|
|
251
|
+
throw this._initError
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (!this._initPromise) {
|
|
255
|
+
this._initPromise = this.doInitialize()
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const timeoutPromise = new Promise<never>((_, reject) => {
|
|
259
|
+
setTimeout(() => reject(new Error(`KimchiProvider initialization timed out after ${timeoutMs}ms`)), timeoutMs)
|
|
260
|
+
})
|
|
261
|
+
|
|
262
|
+
await Promise.race([this._initPromise, timeoutPromise])
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
async dispose(): Promise<void> {
|
|
266
|
+
this._compiledCircuits.clear()
|
|
267
|
+
this._status.isReady = false
|
|
268
|
+
this._initPromise = null
|
|
269
|
+
this._initError = null
|
|
270
|
+
|
|
271
|
+
if (this._config.verbose) {
|
|
272
|
+
console.log('[KimchiProvider] Disposed')
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// ─── Circuit Management ───────────────────────────────────────────────────
|
|
277
|
+
|
|
278
|
+
getAvailableCircuits(): string[] {
|
|
279
|
+
return Array.from(this._circuits.keys())
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
hasCircuit(circuitId: string): boolean {
|
|
283
|
+
return this._circuits.has(circuitId)
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Register a circuit configuration
|
|
288
|
+
*/
|
|
289
|
+
registerCircuit(config: KimchiCircuitConfig): void {
|
|
290
|
+
this._circuits.set(config.id, config)
|
|
291
|
+
|
|
292
|
+
if (this._config.verbose) {
|
|
293
|
+
console.log(`[KimchiProvider] Registered circuit: ${config.id} (${config.gateCount} gates)`)
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Compile a circuit for proof generation
|
|
299
|
+
*
|
|
300
|
+
* Compiling in Kimchi/o1js creates the verification key and prepares
|
|
301
|
+
* the circuit for proving.
|
|
302
|
+
*/
|
|
303
|
+
async compileCircuit(circuitId: string): Promise<string> {
|
|
304
|
+
const circuit = this._circuits.get(circuitId)
|
|
305
|
+
if (!circuit) {
|
|
306
|
+
throw new Error(`Circuit not found: ${circuitId}`)
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
if (this._config.verbose) {
|
|
310
|
+
console.log(`[KimchiProvider] Compiling circuit: ${circuitId}`)
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// TODO: Actual compilation with o1js
|
|
314
|
+
// For now, simulate compilation
|
|
315
|
+
await this.delay(100 + circuit.gateCount * 0.1)
|
|
316
|
+
|
|
317
|
+
// Generate a mock verification key hash
|
|
318
|
+
const vkHash = `0x${bytesToHex(randomBytes(32))}`
|
|
319
|
+
|
|
320
|
+
this._compiledCircuits.set(circuitId, {
|
|
321
|
+
compiled: true,
|
|
322
|
+
vkHash,
|
|
323
|
+
})
|
|
324
|
+
|
|
325
|
+
if (this._config.verbose) {
|
|
326
|
+
console.log(`[KimchiProvider] Circuit compiled: ${circuitId}, vkHash: ${vkHash.slice(0, 18)}...`)
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
return vkHash
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Check if a circuit is compiled
|
|
334
|
+
*/
|
|
335
|
+
isCircuitCompiled(circuitId: string): boolean {
|
|
336
|
+
return this._compiledCircuits.get(circuitId)?.compiled === true
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
// ─── Proof Generation ─────────────────────────────────────────────────────
|
|
340
|
+
|
|
341
|
+
async generateProof(request: ProofGenerationRequest): Promise<ProofGenerationResult> {
|
|
342
|
+
const startTime = Date.now()
|
|
343
|
+
|
|
344
|
+
if (!this._status.isReady) {
|
|
345
|
+
return {
|
|
346
|
+
success: false,
|
|
347
|
+
error: 'Provider not initialized',
|
|
348
|
+
timeMs: Date.now() - startTime,
|
|
349
|
+
providerId: `kimchi-${this.generateProviderId()}`,
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
const circuit = this._circuits.get(request.circuitId)
|
|
354
|
+
if (!circuit) {
|
|
355
|
+
return {
|
|
356
|
+
success: false,
|
|
357
|
+
error: `Circuit not found: ${request.circuitId}`,
|
|
358
|
+
timeMs: Date.now() - startTime,
|
|
359
|
+
providerId: `kimchi-${this.generateProviderId()}`,
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
try {
|
|
364
|
+
this._status.isBusy = true
|
|
365
|
+
|
|
366
|
+
// Auto-compile if not compiled
|
|
367
|
+
if (!this.isCircuitCompiled(request.circuitId)) {
|
|
368
|
+
await this.compileCircuit(request.circuitId)
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// TODO: Implement actual Kimchi proof generation with o1js
|
|
372
|
+
const proof = await this.generateMockProof(request, circuit)
|
|
373
|
+
|
|
374
|
+
// Update metrics
|
|
375
|
+
const timeMs = Date.now() - startTime
|
|
376
|
+
this._metrics.proofsGenerated++
|
|
377
|
+
this._metrics.avgGenerationTimeMs = (
|
|
378
|
+
(this._metrics.avgGenerationTimeMs * (this._metrics.proofsGenerated - 1) + timeMs) /
|
|
379
|
+
this._metrics.proofsGenerated
|
|
380
|
+
)
|
|
381
|
+
|
|
382
|
+
return {
|
|
383
|
+
success: true,
|
|
384
|
+
proof,
|
|
385
|
+
timeMs,
|
|
386
|
+
providerId: `kimchi-${this.generateProviderId()}`,
|
|
387
|
+
}
|
|
388
|
+
} catch (error) {
|
|
389
|
+
this._metrics.successRate = this._metrics.proofsGenerated > 0
|
|
390
|
+
? (this._metrics.proofsGenerated - 1) / this._metrics.proofsGenerated
|
|
391
|
+
: 0
|
|
392
|
+
|
|
393
|
+
return {
|
|
394
|
+
success: false,
|
|
395
|
+
error: error instanceof Error ? error.message : 'Unknown error during proof generation',
|
|
396
|
+
timeMs: Date.now() - startTime,
|
|
397
|
+
providerId: `kimchi-${this.generateProviderId()}`,
|
|
398
|
+
}
|
|
399
|
+
} finally {
|
|
400
|
+
this._status.isBusy = false
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
private async generateMockProof(
|
|
405
|
+
request: ProofGenerationRequest,
|
|
406
|
+
circuit: KimchiCircuitConfig,
|
|
407
|
+
): Promise<SingleProof> {
|
|
408
|
+
// Simulate proof generation time based on circuit complexity
|
|
409
|
+
const simulatedTimeMs = Math.min(200 + circuit.gateCount * 0.5, 5000)
|
|
410
|
+
await this.delay(simulatedTimeMs)
|
|
411
|
+
|
|
412
|
+
// Generate Kimchi-style proof (~22KB)
|
|
413
|
+
const proofBytes = randomBytes(KIMCHI_PROOF_SIZE)
|
|
414
|
+
const proofHex = `0x${bytesToHex(proofBytes)}` as HexString
|
|
415
|
+
|
|
416
|
+
// Convert public inputs to HexString array
|
|
417
|
+
const publicInputs: HexString[] = Object.values(request.publicInputs).map((v) => {
|
|
418
|
+
if (typeof v === 'string' && v.startsWith('0x')) {
|
|
419
|
+
return v as HexString
|
|
420
|
+
}
|
|
421
|
+
if (typeof v === 'bigint' || typeof v === 'number') {
|
|
422
|
+
return `0x${v.toString(16).padStart(64, '0')}` as HexString
|
|
423
|
+
}
|
|
424
|
+
return `0x${Buffer.from(String(v)).toString('hex')}` as HexString
|
|
425
|
+
})
|
|
426
|
+
|
|
427
|
+
const vkHash = this._compiledCircuits.get(request.circuitId)?.vkHash
|
|
428
|
+
|
|
429
|
+
return {
|
|
430
|
+
id: `kimchi-proof-${Date.now()}-${randomBytes(4).reduce((a, b) => a + b.toString(16), '')}`,
|
|
431
|
+
proof: proofHex,
|
|
432
|
+
publicInputs,
|
|
433
|
+
verificationKey: vkHash as HexString | undefined,
|
|
434
|
+
metadata: {
|
|
435
|
+
system: 'kimchi',
|
|
436
|
+
systemVersion: KIMCHI_VERSION,
|
|
437
|
+
circuitId: circuit.id,
|
|
438
|
+
circuitVersion: '1.0.0',
|
|
439
|
+
generatedAt: Date.now(),
|
|
440
|
+
proofSizeBytes: KIMCHI_PROOF_SIZE,
|
|
441
|
+
verificationCost: BigInt(Math.ceil(circuit.gateCount / 10)),
|
|
442
|
+
targetChainId: this._config.network === 'mainnet' ? 'mina:mainnet' : 'mina:testnet',
|
|
443
|
+
},
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
// ─── Verification ─────────────────────────────────────────────────────────
|
|
448
|
+
|
|
449
|
+
async verifyProof(proof: SingleProof): Promise<boolean> {
|
|
450
|
+
if (!this._status.isReady) {
|
|
451
|
+
throw new Error('Provider not initialized')
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
const startTime = Date.now()
|
|
455
|
+
|
|
456
|
+
try {
|
|
457
|
+
// TODO: Implement actual Kimchi verification
|
|
458
|
+
const isValid = this.validateProofFormat(proof)
|
|
459
|
+
|
|
460
|
+
// Update metrics
|
|
461
|
+
const timeMs = Date.now() - startTime
|
|
462
|
+
this._metrics.proofsVerified++
|
|
463
|
+
this._metrics.avgVerificationTimeMs = (
|
|
464
|
+
(this._metrics.avgVerificationTimeMs * (this._metrics.proofsVerified - 1) + timeMs) /
|
|
465
|
+
this._metrics.proofsVerified
|
|
466
|
+
)
|
|
467
|
+
|
|
468
|
+
return isValid
|
|
469
|
+
} catch {
|
|
470
|
+
return false
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
async verifyBatch(proofs: SingleProof[]): Promise<boolean[]> {
|
|
475
|
+
if (!this._status.isReady) {
|
|
476
|
+
throw new Error('Provider not initialized')
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
// TODO: Implement batch verification
|
|
480
|
+
const results: boolean[] = []
|
|
481
|
+
for (const proof of proofs) {
|
|
482
|
+
results.push(await this.verifyProof(proof))
|
|
483
|
+
}
|
|
484
|
+
return results
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
private validateProofFormat(proof: SingleProof): boolean {
|
|
488
|
+
if (!proof.id || !proof.proof || !proof.metadata) {
|
|
489
|
+
return false
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
if (proof.metadata.system !== 'kimchi') {
|
|
493
|
+
return false
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
if (!proof.proof.startsWith('0x')) {
|
|
497
|
+
return false
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
// Kimchi proofs should be approximately 22KB
|
|
501
|
+
const proofBytes = (proof.proof.length - 2) / 2
|
|
502
|
+
if (proofBytes < KIMCHI_PROOF_SIZE * 0.5 || proofBytes > KIMCHI_PROOF_SIZE * 2) {
|
|
503
|
+
// Allow some variance but flag significantly wrong sizes
|
|
504
|
+
if (this._config.verbose) {
|
|
505
|
+
console.warn(`[KimchiProvider] Unusual proof size: ${proofBytes} bytes (expected ~${KIMCHI_PROOF_SIZE})`)
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
if (proof.metadata.expiresAt && proof.metadata.expiresAt < Date.now()) {
|
|
510
|
+
return false
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
return true
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
// ─── Recursive Proving (Pickles) ──────────────────────────────────────────
|
|
517
|
+
|
|
518
|
+
/**
|
|
519
|
+
* Check if Pickles recursive proving is available
|
|
520
|
+
*/
|
|
521
|
+
supportsRecursion(): boolean {
|
|
522
|
+
return this._config.enablePickles
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
/**
|
|
526
|
+
* Merge multiple proofs recursively using Pickles
|
|
527
|
+
*
|
|
528
|
+
* This enables constant-size proofs regardless of how many
|
|
529
|
+
* proofs are combined.
|
|
530
|
+
*/
|
|
531
|
+
async mergeProofsRecursively(proofs: SingleProof[]): Promise<SingleProof | null> {
|
|
532
|
+
if (!this._config.enablePickles) {
|
|
533
|
+
if (this._config.verbose) {
|
|
534
|
+
console.warn('[KimchiProvider] Pickles not enabled for recursive merging')
|
|
535
|
+
}
|
|
536
|
+
return null
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
if (proofs.length === 0) {
|
|
540
|
+
return null
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
if (proofs.length === 1) {
|
|
544
|
+
return proofs[0]
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
// TODO: Implement actual Pickles recursive merge
|
|
548
|
+
// For now, return a mock merged proof
|
|
549
|
+
if (this._config.verbose) {
|
|
550
|
+
console.log(`[KimchiProvider] Merging ${proofs.length} proofs recursively`)
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
await this.delay(proofs.length * 100)
|
|
554
|
+
|
|
555
|
+
const mergedProofBytes = randomBytes(KIMCHI_PROOF_SIZE)
|
|
556
|
+
const combinedInputs = proofs.flatMap(p => p.publicInputs)
|
|
557
|
+
|
|
558
|
+
return {
|
|
559
|
+
id: `kimchi-merged-${Date.now()}`,
|
|
560
|
+
proof: `0x${bytesToHex(mergedProofBytes)}`,
|
|
561
|
+
publicInputs: combinedInputs.slice(0, 10) as HexString[], // Limit public inputs
|
|
562
|
+
metadata: {
|
|
563
|
+
system: 'kimchi',
|
|
564
|
+
systemVersion: KIMCHI_VERSION,
|
|
565
|
+
circuitId: 'pickles_merge',
|
|
566
|
+
circuitVersion: '1.0.0',
|
|
567
|
+
generatedAt: Date.now(),
|
|
568
|
+
proofSizeBytes: KIMCHI_PROOF_SIZE,
|
|
569
|
+
},
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
// ─── Helpers ──────────────────────────────────────────────────────────────
|
|
574
|
+
|
|
575
|
+
private async simulateAsyncLoad(): Promise<void> {
|
|
576
|
+
await this.delay(50)
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
private delay(ms: number): Promise<void> {
|
|
580
|
+
return new Promise(resolve => setTimeout(resolve, ms))
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
private generateProviderId(): string {
|
|
584
|
+
return bytesToHex(randomBytes(4))
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
// ─── Factory Functions ──────────────────────────────────────────────────────
|
|
589
|
+
|
|
590
|
+
/**
|
|
591
|
+
* Create a Kimchi provider with standard configuration
|
|
592
|
+
*/
|
|
593
|
+
export function createKimchiProvider(config?: KimchiProviderConfig): KimchiProvider {
|
|
594
|
+
return new KimchiProvider(config)
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
/**
|
|
598
|
+
* Create a Kimchi provider configured for Mina mainnet
|
|
599
|
+
*/
|
|
600
|
+
export function createMinaMainnetProvider(): KimchiProvider {
|
|
601
|
+
return new KimchiProvider({
|
|
602
|
+
network: 'mainnet',
|
|
603
|
+
enablePickles: true,
|
|
604
|
+
circuits: [
|
|
605
|
+
{
|
|
606
|
+
id: 'token_transfer',
|
|
607
|
+
name: 'Token Transfer Circuit',
|
|
608
|
+
gateCount: 5000,
|
|
609
|
+
publicInputCount: 4,
|
|
610
|
+
usesRecursion: false,
|
|
611
|
+
},
|
|
612
|
+
{
|
|
613
|
+
id: 'state_update',
|
|
614
|
+
name: 'State Update Circuit',
|
|
615
|
+
gateCount: 8000,
|
|
616
|
+
publicInputCount: 6,
|
|
617
|
+
usesRecursion: true,
|
|
618
|
+
},
|
|
619
|
+
],
|
|
620
|
+
})
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
/**
|
|
624
|
+
* Create a Kimchi provider configured for zkApp development
|
|
625
|
+
*/
|
|
626
|
+
export function createZkAppProvider(network: 'testnet' | 'local' = 'testnet'): KimchiProvider {
|
|
627
|
+
return new KimchiProvider({
|
|
628
|
+
network,
|
|
629
|
+
enablePickles: true,
|
|
630
|
+
verbose: network === 'local',
|
|
631
|
+
circuits: [
|
|
632
|
+
{
|
|
633
|
+
id: 'zkapp_method',
|
|
634
|
+
name: 'zkApp Method Circuit',
|
|
635
|
+
gateCount: 3000,
|
|
636
|
+
publicInputCount: 3,
|
|
637
|
+
usesRecursion: false,
|
|
638
|
+
},
|
|
639
|
+
],
|
|
640
|
+
})
|
|
641
|
+
}
|