/
privatekey.go
76 lines (58 loc) · 2.12 KB
/
privatekey.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
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package privacy
import (
"bytes"
"unsafe"
"github.com/33cn/chain33/common/crypto"
"github.com/33cn/chain33/common/crypto/sha3"
"github.com/33cn/chain33/common/ed25519/edwards25519"
)
// PrivKeyPrivacy struct data type
type PrivKeyPrivacy [privateKeyLen]byte
// Bytes convert to bytes
func (privKey PrivKeyPrivacy) Bytes() []byte {
return privKey[:]
}
// Sign signature trasaction
func (privKey PrivKeyPrivacy) Sign(msg []byte) crypto.Signature {
temp := new([64]byte)
randomScalar := new([32]byte)
copy(temp[:], crypto.CRandBytes(64))
edwards25519.ScReduce(randomScalar, temp)
var sigcommdata sigcommArray
sigcommPtr := (*sigcomm)(unsafe.Pointer(&sigcommdata))
copy(sigcommPtr.pubkey[:], privKey.PubKey().Bytes())
hash := sha3.Sum256(msg)
copy(sigcommPtr.hash[:], hash[:])
var K edwards25519.ExtendedGroupElement
edwards25519.GeScalarMultBase(&K, randomScalar)
K.ToBytes((*[KeyLen32]byte)(unsafe.Pointer(&sigcommPtr.comm[0])))
var sigOnetime SignatureOnetime
addr32 := (*[KeyLen32]byte)(unsafe.Pointer(&sigOnetime))
hash2scalar(sigcommdata[:], addr32)
addr32Latter := (*[KeyLen32]byte)(unsafe.Pointer(&sigOnetime[KeyLen32]))
addr32Priv := (*[KeyLen32]byte)(unsafe.Pointer(&privKey))
edwards25519.ScMulSub(addr32Latter, addr32, addr32Priv, randomScalar)
return sigOnetime
}
// PubKey get public key
func (privKey PrivKeyPrivacy) PubKey() crypto.PubKey {
var pubKeyPrivacy PubKeyPrivacy
addr32 := (*[KeyLen32]byte)(unsafe.Pointer(&privKey.Bytes()[0]))
addr64 := (*[privateKeyLen]byte)(unsafe.Pointer(&privKey.Bytes()[0]))
var A edwards25519.ExtendedGroupElement
pubKeyAddr32 := (*[KeyLen32]byte)(unsafe.Pointer(&pubKeyPrivacy))
edwards25519.GeScalarMultBase(&A, addr32)
A.ToBytes(pubKeyAddr32)
copy(addr64[KeyLen32:], pubKeyAddr32[:])
return pubKeyPrivacy
}
// Equals check equals
func (privKey PrivKeyPrivacy) Equals(other crypto.PrivKey) bool {
if otherEd, ok := other.(PrivKeyPrivacy); ok {
return bytes.Equal(privKey[:], otherEd[:])
}
return false
}