-
Notifications
You must be signed in to change notification settings - Fork 28
/
bliss.go
313 lines (275 loc) · 8.44 KB
/
bliss.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package bliss
import (
"crypto/rand"
"io"
"github.com/HcashOrg/bliss"
"github.com/HcashOrg/bliss/poly"
"github.com/HcashOrg/bliss/sampler"
hccrypto "github.com/HcashOrg/hcd/crypto"
)
var pqcTypeBliss = 4
var BlissDSA = newBlissDSA()
type blissDSA struct {
// Private keys
newPrivateKey func(s1, s2, a *poly.PolyArray) hccrypto.PrivateKey
privKeyFromBytes func(pk []byte) (hccrypto.PrivateKey, hccrypto.PublicKey)
privKeyBytesLen func() int
// Public keys
newPublicKey func(a *poly.PolyArray) hccrypto.PublicKey
parsePubKey func(pubKeyStr []byte) (hccrypto.PublicKey, error)
pubKeyBytesLen func() int
// Signatures
newSignature func(z1, z2 *poly.PolyArray, c []uint32) hccrypto.Signature
parseDERSignature func(sigStr []byte) (hccrypto.Signature, error)
parseSignature func(sigStr []byte) (hccrypto.Signature, error)
recoverCompact func(signature, hash []byte) (hccrypto.PublicKey, bool, error)
//
generateKey func(rand io.Reader) (hccrypto.PrivateKey, hccrypto.PublicKey, error)
sign func(priv hccrypto.PrivateKey, hash []byte) (hccrypto.Signature, error)
verify func(pub hccrypto.PublicKey, hash []byte, sig hccrypto.Signature) bool
// Symmetric cipher encryption
//generateSharedSecret func(privkey []byte, x, y *big.Int) []byte
//encrypt func(x, y *big.Int, in []byte) ([]byte, error)
//decrypt func(privkey []byte, in []byte) ([]byte, error)
}
// Private keys
func (sp blissDSA) NewPrivateKey(s1, s2, a *poly.PolyArray) hccrypto.PrivateKey {
return sp.newPrivateKey(s1, s2, a)
}
func (sp blissDSA) PrivKeyFromBytes(pk []byte) (hccrypto.PrivateKey, hccrypto.PublicKey) {
return sp.privKeyFromBytes(pk)
}
func (sp blissDSA) PrivKeyBytesLen() int {
return sp.privKeyBytesLen()
}
// Public keys
func (sp blissDSA) NewPublicKey(a *poly.PolyArray) hccrypto.PublicKey {
return sp.newPublicKey(a)
}
func (sp blissDSA) ParsePubKey(pubKeyStr []byte) (hccrypto.PublicKey, error) {
return sp.parsePubKey(pubKeyStr)
}
func (sp blissDSA) PubKeyBytesLen() int {
return sp.pubKeyBytesLen()
}
// Signatures
func (sp blissDSA) NewSignature(z1, z2 *poly.PolyArray, c []uint32) hccrypto.Signature {
return sp.newSignature(z1, z2, c)
}
func (sp blissDSA) ParseDERSignature(sigStr []byte) (hccrypto.Signature, error) {
return sp.parseDERSignature(sigStr)
}
func (sp blissDSA) ParseSignature(sigStr []byte) (hccrypto.Signature, error) {
return sp.parseSignature(sigStr)
}
func (sp blissDSA) RecoverCompact(signature, hash []byte) (hccrypto.PublicKey, bool,
error) {
return sp.recoverCompact(signature, hash)
}
// ECDSA
func (sp blissDSA) GenerateKey(rand io.Reader) (hccrypto.PrivateKey, hccrypto.PublicKey,
error) {
return sp.generateKey(rand)
}
func (sp blissDSA) Sign(priv hccrypto.PrivateKey, hash []byte) (hccrypto.Signature, error) {
return sp.sign(priv, hash)
}
func (sp blissDSA) Verify(pub hccrypto.PublicKey, hash []byte, sig hccrypto.Signature) bool {
return sp.verify(pub, hash, sig)
}
func newBlissDSA() DSA {
var bliss DSA = &blissDSA{
// Private keys
newPrivateKey: func(s1, s2, a *poly.PolyArray) hccrypto.PrivateKey {
if s1 == nil || s2 == nil || a == nil {
return nil
}
n := s1.Param().N
s1data := s1.GetData()
s2data := s2.GetData()
ret := make([]byte, n*2+1)
ret[0] = byte(s1.Param().Version)
s1part := ret[1 : 1+n]
s2part := ret[1+n:]
for i := 0; i < int(n); i++ {
s1part[i] = byte(s1data[i] + 4)
s2part[i] = byte(s2data[i] + 4)
}
blissPK, err := bliss.DecodePrivateKey(ret)
if err != nil {
return nil
}
return &PrivateKey{
PrivateKey: *blissPK,
}
},
privKeyFromBytes: func(pk []byte) (hccrypto.PrivateKey, hccrypto.PublicKey) {
blissPK, err := bliss.DeserializePrivateKey(pk)
if err != nil {
return nil, nil
}
var privateKey PrivateKey
var publicKey PublicKey
privateKey.PrivateKey = *blissPK
publicKey.PublicKey = *(blissPK.PublicKey())
return privateKey, publicKey
},
privKeyBytesLen: func() int {
return BlissPrivKeyLen
},
// Public keys
newPublicKey: func(a *poly.PolyArray) hccrypto.PublicKey {
if a == nil {
return nil
}
n := a.Param().N
data := a.GetData()
ret := make([]byte, n*2+1)
ret[0] = byte(a.Param().Version)
for i := 0; i < int(n); i++ {
ret[i*2+1] = byte(uint16(data[i]) >> 8)
ret[i*2+2] = byte(uint16(data[i]) & 0xff)
}
blissPK, err := bliss.DecodePublicKey(ret)
if err != nil {
return nil
}
return &PublicKey{
PublicKey: *blissPK,
}
},
parsePubKey: func(pubKeyStr []byte) (hccrypto.PublicKey, error) {
blissPK, err := bliss.DeserializePublicKey(pubKeyStr)
if err != nil {
return nil, err
}
return &PublicKey{
PublicKey: *blissPK,
}, nil
},
pubKeyBytesLen: func() int {
return BlissPubKeyLen
},
// Signatures
newSignature: func(z1, z2 *poly.PolyArray, c []uint32) hccrypto.Signature {
if z1 == nil || z2 == nil || c == nil {
return nil
}
n := z1.Param().N
kappa := z1.Param().Kappa
z1len := n * 2
z2len := n + n/8
clen := 2 * kappa
z1data := z1.GetData()
z2data := z2.GetData()
cdata := c
ret := make([]byte, 1+z1len+z2len+clen)
ret[0] = byte(z1.Param().Version)
z1part := ret[1 : 1+z1len]
z2part := ret[1+z1len : 1+z1len+z2len]
cpart := ret[1+z1len+z2len:]
// It is easy to store z1. Take each element as
// an uint16, although they are actually a littble
// bit smaller than 16 bits.
for i := 0; i < int(n); i++ {
tmp := z1.NumModQ(z1data[i])
z1part[i*2] = byte(uint16(tmp) >> 8)
z1part[i*2+1] = byte(uint16(tmp) & 0xff)
}
// z2 is much smaller than z1, bounded by p/2
// An additional bit array is used to store the signs
z2left := z2part[:n]
z2right := z2part[n:]
for i := 0; i < int(n); i++ {
z2left[i] = byte(uint16(bliss.Abs(z2data[i])) & 0xff)
}
for i := 0; i < int(n)/8; i++ {
tmp := byte(0)
for j := 0; j < 8; j++ {
tmp <<= 1
if z2data[i*8+j] > 0 {
tmp += 1
}
}
// Each extra bit takes a byte array of size n/8
z2right[i] = tmp
}
// c is represented by a list of kappa integers in [0,n)
// For simplicity, we use 2 bytes to store each index.
for i := 0; i < int(kappa); i++ {
cpart[i*2] = byte(uint16(cdata[i]) >> 8)
cpart[i*2+1] = byte(uint16(cdata[i]) & 0xff)
}
sig, err := bliss.DecodeSignature(ret)
if err != nil {
return nil
}
return &Signature{
Signature: *sig,
}
},
parseDERSignature: func(sigStr []byte) (hccrypto.Signature, error) {
sig, err := bliss.DeserializeBlissSignature(sigStr)
if err != nil {
return nil, err
}
return &Signature{
Signature: *sig,
}, nil
},
parseSignature: func(sigStr []byte) (hccrypto.Signature, error) {
sig, err := bliss.DeserializeBlissSignature(sigStr)
if err != nil {
return nil, err
}
return &Signature{
Signature: *sig,
}, nil
},
recoverCompact: func(signature, hash []byte) (hccrypto.PublicKey, bool, error) {
return nil, false, nil
},
generateKey: func(rand io.Reader) (hccrypto.PrivateKey, hccrypto.PublicKey, error) {
seed := make([]byte, sampler.SHA_512_DIGEST_LENGTH)
rand.Read(seed)
entropy, err := sampler.NewEntropy(seed)
if err != nil {
return nil, nil, err
}
blissPK, err := bliss.GeneratePrivateKey(BlissVersion, entropy)
if err != nil {
return nil, nil, err
}
privateKey := &PrivateKey{
PrivateKey: *blissPK,
}
publicKey := &PublicKey{
PublicKey: *blissPK.PublicKey(),
}
return privateKey, publicKey, nil
},
sign: func(priv hccrypto.PrivateKey, hash []byte) (hccrypto.Signature, error) {
seed := make([]byte, sampler.SHA_512_DIGEST_LENGTH)
rand.Read(seed)
entropy, err := sampler.NewEntropy(seed)
if err != nil {
return nil, err
}
priv1 := priv.(PrivateKey)
sig, err := priv1.Sign(hash, entropy)
if err != nil {
return nil, err
}
return &Signature{
Signature: *sig,
}, nil
},
verify: func(pub hccrypto.PublicKey, hash []byte, sig hccrypto.Signature) bool {
signature := sig.(*Signature)
blissSig := signature.Signature
result, _ := pub.(*PublicKey).Verify(hash, &blissSig)
return result
},
}
return bliss.(DSA)
}