-
Notifications
You must be signed in to change notification settings - Fork 0
/
scalar.go
144 lines (109 loc) · 3.28 KB
/
scalar.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
package secp256k1
import (
crypto "github.com/ThotaGopichandThota/gn-crypto1"
"github.com/ThotaGopichandThota/gn-core3/core/check"
secp "github.com/decred/dcrd/dcrec/secp256k1/v4"
)
var _ crypto.Scalar = (*secp256k1Scalar)(nil)
type secp256k1Scalar struct {
secp.PrivateKey
}
// GetUnderlyingObj returns the object the implementation wraps
func (e *secp256k1Scalar) GetUnderlyingObj() interface{} {
return e.PrivateKey
}
// MarshalBinary transforms the Scalar into a byte array
func (e *secp256k1Scalar) MarshalBinary() ([]byte, error) {
return e.PrivateKey.Serialize(), nil
}
// UnmarshalBinary recreates the Scalar from a byte array
func (e *secp256k1Scalar) UnmarshalBinary(key []byte) error {
privKey := secp.PrivKeyFromBytes(key)
e.PrivateKey = *privKey
return nil
}
// Equal tests if receiver is equal with the scalar s given as parameter.
// Both scalars need to be derived from the same Group
func (e *secp256k1Scalar) Equal(s crypto.Scalar) (bool, error) {
if check.IfNil(s) {
return false, crypto.ErrNilParam
}
scalar, ok := s.(*secp256k1Scalar)
if !ok {
return false, crypto.ErrInvalidPrivateKey
}
return e.PrivateKey.PubKey().IsEqual(scalar.PubKey()), nil
}
// Set sets the receiver to Scalar s given as parameter
func (e *secp256k1Scalar) Set(s crypto.Scalar) error {
if check.IfNil(s) {
return crypto.ErrNilParam
}
scalar, ok := s.(*secp256k1Scalar)
if !ok {
return crypto.ErrInvalidPrivateKey
}
e.PrivateKey = scalar.PrivateKey
return nil
}
// Clone creates a new Scalar with same value as receiver
func (e *secp256k1Scalar) Clone() crypto.Scalar {
if e == nil {
return nil
}
e2 := &secp256k1Scalar{}
scalarBytes, _ := e.MarshalBinary()
_ = e2.UnmarshalBinary(scalarBytes)
return e2
}
// SetInt64 does nothing
func (e *secp256k1Scalar) SetInt64(_ int64) {
log.Error("secp256k1Scalar", "SetInt64 not implemented")
}
// Zero returns nil
func (e *secp256k1Scalar) Zero() crypto.Scalar {
log.Error("secp256k1Scalar", "Zero not implemented")
return nil
}
// Add returns nil
func (e *secp256k1Scalar) Add(_ crypto.Scalar) (crypto.Scalar, error) {
return nil, crypto.ErrNotImplemented
}
// Sub returns nil
func (e *secp256k1Scalar) Sub(_ crypto.Scalar) (crypto.Scalar, error) {
return nil, crypto.ErrNotImplemented
}
// Neg returns nil
func (e *secp256k1Scalar) Neg() crypto.Scalar {
log.Error("secp256k1Scalar", "Neg not implemented")
return nil
}
// One returns nil
func (e *secp256k1Scalar) One() crypto.Scalar {
log.Error("secp256k1Scalar", "One not implemented")
return nil
}
// Mul returns nil
func (e *secp256k1Scalar) Mul(_ crypto.Scalar) (crypto.Scalar, error) {
return nil, crypto.ErrNotImplemented
}
// Div returns nil
func (e *secp256k1Scalar) Div(_ crypto.Scalar) (crypto.Scalar, error) {
return nil, crypto.ErrNotImplemented
}
// Inv returns nil
func (e *secp256k1Scalar) Inv(_ crypto.Scalar) (crypto.Scalar, error) {
return nil, crypto.ErrNotImplemented
}
// Pick returns nil
func (e *secp256k1Scalar) Pick() (crypto.Scalar, error) {
return nil, crypto.ErrNotImplemented
}
// SetBytes returns nil
func (e *secp256k1Scalar) SetBytes(_ []byte) (crypto.Scalar, error) {
return nil, crypto.ErrNotImplemented
}
// IsInterfaceNil returns true if there is no value under the interface
func (e *secp256k1Scalar) IsInterfaceNil() bool {
return e == nil
}