/
QuickSigner.go
135 lines (111 loc) · 2.68 KB
/
QuickSigner.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 types
import (
"errors"
"github.com/shx-project/sphinx/common"
"github.com/shx-project/sphinx/common/crypto"
"github.com/shx-project/sphinx/common/log"
"runtime"
)
var (
ErrSignCheckFailed = errors.New("recover pubkey failed")
)
// result for recover pubkey
type RecoverPubkey struct {
Tx *Transaction // recover tx's hash
Hash []byte
Sig []byte // signature
}
type TaskTh struct {
isFull bool
queue chan *RecoverPubkey
}
type QSHandle struct {
bInit bool
maxThNum int // thread number for soft ecc recover.
thPool []*TaskTh // task pool
idx int
}
var (
qsHandle = &QSHandle{bInit: false, maxThNum: 2}
)
func QSGetInstance() *QSHandle {
return qsHandle
}
func (qs *QSHandle) postToSoft(rs *RecoverPubkey) bool {
for i := 0; i < qs.maxThNum; i++ {
idx := (qs.idx + i) % qs.maxThNum
select {
case qs.thPool[idx].queue <- rs:
qs.idx++
return true
default:
log.Debug("qs", "thPool ", idx, "is full", len(qs.thPool[idx].queue))
}
}
return false
}
// receive task and execute soft ecc-recover
func (qs *QSHandle) asyncSoftRecoverPubTask(queue chan *RecoverPubkey) {
for {
select {
case rs, ok := <-queue:
if !ok {
return
}
pub, _ := crypto.Ecrecover(rs.Hash, rs.Sig)
var addr = common.Address{}
copy(addr[:], crypto.Keccak256(pub[1:])[12:])
}
}
}
func (qs *QSHandle) Init() error {
if qs.bInit {
return nil
}
// use 1/4 cpu
if runtime.NumCPU() > qs.maxThNum {
qs.maxThNum = runtime.NumCPU() - 1
}
qs.thPool = make([]*TaskTh, qs.maxThNum)
for i := 0; i < qs.maxThNum; i++ {
qs.thPool[i] = &TaskTh{isFull: false, queue: make(chan *RecoverPubkey, 1000000)}
go qs.asyncSoftRecoverPubTask(qs.thPool[i].queue)
}
return nil
}
func (qs *QSHandle) Release() error {
for i := 0; i < qs.maxThNum; i++ {
close(qs.thPool[i].queue)
}
return nil
}
func softRecoverPubkey(hash []byte, r []byte, s []byte, v byte) ([]byte, error) {
var (
result = make([]byte, 65)
sig = make([]byte, 65)
)
copy(sig[32-len(r):32], r)
copy(sig[64-len(s):64], s)
sig[64] = v
pub, err := crypto.Ecrecover(hash[:], sig)
if err != nil {
return nil, ErrSignCheckFailed
}
copy(result[:], pub[0:])
return result, nil
}
func (qs *QSHandle) ASyncValidateSign(tx *Transaction, hash []byte, r []byte, s []byte, v byte) error {
rs := RecoverPubkey{Tx:tx, Hash: make([]byte, 32), Sig: make([]byte, 65)}
copy(rs.Hash, hash)
copy(rs.Sig[32-len(r):32], r)
copy(rs.Sig[64-len(s):64], s)
rs.Sig[64] = v
qs.postToSoft(&rs)
return nil
}
func (qs *QSHandle) ValidateSign(hash []byte, r []byte, s []byte, v byte) ([]byte, error) {
return softRecoverPubkey(hash, r, s, v)
}
func init() {
QSGetInstance().Init()
}