seven.hoon 26 KB

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