-
Notifications
You must be signed in to change notification settings - Fork 1
/
lms_test.go
125 lines (105 loc) · 2.64 KB
/
lms_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
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
package lms
import (
"fmt"
"testing"
"time"
"github.com/LoCCS/lmots"
"github.com/LoCCS/lmots/rand"
)
const (
H = 16 // the height of the merkle tree
)
func TestLMSApp(t *testing.T) {
seed := make([]byte, lmots.N)
rand.Reader.Read(seed)
agentStart := time.Now()
merkleAgent, err := NewMerkleAgent(H, seed)
agentTime := time.Since(agentStart)
if err != nil {
fmt.Println(err)
}
fmt.Printf("Time on new merkle Agent with height %v : %v\n", H, agentTime)
var signSum time.Duration
var verifySum time.Duration
var maxsig time.Duration
var maxver time.Duration
success := 0
failure := 0
for i := 0; i < 1<<H+1; i++ {
if i%1837 == 0 {
fmt.Printf("Success %v, failure %v\n", success, failure)
mBytes, _ := merkleAgent.Serialize()
sBytes := merkleAgent.SerializeSecretKey()
merkleAgent.Rebuild(mBytes, sBytes)
}
message := make([]byte, lmots.N)
rand.Reader.Read(message)
signStart := time.Now()
_, sigraw, err := Sign(merkleAgent, message)
signTime := time.Since(signStart)
if err != nil {
if err == ErrOutOfKeys {
if (1 << H) != i {
t.Fatalf("invalid i: want %v, got %v", (1 << H), i)
}
fmt.Printf("%s at i = %v", err, i)
break
} else {
t.Fatal(err)
}
}
sigData, err := sigraw.Serialize()
if nil != err {
t.Fatal(err)
}
sig := new(MerkleSig)
if err := sig.Deserialize(sigData); nil != err {
t.Fatal(err)
}
signSum += signTime
if signTime > maxsig {
maxsig = signTime
fmt.Printf("Currently max sig time: %v\n", maxsig)
}
verifyStart := time.Now()
result := Verify(merkleAgent.Root, message, sig)
verifyTime := time.Since(verifyStart)
verifySum += verifyTime
if verifyTime > maxver {
maxver = verifyTime
fmt.Printf("Currently max verify time: %v\n", maxver)
}
if result {
success++
} else {
failure++
return
}
}
fmt.Println()
fmt.Printf("Success %v, failure %v\n", success, failure)
fmt.Printf("Merkle agent building time %v : %v\n", H, agentTime)
fmt.Printf("Average signature time: %v\n", signSum/(1<<H))
fmt.Printf("Max signature time: %v\n", maxsig)
fmt.Printf("Average verification time: %v\n", verifySum/(1<<H))
fmt.Printf("Max verify time: %v\n", maxver)
}
func TestLMSStdOps(t *testing.T) {
const H = 3
seed := make([]byte, lmots.N)
rand.Reader.Read(seed)
merkleAgent, err := NewMerkleAgent(H, seed)
if nil != err {
t.Fatal("unexpected error in setting up")
}
// make a random message
msg := make([]byte, lmots.N)
rand.Reader.Read(msg)
_, sig, err := Sign(merkleAgent, msg)
if nil != err {
t.Fatalf("error in signing %x", msg)
}
if !Verify(merkleAgent.Root, msg, sig) {
t.Fatal("verification failed")
}
}