22 * This module defines the `RuntimeTable` class, which represents a runtime table used in zk-SNARKs.
33 */
44
5+ import { assert } from "console" ;
56import { Field } from "../field.js" ;
67import { Gates } from "../gates.js" ;
78
@@ -24,23 +25,34 @@ class RuntimeTable {
2425 * Indices that define the structure of the runtime table.
2526 * They can be consecutive or not, but they must be unique.
2627 */
27- readonly indices : bigint [ ] ;
28+ readonly indices : Set < bigint > ;
2829 /**
2930 * Pending pairs to be checked on the runtime table.
3031 */
3132 pairs : Array < [ bigint , Field ] > = [ ] ;
3233
3334 constructor ( id : number , indices : bigint [ ] ) {
34- this . id = id ;
35- this . indices = indices ;
36- Gates . addRuntimeTableConfig ( id , indices ) ;
35+ // check that id is not 0 or 1, as those are reserved values
36+ assert ( id !== 0 && id !== 1 , "Runtime table id must be different than 0 and 1" ) ;
37+
38+ // check that all the indices are unique
39+ let uniqueIndices = new Set ( indices ) ;
40+ assert ( uniqueIndices . size === indices . length , "Runtime table indices must be unique" ) ;
41+
42+ // initialize the runtime table
43+ this . id = id ;
44+ this . indices = uniqueIndices ;
45+ Gates . addRuntimeTableConfig ( id , indices ) ;
3746 }
3847
3948 /**
4049 * Inserts key-value pairs into the runtime table.
4150 * Under the hood, this method uses the `Gates.lookup` function to perform
4251 * lookups to the table with identifier `this.id`. One single lookup gate
43- * can store up to 3 different pairs of index and value.
52+ * can store up to 3 different pairs of index and value.
53+ *
54+ * It throws when trying to insert a pair with an index that is not part of
55+ * the runtime table.
4456 *
4557 * @param pairs Array of pairs [index, value] to insert into the runtime table.
4658 */
@@ -50,13 +62,18 @@ class RuntimeTable {
5062 const [ idx0 , value0 ] = chunk [ 0 ] ;
5163 const [ idx1 , value1 ] = chunk [ 1 ] || [ idx0 , value0 ] ;
5264 const [ idx2 , value2 ] = chunk [ 2 ] || [ idx0 , value0 ] ;
65+
66+ assert ( this . indices . has ( idx0 ) && this . indices . has ( idx1 ) && this . indices . has ( idx2 ) ,
67+ `Indices must be part of the runtime table with id ${ this . id } ` ) ;
68+
5369 Gates . lookup ( Field . from ( this . id ) , Field . from ( idx0 ) , value0 , Field . from ( idx1 ) , value1 , Field . from ( idx2 ) , value2 ) ;
5470 }
5571 }
5672
5773 /**
58- * Checks if a key-value pair exists in the runtime table. Note that the same
59- * index can be queried several times as long as the value remains the same.
74+ * In-circuit checks if a key-value pair exists in the runtime table. Note
75+ * that the same index can be queried several times as long as the value
76+ * remains the same.
6077 *
6178 * Every three calls to this method for the same identifier will be grouped
6279 * into a single lookup gate for efficiency.
0 commit comments