-
Notifications
You must be signed in to change notification settings - Fork 4
/
batch_threshsign.go
187 lines (161 loc) · 5.23 KB
/
batch_threshsign.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package threshsign
/*
#include "enclave_threshsign/untrusted/BatchThreshsign.h"
#cgo LDFLAGS: -L${SRCDIR}/enclave_threshsign -lthreshsign
#cgo CFLAGS: -I/opt/sgxsdk/include
*/
import "C"
import (
"crypto/sha256"
"unsafe"
"github.com/ibalajiarun/go-consensus/peer/peerpb"
)
type ThreshsignRequest struct {
Message []byte
Counter uint64
SigShares map[peerpb.PeerID][]byte
CombinedSig []byte
}
func (s *ThreshsignEnclave) BatchSign(reqBatch []ThreshsignRequest) [][]byte {
hashes := make([]byte, len(reqBatch)*32)
counterIds := make([]C.uint32_t, len(reqBatch))
counters := make([]C.uint64_t, len(reqBatch))
sigBatch := make([]byte, len(reqBatch)*256)
sigLens := make([]C.unsigned, len(reqBatch))
for i, r := range reqBatch {
hash := sha256.Sum256(r.Message)
copy(hashes[32*i:32*(i+1)], hash[:])
counters[i] = C.uint64_t(r.Counter)
sigLens[i] = 256
}
ret := C.threshsign_batch_sign(
s.cPtr,
C.unsigned(len(reqBatch)),
(*C.char)(unsafe.Pointer(&hashes[0])),
(*C.uint32_t)(&counterIds[0]),
(*C.uint64_t)(&counters[0]),
(*C.char)(unsafe.Pointer(&sigBatch[0])),
(*C.unsigned)(unsafe.Pointer(&sigLens[0])),
)
if ret != 0 {
panic("Unable to sign")
}
retSigs := make([][]byte, len(reqBatch))
for i := range retSigs {
sigLen := int(sigLens[i])
retSigs[i] = sigBatch[256*i : (256*i)+sigLen]
}
return retSigs
}
func (s *ThreshsignEnclave) BatchSignUsingEnclave(reqBatch []ThreshsignRequest) [][]byte {
hashes := make([]byte, len(reqBatch)*32)
counterIds := make([]C.uint32_t, len(reqBatch))
counters := make([]C.uint64_t, len(reqBatch))
sigBatch := make([]byte, len(reqBatch)*256)
sigLens := make([]C.unsigned, len(reqBatch))
for i, r := range reqBatch {
hash := sha256.Sum256(r.Message)
copy(hashes[32*i:32*(i+1)], hash[:])
counterIds[i] = C.uint32_t(r.Counter)
counters[i] = C.uint64_t(r.Counter)
sigLens[i] = 256
}
ret := C.threshsign_batch_sign_using_enclave_batch(
s.cPtr,
C.unsigned(len(reqBatch)),
(*C.char)(unsafe.Pointer(&hashes[0])),
(*C.uint32_t)(&counterIds[0]),
(*C.uint64_t)(&counters[0]),
(*C.char)(unsafe.Pointer(&sigBatch[0])),
(*C.unsigned)(unsafe.Pointer(&sigLens[0])),
)
if ret != 0 {
panic("Unable to sign using enclave")
}
retSigs := make([][]byte, len(reqBatch))
for i := range retSigs {
sigLen := int(sigLens[i])
retSigs[i] = sigBatch[256*i : (256*i)+sigLen]
}
return retSigs
}
func (s *ThreshsignEnclave) BatchAggregateSigAndVerify(reqBatch []ThreshsignRequest) [][]byte {
hashes := make([]byte, len(reqBatch)*32)
counterIds := make([]C.uint32_t, len(reqBatch))
counters := make([]C.uint64_t, len(reqBatch))
sigBatch := make([]byte, len(reqBatch)*256)
sigLens := make([]C.unsigned, len(reqBatch))
cSigsharesBatch := make([]byte, 0, len(reqBatch))
cSigshareBatchLens := make([]C.unsigned, 0, len(reqBatch))
cSigshareBatchCounts := make([]C.unsigned, 0, len(reqBatch))
cSigshareLens := make([]C.unsigned, 0, len(reqBatch))
cSigindex := make([]C.int, 0, len(reqBatch))
for i, r := range reqBatch {
hash := sha256.Sum256(r.Message)
copy(hashes[32*i:32*(i+1)], hash[:])
counters[i] = C.uint64_t(r.Counter)
sigLens[i] = 256
totalLen := 0
for idx, sigshare := range r.SigShares {
cSigsharesBatch = append(cSigsharesBatch, sigshare...)
cSigshareLens = append(cSigshareLens, C.unsigned(len(sigshare)))
cSigindex = append(cSigindex, C.int(idx+1))
totalLen += len(sigshare)
}
cSigshareBatchLens = append(cSigshareBatchLens, C.unsigned(totalLen))
cSigshareBatchCounts = append(cSigshareBatchCounts, C.unsigned(len(r.SigShares)))
}
ret := C.threshsign_batch_aggregate_and_verify(
s.cPtr,
C.unsigned(len(reqBatch)),
(*C.char)(unsafe.Pointer(&hashes[0])),
(*C.uint32_t)(&counterIds[0]),
(*C.uint64_t)(&counters[0]),
(*C.char)(unsafe.Pointer(&cSigsharesBatch[0])),
(*C.unsigned)(&cSigshareBatchLens[0]),
(*C.unsigned)(&cSigshareBatchCounts[0]),
(*C.unsigned)(&cSigshareLens[0]),
(*C.int)(&cSigindex[0]),
(*C.char)(unsafe.Pointer(&sigBatch[0])),
(*C.unsigned)(unsafe.Pointer(&sigLens[0])),
)
if ret != 0 {
panic("Unable to aggregate and verify")
}
retSigs := make([][]byte, len(reqBatch))
for i := range retSigs {
sigLen := int(sigLens[i])
retSigs[i] = sigBatch[256*i : (256*i)+sigLen]
// fmt.Printf("agg_sig %v %s\n", sigLen, retSigs[i])
}
return retSigs
}
func (s *ThreshsignEnclave) BatchVerify(reqBatch []ThreshsignRequest) bool {
hashes := make([]byte, len(reqBatch)*32)
counterIds := make([]C.uint32_t, len(reqBatch))
counters := make([]C.uint64_t, len(reqBatch))
sigBatch := make([]byte, 0, len(reqBatch))
sigLens := make([]C.unsigned, len(reqBatch))
for i, r := range reqBatch {
hash := sha256.Sum256(r.Message)
copy(hashes[32*i:32*(i+1)], hash[:])
counters[i] = C.uint64_t(r.Counter)
// fmt.Printf("verify_agg_sig %p %v %s\n", s.cPtr, len(r.CombinedSig), r.CombinedSig)
// os.Stdout.Sync()
sigBatch = append(sigBatch, r.CombinedSig...)
sigLens[i] = C.unsigned(len(r.CombinedSig))
}
ret := C.threshsign_batch_verify(
s.cPtr,
C.unsigned(len(reqBatch)),
(*C.char)(unsafe.Pointer(&hashes[0])),
(*C.uint32_t)(&counterIds[0]),
(*C.uint64_t)(&counters[0]),
(*C.char)(unsafe.Pointer(&sigBatch[0])),
(*C.unsigned)(unsafe.Pointer(&sigLens[0])),
)
if ret != 0 {
panic("Unable to verify")
}
return true
}