Slide 1

Slide 1 text

diving into @ordepdev Merkle Trees

Slide 2

Slide 2 text

@ordepdev @ordepdev

Slide 3

Slide 3 text

@ordepdev

Slide 4

Slide 4 text

@ordepdev

Slide 5

Slide 5 text

https://people.eecs.berkeley.edu/~raluca/cs261-f15/readings/merkle.pdf 1987

Slide 6

Slide 6 text

“[…] can sign an unlimited number of messages, and the signature size increases logarithmically.”

Slide 7

Slide 7 text

“The general idea in this new system is to use an infinite tree of one-time signatures.”

Slide 8

Slide 8 text

https://people.eecs.berkeley.edu/~raluca/cs261-f15/readings/merkle.pdf https://patents.google.com/patent/US4309569 http://www.merkle.com/papers/Thesis1979.pdf 1979

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

1989 http://merkle.com/papers/Certified1979.pdf

Slide 11

Slide 11 text

“[…] is a data structure used for efficiently summarizing and verifying the integrity of large sets of data.”

Slide 12

Slide 12 text

“[…] it was used for the purpose of one-time signatures and authenticated public key distribution.”

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

Summarize and Verify

Slide 15

Slide 15 text

One-way Functions

Slide 16

Slide 16 text

“A one-way function f is a function that is easy to compute but difficult to invert.”

Slide 17

Slide 17 text

() () () 4a23ca438f3 065b90acc7 ccca99192c

Slide 18

Slide 18 text

Why do we need them?

Slide 19

Slide 19 text

“[…] being able to store and identify data with a fixed length output can create vast storage savings.”

Slide 20

Slide 20 text

“The person who computes y=F(x) is the only person who knows x.”

Slide 21

Slide 21 text

One-time Signatures

Slide 22

Slide 22 text

“One time signatures are practical between a single pair of users who are willing to exchange the large amount of data necessary […]”

Slide 23

Slide 23 text

“If 1000 messages are to be signed before new public authentication data is needed, over 20,000,000 bits or 2.5 megabytes must be stored as public information.”

Slide 24

Slide 24 text

Improving one- time signatures

Slide 25

Slide 25 text

https://people.eecs.berkeley.edu/~raluca/cs261-f15/readings/merkle.pdf http://www.merkle.com/papers/Thesis1979.pdf

Slide 26

Slide 26 text

How to compute a Merkle Root?

Slide 27

Slide 27 text

9ec4 L1 L2

Slide 28

Slide 28 text

h(L1)

Slide 29

Slide 29 text

9ec4 L1 L2

Slide 30

Slide 30 text

h(L2)

Slide 31

Slide 31 text

a871 L1 L2 7e6a 9ec4

Slide 32

Slide 32 text

h(h(L1)||h(L2))

Slide 33

Slide 33 text

aea9 a871 7e6a L1 L2 9ec4

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

aea9 a871 7e6a L1 L2 9ec4 MerkleTree.Leaf

Slide 36

Slide 36 text

defmodule MerkleTree.Leaf do defstruct [:hash, :value] @type hash :: String.t @type value :: String.t @type t :: %MerkleTree.Leaf{ hash: hash, value: value } end

Slide 37

Slide 37 text

defmodule MerkleTree.Leaf do defstruct [:hash, :value] @type hash :: String.t @type value :: String.t @type t :: %MerkleTree.Leaf{ hash: hash, value: value } end

Slide 38

Slide 38 text

aea9 a871 7e6a L1 L2 9ec4 MerkleTree.Node

Slide 39

Slide 39 text

defmodule MerkleTree.Node do defstruct [:hash, :left, :right] @type hash :: String.t @type left :: MerkleTree.Node.t | MerkleTree.Leaf.t @type right :: MerkleTree.Node.t | MerkleTree.Leaf.t @type t :: %MerkleTree.Node{ hash: hash, left: left, right: right } end

Slide 40

Slide 40 text

defmodule MerkleTree.Node do defstruct [:hash, :left, :right] @type hash :: String.t @type left :: MerkleTree.Node.t | MerkleTree.Leaf.t @type right :: MerkleTree.Node.t | MerkleTree.Leaf.t @type t :: %MerkleTree.Node{ hash: hash, left: left, right: right } end

Slide 41

Slide 41 text

aea9 a871 7e6a L1 L2 9ec4 MerkleTree.Leaf MerkleTree.Node MerkleTree[:root]

Slide 42

Slide 42 text

defmodule MerkleTree do defstruct [:root] @type root :: MerkleTree.Node.t @type t :: %MerkleTree{ root: root } end

