-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
62 lines (48 loc) · 1.04 KB
/
main.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
package main
import (
"crypto/sha256"
"imt/db"
"imt/imt"
"math/big"
"os"
"github.com/cockroachdb/pebble"
)
func main() {
tmp, _ := os.MkdirTemp("", "*")
defer os.RemoveAll(tmp)
pebbleDb, err := pebble.Open(tmp, &pebble.Options{})
if err != nil {
panic(err)
}
defer pebbleDb.Close()
db := db.NewPebbleDb(pebbleDb)
defer db.Close()
t := imt.NewTree(db, 32, 4, hash)
setAndValidate(t, big.NewInt(1), big.NewInt(5))
setAndValidate(t, big.NewInt(5), big.NewInt(5))
setAndValidate(t, big.NewInt(3), big.NewInt(5))
setAndValidate(t, big.NewInt(4), big.NewInt(5))
setAndValidate(t, big.NewInt(10), big.NewInt(5))
}
func setAndValidate(t *imt.Tree, key, value *big.Int) {
proof, err := t.Set(key, value)
if err != nil {
panic(err)
}
valid, err := proof.IsValid(t)
if err != nil {
panic(err)
}
if !valid {
panic("Validation failed")
}
}
func hash(v []*big.Int) (*big.Int, error) {
h := sha256.New()
for i := 0; i < len(v); i++ {
bn := v[i]
h.Write(bn.Bytes())
}
s := h.Sum(nil)
return new(big.Int).SetBytes(s), nil
}