-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerkle_test.go
83 lines (67 loc) · 1.41 KB
/
merkle_test.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
package merkle_signing
import (
"crypto/rand"
"crypto/sha256"
"github.com/oasisprotocol/ed25519"
"strconv"
"testing"
)
type UTXO struct {
x string
}
//CalculateHash hashes the fields of an UTXO
func (t UTXO) CalculateHash() ([]byte, error) {
h := sha256.New()
if _, err := h.Write([]byte(t.x)); err != nil {
return nil, err
}
return h.Sum(nil), nil
}
func TestInterface(t *testing.T) {
var hashes [][]byte
for i := 1; i <= 5; i++ {
hash, err := UTXO{x: strconv.Itoa(i)}.CalculateHash()
if err != nil {
panic(err)
}
hashes = append(hashes, hash)
}
pub, priv, _ := ed25519.GenerateKey(rand.Reader)
sigs := SignMerkle(&priv, pub, hashes)
for i, sig := range sigs {
valid, err := VerifyMerkle(hashes[i], sig)
if err != nil {
panic(err)
}
if !valid {
t.Fatal("verification was not valid")
}
hashes[i][0] = byte(1)
invalid, err := VerifyMerkle(hashes[i], sig)
if err != nil {
panic(err)
}
if invalid {
t.Fatal("wanted invalid verification, but got valid")
}
}
}
func TestSingle(t *testing.T) {
hash, err := UTXO{x: "1"}.CalculateHash()
if err != nil {
panic(err)
}
items := [][]byte{hash}
pub, priv, _ := ed25519.GenerateKey(rand.Reader)
sigs := SignMerkle(&priv, pub, items)
if len(sigs) != 1 {
t.Fatal("wrong length")
}
valid, err := VerifyMerkle(hash, sigs[0])
if err != nil {
panic(err)
}
if !valid {
t.Fatal("verification was not valid")
}
}