verifier.hoon 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. /= nock-common /common/nock-common
  2. /= * /common/zeke
  3. ::
  4. => :* stark-engine
  5. nock-common=nock-common
  6. ==
  7. ~% %stark-verifier ..stark-engine-jet-hook ~
  8. |%
  9. :: copied from sur/verifier.hoon because of => stark-engine
  10. +$ verify-result [commitment=noun-digest:tip5 nonce=noun-digest:tip5]
  11. +$ elem-list (list [idx=@ trace-elems=(list belt) comp-elems=(list felt) deep-elem=felt])
  12. ::
  13. ++ verify
  14. =| test-mode=_|
  15. |= [=proof override=(unit (list term)) verifier-eny=@]
  16. ^- ?
  17. =/ args [proof override verifier-eny test-mode]
  18. -:(mule |.((verify-inner args)))
  19. ::
  20. ++ verify-inner
  21. ~/ %verify-inner
  22. |= [=proof override=(unit (list term)) verifier-eny=@ test-mode=?]
  23. ^- verify-result
  24. ?> =(~ hashes.proof)
  25. =^ puzzle proof
  26. =^(c proof ~(pull proof-stream proof) ?>(?=(%puzzle -.c) c^proof))
  27. =/ [s=* f=*] (puzzle-nock commitment.puzzle nonce.puzzle len.puzzle)
  28. ::
  29. :: get computation in raw noun form
  30. ?> (based-noun p.puzzle)
  31. ::
  32. =. table-names %- sort
  33. :_ t-order
  34. ?~ override
  35. gen-table-names:nock-common
  36. u.override
  37. ::~& table-names+table-names
  38. ::
  39. ::
  40. :: compute dynamic table widths
  41. =. table-base-widths (compute-base-widths override)
  42. =. table-full-widths (compute-full-widths override)
  43. ::
  44. :: get table heights
  45. =^ heights proof
  46. =^(h proof ~(pull proof-stream proof) ?>(?=(%heights -.h) p.h^proof))
  47. =/ num-tables (lent heights)
  48. ?> =(num-tables (lent core-table-names:nock-common))
  49. ::~& table-heights+heights
  50. ::
  51. =/ c constraints
  52. =/ pre=preprocess-0 prep.stark-config
  53. ::
  54. ::
  55. :: remove preprocess data for unused tables
  56. =. pre
  57. (remove-unused-constraints:nock-common pre table-names override)
  58. ::
  59. =/ clc ~(. calc heights cd.pre)
  60. ::
  61. :: verify size of proof
  62. =/ expected-num-proof-items=@ud
  63. ;: add
  64. ::
  65. :: number of static items in proof-data
  66. ::
  67. 12
  68. ::
  69. :: number of items written by FRI
  70. ::
  71. %+ add num-rounds:fri:clc
  72. (mul num-rounds:fri:clc num-spot-checks:fri:clc)
  73. ::
  74. :: number of merkle lookups into the deep codeword
  75. ::
  76. (mul 4 num-spot-checks:fri:clc)
  77. ==
  78. =/ actual-num-proof-items=@ud (lent objects.proof)
  79. ?> =(expected-num-proof-items actual-num-proof-items)
  80. ::
  81. :: get merkle root of base tables
  82. =^ base-root proof
  83. =^(b proof ~(pull proof-stream proof) ?>(?=(%m-root -.b) p.b^proof))
  84. ::
  85. =/ rng ~(verifier-fiat-shamir proof-stream proof)
  86. ::
  87. :: get coefficients for table extensions
  88. :: challenges: a, b, c, alpha
  89. =^ chals-rd1=(list belt) rng (belts:rng num-chals-rd1:chal)
  90. ::
  91. :: get merkle root of extension tables
  92. =^ ext-root proof
  93. =^(e proof ~(pull proof-stream proof) ?>(?=(%m-root -.e) p.e^proof))
  94. ::
  95. =. rng ~(verifier-fiat-shamir proof-stream proof)
  96. ::
  97. :: get coefficients for table extensions
  98. :: challenges: beta, z
  99. =^ chals-rd2=(list belt) rng (belts:rng num-chals-rd2:chal)
  100. =/ challenges (weld chals-rd1 chals-rd2)
  101. =/ chal-map=(map term belt)
  102. (bp-zip-chals-list:chal chal-names-basic:chal challenges)
  103. ::
  104. =/ [alf=pelt j=pelt k=pelt l=pelt m=pelt z=pelt]
  105. :* (got-pelt chal-map %alf)
  106. (got-pelt chal-map %j)
  107. (got-pelt chal-map %k)
  108. (got-pelt chal-map %l)
  109. (got-pelt chal-map %m)
  110. (got-pelt chal-map %z)
  111. ==
  112. ::
  113. =/ subj-data
  114. (build-tree-data:fock s alf)
  115. =/ form-data
  116. (build-tree-data:fock f alf)
  117. =/ prod-data
  118. (build-tree-data:fock p.puzzle alf)
  119. ::
  120. :: get terminals
  121. =^ terminals proof
  122. =^(t proof ~(pull proof-stream proof) ?>(?=(%terms -.t) p.t^proof))
  123. ::
  124. =. rng ~(verifier-fiat-shamir proof-stream proof)
  125. ::
  126. :: verify that len.terminals is as expected
  127. ?. .= len.terminals
  128. %- lent
  129. %+ roll all-terminal-names:nock-common
  130. |= [terms=(list term) acc=(list term)]
  131. ^- (list term)
  132. (weld acc terms)
  133. ~& "len.terminals is wrong"
  134. !!
  135. ::
  136. :: verify that len.terminals is the length of the data buffer of the bpoly
  137. ?. ~(chck bop terminals)
  138. ~& "len.terminals is not equal to the data buffer"
  139. !!
  140. ::
  141. ::
  142. =/ [terminal-map=(map term belt) dyn-map=(map @ bpoly)]
  143. =- [term-map dyn-map]
  144. %+ roll all-terminal-names:nock-common
  145. |= [terms=(list term) table-num=@ idx=@ term-map=(map term belt) dyn-map=(map @ bpoly)]
  146. :^ +(table-num)
  147. (add idx (lent terms))
  148. %- ~(gas by term-map)
  149. %+ iturn terms
  150. |= [i=@ t=term]
  151. [t (~(snag bop terminals) (add idx i))]
  152. %- ~(put by dyn-map)
  153. [table-num (~(swag bop terminals) idx (lent terms))]
  154. ::
  155. ?. (linking-checks subj-data form-data prod-data j k l m z terminal-map)
  156. ~& "failed input linking checks" !!
  157. ::
  158. ::
  159. :: evaluate the second composition poly
  160. =/ total-extra-constraints=@
  161. %+ roll (range num-tables)
  162. |= [i=@ acc=@]
  163. =/ cs (~(got by count-map.pre) i)
  164. ;: add
  165. acc
  166. boundary.cs
  167. row.cs
  168. transition.cs
  169. terminal.cs
  170. extra.cs
  171. ==
  172. =^ extra-comp-weights=bpoly rng
  173. =^ belts rng (belts:rng (mul 2 total-extra-constraints))
  174. [(init-bpoly belts) rng]
  175. =/ extra-composition-chals=(map @ bpoly)
  176. %- ~(gas by *(map @ bpoly))
  177. =- -<
  178. %+ roll (range num-tables)
  179. |= [i=@ acc=(list [@ bpoly]) num=@]
  180. =/ cs (~(got by count-map.pre) i)
  181. =/ num-constraints=@
  182. ;: add
  183. boundary.cs
  184. row.cs
  185. transition.cs
  186. terminal.cs
  187. extra.cs
  188. ==
  189. :_ (add num (mul 2 num-constraints))
  190. [[i (~(scag bop (~(slag bop extra-comp-weights) num)) (mul 2 num-constraints))] acc]
  191. ::
  192. =^ extra-comp-bpoly proof
  193. =^(c proof ~(pull proof-stream proof) ?>(?=(%poly -.c) p.c^proof))
  194. ::
  195. =. rng ~(verifier-fiat-shamir proof-stream proof)
  196. ::
  197. =^ extra-comp-eval-point rng $:felt:rng
  198. ::
  199. =^ extra-trace-evaluations=fpoly proof
  200. =^(t proof ~(pull proof-stream proof) ?>(?=(%evals -.t) p.t^proof))
  201. ::
  202. :: check that the size of the evaluations is exactly twice the total number of
  203. :: columns across all tables
  204. =/ total-cols=@
  205. %+ roll table-full-widths
  206. |= [w=@ acc=@]
  207. (add w acc)
  208. ?> =(len.extra-trace-evaluations (mul 2 total-cols))
  209. ?> ~(chck fop extra-trace-evaluations)
  210. ::
  211. =/ extra-composition-eval=felt
  212. %- eval-composition-poly
  213. :* extra-trace-evaluations
  214. heights
  215. constraint-map.pre
  216. count-map.pre
  217. dyn-map
  218. extra-composition-chals
  219. challenges
  220. max-degree:clc
  221. extra-comp-eval-point
  222. table-full-widths
  223. s
  224. f
  225. %.y
  226. ==
  227. ::
  228. =/ extra-comp-bpoly-eval (bpeval-lift extra-comp-bpoly extra-comp-eval-point)
  229. ::
  230. :: check that the extra composition eval equals the eval pt
  231. ?> =(extra-composition-eval extra-comp-bpoly-eval)
  232. ::
  233. :: get merkle root of mega-extension tables
  234. =^ mega-ext-root proof
  235. =^(m proof ~(pull proof-stream proof) ?>(?=(%m-root -.m) p.m^proof))
  236. ::
  237. =. rng ~(verifier-fiat-shamir proof-stream proof)
  238. ::
  239. :: We now use the randomness to compute the expected fingerprints of the compute stack and product stack based on the given [s f] and product, respectively.
  240. :: We then dynamically generate constraints that force the cs and ps to be equivalent to the expected fingerprints.
  241. :: As long as the prover replicates this exact protocol, the opened indicies should match up.
  242. :: The boundary constraint then ensures that the computation in cleartext is linked to the computation in the trace.
  243. ::
  244. :: generate scalars for the random linear combination of the composition polynomial
  245. =/ total-constraints=@
  246. %+ roll (range num-tables)
  247. |= [i=@ acc=@]
  248. =/ cs (~(got by count-map.pre) i)
  249. ;: add
  250. acc
  251. boundary.cs
  252. row.cs
  253. transition.cs
  254. terminal.cs
  255. ==
  256. =^ comp-weights=bpoly rng
  257. =^ belts rng (belts:rng (mul 2 total-constraints))
  258. [(init-bpoly belts) rng]
  259. =/ composition-chals=(map @ bpoly)
  260. %- ~(gas by *(map @ bpoly))
  261. =- -<
  262. %+ roll (range num-tables)
  263. |= [i=@ acc=(list [@ bpoly]) num=@]
  264. =/ cs (~(got by count-map.pre) i)
  265. =/ num-constraints=@
  266. ;: add
  267. boundary.cs
  268. row.cs
  269. transition.cs
  270. terminal.cs
  271. ==
  272. :_ (add num (mul 2 num-constraints))
  273. [[i (~(scag bop (~(slag bop comp-weights) num)) (mul 2 num-constraints))] acc]
  274. ::~& max-degree+max-degree:clc
  275. ::
  276. :: read the composition piece codewords
  277. =^ comp-root proof
  278. =^(c proof ~(pull proof-stream proof) ?>(?=(%comp-m -.c) [p.c num.c]^proof))
  279. ::
  280. ::
  281. =. rng ~(verifier-fiat-shamir proof-stream proof)
  282. ::
  283. :: generate the DEEP challenge
  284. =^ deep-challenge=felt rng
  285. =^ deep-candidate=felt rng $:felt:rng
  286. =/ n fri-domain-len:clc
  287. =/ exp-offset (lift (bpow generator:stark-engine n))
  288. |-
  289. =/ exp-deep-can (fpow deep-candidate n)
  290. :: reject if deep-candidate is in the evaluation domain H or FRI domain D
  291. ?. ?|(=(exp-deep-can f1) =(exp-deep-can exp-offset))
  292. [deep-candidate rng]
  293. =^ felt rng $:felt:rng
  294. $(deep-candidate felt)
  295. ::
  296. :: read the trace evaluations at the DEEP challenge point
  297. =^ trace-evaluations=fpoly proof
  298. =^(t proof ~(pull proof-stream proof) ?>(?=(%evals -.t) p.t^proof))
  299. ::
  300. ::
  301. :: check that the size of the evaluations is exactly twice the total number of
  302. :: columns across all tables
  303. ?> =(len.trace-evaluations (mul 2 total-cols))
  304. ?> ~(chck fop trace-evaluations)
  305. ::
  306. :: read the composition piece evaluations at the DEEP challenge point
  307. =^ composition-piece-evaluations=fpoly proof
  308. =^(c proof ~(pull proof-stream proof) ?>(?=(%evals -.c) p.c^proof))
  309. ::
  310. :: check that there are the correct number of composition piece evaluations and no more
  311. ?. ?& =(len.composition-piece-evaluations +.comp-root)
  312. ~(chck fop composition-piece-evaluations)
  313. ==
  314. ~& >> %num-composition-piece-evals-wrong
  315. !!
  316. ::
  317. =. rng ~(verifier-fiat-shamir proof-stream proof)
  318. :: verify the composition polynomial equals the composition pieces by evaluating each side
  319. :: at the DEEP challenge point
  320. =/ composition-eval=felt
  321. %- eval-composition-poly
  322. :* trace-evaluations
  323. heights
  324. constraint-map.pre
  325. count-map.pre
  326. dyn-map
  327. composition-chals
  328. challenges
  329. max-degree:clc
  330. deep-challenge
  331. table-full-widths
  332. s
  333. f
  334. %.n
  335. ==
  336. ::
  337. =/ decomposition-eval=felt
  338. %+ roll (range +.comp-root)
  339. |= [i=@ acc=_(lift 0)]
  340. ~| 'A crash here sometimes indicates that one of the constraints is degree 5 or higher'
  341. %+ fadd acc
  342. (fmul (fpow deep-challenge i) (~(snag fop composition-piece-evaluations) i))
  343. ::
  344. ?. =(composition-eval decomposition-eval)
  345. ~& %composition-eval-failed
  346. ~& %invalid-proof !!
  347. ::~& %composition-eval-passed
  348. ::
  349. ::
  350. :: generate random weights for DEEP composition polynomial
  351. =^ deep-weights=fpoly rng
  352. =^ felt-list rng
  353. %- felts:rng
  354. :(add len.trace-evaluations len.extra-trace-evaluations len.composition-piece-evaluations)
  355. [(init-fpoly felt-list) rng]
  356. ::
  357. :: read the merkle root of the DEEP composition polynomial
  358. =^ deep-root proof
  359. =^(d proof ~(pull proof-stream proof) ?>(?=(%m-root -.d) p.d^proof))
  360. ::
  361. :: verify the DEEP composition polynomial is low degree
  362. =^ [fri-indices=(list @) merks=(list merk-data:merkle) deep-cosets=(map @ fpoly) fri-res=?] proof
  363. (verify:fri:clc proof deep-root)
  364. ::
  365. ?. =(fri-res %.y)
  366. ~& %deep-composition-polynomial-is-not-low-degree
  367. ~& %invalid-proof !!
  368. ::~& %deep-composition-polynomial-is-low-degree
  369. ::
  370. ::
  371. :: verify the DEEP codeword is actually the codeword of the DEEP composition polynomial by evaluating
  372. :: it at all the top level FRI points by opening the trace and piece polynomials and comparing with the
  373. :: deep codeword. This convinces the verifier that it ran FRI on the correct polynomial.
  374. ::
  375. :: Open trace and composition piece polynomials at the top level FRI indices
  376. ::
  377. =^ [elems=elem-list merk-proofs=(list merk-data:merkle)]
  378. proof
  379. %+ roll fri-indices
  380. |= $: idx=@
  381. $: l=(list [idx=@ trace-elems=(list belt) comp-elems=(list felt) deep-elem=felt])
  382. proofs=_merks
  383. ==
  384. proof=_proof
  385. ==
  386. =/ axis (index-to-axis:merkle (xeb fri-domain-len:clc) idx)
  387. =^ base-trace-opening proof
  388. =^(mp proof ~(pull proof-stream proof) ?>(?=(%m-pathbf -.mp) p.mp^proof))
  389. =^ ext-opening proof
  390. =^(mp proof ~(pull proof-stream proof) ?>(?=(%m-pathbf -.mp) p.mp^proof))
  391. =^ mega-ext-opening proof
  392. =^(mp proof ~(pull proof-stream proof) ?>(?=(%m-pathbf -.mp) p.mp^proof))
  393. =^ comp-opening proof
  394. =^(mp proof ~(pull proof-stream proof) ?>(?=(%m-pathbf -.mp) p.mp^proof))
  395. ::
  396. =. proofs
  397. :*
  398. :* (hash-hashable:tip5 (hashable-bpoly:tip5 leaf.base-trace-opening))
  399. axis base-root path.base-trace-opening
  400. ==
  401. ::
  402. :* (hash-hashable:tip5 (hashable-bpoly:tip5 leaf.ext-opening))
  403. axis ext-root path.ext-opening
  404. ==
  405. ::
  406. :* (hash-hashable:tip5 (hashable-bpoly:tip5 leaf.mega-ext-opening))
  407. axis mega-ext-root path.mega-ext-opening
  408. ==
  409. ::
  410. :* (hash-hashable:tip5 (hashable-bpoly:tip5 leaf.comp-opening))
  411. axis -.comp-root path.comp-opening
  412. ==
  413. ::
  414. proofs
  415. ==
  416. =/ base-elems (bpoly-to-list leaf.base-trace-opening)
  417. ?> (levy base-elems based)
  418. =/ ext-elems (bpoly-to-list leaf.ext-opening)
  419. ?> (levy ext-elems based)
  420. =/ mega-ext-elems (bpoly-to-list leaf.mega-ext-opening)
  421. ?> (levy mega-ext-elems based)
  422. =/ trace-elems=(list belt)
  423. %- zing
  424. :: combines base, ext, and mega-ext openings, divided by table
  425. ^- (list (list belt))
  426. %- turn :_ weld
  427. %+ zip-up
  428. (clev base-elems table-base-widths-static:nock-common)
  429. %- turn :_ weld
  430. %+ zip-up
  431. (clev ext-elems table-ext-widths-static:nock-common)
  432. (clev mega-ext-elems table-mega-ext-widths-static:nock-common)
  433. =/ comp-elems (bpoly-to-list leaf.comp-opening)
  434. ?> (levy comp-elems based)
  435. ::
  436. :: The openings to the deep codeword itself were already read out of the proof
  437. :: during FRI. verify:fri returned deep-cosets=(map @ fpoly), which is all the cosets
  438. :: read from the deep codeword, keyed by coset-idx. We will use this map to find the
  439. :: deep codeword point instead of wasting proof space by writing it into the proof twice.
  440. ::
  441. =/ coset-idx (mod idx (div fri-domain-len:clc folding-deg:fri:clc))
  442. =/ entry (div idx (div fri-domain-len:clc folding-deg:fri:clc))
  443. =/ coset=fpoly (~(got by deep-cosets) coset-idx)
  444. =/ deep-elem=felt (~(snag fop coset) entry)
  445. ::
  446. :_ proof
  447. :- [[idx trace-elems comp-elems deep-elem] l]
  448. proofs
  449. ::
  450. ?: &(=(test-mode %.n) !(verify-merk-proofs merk-proofs verifier-eny))
  451. ~& %failed-to-verify-merk-proofs !!
  452. ::
  453. :: evaluate DEEP polynomial at the indices
  454. =/ omega=felt (lift omega:clc)
  455. =/ all-evals (~(weld fop trace-evaluations) extra-trace-evaluations)
  456. =/ eval-res=?
  457. %+ roll elems
  458. |= [[idx=@ trace-elems=(list belt) comp-elems=(list belt) deep-elem=felt] acc=?]
  459. ^- ?
  460. =/ deep-eval
  461. %- evaluate-deep
  462. :* all-evals
  463. composition-piece-evaluations
  464. trace-elems
  465. comp-elems
  466. +.comp-root
  467. deep-weights
  468. heights
  469. table-full-widths
  470. omega
  471. idx
  472. deep-challenge
  473. extra-comp-eval-point
  474. ==
  475. ~| "DEEP codeword doesn't match evaluation"
  476. ?> =(deep-eval deep-elem)
  477. &(acc %.y)
  478. ~| "DEEP codeword doesn't match evaluation"
  479. ::
  480. ?> =(eval-res %.y)
  481. ::~& %deep-codeword-matches
  482. ::~& %proof-verified
  483. [commitment nonce]:puzzle
  484. ::
  485. ++ compute-base-widths
  486. ~/ %compute-base-widths
  487. |= override=(unit (list term))
  488. ^- (list @)
  489. ?~ override
  490. core-table-base-widths-static:nock-common
  491. (custom-table-base-widths-static:nock-common table-names)
  492. ::
  493. ++ compute-full-widths
  494. ~/ %compute-full-widths
  495. |= override=(unit (list term))
  496. ?~ override
  497. core-table-full-widths-static:nock-common
  498. (custom-table-full-widths-static:nock-common table-names)
  499. ::
  500. ++ linking-checks
  501. ~/ %linking-checks
  502. |= $: s=tree-data f=tree-data p=tree-data
  503. j=pelt k=pelt l=pelt m=pelt z=pelt
  504. mp=(map term belt)
  505. ==
  506. ^- ?
  507. =/ ifp-f (compress-pelt ~[j k l] ~[size dyck leaf]:f)
  508. =/ ifp-s (compress-pelt ~[j k l] ~[size dyck leaf]:s)
  509. ?&
  510. =; bool
  511. ?: bool bool
  512. ~&("memory table node count input check failed" bool)
  513. .= ?@ n.s
  514. z
  515. (pmul z z)
  516. (got-pelt mp %memory-nc)
  517. ::
  518. =; bool
  519. ?: bool bool
  520. ~&("memory table kvs input check failed" bool)
  521. .= ?@ n.s
  522. (pmul z (padd ifp-f (pscal 0 m)))
  523. %+ padd
  524. (pmul z (padd ifp-s (pscal 1 m)))
  525. :(pmul z z (padd ifp-f (pscal 0 m)))
  526. (got-pelt mp %memory-kvs)
  527. ::
  528. =; bool
  529. ?: bool bool
  530. ~&("compute table subject size input check failed" bool)
  531. =(size.s (got-pelt mp %compute-s-size))
  532. ::
  533. =; bool
  534. ?: bool bool
  535. ~&("compute table subject dyck word input check failed" bool)
  536. .= dyck.s
  537. (got-pelt mp %compute-s-dyck)
  538. ::
  539. =; bool
  540. ?: bool bool
  541. ~&("compute table subject leaf vector input check failed" bool)
  542. =(leaf.s (got-pelt mp %compute-s-leaf))
  543. ::
  544. =; bool
  545. ?: bool bool
  546. ~&("compute table formula size input check failed" bool)
  547. =(size.f (got-pelt mp %compute-f-size))
  548. ::
  549. =; bool
  550. ?: bool bool
  551. ~&("compute table formula dyck word input check failed" bool)
  552. =(dyck.f (got-pelt mp %compute-f-dyck))
  553. ::
  554. =; bool
  555. ?: bool bool
  556. ~&("compute table formula leaf vector input check failed" bool)
  557. =(leaf.f (got-pelt mp %compute-f-leaf))
  558. ::
  559. =; bool
  560. ?: bool bool
  561. ~&("compute table product size input check failed" bool)
  562. =(size.p (got-pelt mp %compute-e-size))
  563. ::
  564. =; bool
  565. ?: bool bool
  566. ~&("compute table product dyck word input check failed" bool)
  567. =(dyck.p (got-pelt mp %compute-e-dyck))
  568. ::
  569. =; bool
  570. ?: bool bool
  571. ~&("compute table product leaf vector input check failed" bool)
  572. =(leaf.p (got-pelt mp %compute-e-leaf))
  573. ::
  574. =; bool
  575. ?: bool bool
  576. ~&("decode multiset terminal comparison check failed" bool)
  577. =((got-pelt mp %compute-decode-mset) (got-pelt mp %memory-decode-mset))
  578. ::
  579. =; bool
  580. ?: bool bool
  581. ~&("Nock 0 multiset terminal comparison check failed" bool)
  582. =((got-pelt mp %compute-op0-mset) (got-pelt mp %memory-op0-mset))
  583. ==
  584. ::
  585. ++ eval-composition-poly
  586. ~/ %eval-composition-poly
  587. |= $: trace-evaluations=fpoly
  588. heights=(list @)
  589. constraint-map=(map @ constraints)
  590. constraint-counts=(map @ constraint-counts)
  591. dyn-map=(map @ bpoly)
  592. composition-chals=(map @ bpoly)
  593. challenges=(list belt)
  594. max-degree=@
  595. deep-challenge=felt
  596. table-full-widths=(list @)
  597. s=*
  598. f=*
  599. is-extra=?
  600. ==
  601. ^- felt
  602. =/ max-height=@
  603. %- bex %- xeb %- dec
  604. (roll heights max)
  605. =/ dp (degree-processing heights constraint-map is-extra)
  606. =/ boundary-zerofier=felt
  607. (finv (fsub deep-challenge (lift 1)))
  608. =/ chal-map=(map @ belt)
  609. (make-challenge-map:chal challenges s f)
  610. |^
  611. =- -<
  612. %^ zip-roll (range (lent heights)) heights
  613. |= [[i=@ height=@] acc=_(lift 0) evals=_trace-evaluations]
  614. =/ width=@ (snag i table-full-widths)
  615. =/ omicron (lift (ordered-root height))
  616. =/ last-row (fsub deep-challenge (finv omicron))
  617. =/ terminal-zerofier (finv last-row) :: f(X)=1/(X-g^{-1})
  618. =/ chals=bpoly (~(got by composition-chals) i)
  619. =/ height=@ (snag i heights)
  620. =/ constraints (~(got by constraint-w-deg-map.dp) i)
  621. =/ counts (~(got by constraint-counts) i)
  622. =/ dyns (~(got by dyn-map) i)
  623. =/ row-zerofier (finv (fsub (fpow deep-challenge height) (lift 1))) :: f(X)=1/(X^N-1)
  624. =/ transition-zerofier :: f(X)=(X-g^{-1})/(X^N-1)
  625. (fmul last-row row-zerofier)
  626. ::
  627. =/ current-evals=fpoly (~(scag fop evals) (mul 2 width))
  628. :_ (~(slag fop evals) (mul 2 width))
  629. ;: fadd
  630. acc
  631. ::
  632. %+ fmul boundary-zerofier
  633. %- evaluate-constraints
  634. :* boundary.constraints
  635. dyns
  636. chal-map
  637. current-evals
  638. (~(scag bop chals) (mul 2 boundary.counts))
  639. ==
  640. ::
  641. %+ fmul row-zerofier
  642. %- evaluate-constraints
  643. :* row.constraints
  644. dyns
  645. chal-map
  646. current-evals
  647. ::
  648. %+ ~(swag bop chals)
  649. (mul 2 boundary.counts)
  650. (mul 2 row.counts)
  651. ::
  652. ==
  653. ::
  654. %+ fmul transition-zerofier
  655. %- evaluate-constraints
  656. :* transition.constraints
  657. dyns
  658. chal-map
  659. current-evals
  660. ::
  661. %+ ~(swag bop chals)
  662. (mul 2 (add boundary.counts row.counts))
  663. (mul 2 transition.counts)
  664. ::
  665. ==
  666. ::
  667. %+ fmul terminal-zerofier
  668. %- evaluate-constraints
  669. :* terminal.constraints
  670. dyns
  671. chal-map
  672. current-evals
  673. ::
  674. %+ ~(swag bop chals)
  675. (mul 2 :(add boundary.counts row.counts transition.counts))
  676. (mul 2 terminal.counts)
  677. ::
  678. ==
  679. ::
  680. ?. is-extra (lift 0)
  681. %+ fmul row-zerofier
  682. %- evaluate-constraints
  683. :* extra.constraints
  684. dyns
  685. chal-map
  686. current-evals
  687. ::
  688. %- ~(slag bop chals)
  689. %+ mul 2
  690. ;: add
  691. boundary.counts
  692. row.counts
  693. transition.counts
  694. terminal.counts
  695. ==
  696. ::
  697. ==
  698. ==
  699. ::
  700. ++ evaluate-constraints
  701. |= $: constraints=(list [(list @) mp-ultra])
  702. dyns=bpoly
  703. chal-map=(map @ belt)
  704. evals=fpoly
  705. chals=bpoly
  706. ==
  707. ^- felt
  708. =- acc
  709. %+ roll constraints
  710. |= [[degs=(list @) c=mp-ultra] [idx=@ acc=_(lift 0)]]
  711. ::
  712. :: evaled is a list because the %comp constraint type
  713. :: can contain multiple mp-mega constraints.
  714. =/ evaled=(list felt) (mpeval-ultra %ext c evals chal-map dyns)
  715. %+ roll
  716. (zip-up degs evaled)
  717. |= [[deg=@ eval=felt] [idx=_idx acc=_acc]]
  718. :- +(idx)
  719. ::
  720. :: Each constraint corresponds to two weights: alpha and beta. The verifier
  721. :: samples 2*num_constraints random values and we assume that the alpha
  722. :: and beta weights for a given constraint are situated next to each other
  723. :: in the array.
  724. ::
  725. =/ alpha (~(snag bop chals) (mul 2 idx))
  726. =/ beta (~(snag bop chals) (add 1 (mul 2 idx)))
  727. ::
  728. :: TODO: I've removed the degree adjustments but left it commented out. Once we figrue
  729. :: out exactly how to do it we can put it back in.
  730. %+ fadd acc
  731. %+ fmul eval
  732. %+ fadd (lift beta)
  733. %+ fmul (lift alpha)
  734. (fpow deep-challenge (sub fri-deg-bound.dp deg))
  735. -- ::+eval-composition-poly
  736. ::
  737. ++ evaluate-deep
  738. ~/ %evaluate-deep
  739. |= $: trace-evaluations=fpoly
  740. comp-evaluations=fpoly
  741. trace-elems=(list belt)
  742. comp-elems=(list belt)
  743. num-comp-pieces=@
  744. weights=fpoly
  745. heights=(list @)
  746. full-widths=(list @)
  747. omega=felt
  748. index=@
  749. deep-challenge=felt
  750. new-comp-eval=felt
  751. ==
  752. ^- felt
  753. =/ omega-pow (fmul (lift g) (fpow omega index))
  754. |^
  755. =/ [acc=felt num=@ @]
  756. %^ zip-roll (range (lent heights)) heights
  757. |= [[i=@ height=@] acc=_(lift 0) num=@ total-full-width=@]
  758. =/ full-width (snag i full-widths)
  759. =/ omicron (lift (ordered-root height))
  760. =/ current-trace-elems (swag [total-full-width full-width] trace-elems)
  761. =/ dat=[acc=felt num=@] [acc num]
  762. :: first row trace columns
  763. =/ denom (fsub omega-pow deep-challenge)
  764. =. dat
  765. %- process-belt
  766. :* current-trace-elems
  767. trace-evaluations
  768. weights
  769. full-width
  770. num.dat
  771. denom
  772. acc.dat
  773. ==
  774. :: second row trace columns obtained by shifting by omicron
  775. =. denom (fsub omega-pow (fmul deep-challenge omicron))
  776. =. dat
  777. %- process-belt
  778. :* current-trace-elems
  779. trace-evaluations
  780. weights
  781. full-width
  782. num.dat
  783. denom
  784. acc.dat
  785. ==
  786. [acc.dat num.dat (add total-full-width full-width)]
  787. ::
  788. ::
  789. =/ [acc=felt num=@ @]
  790. %^ zip-roll (range (lent heights)) heights
  791. |= [[i=@ height=@] acc=_acc num=_num total-full-width=@]
  792. =/ full-width (snag i full-widths)
  793. =/ omicron (lift (ordered-root height))
  794. =/ current-trace-elems (swag [total-full-width full-width] trace-elems)
  795. =/ dat=[acc=felt num=@] [acc num]
  796. :: first row trace columns
  797. :: evaluate new evals
  798. =/ denom (fsub omega-pow new-comp-eval)
  799. =. dat
  800. %- process-belt
  801. :* current-trace-elems
  802. trace-evaluations
  803. weights
  804. full-width
  805. num.dat
  806. denom
  807. acc.dat
  808. ==
  809. :: second row trace columns obtained by shifting by omicron
  810. =. denom (fsub omega-pow (fmul new-comp-eval omicron))
  811. =. dat
  812. %- process-belt
  813. :* current-trace-elems
  814. trace-evaluations
  815. weights
  816. full-width
  817. num.dat
  818. denom
  819. acc.dat
  820. ==
  821. [acc.dat num.dat (add total-full-width full-width)]
  822. ::
  823. =/ denom (fsub omega-pow (fpow deep-challenge num-comp-pieces))
  824. =- -<
  825. %- process-belt
  826. :* comp-elems
  827. comp-evaluations
  828. (~(slag fop weights) num)
  829. num-comp-pieces
  830. 0
  831. denom
  832. acc
  833. ==
  834. ::
  835. ++ process-belt
  836. |= $: elems=(list belt)
  837. evals=fpoly
  838. weights=fpoly
  839. width=@
  840. num=@
  841. denom=felt
  842. acc=felt
  843. ==
  844. ^- [felt @]
  845. %+ roll (range width)
  846. |= [i=@ acc=_acc num=_num]
  847. :_ +(num)
  848. %+ fadd acc
  849. %+ fmul (~(snag fop weights) num)
  850. %+ fdiv
  851. (fsub (lift (snag i elems)) (~(snag fop evals) num))
  852. denom
  853. -- ::+evaluate-deep
  854. ::
  855. :: verify a list of merkle proofs in a random order. This is to guard against DDOS attacks.
  856. ++ verify-merk-proofs
  857. ~/ %verify-merk-proofs
  858. |= [ps=(list merk-data:merkle) eny=@]
  859. ^- ?
  860. =/ tog-eny (new:tog:tip5 sponge:(absorb:(new:sponge:tip5) (mod eny p)^~))
  861. =/ lst=(list [@ merk-data:merkle])
  862. =- l
  863. %+ roll ps
  864. |= [m=merk-data:merkle rng=_tog-eny l=(list [@ merk-data:merkle])]
  865. ^+ [tog:tip5 *(list [@ merk-data:merkle])]
  866. =^ rnd rng (belts:rng 1)
  867. :- rng
  868. [[(head rnd) m] l]
  869. =/ sorted=(list [@ m=merk-data:merkle])
  870. %+ sort lst
  871. |= [x=[@ *] y=[@ *]]
  872. (lth -.x -.y)
  873. |-
  874. ?~ sorted %.y
  875. =/ res (verify-merk-proof:merkle m.i.sorted)
  876. ?. res
  877. %.n
  878. $(sorted t.sorted)
  879. --