Slide 43

Slide 43 text

defmodule MerkleTree do defstruct [:root] @type root :: MerkleTree.Node.t @type t :: %MerkleTree{ root: root } end

Slide 44

Slide 44 text

Hashing the data blocks

Slide 45

Slide 45 text

defmodule MerkleTree.Crypto do def hash(input, type \\ :sha256) do type |> :crypto.hash("#{input}") |> Base.encode16 end end

Slide 46

Slide 46 text

defmodule MerkleTree do alias MerkleTree.Leaf alias MerkleTree.Crypto def new(blocks) do blocks |> Enum.map(&%Leaf.build(&1, Crypto.hash(&1))) end end

Slide 47

Slide 47 text

defmodule MerkleTree do alias MerkleTree.Leaf alias MerkleTree.Crypto def new(blocks) do blocks |> Enum.map(&%Leaf.build(&1, Crypto.hash(&1))) end end

Slide 48

Slide 48 text

defmodule MerkleTree do alias MerkleTree.Leaf alias MerkleTree.Crypto def new(blocks) do blocks |> Enum.map(&%Leaf.build(&1, Crypto.hash(&1))) end end

Slide 49

Slide 49 text

defmodule MerkleTree.Leaf do def build(value, hash) do %Leaf{value: value, hash: hash} end end

Slide 50

Slide 50 text

[“L1”, “L2”] |> MerkleTree.new

Slide 51

Slide 51 text

[ %MerkleTree.Leaf{ value: “L1”, hash: “6B86B273FF34FCE19D6B804EFF5A3F5747ADA4EA...” }, %MerkleTree.Leaf{ value: “L2”, hash: “D4735E3A265E16EEE03F59718B9B5D03019C07D8...” } ]

Slide 52

Slide 52 text

[ %MerkleTree.Leaf{ value: “L1”, hash: “6B86B273FF34FCE19D6B804EFF5A3F5747ADA4EA...” }, %MerkleTree.Leaf{ value: “L2”, hash: “D4735E3A265E16EEE03F59718B9B5D03019C07D8...” } ]

Slide 53

Slide 53 text

[ %MerkleTree.Leaf{ value: “L1”, hash: “6B86B273FF34FCE19D6B804EFF5A3F5747ADA4EA...” }, %MerkleTree.Leaf{ value: “L2”, hash: “D4735E3A265E16EEE03F59718B9B5D03019C07D8...” } ]

Slide 54

Slide 54 text

Hashing the nodes

Slide 55

Slide 55 text

defmodule MerkleTree.Node do alias MerkleTree.Crypto def new(nodes) do nodes |> Enum.map_join(&(&1.hash)) |> Crypto.hash |> build(nodes) end end

Slide 56

Slide 56 text

defmodule MerkleTree.Node do alias MerkleTree.Crypto def new(nodes) do nodes |> Enum.map_join(&(&1.hash)) |> Crypto.hash |> build(nodes) end end

Slide 57

Slide 57 text

defmodule MerkleTree.Node do alias MerkleTree.Crypto def new(nodes) do nodes |> Enum.map_join(&(&1.hash)) |> Crypto.hash |> build(nodes) end end

Slide 58

Slide 58 text

defmodule MerkleTree.Node do alias MerkleTree.Crypto def new(nodes) do nodes |> Enum.map_join(&(&1.hash)) |> Crypto.hash |> build(nodes) end end

Slide 59

Slide 59 text

defmodule MerkleTree.Node do alias MerkleTree.Crypto def new(nodes) do nodes |> Enum.map_join(&(&1.hash)) |> Crypto.hash |> build(nodes) end end

Slide 60

Slide 60 text

defmodule MerkleTree.Node do def build(hash, [left, right]) do %Node{hash: hash, left: left, right: right} end end

Slide 61

Slide 61 text

MerkleTree.Node.new([ %MerkleTree.Leaf{ value: “L1”, hash: “6B86B273FF34FCE19D6B804EFF5A3F5747ADA4EA...” }, %MerkleTree.Leaf{ value: “L2”, hash: “D4735E3A265E16EEE03F59718B9B5D03019C07D8...” } ])

Slide 62

Slide 62 text

%MerkleTree.Node{ hash: “F5FC2A0E6D4568040ED7B8D2C59A16B73B8C9EC9...”, left: %MerkleTree.Leaf{ value: “L1”, hash: “6B86B273FF34FCE19D6B804EFF5A3F5747ADA4EA...” }, right: %MerkleTree.Leaf{ value: “L2”, hash: “D4735E3A265E16EEE03F59718B9B5D03019C07D8...” } }

