four.hoon 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /= ztd-three /common/ztd/three
  2. => ztd-three
  3. ~% %proof-lib ..merkle ~
  4. :: proof-library
  5. |%
  6. +| %sur-proof-stream
  7. +$ noun-digests (list noun-digest:tip5)
  8. +$ proof-path [leaf=fpoly path=noun-digests]
  9. +$ proof-path-bf [leaf=bpoly path=noun-digests]
  10. ::
  11. +$ proof-data
  12. $% [%m-root p=noun-digest:tip5] :: merk-root
  13. [%puzzle commitment=noun-digest:tip5 nonce=noun-digest:tip5 len=@ p=*]
  14. [%codeword p=fpoly]
  15. [%terms p=bpoly] :: terminals
  16. [%m-paths a=proof-path b=proof-path c=proof-path]
  17. [%m-path p=proof-path] :: merk-path
  18. [%m-pathbf p=proof-path-bf] :: merk-path-bf
  19. [%comp-m p=noun-digest:tip5 num=@] :: composition-merk
  20. [%evals p=fpoly] :: evaluations
  21. [%heights p=(list @)] :: n, where 2^n is the number of rows
  22. [%poly p=bpoly]
  23. ==
  24. ::
  25. +$ proof-objects (list proof-data)
  26. ::
  27. +$ proof
  28. $: version=%0
  29. objects=proof-objects
  30. hashes=(list noun-digest:tip5)
  31. read-index=@
  32. ==
  33. ::
  34. +$ tip5-hash-atom @ux
  35. ::
  36. :: number of items in proof used for pow
  37. ++ pow-items 7
  38. :: extract pow from proof
  39. ++ get-pow
  40. ~/ %get-pow
  41. |= p=proof
  42. ^- proof
  43. p(objects (scag pow-items objects.p))
  44. ::
  45. ++ proof-to-pow
  46. ~/ %proof-to-pow
  47. |= =proof
  48. ^- tip5-hash-atom
  49. (digest-to-atom:tip5 (hash-proof (get-pow proof)))
  50. ::
  51. ++ hashable-proof-objects
  52. ~/ %hashable-proof-objects
  53. |= ps=proof-objects
  54. ^- hashable:tip5
  55. [%list (turn ps hashable-proof-data)]
  56. ::
  57. ++ hash-proof
  58. ~/ %hash-proof
  59. |= p=proof
  60. ^- noun-digest:tip5
  61. =/ rng (absorb-proof-objects objects.p ~)
  62. =^ lis=(list belt) rng (belts:rng 5)
  63. =- ?> ?=(noun-digest:tip5 -) -
  64. (list-to-tuple:tip5 lis)
  65. ::
  66. ++ absorb-proof-objects
  67. ~/ %absorb-proof-objects
  68. |= [objs=proof-objects hashes=(list noun-digest:tip5)]
  69. ^+ tog:tip5
  70. =. objs (slag (lent hashes) objs)
  71. =/ lis-digests=[%list (list hashable:tip5)]
  72. =/ h (hashable-noun-digests:tip5 hashes)
  73. ?> ?=(%list -.h)
  74. h
  75. =/ lis-objects=[%list (list hashable:tip5)]
  76. =/ h (hashable-proof-objects objs)
  77. ?> ?=(%list -.h)
  78. h
  79. =/ big-lis=(list noun-digest:tip5)
  80. (turn `(list hashable:tip5)`(weld +.lis-digests +.lis-objects) hash-hashable:tip5)
  81. =/ sponge (new:sponge:tip5)
  82. |-
  83. ?~ big-lis
  84. (new:tog:tip5 sponge:sponge)
  85. =+ [a=@ b=@ c=@ d=@ e=@]=i.big-lis
  86. =/ lis=(list belt) [a b c d e ~]
  87. $(big-lis t.big-lis, sponge (absorb:sponge lis))
  88. ::
  89. ::
  90. ++ hashable-proof-data
  91. ~/ %hashable-proof-data
  92. |= pd=proof-data
  93. ^- hashable:tip5
  94. ?- -.pd
  95. %m-root [leaf+%m-root hash+p.pd]
  96. %puzzle [leaf+%puzzle hash+commitment.pd hash+nonce.pd leaf+len.pd leaf+p.pd]
  97. %comp-m [leaf+%comp-m hash+p.pd leaf+num.pd]
  98. %heights [leaf+%heights leaf+p.pd]
  99. %codeword [leaf+%codeword (hashable-fpoly:tip5 p.pd)]
  100. %evals [leaf+%evals (hashable-fpoly:tip5 p.pd)]
  101. %terms [leaf+%terms (hashable-bpoly:tip5 p.pd)]
  102. %poly [leaf+%poly (hashable-bpoly:tip5 p.pd)]
  103. ::
  104. %m-pathbf
  105. :- leaf+%m-pathbf
  106. :- (hashable-bpoly:tip5 leaf.p.pd)
  107. (hashable-noun-digests:tip5 path.p.pd)
  108. ::
  109. %m-path
  110. :- leaf+%m-mpath
  111. :- (hashable-fpoly:tip5 leaf.p.pd)
  112. (hashable-noun-digests:tip5 path.p.pd)
  113. ::
  114. %m-paths
  115. :- leaf+%m-mpaths
  116. :+ :- (hashable-fpoly:tip5 leaf.a.pd)
  117. (hashable-noun-digests:tip5 path.a.pd)
  118. :- (hashable-fpoly:tip5 leaf.b.pd)
  119. (hashable-noun-digests:tip5 path.b.pd)
  120. :- (hashable-fpoly:tip5 leaf.c.pd)
  121. (hashable-noun-digests:tip5 path.c.pd)
  122. ==
  123. ::
  124. ++ hash-proof-data
  125. ~/ %hash-proof-data
  126. |= pd=proof-data
  127. ^- noun-digest:tip5
  128. (hash-hashable:tip5 (hashable-proof-data pd))
  129. ::
  130. +| %sur-fock
  131. ::
  132. :: $zero-map: see description
  133. ::
  134. :: Nock 10 edits the noun so it has subject for the original noun and new-subject for the
  135. :: new edited noun. Nock 0 is proved exactly like a nock 10 but with new-subject=subject.
  136. :: So when recording a nock 0 you want to just pass subject in for new-subject.
  137. :: Basically a nock 0 is a special case of nock 10 where the edited tree is the original tree.
  138. +$ zero-map (map subject=* (map [axis=* new-subject=*] count=@))
  139. +$ decode-map (map [formula=* head=* tail=*] count=@)
  140. +$ fock-return
  141. $+ fock-return
  142. $: queue=(list *)
  143. zeroes=zero-map
  144. decodes=decode-map
  145. [s=* f=*]
  146. ::jutes=(list [@tas sam=* prod=*])
  147. ==
  148. :: $dyck-stack: horner accumulated stack of dyck path
  149. :: $dyck-felt: felt representing dyck-stack
  150. :: $leaf-stack: horner accumulated stack of leaves
  151. :: $leaf-felt: felt representing leaf-stack
  152. :: $ion-fprint: compressed $ion-triple: a*len + b*dyck-felt + c*leaf-felt
  153. :: $ion-triple: dyck encoding of a noun. called the ION fingerprint in the EDEN paper.
  154. :: $compute-stack: horner accumulated stack of packed-tree-felts
  155. :: $compute-felt: felt representing compute-stack
  156. :: $tree-data:
  157. ::
  158. :: .len: length of the leaf stack
  159. +$ tree-data
  160. $~ [pone pzero pzero 0]
  161. $: size=pelt :: alf^len
  162. dyck=pelt
  163. leaf=pelt
  164. n=*
  165. ==
  166. ::
  167. +$ pelt $~(pzero @ux)
  168. ::
  169. ++ pzero `pelt`(lift 0)
  170. ++ pone `pelt`(lift 1)
  171. ::
  172. ++ pelt-lift
  173. ~/ %pelt-lift
  174. |= b=belt
  175. ^- pelt
  176. (lift b)
  177. ::
  178. ++ padd
  179. ~/ %padd
  180. |= [p=pelt q=pelt]
  181. ^- pelt
  182. (fadd p q)
  183. ::
  184. ++ pneg
  185. ~/ %pneg
  186. |= p=pelt
  187. ^- pelt
  188. (fneg p)
  189. ::
  190. ++ psub
  191. ~/ %psub
  192. |= [p=pelt q=pelt]
  193. ^- pelt
  194. (fsub p q)
  195. ::
  196. ++ pmul
  197. ~/ %pmul
  198. |= [p=pelt q=pelt]
  199. ^- pelt
  200. (fmul p q)
  201. ::
  202. ++ pscal
  203. ~/ %pscal
  204. |= [c=belt p=pelt]
  205. ^- pelt
  206. dat:(bpscal c [3 p])
  207. ::
  208. ::
  209. ++ pinv
  210. ~/ %pinv
  211. |= p=pelt
  212. ^- pelt
  213. (finv p)
  214. ::
  215. :: +ppow: field power; computes x^n
  216. ++ ppow
  217. ~/ %ppow
  218. |= [x=pelt n=@]
  219. (fpow x n)
  220. ::
  221. ++ print-pelt
  222. ~/ %print-pelt
  223. |= [=pelt t=(list belt)]
  224. ^- (list belt)
  225. :^ (~(snag bop [3 pelt]) 0)
  226. (~(snag bop [3 pelt]) 1)
  227. (~(snag bop [3 pelt]) 2)
  228. t
  229. ::
  230. ++ got-pelt
  231. ~/ %got-pelt
  232. |= [mp=(map term belt) t=term]
  233. ^- pelt
  234. =< dat
  235. %- init-bpoly
  236. :~ (~(got by mp) (crip (weld (trip t) "-a")))
  237. (~(got by mp) (crip (weld (trip t) "-b")))
  238. (~(got by mp) (crip (weld (trip t) "-c")))
  239. ==
  240. ::
  241. ++ compress-pelt
  242. ~/ %compress-pelt
  243. |= [cs=(list pelt) ps=(list pelt)]
  244. ^- pelt
  245. %+ roll (zip-up cs ps)
  246. |= [[c=pelt p=pelt] acc=pelt]
  247. ^- pelt
  248. (padd acc (pmul c p))
  249. ::
  250. +$ compute-queue (list tree-data)
  251. ::
  252. +| %constants
  253. ++ ext-degree 3 :: goldilocks field degree
  254. --