-
Notifications
You must be signed in to change notification settings - Fork 0
/
scalar.go
206 lines (159 loc) · 5.68 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
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
package ed25519
import (
"bytes"
"crypto/ed25519"
crypto "github.com/ThotaGopichandThota/gn-crypto1"
"github.com/ThotaGopichandThota/gn-core3/core/check"
)
var _ crypto.Scalar = (*ed25519Scalar)(nil)
type ed25519Scalar struct {
ed25519.PrivateKey
}
// Equal checks if the underlying private key inside the scalar objects contain the same bytes
func (es *ed25519Scalar) Equal(s crypto.Scalar) (bool, error) {
privateKey, err := es.getPrivateKeyFromScalar(s)
if err != nil {
return false, err
}
return bytes.Equal(privateKey, es.PrivateKey), nil
}
// Set sets the underlying private key inside the scalar to the private key of the provided scalar
func (es *ed25519Scalar) Set(s crypto.Scalar) error {
privateKey, err := es.getPrivateKeyFromScalar(s)
if err != nil {
return err
}
es.PrivateKey = privateKey
return nil
}
// Clone creates a new Scalar with same value as receiver
func (es *ed25519Scalar) Clone() crypto.Scalar {
scalarBytes := make([]byte, len(es.PrivateKey))
copy(scalarBytes, es.PrivateKey)
return &ed25519Scalar{scalarBytes}
}
// GetUnderlyingObj returns the object the implementation wraps
func (es *ed25519Scalar) GetUnderlyingObj() interface{} {
err := isKeyValid(es.PrivateKey)
if err != nil {
log.Error("ed25519Scalar",
"message", "GetUnderlyingObj invalid private key construction")
return nil
}
return es.PrivateKey
}
// MarshalBinary encodes the receiver into a binary form and returns the result.
func (es *ed25519Scalar) MarshalBinary() ([]byte, error) {
err := isKeyValid(es.PrivateKey)
if err != nil {
return nil, err
}
return es.PrivateKey, nil
}
// UnmarshalBinary decodes a scalar from its byte array representation and sets the receiver to this value
func (es *ed25519Scalar) UnmarshalBinary(s []byte) error {
switch len(s) {
case ed25519.SeedSize:
es.PrivateKey = ed25519.NewKeyFromSeed(s)
case ed25519.PrivateKeySize:
err := isKeyValid(s)
if err != nil {
return err
}
es.PrivateKey = s
default:
return crypto.ErrInvalidPrivateKey
}
return nil
}
// SetInt64 is not needed for this use case, should be removed if possible
func (es *ed25519Scalar) SetInt64(_ int64) {
log.Error("ed25519Scalar",
"message", "SetInt64 for ed25519Scalar is not implemented, should not be called")
}
// Zero is not needed for this use case, should be removed if possible
func (es *ed25519Scalar) Zero() crypto.Scalar {
log.Error("ed25519Scalar",
"message", "SetInt64 for ed25519Scalar is not implemented, should not be called")
return nil
}
// Add is not needed for this use case, should be removed if possible
func (es *ed25519Scalar) Add(_ crypto.Scalar) (crypto.Scalar, error) {
log.Error("ed25519Scalar",
"message", "Add for ed25519Scalar is not implemented, should not be called")
return nil, crypto.ErrNotImplemented
}
// Sub is not needed for this use case, should be removed if possible
func (es *ed25519Scalar) Sub(_ crypto.Scalar) (crypto.Scalar, error) {
log.Error("ed25519Scalar",
"message", "Sub for ed25519Scalar is not implemented, should not be called")
return nil, crypto.ErrNotImplemented
}
// Neg is not needed for this use case, should be removed if possible
func (es *ed25519Scalar) Neg() crypto.Scalar {
log.Error("ed25519Scalar",
"message", "Neg for ed25519Scalar is not implemented, should not be called")
return nil
}
// One is not needed for this use case, should be removed if possible
func (es *ed25519Scalar) One() crypto.Scalar {
log.Error("ed25519Scalar",
"message", "One for ed25519Scalar is not implemented, should not be called")
return nil
}
// Mul returns the modular product of receiver with scalar s given as parameter
func (es *ed25519Scalar) Mul(_ crypto.Scalar) (crypto.Scalar, error) {
log.Error("ed25519Scalar",
"message", "Mul for ed25519Scalar is not implemented, should not be called")
return nil, crypto.ErrNotImplemented
}
// Div returns the modular division between receiver and scalar s given as parameter
func (es *ed25519Scalar) Div(_ crypto.Scalar) (crypto.Scalar, error) {
log.Error("ed25519Scalar",
"message", "Div for ed25519Scalar is not implemented, should not be called")
return nil, crypto.ErrNotImplemented
}
// Inv returns the modular inverse of scalar s given as parameter
func (es *ed25519Scalar) Inv(_ crypto.Scalar) (crypto.Scalar, error) {
log.Error("ed25519Scalar",
"message", "Inv for ed25519Scalar is not implemented, should not be called")
return nil, crypto.ErrNotImplemented
}
// Pick returns a fresh random or pseudo-random scalar
func (es *ed25519Scalar) Pick() (crypto.Scalar, error) {
log.Error("ed25519Scalar",
"message", "Pick for ed25519Scalar is not implemented, should not be called")
return nil, crypto.ErrNotImplemented
}
// SetBytes sets the scalar from a byte-slice,
// reducing if necessary to the appropriate modulus.
func (es *ed25519Scalar) SetBytes(_ []byte) (crypto.Scalar, error) {
log.Error("ed25519Scalar",
"message", "SetBytes for ed25519Scalar is not implemented, should not be called")
return nil, crypto.ErrNotImplemented
}
// IsInterfaceNil returns true if there is no value under the interface
func (es *ed25519Scalar) IsInterfaceNil() bool {
return es == nil
}
func (es *ed25519Scalar) getPrivateKeyFromScalar(s crypto.Scalar) (ed25519.PrivateKey, error) {
if check.IfNil(s) {
return nil, crypto.ErrNilParam
}
privateKey, ok := s.GetUnderlyingObj().(ed25519.PrivateKey)
if !ok {
return nil, crypto.ErrInvalidPrivateKey
}
return privateKey, nil
}
func isKeyValid(key ed25519.PrivateKey) error {
if len(key) != ed25519.PrivateKeySize {
return crypto.ErrWrongPrivateKeySize
}
seed := key.Seed()
validationKey := ed25519.NewKeyFromSeed(seed)
if !bytes.Equal(key, validationKey) {
return crypto.ErrWrongPrivateKeyStructure
}
return nil
}