five.hoon 26 KB

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