verifier.hoon 23 KB

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