eight.hoon 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  1. /= ztd-seven /common/ztd/seven
  2. => ztd-seven
  3. ~% %stark-core ..tlib ~
  4. :: stark-core
  5. |%
  6. +| %types
  7. :: $zerofier-cache: cache from table height -> zerofier
  8. +$ constraint-degrees [boundary=@ row=@ transition=@ terminal=@ extra=@]
  9. :: $table-to-constraint-degree: a map from table number to maximum constraint degree for that table
  10. +$ table-to-constraint-degree (map @ constraint-degrees)
  11. :: mp-ultra constraint along with corresponding degrees of the constraints inside
  12. +$ constraint-data [cs=mp-ultra degs=(list @)]
  13. :: all constraints for one table
  14. +$ constraints
  15. $: boundary=(list constraint-data)
  16. row=(list constraint-data)
  17. transition=(list constraint-data)
  18. terminal=(list constraint-data)
  19. extra=(list constraint-data)
  20. ==
  21. :: constraint types
  22. +$ constraint-type ?(%boundary %row %transition %terminal)
  23. ::
  24. +$ constraint-counts
  25. $: boundary=@
  26. row=@
  27. transition=@
  28. terminal=@
  29. extra=@
  30. ==
  31. ::
  32. :: $preprocess-0: preprocess with a version tag attached
  33. +$ preprocess-0
  34. $: %0
  35. cd=table-to-constraint-degree :: maximum degree of constraints for each table
  36. constraint-map=(map @ constraints) :: map from table number -> constraints
  37. count-map=(map @ constraint-counts) :: map from table number -> constraint-counts
  38. ==
  39. ::
  40. :: $stark-config: prover+verifier parameters unrelated to a particular computation
  41. +$ stark-config
  42. $: conf=[log-expand-factor=_6 security-level=_50]
  43. prep=preprocess-0
  44. ==
  45. ::TODO this type could potentially be improved
  46. +$ stark-input
  47. $: table-names=(list term)
  48. all-verifier-funcs=(list verifier-funcs) :: all verifier-funcs, whether theyre used or not
  49. table-base-widths=(list @)
  50. table-full-widths=(list @)
  51. =stark-config
  52. ==
  53. ::
  54. +| %cores
  55. ++ quot
  56. =/ util constraint-util
  57. =| tm=table-mary
  58. ~% %quot ..quot ~
  59. |%
  60. ++ height (height-mary:tlib p.tm)
  61. ++ omicron
  62. :: TODO: specialized for 2^64 - 2^32 + 1
  63. ~+((ordered-root height))
  64. ::
  65. ++ omicron-domain
  66. ^- (list felt)
  67. ~+
  68. %+ turn (range height)
  69. |= i=@
  70. (lift (bpow omicron i))
  71. -- ::quot
  72. ::
  73. +| %misc
  74. :: +t-order: order terms (table names) by <=, except %jute table is always last
  75. ++ t-order
  76. |= [a=term b=term]
  77. ~+ ^- ?
  78. ?: =(b %jute) %.y
  79. ?: =(a %jute) %.n
  80. (lth `@`a `@`b)
  81. ::
  82. :: +td-order: order table-dats using +t-order
  83. ++ td-order
  84. |= [a=table-dat b=table-dat]
  85. ~+ ^- ?
  86. (t-order name.p.a name.p.b)
  87. ::
  88. :: +tg-order: general ordering arm for lists with head given by table name
  89. ++ tg-order
  90. |= [a=[name=term *] b=[name=term *]]
  91. ~+ ^- ?
  92. (t-order name.a name.b)
  93. ::
  94. ::
  95. :: jetted functions used by the stark prover
  96. +| %jetted-funcs
  97. +$ codeword-commitments
  98. $: polys=(list mary)
  99. codewords=mary
  100. merk-heap=(pair @ merk-heap:merkle)
  101. ==
  102. ::
  103. ++ compute-codeword-commitments
  104. ~/ %compute-codeword-commitments
  105. |= $: table-marys=(list mary)
  106. fri-domain-len=@
  107. total-cols=@
  108. ==
  109. ^- codeword-commitments
  110. ::
  111. :: convert the ext columns to marys
  112. ::
  113. :: think of each mary as a list of the table's columns, interpolated to polynomials
  114. =/ table-polys=(list mary)
  115. (compute-table-polys table-marys)
  116. ::
  117. :: this mary is a list of all tables' columns, extended to codewords
  118. =/ codewords=mary
  119. (compute-lde table-polys fri-domain-len total-cols)
  120. ::
  121. :: this mary is a list of rows, each row the values of above codewords at a fixed domain elt
  122. =/ codeword-array=mary
  123. (transpose-bpolys codewords)
  124. =/ merk-heap=(pair @ merk-heap:merkle)
  125. (bp-build-merk-heap:merkle codeword-array)
  126. [table-polys codeword-array merk-heap]
  127. ::
  128. :: interpolate polynomials through table columns
  129. ++ compute-table-polys
  130. |= tables=(list mary)
  131. ^- (list mary)
  132. %+ turn tables
  133. |= p=mary
  134. =/ height (height-mary:tlib p)
  135. ?: =(height 0)
  136. ~|("compute-table-polys: height 0 table detected" !!)
  137. (interpolate-table p height)
  138. ::
  139. ++ compute-lde
  140. ~/ %compute-lde
  141. |= $: table-polys=(list mary)
  142. fri-domain-len=@
  143. num-cols=@
  144. ==
  145. ^- mary
  146. =/ fps=(list mary)
  147. %+ turn table-polys
  148. |= t=mary
  149. (turn-coseword t g fri-domain-len)
  150. =/ res=mary
  151. :+ step=fri-domain-len
  152. len=num-cols
  153. dat=(lsh [6 (mul fri-domain-len num-cols)] 1)
  154. =; [@ ret=mary]
  155. ret
  156. %+ roll
  157. fps
  158. |= [curr=mary [idx=@ res=_res]]
  159. ?> =(step.curr fri-domain-len)
  160. =/ chunk (mul step.curr len.array.curr)
  161. :- (add idx chunk)
  162. res(dat.array (sew 6 [idx chunk dat.array.curr] dat.array.res))
  163. ::
  164. ::
  165. :: the @ is a degree upper bound D_j of the associated composition
  166. :: codeword, and thereby dependent on trace length, i.e.
  167. :: deg(mp constraint)*(trace-len - 1) - deg(zerofier)
  168. +$ constraints-w-deg
  169. $: boundary=(list [(list @) mp-ultra])
  170. row=(list [(list @) mp-ultra])
  171. transition=(list [(list @) mp-ultra])
  172. terminal=(list [(list @) mp-ultra])
  173. extra=(list [(list @) mp-ultra])
  174. ==
  175. ::
  176. :: fri-deg-bound is D-1, where D is the next power of 2 greater than
  177. :: the degree bounds of all composition codewords
  178. ++ degree-processing
  179. |= [heights=(list @) constraint-map=(map @ constraints) is-extra=?]
  180. ^- [fri-deg-bound=@ constraint-w-deg-map=(map @ constraints-w-deg)]
  181. =- [(dec (bex (xeb (dec d)))) m]
  182. %+ roll (range (lent heights))
  183. |= [i=@ d=@ m=(map @ constraints-w-deg)]
  184. =/ height=@ (snag i heights)
  185. =/ constraints (~(got by constraint-map) i)
  186. =- :- :(max d d.bnd d.row d.trn d.trm d.xta)
  187. (~(put by m) i [c.bnd c.row c.trn c.trm c.xta])
  188. :: attach composition degree to each mp & keep a running max of degrees
  189. :: divided by boundary, row, transition, terminal
  190. :*
  191. ^= bnd=[c d]
  192. %^ spin boundary.constraints 0
  193. |= [cd=constraint-data d=@]
  194. =; degrees=(list @)
  195. :- [degrees cs.cd]
  196. (roll `(list @)`[d degrees] max)
  197. %+ turn degs.cd
  198. |= deg=@
  199. ?: =(height 1) 0
  200. (dec (mul deg (dec height)))
  201. ::
  202. ^= row=[c d]
  203. %^ spin row.constraints 0
  204. |= [cd=constraint-data d=@]
  205. =; degrees=(list @)
  206. :- [degrees cs.cd]
  207. (roll `(list @)`[d degrees] max)
  208. %+ turn degs.cd
  209. |= deg=@
  210. ?: ?|(=(height 1) =(deg 1)) 0
  211. (sub (mul deg (dec height)) height)
  212. ::
  213. ^= trn=[c d]
  214. %^ spin transition.constraints 0
  215. |= [cd=constraint-data d=@]
  216. =; degrees=(list @)
  217. :- [degrees cs.cd]
  218. (roll `(list @)`[d degrees] max)
  219. %+ turn degs.cd
  220. |=(@ (mul (dec +<) (dec height)))
  221. ::
  222. ^= trm=[c d]
  223. %^ spin terminal.constraints 0
  224. |= [cd=constraint-data d=@]
  225. =; degrees=(list @)
  226. :- [degrees cs.cd]
  227. (roll `(list @)`[d degrees] max)
  228. %+ turn degs.cd
  229. |= deg=@
  230. ?: =(height 1) 0
  231. (dec (mul deg (dec height)))
  232. ::
  233. ^= xta=[c d]
  234. ?. is-extra [~ 0]
  235. %^ spin extra.constraints 0
  236. |= [cd=constraint-data d=@]
  237. =; degrees=(list @)
  238. :- [degrees cs.cd]
  239. (roll `(list @)`[d degrees] max)
  240. %+ turn degs.cd
  241. |= deg=@
  242. ?: ?|(=(height 1) =(deg 1)) 0
  243. (sub (mul deg (dec height)) height)
  244. ==
  245. ::
  246. ++ compute-composition-poly
  247. ~/ %compute-composition-poly-hoon
  248. |= $: omicrons=bpoly
  249. heights=(list @)
  250. tworow-trace-polys=(list bpoly)
  251. constraint-map=(map @ constraints)
  252. constraint-counts=(map @ constraint-counts)
  253. composition-chals=(map @ bpoly)
  254. chal-map=(map @ belt)
  255. dyn-map=(map @ bpoly)
  256. is-extra=?
  257. ==
  258. ^- bpoly
  259. (do-compute-composition-poly +<)
  260. ::
  261. ++ do-compute-composition-poly
  262. ~/ %compute-composition-poly
  263. |= $: omicrons=bpoly
  264. heights=(list @)
  265. tworow-trace-polys=(list bpoly)
  266. constraint-map=(map @ constraints)
  267. constraint-counts=(map @ constraint-counts)
  268. composition-chals=(map @ bpoly)
  269. chal-map=(map @ belt)
  270. dyn-map=(map @ bpoly)
  271. is-extra=?
  272. ==
  273. ^- bpoly
  274. =/ max-height=@
  275. %- bex %- xeb %- dec
  276. (roll heights max)
  277. =/ dp (degree-processing heights constraint-map is-extra)
  278. |^
  279. =/ boundary-zerofier (init-bpoly ~[(bneg 1) 1]) :: f(X)=X-1
  280. ::
  281. %+ roll (range len.omicrons)
  282. |= [i=@ acc=_zero-bpoly]
  283. =/ height=@ (snag i heights)
  284. =/ omicron (~(snag bop omicrons) i)
  285. =/ last-row (init-bpoly ~[(bneg (binv omicron)) 1]) :: f(X)=X-g^{-1}
  286. =/ chals (~(got by composition-chals) i)
  287. =/ trace (snag i tworow-trace-polys)
  288. =/ constraints (~(got by constraint-w-deg-map.dp) i)
  289. =/ counts (~(got by constraint-counts) i)
  290. =/ dyns (~(got by dyn-map) i)
  291. ::
  292. =/ row-zerofier :: f(X) = (X^N-1)
  293. (bpsub (bppow id-bpoly height) one-bpoly)
  294. ::
  295. ;: bpadd
  296. acc
  297. ::
  298. %- bpdiv
  299. :_ boundary-zerofier
  300. %- process-composition-constraints
  301. :* boundary.constraints
  302. trace
  303. (~(scag bop chals) (mul 2 boundary.counts))
  304. dyns
  305. ==
  306. ::
  307. %- bpdiv
  308. :_ row-zerofier
  309. %- process-composition-constraints
  310. :* row.constraints
  311. trace
  312. ::
  313. %+ ~(swag bop chals)
  314. (mul 2 boundary.counts)
  315. (mul 2 row.counts)
  316. ::
  317. dyns
  318. ==
  319. ::
  320. %- bpdiv
  321. :: note: the transition zerofier = row-zerofier/last-row
  322. :: here, we are computing composition-constraints/transition-zerofier
  323. :_ row-zerofier
  324. %+ bpmul last-row
  325. %- process-composition-constraints
  326. :* transition.constraints
  327. trace
  328. ::
  329. %+ ~(swag bop chals)
  330. (mul 2 (add boundary.counts row.counts))
  331. (mul 2 transition.counts)
  332. ::
  333. dyns
  334. ==
  335. ::
  336. %- bpdiv
  337. :_ last-row
  338. %- process-composition-constraints
  339. :* terminal.constraints
  340. trace
  341. ::
  342. %+ ~(swag bop chals)
  343. (mul 2 :(add boundary.counts row.counts transition.counts))
  344. (mul 2 terminal.counts)
  345. ::
  346. dyns
  347. ==
  348. ::
  349. ?. is-extra zero-bpoly
  350. %- bpdiv
  351. :_ row-zerofier
  352. %- process-composition-constraints
  353. :* extra.constraints
  354. trace
  355. ::
  356. %- ~(slag bop chals)
  357. %+ mul 2
  358. ;: add
  359. boundary.counts
  360. row.counts
  361. transition.counts
  362. terminal.counts
  363. ==
  364. ::
  365. dyns
  366. ==
  367. ::
  368. ==
  369. ::
  370. ++ process-composition-constraints
  371. |= $: constraints=(list [(list @) mp-ultra])
  372. trace=bpoly
  373. weights=bpoly
  374. dyns=bpoly
  375. ==
  376. =- (bpcan acc)
  377. %+ roll constraints
  378. |= [[degs=(list @) mp=mp-ultra] [idx=@ acc=_zero-bpoly]]
  379. ::
  380. :: mp-substitute-ultra returns a list because the %comp
  381. :: constraint type can contain multiple mp-mega constraints.
  382. ::
  383. =/ comps=(list bpoly)
  384. (mp-substitute-ultra mp trace max-height chal-map dyns)
  385. %+ roll
  386. (zip-up degs comps)
  387. |= [[deg=@ comp=bpoly] [idx=_idx acc=_acc]]
  388. :- +(idx)
  389. ::
  390. :: Each constraint corresponds to two weights: alpha and beta. The verifier
  391. :: samples 2*num_constraints random values and we assume that the alpha
  392. :: and beta weights for a given constraint are situated next to each other
  393. :: in the array.
  394. ::
  395. =/ alpha (~(snag bop weights) (mul 2 idx))
  396. =/ beta (~(snag bop weights) (add 1 (mul 2 idx)))
  397. ::
  398. :: adjust degree up to fri-deg-bound.
  399. :: if fri-deg-bound is D-1 then we construct:
  400. :: p(x)*(α*X^{D-1-D_j} + β)
  401. :: which will make the polynomial exactly degree D-1 which is what we want.
  402. =/ comp-coeff (bp-ifft comp)
  403. %+ bpadd acc
  404. %+ bpadd
  405. (bpscal beta comp-coeff)
  406. %- %~ weld bop
  407. (init-bpoly (reap (sub fri-deg-bound.dp deg) 0))
  408. (bpscal alpha comp-coeff)
  409. --
  410. ::
  411. :: compute the DEEP Composition Polynomial
  412. ++ compute-deep
  413. ~/ %compute-deep
  414. |= $: trace-polys=(list mary)
  415. trace-openings=fpoly
  416. composition-pieces=(list fpoly)
  417. composition-piece-openings=fpoly
  418. weights=fpoly
  419. omicrons=fpoly
  420. deep-challenge=felt
  421. comp-eval-point=felt
  422. ==
  423. |^ ^- fpoly
  424. =/ [acc=fpoly num=@]
  425. %^ zip-roll (range (lent trace-polys)) trace-polys
  426. |= [[i=@ p=mary] acc=_zero-fpoly num=@]
  427. =/ lis=(list fpoly)
  428. %+ turn (range len.array.p)
  429. |= i=@
  430. (bpoly-to-fpoly (~(snag-as-bpoly ave p) i))
  431. =/ omicron (~(snag fop omicrons) i)
  432. =/ [first-row=fpoly num=@] :: first row: f(x)-f(Z)/x-Z
  433. %- weighted-linear-combo
  434. :* lis
  435. trace-openings
  436. num
  437. (fp-c deep-challenge)
  438. weights
  439. ==
  440. =/ [second-row=fpoly num=@] :: second row: f(x)-f(gZ)/x-gZ
  441. %- weighted-linear-combo
  442. :* lis
  443. trace-openings
  444. num
  445. (fp-c (fmul omicron deep-challenge))
  446. weights
  447. ==
  448. :_ num
  449. :(fpadd acc first-row second-row)
  450. ::
  451. :: do the same thing for the second composition poly evals
  452. =/ [acc=fpoly num=@]
  453. %^ zip-roll (range (lent trace-polys)) trace-polys
  454. |= [[i=@ p=mary] acc=_acc num=_num]
  455. =/ lis=(list fpoly)
  456. %+ turn (range len.array.p)
  457. |= i=@
  458. (bpoly-to-fpoly (~(snag-as-bpoly ave p) i))
  459. =/ omicron (~(snag fop omicrons) i)
  460. :: add new composition poly
  461. =/ [new-first-row=fpoly num=@] :: first row: f(x)-f(Z)/x-Z
  462. %- weighted-linear-combo
  463. :* lis
  464. trace-openings
  465. num
  466. (fp-c comp-eval-point)
  467. weights
  468. ==
  469. :: second row
  470. =/ [new-second-row=fpoly num=@] :: second row: f(x)-f(gZ)/x-gZ
  471. %- weighted-linear-combo
  472. :* lis
  473. trace-openings
  474. num
  475. (fp-c (fmul omicron comp-eval-point))
  476. weights
  477. ==
  478. :_ num
  479. :(fpadd acc new-first-row new-second-row)
  480. ::
  481. =/ [pieces=fpoly @]
  482. %- weighted-linear-combo
  483. :* composition-pieces
  484. composition-piece-openings
  485. 0
  486. (fp-c (fpow deep-challenge (lent composition-pieces))) :: f(X)=X^D
  487. (~(slag fop weights) num)
  488. ==
  489. (fpadd acc pieces)
  490. ::
  491. ++ weighted-linear-combo
  492. |= [polys=(list fpoly) openings=fpoly idx=@ x-poly=fpoly weights=fpoly]
  493. ^- [fpoly @]
  494. =- [acc num]
  495. %+ roll polys
  496. |= [poly=fpoly acc=_zero-fpoly num=_idx]
  497. :_ +(num)
  498. %+ fpadd acc
  499. %+ fpscal (~(snag fop weights) num)
  500. %+ fpdiv
  501. (fpsub poly (fp-c (~(snag fop openings) num)))
  502. (fpsub id-fpoly x-poly)
  503. --
  504. ::
  505. :: +precompute-ntts
  506. ::
  507. ::
  508. ++ precompute-ntts
  509. ~/ %precompute-ntts
  510. |= [polys=mary height=@ ntt-len=@]
  511. ^- bpoly
  512. %- need
  513. =/ new-len (mul height ntt-len)
  514. %+ roll (range len.array.polys)
  515. |= [i=@ acc=(unit bpoly)]
  516. =/ p=bpoly (~(snag-as-bpoly ave polys) i)
  517. =/ fft=bpoly
  518. (bp-fft (~(zero-extend bop p) (sub new-len len.p)))
  519. ?~ acc (some fft)
  520. (some (~(weld bop u.acc) fft))
  521. ::
  522. -- ::stark-core
  523. ::
  524. =>
  525. :: fock-core
  526. ~% %fock-core ..constraint-degrees ~
  527. |%
  528. +| %cores
  529. ++ fock :: /lib/fock
  530. =/ util constraint-util
  531. =, chal
  532. =>
  533. ~% %fock ..fock ~
  534. |%
  535. ++ indirect-to-bits
  536. ~/ %indirect-to-bits
  537. |= axi=@
  538. ^- (list ?)
  539. =| lis=(list ?)
  540. |-
  541. ?: =(1 axi) lis
  542. =/ hav (dvr axi 2)
  543. $(axi p.hav, lis [=(q.hav 0) lis])
  544. ::
  545. ++ build-compute-queue
  546. ~/ %build-compute-queue
  547. |= [lis=(list *) alf=pelt]
  548. ^- compute-queue
  549. %+ turn lis
  550. |= t=*
  551. (build-tree-data:constraint-util t alf)
  552. ::
  553. ++ build-jute-list
  554. ~/ %build-jute-list
  555. |= [lis=(list [@tas * *]) a=pelt b=pelt c=pelt alf=pelt]
  556. ^- (list jute-data)
  557. %+ turn lis
  558. |= [name=@tas sam=* prod=*]
  559. :+ name
  560. (build-tree-data:constraint-util sam alf)
  561. (build-tree-data:constraint-util prod alf)
  562. ::
  563. :: +noun-get-zero-mults: computes how many times a noun shows up in the zero table
  564. ::
  565. :: utilized by the noun table to get %ext-mul and the exponent table to get the
  566. :: correct exponent multiset multiplicities. See the leading comment in
  567. :: table/noun.hoon for more information about what this is computing.
  568. ++ noun-get-zero-mults
  569. ~/ %noun-get-zero-mults
  570. |= ret=fock-return
  571. ^- (map tree=* m=@)
  572. =| mult=(map tree=* m=@)
  573. %+ roll ~(tap by zeroes.ret)
  574. |= [[subject=* data=(map [axis=* new-subj=*] count=@)] mult=(map tree=* m=@)]
  575. %+ roll ~(tap by data)
  576. |= [[[axis=* new-subj=*] count=@] mult=_mult]
  577. ?> ?=(belt axis)
  578. =- mult
  579. ::
  580. %+ roll (path-to-axis:shape axis)
  581. |= [dir=belt tree=_subject new-tree=_new-subj mult=_mult]
  582. |^
  583. =/ child-tree ?:(=(dir 0) -:tree +:tree)
  584. =/ new-child-tree ?:(=(dir 0) -:new-tree +:new-tree)
  585. ::
  586. =. mult (put-tree mult -:tree)
  587. =. mult (put-tree mult +:tree)
  588. =. mult (put-tree mult -:new-tree)
  589. =. mult (put-tree mult +:new-tree)
  590. ::
  591. [child-tree new-child-tree mult]
  592. ::
  593. ++ put-tree
  594. |= [mult=(map tree=* m=@) tree=*]
  595. (~(put by mult) tree +((~(gut by mult) tree 0)))
  596. -- ::+noun-get-zero-mults
  597. --
  598. ::
  599. =, util
  600. ~% %inner-fock ..indirect-to-bits ~
  601. |%
  602. ::
  603. :: The natural way to interpret nock is in depth-first order, but the compute table needs
  604. :: the data in breadth-first order. The solution is that axes are a breadth-first ordering
  605. :: of a tree. So we do a normal DFS interpreter but tag each node in the "tree of formulae"
  606. :: with its axis. Then sort by the axes to get them in BFS and we're done.
  607. ::
  608. :: One detail is that nock 2 has 3 subformulaes (you have to count the outer eval itself)
  609. :: and so the tree must be ternary instead of binary. So the axes are 3n, 3n+1, and 3n+2
  610. :: for the children.
  611. ::
  612. ++ fink
  613. |= sam=^
  614. ^- (pair * fock-return)
  615. ::
  616. |^ ^- (pair * fock-return)
  617. =/ data (interpret 1 -.sam +.sam *interpret-data)
  618. =/ sorted-data=(list formula-data)
  619. %+ sort formulae.q.data
  620. |= [a=formula-data b=formula-data]
  621. (lth axis.a axis.b)
  622. =/ queue=(list *)
  623. %- zing
  624. %- flop
  625. %+ roll sorted-data
  626. |= [item=formula-data acc=(list (list *))]
  627. [extra-data.item ~[s.item f.item p.item] acc]
  628. =| ret=fock-return
  629. :- p.data
  630. %_ ret
  631. s -.sam
  632. f +.sam
  633. queue queue
  634. zeroes zeroes.q.data
  635. decodes decodes.q.data
  636. ==
  637. ::
  638. :: extra-data is extra nouns that the compute table needs for a
  639. :: particular nock opcode. Usually the products of subformulae,
  640. :: but it varies by opcode.
  641. +$ formula-data [axis=@ s=* f=* p=* extra-data=(list *)]
  642. +$ interpret-data
  643. $: zeroes=zero-map
  644. decodes=decode-map
  645. formulae=(list formula-data)
  646. ==
  647. ::
  648. ++ interpret
  649. |= [axis=@ s=* f=* acc=interpret-data]
  650. ^- (pair * interpret-data)
  651. ?@ f !!
  652. ?+ f ~|(bad-formula+f !!)
  653. [^ *]
  654. =/ left (interpret (mul 3 axis) s -.f acc)
  655. =/ right (interpret +((mul 3 axis)) s +.f q.left)
  656. =/ prod [p.left p.right]
  657. =/ acc q.right
  658. =. decodes.acc
  659. %+ record-cons decodes.acc
  660. [f -.f +.f]
  661. =. formulae.acc
  662. [[axis s f prod ~[p.left p.right]] formulae.acc]
  663. :- prod
  664. acc
  665. ::
  666. [%0 axis=*]
  667. =/ prod (need (frag axis.f s))
  668. =. decodes.acc
  669. %+ record-cons decodes.acc
  670. [f %0 axis.f]
  671. =. zeroes.acc
  672. %+ record zeroes.acc
  673. [s axis.f s]
  674. =. formulae.acc
  675. [[axis s f prod ~] formulae.acc]
  676. :- prod
  677. acc
  678. ::
  679. [%1 constant=*]
  680. =. decodes.acc
  681. %+ record-cons decodes.acc
  682. [f %1 constant.f]
  683. =. formulae.acc
  684. [[axis s f constant.f ~] formulae.acc]
  685. :- constant.f
  686. acc
  687. ::
  688. [%2 subject=* formula=*]
  689. =/ sf1 (interpret (mul 3 axis) s subject.f acc)
  690. =/ sf2 (interpret +((mul 3 axis)) s formula.f q.sf1)
  691. =/ sf3 (interpret (add 2 (mul 3 axis)) p.sf1 p.sf2 q.sf2)
  692. =/ acc q.sf3
  693. =. decodes.acc
  694. %+ record-cons decodes.acc
  695. [f %2 [subject.f formula.f]]
  696. =. decodes.acc
  697. %+ record-cons decodes.acc
  698. [[subject.f formula.f] subject.f formula.f]
  699. =. formulae.acc
  700. [[axis s f p.sf3 ~[p.sf1 p.sf2]] formulae.acc]
  701. :- p.sf3
  702. acc
  703. ::
  704. [%3 argument=*]
  705. =/ arg (interpret (mul 3 axis) s argument.f acc)
  706. =/ prod .?(p.arg)
  707. =/ acc q.arg
  708. =. decodes.acc
  709. %+ record-cons decodes.acc
  710. [f %3 argument.f]
  711. =. formulae.acc
  712. [[axis s f prod ~[p.arg]] formulae.acc]
  713. :- prod
  714. acc
  715. ::
  716. [%4 argument=*]
  717. =/ arg (interpret (mul 3 axis) s argument.f acc)
  718. ?^ p.arg !!
  719. =/ prod .+(p.arg)
  720. =/ acc q.arg
  721. =. decodes.acc
  722. %+ record-cons decodes.acc
  723. [f %4 argument.f]
  724. =. formulae.acc
  725. [[axis s f prod ~[p.arg]] formulae.acc]
  726. :- prod
  727. acc
  728. ::
  729. [%5 a=* b=*]
  730. =/ a (interpret (mul 3 axis) s a.f acc)
  731. =/ b (interpret +((mul 3 axis)) s b.f q.a)
  732. =/ prod .=(p.a p.b)
  733. =/ acc q.b
  734. =. decodes.acc
  735. %+ record-cons decodes.acc
  736. [f %5 [a.f b.f]]
  737. =. decodes.acc
  738. %+ record-cons decodes.acc
  739. [[a.f b.f] a.f b.f]
  740. =. formulae.acc
  741. [[axis s f prod ~[p.a p.b]] formulae.acc]
  742. :- prod
  743. acc
  744. ::
  745. [%6 test=* yes=* no=*]
  746. =/ test (interpret +((mul 3 axis)) s test.f acc)
  747. ?> ?=(? p.test)
  748. =/ sf
  749. ?- p.test
  750. %.y yes.f
  751. %.n no.f
  752. ==
  753. =/ prod (interpret (mul 3 axis) s sf q.test)
  754. =/ acc q.prod
  755. =. decodes.acc
  756. %+ record-cons decodes.acc
  757. [f %6 [test.f yes.f no.f]]
  758. =. decodes.acc
  759. %+ record-cons decodes.acc
  760. :+ [test.f yes.f no.f]
  761. test.f
  762. [yes.f no.f]
  763. =. decodes.acc
  764. %+ record-cons decodes.acc
  765. :+ [yes.f no.f]
  766. yes.f
  767. no.f
  768. =. formulae.acc
  769. [[axis s f p.prod ~[sf p.prod p.test]] formulae.acc]
  770. :- p.prod
  771. acc
  772. ::
  773. [%7 subject=* next=*]
  774. =/ sub (interpret +((mul 3 axis)) s subject.f acc)
  775. =/ prod (interpret (mul 3 axis) p.sub next.f q.sub)
  776. =/ acc q.prod
  777. =. decodes.acc
  778. %+ record-cons decodes.acc
  779. [f %7 [subject.f next.f]]
  780. =. decodes.acc
  781. %+ record-cons decodes.acc
  782. [[subject.f next.f] subject.f next.f]
  783. =. formulae.acc
  784. [[axis s f p.prod ~[p.sub]] formulae.acc]
  785. :- p.prod
  786. acc
  787. ::
  788. [%8 head=* next=*]
  789. =/ head (interpret +((mul 3 axis)) s head.f acc)
  790. =/ prod (interpret (mul 3 axis) [p.head s] next.f q.head)
  791. =/ acc q.prod
  792. =. decodes.acc
  793. %+ record-cons decodes.acc
  794. [f %8 [head.f next.f]]
  795. =. decodes.acc
  796. %+ record-cons decodes.acc
  797. [[head.f next.f] head.f next.f]
  798. =. formulae.acc
  799. [[axis s f p.prod ~[[p.head s] p.head]] formulae.acc]
  800. :- p.prod
  801. acc
  802. ::
  803. [%9 axis=* core=*]
  804. !!
  805. ::
  806. [%10 [axis=@ value=*] target=*]
  807. !!
  808. ::
  809. [%11 tag=@ next=*]
  810. !!
  811. ::
  812. [%11 [tag=@ clue=*] next=*]
  813. !!
  814. ==
  815. ++ edit
  816. |= [axis=@ target=* value=*]
  817. ^- (unit)
  818. ?: =(1 axis) `value
  819. ?@ target ~
  820. =/ pick (cap axis)
  821. =/ mutant
  822. %= $
  823. axis (mas axis)
  824. target ?-(pick %2 -.target, %3 +.target)
  825. ==
  826. ?~ mutant ~
  827. ?- pick
  828. %2 `[u.mutant +.target]
  829. %3 `[-.target u.mutant]
  830. ==
  831. ::
  832. ++ record-all
  833. |= [zeroes=zero-map zs=(list [subject=* axis=* new-subject=*])]
  834. ^- zero-map
  835. %+ roll zs
  836. |= [z=[subject=* axis=* new-subject=*] new-zeroes=_zeroes]
  837. (record new-zeroes z)
  838. ::
  839. ++ record
  840. |= [zeroes=zero-map subject=* axis=* new-subject=*]
  841. ^- zero-map
  842. ?~ rec=(~(get by zeroes) subject)
  843. %+ ~(put by zeroes)
  844. subject
  845. (~(put by *(map [* *] @)) [axis new-subject] 1)
  846. ?~ mem=(~(get by u.rec) [axis new-subject])
  847. %+ ~(put by zeroes)
  848. subject
  849. (~(put by u.rec) [axis new-subject] 1)
  850. %+ ~(put by zeroes)
  851. subject
  852. (~(put by u.rec) [axis new-subject] +(u.mem))
  853. ::
  854. ++ record-cons
  855. |= [decodes=decode-map formula=* head=* tail=*]
  856. ^- decode-map
  857. %- ~(put by decodes)
  858. :- [formula head tail]
  859. .+ (~(gut by decodes) [formula head tail] 0)
  860. ::
  861. ++ frag
  862. |= [axis=* noun=*]
  863. ^- (unit)
  864. |^
  865. ?@ axis (frag-atom axis noun)
  866. ~|(%axis-is-too-big !!)
  867. :: TODO actually support the cell case
  868. ::?> ?=(@ -.axis)
  869. ::$(axis +.axis, noun (need (frag-atom -.axis noun)))
  870. ::
  871. ++ frag-atom
  872. |= [axis=@ noun=*]
  873. ^- (unit)
  874. ?: =(0 axis) ~
  875. |- ^- (unit)
  876. ?: =(1 axis) `noun
  877. ?@ noun ~
  878. =/ pick (cap axis)
  879. %= $
  880. axis (mas axis)
  881. noun ?-(pick %2 -.noun, %3 +.noun)
  882. ==
  883. --
  884. --
  885. -- ::fock
  886. --
  887. ~% %pow ..fock ~
  888. |%
  889. ++ pow-len `@`64
  890. ::
  891. :: +puzzle-nock: powork puzzle
  892. ::
  893. ++ puzzle-nock
  894. ~/ %puzzle-nock
  895. |= [block-commitment=noun-digest:tip5 nonce=noun-digest:tip5 length=@]
  896. ^- [* *]
  897. =+ [a b c d e]=block-commitment
  898. =+ [f g h i j]=nonce
  899. =/ sponge (new:sponge:tip5)
  900. =. sponge (absorb:sponge `(list belt)`[a b c d e f g h i j ~])
  901. =/ rng
  902. (new:tog:tip5 sponge:sponge)
  903. =^ belts-list rng (belts:rng length)
  904. =/ subj (gen-tree belts-list)
  905. =/ form (powork length)
  906. [subj form]
  907. ::
  908. ++ powork
  909. ~/ %powork
  910. |= n=@
  911. ^- nock
  912. =/ start n
  913. =/ form=nock [%1 0]
  914. %+ roll (gulf 0 (dec n))
  915. |= [i=@ nok=_form]
  916. =/ hed (add start i)
  917. :- [%6 [%3 %0 hed] [%0 0] [%0 hed]]
  918. nok
  919. ::
  920. ++ gen-tree
  921. ~/ %gen-tree
  922. |= leaves=(list @)
  923. ^- *
  924. ?: ?=([@ ~] leaves)
  925. i.leaves
  926. =/ split-leaves (split:shape (div (lent leaves) 2) leaves)
  927. :- $(leaves -:split-leaves)
  928. $(leaves +:split-leaves)
  929. ::
  930. -- :: %pow
  931. ::
  932. ~% %stark-engine ..puzzle-nock ~
  933. :: stark-engine
  934. |%
  935. ::
  936. :: This is a dummy arm which is only here so lib/stark/prover.hoon can use it as its parent core.
  937. :: Without it, jets won't work in that file.
  938. ++ stark-engine-jet-hook
  939. ~/ %stark-engine-jet-hook
  940. |= n=@
  941. !!
  942. ::
  943. ++ stark-engine
  944. =/ util constraint-util
  945. =, mp-to-graph
  946. ~% %stark-door ..stark-engine ~
  947. |_ stark-input
  948. ++ num-challenges num-chals:chal
  949. :: inverse of the rate of the RS code
  950. ++ expand-factor (bex log-expand-factor.conf.stark-config)
  951. :: Number of spot checks the verifier performs per round to check the folding
  952. ++ num-spot-checks (div security-level.conf.stark-config log-expand-factor.conf.stark-config)
  953. :: offset for the coset used for FRI
  954. ++ generator 7
  955. :: size of each FRI fold step. codeword C_i is 1/fri-folding-deg the size of C_{i-i}
  956. :: must be a power of 2
  957. ++ fri-folding-deg 8
  958. ::
  959. ++ calc
  960. ~/ %calc
  961. |_ [heights=(list @) cd=table-to-constraint-degree]
  962. ::
  963. ++ omega ^~((ordered-root fri-domain-len))
  964. ::
  965. :: fri domain length should be the fri expand factor times the max table size
  966. :: (rounded to a power of 2)
  967. ++ fri-domain-len
  968. ~+
  969. =/ max-padded-height
  970. %- bex %- xeb %- dec
  971. (roll heights max)
  972. (mul max-padded-height expand-factor)
  973. ::
  974. ++ fri
  975. ~+
  976. ~(. fri-door generator omega fri-domain-len expand-factor num-spot-checks fri-folding-deg)
  977. ::
  978. ++ max-constraint-degree
  979. ~/ %max-constraint-degree
  980. |= cd=constraint-degrees
  981. ^- @
  982. (max (max (max [boundary transition]:cd) row.cd) terminal.cd)
  983. ::
  984. ++ max-degree $:do-max-degree
  985. ::
  986. ++ do-max-degree
  987. ~% %max-degree + ~
  988. |.
  989. ~+
  990. %- dec %- bex %- xeb %- dec
  991. %^ zip-roll (range (lent heights)) heights
  992. |= [[i=@ h=@] d=_5]
  993. =/ deg (max-constraint-degree (~(got by cd) i))
  994. %+ max d
  995. =/ trace-degree h
  996. (sub (mul deg trace-degree) (dec h))
  997. --
  998. ::
  999. ++ compute-constraint-degree
  1000. ~/ %compute-constraint-degree
  1001. |= [funcs=verifier-funcs maybe-jutes=(unit jute-map)]
  1002. ^- constraint-degrees
  1003. =/ jutes=jute-map ?^(maybe-jutes (need maybe-jutes) *jute-map)
  1004. =- [(snag 0 -) (snag 1 -) (snag 2 -) (snag 3 -) (snag 4 -)]
  1005. %+ turn
  1006. :~ (unlabel-constraints:util boundary-constraints:funcs)
  1007. (unlabel-constraints:util row-constraints:funcs)
  1008. (unlabel-constraints:util transition-constraints:funcs)
  1009. (unlabel-constraints:util terminal-constraints:funcs)
  1010. (unlabel-constraints:util extra-constraints:funcs)
  1011. ==
  1012. |= l=(list mp-ultra)
  1013. %+ roll
  1014. l
  1015. |= [constraint=mp-ultra d=@]
  1016. %+ roll
  1017. (mp-degree-ultra constraint)
  1018. |= [a=@ d=_d]
  1019. (max d a)
  1020. ::
  1021. ++ get-max-constraint-degree
  1022. ~/ %get-max-constraint-degree
  1023. |= tab=table-to-constraint-degree
  1024. ~+
  1025. ^- @
  1026. %+ roll ~(val by tab)
  1027. |= [cd=constraint-degrees acc=@]
  1028. (max acc (max boundary.cd (max row.cd (max [transition terminal]:cd))))
  1029. -- ::stark-engine
  1030. --