five.hoon 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. /= ztd-four /common/ztd/four
  2. => ztd-four
  3. ~% %utils ..proof-path ~
  4. :: utils
  5. |%
  6. ++ proof-stream :: /lib/proof-stream
  7. ~% %proof-stream +> ~
  8. |_ proof
  9. ++ push
  10. ~/ %push
  11. |= dat=proof-data
  12. ^- proof
  13. :+ (snoc objects dat)
  14. (snoc hashes (hash-hashable:tip5 (hashable-proof-data dat)))
  15. read-index
  16. ::
  17. ++ pull
  18. ^- [proof-data proof]
  19. ?> (lth read-index (lent objects))
  20. =/ dat (snag read-index objects)
  21. :- dat
  22. :+ objects
  23. (snoc hashes (hash-hashable:tip5 (hashable-proof-data dat)))
  24. +(read-index)
  25. ::
  26. ++ prover-fiat-shamir
  27. ^+ tog:tip5
  28. (absorb-proof-objects objects hashes)
  29. ::
  30. ++ verifier-fiat-shamir
  31. ^+ tog:tip5
  32. =/ objects=(list proof-data) (scag read-index objects)
  33. =/ hashes=(list noun-digest:tip5) (scag read-index hashes)
  34. (absorb-proof-objects objects hashes)
  35. -- ::proof-stream
  36. ::
  37. +$ mp-pelt [a=mp-mega b=mp-mega c=mp-mega]
  38. +$ mp-pelt-comp [dep=mp-pelt com=mp-pelt]
  39. ::
  40. :: triple belt
  41. ++ rack
  42. |= b=belt
  43. ^- [belt belt belt]
  44. [b b b]
  45. ::
  46. :: raise belt
  47. ++ reck
  48. |= b=belt
  49. ^- [belt belt belt]
  50. [b 0 0]
  51. ::
  52. ++ constraint-util :: /lib/constraint-util
  53. =, mp-to-mega
  54. ~% %constraint-util ..constraint-util ~
  55. |%
  56. ++ unlabel-constraints
  57. ~/ %unlabel-constraints
  58. |= mp=(map term mp-ultra)
  59. ^- (list mp-ultra)
  60. ~+
  61. (turn ~(tap by mp) tail)
  62. ::
  63. ++ lift-to-mp-pelt
  64. |= m=mp-mega
  65. ^- mp-pelt
  66. [m *mp-mega *mp-mega]
  67. ::
  68. ++ mpadd-pelt
  69. |= [p=mp-pelt q=mp-pelt]
  70. ^- mp-pelt
  71. :+ (mpadd a.p a.q)
  72. (mpadd b.p b.q)
  73. (mpadd c.p c.q)
  74. ::
  75. ++ mpsub-pelt
  76. |= [p=mp-pelt q=mp-pelt]
  77. ^- mp-pelt
  78. :+ (mpsub a.p a.q)
  79. (mpsub b.p b.q)
  80. (mpsub c.p c.q)
  81. ::
  82. ++ mpcomp-pelt
  83. |= [p=mp-pelt q=mp-pelt]
  84. ^- mp-pelt-comp
  85. [dep=p com=q]
  86. ::
  87. ++ mpmul-pelt
  88. |= [p=mp-pelt q=mp-pelt]
  89. ^- mp-pelt
  90. =/ m0 (mpmul a.p a.q)
  91. =/ m1 (mpmul b.p b.q)
  92. =/ m2 (mpmul c.p c.q)
  93. =/ n01
  94. %+ mpsub
  95. (mpmul (mpadd a.p b.p) (mpadd a.q b.q))
  96. (mpadd m0 m1)
  97. =/ n02
  98. %+ mpsub
  99. (mpmul (mpadd a.p c.p) (mpadd a.q c.q))
  100. (mpadd m0 m2)
  101. =/ n12
  102. %+ mpsub
  103. (mpmul (mpadd b.p c.p) (mpadd b.q c.q))
  104. (mpadd m1 m2)
  105. :+ (mpsub m0 n12)
  106. (mpsub (mpadd n01 n12) m2)
  107. :(mpadd n02 m1 m2)
  108. ::
  109. :: pass in m=(mp-c c) to scale by belt constant c
  110. ++ mpscal-pelt
  111. |= [m=mp-mega p=mp-pelt]
  112. ^- mp-pelt
  113. :+ (mpmul m a.p)
  114. (mpmul m b.p)
  115. (mpmul m c.p)
  116. ::
  117. ++ lift-to-mega
  118. |= =mp-mega
  119. ^- mp-ultra
  120. [%mega mp-mega]
  121. ::
  122. ++ tag-mp-comp
  123. |= [name=term mp=mp-pelt-comp tail=(list [term mp-ultra])]
  124. ^- (list [term mp-ultra])
  125. :- :- (crip (weld (trip name) "-comp"))
  126. :+ %comp
  127. ~[a.dep.mp b.dep.mp c.dep.mp]
  128. ~[a.com.mp b.com.mp c.com.mp]
  129. tail
  130. ::
  131. ++ tag-mp-pelt
  132. |= [name=term =mp-pelt tail=(list [term mp-ultra])]
  133. ^- (list [term mp-ultra])
  134. :^ [(crip (weld (trip name) "-a")) [%mega a.mp-pelt]]
  135. [(crip (weld (trip name) "-b")) [%mega b.mp-pelt]]
  136. [(crip (weld (trip name) "-c")) [%mega c.mp-pelt]]
  137. tail
  138. ::
  139. ++ untagged-mp-pelt
  140. |= [=mp-pelt tail=(list mp-ultra)]
  141. ^- (list mp-ultra)
  142. :* (lift-to-mega a.mp-pelt)
  143. (lift-to-mega b.mp-pelt)
  144. (lift-to-mega c.mp-pelt)
  145. tail
  146. ==
  147. ::
  148. ++ pelt-col
  149. |= [name=term tail=(list term)]
  150. ^- (list term)
  151. :^ (crip (weld (trip name) "-a"))
  152. (crip (weld (trip name) "-b"))
  153. (crip (weld (trip name) "-c"))
  154. tail
  155. ::
  156. +$ pelt-chal @ux
  157. ++ make-pelt-chal
  158. |= r=$-(term belt)
  159. |= name=term
  160. ^- pelt-chal
  161. =< dat
  162. %- init-bpoly
  163. :~ (r (crip (weld (trip name) "-a")))
  164. (r (crip (weld (trip name) "-b")))
  165. (r (crip (weld (trip name) "-c")))
  166. ==
  167. ::
  168. +$ felt-stack
  169. $: alf=felt
  170. alf-inv=felt
  171. len=@
  172. dat=felt
  173. ==
  174. ::
  175. ++ init-fstack
  176. ~/ %init-fstack
  177. |= alf=felt
  178. ^- felt-stack
  179. =/ alf-inv=felt (finv alf)
  180. [alf alf-inv 0 (lift 0)]
  181. ::
  182. :: +fstack: door for working with a $felt-stack
  183. ::
  184. :: bottom [a b c] top
  185. :: empty stack [] <-> dat 0
  186. ++ fstack
  187. ~/ %fstack
  188. |_ fs=felt-stack
  189. ++ push
  190. :: [a b c] => [a b c x]
  191. ~/ %push
  192. |= x=felt
  193. ^- felt-stack
  194. fs(len +(len.fs), dat (fadd (fmul dat.fs alf.fs) x))
  195. ++ pop
  196. :: [a b c x] => [a b c]
  197. ~/ %pop
  198. |= x=felt
  199. ^- felt-stack
  200. ?> (gth len.fs 0)
  201. fs(len (dec len.fs), dat (fmul (fsub dat.fs x) alf-inv.fs))
  202. ++ push-all
  203. :: [a b c] => [a b c x1 ... xn]
  204. ~/ %push-all
  205. |= xs=(list felt)
  206. ^- felt-stack
  207. %+ roll xs
  208. |= [x=felt fs-new=_fs]
  209. (~(push fstack fs-new) x)
  210. ++ push-bottom
  211. :: [a b c] => [x a b c]
  212. ~/ %push-bottom
  213. |= x=felt
  214. ^- felt-stack
  215. :: alf^len * x + dat.fs
  216. fs(len +(len.fs), dat (fadd (fmul (fpow alf.fs len.fs) x) dat.fs))
  217. ++ push-bottom-all
  218. :: [a b c] => [x0 ... xn a b c]
  219. ~/ %push-bottom-all
  220. |= xs=(list felt)
  221. ^- felt-stack
  222. %+ roll (flop xs)
  223. :: let sx = (flop xs)
  224. :: [a b c] => [sx2 sx1 sx0 a b c]
  225. :: = [a b c] => [xs0 sx1 sx2 a b c]
  226. |= [x=felt fs-new=_fs]
  227. (~(push-bottom fstack fs-new) x)
  228. ++ cons
  229. :: stack cons: [a b], [c d] => [a b c d]
  230. ~/ %cons
  231. |= other=felt-stack
  232. ^- felt-stack
  233. ?> =(alf.fs alf.other)
  234. :: alf^len(other) * dat.fs + dat.other
  235. %_ fs
  236. len (add len.fs len.other)
  237. dat (fadd (fmul (fpow alf.fs len.other) dat.fs) dat.other)
  238. ==
  239. ++ pop-all
  240. ~/ %pop-all
  241. |= xs=(list felt)
  242. ^- felt-stack
  243. %+ roll xs
  244. |= [x=felt fs-new=_fs]
  245. ?> (gth len.fs 0)
  246. (~(pop fstack fs-new) x)
  247. ::
  248. ++ is-empty =(len.fs 0)
  249. --
  250. ::
  251. +$ belt-stack
  252. $: alf=belt
  253. alf-inv=belt
  254. len=@
  255. dat=belt
  256. ==
  257. ::
  258. ++ init-bstack
  259. ~/ %init-bstack
  260. |= alf=belt
  261. ^- belt-stack
  262. =/ alf-inv=belt (binv alf)
  263. [alf alf-inv 0 0]
  264. ::
  265. :: +bstack: door for working with a $belt-stack
  266. ::
  267. :: bottom [a b c] top
  268. :: empty stack [] <-> dat 0
  269. ++ bstack
  270. ~/ %bstack
  271. |_ bs=belt-stack
  272. ++ push
  273. :: [a b c] => [a b c x]
  274. ~/ %push
  275. |= x=belt
  276. ^- belt-stack
  277. bs(len +(len.bs), dat (badd (bmul dat.bs alf.bs) x))
  278. ++ pop
  279. :: [a b c x] => [a b c]
  280. ~/ %pop
  281. |= x=belt
  282. ^- belt-stack
  283. ?> (gth len.bs 0)
  284. bs(len (dec len.bs), dat (bmul (bsub dat.bs x) alf-inv.bs))
  285. ++ push-all
  286. :: [a b c] => [a b c x1 ... xn]
  287. ~/ %push-all
  288. |= xs=(list belt)
  289. ^- belt-stack
  290. %+ roll xs
  291. |= [x=belt bs-new=_bs]
  292. (~(push bstack bs-new) x)
  293. ++ push-bottom
  294. :: [a b c] => [x a b c]
  295. ~/ %push-bottom
  296. |= x=belt
  297. ^- belt-stack
  298. :: alf^len * x + dat.fs
  299. bs(len +(len.bs), dat (badd (bmul (bpow alf.bs len.bs) x) dat.bs))
  300. ++ push-bottom-all
  301. :: [a b c] => [x0 ... xn a b c]
  302. ~/ %push-bottom-all
  303. |= xs=(list belt)
  304. ^- belt-stack
  305. %+ roll (flop xs)
  306. :: let sx = (flop xs)
  307. :: [a b c] => [sx2 sx1 sx0 a b c]
  308. :: = [a b c] => [xs0 sx1 sx2 a b c]
  309. |= [x=belt bs-new=_bs]
  310. (~(push-bottom bstack bs-new) x)
  311. ++ cons
  312. :: stack cons: [a b], [c d] => [a b c d]
  313. ~/ %cons
  314. |= other=belt-stack
  315. ^- belt-stack
  316. ?> =(alf.bs alf.other)
  317. :: alf^len(other) * dat.bs + dat.other
  318. %_ bs
  319. len (add len.bs len.other)
  320. dat (badd (bmul (bpow alf.bs len.other) dat.bs) dat.other)
  321. ==
  322. ++ pop-all
  323. ~/ %pop-all
  324. |= xs=(list belt)
  325. ^- belt-stack
  326. %+ roll xs
  327. |= [x=belt bs-new=_bs]
  328. ?> (gth len.bs 0)
  329. (~(pop bstack bs-new) x)
  330. ::
  331. ++ is-empty =(len.bs 0)
  332. --
  333. ::
  334. +$ pelt-stack
  335. $: alf=pelt
  336. alf-inv=pelt
  337. len=@
  338. dat=pelt
  339. ==
  340. ::
  341. ++ init-pstack
  342. ~/ %init-pstack
  343. |= alf=pelt
  344. ^- pelt-stack
  345. =/ alf-inv=pelt (pinv alf)
  346. [alf alf-inv 0 (pelt-lift 0)]
  347. ::
  348. :: +pstack: door for working with a $pelt-stack
  349. ::
  350. :: bottom [a b c] top
  351. :: empty stack [] <-> dat 0
  352. ++ pstack
  353. ~/ %pstack
  354. |_ ps=pelt-stack
  355. ++ push
  356. :: [a b c] => [a b c x]
  357. ~/ %push
  358. |= x=pelt
  359. ^- pelt-stack
  360. ps(len +(len.ps), dat (padd (pmul dat.ps alf.ps) x))
  361. ++ pop
  362. :: [a b c x] => [a b c]
  363. ~/ %pop
  364. |= x=pelt
  365. ^- pelt-stack
  366. ?> (gth len.ps 0)
  367. ps(len (dec len.ps), dat (pmul (psub dat.ps x) alf-inv.ps))
  368. ++ push-all
  369. :: [a b c] => [a b c x1 ... xn]
  370. ~/ %push-all
  371. |= xs=(list pelt)
  372. ^- pelt-stack
  373. %+ roll xs
  374. |= [x=pelt ps-new=_ps]
  375. (~(push pstack ps-new) x)
  376. ++ push-bottom
  377. :: [a b c] => [x a b c]
  378. ~/ %push-bottom
  379. |= x=pelt
  380. ^- pelt-stack
  381. :: alf^len * x + dat.fs
  382. ps(len +(len.ps), dat (padd (pmul (ppow alf.ps len.ps) x) dat.ps))
  383. ++ push-bottom-all
  384. :: [a b c] => [x0 ... xn a b c]
  385. ~/ %push-bottom-all
  386. |= xs=(list pelt)
  387. ^- pelt-stack
  388. %+ roll (flop xs)
  389. :: let sx = (flop xs)
  390. :: [a b c] => [sx2 sx1 sx0 a b c]
  391. :: = [a b c] => [xs0 sx1 sx2 a b c]
  392. |= [x=pelt ps-new=_ps]
  393. (~(push-bottom pstack ps-new) x)
  394. ++ cons
  395. :: stack cons: [a b], [c d] => [a b c d]
  396. ~/ %cons
  397. |= other=pelt-stack
  398. ^- pelt-stack
  399. ?> =(alf.ps alf.other)
  400. :: alf^len(other) * dat.bs + dat.other
  401. %_ ps
  402. len (add len.ps len.other)
  403. dat (padd (pmul (ppow alf.ps len.other) dat.ps) dat.other)
  404. ==
  405. ++ pop-all
  406. ~/ %pop-all
  407. |= xs=(list pelt)
  408. ^- pelt-stack
  409. %+ roll xs
  410. |= [x=pelt ps-new=_ps]
  411. ?> (gth len.ps 0)
  412. (~(pop pstack ps-new) x)
  413. ::
  414. ++ is-empty =(len.ps 0)
  415. --
  416. :: utilities for working with log derivative multisets
  417. ::
  418. :: $ld-mset: multiset based on the logarithmic derivative
  419. +$ ld-mset
  420. $~ [(lift 0) (lift 0)]
  421. $: bet=felt :: beta - random challenge for polynomial
  422. dat=felt :: data - actual value of multiset to write into trace
  423. ==
  424. ::
  425. ++ init-ld-mset
  426. |= bet=felt
  427. ^- ld-mset
  428. [bet (lift 0)]
  429. ::
  430. ++ ld-union
  431. |= msets=(list ld-mset)
  432. ^- ld-mset
  433. ?~ msets
  434. *ld-mset
  435. =/ bet bet.i.msets
  436. ?: ?!((levy `(list ld-mset)`msets |=(=ld-mset =(bet.ld-mset bet))))
  437. !!
  438. [bet (roll `(list ld-mset)`msets |=([=ld-mset f=felt] (fadd f dat.ld-mset)))]
  439. ::
  440. :: +ld: door for working with ld-msets
  441. ++ ld
  442. ~/ %ld
  443. |_ ms=ld-mset
  444. ::
  445. :: +add: add f to the multiset n times
  446. ::
  447. :: dat' = dat + n/(bet - f)
  448. ++ add
  449. ~/ %add
  450. |= [f=felt n=@]
  451. ^- ld-mset
  452. :- bet.ms
  453. (fadd dat.ms (fmul (lift n) (finv (fsub bet.ms f))))
  454. :: +add-all: add a list of [felt, multiplicity] pairs to the multiset
  455. ::
  456. :: adds them one at a time starting with ms and returns a list of
  457. :: each intermediate memset in order.
  458. ++ add-all
  459. ~/ %add-all
  460. |= l=(list [f=felt n=@])
  461. ^- (list ld-mset)
  462. %+ spun l
  463. |= [[f=felt n=@] acc=_ms]
  464. =/ ret (~(add ld acc) f n)
  465. [ret ret]
  466. ::
  467. :: +remove: remove a felt n times
  468. ++ remove
  469. |= [f=felt n=@]
  470. ^- ld-mset
  471. :- bet.ms
  472. (fsub dat.ms (fmul (lift n) (finv (fsub bet.ms f))))
  473. ::
  474. :: +union: union multiset ms with multiset ms1
  475. ++ union
  476. |= ms1=ld-mset
  477. ^- ld-mset
  478. :: randomness has to be the same to perform union
  479. ?> =(bet.ms bet.ms1)
  480. :- bet.ms
  481. (fadd dat.ms dat.ms1)
  482. --
  483. ::
  484. :: $ld-mset-bf: multiset based on the logarithmic derivative
  485. +$ ld-mset-bf
  486. $: bet=belt :: beta - random challenge for polynomial
  487. dat=belt :: data - actual value of multiset to write into trace
  488. ==
  489. ::
  490. ++ init-ld-mset-bf
  491. |= bet=belt
  492. ^- ld-mset-bf
  493. [bet 0]
  494. ::
  495. ++ ld-union-bf
  496. |= msets=(list ld-mset-bf)
  497. ^- ld-mset-bf
  498. ?~ msets
  499. *ld-mset-bf
  500. =/ bet bet.i.msets
  501. ?: ?!((levy `(list ld-mset-bf)`msets |=(=ld-mset =(bet.ld-mset bet))))
  502. !!
  503. [bet (roll `(list ld-mset-bf)`msets |=([=ld-mset f=belt] (badd f dat.ld-mset)))]
  504. ::
  505. :: +ld: door for working with ld-msets
  506. ++ ld-bf
  507. ~/ %ld-bf
  508. |_ ms=ld-mset-bf
  509. ::
  510. :: +add: add b to the multiset n times
  511. ::
  512. :: dat' = dat + n/(bet - b)
  513. ++ add
  514. ~/ %add
  515. |= [b=belt n=@]
  516. ^- ld-mset-bf
  517. :- bet.ms
  518. (badd dat.ms (bmul n (binv (bsub bet.ms b))))
  519. :: +add-all: add a list of [belt, multiplicity] pairs to the multiset
  520. ::
  521. :: adds them one at a time starting with ms and returns a list of
  522. :: each intermediate memset in order.
  523. ++ add-all
  524. ~/ %add-all
  525. |= l=(list [b=belt n=@])
  526. ^- (list ld-mset-bf)
  527. %+ spun l
  528. |= [[b=belt n=@] acc=_ms]
  529. =/ ret (~(add ld acc) b n)
  530. [ret ret]
  531. ::
  532. :: +remove: remove a belt n times
  533. ++ remove
  534. |= [b=belt n=@]
  535. ^- ld-mset-bf
  536. :- bet.ms
  537. (bsub dat.ms (bmul n (binv (bsub bet.ms b))))
  538. ::
  539. :: +union: union multiset ms with multiset ms1
  540. ++ union
  541. |= ms1=ld-mset-bf
  542. :: randomness has to be the same to perform union
  543. ?> =(bet.ms bet.ms1)
  544. :- bet.ms
  545. (badd dat.ms dat.ms1)
  546. --
  547. ::
  548. :: $ld-mset-pelt: multiset based on the logarithmic derivative
  549. +$ ld-mset-pelt
  550. $~ [pzero pzero]
  551. $: bet=pelt :: beta - random challenge for polynomial
  552. dat=pelt :: data - actual value of multiset to write into trace
  553. ==
  554. ::
  555. ++ init-ld-mset-pelt
  556. |= bet=pelt
  557. ^- ld-mset-pelt
  558. [bet pzero]
  559. ::
  560. ++ ld-pelt-union
  561. |= msets=(list ld-mset-pelt)
  562. ^- ld-mset-pelt
  563. ?~ msets
  564. *ld-mset-pelt
  565. =/ bet bet.i.msets
  566. ?: ?!((levy `(list ld-mset-pelt)`msets |=(=ld-mset-pelt =(bet.ld-mset-pelt bet))))
  567. !!
  568. [bet (roll `(list ld-mset-pelt)`msets |=([=ld-mset-pelt p=pelt] (padd p dat.ld-mset-pelt)))]
  569. ::
  570. :: +ld: door for working with ld-msets
  571. ++ ld-pelt
  572. ~/ %ld-pelt
  573. |_ ms=ld-mset-pelt
  574. ::
  575. :: +add: add f to the multiset n times
  576. ::
  577. :: dat' = dat + n/(bet - f)
  578. ++ add
  579. ~/ %add
  580. |= [p=pelt n=@]
  581. ^- ld-mset-pelt
  582. :- bet.ms
  583. (padd dat.ms (pmul (pelt-lift n) (pinv (psub bet.ms p))))
  584. :: +add-all: add a list of [felt, multiplicity] pairs to the multiset
  585. ::
  586. :: adds them one at a time starting with ms and returns a list of
  587. :: each intermediate memset in order.
  588. ++ add-all
  589. ~/ %add-all
  590. |= l=(list [p=pelt n=@])
  591. ^- (list ld-mset-pelt)
  592. %+ spun l
  593. |= [[p=pelt n=@] acc=_ms]
  594. =/ ret (~(add ld-pelt acc) p n)
  595. [ret ret]
  596. ::
  597. :: +remove: remove a felt n times
  598. ++ remove
  599. |= [p=pelt n=@]
  600. ^- ld-mset-pelt
  601. :- bet.ms
  602. (psub dat.ms (pmul (pelt-lift n) (pinv (psub bet.ms p))))
  603. ::
  604. :: +union: union multiset ms with multiset ms1
  605. ++ union
  606. |= ms1=ld-mset-pelt
  607. ^- ld-mset-pelt
  608. :: randomness has to be the same to perform union
  609. ?> =(bet.ms bet.ms1)
  610. :- bet.ms
  611. (padd dat.ms dat.ms1)
  612. --
  613. ::
  614. :: stack in triplicate
  615. +$ tri-stack [a=belt-stack b=belt-stack c=belt-stack]
  616. :: mset in triplicate
  617. +$ tri-mset [a=ld-mset-bf b=ld-mset-bf c=ld-mset-bf]
  618. ++ print-tri-mset
  619. |= [m=tri-mset t=(list belt)]
  620. ^- (list belt)
  621. [dat.a.m dat.b.m dat.c.m t]
  622. ::
  623. :: door to manipulate tri-stack
  624. ++ tstack
  625. |_ s=tri-stack
  626. ++ push
  627. |= [a=belt b=belt c=belt]
  628. ^- tri-stack
  629. :+ (~(push bstack a.s) a)
  630. (~(push bstack b.s) b)
  631. (~(push bstack c.s) c)
  632. ::
  633. ++ pop
  634. |= [a=belt b=belt c=belt]
  635. ^- tri-stack
  636. :+ (~(pop bstack a.s) a)
  637. (~(pop bstack b.s) b)
  638. (~(pop bstack c.s) c)
  639. ::
  640. ++ push-all
  641. |= xs=(list [belt belt belt])
  642. ^- tri-stack
  643. %+ roll xs
  644. |= [x=[belt belt belt] acc=_s]
  645. (~(push tstack acc) x)
  646. ::
  647. ++ pop-all
  648. |= xs=(list [belt belt belt])
  649. ^- tri-stack
  650. %+ roll xs
  651. |= [x=[belt belt belt] acc=_s]
  652. (~(pop tstack acc) x)
  653. ++ is-empty
  654. ^- ?
  655. ~(is-empty bstack a.s)
  656. --
  657. ::
  658. :: door to manipulate mset-state
  659. ++ tmset
  660. |_ ms=tri-mset
  661. ++ add
  662. |= [b=triple-belt n=@]
  663. ^- tri-mset
  664. :+ (~(add ld-bf a.ms) a.b n)
  665. (~(add ld-bf b.ms) b.b n)
  666. (~(add ld-bf c.ms) c.b n)
  667. ::
  668. ++ add-all
  669. |= l=(list [b=triple-belt n=@])
  670. ^- (list tri-mset)
  671. %+ spun l
  672. |= [[b=triple-belt n=@] st=_ms]
  673. =/ ret=tri-mset (~(add tmset st) b n)
  674. [ret ret]
  675. ::
  676. ++ remove
  677. |= [b=belt n=@]
  678. ^- tri-mset
  679. :+ (~(remove ld-bf a.ms) b n)
  680. (~(remove ld-bf b.ms) b n)
  681. (~(remove ld-bf c.ms) b n)
  682. ::
  683. ++ union
  684. |= ms1=tri-mset
  685. :+ (~(union ld-bf a.ms) a.ms1)
  686. (~(union ld-bf b.ms) b.ms1)
  687. (~(union ld-bf c.ms) c.ms1)
  688. --
  689. ::
  690. ++ init-tri-stack
  691. |= [alf-a=belt alf-b=belt alf-c=belt]
  692. ^- tri-stack
  693. :+ (init-bstack alf-a)
  694. (init-bstack alf-b)
  695. (init-bstack alf-c)
  696. ::
  697. ++ init-tri-mset
  698. |= [bet-a=belt bet-b=belt bet-c=belt]
  699. ^- tri-mset
  700. :+ (init-ld-mset-bf bet-a)
  701. (init-ld-mset-bf bet-b)
  702. (init-ld-mset-bf bet-c)
  703. ::
  704. :: +poly-ld: door handling polynomial constraints for log derivative multiset
  705. ++ poly-ld
  706. ~/ %poly-ld
  707. |_ bet=mp-mega
  708. ::
  709. :: +add: add element v with n multiplicity to old muliset and store in new
  710. ::
  711. :: add element v with n multiplicity to the old multiset in mold and store in
  712. :: the new multiset mnew.
  713. ::
  714. :: ldc' = ldc + n / (bet - value)
  715. :: => (bet-value)*ldc' = (bet-value)*ldc + n
  716. :: => (bet-value)*ldc' - [(bet-value)*ldc) + n] = 0
  717. ++ add
  718. ~/ %add
  719. |= [mold=mp-mega mnew=mp-mega v=mp-mega n=mp-mega]
  720. ^- mp-mega
  721. %+ mpsub (mpmul (mpsub bet v) mnew)
  722. (mpadd n (mpmul (mpsub bet v) mold))
  723. ::
  724. ++ add-two
  725. |= [mold=mp-mega mnew=mp-mega p1=mp-mega p2=mp-mega]
  726. ^- mp-mega
  727. %+ mpsub
  728. :(mpmul mnew (mpsub bet p1) (mpsub bet p2))
  729. ;: mpadd
  730. (mpsub bet p1)
  731. (mpsub bet p2)
  732. :(mpmul mold (mpsub bet p1) (mpsub bet p2))
  733. ==
  734. ::
  735. ++ remove
  736. |= [mold=mp-mega mnew=mp-mega v=mp-mega n=mp-mega]
  737. ^- mp-mega
  738. %+ mpsub (mpmul (mpsub bet v) mnew)
  739. (mpsub (mpmul (mpsub bet v) mold) n)
  740. --
  741. ::
  742. :: +poly-ld: door handling polynomial constraints for log derivative multiset
  743. ++ poly-ld-pelt
  744. |_ bet=mp-pelt
  745. ::
  746. :: +add: add element v with n multiplicity to old muliset and store in new
  747. ::
  748. :: add element v with n multiplicity to the old multiset in mold and store in
  749. :: the new multiset mnew.
  750. ::
  751. :: ldc' = ldc + n / (bet - value)
  752. :: => (bet-value)*ldc' = (bet-value)*ldc + n
  753. :: => (bet-value)*ldc' - [(bet-value)*ldc) + n] = 0
  754. ++ add
  755. ~/ %add
  756. |= [mold=mp-pelt mnew=mp-pelt v=mp-pelt n=mp-mega]
  757. ^- mp-pelt
  758. %+ mpsub-pelt (mpmul-pelt (mpsub-pelt bet v) mnew)
  759. (mpadd-pelt (lift-to-mp-pelt n) (mpmul-pelt (mpsub-pelt bet v) mold))
  760. ::
  761. ++ add-two
  762. |= [mold=mp-pelt mnew=mp-pelt p1=mp-pelt p2=mp-pelt]
  763. ^- mp-pelt
  764. %+ mpsub-pelt
  765. :(mpmul-pelt mnew (mpsub-pelt bet p1) (mpsub-pelt bet p2))
  766. ;: mpadd-pelt
  767. (mpsub-pelt bet p1)
  768. (mpsub-pelt bet p2)
  769. :(mpmul-pelt mold (mpsub-pelt bet p1) (mpsub-pelt bet p2))
  770. ==
  771. ::
  772. ++ remove
  773. |= [mold=mp-pelt mnew=mp-pelt v=mp-pelt n=mp-mega]
  774. ^- mp-pelt
  775. %+ mpsub-pelt (mpmul-pelt (mpsub-pelt bet v) mnew)
  776. (mpsub-pelt (mpmul-pelt (mpsub-pelt bet v) mold) (lift-to-mp-pelt n))
  777. --
  778. :: +subtree-ld-utils: door for ld subtrees
  779. ::
  780. :: utilities for creating zeroes and tens for the log derivative memory multiset
  781. ++ subtree-ld-utils
  782. ~/ %subtree-ld-utils
  783. |_ cs=(list mp-mega)
  784. ::
  785. :: +make-zero: Create a compressed felt of a zero access which can be added to a multiset
  786. ++ make-zero
  787. ~/ %make-zero
  788. |= [noun=mp-mega axis=mp-mega child=mp-mega]
  789. ^- mp-mega
  790. (make-ten noun axis child noun child)
  791. ++ make-ten
  792. ~/ %make-ten
  793. |= $: noun=mp-mega
  794. axis=mp-mega
  795. child=mp-mega
  796. new-noun=mp-mega
  797. new-child=mp-mega
  798. ==
  799. ^- mp-mega
  800. (~(compress poly-tupler cs) ~[noun axis child new-noun new-child])
  801. --
  802. ::
  803. ::
  804. ++ tuple
  805. ~/ %tuple
  806. |_ cs=(list felt)
  807. ++ compress
  808. ~/ %compress
  809. |= fs=(list felt)
  810. ^- felt
  811. %^ zip-roll cs fs
  812. |= [[c=felt f=felt] acc=_(lift 0)]
  813. (fadd acc (fmul c f))
  814. --
  815. ::
  816. ++ tuple-bf
  817. ~/ %tuple-bf
  818. |_ cs=(list belt)
  819. ++ compress
  820. ~/ %compress
  821. |= bs=(list belt)
  822. ^- belt
  823. %^ zip-roll cs bs
  824. |= [[c=belt b=belt] acc=belt]
  825. (badd acc (bmul c b))
  826. --
  827. ::
  828. +$ triple-belt [a=belt b=belt c=belt]
  829. ::
  830. ++ tuple-trip
  831. ~/ %tuple-trip
  832. |_ cs=(list triple-belt)
  833. ++ compress
  834. ~/ %compress
  835. |= bs=(list triple-belt)
  836. ^- triple-belt
  837. %^ zip-roll cs bs
  838. |= [[c=triple-belt b=triple-belt] acc=triple-belt]
  839. :+ (badd a.acc (bmul a.c a.b))
  840. (badd b.acc (bmul b.c b.b))
  841. (badd c.acc (bmul c.c c.b))
  842. --
  843. ::
  844. :: utilities for working with polynomial stacks
  845. ::
  846. :: +poly-stack: door for working with polynomial stacks
  847. ++ poly-stack
  848. ~/ %poly-stack
  849. |_ [alf=mp-mega alf-inv=mp-mega vars=(map term mp-mega)]
  850. ++ v
  851. ~/ %v
  852. |= nam=term
  853. ^- mp-mega
  854. ~+
  855. ~| var-not-found+nam
  856. (~(got by vars) nam)
  857. ++ push
  858. ~/ %push
  859. |= [s=mp-mega nam=mp-mega]
  860. ^- mp-mega
  861. (mpadd (mpmul alf s) nam)
  862. ++ push-all
  863. ~/ %push-all
  864. |= [s=mp-mega nams=(list mp-mega)]
  865. ^- mp-mega
  866. %+ roll nams
  867. |: [nam=`mp-mega`(mp-c 0) mp=`mp-mega`s]
  868. (push mp nam)
  869. ++ pop
  870. ~/ %pop
  871. |= [s=mp-mega nam=mp-mega]
  872. ^- mp-mega
  873. (mpmul alf-inv (mpsub s nam))
  874. ++ pop-all
  875. ~/ %pop-all
  876. |= [s=mp-mega nams=(list mp-mega)]
  877. ^- mp-mega
  878. %+ roll nams
  879. |: [nam=`mp-mega`(mp-c 0) mp=`mp-mega`s]
  880. (pop mp nam)
  881. --
  882. ::
  883. ++ poly-tupler
  884. ~/ %poly-tupler
  885. |_ cs=(list mp-mega)
  886. ++ compress
  887. ~/ %compress
  888. |= nams=(list mp-mega)
  889. ^- mp-mega
  890. %^ zip-roll cs nams
  891. |= [[c=mp-mega n=mp-mega] acc=_(mp-c 0)]
  892. (mpadd acc (mpmul c n))
  893. --
  894. ::
  895. ++ poly-tupler-pelt
  896. ~/ %poly-tupler-pelt
  897. |_ cs=(list mp-pelt)
  898. ++ compress
  899. ~/ %compress
  900. |= nams=(list mp-pelt)
  901. ^- mp-pelt
  902. =/ acc=mp-pelt [(mp-c 0) (mp-c 0) (mp-c 0)]
  903. %^ zip-roll cs nams
  904. |= [[c=mp-pelt n=mp-pelt] acc=_acc]
  905. (mpadd-pelt acc (mpmul-pelt c n))
  906. --
  907. :: column name utilities
  908. ::
  909. :: +grab: alias for snagging from a row using the var name instead of col index
  910. ++ grab
  911. ~/ %grab
  912. |= [label=term row=fpoly var-indices=(map term @)]
  913. %- ~(snag fop row)
  914. (~(got by var-indices) label)
  915. ::
  916. ++ grab-bf
  917. ~/ %grab-bf
  918. |= [label=term row=bpoly var-indices=(map term @)]
  919. %- ~(snag bop row)
  920. (~(got by var-indices) label)
  921. ::
  922. :: noun utilities
  923. ::
  924. ++ noun-utils
  925. ~/ %noun-utils
  926. |_ $: noun-chals=[a=felt b=felt c=felt alf=felt]
  927. tuple-chals=[p=felt q=felt r=felt s=felt t=felt]
  928. ==
  929. ++ build-atom
  930. ~/ %build-atom
  931. |= atom=@
  932. ^- felt
  933. :: general format: (a * len) + (b * dyck) + (c * leaf)
  934. :: for atoms: (a * 1) + (b * 0) + (c * <atom>)
  935. (fadd a.noun-chals (fmul c.noun-chals (lift atom)))
  936. ::
  937. ++ make-zero-ld
  938. ~/ %make-zero-ld
  939. |= [memset=ld-mset noun=felt axis=@ child=felt num=@]
  940. ^- ld-mset
  941. %- ~(add ld memset)
  942. :_ num
  943. %- ~(compress tuple [p q r s t ~]:tuple-chals)
  944. ~[noun (build-atom axis) child noun child]
  945. ::
  946. ++ make-zeroes-ld
  947. ~/ %make-zeroed-ld
  948. |= [memset=ld-mset zs=(list [noun=felt axis=@ child=felt num=@])]
  949. ^- (list ld-mset)
  950. %- ~(add-all ld memset)
  951. %+ turn zs
  952. |= [noun=felt axis=@ child=felt num=@]
  953. :_ num
  954. %- ~(compress tuple [p q r s t ~]:tuple-chals)
  955. ~[noun (build-atom axis) child noun child]
  956. --
  957. ::
  958. ::
  959. ++ noun-poly-utils
  960. ~/ %noun-poly-utils
  961. |_ $: noun-chals=[a=mp-mega b=mp-mega c=mp-mega alf=mp-mega]
  962. vars=(map term mp-mega)
  963. ==
  964. ++ v
  965. ~/ %v
  966. |= nam=term
  967. ^- mp-mega
  968. ~+
  969. ~| var-not-found+nam
  970. (~(got by vars) nam)
  971. ++ build-atom-literal
  972. ~/ %build-atom-literal
  973. |= atom=@
  974. ^- mp-mega
  975. (mpadd a.noun-chals (mpscal atom c.noun-chals))
  976. ++ build-atom-reg
  977. ~/ %build-atom-reg
  978. |= atom=@tas
  979. ^- mp-mega
  980. (mpadd a.noun-chals (mpmul c.noun-chals (v atom)))
  981. ++ build-atom-poly
  982. ~/ %build-atom-poly
  983. |= atom=mp-mega
  984. ^- mp-mega
  985. (mpadd a.noun-chals (mpmul c.noun-chals atom))
  986. --
  987. ::
  988. ::
  989. ::TODO the %tree-utils chapter (and arms formerly in it, such as
  990. ::+get-subtree-multiset) consists of some of the worst code in
  991. ::the system. they're used for generating data for the tables, and there's a
  992. ::lot of duplicated code between the arms that just different enough to make
  993. ::it a pain to refactor. I suspect that some of this should actually be put
  994. ::into $fock-return to avoid recalculating the same things between tables (or
  995. ::even between +build and +extend for the same table). this deserves special
  996. ::attention and a partial solution shouldn't be thrown in as a "btw" for
  997. ::another PR. see also the work on $bushes in the PR section of the old zkvm
  998. ::repo.
  999. ::
  1000. :: TODO make these arms a door over the randomness
  1001. ::
  1002. :: a, b, c, and alf are random weights from the challenges
  1003. ::
  1004. ::
  1005. ++ build-tree-data
  1006. ~/ %build-tree-data
  1007. |= [t=* alf=pelt]
  1008. ^- tree-data
  1009. ~+
  1010. =/ leaf=(list pelt) (turn (leaf-sequence:shape t) pelt-lift)
  1011. =/ dyck=(list pelt) (turn (dyck:shape t) pelt-lift)
  1012. =/ leaf-pelt=pelt
  1013. dat:(~(push-all pstack (init-pstack alf)) leaf)
  1014. =/ dyck-pelt=pelt
  1015. dat:(~(push-all pstack (init-pstack alf)) dyck)
  1016. =/ len (lent leaf)
  1017. =/ size (ppow alf len)
  1018. :* size
  1019. dyck-pelt
  1020. leaf-pelt
  1021. t
  1022. ==
  1023. -- ::constraint
  1024. --