Slide 63

Slide 63 text

%MerkleTree.Node{ hash: “F5FC2A0E6D4568040ED7B8D2C59A16B73B8C9EC9...”, left: %MerkleTree.Leaf{ value: “L1”, hash: “6B86B273FF34FCE19D6B804EFF5A3F5747ADA4EA...” }, right: %MerkleTree.Leaf{ value: “L2”, hash: “D4735E3A265E16EEE03F59718B9B5D03019C07D8...” } }

Slide 64

Slide 64 text

%MerkleTree.Node{ hash: “F5FC2A0E6D4568040ED7B8D2C59A16B73B8C9EC9...”, left: %MerkleTree.Leaf{ value: “L1”, hash: “6B86B273FF34FCE19D6B804EFF5A3F5747ADA4EA...” }, right: %MerkleTree.Leaf{ value: “L2”, hash: “D4735E3A265E16EEE03F59718B9B5D03019C07D8...” } }

Slide 65

Slide 65 text

%MerkleTree.Node{ hash: “F5FC2A0E6D4568040ED7B8D2C59A16B73B8C9EC9...”, left: %MerkleTree.Leaf{ value: “L1”, hash: “6B86B273FF34FCE19D6B804EFF5A3F5747ADA4EA...” }, right: %MerkleTree.Leaf{ value: “L2”, hash: “D4735E3A265E16EEE03F59718B9B5D03019C07D8...” } }

Slide 66

Slide 66 text

Building a bigger tree

Slide 67

Slide 67 text

9ec4 L1 L2 L3 L4

Slide 68

Slide 68 text

9ec4 L1 L2 L3 L4

Slide 69

Slide 69 text

9ec4 L1 L2 L3 L4 7e6a

Slide 70

Slide 70 text

9ec4 L1 L2 L3 L4 7e6a 842c

Slide 71

Slide 71 text

9ec4 L1 L2 L3 L4 7e6a 842c 4aa0

Slide 72

Slide 72 text

9ec4 842c 4aa0 L1 L2 L3 L4 7e6a aea9

Slide 73

Slide 73 text

9ec4 4aa0 L1 L2 L3 L4 7e6a aea9 842c d886

Slide 74

Slide 74 text

d886 9ec4 4aa0 L1 L2 L3 L4 7e6a aea9 842c 4b25

Slide 75

Slide 75 text

All the way up

Slide 76

Slide 76 text

defmodule MerkleTree do alias MerkleTree.Leaf alias MerkleTree.Crypto def new(blocks) do blocks |> Enum.map(&%Leaf.build(&1, Crypto.hash(&1))) end end

Slide 77

Slide 77 text

defmodule MerkleTree do defp build([root]) do %MerkleTree{root: root} end defp build(nodes) do nodes |> Enum.chunk_every(2) |> Enum.map(&Node.new(&1)) |> build end end

Slide 78

Slide 78 text

defmodule MerkleTree do defp build([root]) do %MerkleTree{root: root} end defp build(nodes) do nodes |> Enum.chunk_every(2) |> Enum.map(&Node.new(&1)) |> build end end

Slide 79

Slide 79 text

defmodule MerkleTree do defp build([root]) do %MerkleTree{root: root} end defp build(nodes) do nodes |> Enum.chunk_every(2) |> Enum.map(&Node.new(&1)) |> build end end

Slide 80

Slide 80 text

[“L1”, “L2”,“L3”, “L4”] |> Enum.chunk_every(2)

Slide 81

Slide 81 text

[[“L1”, “L2”],[“L3”, “L4”]]

Slide 82

Slide 82 text

defmodule MerkleTree do defp build([root]) do %MerkleTree{root: root} end defp build(nodes) do nodes |> Enum.chunk_every(2) |> Enum.map(&Node.new(&1)) |> build end end

Slide 83

Slide 83 text

defmodule MerkleTree do defp build([root]) do %MerkleTree{root: root} end defp build(nodes) do nodes |> Enum.chunk_every(2) |> Enum.map(&Node.new(&1)) |> build end end

Slide 84

Slide 84 text

defmodule MerkleTree do defp build([root]) do %MerkleTree{root: root} end defp build(nodes) do nodes |> Enum.chunk_every(2) |> Enum.map(&Node.new(&1)) |> build end end

Slide 85

Slide 85 text

defmodule MerkleTree do defp build([root]) do %MerkleTree{root: root} end defp build(nodes) do nodes |> Enum.chunk_every(2) |> Enum.map(&Node.new(&1)) |> build end end

