📜 [專欄新文章] Merkle Tree in JavaScript
✍️ Johnson
📥 歡迎投稿: https://medium.com/taipei-ethereum-meetup #徵技術分享文 #使用心得 #教學文 #medium
這篇文章會說明 Merkle Tree 的運作原理,以及解釋 Merkle Proofs 的用意,並以 JavaScript / TypeScript 簡單實作出來。
本文為 Tornado Cash 研究系列的 Part 1,本系列以 tornado-core 為教材,學習開發 ZKP 的應用,另兩篇為:
Part 2:ZKP 與智能合約的開發入門
Part 3:Tornado Cash 實例解析
Special thanks to C.C. Liang for review and enlightenment.
本文中實作的 Merkle Tree 是以 TypeScript 重寫的版本,原始版本為 tornado-core 以 JavaScript 實作而成,基本上大同小異。
Merkle Tree 的原理
在理解 Merkle Tree 之前,最基本的先備知識是 hash function,利用 hash 我們可以對資料進行雜湊,而雜湊後的值是不可逆的,假設我們要對 x 值做雜湊,就以 H(x) 來表示,更多內容可參考:
一次搞懂密碼學中的三兄弟 — Encode、Encrypt 跟 Hash
SHA256 Online
而所謂的 Merkle Tree 就是利用特定的 hash function,將一大批資料兩兩進行雜湊,最後產生一個最頂層的雜湊值 root。
當有一筆資料假設是const leaves = [A, B, C, D],我們就用function Hash(left, right),開始製作這顆樹,產生H(H(A) + H(B))與H(H(C) + H(D)),再將這兩個值再做一次 Hash 變成 H(H(H(A) + H(B)) + H(H(C) + H(D))),就會得到這批資料的唯一值,也就是 root。
本文中使用的命名如下:
root:Merkle Tree 最頂端的值,特色是只要底下的資料一有變動,root 值就會改變。
leaf:指單一個資料,如 H(A)。
levels:指樹的高度 (height),以上述 4 個資料的假設,製作出來的 levels 是 2,levels 通常會作為遞迴的次數。
leaves:指 Merkle Tree 上的所有資料,如上述例子中的 H(A), H(B), H(C), H(D)。leaves 的數量會決定樹的 levels,公式是 leaves.length == 2**levels,這段建議先想清楚!
node:指的是非 leaves 也非 root 的節點,或稱作 branch,如上述例子中的H(H(A) + H(B)) 和 H(H(C) + H(D))。
index:指某個 leaf 所在的位置,leaf = leaves[index],index 如果是偶數,leaf 一定在左邊,如果是奇數 leaf 一定在右邊。
Merkle Proofs
Merkle Proofs 的重點就是要證明資料有沒有在樹上。
如何證明?就是提供要證明的 leaf 以及其相對應的路徑 (path) ,經過計算後一旦能夠產生所需要的 root,就能證明這個 leaf 在這顆樹上。
因此這類要判斷資料有無在樹上的證明,類似的說法有:proving inclusion, proving existence, or proving membership。
這個 proof 的特點在於,我們只提供 leaf 和 path 就可以算出 root,而不需要提供所有的資料 (leaves) 去重新計算整顆 Merkle Tree。這讓我們在驗證資料有沒有在樹上時,不需要花費大量的計算時間,更棒的是,這讓我們只需要儲存 root 就好,而不需要儲存所有的資料。
在區塊鏈上,儲存資料的成本通常很高,也因此 Merkle Tree 的設計往往成為擴容上的重點。
我們知道 n 層的 Merkle Tree 可以存放 2**n 個葉子,以 Tornado Cash 的設計來說,他們設定 Merkle Tree 有 20 層,也就是一顆樹上會有 2**20 = 1048576 個葉子,而我們用一個 root 就代表了這 1048576 筆資料。
接續上段的例子,這顆 20 層的 Merkle Tree 所產生的 Proof ,其路徑 (path) 要從最底下的葉子 hash 幾次才能到達頂端的 root 呢?答案就是跟一棵樹的 levels 一樣,我們要驗證 Proof 所要遞迴的次數就會是 20 次。
在實作之前,我們先來看 MerkleTree 在 client 端是怎麼調用的,這有助於我們理解 Merkle Proofs 在做什麼。
基本上一個 proof 的場景會有兩個人:prover 與 verifier。
在給定一筆 leaves 的樹,必定產生一特定 root。prover 標示他的 leaf 在樹上的 index 等於 2,也就是 leaves[2] == 30,以此來產生一個 proof,這個 proof 的內容大致上會是這個樣子:
對 verifier 來說,他要驗證這個 proof,就是用裡面的 leaf 去一個一個與 pathElements 的值做 hash,上述就是 H('30', 40) 後得出 node,再 hash 一次 H('19786...', node) 於是就能得出這棵樹的 root。
重點來了,這麼做有什麼意義?它的巧思在於對 verifier 來說,他只需要儲存一個 root,由 prover 提交證明給他,經過計算後產生的 root 如果跟 verifier 儲存的 root 一樣,那就證明了 prover 所提供的資料確實存在於這個樹上。
而 verifier 若不透過 proof ,要驗證某個 leaf 是否存在於樹上,也可以把 leaves = [10, 20 ,leaf ,40]整筆資料拿去做 MerkleTree 的演算法跑一趟也能產生特定的 root。
但由 prover 先行計算後所提交的 proof,讓 verifier 不必儲存整批資料,也省去了大量的計算時間,即可做出某資料有無在 Merkle Tree 上的判斷。
Sparse Merkle Tree
上述能夠證明資料有無在樹上的 Merkle Proofs 是屬於標準的 Merkle Tree 的功能。但接下來我們要實作的是稍微不一樣的樹,叫做 Sparse Merkle Tree。
Sparse Merkle Tree 的特色在於除了 proving inclusion 之外,還可以 proving non-inclusion。也就是能夠證明某筆資料不在某個 index,例如 H(A) 不在 index 2 ,這是一般 Merkle Tree 沒辦法做到的。
而要做到 non-membership 的功能其實也不難,就是我們要在沒有資料的葉子裡補上 zero value,或是說 null 值。更多內容請參考:What’s a Sparse Merkle Tree。
實作細節
本節將完整的程式碼分成三個片段來解釋。
首先,這裡使用的 Hash Function 是 MiMC,主要是為了之後在 ZKP 專案上的效率考量,你可以替換成其他較常見的 hash function 例如 node.js 內建 crypto 的 sha256:
crypto.createHash("sha256").update(data.toString()).digest("hex");
這裡定義簡單的 Merkle Tree 介面有 root, proof, and insert。
首先我們必須先給定這顆樹的 levels,也就是樹的高度先決定好,樹所能容納的資料量也因此固定為 2**levels 筆資料,至於要不要有 defaultLeaves 則看創建 Merkle Tree 的 client 自行決定,如果有 defaultLeaves 的話,constructor 就會跑下方一大段計算,對 default 資料開始作 hash 去建立 Merkle Tree。
如果沒有 defaultLeaves,我們的樹也不會是空白的,因為這是顆 Sparse Merkle Tree,這裡使用 zeroValue 作為沒有填上資料的值,zeros 陣列會儲存不同 level 所應該使用的 zero value。假設我們已經填上第 0 筆與第 1 筆資料,要填上第 2 筆資料時,第 2 筆資料就要跟 zeros[0] 做 hash,第 2 筆放左邊, zero value 放右邊。
我們將所有的點不論是 leaf, node, root 都用標籤 (index) 標示,並以 key-value 的形式儲存在 storage 裡面。例如第 0 筆資料會是 0–0,第 1 筆會是 0–1,這兩個 hash 後的節點 (node) 會是 1–0。假設 levels 是 2,1–0 節點就要跟 1–1 節點做 hash,即可產出 root (2–0)。
後半部份的重點在於 proof,先把 proof 和 traverse 看懂,基本上就算是打通任督二脈了,之後有興趣再看 insert 和 update。
sibling 是指要和 current 一起 hashLeftRight 的值…也就是相鄰在兩旁的 leaf (or node)。
到這裡程式碼的部分就結束了。
最後,讓我們回到一開始 client 調用 merkleTree 的例子:
以及 proof 的內容:
前面略過了 proof 裡頭的 pathIndices,pathIndices 告訴你的是當前的 leaf (or node) 是要放在左邊,還是放在右邊,大概是這個樣子:
if (indices == 0) hash(A, B);if (indices == 1) hash(B, A);
有興趣的讀者可以實作 verify function 看看就會知道了!
原始碼
TypeScript from gist
JavaScript from tornado-core
參考
Merkle Proofs Explained
What’s a Sparse Merkle Tree?
延伸:Verkle Tree
Merkle Tree in JavaScript was originally published in Taipei Ethereum Meetup on Medium, where people are continuing the conversation by highlighting and responding to this story.
👏 歡迎轉載分享鼓掌
同時也有5部Youtube影片,追蹤數超過12萬的網紅一二三渡辺,也在其Youtube影片中提到,What is GT-R? GT-R is a vehicle developed with a main objective of use by the circuit the skyline that continues from the C10 Hacosca type (Macartu...
existence value 在 Jamie Q Facebook 的最佳解答
🆕 為配合政府防疫應變措施活動延期,
請密切關注官方及後續消息更新,Stay Safe♥️
”HOOMAN” aka ”倫類”,是動物對人類的稱呼。在動物們的日常生活中,HOOMAN 這種生物,往往是打擾或威脅的代名詞。HOOMAN會吸貓、愛玩狗、喜歡把兔子、倉鼠、烏龜…等各種本來在野外的物種養在家裡,還很常吃許多動物的肉。HOOMAN 佔盡主宰地球的優勢,而動物們的聲音與權利,被剝奪並淹沒在 HOOMAN 濫用資源的行為之中。
在這個《 HOOMAN FESTIVAL 倫類戶外音樂節 》中,我們想傳達的理念:是HOOMAN 應該意識到自己的行為,打破原本與動物的關係,從把牠們當成食物或寵物,慢慢轉變為更尊重牠們身為生命個體的價值與存在。
✅美麗的戶外場所
✅現場音樂, DJ
✅健康的食物
✅為動物們發聲
✅一張雙日票玩到 After Party
活動連結: https://fb.me/e/1Zpyh8OhW
在這場活動中,沒有動物會受到傷害!
--------------------------------------------------------------------
《 HOOMAN FESTIVAL 》
This is an outdoor music festival that celebrates animal protection with a meatless food truck market. The idea we want to convey is that HOOMANs (that’s us!) should be aware of our behavior and relationship with animals, and slowly change from treating them as just food or pets. We want to spread a message that respects their value and existence as individual life forms.
《 HOOMAN FESTIVAL 》will be a two-day event held on 5/22~5/23. There will be two-stage performances with electronic music, highly knowledgeable dog training, animal behavior lectures, a rich and varied vegetarian market, and a fun and exciting After Party. Most important of all, "No animals will be harmed during this period."
Come out and celebrate our animal friends at the HOOMAN festival!
DJ Lineup stay tuned!
📅 《 HOOMAN FESTIVAL Event Info 》
Date: 22nd-23rd, May. 2021 (SAT. and SUN.) 11AM~10PM
💠Location: Guizikeng Camping Site (No. 161, Xiushan Road, Beitou District, Taipei City)
🚌 We will arrange the shuttle bus on-site. [ Beitou Station to HOOMAN Festival ]
🎵 First Day's After Party on 22nd, May. (SAT) 11PM. Venue will be announced soon...
🎫 One Day Pass:
Presale NT$1600
At Door NT$1900
After Party Entrance Fee:NT$500
🎫 Two Day Pass:(Included After Party Entrance Fee.)
Presale NT$2400
At Door NT$2900
🎪 Event Activities:
👯♀️ Double stages 20-hours marathon performance.
🎵 Music style: House / Techno
🥗 Vegetarian market: vegetarian food stalls, eat vegetarian for a day to make yourself healthier*
🐶 Animal Lecture: Pet's knowledge / animal behavior analysis / …and other exciting activities, participate with the event pass.
🐵【 For book VIP booth with butler service, please contact the organizer. 📧Email: service@masiveproductions.com 】
existence value 在 許文昌 Man-cheong Facebook 的精選貼文
De Vaulx founded International Value Investors (IVA) in 2008 alongside Chuck de Lardemelle, with whom he ran the firm’s two funds for most of their existence. Both managers left First Eagle, where they had worked under legendary value manager Jean-Marie Eveillard, to form IVA.
De Vaulx’s death comes less than two months after it was announced that IVA planned to close up shop and liquidate its two funds, IVA International (IVIQX) and IVA Worldwide (IVWIX), a process that began in April.
existence value 在 一二三渡辺 Youtube 的最佳貼文
What is GT-R?
GT-R is a vehicle developed with a main objective of use by the circuit the skyline that continues from the C10 Hacosca type (Macartun). It is a car like the assumption of the win in the race though it is a passenger car base, a mission installing equipment and an engine different from other grades of the skyline, and the influences on other domestic production sports cars, etc.Therefore, there are a lot of madly devoted fans, and the engineer also has a special sticking to. The name of GT-R for that is assumed, and there is a model not permitted either, and it assumes the existence of GT-R to be the one of more special crown. 「There are three famous alphabets worldwide. It is G, T, and R. I will promise here. GT-R revives without fail. 」Carlos Ghosn COO (at that time) made remarks on "GT-R concept" ahead by opening about presentation at the press conference on the Tokyo Motor Show in 2001. I can ask this car for the posture of Nissan that offers it because it entrusted such a content to Gorn where only a thorough rationalization strategy is often taken up. This promise is sold, and the mass production type is announced by the 40th Tokyo Motor Show on October 24, 2007 and Carlos Ghosn will be sold accomplishment Nissan and GT-R on December 6, 2007. Especially, it is known, and not only the country but also the world is 13 in the reputation of R from active distinguished services very much in an overseas race that has infiltrated by calling Godzilla (Godzilla) as for the name though it is from R32 type. Therefore, GT-R of R32, R33, and R34 is introduced to foreign countries though the skyline is almost a car only for the domestic sale by exporter's hand. As for the reputation, R33GT-R was sold to the model end very high as regular export in Britain though 100 V-spec was a limitation to R34GT-R similarly in addition by 100 limitation.
This is to receive the thing that eminence in Europe goes up by participating as "NISMO GT-R LM" to demand of saying to Nissan Motor Co., "It wants you to sell it also in foreign countries" strong, and 24 hour in Le Mans endurance race. There is a user who says that there are value (scarcity) more than Ferrari and status, too and the satisfaction rating to a practical side is very high though only the high income group was able to buy 10.8 million yen when converting it into Japanese yen in Britain because of the tariff by the thing that does (The price of a new car of V-specII of a domestic specification is 5 million 748 thousand yen) , saying that "The family is taken though it is efficient as the sports running can be done". Britain..car..performance..technology..high..externals..Tokyo..taxi..criticize..this..writing..Britain..joke..rather..face..usual..passenger car..contents..sports car..concept..fresh..assuming that..accept..thing..turn inside out..expression.It was received with good feeling in "Cargai (car favor)" actually. Both of these 200 total are sold all on several days.
Additionally, the individual was exported to Germany, France, Switzerland, the United States, Hong Kong, and UAE, etc. (To our regret, putting the car that had not suited the standard in the United States according to state might become the violation of the law, and there was a case where the trader was restrained to the police authority, and had been imprisoned, too). Moreover, Michael Clm active with SUPER GT also buys two R34GT-R in Japan, and it transports it to Monaco.
A British specification : with a domestic specification though is especially the enormous discrepancy. Type..locale..regulations..according to..correspondence..security..strengthen..headlight..halogen..valve..make..security..standard..suit..change..speed limiter..Europe..self-imposed restraint..value..improve..scale..speed..meter..adoption..country..specification..spec..adopt..genuine leather..seat..equip..Britain..specification..this..skin..hand..work..put..one..Japan..lineup..provide..red..epidermal..selection..tentatively..Japan..limitation..put on the market.
The name of GT-R still was not revealed at this point, and the flow of GT-A and GT-B that had been put up was drawn to a hot model of the second generation skyline, and there was a rumor of becoming GT-C, too, when marketing it founder GT-R in the Hacosca age was though the reference was exhibited to the prototype of the type in the previous year's 1968 Tokyo Motor Show put on the market immediately before marketing.
There are already a lot of owners of GT-R who dislike excessively putting the hand from the perfection as the tuning car high while stocked. Moreover, because the emblem of GT-R is applied to a standard model and a lot of tunings shown like GT-R are done by transplanting the bumper, it is possible to visit the popularity of the style of GT-R. To catch it ..engine power to keep.. firmly, great Modefai is often added to the exterior an of course favorite ..core.. tuning.

existence value 在 一二三渡辺 Youtube 的最佳解答
What is GT-R?
GT-R is a vehicle developed with a main objective of use by the circuit the skyline that continues from the C10 Hacosca type (Macartun). It is a car like the assumption of the win in the race though it is a passenger car base, a mission installing equipment and an engine different from other grades of the skyline, and the influences on other domestic production sports cars, etc.Therefore, there are a lot of madly devoted fans, and the engineer also has a special sticking to. The name of GT-R for that is assumed, and there is a model not permitted either, and it assumes the existence of GT-R to be the one of more special crown. 「There are three famous alphabets worldwide. It is G, T, and R. I will promise here. GT-R revives without fail. 」Carlos Ghosn COO (at that time) made remarks on "GT-R concept" ahead by opening about presentation at the press conference on the Tokyo Motor Show in 2001. I can ask this car for the posture of Nissan that offers it because it entrusted such a content to Gorn where only a thorough rationalization strategy is often taken up. This promise is sold, and the mass production type is announced by the 40th Tokyo Motor Show on October 24, 2007 and Carlos Ghosn will be sold accomplishment Nissan and GT-R on December 6, 2007. Especially, it is known, and not only the country but also the world is 13 in the reputation of R from active distinguished services very much in an overseas race that has infiltrated by calling Godzilla (Godzilla) as for the name though it is from R32 type. Therefore, GT-R of R32, R33, and R34 is introduced to foreign countries though the skyline is almost a car only for the domestic sale by exporter's hand. As for the reputation, R33GT-R was sold to the model end very high as regular export in Britain though 100 V-spec was a limitation to R34GT-R similarly in addition by 100 limitation.
This is to receive the thing that eminence in Europe goes up by participating as "NISMO GT-R LM" to demand of saying to Nissan Motor Co., "It wants you to sell it also in foreign countries" strong, and 24 hour in Le Mans endurance race. There is a user who says that there are value (scarcity) more than Ferrari and status, too and the satisfaction rating to a practical side is very high though only the high income group was able to buy 10.8 million yen when converting it into Japanese yen in Britain because of the tariff by the thing that does (The price of a new car of V-specII of a domestic specification is 5 million 748 thousand yen) , saying that "The family is taken though it is efficient as the sports running can be done". Britain..car..performance..technology..high..externals..Tokyo..taxi..criticize..this..writing..Britain..joke..rather..face..usual..passenger car..contents..sports car..concept..fresh..assuming that..accept..thing..turn inside out..expression.It was received with good feeling in "Cargai (car favor)" actually. Both of these 200 total are sold all on several days.
Additionally, the individual was exported to Germany, France, Switzerland, the United States, Hong Kong, and UAE, etc. (To our regret, putting the car that had not suited the standard in the United States according to state might become the violation of the law, and there was a case where the trader was restrained to the police authority, and had been imprisoned, too). Moreover, Michael Clm active with SUPER GT also buys two R34GT-R in Japan, and it transports it to Monaco.
A British specification : with a domestic specification though is especially the enormous discrepancy. Type..locale..regulations..according to..correspondence..security..strengthen..headlight..halogen..valve..make..security..standard..suit..change..speed limiter..Europe..self-imposed restraint..value..improve..scale..speed..meter..adoption..country..specification..spec..adopt..genuine leather..seat..equip..Britain..specification..this..skin..hand..work..put..one..Japan..lineup..provide..red..epidermal..selection..tentatively..Japan..limitation..put on the market.
The name of GT-R still was not revealed at this point, and the flow of GT-A and GT-B that had been put up was drawn to a hot model of the second generation skyline, and there was a rumor of becoming GT-C, too, when marketing it founder GT-R in the Hacosca age was though the reference was exhibited to the prototype of the type in the previous year's 1968 Tokyo Motor Show put on the market immediately before marketing.
There are already a lot of owners of GT-R who dislike excessively putting the hand from the perfection as the tuning car high while stocked. Moreover, because the emblem of GT-R is applied to a standard model and a lot of tunings shown like GT-R are done by transplanting the bumper, it is possible to visit the popularity of the style of GT-R. To catch it ..engine power to keep.. firmly, great Modefai is often added to the exterior an of course favorite ..core.. tuning.

existence value 在 一二三渡辺 Youtube 的最佳貼文
What is GT-R?
GT-R is a vehicle developed with a main objective of use by the circuit the skyline that continues from the C10 Hacosca type (Macartun). It is a car like the assumption of the win in the race though it is a passenger car base, a mission installing equipment and an engine different from other grades of the skyline, and the influences on other domestic production sports cars, etc.Therefore, there are a lot of madly devoted fans, and the engineer also has a special sticking to. The name of GT-R for that is assumed, and there is a model not permitted either, and it assumes the existence of GT-R to be the one of more special crown. 「There are three famous alphabets worldwide. It is G, T, and R. I will promise here. GT-R revives without fail. 」Carlos Ghosn COO (at that time) made remarks on "GT-R concept" ahead by opening about presentation at the press conference on the Tokyo Motor Show in 2001. I can ask this car for the posture of Nissan that offers it because it entrusted such a content to Gorn where only a thorough rationalization strategy is often taken up. This promise is sold, and the mass production type is announced by the 40th Tokyo Motor Show on October 24, 2007 and Carlos Ghosn will be sold accomplishment Nissan and GT-R on December 6, 2007. Especially, it is known, and not only the country but also the world is 13 in the reputation of R from active distinguished services very much in an overseas race that has infiltrated by calling Godzilla (Godzilla) as for the name though it is from R32 type. Therefore, GT-R of R32, R33, and R34 is introduced to foreign countries though the skyline is almost a car only for the domestic sale by exporter's hand. As for the reputation, R33GT-R was sold to the model end very high as regular export in Britain though 100 V-spec was a limitation to R34GT-R similarly in addition by 100 limitation.
This is to receive the thing that eminence in Europe goes up by participating as "NISMO GT-R LM" to demand of saying to Nissan Motor Co., "It wants you to sell it also in foreign countries" strong, and 24 hour in Le Mans endurance race. There is a user who says that there are value (scarcity) more than Ferrari and status, too and the satisfaction rating to a practical side is very high though only the high income group was able to buy 10.8 million yen when converting it into Japanese yen in Britain because of the tariff by the thing that does (The price of a new car of V-specII of a domestic specification is 5 million 748 thousand yen) , saying that "The family is taken though it is efficient as the sports running can be done". Britain..car..performance..technology..high..externals..Tokyo..taxi..criticize..this..writing..Britain..joke..rather..face..usual..passenger car..contents..sports car..concept..fresh..assuming that..accept..thing..turn inside out..expression.It was received with good feeling in "Cargai (car favor)" actually. Both of these 200 total are sold all on several days.
Additionally, the individual was exported to Germany, France, Switzerland, the United States, Hong Kong, and UAE, etc. (To our regret, putting the car that had not suited the standard in the United States according to state might become the violation of the law, and there was a case where the trader was restrained to the police authority, and had been imprisoned, too). Moreover, Michael Clm active with SUPER GT also buys two R34GT-R in Japan, and it transports it to Monaco.
A British specification : with a domestic specification though is especially the enormous discrepancy. Type..locale..regulations..according to..correspondence..security..strengthen..headlight..halogen..valve..make..security..standard..suit..change..speed limiter..Europe..self-imposed restraint..value..improve..scale..speed..meter..adoption..country..specification..spec..adopt..genuine leather..seat..equip..Britain..specification..this..skin..hand..work..put..one..Japan..lineup..provide..red..epidermal..selection..tentatively..Japan..limitation..put on the market.
The name of GT-R still was not revealed at this point, and the flow of GT-A and GT-B that had been put up was drawn to a hot model of the second generation skyline, and there was a rumor of becoming GT-C, too, when marketing it founder GT-R in the Hacosca age was though the reference was exhibited to the prototype of the type in the previous year's 1968 Tokyo Motor Show put on the market immediately before marketing.
There are already a lot of owners of GT-R who dislike excessively putting the hand from the perfection as the tuning car high while stocked. Moreover, because the emblem of GT-R is applied to a standard model and a lot of tunings shown like GT-R are done by transplanting the bumper, it is possible to visit the popularity of the style of GT-R. To catch it ..engine power to keep.. firmly, great Modefai is often added to the exterior an of course favorite ..core.. tuning.
