seven.hoon 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. /= ztd-six /common/ztd/six
  2. => ztd-six
  3. ~% %table-lib ..fri-door ~
  4. :: table-lib
  5. |%
  6. ::
  7. +| %jute-types
  8. +$ jute-data [name=@tas sam=tree-data prod=tree-data]
  9. +$ jute-map (map @tas @)
  10. +$ atom-data [register=@tas axis=@ interim-mem-set=(unit @tas)]
  11. +$ register-map (map @tas felt)
  12. +$ encoder [a=mp-graph c=mp-graph]
  13. ::
  14. ++ one-poly ^~((mp-c:mp-to-graph 1))
  15. ++ zero-poly ^~((mp-c:mp-to-graph 0))
  16. ::
  17. +$ jute-func-map (map @tas jute-funcs)
  18. ::
  19. :: +jute-funcs: jute interface
  20. ++ jute-funcs
  21. $+ jute-funcs
  22. $_ ^|
  23. |%
  24. ::
  25. ++ atoms
  26. ^- (list atom-data)
  27. ~
  28. :: compute the actual jet corresponding to this jute
  29. ++ compute
  30. |~ sam=*
  31. ^- *
  32. 0
  33. ::
  34. ++ u8s
  35. |~ jute-info=[name=@tas sam=* prod=*]
  36. ^- (list belt)
  37. ~
  38. ::
  39. :: write base row of the jute table for this jute
  40. ++ build
  41. |~ jute-info=[name=@tas sam=* prod=*]
  42. ^- (map @tas felt)
  43. ~
  44. ::
  45. ++ u8-msets
  46. |~ jute-info=[name=@tas sam=* prod=*]
  47. ^- (list @tas)
  48. ~
  49. ::
  50. :: write extension columns for this jute
  51. ++ extend
  52. |~ [=jute-data challenges=(list felt)]
  53. ^- (map @tas felt)
  54. ~
  55. ::
  56. :: transition constraints for this jute
  57. ++ transition-constraints
  58. |~ vars=(map term mp-graph)
  59. *(map term mp-graph)
  60. --
  61. ::
  62. +| %table-types
  63. +$ row bpoly
  64. +$ belt-row (list belt)
  65. +$ matrix (list row)
  66. ::
  67. +$ col-name term :: name of a column
  68. +$ header $: name=term :: name of the table
  69. field=@ :: cardinality of the field that the table is defined over
  70. base-width=@ :: base number of columns
  71. ext-width=@ :: number of extension columns (doesn't include base)
  72. mega-ext-width=@ :: number of mega-extension columns
  73. full-width=@
  74. num-randomizers=@
  75. ==
  76. :: $table: a type that aspirationally contains all the data needed for a table utilized by the prover.
  77. +$ table [header p=matrix]
  78. +$ table-mary [header p=mary]
  79. +$ table-dat (trel table-mary table-funcs verifier-funcs)
  80. ::
  81. ::
  82. :: the following $matrix type validates that the length of each row is the same. this hurts
  83. :: performance, and eventually we will move towards a more efficient memory allocation anyways,
  84. :: so it is commented out. but you can still use it for debugging by uncommenting it and commenting
  85. :: the $matrix entry above.
  86. :: +$ matrix $| (list row)
  87. :: |= a=(list row)
  88. :: |-
  89. :: ?~ a %.y
  90. :: ?~ t.a %.y
  91. :: ?: =(len.i.a len.i.t.a)
  92. :: $(a t.a)
  93. :: %.n
  94. ::
  95. ::
  96. :: interfaces implemented by each table
  97. +| %table-interfaces
  98. ::
  99. :: +static-table-common: static table data shared by everything that cares about tables
  100. :: TODO either the static parts of the jute table should implement this, or
  101. :: dynamic tables need their own interface entirely
  102. ++ static-table-common
  103. $+ static-table-common
  104. $_ ^|
  105. |%
  106. :: +name: name of the table
  107. :: +basic-column-names: names for base columns as terms
  108. :: +ext-column-names: names for extension columns as terms
  109. :: +column-names: names for all columns as terms
  110. ++ name *term
  111. ++ column-names
  112. *(list col-name)
  113. ++ basic-column-names
  114. *(list col-name)
  115. ++ ext-column-names
  116. *(list col-name)
  117. ++ mega-ext-column-names
  118. *(list col-name)
  119. ++ variables
  120. *(map col-name mp-mega)
  121. ++ terminal-names
  122. *(list col-name)
  123. --
  124. ::
  125. ++ table-funcs
  126. $+ table-funcs
  127. $_ ^|
  128. |%
  129. ::
  130. :: +build: Build the table (Algebraic Execution Trace) for a given nock computation.
  131. ::
  132. :: The returned table is a mary of its rows. The step of the mary
  133. :: is the number of columns while its length is the number of rows.
  134. ::
  135. ++ build
  136. |~ fock-meta=fock-return
  137. *table-mary
  138. ::
  139. :: +extend: Returns extension columns for first pass of challenges
  140. ::
  141. :: The columns are commitments to the validity of the ION fingerprints
  142. ::
  143. ++ extend
  144. |~ [table=table-mary challenges=(list belt) fock-meta=fock-return]
  145. *table-mary
  146. ::
  147. :: +mega-extend: Returns extension columns for the second pass of challenges
  148. ::
  149. ++ mega-extend
  150. |~ [table=table-mary challenges=(list belt) fock-meta=fock-return]
  151. *table-mary
  152. ::
  153. :: +pad: include extra rows until their number equals the next power of two
  154. ::
  155. :: If the height of the table is 6, then it will be padded to 8.
  156. :: If the height of the table is already a power of 2, it will remain
  157. :: the same, e.g pad(8) = 8.
  158. ::
  159. ++ pad
  160. |~ table=table-mary
  161. *table-mary
  162. ::
  163. :: +terminal: produce a map of felts sourced from the final row of the built and extended table
  164. ::
  165. :: The output of this is made available to other tables in the terminal-constraints
  166. :: arm, where tables can specify constraints to interrelate columns by
  167. :: e.g. forcing terminal values of a multiset to be equal.
  168. ::
  169. ++ terminal
  170. |~ =table-mary
  171. *bpoly
  172. ::
  173. ++ boundary-constraints boundary-constraints:*verifier-funcs
  174. ++ row-constraints row-constraints:*verifier-funcs
  175. ++ transition-constraints transition-constraints:*verifier-funcs
  176. ++ terminal-constraints terminal-constraints:*verifier-funcs
  177. ++ extra-constraints extra-constraints:*verifier-funcs
  178. --
  179. ::
  180. ++ verifier-funcs
  181. :: the verifier interface is a strict subset of the prover interface because verify:nock-verifier only uses constraints
  182. $+ verifier-funcs
  183. $_ ^|
  184. |%
  185. ::
  186. :: apply to the first row only
  187. ++ boundary-constraints
  188. *(map term mp-ultra)
  189. ::
  190. :: apply to a single row
  191. ++ row-constraints
  192. *(map term mp-ultra)
  193. ::
  194. :: apply to adjacent row-pairs
  195. ++ transition-constraints
  196. *(map term mp-ultra)
  197. ::
  198. :: apply to the final row only
  199. ++ terminal-constraints
  200. *(map term mp-ultra)
  201. ::
  202. :: apply to extra composition poly only
  203. ++ extra-constraints
  204. *(map term mp-ultra)
  205. --
  206. ::
  207. +| %cores
  208. ::
  209. ++ dyn
  210. ~/ %dyn
  211. |%
  212. ++ c
  213. |= [nam=term tail=(list term)]
  214. ^- (list term)
  215. :^ (crip (weld (trip nam) "-a"))
  216. (crip (weld (trip nam) "-b"))
  217. (crip (weld (trip nam) "-c"))
  218. tail
  219. ::
  220. ++ terminal-names
  221. ^- (list term)
  222. %+ c %compute-terminal-one
  223. %+ c %memory-terminal-one
  224. ~
  225. ::
  226. ++ num-terms (lent terminal-names)
  227. ::
  228. ++ make-dyn-mps
  229. ~/ %make-dyn-mps
  230. |= [dyn-names=(list term)]
  231. ^- (map term mp-mega)
  232. ~+
  233. %- ~(gas by *(map term mp-mega))
  234. %+ iturn dyn-names
  235. |= [i=@ nam=term]
  236. [nam (mp-dyn:mp-to-mega i)]
  237. ::
  238. ++ dyn
  239. |_ terms=(map term mp-mega)
  240. ++ d
  241. |= nam=term
  242. ^- mp-pelt
  243. =/ nam-a (crip (weld (trip nam) "-a"))
  244. =/ nam-b (crip (weld (trip nam) "-b"))
  245. =/ nam-c (crip (weld (trip nam) "-c"))
  246. ?> (~(has by terms) nam-a)
  247. ?> (~(has by terms) nam-b)
  248. ?> (~(has by terms) nam-c)
  249. :+ (~(got by terms) nam-a)
  250. (~(got by terms) nam-b)
  251. (~(got by terms) nam-c)
  252. --
  253. ::
  254. ++ pdyn
  255. |_ terms=(map term belt)
  256. ++ r
  257. |= nam=term
  258. ^- belt
  259. ~| missing-terminal+nam
  260. (~(got by terms) nam)
  261. --
  262. ::
  263. ++ make-dynamic-map
  264. ~/ %make-dynamic-map
  265. |= raw-terms=(list belt)
  266. ^- (map @ belt)
  267. ~+
  268. ~| "make sure that you are passing in the right number of raw terminals, especially in your test arm"
  269. ?> (gte (lent raw-terms) num-terms) :: ensure we have enough terminals
  270. =/ raw-terms (scag num-terms raw-terms)
  271. %- ~(gas by *(map @ belt))
  272. %+ iturn raw-terms
  273. |= [i=@ term=belt]
  274. [i term]
  275. -- :: dyn
  276. ::
  277. ++ chal :: /lib/challenges
  278. ~/ %chal
  279. |%
  280. ++ c
  281. |= [nam=term tail=(list term)]
  282. ^- (list term)
  283. :^ (crip (weld (trip nam) "-a"))
  284. (crip (weld (trip nam) "-b"))
  285. (crip (weld (trip nam) "-c"))
  286. tail
  287. ::
  288. ++ chal-names-rd1
  289. ^- (list term)
  290. %+ c %a :: tuple packing for ions
  291. %+ c %b
  292. %+ c %c
  293. %+ c %d
  294. %+ c %e
  295. %+ c %f
  296. %+ c %g
  297. %+ c %p :: tuple packing for other tuples
  298. %+ c %q
  299. %+ c %r
  300. %+ c %s
  301. %+ c %t
  302. %+ c %u
  303. %+ c %alf :: stack
  304. ~
  305. ::
  306. +$ ext-chals
  307. $: a=pelt-chal:constraint-util
  308. b=pelt-chal:constraint-util
  309. c=pelt-chal:constraint-util
  310. d=pelt-chal:constraint-util
  311. e=pelt-chal:constraint-util
  312. f=pelt-chal:constraint-util
  313. g=pelt-chal:constraint-util
  314. p=pelt-chal:constraint-util
  315. q=pelt-chal:constraint-util
  316. r=pelt-chal:constraint-util
  317. s=pelt-chal:constraint-util
  318. t=pelt-chal:constraint-util
  319. u=pelt-chal:constraint-util
  320. alf=pelt-chal:constraint-util
  321. ==
  322. ::
  323. ++ init-ext-chals
  324. |= challenges=(list belt)
  325. ^- ext-chals
  326. =/ r
  327. %- make-pelt-chal:constraint-util
  328. ~(r prnd:chal (bp-zip-chals-list:chal chal-names-rd1:chal challenges))
  329. :: alf for stacks, bet for multisets, z for kv store, a-g for tuples
  330. :* (r %a)
  331. (r %b)
  332. (r %c)
  333. (r %d)
  334. (r %e)
  335. (r %f)
  336. (r %g)
  337. (r %p)
  338. (r %q)
  339. (r %r)
  340. (r %s)
  341. (r %t)
  342. (r %u)
  343. (r %alf)
  344. ==
  345. ::
  346. ++ chal-names-rd2
  347. ^- (list term)
  348. %+ c %j
  349. %+ c %k
  350. %+ c %l
  351. %+ c %m
  352. %+ c %n
  353. %+ c %o
  354. %+ c %w
  355. %+ c %x
  356. %+ c %y
  357. %+ c %z :: key-value store
  358. %+ c %bet :: op0 multiset
  359. %+ c %gam :: decoder multiset
  360. ~
  361. ::
  362. ++ chal-names-basic
  363. ^- (list term)
  364. (weld chal-names-rd1 chal-names-rd2)
  365. ::
  366. +$ mega-ext-chals
  367. $: a=pelt-chal:constraint-util
  368. b=pelt-chal:constraint-util
  369. c=pelt-chal:constraint-util
  370. d=pelt-chal:constraint-util
  371. e=pelt-chal:constraint-util
  372. f=pelt-chal:constraint-util
  373. g=pelt-chal:constraint-util
  374. p=pelt-chal:constraint-util
  375. q=pelt-chal:constraint-util
  376. r=pelt-chal:constraint-util
  377. s=pelt-chal:constraint-util
  378. t=pelt-chal:constraint-util
  379. u=pelt-chal:constraint-util
  380. alf=pelt-chal:constraint-util
  381. j=pelt-chal:constraint-util
  382. k=pelt-chal:constraint-util
  383. l=pelt-chal:constraint-util
  384. m=pelt-chal:constraint-util
  385. n=pelt-chal:constraint-util
  386. o=pelt-chal:constraint-util
  387. w=pelt-chal:constraint-util
  388. x=pelt-chal:constraint-util
  389. y=pelt-chal:constraint-util
  390. z=pelt-chal:constraint-util
  391. bet=pelt-chal:constraint-util
  392. gam=pelt-chal:constraint-util
  393. ==
  394. ::
  395. ++ init-mega-ext-chals
  396. |= challenges=(list belt)
  397. ^- mega-ext-chals
  398. =/ r
  399. %- make-pelt-chal:constraint-util
  400. ~(r prnd:chal (bp-zip-chals-list:chal chal-names-basic:chal challenges))
  401. :: alf for stacks, bet for multisets, z for kv store, a-g for tuples
  402. :* (r %a)
  403. (r %b)
  404. (r %c)
  405. (r %d)
  406. (r %e)
  407. (r %f)
  408. (r %g)
  409. (r %p)
  410. (r %q)
  411. (r %r)
  412. (r %s)
  413. (r %t)
  414. (r %u)
  415. (r %alf)
  416. (r %j)
  417. (r %k)
  418. (r %l)
  419. (r %m)
  420. (r %n)
  421. (r %o)
  422. (r %w)
  423. (r %x)
  424. (r %y)
  425. (r %z)
  426. (r %bet)
  427. (r %gam)
  428. ==
  429. ::
  430. ++ chals-derived
  431. ^- (list term)
  432. :: for now we just have inverses but other could be added if needed
  433. %+ c %inv-alf
  434. %+ c %input
  435. ~
  436. ::
  437. ++ chal-names-all
  438. ^- (list term)
  439. (weld chal-names-basic chals-derived)
  440. ::
  441. ++ num-chals (lent chal-names-basic)
  442. ::
  443. ++ num-chals-rd1 (lent chal-names-rd1)
  444. ::
  445. ++ num-chals-rd2 (lent chal-names-rd2)
  446. ::
  447. ::
  448. :: +rnd: randomness symbols for use in constraint graphs
  449. ::
  450. ++ make-chal-mps
  451. ~/ %make-chal-mps
  452. |= [chal-names=(list term)]
  453. ^- (map term mp-mega)
  454. ~+
  455. %- ~(gas by *(map term mp-mega))
  456. %+ iturn chal-names
  457. |= [i=@ nam=term]
  458. [nam (mp-chal:mp-to-mega i)]
  459. ::
  460. ++ rnd
  461. |_ chals=(map term mp-mega)
  462. ++ r
  463. |= nam=term
  464. ^- mp-pelt
  465. =/ nam-a (crip (weld (trip nam) "-a"))
  466. =/ nam-b (crip (weld (trip nam) "-b"))
  467. =/ nam-c (crip (weld (trip nam) "-c"))
  468. ?> (~(has by chals) nam-a)
  469. ?> (~(has by chals) nam-b)
  470. ?> (~(has by chals) nam-c)
  471. :+ (~(got by chals) nam-a)
  472. (~(got by chals) nam-b)
  473. (~(got by chals) nam-c)
  474. --
  475. ::
  476. :: +prnd: populated randomness felt values for use in extending a table
  477. ++ prnd
  478. |_ chals=(map term belt)
  479. ++ r
  480. |= nam=term
  481. ^- belt
  482. ~| missing-challenge+nam
  483. (~(got by chals) nam)
  484. --
  485. ::
  486. ++ make-challenge-map
  487. ~/ %make-challenge-map
  488. |= [raw-chals=(list belt) [s=* f=*]]
  489. ^- (map @ belt)
  490. ~+
  491. ~| "make sure that you are passing in the right number of raw challenges, especially in your test arm"
  492. ?> (gte (lent raw-chals) num-chals) :: ensure we have enough random values
  493. =/ raw-chals (scag num-chals raw-chals)
  494. =/ named-chals
  495. %- ~(gas by *(map term belt))
  496. (zip-up chal-names-basic raw-chals)
  497. =/ a (got-pelt named-chals %a)
  498. =/ b (got-pelt named-chals %b)
  499. =/ c (got-pelt named-chals %c)
  500. =/ alf (got-pelt named-chals %alf)
  501. =/ inv-alf
  502. (pinv (got-pelt named-chals %alf))
  503. =/ input-ifp
  504. =- (compress-pelt ~[a b c] ~[size dyck leaf]:-)
  505. (build-tree-data:constraint-util s alf)
  506. =. raw-chals
  507. %+ weld raw-chals
  508. :~ (~(snag bop [3 inv-alf]) 0)
  509. (~(snag bop [3 inv-alf]) 1)
  510. (~(snag bop [3 inv-alf]) 2)
  511. (~(snag bop [3 input-ifp]) 0)
  512. (~(snag bop [3 input-ifp]) 1)
  513. (~(snag bop [3 input-ifp]) 2)
  514. ==
  515. ::~[a.inv-alf b.inv-alf c.inv-alf]
  516. ::~[a.input-ifp b.input-ifp c.input-ifp]
  517. %- ~(gas by *(map @ belt))
  518. %+ iturn raw-chals
  519. |= [i=@ chal=belt]
  520. [i chal]
  521. ::
  522. ++ zip-chals
  523. ~/ %zip-chals
  524. |= [names=(set term) raw-chals=(list felt)]
  525. (~(gas by *(map term felt)) (zip ~(tap in names) raw-chals same))
  526. ::
  527. ++ bp-zip-chals
  528. ~/ %bp-zip-chals
  529. |= [names=(set term) raw-chals=(list belt)]
  530. (~(gas by *(map term belt)) (zip ~(tap in names) raw-chals same))
  531. ::
  532. ++ bp-zip-chals-list
  533. ~/ %bp-zip-chals
  534. |= [names=(list term) raw-chals=(list belt)]
  535. (~(gas by *(map term belt)) (zip names raw-chals same))
  536. -- ::challenges
  537. ::
  538. ++ terminal :: /lib/terminal ::TODO this is a core with one arm - move elsewhere?
  539. =, mp-to-graph
  540. ~% %terminal ..terminal ~
  541. |%
  542. :: +gen-consistency-checks: assist with validating the terminals map
  543. ::
  544. :: the purpose of this arm is to assist with validating the terminals map in sam of terminal-constraints
  545. ::
  546. :: the terminals map is out-of-band data, i.e., it is not constrained and is to be considered untrusted
  547. :: thus, we need to manually ensure, per table, that all values in the inner map are correct.
  548. ::
  549. :: we also need to assert that the list of column names (inner map's keys) are a (non-strict) subset
  550. :: of the full column names of the table
  551. :: we will generate equality constraints for each
  552. ::
  553. :: extraneous-columns := terminal-columns - all-columns
  554. ++ gen-consistency-checks
  555. ~/ %gen-consistency-checks
  556. |= [our-terminals=(map term felt) our-columns=(list term) v=$-(@tas mp-graph)]
  557. ^- (list mp-graph)
  558. =/ terminal-columns ~(key by our-terminals)
  559. =/ all-columns (~(gas in *(set col-name)) our-columns)
  560. =/ extraneous-columns (~(dif in terminal-columns) all-columns)
  561. ?> =(~ extraneous-columns)
  562. %+ turn ~(tap in terminal-columns)
  563. |= col=@tas
  564. ^- mp-graph
  565. =/ term-val (~(got by our-terminals) col)
  566. :: terminal constraint:
  567. ::
  568. :: <col> = <terminal-val>,
  569. :: <col> - <terminal-val> = 0
  570. :: for all col in table columns names
  571. :: for all terminal-val in claimed terminal values received OOB for this table
  572. ::
  573. (mpsub (v col) (mp-c term-val))
  574. -- ::terminal
  575. ::
  576. ++ tlib :: /lib/table
  577. ~/ %tlib
  578. =/ util constraint-util ::TODO =/ vs =*?
  579. |%
  580. ::
  581. :: +height: target padding height for given table is the next smallest power of 2
  582. ::
  583. :: e.g. if (lent p.table) == 5, (height table) == 8
  584. ++ height
  585. ~/ %height
  586. |= =table
  587. ^- @
  588. ~+
  589. =/ len (lent p.table)
  590. ?: =(len 0) 0
  591. (bex (xeb (dec len)))
  592. ::
  593. ++ height-mary
  594. ~/ %height-mary
  595. |= p=mary
  596. ^- @
  597. ~+
  598. =/ len len.array.p
  599. ?: =(len 0) 0
  600. (bex (xeb (dec len)))
  601. ::
  602. ++ weld-exts
  603. ~/ %weld-exts
  604. |= [l=table-mary r=table-mary]
  605. ^- table-mary
  606. ?> =(-.l -.r)
  607. :- -.l
  608. (~(weld-step ave p.l) p.r)
  609. ::
  610. :: TODO make some of these functions a door
  611. ++ table-to-verifier-funcs
  612. ~/ %table-to-verifier-funcs
  613. :: this arm is theoretically a temporary shim to assist the separation of the codebase
  614. :: ideally this disappears after sufficient surgery but may be here to stay
  615. |= fs=table-funcs
  616. ^- verifier-funcs
  617. |%
  618. ++ boundary-constraints boundary-constraints:fs
  619. ++ row-constraints row-constraints:fs
  620. ++ transition-constraints transition-constraints:fs
  621. ++ terminal-constraints terminal-constraints:fs
  622. ++ extra-constraints extra-constraints:fs
  623. --
  624. ::
  625. :: (representing the state of the computation at each step), but rather
  626. :: a list of those base elements *lifted* into extension field elements.
  627. ::
  628. :: Thus, fpoly is being used to represent a generic list of felts
  629. :: not any specific polynomial with felt coefficients.
  630. ::
  631. :: TODO the following 2 arms could go into the tab door and be renamed shorter
  632. ++ belt2d-to-matrix
  633. ~/ %belt2d-to-matrix
  634. |= btable=(list (list belt))
  635. ^- matrix
  636. (turn btable lift-to-fpoly)
  637. ::
  638. ++ matrix-to-belt2d
  639. ~/ %matrix-to-belt2d
  640. |= mat=matrix
  641. ^- (list (list))
  642. (turn mat row-to-belts)
  643. ::
  644. ++ row-to-belts
  645. ~/ %row-to-belts
  646. |= row=row
  647. ^- (list)
  648. (turn ~(to-poly fop row) drop)
  649. ::
  650. :: +var: helper door. allows for terse `(v %idx)` style variable accesses
  651. ::
  652. :: after initializing with a variables map
  653. ::
  654. ++ var
  655. |_ variables=(map term mp-mega)
  656. ++ v
  657. |= nam=term
  658. ^- mp-mega
  659. ~+
  660. ~| var-not-found+nam
  661. (~(got by variables) nam)
  662. --
  663. ++ var-pelt
  664. |_ variables=(map term mp-mega)
  665. ++ v
  666. |= nam=term
  667. ^- mp-pelt
  668. ~+
  669. ~| var-not-found+nam
  670. :+ (~(got by variables) (crip (weld (trip nam) "-a")))
  671. (~(got by variables) (crip (weld (trip nam) "-b")))
  672. (~(got by variables) (crip (weld (trip nam) "-c")))
  673. ::
  674. ++ v-n
  675. |= nam=term
  676. ^- mp-pelt
  677. ~+
  678. ~| var-not-found+nam
  679. :+ (~(got by variables) (crip (weld (trip nam) "-a-n")))
  680. (~(got by variables) (crip (weld (trip nam) "-b-n")))
  681. (~(got by variables) (crip (weld (trip nam) "-c-n")))
  682. ::
  683. ++ c
  684. |= nam=term
  685. ^- mp-pelt
  686. ~+
  687. ~| var-not-found+nam
  688. =/ mp (~(got by variables) nam)
  689. [mp mp mp]
  690. --
  691. ::
  692. :: +make-vars:
  693. ::
  694. :: given a list of variable names (i.e., column names),
  695. :: produce a map from variable names to corresponding mp-graph
  696. ++ make-vars
  697. ~/ %make-vars
  698. |= [var-names=(list col-name)]
  699. |^ ^- (map col-name mp-mega)
  700. ~+
  701. ::
  702. :: Equivalent to:
  703. ::
  704. :: %- ~(gas by (map term mp-graph:f))
  705. :: :~ [%idx (make-variable 0)]
  706. :: [%a (make-variable 1)]
  707. :: ...
  708. :: [%a-n (make-variable 2*n)]
  709. :: ==
  710. ::
  711. =/ num-succ 1
  712. :: is the number of successors to generate
  713. :: hardcoded to 1 because that is all the current stark impl supports
  714. :: i.e., cannot have constraints of the form idx'' = idx' + 1
  715. ::
  716. =/ successor-names=(list col-name)
  717. :: flat list of succesors ~[%idx-n %a-n %idx-n-n %a-n-n]
  718. %- zing
  719. ^- (list (list col-name))
  720. :: list of ith successors idents for all i
  721. :: e.g. ~[[%idx-n %a-n] ~[%idx-n-n %a-n-n]]
  722. %+ turn (gulf 1 num-succ)
  723. |= succ-num=@
  724. :: a list of all ith successor idents e.g. ~[%idx-n %a-n]
  725. (turn var-names |=(nam=col-name `@tas`(successor-name nam succ-num)))
  726. ::
  727. =/ vars-all (weld var-names successor-names)
  728. :: produce the final map of all var-names to mp-graphs
  729. %- ~(gas by *(map col-name mp-mega))
  730. %+ iturn vars-all
  731. |= [i=@ var=col-name]
  732. [var (mp-var:mp-to-mega i)]
  733. ::
  734. ++ successor-name
  735. |= [nam=col-name n=@]
  736. ^- col-name
  737. :: example: when n=2, idx -> xdi -> -n-nxdi -> idx-n-n
  738. :: idk why it works but it does lol
  739. (crip (flop (runt [n '-n'] (flop (trip nam)))))
  740. --
  741. ::
  742. ++ weighted-sum
  743. ~/ %weighted-sum
  744. |= [=row weights=fpoly]
  745. ^- felt
  746. ?> =(len.row len.weights)
  747. %+ roll
  748. ~(to-poly fop (~(zip fop row) weights fmul))
  749. fadd
  750. ::
  751. ++ static-unit-distance
  752. ~/ %static-unit-distance
  753. |= [domain-len=@ height=@]
  754. ^- @
  755. ?: =(height 0)
  756. 0
  757. (div domain-len height)
  758. -- ::tlib
  759. ::
  760. :: +jlib: parts of lib/jute that arent related to particular jutes
  761. ++ jlib
  762. |%
  763. :: +compute-jute-map
  764. ::
  765. :: map jute name -> index. used to pick which selector goes with which jute.
  766. :: we sort the list so the order is deterministic.
  767. ++ compute-jute-map
  768. |= jutes=(list [@tas sam=* prod=*])
  769. ^- jute-map
  770. ?~ jutes ~
  771. =/ jute-list=(list term)
  772. %- sort
  773. :_ gth
  774. %~ tap in
  775. %- ~(gas in *(set @tas))
  776. (turn jutes |=([name=@tas *] name))
  777. %- ~(gas by *(map term @))
  778. (zip-up jute-list (range (lent jute-list)))
  779. --
  780. ::
  781. ++ zkvm-debug
  782. |%
  783. +| %tables
  784. ::TODO see if this can be made into a door once table structs are more stable
  785. ++ print-row
  786. |= [row=belt-row names=(list term)]
  787. ^- @
  788. ~& > "["
  789. =+
  790. %+ turn (zip-up row names)
  791. |= [c=belt name=term]
  792. ~& > "{<name>}:{<c>}"
  793. 0
  794. ~& > "]"
  795. 0
  796. ::
  797. ++ print-table
  798. |= col-names=(list col-name)
  799. |= tab=table-mary
  800. ^- table-mary
  801. =- tab
  802. %+ turn (range len.array.p.tab)
  803. |= i=@
  804. =/ row=bpoly (~(snag-as-bpoly ave p.tab) i)
  805. ~& > "row={<i>}"
  806. (print-row (bpoly-to-list row) col-names)
  807. ::
  808. ++ test
  809. |= [table=table-mary [s=* f=*]]
  810. |= $: challenges=(list belt)
  811. dynamics=bpoly
  812. funcs=verifier-funcs
  813. ==
  814. :: TODO maybe add a should-fail flag that silences failed constraints or modifies printf
  815. :: or change signature to surface error unit?
  816. =/ challenge-map=(map term belt)
  817. (make-challenge-map:chal challenges s f)
  818. |^ ^- ?
  819. =/ bound-fail (run-bounds boundary-constraints:funcs dynamics)
  820. ?. ?=(~ bound-fail)
  821. ~&((need bound-fail) %.n)
  822. =/ term-fail (run-terms terminal-constraints:funcs dynamics)
  823. ?. ?=(~ term-fail)
  824. ~&((need term-fail) %.n)
  825. ?: =(len.array.p.table 0)
  826. %.n
  827. ?: =(len.array.p.table 1)
  828. %.y :: 1 row table automatically passes transition constraints
  829. =/ row-fail (run-rows row-constraints:funcs dynamics)
  830. ?. ?=(~ row-fail)
  831. ~&((need row-fail) %.n)
  832. =/ trans-fail (run-trans transition-constraints:funcs dynamics)
  833. ?. ?=(~ trans-fail)
  834. ~&((need trans-fail) %.n)
  835. %.y
  836. ::
  837. ++ run-bounds
  838. |= [boundary-constraints-labeled=(map @tas mp-ultra) dynamics=bpoly]
  839. %+ mevy ~(tap by boundary-constraints-labeled)
  840. |= [name=@tas constraint=mp-ultra]
  841. =/ point (~(snag-as-bpoly ave p.table) 0)
  842. =/ eval (mpeval-ultra %base constraint point challenge-map dynamics)
  843. ?: (levy eval |=(b=belt =(b 0))) ~
  844. %- some
  845. :* %constraint-failed-bounds
  846. table=name.table
  847. name=name
  848. row=~(to-poly bop point)
  849. result=eval
  850. ==
  851. ::
  852. ++ run-terms
  853. |= [terminal-constraints-labeled=(map @tas mp-ultra) dynamics=bpoly]
  854. %+ mevy ~(tap by terminal-constraints-labeled)
  855. |= [name=@tas constraint=mp-ultra]
  856. =/ last (dec len.array.p.table)
  857. =/ point (~(snag-as-bpoly ave p.table) last)
  858. =/ eval (mpeval-ultra %base constraint point challenge-map dynamics)
  859. ?: (levy eval |=(b=belt =(b 0))) ~
  860. %- some
  861. :* %constraint-failed-terms
  862. table=name.table
  863. name=name
  864. row=~(to-poly bop point)
  865. result=eval
  866. ==
  867. ::
  868. ++ run-rows
  869. |= [row-constraints-labeled=(map @tas mp-ultra) dynamics=bpoly]
  870. :: produces ~ if all constraints pass on all points
  871. :: and [~ err] on first error
  872. %+ mevy ~(tap by row-constraints-labeled)
  873. :: following gate produces ~ if given constraint passes on all points
  874. :: and [~ err] on first error
  875. |= [name=@tas constraint=mp-ultra]
  876. %+ mevy (range len.array.p.table)
  877. |= i=@
  878. =/ point (~(snag-as-bpoly ave p.table) i)
  879. =/ eval (mpeval-ultra %base constraint point challenge-map dynamics)
  880. ?: (levy eval |=(b=belt =(b 0))) ~
  881. %- some
  882. :* %constraint-failed-row
  883. table=name.table
  884. name=name
  885. row-num=i
  886. row=~(to-poly bop point)
  887. result=eval
  888. ==
  889. ::
  890. ++ run-trans
  891. |= [transition-constraints-labeled=(map @tas mp-ultra) dynamics=bpoly]
  892. :: produces ~ if all constraints pass on all points
  893. :: and [~ err] on first error
  894. %+ mevy ~(tap by transition-constraints-labeled)
  895. :: following gate produces ~ if given constraint passes on all points
  896. :: and [~ err] on first error
  897. |= [name=@tas constraint=mp-ultra]
  898. %+ mevy (range (dec len.array.p.table))
  899. |= i=@
  900. =/ point (~(snag-as-bpoly ave p.table) i)
  901. =/ next-point (~(snag-as-bpoly ave p.table) +(i))
  902. =/ combo-point (~(weld bop point) next-point)
  903. =/ eval (mpeval-ultra %base constraint combo-point challenge-map dynamics)
  904. ?: (levy eval |=(b=belt =(b 0))) ~
  905. %- some
  906. :* %constraint-failed-trans
  907. table=name.table
  908. name=name
  909. row-num=i
  910. row=~(to-poly bop point)
  911. next-row=~(to-poly bop next-point)
  912. result=eval
  913. ==
  914. ::
  915. ++ labeled-constraints
  916. |= [constraints=(list mp-ultra) prefix=tape]
  917. =/ len (lent constraints)
  918. %- ~(gas by *(map @tas mp-ultra))
  919. (zip (make-labels prefix len) constraints same)
  920. ::
  921. ++ make-labels
  922. |= [prefix=tape n=@]
  923. ^- (list @t)
  924. ?: =(n 0) ~
  925. %+ turn (range 1 (add 1 n))
  926. |= i=@
  927. ^- term
  928. (crip (welp prefix (scot %ud i)^~))
  929. --
  930. --
  931. --