-
Notifications
You must be signed in to change notification settings - Fork 14
/
test_tree.go
135 lines (117 loc) · 3 KB
/
test_tree.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"github.com/iden3/go-iden3-crypto/poseidon"
"math/big"
)
type PoseidonNode interface {
depth() int
value() big.Int
withValue(index int, val big.Int) PoseidonNode
writeProof(index int, out []big.Int)
}
func indexIsLeft(index int, depth int) bool {
return index&(1<<(depth-1)) == 0
}
func (node *PoseidonFullNode) depth() int {
return node.dep
}
func (node *PoseidonEmptyNode) depth() int {
return node.dep
}
func (node *PoseidonFullNode) value() big.Int {
return node.val
}
func (node *PoseidonEmptyNode) value() big.Int {
return node.emptyTreeValues[node.depth()]
}
func (node *PoseidonFullNode) withValue(index int, val big.Int) PoseidonNode {
result := PoseidonFullNode{
dep: node.depth(),
left: node.left,
right: node.right,
}
if node.depth() == 0 {
result.val = val
} else {
if indexIsLeft(index, node.depth()) {
result.left = node.left.withValue(index, val)
} else {
result.right = node.right.withValue(index, val)
}
result.initHash()
}
return &result
}
func (node *PoseidonEmptyNode) withValue(index int, val big.Int) PoseidonNode {
result := PoseidonFullNode{
dep: node.depth(),
}
if node.depth() == 0 {
result.val = val
} else {
emptyChild := PoseidonEmptyNode{dep: node.depth() - 1, emptyTreeValues: node.emptyTreeValues}
initializedChild := emptyChild.withValue(index, val)
if indexIsLeft(index, node.depth()) {
result.left = initializedChild
result.right = &emptyChild
} else {
result.left = &emptyChild
result.right = initializedChild
}
result.initHash()
}
return &result
}
func (node *PoseidonFullNode) writeProof(index int, out []big.Int) {
if node.depth() == 0 {
return
}
if indexIsLeft(index, node.depth()) {
out[node.depth()-1] = node.right.value()
node.left.writeProof(index, out)
} else {
out[node.depth()-1] = node.left.value()
node.right.writeProof(index, out)
}
}
func (node *PoseidonEmptyNode) writeProof(index int, out []big.Int) {
for i := 0; i < node.depth(); i++ {
out[i] = node.emptyTreeValues[i]
}
}
type PoseidonFullNode struct {
dep int
val big.Int
left PoseidonNode
right PoseidonNode
}
func (node *PoseidonFullNode) initHash() {
leftVal := node.left.value()
rightVal := node.right.value()
newVal, _ := poseidon.Hash([]*big.Int{&leftVal, &rightVal})
node.val = *newVal
}
type PoseidonEmptyNode struct {
dep int
emptyTreeValues []big.Int
}
type PoseidonTree struct {
root PoseidonNode
}
func (tree *PoseidonTree) Root() big.Int {
return tree.root.value()
}
func (tree *PoseidonTree) Update(index int, value big.Int) []big.Int {
tree.root = tree.root.withValue(index, value)
proof := make([]big.Int, tree.root.depth())
tree.root.writeProof(index, proof)
return proof
}
func NewTree(depth int) PoseidonTree {
initHashes := make([]big.Int, depth+1)
for i := 1; i <= depth; i++ {
val, _ := poseidon.Hash([]*big.Int{&initHashes[i-1], &initHashes[i-1]})
initHashes[i] = *val
}
return PoseidonTree{root: &PoseidonEmptyNode{dep: depth, emptyTreeValues: initHashes}}
}