seven.hoon 26 KB

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