-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcrypto_test.go
104 lines (91 loc) · 2.34 KB
/
crypto_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
package main
import (
"crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"fmt"
"runtime"
"sync"
"testing"
"time"
"github.com/stretchr/testify/require"
)
var msg = "HelloWorld"
var THREAD = runtime.NumCPU()
var COUNT = 100000
type DataAndSign struct {
Data []byte
Sign []byte
}
func TestP256(t *testing.T) {
priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
require.Nil(t, err)
start := time.Now()
wg := sync.WaitGroup{}
wg.Add(THREAD)
txBatch := []*DataAndSign{}
lock := sync.Mutex{}
for cpu := 0; cpu < THREAD; cpu++ {
go func(c int) {
defer wg.Done()
for i := 0; i < COUNT; i++ {
hash := sha256.Sum256(Uint32ToBytes(uint32(c*COUNT + i)))
sign, _ := ecdsa.SignASN1(rand.Reader, priv, hash[:])
lock.Lock()
txBatch = append(txBatch, &DataAndSign{Data: Uint32ToBytes(uint32(c*COUNT + i)), Sign: sign})
lock.Unlock()
}
}(cpu)
}
wg.Wait()
t.Logf("ECDSA total generate and sign tx count=%d, cost:%v, TPS:%v", COUNT*THREAD, time.Since(start),
float64(COUNT*THREAD)/time.Since(start).Seconds())
pub := &priv.PublicKey
wg = sync.WaitGroup{}
start = time.Now()
wg.Add(THREAD)
for cpu := 0; cpu < THREAD; cpu++ {
go func(c int) {
defer wg.Done()
for i := 0; i < COUNT; i++ {
data := txBatch[c*COUNT+i]
hash := sha256.Sum256([]byte(data.Data))
b := ecdsa.VerifyASN1(pub, hash[:], data.Sign)
if !b {
t.Fatal(fmt.Sprintf("index=%d, data:%x,sign:%x", c*COUNT+i, data.Data, data.Sign))
}
}
}(cpu)
}
wg.Wait()
t.Logf("ECDSA total verify tx count=%d, cost:%v, TPS:%v", COUNT*THREAD, time.Since(start),
float64(COUNT*THREAD)/time.Since(start).Seconds())
}
func TestEd25519(t *testing.T) {
message := []byte(msg)
pub, priv, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
t.Fatal(err)
}
signature := ed25519.Sign(priv, message)
start := time.Now()
wg := sync.WaitGroup{}
wg.Add(THREAD)
for cpu := 0; cpu < THREAD; cpu++ {
go func(c int) {
defer wg.Done()
for i := 0; i < COUNT; i++ {
b := ed25519.Verify(pub, message, signature)
if !b {
t.Fatal("Verify fail")
}
}
//t.Logf("CPU[%d] verify count=%d, cost:%v", c, COUNT, time.Since(start))
}(cpu)
}
wg.Wait()
t.Logf("ED25519 total verify count=%d, cost:%v, TPS:%v", COUNT*THREAD, time.Since(start),
float64(COUNT*THREAD)/time.Since(start).Seconds())
}