Slide 86

Slide 86 text

defmodule MerkleTree do defp build([root]) do %MerkleTree{root: root} end defp build(nodes) do nodes |> Enum.chunk_every(2) |> Enum.map(&Node.new(&1)) |> build end end

Slide 87

Slide 87 text

defmodule MerkleTree do alias MerkleTree.Leaf alias MerkleTree.Crypto def new(blocks) do blocks |> Enum.map(&%Leaf.build(&1, Crypto.hash(&1))) |> build end end

Slide 88

Slide 88 text

defmodule MerkleTree do alias MerkleTree.Leaf alias MerkleTree.Crypto def new(blocks) do blocks |> Enum.map(&%Leaf.build(&1, Crypto.hash(&1))) |> build end end

Slide 89

Slide 89 text

defmodule MerkleTree do alias MerkleTree.Leaf alias MerkleTree.Crypto def new(blocks) do blocks |> Enum.map(&%Leaf.build(&1, Crypto.hash(&1))) |> build end end

Slide 90

Slide 90 text

defmodule MerkleTree do alias MerkleTree.Leaf alias MerkleTree.Crypto def new(blocks) do blocks |> Enum.map(&%Leaf.build(&1, Crypto.hash(&1))) |> build end end

Slide 91

Slide 91 text

[“L1”, “L2”] |> MerkleTree.new

Slide 92

Slide 92 text

%MerkleTree{ root: %MerkleTree.Node{ hash: “8C569660D98A20D59DE10E134D81A8CE55...”, left: %MerkleTree.Leaf{ value: “L1”, hash: “DFFE8596427FC50E8F64654A609AF135...” }, right: %MerkleTree.Leaf{ value: “L2”, hash: “D76354D8457898445BB69E0DC0DC95FB...” } } }

Slide 93

Slide 93 text

%MerkleTree{ root: %MerkleTree.Node{ hash: “8C569660D98A20D59DE10E134D81A8CE55...”, left: %MerkleTree.Leaf{ value: “L1”, hash: “DFFE8596427FC50E8F64654A609AF135...” }, right: %MerkleTree.Leaf{ value: “L2”, hash: “D76354D8457898445BB69E0DC0DC95FB...” } } }

Slide 94

Slide 94 text

%MerkleTree{ root: %MerkleTree.Node{ hash: “8C569660D98A20D59DE10E134D81A8CE55...”, left: %MerkleTree.Leaf{ value: “L1”, hash: “DFFE8596427FC50E8F64654A609AF135...” }, right: %MerkleTree.Leaf{ value: “L2”, hash: “D76354D8457898445BB69E0DC0DC95FB...” } } }

Slide 95

Slide 95 text

%MerkleTree{ root: %MerkleTree.Node{ hash: “8C569660D98A20D59DE10E134D81A8CE55...”, left: %MerkleTree.Leaf{ value: “L1”, hash: “DFFE8596427FC50E8F64654A609AF135...” }, right: %MerkleTree.Leaf{ value: “L2”, hash: “D76354D8457898445BB69E0DC0DC95FB...” } } }

Slide 96

Slide 96 text

%MerkleTree{ root: %MerkleTree.Node{ hash: “8C569660D98A20D59DE10E134D81A8CE55...”, left: %MerkleTree.Leaf{ value: “L1”, hash: “DFFE8596427FC50E8F64654A609AF135...” }, right: %MerkleTree.Leaf{ value: “L2”, hash: “D76354D8457898445BB69E0DC0DC95FB...” } } }

Slide 97

Slide 97 text

Audit Proof

Slide 98

Slide 98 text

P2 9ec4 H3 L1 L2 L3 L4 H2 P1 H3 R

Slide 99

Slide 99 text

P2 9ec4 h(L4) L1 L2 L3 L4 h(L2) P1 h(L3) R H3 H2 H3

Slide 100

Slide 100 text

P2 9ec4 h(L4) L1 L2 L3 L4 H2 P1 h(L3) R H3 H3

Slide 101

Slide 101 text

P2 9ec4 h(L4) L1 L2 L3 L4 h(L2) P1 h(L3) R H2 H3 H3

Slide 102

Slide 102 text

P2 9ec4 h(L4) L1 L2 L3 L4 h(L2) P1 h(L3) R H2 H3 H3

Slide 103

Slide 103 text

P2 9ec4 h(L4) L1 L2 L3 L4 h(L2) P1 h(L3) R H2 H3 H3

Slide 104

Slide 104 text

How they are useful?

Slide 105

Slide 105 text

Detect Inconsistencies

Slide 106

