@@ -7,13 +7,15 @@ import { RuntimeTable } from '../gadgets/runtime-table.js';
77import { constraintSystem , contains } from '../../testing/constraint-system.js' ;
88import { FeatureFlags } from '../../proof-system/feature-flags.js' ;
99import { Cache } from '../../proof-system/cache.js' ;
10+ import { Provable } from '../provable.js' ;
1011
11- let uint = ( n : number | bigint ) : Spec < bigint , Field > => {
12- return fieldWithRng ( Random . bignat ( ( 1n << BigInt ( n ) ) - 1n ) ) ;
13- } ;
1412
15- // Runtime table tests
13+ // in-circuit unit tests
1614{
15+ let uint = ( n : number | bigint ) : Spec < bigint , Field > => {
16+ return fieldWithRng ( Random . bignat ( ( 1n << BigInt ( n ) ) - 1n ) ) ;
17+ } ;
18+
1719 let RuntimeTableZkProgram = ZkProgram ( {
1820 name : 'runtime-table' ,
1921 methods : {
@@ -68,3 +70,39 @@ let uint = (n: number | bigint): Spec<bigint, Field> => {
6870 }
6971 ) ;
7072}
73+
74+ // off-circuit checks
75+ {
76+ function expectThrows ( fn : ( ) => void | Promise < void > , msg : string ) {
77+ let threw = false ;
78+ try {
79+ fn ( ) ;
80+ } catch {
81+ threw = true ;
82+ }
83+ assert ( threw , msg ) ;
84+ }
85+
86+ // Cannot create a table with reserved id
87+ expectThrows ( ( ) => {
88+ new RuntimeTable ( 0 , [ 0n , 1n ] ) ;
89+ } , 'Table id 0 is reserved' ) ;
90+
91+ expectThrows ( ( ) => {
92+ new RuntimeTable ( 1 , [ 0n , 1n ] ) ;
93+ } , 'Table id 1 is reserved' ) ;
94+
95+ // Cannot create a table with duplicate indices
96+ expectThrows ( ( ) => {
97+ new RuntimeTable ( 3 , [ 0n , 1n , 2n , 2n ] ) ;
98+ } , 'Indices must be unique' ) ;
99+
100+ // Cannot insert pairs with indices not in the table
101+ await Provable . runAndCheck ( async ( ) => {
102+ let table = new RuntimeTable ( 42 , [ 0n , 1n , 2n , 3n , 4n , 5n ] ) ;
103+
104+ expectThrows ( ( ) => {
105+ table . insert ( [ [ 0n , new Field ( 1 ) ] , [ 6n , new Field ( 2 ) ] ] ) ;
106+ } , 'Indices must be preallocated at creation of the runtime table' ) ;
107+ } ) ;
108+ }
0 commit comments