eight.hoon 29 KB

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