-
Notifications
You must be signed in to change notification settings - Fork 0
/
suite.go
117 lines (90 loc) · 2.94 KB
/
suite.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
package secp256k1
import (
"crypto/cipher"
crypto "github.com/ThotaGopichandThota/gn-crypto1"
logger "github.com/ThotaGopichandThota/gn-logger1"
secp "github.com/decred/dcrd/dcrec/secp256k1/v4"
)
var log = logger.GetOrCreate("crypto/signing/secp256k1")
var _ crypto.Group = (*secp256k1Suite)(nil)
var _ crypto.Random = (*secp256k1Suite)(nil)
var _ crypto.Suite = (*secp256k1Suite)(nil)
// Secp256k1Str suite string name
const Secp256k1Str = "secp256k1"
// These constants define the lengths of serialized public keys.
const (
PrivKeyBytesLen = 32
PubKeyBytesLenCompressed = 33
)
type secp256k1Suite struct{}
// NewSecp256k1 returns a wrapper over secp256k1 suite
func NewSecp256k1() *secp256k1Suite {
return &secp256k1Suite{}
}
// CreateKeyPair creates a scalar and a point pair that can be used in asymmetric cryptography
func (s *secp256k1Suite) CreateKeyPair() (crypto.Scalar, crypto.Point) {
privKey, err := secp.GeneratePrivateKey()
if err != nil {
panic("could not create secp256k1 key pair: " + err.Error())
}
return &secp256k1Scalar{*privKey}, &secp256k1Point{*privKey.PubKey()}
}
// String returns the string for the group
func (s *secp256k1Suite) String() string {
return Secp256k1Str
}
// ScalarLen returns the maximum length of scalars in bytes
func (s *secp256k1Suite) ScalarLen() int {
return PrivKeyBytesLen
}
// CreateScalar creates a new Scalar
func (s *secp256k1Suite) CreateScalar() crypto.Scalar {
privKey, err := secp.GeneratePrivateKey()
if err != nil {
panic("could not create secp256k1 key pair: " + err.Error())
}
return &secp256k1Scalar{*privKey}
}
// PointLen returns the max length of point in nb of bytes
func (s *secp256k1Suite) PointLen() int {
return PubKeyBytesLenCompressed
}
// CreatePoint creates a new point
func (s *secp256k1Suite) CreatePoint() crypto.Point {
_, publicKey := s.CreateKeyPair()
return publicKey
}
// CreatePointForScalar creates a new point corresponding to the given scalar
func (s *secp256k1Suite) CreatePointForScalar(scalar crypto.Scalar) (crypto.Point, error) {
privateKey, ok := scalar.GetUnderlyingObj().(secp.PrivateKey)
if !ok {
return nil, crypto.ErrInvalidPrivateKey
}
return &secp256k1Point{*privateKey.PubKey()}, nil
}
// CheckPointValid returns nil
func (s *secp256k1Suite) CheckPointValid(pointBytes []byte) error {
if len(pointBytes) != s.PointLen() {
return crypto.ErrInvalidParam
}
point := s.CreatePoint()
err := point.UnmarshalBinary(pointBytes)
if err != nil {
return err
}
return nil
}
// RandomStream returns nil
func (s *secp256k1Suite) RandomStream() cipher.Stream {
log.Error("secp256k1Suite", "RandomStream not implemented")
return nil
}
// GetUnderlyingSuite returns nil
func (s *secp256k1Suite) GetUnderlyingSuite() interface{} {
log.Error("secp256k1Suite", "GetUnderlyingSuite not implemented")
return nil
}
// IsInterfaceNil returns true if there is no value under the interface
func (s *secp256k1Suite) IsInterfaceNil() bool {
return s == nil
}