verifier.hoon 23 KB

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