eight.hoon 27 KB

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