Slide 106 text

L1 L4 Replica 1 L1 L3 L1 L4 L1 L2 L1 L4 L1 L2 Replica 2 Replica 3 Repair Coordinator

Slide 107

Slide 107 text

L1 L4 Replica 1 H1 H3 L1 L3 L1 L4 H1 H2 L1 L2 L1 L4 H1 H2 L1 L2 Replica 2 Replica 3 Repair Coordinator

Slide 108

Slide 108 text

L1 L4 Replica 1 B H1 H3 L1 L3 L1 L4 A H1 H2 L1 L2 L1 L4 A H1 H2 L1 L2 Replica 2 Replica 3 Repair Coordinator

Slide 109

Slide 109 text

L1 L4 Replica 1 B H1 H3 L1 L3 L1 L4 A H1 H2 L1 L2 L1 L4 A H1 H2 L1 L2 Replica 2 Replica 3 Repair Coordinator

Slide 110

Slide 110 text

B H1 H2 H1 H3 L1 L2 L1 L3 A Replica 2 Replica 3

Slide 111

Slide 111 text

B H1 H2 H1 H3 L1 L2 L1 L3 A Replica 2 Replica 3

Slide 112

Slide 112 text

B H1 H2 H1 H3 L1 L2 L1 L3 A Replica 2 Replica 3

Slide 113

Slide 113 text

B H1 H2 H1 H3 L1 L2 L1 L2 A Replica 2 Replica 3

Slide 114

Slide 114 text

B H1 H2 H1 H2 L1 L2 L1 L2 A Replica 2 Replica 3

Slide 115

Slide 115 text

A H1 H2 H1 H2 L1 L2 L1 L2 A Replica 2 Replica 3

Slide 116

Slide 116 text

Dynamo, Riak, and Cassandra use this to repair bad replicas!

Slide 117

Slide 117 text

Peer-to-peer file sharing

Slide 118

Slide 118 text

9cee

Slide 119

Slide 119 text

9cee L3

Slide 120

Slide 120 text

9cee b2d0 L3

Slide 121

Slide 121 text

9cee b2d0 L3 8f14 b2d0

Slide 122

Slide 122 text

9cee b2d0 L3 8f14 165f b2d0

Slide 123

Slide 123 text

165f 8f14 L3 e831 b2d0 9cee b2d0

Slide 124

Slide 124 text

165f 8f14 L3 e831 b2d0 9cee ✅ ✅ b2d0

Slide 125

Slide 125 text

Copy-on-write

Slide 126

Slide 126 text

Copy-on-write data structures are also called persistent data structures.

Slide 127

Slide 127 text

165f a871 L1 L2 L3 e4da e831 b2d0 9cee L4 8f14

Slide 128

Slide 128 text

165f a871 L1 L2 L3 e4da e831 b2d0 9cee L4 8f14 L4

Slide 129

Slide 129 text

165f a871 L1 L2 L3 e4da e831 b2d0 9cee L4 8f14 L4 5e42

Slide 130

Slide 130 text

165f a871 L1 L2 L3 e4da e831 b2d0 9cee L4 8f14 L4 5e42 3d0b

Slide 131

Slide 131 text

a871 L1 L2 L3 L4 e4da e831 b2d0 L4 4a88 3d0b 5e42

Slide 132

Slide 132 text

Instead of taking a full copy, we can share the same tree between both the copy and the original tree.

Slide 133

Slide 133 text

165f a871 L1 L2 L3 e4da e831 b2d0 9cee L4 5e42 3d0b 4a88 8f14 L4

Slide 134

Slide 134 text

165f a871 L1 L2 L3 e4da e831 b2d0 9cee L4 8f14 Version 1

Slide 135

Slide 135 text

a871 L1 L2 L3 L4 e4da e831 b2d0 L4 4a88 3d0b 5e42 Version 2

Slide 136

Slide 136 text

Wrapping-up

Slide 137

Slide 137 text

Merkle trees are binary trees containing an infinite number of cryptographic hashes.

Slide 138

Slide 138 text

Leaves contains hashes of data blocks.

Slide 139

Slide 139 text

Nodes contains hashes of their children.

Slide 140

Slide 140 text

Produce a root hash that summarizes an entire data set and it’s publicly distributed.

Slide 141

Slide 141 text

Easily prove that a given data block exists in the tree.

Slide 142

Slide 142 text

You can find them in…

Slide 143

Slide 143 text

And lots of papers…

Slide 144

Slide 144 text

@ordepdev Thank you.

Slide 145

Slide 145 text

diving into @ordepdev Merkle Trees