1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//! # aethel-vault
//!
//! **aethel-vault** is the agent-held wallet: the crate is `aethel-vault`,
//! the repository is `aethel-runtime`. Not to be confused with
//! `pqc-privacy`'s unrelated AES-GCM + Reed-Solomon *shard* module
//! (historically also called "vault" there — that is a research
//! component; this crate owns the word "wallet").
//!
//! ## Two assets, not one (V-7)
//!
//! This crate holds and moves two entirely separate things, and never
//! bridges between them:
//!
//! - **Confidential internal balance** (`fhe-state` feature,
//! [`vault`]/[`client`]) — an `FheUint64` ciphertext the vault never
//! decrypts, homomorphically transferred between agent-chosen ledger
//! entries. It exists only in the agent's own storage (which may be a
//! hostile host); privacy here is against *that storage*. **It is not a
//! token, not on any chain, and not USDC.** Older docs in this crate
//! described this as "on-chain" with "validator" processing — that
//! language was wrong and has been corrected; there is no chain and no
//! validator anywhere in this crate.
//! - **Settlement asset** (`signer` feature, default,
//! [`settlement`]) — USDC on Base, Ethereum, or Arbitrum One, moved by an
//! EIP-3009 `transferWithAuthorization` signature the agent's *external*
//! wallet produces. Public by nature; bounded by [`policy::SpendPolicy`].
//!
//! An FHE transfer never moves USDC, and an EIP-3009 authorization never
//! changes an FHE balance unless the agent explicitly records it — there
//! is no bridge in this crate. See [`vault`]'s module docs for more detail.
//!
//! ## Zero ECDSA / 100% PQC signer; secp256k1 lives in the agent's wallet
//!
//! This crate never implements, produces, or verifies a secp256k1 ECDSA
//! signature. The [`settlement::SettlementSigner`] trait is the injection
//! point: an agent's existing wallet (hardware, MPC, browser, HSM) signs
//! the EIP-712 digest this crate builds. This crate's *own* signatures —
//! spend intents, settlement receipts, wallet-bind attestations, HITL
//! approvals — are all ML-DSA-65 via `aethel_core::signing::Identity`,
//! under the purpose-separated contexts in `aethel_core::signing::purpose`.
//! A hybrid (PQC + classical) posture, if wanted, belongs at SAGP (the
//! gateway), not here. See [`settlement`]'s module docs for the full
//! rationale and the locked rules it encodes.
//!
//! ## Custody rule: the ServerKey — and the settlement signer — never leave the agent
//!
//! - **ServerKey** (`fhe-state` feature): held only in a process-local,
//! never-serialized runtime structure. It never appears in exported or
//! persisted [`vault::VaultState`] bytes — see [`vault`]'s "Custody rule"
//! docs (V-2).
//! - **Settlement signer** (`signer` feature): the
//! [`settlement::SettlementSigner`] trait is injected by the caller; no
//! implementation ships in this crate except a test-only mock. This
//! crate never holds a secp256k1 private key.
//! - **There is no "hosted vault" mode.** Naming a Cargo feature `hosted`
//! is a hard compile error (see below). Do not host `aethel-vault` on a
//! third party's infrastructure; the agent runtime, or a wallet process
//! the principal already runs, holds every secret this crate touches.
//!
//! ## Feature Flags
//!
//! - `signer` (**default**) — [`settlement`] (EIP-3009/x402 export),
//! [`policy`] (standing spend policy), [`receipt`] (optional signed
//! receipts). No `tfhe`, no HelixDB gRPC stack. This is the Wave-1
//! "vault as signer of a normal USDC wallet" path.
//! - `fhe-state` — the confidential internal ledger ([`vault`]'s
//! `VaultState`/homomorphic transfer state machine, [`client`]'s
//! `NativeVaultClient`). Pulls `tfhe`. Optional: a differentiator for
//! agents storing state on a hostile host, not a Wave-1 gate.
//! **Experimental.** Its core ledger operations have no real tests yet.
//! - `helixdb` — the [`storage::helixdb`] gRPC adapter. Pulls `tokio`,
//! `tonic`, `prost`, and runs `prost-build` in `build.rs`.
//! **Experimental.** The adapter is stubs: every storage call returns
//! `HelixError::NotImplemented`.
//! - `std` (default) — standard library support. Does not, by itself,
//! pull `tokio`/`tonic`/`prost` (that is `helixdb`'s job) or `tfhe`
//! (that is `fhe-state`'s job).
//! - `wasm` — `wasm-bindgen` exports for the confidential-ledger state
//! machine and client.
//! - `hosted` — **does not exist as a real feature.** Enabling it is a
//! hard `compile_error!` (see below). This is the custody rule enforced
//! structurally, not just documented.
//!
//! `default = ["std", "signer"]`: a plain `cargo add aethel-vault` builds
//! the thin signer path only — no `tfhe`, no `tokio`, no `tonic`, no
//! `prost` anywhere in the dependency graph (confirm with `cargo tree -e
//! normal`).
//!
//! ## Modules
//!
//! - [`settlement`] — EIP-3009/x402 export (`signer`, default). `Chain`
//! table, `TransferWithAuthorization`, `SettlementSigner` trait,
//! `Wallet::authorize_eip3009`.
//! - [`policy`] — standing spend policy (`signer`, default). `SpendPolicy`,
//! `PolicyState`, `Rail`, `HitlApproval`.
//! - [`receipt`] — optional signed settlement receipts (`signer`, default).
//! - [`error`] — `VaultError`, the Rust-native error type for the
//! `signer`-mode surface.
//! - [`vault`] — the confidential-ledger state machine (`fhe-state`).
//! Homomorphic balance management using `FheUint64` ciphertexts via
//! `tfhe-rs`.
//! - [`client`] — the confidential ledger's client SDK (`fhe-state`
//! native, or `wasm` for JS).
//! - [`storage`] — HelixDB storage adapter (`helixdb` feature only).
//! - [`sdk`] — Client SDK module. TypeScript and Rust SDK utilities.
//!
//! ## Security Properties
//!
//! - **Zero Plaintext Leakage (confidential ledger)**: `fhe-state` balances
//! exist solely as `FheUint64` ciphertexts in the agent's own storage;
//! this crate never decrypts them.
//! - **Post-Quantum Hardness**: the confidential ledger's security reduces
//! to LWE hardness over the Torus; this crate's own signatures
//! (spend intent, receipt, wallet bind, HITL approval) are ML-DSA-65.
//! - **Ephemeral Vault IDs**: anonymous vault identifiers derived from
//! Polymorphic Lattice Projections; no persistent on-chain footprint —
//! and no on-chain footprint of any kind, since the confidential ledger
//! is not on any chain.
//! - **Policy before signature**: [`settlement::Wallet::authorize_eip3009`]
//! checks [`policy::SpendPolicy`] before it ever calls the injected
//! `SettlementSigner` or signs a `SpendIntent`. A denied or
//! unsatisfied-HITL request produces no signature of any kind.
//!
//! ---
//!
//! ## The 0x307 crate family
//!
//! `aethel-vault` is one of six open-source crates from [0x307](https://0x307.com/crates), held to one
//! audit and stability standard.
//!
//! | Crate | Tier | What it does |
//! |---|---|---|
//! | [pqc-sig](https://crates.io/crates/pqc-sig) | Production | ML-DSA, SLH-DSA and FN-DSA signatures (FIPS 204/205/206) |
//! | [pqc-kem](https://crates.io/crates/pqc-kem) | Production | ML-KEM (FIPS 203), the X25519 + ML-KEM-768 hybrid, X-Wing, and sealed boxes |
//! | [aethel-core](https://crates.io/crates/aethel-core) | Production | Post-quantum anonymous identity: a separate identifier per context, context-bound ML-DSA signing |
//! | [aethel-sdk](https://crates.io/crates/aethel-sdk) | Preview | The SDK over aethel-core, and the place to start |
//! | [aethel-vault](https://crates.io/crates/aethel-vault) (this crate) | Preview | Agent-held wallet: policy-gated x402 / EIP-3009 signing with ML-DSA-65 spend records |
//! | [pqc-privacy](https://crates.io/crates/pqc-privacy) | Lab | Research bundle, kept off every identity and payment path |
//!
//! **Production** crates are thin, standards-bound libraries meant to be depended on today. **Preview** crates work and are published, with APIs still settling. **Lab** crates are research, never on an identity or payment path.
//!
//! **Runnable examples:** [0x307/examples](https://github.com/0x307/examples), one program per
//! crate, pinned to the published versions.
//!
//! **Audit status:** None of these crates has been independently audited, and none holds a CMVP / FIPS 140-3 validation. "FIPS 203/204/205/206" means the algorithms follow those standards, not that the code is certified. Known issues for this crate are in
//! [SECURITY.md](https://github.com/0x307/aethel-runtime/blob/main/SECURITY.md). Version policy:
//! [STABILITY.md](https://github.com/0x307/aethel-runtime/blob/main/STABILITY.md).
//!
// V-2: aethel-vault has no hosted mode, structurally. Naming this feature
// is a hard compile error, not just a documented convention — see the
// crate-level "Custody rule" docs above and `docs/TFHE-VAULT-SPEC.md`.
compile_error!;
extern crate alloc;
// ── WASM allocator + panic handler ────────────────────────────────────────────
//
// For wasm32-unknown-unknown without std, we need:
// 1. A global allocator (wasm-bindgen provides one via its start function)
// 2. A panic handler
//
// wasm-bindgen automatically provides both when it's linked in.
// We just need to ensure the wasm-bindgen start function runs.
// ── Module Declarations ───────────────────────────────────────────────────────
/// The confidential-ledger state machine (`fhe-state` feature) —
/// homomorphic balance management, `FheUint64` ciphertexts. See the
/// module's own docs for the "Two assets, not one" statement and the V-2
/// custody rule. [`vault::derive_vault_id`] and the `ERR_*` constants are
/// available regardless of `fhe-state`.
/// The confidential ledger's client SDK — key generation, balance
/// encryption, payload construction. [`client::LedgerPayload`] is always
/// available; [`client::NativeVaultClient`] requires `fhe-state` and
/// [`client::AethelVaultClient`] requires `wasm`.
/// EIP-3009/x402 settlement export (`signer` feature, default). See the
/// module's own docs for the "Zero ECDSA / 100% PQC signer" statement.
/// Standing spend policy, enforced before any settlement intent is signed
/// (`signer` feature, default).
/// Optional signed settlement receipts (`signer` feature, default).
/// Emitted only on request — see the module's own docs (V-5 / UX-001).
/// `VaultError` — the Rust-native error type for the `signer`-mode surface.
/// Storage layer — HelixDB vector-graph hybrid database adapter.
///
/// Provides async gRPC client for encrypted vault state storage and retrieval.
/// Only available with the `helixdb` feature.
/// SDK module — TypeScript and Rust utilities for HelixDB integration.
// ── Re-exports ────────────────────────────────────────────────────────────────
pub use ;
pub use ;
/// Re-export `LedgerPayload` for all targets (V-7: renamed from
/// `ContractPayload`, which remains available as a deprecated alias).
pub use LedgerPayload;
pub use ;
pub use NativeVaultClient;
pub use VaultError;
pub use ;
pub use ;
pub use ;
pub use HelixDbAdapter;
// ── Top-level WASM exports ────────────────────────────────────────────────────
//
// These re-export vault functions with wasm_bindgen for direct JS consumption.
// Homomorphic-ledger exports additionally require `fhe-state`.
use *;
/// Initialize the vault with a TFHE ServerKey (WASM export, `fhe-state`).
///
/// Must be called before any other vault operations.
///
/// # Parameters
///
/// - `server_key_bytes`: Bincode-serialized `ServerKey`.
///
/// # Returns
///
/// `0` on success, non-zero error code on failure.
/// Register a vault with an encrypted initial balance (WASM export, `fhe-state`).
///
/// # Parameters
///
/// - `vault_id`: 32-byte vault ID (derived from PLP projection via `vault_derive_id`).
/// - `initial_balance_ct`: Bincode-serialized `FheUint64` initial balance ciphertext.
///
/// # Returns
///
/// `0` on success, non-zero error code on failure.
/// Execute a homomorphic transfer between two vaults (WASM export, `fhe-state`).
///
/// # Parameters
///
/// - `sender_id`: 32-byte sender vault ID.
/// - `receiver_id`: 32-byte receiver vault ID.
/// - `transfer_ct`: Bincode-serialized `FheUint64` transfer amount ciphertext.
///
/// # Returns
///
/// `0` on success, non-zero error code on failure.
/// Execute an authenticated homomorphic transfer from `aethel-plp-1` wire
/// bytes (WASM export, `fhe-state`). Bytes-taking counterpart of
/// [`vault_transfer`] that also checks a PLP ownership proof against the
/// sender vault's registered identity — see
/// [`vault::homomorphic_transfer_authenticated_bytes`] for the full order
/// of checks (SAGP-PG-001 V-3 residual / `docs/ROADMAP.md` §2).
///
/// # Parameters
///
/// - `sender_id` / `receiver_id`: 32-byte vault IDs.
/// - `transfer_ct`: Bincode-serialized `FheUint64` transfer amount ciphertext.
/// - `projection_bytes` / `proof_bytes`: `aethel-plp-1` wire envelopes.
/// - `context`: the verifier's own context bytes.
///
/// # Returns
///
/// `0` on success, non-zero error code on failure (including the new
/// `ERR_WIRE_VERIFY_FAILED` (6)).
/// Register a vault bound to an `aethel-core` PLP identity projection
/// (WASM export, `fhe-state`).
///
/// Unlike [`vault_register`], the vault ID is derived server-side from
/// `projection_bytes` rather than caller-supplied, so it cannot be
/// registered under an ID unrelated to the projection.
///
/// # Parameters
///
/// - `projection_bytes`: Serialized `EphemeralProjection` from aethel-core PLP.
/// - `initial_balance_ct`: Bincode-serialized `FheUint64` initial balance ciphertext.
///
/// # Returns
///
/// The derived 32-byte vault ID, or an empty `Vec` if `projection_bytes`
/// does not decode as an `EphemeralProjection`.
/// Export the entire vault state as bytes for HelixDB persistence (WASM
/// export, `fhe-state`).
///
/// Never contains the ServerKey — see [`vault`]'s "Custody rule" docs (V-2).
///
/// # Returns
///
/// Bincode-serialized `VaultState` bytes (version-prefixed). Empty if
/// state is uninitialized.
/// Import vault state from HelixDB bytes (WASM export, `fhe-state`).
///
/// # Parameters
///
/// - `state_bytes`: Bincode-serialized `VaultState` (version-prefixed).
///
/// # Returns
///
/// `0` on success, non-zero error code on failure.
/// Derive a vault ID from PLP projection bytes (WASM export).
///
/// Available regardless of `fhe-state`: pure hashing.
///
/// # Parameters
///
/// - `projection_bytes`: Serialized `EphemeralProjection` from aethel-core PLP.
///
/// # Returns
///
/// 32-byte vault ID as `Vec<u8>`.