Skip to content

Commit 7d2fff1

Browse files
committed
exhaustive doc comment for the RuntimeTable class
1 parent 870d8a2 commit 7d2fff1

File tree

1 file changed

+74
-2
lines changed

1 file changed

+74
-2
lines changed

src/lib/provable/gadgets/runtime-table.ts

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,80 @@ export {
1111
};
1212

1313
/**
14-
* The `RuntimeTable` class represents a provable table whose entries can be defined
15-
* at runtime. It allows inserting key-value pairs and checking for their existence.
14+
* # RuntimeTable
15+
*
16+
* A **provable lookup table** whose entries are defined at runtime (during circuit construction).
17+
* It constrains that certain `(index, value)` pairs *exist* in a table identified by `id`, using
18+
* efficient **lookup gates** under the hood. Each inner lookup gate can batch up to **3 pairs**.
19+
*
20+
* ## When to use
21+
* - **small/medium, runtime-chosen set** of `(index, value)` pairs and want to prove
22+
* **membership** of queried pairs in that set.
23+
* - **ergonomic batching**: repeated `lookup()` calls automatically group into 3-tuples
24+
* so it creates pay fewer gates when possible (instead of writing repetitive `Gates.lookup(...)`
25+
* calls and manually handling batching of lookup entries).
26+
* - **expressiveness**: all runtime tables will be condensed into one long table under the hood,
27+
* so it is highly recommended to use distinct `id`s for unrelated tables to achieve better
28+
* separation of concerns and avoid accidental collisions, at no extra cost.
29+
*
30+
* ## When *not* to use
31+
* - **static and global tables**: Prefer built-ins for fixed-tables that already exist in the system.
32+
* (a.k.a. standard 4-bit XOR or 12-bit length range-check tables).
33+
* - **hiding properties**: lookup tables **constrain membership**, but don’t provide secrecy
34+
* of the values by themselves. If data privacy is needed, consider using the **witness** to hold
35+
* the values and protect from exposure to the verifier.
36+
* - **huge tables**: runtime lookups are efficient for a limited amount of entries, but their
37+
* size is limited by the underlying circuit size (i.e. 2^16). Applications needing more storage
38+
* should consider an optimized custom solution.
39+
* - **mutable data**: runtime tables are write-once only, so once inserted entries in table are
40+
* remain fixed. To represent changing data, consider using DynamicArrays.
41+
*
42+
* ## Invariants & constraints
43+
* - `id !== 0 && id !== 1`. (Reserved for XOR and range-check tables.)
44+
* - `indices` are **unique**. Duplicates are rejected.
45+
* - `lookup()` **batches** each 3 calls (for the same table) into **one** gate automatically.
46+
* - `check()` call is required for soundness to flush 1–2 pending pairs before the end of the circuit.
47+
*
48+
* ## Complexity
49+
* - Gate cost for membership checks is ~`ceil(#pairs / 3)` lookup gates per table id,
50+
* plus one lookup gate per `insert()` triplet.
51+
*
52+
* ## Example
53+
* ```ts
54+
* // Define a runtime table with id=5 and allowed indices {10n, 20n, 30n}
55+
* const rt = new RuntimeTable(5, [10n, 20n, 30n]);
56+
*
57+
* // Populate some pairs (you can insert in chunks of up to 3)
58+
* rt.insert([
59+
* [10n, Field.from(123)],
60+
* [20n, Field.from(456)],
61+
* [30n, Field.from(789)],
62+
* ]);
63+
*
64+
* // Constrain that these pairs exist in the table
65+
* rt.lookup(10n, Field.from(123));
66+
* rt.lookup(20n, Field.from(456));
67+
* // These two calls will be grouped; add a third, or call check() to flush
68+
* rt.check(); // flush pending lookups (important!)
69+
* ```
70+
*
71+
* ## Gotchas
72+
* - **Don’t forget `check()`**: If you finish a proof block with 1–2 pending `lookup()` calls,
73+
* call `check()` to emit the final lookup gate. Otherwise those constraints won’t land.
74+
* - **Index validation**: `insert()` and `lookup()` throw if the index isn’t whitelisted in `indices`.
75+
* - **ID collisions**: Pick distinct `id`s for unrelated runtime tables.
76+
* - **flag settings**: zkApps with runtime tables must be compiled with the `withRuntimeTables` flag.
77+
*
78+
* @remarks
79+
* Construction registers the table configuration via `Gates.addRuntimeTableConfig(id, indices)`.
80+
* Subsequent `insert()`/`lookup()` use that configuration to emit lookup gates. Please refrain from
81+
* using that function directly, as it will be deprecated in the future.
82+
*
83+
* @see Gates.lookup
84+
* @see Gates.addRuntimeTableConfig
85+
* @see Gadgets.inTable
86+
* @see DynamicArray for a mutable alternative to store runtime data.
87+
* @public
1688
*/
1789
class RuntimeTable {
1890
/**

0 commit comments

Comments
 (0)